6 #include <xcb/xcb_event.h>
7 #include <xcb/xcb_icccm.h>
20 void handle_event(xcb_generic_event_t *evt)
22 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
27 case XCB_DESTROY_NOTIFY:
30 case XCB_UNMAP_NOTIFY:
33 case XCB_CLIENT_MESSAGE:
36 case XCB_CONFIGURE_REQUEST:
37 configure_request(evt);
39 case XCB_PROPERTY_NOTIFY:
42 case XCB_ENTER_NOTIFY:
45 case XCB_MOTION_NOTIFY:
52 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
58 void map_request(xcb_generic_event_t *evt)
60 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
62 PRINTF("map request %X\n", e->window);
64 manage_window(mon, mon->desk, e->window);
67 void configure_request(xcb_generic_event_t *evt)
69 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
71 PRINTF("configure request %X\n", e->window);
74 bool is_managed = locate_window(e->window, &loc);
76 if (!is_managed || is_floating(loc.node->client)) {
81 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
82 mask |= XCB_CONFIG_WINDOW_X;
85 loc.node->client->floating_rectangle.x = e->x;
88 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
89 mask |= XCB_CONFIG_WINDOW_Y;
92 loc.node->client->floating_rectangle.y = e->y;
95 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
96 mask |= XCB_CONFIG_WINDOW_WIDTH;
97 values[i++] = e->width;
99 loc.node->client->floating_rectangle.width = e->width;
102 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
103 mask |= XCB_CONFIG_WINDOW_HEIGHT;
104 values[i++] = e->height;
106 loc.node->client->floating_rectangle.height = e->height;
109 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
110 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
111 values[i++] = e->border_width;
114 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
115 mask |= XCB_CONFIG_WINDOW_SIBLING;
116 values[i++] = e->sibling;
119 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
120 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
121 values[i++] = e->stack_mode;
124 xcb_configure_window(dpy, e->window, mask, values);
126 window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
128 xcb_configure_notify_event_t evt;
129 xcb_rectangle_t rect;
131 xcb_window_t win = loc.node->client->window;
133 if (is_tiled(loc.node->client)) {
134 rect = loc.node->client->tiled_rectangle;
137 rect = loc.monitor->rectangle;
141 evt.response_type = XCB_CONFIGURE_NOTIFY;
144 evt.above_sibling = XCB_NONE;
147 evt.width = rect.width;
148 evt.height = rect.height;
149 evt.border_width = bw;
150 evt.override_redirect = false;
152 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
156 void destroy_notify(xcb_generic_event_t *evt)
158 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
160 PRINTF("destroy notify %X\n", e->window);
163 if (locate_window(e->window, &loc)) {
164 remove_node(loc.desktop, loc.node);
165 arrange(loc.monitor, loc.desktop);
169 void unmap_notify(xcb_generic_event_t *evt)
171 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
173 PRINTF("unmap notify %X\n", e->window);
176 if (locate_window(e->window, &loc)) {
177 remove_node(loc.desktop, loc.node);
178 arrange(loc.monitor, loc.desktop);
182 void property_notify(xcb_generic_event_t *evt)
184 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
185 xcb_icccm_wm_hints_t hints;
187 /* PRINTF("property notify %X\n", e->window); */
189 if (e->atom != XCB_ATOM_WM_HINTS)
193 if (locate_window(e->window, &loc)
194 && xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1)
195 set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
198 void client_message(xcb_generic_event_t *evt)
200 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
202 PRINTF("client message %X %u\n", e->window, e->type);
204 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
206 if (ewmh_locate_desktop(e->data.data32[0], &loc))
207 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
212 if (!locate_window(e->window, &loc))
215 if (e->type == ewmh->_NET_WM_STATE) {
216 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
217 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
218 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
219 if (loc.node == mon->desk->focus)
221 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
222 set_fullscreen(loc.desktop, loc.desktop->focus, false);
223 arrange(loc.monitor, loc.desktop);
225 focus_node(loc.monitor, loc.desktop, loc.node);
226 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
228 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
229 transfer_node(loc.monitor, loc.desktop, dloc.monitor, dloc.desktop, loc.node);
233 void focus_in(xcb_generic_event_t *evt)
235 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
237 /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
239 if (e->mode == XCB_NOTIFY_MODE_GRAB
240 || e->mode == XCB_NOTIFY_MODE_UNGRAB)
242 if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
243 e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
244 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
245 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
246 (mon->desk->focus == NULL
247 || mon->desk->focus->client->window != e->event))
248 update_input_focus();
251 void enter_notify(xcb_generic_event_t *evt)
253 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
254 xcb_window_t win = e->event;
256 PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
258 if (e->mode != XCB_NOTIFY_MODE_NORMAL
259 || (mon->desk->focus != NULL && mon->desk->focus->client->window == win))
262 enable_motion_recorder();
265 void motion_notify(xcb_generic_event_t *evt)
267 PUTS("motion notify");
269 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
271 int dtime = e->time - last_motion_time;
273 last_motion_time = e->time;
274 last_motion_x = e->event_x;
275 last_motion_y = e->event_y;
279 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
283 disable_motion_recorder();
285 xcb_window_t win = XCB_NONE;
286 query_pointer(&win, NULL);
287 if (win != XCB_NONE) {
288 bool backup = pointer_follows_monitor;
290 pointer_follows_monitor = false;
292 pointer_follows_monitor = backup;
297 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
299 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
300 if (action == XCB_EWMH_WM_STATE_ADD)
301 set_fullscreen(d, n, true);
302 else if (action == XCB_EWMH_WM_STATE_REMOVE)
303 set_fullscreen(d, n, false);
304 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
305 set_fullscreen(d, n, !n->client->fullscreen);
307 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
308 if (action == XCB_EWMH_WM_STATE_ADD)
309 set_urgency(m, d, n, true);
310 else if (action == XCB_EWMH_WM_STATE_REMOVE)
311 set_urgency(m, d, n, false);
312 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
313 set_urgency(m, d, n, !n->client->urgent);
317 void grab_pointer(pointer_action_t pac)
319 PRINTF("grab pointer %u\n", pac);
321 xcb_window_t win = XCB_NONE;
324 query_pointer(&win, &pos);
327 if (locate_window(win, &loc)) {
329 frozen_pointer->position = pos;
330 frozen_pointer->action = pac;
331 c = loc.node->client;
332 frozen_pointer->monitor = loc.monitor;
333 frozen_pointer->desktop = loc.desktop;
334 frozen_pointer->node = loc.node;
335 frozen_pointer->client = c;
336 frozen_pointer->window = c->window;
337 frozen_pointer->horizontal_fence = NULL;
338 frozen_pointer->vertical_fence = NULL;
342 if (loc.node != mon->desk->focus) {
343 bool backup = pointer_follows_monitor;
344 pointer_follows_monitor = false;
345 focus_node(loc.monitor, loc.desktop, loc.node);
346 pointer_follows_monitor = backup;
347 } else if (focus_follows_pointer && is_floating(loc.node->client)) {
348 stack(loc.desktop, loc.node);
352 case ACTION_RESIZE_SIDE:
353 case ACTION_RESIZE_CORNER:
355 frozen_pointer->rectangle = c->tiled_rectangle;
356 frozen_pointer->is_tiled = true;
357 } else if (is_floating(c)) {
358 frozen_pointer->rectangle = c->floating_rectangle;
359 frozen_pointer->is_tiled = false;
361 frozen_pointer->action = ACTION_NONE;
364 if (pac == ACTION_RESIZE_SIDE) {
365 float W = frozen_pointer->rectangle.width;
366 float H = frozen_pointer->rectangle.height;
368 float x = pos.x - frozen_pointer->rectangle.x;
369 float y = pos.y - frozen_pointer->rectangle.y;
370 float diag_a = ratio * y;
371 float diag_b = W - diag_a;
374 frozen_pointer->side = SIDE_LEFT;
376 frozen_pointer->side = SIDE_BOTTOM;
379 frozen_pointer->side = SIDE_TOP;
381 frozen_pointer->side = SIDE_RIGHT;
383 } else if (pac == ACTION_RESIZE_CORNER) {
384 int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
385 int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
388 frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
390 frozen_pointer->corner = CORNER_TOP_RIGHT;
393 frozen_pointer->corner = CORNER_BOTTOM_LEFT;
395 frozen_pointer->corner = CORNER_TOP_LEFT;
398 if (frozen_pointer->is_tiled) {
399 if (pac == ACTION_RESIZE_SIDE) {
400 switch (frozen_pointer->side) {
402 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
405 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
408 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
411 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
414 } else if (pac == ACTION_RESIZE_CORNER) {
415 switch (frozen_pointer->corner) {
416 case CORNER_TOP_LEFT:
417 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
418 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
420 case CORNER_TOP_RIGHT:
421 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
422 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
424 case CORNER_BOTTOM_RIGHT:
425 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
426 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
428 case CORNER_BOTTOM_LEFT:
429 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
430 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
434 if (frozen_pointer->horizontal_fence != NULL)
435 frozen_pointer->horizontal_ratio = frozen_pointer->horizontal_fence->split_ratio;
436 if (frozen_pointer->vertical_fence != NULL)
437 frozen_pointer->vertical_ratio = frozen_pointer->vertical_fence->split_ratio;
444 if (pac == ACTION_FOCUS) {
445 monitor_t *m = monitor_from_point(pos);
447 focus_node(m, m->desk, m->desk->focus);
449 frozen_pointer->action = ACTION_NONE;
453 void track_pointer(int root_x, int root_y)
455 if (frozen_pointer->action == ACTION_NONE)
458 int16_t delta_x, delta_y, x = 0, y = 0, w = 1, h = 1;
459 uint16_t width, height;
461 pointer_action_t pac = frozen_pointer->action;
462 monitor_t *m = frozen_pointer->monitor;
463 desktop_t *d = frozen_pointer->desktop;
464 node_t *n = frozen_pointer->node;
465 client_t *c = frozen_pointer->client;
466 xcb_window_t win = frozen_pointer->window;
467 xcb_rectangle_t rect = frozen_pointer->rectangle;
468 node_t *vertical_fence = frozen_pointer->vertical_fence;
469 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
471 delta_x = root_x - frozen_pointer->position.x;
472 delta_y = root_y - frozen_pointer->position.y;
476 if (frozen_pointer->is_tiled) {
477 xcb_window_t pwin = XCB_NONE;
478 query_pointer(&pwin, NULL);
482 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
483 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
484 swap_nodes(n, loc.node);
487 if (is_managed && loc.monitor == m) {
489 } else if (!is_managed) {
490 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
491 monitor_t *pmon = monitor_from_point(pt);
492 if (pmon == NULL || pmon == m) {
496 loc.desktop = pmon->desk;
499 transfer_node(m, d, loc.monitor, loc.desktop, n);
500 frozen_pointer->monitor = loc.monitor;
501 frozen_pointer->desktop = loc.desktop;
504 x = rect.x + delta_x;
505 y = rect.y + delta_y;
506 window_move(win, x, y);
507 c->floating_rectangle.x = x;
508 c->floating_rectangle.y = y;
509 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
510 monitor_t *pmon = monitor_from_point(pt);
511 if (pmon == NULL || pmon == m)
513 transfer_node(m, d, pmon, pmon->desk, n);
514 frozen_pointer->monitor = pmon;
515 frozen_pointer->desktop = pmon->desk;
518 case ACTION_RESIZE_SIDE:
519 case ACTION_RESIZE_CORNER:
520 if (frozen_pointer->is_tiled) {
521 if (vertical_fence != NULL) {
522 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
525 vertical_fence->split_ratio = sr;
527 if (horizontal_fence != NULL) {
528 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
531 horizontal_fence->split_ratio = sr;
533 arrange(mon, mon->desk);
535 if (pac == ACTION_RESIZE_SIDE) {
536 switch (frozen_pointer->side) {
539 y = rect.y + delta_y;
541 h = rect.height - delta_y;
546 w = rect.width + delta_x;
553 h = rect.height + delta_y;
556 x = rect.x + delta_x;
558 w = rect.width - delta_x;
564 window_move_resize(win, x, y, width, height);
565 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
566 window_draw_border(n, d->focus == n, mon == m);
567 } else if (pac == ACTION_RESIZE_CORNER) {
568 switch (frozen_pointer->corner) {
569 case CORNER_TOP_LEFT:
570 x = rect.x + delta_x;
571 y = rect.y + delta_y;
572 w = rect.width - delta_x;
573 h = rect.height - delta_y;
575 case CORNER_TOP_RIGHT:
577 y = rect.y + delta_y;
578 w = rect.width + delta_x;
579 h = rect.height - delta_y;
581 case CORNER_BOTTOM_LEFT:
582 x = rect.x + delta_x;
584 w = rect.width - delta_x;
585 h = rect.height + delta_y;
587 case CORNER_BOTTOM_RIGHT:
590 w = rect.width + delta_x;
591 h = rect.height + delta_y;
596 window_move_resize(win, x, y, width, height);
597 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
598 window_draw_border(n, d->focus == n, mon == m);