2 #include <xcb/xcb_icccm.h>
13 void handle_event(xcb_generic_event_t *evt)
15 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
20 case XCB_DESTROY_NOTIFY:
23 case XCB_UNMAP_NOTIFY:
26 case XCB_CLIENT_MESSAGE:
29 case XCB_CONFIGURE_REQUEST:
30 configure_request(evt);
32 case XCB_PROPERTY_NOTIFY:
35 case XCB_ENTER_NOTIFY:
38 case XCB_MOTION_NOTIFY:
45 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
51 void map_request(xcb_generic_event_t *evt)
53 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
55 PRINTF("map request %X\n", e->window);
57 manage_window(mon, mon->desk, e->window);
60 void configure_request(xcb_generic_event_t *evt)
62 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
64 PRINTF("configure request %X\n", e->window);
67 bool is_managed = locate_window(e->window, &loc);
69 if (!is_managed || is_floating(loc.node->client)) {
74 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
75 mask |= XCB_CONFIG_WINDOW_X;
78 loc.node->client->floating_rectangle.x = e->x;
81 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
82 mask |= XCB_CONFIG_WINDOW_Y;
85 loc.node->client->floating_rectangle.y = e->y;
88 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
89 mask |= XCB_CONFIG_WINDOW_WIDTH;
90 values[i++] = e->width;
92 loc.node->client->floating_rectangle.width = e->width;
95 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
96 mask |= XCB_CONFIG_WINDOW_HEIGHT;
97 values[i++] = e->height;
99 loc.node->client->floating_rectangle.height = e->height;
102 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
103 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
104 values[i++] = e->border_width;
107 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
108 mask |= XCB_CONFIG_WINDOW_SIBLING;
109 values[i++] = e->sibling;
112 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
113 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
114 values[i++] = e->stack_mode;
117 xcb_configure_window(dpy, e->window, mask, values);
119 window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
121 xcb_configure_notify_event_t evt;
122 xcb_rectangle_t rect;
124 xcb_window_t win = loc.node->client->window;
126 if (is_tiled(loc.node->client)) {
127 rect = loc.node->client->tiled_rectangle;
130 rect = loc.monitor->rectangle;
134 evt.response_type = XCB_CONFIGURE_NOTIFY;
137 evt.above_sibling = XCB_NONE;
140 evt.width = rect.width;
141 evt.height = rect.height;
142 evt.border_width = bw;
143 evt.override_redirect = false;
145 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
149 void destroy_notify(xcb_generic_event_t *evt)
151 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
153 PRINTF("destroy notify %X\n", e->window);
156 if (locate_window(e->window, &loc)) {
157 remove_node(loc.desktop, loc.node);
158 arrange(loc.monitor, loc.desktop);
162 void unmap_notify(xcb_generic_event_t *evt)
164 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
166 PRINTF("unmap notify %X\n", e->window);
169 if (locate_window(e->window, &loc)) {
170 remove_node(loc.desktop, loc.node);
171 arrange(loc.monitor, loc.desktop);
175 void property_notify(xcb_generic_event_t *evt)
177 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
178 xcb_icccm_wm_hints_t hints;
180 /* PRINTF("property notify %X\n", e->window); */
182 if (e->atom != XCB_ATOM_WM_HINTS)
186 if (locate_window(e->window, &loc)
187 && xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1)
188 set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
191 void client_message(xcb_generic_event_t *evt)
193 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
195 PRINTF("client message %X %u\n", e->window, e->type);
197 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
199 if (ewmh_locate_desktop(e->data.data32[0], &loc))
200 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
205 if (!locate_window(e->window, &loc))
208 if (e->type == ewmh->_NET_WM_STATE) {
209 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
210 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
211 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
212 if (loc.node == mon->desk->focus)
214 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
215 set_fullscreen(loc.desktop, loc.desktop->focus, false);
216 arrange(loc.monitor, loc.desktop);
218 focus_node(loc.monitor, loc.desktop, loc.node);
219 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
221 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
222 transfer_node(loc.monitor, loc.desktop, dloc.monitor, dloc.desktop, loc.node);
226 void focus_in(xcb_generic_event_t *evt)
228 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
230 /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
232 if (e->mode == XCB_NOTIFY_MODE_GRAB
233 || e->mode == XCB_NOTIFY_MODE_UNGRAB)
235 if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
236 e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
237 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
238 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
239 (mon->desk->focus == NULL
240 || mon->desk->focus->client->window != e->event))
241 update_input_focus();
244 void enter_notify(xcb_generic_event_t *evt)
246 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
247 xcb_window_t win = e->event;
249 PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
251 if (e->mode != XCB_NOTIFY_MODE_NORMAL
252 || (mon->desk->focus != NULL && mon->desk->focus->client->window == win))
255 enable_motion_recorder();
258 void motion_notify(xcb_generic_event_t *evt)
260 PUTS("motion notify");
262 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
264 int dtime = e->time - last_motion_time;
266 last_motion_time = e->time;
267 last_motion_x = e->event_x;
268 last_motion_y = e->event_y;
272 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
276 disable_motion_recorder();
278 xcb_window_t win = XCB_NONE;
279 query_pointer(&win, NULL);
280 if (win != XCB_NONE) {
281 bool backup = pointer_follows_monitor;
283 pointer_follows_monitor = false;
285 pointer_follows_monitor = backup;
290 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
292 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
293 if (action == XCB_EWMH_WM_STATE_ADD)
294 set_fullscreen(d, n, true);
295 else if (action == XCB_EWMH_WM_STATE_REMOVE)
296 set_fullscreen(d, n, false);
297 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
298 set_fullscreen(d, n, !n->client->fullscreen);
300 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
301 if (action == XCB_EWMH_WM_STATE_ADD)
302 set_urgency(m, d, n, true);
303 else if (action == XCB_EWMH_WM_STATE_REMOVE)
304 set_urgency(m, d, n, false);
305 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
306 set_urgency(m, d, n, !n->client->urgent);
310 void grab_pointer(pointer_action_t pac)
312 PRINTF("grab pointer %u\n", pac);
314 xcb_window_t win = XCB_NONE;
317 query_pointer(&win, &pos);
320 if (locate_window(win, &loc)) {
322 frozen_pointer->position = pos;
323 frozen_pointer->action = pac;
324 c = loc.node->client;
325 frozen_pointer->monitor = loc.monitor;
326 frozen_pointer->desktop = loc.desktop;
327 frozen_pointer->node = loc.node;
328 frozen_pointer->client = c;
329 frozen_pointer->window = c->window;
330 frozen_pointer->horizontal_fence = NULL;
331 frozen_pointer->vertical_fence = NULL;
335 if (loc.node != mon->desk->focus) {
336 bool backup = pointer_follows_monitor;
337 pointer_follows_monitor = false;
338 focus_node(loc.monitor, loc.desktop, loc.node);
339 pointer_follows_monitor = backup;
340 } else if (focus_follows_pointer && is_floating(loc.node->client)) {
341 stack(loc.desktop, loc.node);
345 case ACTION_RESIZE_SIDE:
346 case ACTION_RESIZE_CORNER:
348 frozen_pointer->rectangle = c->tiled_rectangle;
349 frozen_pointer->is_tiled = true;
350 } else if (is_floating(c)) {
351 frozen_pointer->rectangle = c->floating_rectangle;
352 frozen_pointer->is_tiled = false;
354 frozen_pointer->action = ACTION_NONE;
357 if (pac == ACTION_RESIZE_SIDE) {
358 float W = frozen_pointer->rectangle.width;
359 float H = frozen_pointer->rectangle.height;
361 float x = pos.x - frozen_pointer->rectangle.x;
362 float y = pos.y - frozen_pointer->rectangle.y;
363 float diag_a = ratio * y;
364 float diag_b = W - diag_a;
367 frozen_pointer->side = SIDE_LEFT;
369 frozen_pointer->side = SIDE_BOTTOM;
372 frozen_pointer->side = SIDE_TOP;
374 frozen_pointer->side = SIDE_RIGHT;
376 } else if (pac == ACTION_RESIZE_CORNER) {
377 int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
378 int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
381 frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
383 frozen_pointer->corner = CORNER_TOP_RIGHT;
386 frozen_pointer->corner = CORNER_BOTTOM_LEFT;
388 frozen_pointer->corner = CORNER_TOP_LEFT;
391 if (frozen_pointer->is_tiled) {
392 if (pac == ACTION_RESIZE_SIDE) {
393 switch (frozen_pointer->side) {
395 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
398 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
401 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
404 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
407 } else if (pac == ACTION_RESIZE_CORNER) {
408 switch (frozen_pointer->corner) {
409 case CORNER_TOP_LEFT:
410 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
411 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
413 case CORNER_TOP_RIGHT:
414 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
415 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
417 case CORNER_BOTTOM_RIGHT:
418 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
419 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
421 case CORNER_BOTTOM_LEFT:
422 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
423 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
427 if (frozen_pointer->horizontal_fence != NULL)
428 frozen_pointer->horizontal_ratio = frozen_pointer->horizontal_fence->split_ratio;
429 if (frozen_pointer->vertical_fence != NULL)
430 frozen_pointer->vertical_ratio = frozen_pointer->vertical_fence->split_ratio;
437 if (pac == ACTION_FOCUS) {
438 monitor_t *m = monitor_from_point(pos);
440 focus_node(m, m->desk, m->desk->focus);
442 frozen_pointer->action = ACTION_NONE;
446 void track_pointer(int root_x, int root_y)
448 if (frozen_pointer->action == ACTION_NONE)
451 int16_t delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
452 uint16_t width, height;
454 pointer_action_t pac = frozen_pointer->action;
455 monitor_t *m = frozen_pointer->monitor;
456 desktop_t *d = frozen_pointer->desktop;
457 node_t *n = frozen_pointer->node;
458 client_t *c = frozen_pointer->client;
459 xcb_window_t win = frozen_pointer->window;
460 xcb_rectangle_t rect = frozen_pointer->rectangle;
461 node_t *vertical_fence = frozen_pointer->vertical_fence;
462 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
464 delta_x = root_x - frozen_pointer->position.x;
465 delta_y = root_y - frozen_pointer->position.y;
469 if (frozen_pointer->is_tiled) {
470 xcb_window_t pwin = XCB_NONE;
471 query_pointer(&pwin, NULL);
475 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
476 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
477 swap_nodes(n, loc.node);
480 if (is_managed && loc.monitor == m) {
482 } else if (!is_managed) {
483 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
484 monitor_t *pmon = monitor_from_point(pt);
485 if (pmon == NULL || pmon == m) {
489 loc.desktop = pmon->desk;
492 transfer_node(m, d, loc.monitor, loc.desktop, n);
493 frozen_pointer->monitor = loc.monitor;
494 frozen_pointer->desktop = loc.desktop;
497 x = rect.x + delta_x;
498 y = rect.y + delta_y;
499 window_move(win, x, y);
500 c->floating_rectangle.x = x;
501 c->floating_rectangle.y = y;
502 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
503 monitor_t *pmon = monitor_from_point(pt);
504 if (pmon == NULL || pmon == m)
506 transfer_node(m, d, pmon, pmon->desk, n);
507 frozen_pointer->monitor = pmon;
508 frozen_pointer->desktop = pmon->desk;
511 case ACTION_RESIZE_SIDE:
512 case ACTION_RESIZE_CORNER:
513 if (frozen_pointer->is_tiled) {
514 if (vertical_fence != NULL) {
515 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
518 vertical_fence->split_ratio = sr;
520 if (horizontal_fence != NULL) {
521 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
524 horizontal_fence->split_ratio = sr;
526 arrange(mon, mon->desk);
528 if (pac == ACTION_RESIZE_SIDE) {
529 switch (frozen_pointer->side) {
532 y = rect.y + delta_y;
534 h = rect.height - delta_y;
539 w = rect.width + delta_x;
546 h = rect.height + delta_y;
549 x = rect.x + delta_x;
551 w = rect.width - delta_x;
557 window_move_resize(win, x, y, width, height);
558 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
559 window_draw_border(n, d->focus == n, mon == m);
560 } else if (pac == ACTION_RESIZE_CORNER) {
561 switch (frozen_pointer->corner) {
562 case CORNER_TOP_LEFT:
563 x = rect.x + delta_x;
564 y = rect.y + delta_y;
565 w = rect.width - delta_x;
566 h = rect.height - delta_y;
568 case CORNER_TOP_RIGHT:
570 y = rect.y + delta_y;
571 w = rect.width + delta_x;
572 h = rect.height - delta_y;
574 case CORNER_BOTTOM_LEFT:
575 x = rect.x + delta_x;
577 w = rect.width - delta_x;
578 h = rect.height + delta_y;
580 case CORNER_BOTTOM_RIGHT:
583 w = rect.width + delta_x;
584 h = rect.height + delta_y;
589 window_move_resize(win, x, y, width, height);
590 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
591 window_draw_border(n, d->focus == n, mon == m);