6 #include <xcb/xcb_event.h>
7 #include <xcb/xcb_icccm.h>
19 void handle_event(xcb_generic_event_t *evt)
21 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
26 case XCB_DESTROY_NOTIFY:
29 case XCB_UNMAP_NOTIFY:
32 case XCB_CLIENT_MESSAGE:
35 case XCB_CONFIGURE_REQUEST:
36 configure_request(evt);
38 case XCB_PROPERTY_NOTIFY:
41 case XCB_ENTER_NOTIFY:
44 case XCB_MOTION_NOTIFY:
48 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
54 void map_request(xcb_generic_event_t *evt)
56 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
58 PRINTF("map request %X\n", e->window);
60 manage_window(mon, mon->desk, e->window);
63 void configure_request(xcb_generic_event_t *evt)
65 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
67 PRINTF("configure request %X\n", e->window);
70 bool is_managed = locate_window(e->window, &loc);
72 if (!is_managed || is_floating(loc.node->client)) {
77 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
78 mask |= XCB_CONFIG_WINDOW_X;
81 loc.node->client->floating_rectangle.x = e->x;
84 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
85 mask |= XCB_CONFIG_WINDOW_Y;
88 loc.node->client->floating_rectangle.y = e->y;
91 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
92 mask |= XCB_CONFIG_WINDOW_WIDTH;
93 values[i++] = e->width;
95 loc.node->client->floating_rectangle.width = e->width;
98 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
99 mask |= XCB_CONFIG_WINDOW_HEIGHT;
100 values[i++] = e->height;
102 loc.node->client->floating_rectangle.height = e->height;
105 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
106 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
107 values[i++] = e->border_width;
110 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
111 mask |= XCB_CONFIG_WINDOW_SIBLING;
112 values[i++] = e->sibling;
115 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
116 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
117 values[i++] = e->stack_mode;
120 xcb_configure_window(dpy, e->window, mask, values);
122 window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
124 xcb_configure_notify_event_t evt;
125 xcb_rectangle_t rect;
127 xcb_window_t win = loc.node->client->window;
129 if (is_tiled(loc.node->client)) {
130 rect = loc.node->client->tiled_rectangle;
133 rect = loc.monitor->rectangle;
137 evt.response_type = XCB_CONFIGURE_NOTIFY;
140 evt.above_sibling = XCB_NONE;
143 evt.width = rect.width;
144 evt.height = rect.height;
145 evt.border_width = bw;
146 evt.override_redirect = false;
148 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
152 void destroy_notify(xcb_generic_event_t *evt)
154 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
156 PRINTF("destroy notify %X\n", e->window);
159 if (locate_window(e->window, &loc)) {
160 remove_node(loc.desktop, loc.node);
161 arrange(loc.monitor, loc.desktop);
165 void unmap_notify(xcb_generic_event_t *evt)
167 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
169 PRINTF("unmap notify %X\n", e->window);
172 if (locate_window(e->window, &loc)) {
173 remove_node(loc.desktop, loc.node);
174 arrange(loc.monitor, loc.desktop);
178 void property_notify(xcb_generic_event_t *evt)
180 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
181 xcb_icccm_wm_hints_t hints;
183 /* PRINTF("property notify %X\n", e->window); */
185 if (e->atom != XCB_ATOM_WM_HINTS)
189 if (locate_window(e->window, &loc)
190 && xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1)
191 set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
194 void client_message(xcb_generic_event_t *evt)
196 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
198 PRINTF("client message %X %u\n", e->window, e->type);
200 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
202 if (ewmh_locate_desktop(e->data.data32[0], &loc))
203 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
208 if (!locate_window(e->window, &loc))
211 if (e->type == ewmh->_NET_WM_STATE) {
212 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
213 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
214 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
215 if (loc.node == mon->desk->focus)
217 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
218 set_fullscreen(loc.desktop, loc.desktop->focus, false);
219 arrange(loc.monitor, loc.desktop);
221 focus_node(loc.monitor, loc.desktop, loc.node);
222 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
224 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
225 transfer_node(loc.monitor, loc.desktop, dloc.monitor, dloc.desktop, loc.node);
229 void enter_notify(xcb_generic_event_t *evt)
231 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
232 xcb_window_t win = e->event;
234 PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
236 if (e->mode != XCB_NOTIFY_MODE_NORMAL
237 || (mon->desk->focus != NULL && mon->desk->focus->client->window == win))
240 enable_motion_recorder();
243 void motion_notify(xcb_generic_event_t *evt)
245 PUTS("motion notify");
247 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
249 int dtime = e->time - last_motion_time;
251 last_motion_time = e->time;
252 last_motion_x = e->event_x;
253 last_motion_y = e->event_y;
257 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
261 disable_motion_recorder();
263 xcb_window_t win = XCB_NONE;
264 query_pointer(&win, NULL);
265 if (win != XCB_NONE) {
266 bool backup = pointer_follows_monitor;
267 pointer_follows_monitor = false;
269 pointer_follows_monitor = backup;
273 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
275 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
276 if (action == XCB_EWMH_WM_STATE_ADD)
277 set_fullscreen(d, n, true);
278 else if (action == XCB_EWMH_WM_STATE_REMOVE)
279 set_fullscreen(d, n, false);
280 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
281 set_fullscreen(d, n, !n->client->fullscreen);
283 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
284 if (action == XCB_EWMH_WM_STATE_ADD)
285 set_urgency(m, d, n, true);
286 else if (action == XCB_EWMH_WM_STATE_REMOVE)
287 set_urgency(m, d, n, false);
288 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
289 set_urgency(m, d, n, !n->client->urgent);
293 void grab_pointer(pointer_action_t pac)
295 PRINTF("grab pointer %u\n", pac);
297 xcb_window_t win = XCB_NONE;
300 query_pointer(&win, &pos);
306 if (locate_window(win, &loc)) {
308 frozen_pointer->position = pos;
309 frozen_pointer->action = pac;
310 c = loc.node->client;
311 frozen_pointer->monitor = loc.monitor;
312 frozen_pointer->desktop = loc.desktop;
313 frozen_pointer->node = loc.node;
314 frozen_pointer->client = c;
315 frozen_pointer->window = c->window;
316 frozen_pointer->horizontal_fence = NULL;
317 frozen_pointer->vertical_fence = NULL;
321 if (loc.node != mon->desk->focus) {
322 bool backup = pointer_follows_monitor;
323 pointer_follows_monitor = false;
324 focus_node(loc.monitor, loc.desktop, loc.node);
325 pointer_follows_monitor = backup;
329 case ACTION_RESIZE_SIDE:
330 case ACTION_RESIZE_CORNER:
332 frozen_pointer->rectangle = c->tiled_rectangle;
333 frozen_pointer->is_tiled = true;
334 } else if (is_floating(c)) {
335 frozen_pointer->rectangle = c->floating_rectangle;
336 frozen_pointer->is_tiled = false;
338 frozen_pointer->action = ACTION_NONE;
341 if (pac == ACTION_RESIZE_SIDE) {
342 float W = frozen_pointer->rectangle.width;
343 float H = frozen_pointer->rectangle.height;
345 float x = pos.x - frozen_pointer->rectangle.x;
346 float y = pos.y - frozen_pointer->rectangle.y;
347 float diag_a = ratio * y;
348 float diag_b = W - diag_a;
351 frozen_pointer->side = SIDE_LEFT;
353 frozen_pointer->side = SIDE_BOTTOM;
356 frozen_pointer->side = SIDE_TOP;
358 frozen_pointer->side = SIDE_RIGHT;
360 } else if (pac == ACTION_RESIZE_CORNER) {
361 int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
362 int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
365 frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
367 frozen_pointer->corner = CORNER_TOP_RIGHT;
370 frozen_pointer->corner = CORNER_BOTTOM_LEFT;
372 frozen_pointer->corner = CORNER_TOP_LEFT;
375 if (frozen_pointer->is_tiled) {
376 if (pac == ACTION_RESIZE_SIDE) {
377 switch (frozen_pointer->side) {
379 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
382 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
385 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
388 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
391 } else if (pac == ACTION_RESIZE_CORNER) {
392 switch (frozen_pointer->corner) {
393 case CORNER_TOP_LEFT:
394 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
395 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
397 case CORNER_TOP_RIGHT:
398 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
399 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
401 case CORNER_BOTTOM_RIGHT:
402 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
403 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
405 case CORNER_BOTTOM_LEFT:
406 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
407 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
411 if (frozen_pointer->horizontal_fence != NULL)
412 frozen_pointer->horizontal_ratio = frozen_pointer->horizontal_fence->split_ratio;
413 if (frozen_pointer->vertical_fence != NULL)
414 frozen_pointer->vertical_ratio = frozen_pointer->vertical_fence->split_ratio;
421 frozen_pointer->action = ACTION_NONE;
425 void track_pointer(int root_x, int root_y)
427 if (frozen_pointer->action == ACTION_NONE)
430 int16_t delta_x, delta_y, x, y, w, h;
431 uint16_t width, height;
433 pointer_action_t pac = frozen_pointer->action;
434 monitor_t *m = frozen_pointer->monitor;
435 desktop_t *d = frozen_pointer->desktop;
436 node_t *n = frozen_pointer->node;
437 client_t *c = frozen_pointer->client;
438 xcb_window_t win = frozen_pointer->window;
439 xcb_rectangle_t rect = frozen_pointer->rectangle;
440 node_t *vertical_fence = frozen_pointer->vertical_fence;
441 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
446 delta_x = root_x - frozen_pointer->position.x;
447 delta_y = root_y - frozen_pointer->position.y;
451 if (frozen_pointer->is_tiled) {
452 xcb_window_t pwin = XCB_NONE;
453 query_pointer(&pwin, NULL);
457 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
458 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
459 swap_nodes(n, loc.node);
462 if (is_managed && loc.monitor == m) {
464 } else if (!is_managed) {
465 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
466 monitor_t *pmon = monitor_from_point(pt);
467 if (pmon == NULL || pmon == m) {
471 loc.desktop = pmon->desk;
474 transfer_node(m, d, loc.monitor, loc.desktop, n);
475 frozen_pointer->monitor = loc.monitor;
476 frozen_pointer->desktop = loc.desktop;
479 x = rect.x + delta_x;
480 y = rect.y + delta_y;
481 window_move(win, x, y);
482 c->floating_rectangle.x = x;
483 c->floating_rectangle.y = y;
484 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
485 monitor_t *pmon = monitor_from_point(pt);
486 if (pmon == NULL || pmon == m)
488 transfer_node(m, d, pmon, pmon->desk, n);
489 frozen_pointer->monitor = pmon;
490 frozen_pointer->desktop = pmon->desk;
493 case ACTION_RESIZE_SIDE:
494 case ACTION_RESIZE_CORNER:
495 if (frozen_pointer->is_tiled) {
496 if (vertical_fence != NULL) {
497 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
500 vertical_fence->split_ratio = sr;
502 if (horizontal_fence != NULL) {
503 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
506 horizontal_fence->split_ratio = sr;
508 arrange(mon, mon->desk);
510 if (pac == ACTION_RESIZE_SIDE) {
511 switch (frozen_pointer->side) {
514 y = rect.y + delta_y;
516 h = rect.height - delta_y;
521 w = rect.width + delta_x;
528 h = rect.height + delta_y;
531 x = rect.x + delta_x;
533 w = rect.width - delta_x;
539 window_move_resize(win, x, y, width, height);
540 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
541 window_draw_border(n, d->focus == n, mon == m);
542 } else if (pac == ACTION_RESIZE_CORNER) {
543 switch (frozen_pointer->corner) {
544 case CORNER_TOP_LEFT:
545 x = rect.x + delta_x;
546 y = rect.y + delta_y;
547 w = rect.width - delta_x;
548 h = rect.height - delta_y;
550 case CORNER_TOP_RIGHT:
552 y = rect.y + delta_y;
553 w = rect.width + delta_x;
554 h = rect.height - delta_y;
556 case CORNER_BOTTOM_LEFT:
557 x = rect.x + delta_x;
559 w = rect.width - delta_x;
560 h = rect.height + delta_y;
562 case CORNER_BOTTOM_RIGHT:
565 w = rect.width + delta_x;
566 h = rect.height + delta_y;
571 window_move_resize(win, x, y, width, height);
572 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
573 window_draw_border(n, d->focus == n, mon == m);