5 #include <xcb/xcb_event.h>
6 #include <xcb/xcb_icccm.h>
17 void handle_event(xcb_generic_event_t *evt)
19 switch (XCB_EVENT_RESPONSE_TYPE(evt)) {
23 case XCB_DESTROY_NOTIFY:
26 case XCB_UNMAP_NOTIFY:
29 case XCB_CLIENT_MESSAGE:
32 case XCB_CONFIGURE_REQUEST:
33 configure_request(evt);
35 case XCB_PROPERTY_NOTIFY:
38 case XCB_ENTER_NOTIFY:
41 case XCB_MOTION_NOTIFY:
49 void map_request(xcb_generic_event_t *evt)
51 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
53 PRINTF("map request %X\n", e->window);
55 manage_window(mon, mon->desk, e->window);
58 void configure_request(xcb_generic_event_t *evt)
60 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
62 PRINTF("configure request %X\n", e->window);
64 window_location_t loc;
65 bool is_managed = locate_window(e->window, &loc);
67 if (!is_managed || is_floating(loc.node->client)) {
72 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
73 mask |= XCB_CONFIG_WINDOW_X;
76 loc.node->client->floating_rectangle.x = e->x;
79 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
80 mask |= XCB_CONFIG_WINDOW_Y;
83 loc.node->client->floating_rectangle.y = e->y;
86 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
87 mask |= XCB_CONFIG_WINDOW_WIDTH;
88 values[i++] = e->width;
90 loc.node->client->floating_rectangle.width = e->width;
93 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
94 mask |= XCB_CONFIG_WINDOW_HEIGHT;
95 values[i++] = e->height;
97 loc.node->client->floating_rectangle.height = e->height;
100 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
101 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
102 values[i++] = e->border_width;
105 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
106 mask |= XCB_CONFIG_WINDOW_SIBLING;
107 values[i++] = e->sibling;
110 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
111 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
112 values[i++] = e->stack_mode;
115 xcb_configure_window(dpy, e->window, mask, values);
117 window_draw_border(loc.node, loc.node == loc.desktop->focus, loc.monitor == mon);
119 xcb_configure_notify_event_t evt;
120 xcb_rectangle_t rect;
122 xcb_window_t win = loc.node->client->window;
124 if (is_tiled(loc.node->client)) {
125 rect = loc.node->client->tiled_rectangle;
128 rect = loc.monitor->rectangle;
132 evt.response_type = XCB_CONFIGURE_NOTIFY;
135 evt.above_sibling = XCB_NONE;
138 evt.width = rect.width;
139 evt.height = rect.height;
140 evt.border_width = bw;
141 evt.override_redirect = false;
143 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
147 void destroy_notify(xcb_generic_event_t *evt)
149 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
151 PRINTF("destroy notify %X\n", e->window);
153 window_location_t loc;
154 if (locate_window(e->window, &loc)) {
155 remove_node(loc.desktop, loc.node);
156 arrange(loc.monitor, loc.desktop);
160 void unmap_notify(xcb_generic_event_t *evt)
162 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
164 PRINTF("unmap notify %X\n", e->window);
166 window_location_t loc;
167 if (locate_window(e->window, &loc)) {
168 remove_node(loc.desktop, loc.node);
169 arrange(loc.monitor, loc.desktop);
173 void property_notify(xcb_generic_event_t *evt)
175 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
176 xcb_icccm_wm_hints_t hints;
178 /* PRINTF("property notify %X\n", e->window); */
180 if (e->atom != XCB_ATOM_WM_HINTS)
183 window_location_t loc;
184 if (locate_window(e->window, &loc)) {
185 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1) {
186 uint32_t urgent = xcb_icccm_wm_hints_get_urgency(&hints);
187 if (urgent != 0 && loc.node != mon->desk->focus) {
188 loc.node->client->urgent = urgent;
190 if (loc.monitor->desk == loc.desktop)
191 arrange(loc.monitor, loc.desktop);
197 void client_message(xcb_generic_event_t *evt)
199 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
201 PRINTF("client message %X %u\n", e->window, e->type);
203 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
204 desktop_location_t loc;
205 if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
206 select_monitor(loc.monitor);
207 select_desktop(loc.desktop);
212 window_location_t loc;
213 if (!locate_window(e->window, &loc))
216 if (e->type == ewmh->_NET_WM_STATE) {
217 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
218 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
219 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
220 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node)
221 toggle_fullscreen(loc.monitor, loc.desktop->focus->client);
222 select_monitor(loc.monitor);
223 select_desktop(loc.desktop);
224 focus_node(loc.monitor, loc.desktop, loc.node, true);
225 arrange(loc.monitor, loc.desktop);
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(void)
245 PUTS("motion notify");
247 disable_motion_recorder();
249 xcb_window_t win = XCB_NONE;
250 query_pointer(&win, NULL);
255 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
257 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
258 bool fs = n->client->fullscreen;
259 if (action == XCB_EWMH_WM_STATE_TOGGLE
260 || (fs && action == XCB_EWMH_WM_STATE_REMOVE)
261 || (!fs && action == XCB_EWMH_WM_STATE_ADD)) {
262 toggle_fullscreen(m, n->client);
268 void grab_pointer(pointer_action_t pac)
270 PRINTF("grab pointer %u\n", pac);
272 xcb_window_t win = XCB_NONE;
275 query_pointer(&win, &pos);
280 window_location_t loc;
281 if (locate_window(win, &loc)) {
283 frozen_pointer->position = pos;
284 frozen_pointer->action = pac;
285 c = loc.node->client;
286 frozen_pointer->monitor = loc.monitor;
287 frozen_pointer->desktop = loc.desktop;
288 frozen_pointer->node = loc.node;
289 frozen_pointer->client = c;
290 frozen_pointer->window = c->window;
291 frozen_pointer->horizontal_fence = NULL;
292 frozen_pointer->vertical_fence = NULL;
296 focus_node(loc.monitor, loc.desktop, loc.node, true);
299 case ACTION_RESIZE_SIDE:
300 case ACTION_RESIZE_CORNER:
302 frozen_pointer->rectangle = c->tiled_rectangle;
303 frozen_pointer->is_tiled = true;
304 } else if (is_floating(c)) {
305 frozen_pointer->rectangle = c->floating_rectangle;
306 frozen_pointer->is_tiled = false;
308 frozen_pointer->action = ACTION_NONE;
311 if (pac == ACTION_RESIZE_SIDE) {
312 float W = frozen_pointer->rectangle.width;
313 float H = frozen_pointer->rectangle.height;
315 float x = pos.x - frozen_pointer->rectangle.x;
316 float y = pos.y - frozen_pointer->rectangle.y;
317 float diag_a = ratio * y;
318 float diag_b = W - diag_a;
321 frozen_pointer->side = SIDE_LEFT;
323 frozen_pointer->side = SIDE_BOTTOM;
326 frozen_pointer->side = SIDE_TOP;
328 frozen_pointer->side = SIDE_RIGHT;
330 } else if (pac == ACTION_RESIZE_CORNER) {
331 int16_t mid_x = frozen_pointer->rectangle.x + (frozen_pointer->rectangle.width / 2);
332 int16_t mid_y = frozen_pointer->rectangle.y + (frozen_pointer->rectangle.height / 2);
335 frozen_pointer->corner = CORNER_BOTTOM_RIGHT;
337 frozen_pointer->corner = CORNER_TOP_RIGHT;
340 frozen_pointer->corner = CORNER_BOTTOM_LEFT;
342 frozen_pointer->corner = CORNER_TOP_LEFT;
345 if (frozen_pointer->is_tiled) {
346 if (pac == ACTION_RESIZE_SIDE) {
347 switch (frozen_pointer->side) {
349 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
352 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
355 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
358 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
361 } else if (pac == ACTION_RESIZE_CORNER) {
362 switch (frozen_pointer->corner) {
363 case CORNER_TOP_LEFT:
364 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
365 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
367 case CORNER_TOP_RIGHT:
368 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_UP);
369 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
371 case CORNER_BOTTOM_RIGHT:
372 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
373 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_RIGHT);
375 case CORNER_BOTTOM_LEFT:
376 frozen_pointer->horizontal_fence = find_fence(loc.node, DIR_DOWN);
377 frozen_pointer->vertical_fence = find_fence(loc.node, DIR_LEFT);
381 if (frozen_pointer->horizontal_fence != NULL)
382 frozen_pointer->horizontal_ratio = frozen_pointer->horizontal_fence->split_ratio;
383 if (frozen_pointer->vertical_fence != NULL)
384 frozen_pointer->vertical_ratio = frozen_pointer->vertical_fence->split_ratio;
391 frozen_pointer->action = ACTION_NONE;
395 void track_pointer(int root_x, int root_y)
397 if (frozen_pointer->action == ACTION_NONE)
400 int16_t delta_x, delta_y, x, y, w, h;
401 uint16_t width, height;
403 pointer_action_t pac = frozen_pointer->action;
404 monitor_t *m = frozen_pointer->monitor;
405 desktop_t *d = frozen_pointer->desktop;
406 node_t *n = frozen_pointer->node;
407 client_t *c = frozen_pointer->client;
408 xcb_window_t win = frozen_pointer->window;
409 xcb_rectangle_t rect = frozen_pointer->rectangle;
410 node_t *vertical_fence = frozen_pointer->vertical_fence;
411 node_t *horizontal_fence = frozen_pointer->horizontal_fence;
416 delta_x = root_x - frozen_pointer->position.x;
417 delta_y = root_y - frozen_pointer->position.y;
421 if (frozen_pointer->is_tiled) {
422 xcb_window_t pwin = XCB_NONE;
423 query_pointer(&pwin, NULL);
426 window_location_t loc;
427 bool is_managed = (pwin == XCB_NONE ? false : locate_window(pwin, &loc));
428 if (is_managed && is_tiled(loc.node->client) && loc.monitor == m) {
429 swap_nodes(d, n, loc.desktop, loc.node);
432 if (is_managed && loc.monitor == m) {
434 } else if (!is_managed) {
435 xcb_point_t pt = (xcb_point_t) {root_x, root_y};
436 monitor_t *pmon = monitor_from_point(pt);
437 if (pmon == NULL || pmon == m) {
441 loc.desktop = pmon->desk;
444 transfer_node(m, d, loc.monitor, loc.desktop, n);
446 arrange(loc.monitor, loc.desktop);
447 frozen_pointer->monitor = loc.monitor;
448 frozen_pointer->desktop = loc.desktop;
451 x = rect.x + delta_x;
452 y = rect.y + delta_y;
453 window_move(win, x, y);
456 case ACTION_RESIZE_SIDE:
457 case ACTION_RESIZE_CORNER:
458 if (frozen_pointer->is_tiled) {
459 if (vertical_fence != NULL) {
460 double sr = frozen_pointer->vertical_ratio + (double) delta_x / vertical_fence->rectangle.width;
463 vertical_fence->split_ratio = sr;
465 if (horizontal_fence != NULL) {
466 double sr = frozen_pointer->horizontal_ratio + (double) delta_y / horizontal_fence->rectangle.height;
469 horizontal_fence->split_ratio = sr;
471 arrange(mon, mon->desk);
473 if (pac == ACTION_RESIZE_SIDE) {
474 switch (frozen_pointer->side) {
477 y = rect.y + delta_y;
479 h = rect.height - delta_y;
484 w = rect.width + delta_x;
491 h = rect.height + delta_y;
494 x = rect.x + delta_x;
496 w = rect.width - delta_x;
502 window_move_resize(win, x, y, width, height);
503 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
504 window_draw_border(n, d->focus == n, mon == m);
505 } else if (pac == ACTION_RESIZE_CORNER) {
506 switch (frozen_pointer->corner) {
507 case CORNER_TOP_LEFT:
508 x = rect.x + delta_x;
509 y = rect.y + delta_y;
510 w = rect.width - delta_x;
511 h = rect.height - delta_y;
513 case CORNER_TOP_RIGHT:
515 y = rect.y + delta_y;
516 w = rect.width + delta_x;
517 h = rect.height - delta_y;
519 case CORNER_BOTTOM_LEFT:
520 x = rect.x + delta_x;
522 w = rect.width - delta_x;
523 h = rect.height + delta_y;
525 case CORNER_BOTTOM_RIGHT:
528 w = rect.width + delta_x;
529 h = rect.height + delta_y;
534 window_move_resize(win, x, y, width, height);
535 c->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
536 window_draw_border(n, d->focus == n, mon == m);
546 void ungrab_pointer(void)
548 PUTS("ungrab pointer");
550 if (frozen_pointer->action == ACTION_NONE || frozen_pointer->is_tiled)
553 update_floating_rectangle(frozen_pointer->client);
554 monitor_t *m = underlying_monitor(frozen_pointer->client);
555 if (m != NULL && m != frozen_pointer->monitor) {
556 transfer_node(frozen_pointer->monitor, frozen_pointer->desktop, m, m->desk, frozen_pointer->node);