1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "subscribe.h"
37 void handle_event(xcb_generic_event_t *evt)
39 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
44 case XCB_DESTROY_NOTIFY:
47 case XCB_UNMAP_NOTIFY:
50 case XCB_CLIENT_MESSAGE:
53 case XCB_CONFIGURE_REQUEST:
54 configure_request(evt);
56 case XCB_CONFIGURE_NOTIFY:
57 configure_notify(evt);
59 case XCB_PROPERTY_NOTIFY:
62 case XCB_ENTER_NOTIFY:
65 case XCB_MOTION_NOTIFY:
68 case XCB_BUTTON_PRESS:
74 case XCB_MAPPING_NOTIFY:
81 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
88 void map_request(xcb_generic_event_t *evt)
90 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
92 schedule_window(e->window);
95 void configure_request(xcb_generic_event_t *evt)
97 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
100 bool is_managed = locate_window(e->window, &loc);
101 client_t *c = (is_managed ? loc.node->client : NULL);
102 uint16_t width, height;
107 unsigned short i = 0;
109 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
110 mask |= XCB_CONFIG_WINDOW_X;
114 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
115 mask |= XCB_CONFIG_WINDOW_Y;
119 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
120 mask |= XCB_CONFIG_WINDOW_WIDTH;
121 values[i++] = e->width;
124 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
125 mask |= XCB_CONFIG_WINDOW_HEIGHT;
126 values[i++] = e->height;
129 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
130 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
131 values[i++] = e->border_width;
134 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
135 mask |= XCB_CONFIG_WINDOW_SIBLING;
136 values[i++] = e->sibling;
139 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
140 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
141 values[i++] = e->stack_mode;
144 xcb_configure_window(dpy, e->window, mask, values);
146 } else if (IS_FLOATING(c)) {
147 width = c->floating_rectangle.width;
148 height = c->floating_rectangle.height;
150 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
151 c->floating_rectangle.x = e->x;
154 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
155 c->floating_rectangle.y = e->y;
158 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
162 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
166 apply_size_hints(c, &width, &height);
167 c->floating_rectangle.width = width;
168 c->floating_rectangle.height = height;
169 xcb_rectangle_t r = c->floating_rectangle;
171 window_move_resize(e->window, r.x, r.y, r.width, r.height);
173 put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", loc.monitor->id, loc.desktop->id, e->window, r.width, r.height, r.x, r.y);
175 monitor_t *m = monitor_from_client(c);
176 if (m != loc.monitor) {
177 transfer_node(loc.monitor, loc.desktop, loc.node, m, m->desk, m->desk->focus, false);
180 if (c->state == STATE_PSEUDO_TILED) {
181 width = c->floating_rectangle.width;
182 height = c->floating_rectangle.height;
183 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
186 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
189 apply_size_hints(c, &width, &height);
190 if (width != c->floating_rectangle.width || height != c->floating_rectangle.height) {
191 c->floating_rectangle.width = width;
192 c->floating_rectangle.height = height;
193 arrange(loc.monitor, loc.desktop);
198 xcb_configure_notify_event_t evt;
199 unsigned int bw = c->border_width;
201 xcb_rectangle_t r = IS_FULLSCREEN(c) ? loc.monitor->rectangle : c->tiled_rectangle;
203 evt.response_type = XCB_CONFIGURE_NOTIFY;
204 evt.event = e->window;
205 evt.window = e->window;
206 evt.above_sibling = XCB_NONE;
210 evt.height = r.height;
211 evt.border_width = bw;
212 evt.override_redirect = false;
214 xcb_send_event(dpy, false, e->window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
218 void configure_notify(xcb_generic_event_t *evt)
220 xcb_configure_notify_event_t *e = (xcb_configure_notify_event_t *) evt;
222 if (e->window == root) {
223 screen_width = e->width;
224 screen_height = e->height;
228 void destroy_notify(xcb_generic_event_t *evt)
230 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
232 unmanage_window(e->window);
235 void unmap_notify(xcb_generic_event_t *evt)
237 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
239 if (e->window == motion_recorder.id) {
240 /* Unmapping the motion recorder in `query_pointer` will produce
241 * unwanted enter notify events. We can filter those events because
242 * their sequence number is the same as the sequence number of the
243 * related unmap notify event. This is a technique used by i3-wm to
244 * filter enter notify events. */
245 motion_recorder.sequence = e->sequence;
249 /* Filter out destroyed windows */
250 if (!window_exists(e->window)) {
254 set_window_state(e->window, XCB_ICCCM_WM_STATE_WITHDRAWN);
255 unmanage_window(e->window);
258 void property_notify(xcb_generic_event_t *evt)
260 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
262 if (e->atom == ewmh->_NET_WM_STRUT_PARTIAL && ewmh_handle_struts(e->window)) {
263 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
264 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
270 if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS) {
275 if (!locate_window(e->window, &loc)) {
279 if (e->atom == XCB_ATOM_WM_HINTS) {
280 xcb_icccm_wm_hints_t hints;
281 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
282 (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
283 set_urgent(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
284 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
285 client_t *c = loc.node->client;
286 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &c->size_hints, NULL) == 1) {
287 arrange(loc.monitor, loc.desktop);
292 void client_message(xcb_generic_event_t *evt)
294 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
296 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
298 if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
299 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
305 if (!locate_window(e->window, &loc)) {
309 if (e->type == ewmh->_NET_WM_STATE) {
310 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
311 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
312 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
313 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
314 loc.node == mon->desk->focus) {
317 focus_node(loc.monitor, loc.desktop, loc.node);
318 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
320 if (ewmh_locate_desktop(e->data.data32[0], &dloc)) {
321 transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus, false);
323 } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
324 close_node(loc.node);
328 void focus_in(xcb_generic_event_t *evt)
330 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
332 if (e->mode == XCB_NOTIFY_MODE_GRAB || e->mode == XCB_NOTIFY_MODE_UNGRAB
333 || e->detail == XCB_NOTIFY_DETAIL_POINTER || e->detail == XCB_NOTIFY_DETAIL_POINTER_ROOT
334 || e->detail == XCB_NOTIFY_DETAIL_NONE) {
338 if (mon->desk->focus != NULL && e->event == mon->desk->focus->id) {
343 if (locate_window(e->event, &loc)) {
344 // prevent input focus stealing
345 update_input_focus();
349 void button_press(xcb_generic_event_t *evt)
351 xcb_button_press_event_t *e = (xcb_button_press_event_t *) evt;
353 for (unsigned int i = 0; i < LENGTH(BUTTONS); i++) {
354 if (e->detail != BUTTONS[i]) {
357 if ((click_to_focus == (int8_t) XCB_BUTTON_INDEX_ANY || click_to_focus == (int8_t) BUTTONS[i]) &&
358 cleaned_mask(e->state) == XCB_NONE) {
359 bool pff = pointer_follows_focus;
360 bool pfm = pointer_follows_monitor;
361 pointer_follows_focus = false;
362 pointer_follows_monitor = false;
363 replay = !grab_pointer(ACTION_FOCUS) || !swallow_first_click;
364 pointer_follows_focus = pff;
365 pointer_follows_monitor = pfm;
367 grab_pointer(pointer_actions[i]);
370 xcb_allow_events(dpy, replay ? XCB_ALLOW_REPLAY_POINTER : XCB_ALLOW_SYNC_POINTER, e->time);
374 void enter_notify(xcb_generic_event_t *evt)
376 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
377 xcb_window_t win = e->event;
379 if (e->mode != XCB_NOTIFY_MODE_NORMAL || e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
383 /* Ignore the enter notify events that we generated by unmapping the motion
384 * recorder window in `query_pointer`. */
385 if (motion_recorder.enabled && motion_recorder.sequence == e->sequence) {
389 if (win == mon->root || (mon->desk->focus != NULL &&
390 (win == mon->desk->focus->id ||
391 (mon->desk->focus->presel != NULL &&
392 win == mon->desk->focus->presel->feedback)))) {
396 update_motion_recorder();
399 void motion_notify(xcb_generic_event_t *evt)
401 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
403 static uint16_t last_motion_x = 0, last_motion_y = 0;
404 static xcb_timestamp_t last_motion_time = 0;
406 int64_t dtime = e->time - last_motion_time;
408 /* Ignore unintentional pointer motions. */
410 last_motion_time = e->time;
411 last_motion_x = e->event_x;
412 last_motion_y = e->event_y;
415 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
420 disable_motion_recorder();
422 xcb_window_t win = XCB_NONE;
423 query_pointer(&win, NULL);
425 bool pff = pointer_follows_focus;
426 bool pfm = pointer_follows_monitor;
427 pointer_follows_focus = false;
428 pointer_follows_monitor = false;
431 if (locate_window(win, &loc)) {
432 if (loc.monitor->desk == loc.desktop && loc.node != mon->desk->focus) {
433 focus_node(loc.monitor, loc.desktop, loc.node);
436 xcb_point_t pt = {e->root_x, e->root_y};
437 monitor_t *m = monitor_from_point(pt);
438 if (m != NULL && m != mon) {
439 focus_node(m, m->desk, m->desk->focus);
443 pointer_follows_focus = pff;
444 pointer_follows_monitor = pfm;
448 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
450 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
451 if (action == XCB_EWMH_WM_STATE_ADD) {
452 set_state(m, d, n, STATE_FULLSCREEN);
453 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
454 if (n->client->state == STATE_FULLSCREEN) {
455 set_state(m, d, n, n->client->last_state);
457 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
458 set_state(m, d, n, IS_FULLSCREEN(n->client) ? n->client->last_state : STATE_FULLSCREEN);
461 } else if (state == ewmh->_NET_WM_STATE_BELOW) {
462 if (action == XCB_EWMH_WM_STATE_ADD) {
463 set_layer(m, d, n, LAYER_BELOW);
464 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
465 if (n->client->layer == LAYER_BELOW) {
466 set_layer(m, d, n, n->client->last_layer);
468 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
469 set_layer(m, d, n, n->client->layer == LAYER_BELOW ? n->client->last_layer : LAYER_BELOW);
471 } else if (state == ewmh->_NET_WM_STATE_ABOVE) {
472 if (action == XCB_EWMH_WM_STATE_ADD) {
473 set_layer(m, d, n, LAYER_ABOVE);
474 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
475 if (n->client->layer == LAYER_ABOVE) {
476 set_layer(m, d, n, n->client->last_layer);
478 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
479 set_layer(m, d, n, n->client->layer == LAYER_ABOVE ? n->client->last_layer : LAYER_ABOVE);
481 } else if (state == ewmh->_NET_WM_STATE_HIDDEN) {
482 if (action == XCB_EWMH_WM_STATE_ADD) {
483 set_hidden(m, d, n, true);
484 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
485 set_hidden(m, d, n, false);
486 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
487 set_hidden(m, d, n, !n->hidden);
489 } else if (state == ewmh->_NET_WM_STATE_STICKY) {
490 if (action == XCB_EWMH_WM_STATE_ADD) {
491 set_sticky(m, d, n, true);
492 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
493 set_sticky(m, d, n, false);
494 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
495 set_sticky(m, d, n, !n->sticky);
497 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
498 if (action == XCB_EWMH_WM_STATE_ADD) {
499 set_urgent(m, d, n, true);
500 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
501 set_urgent(m, d, n, false);
502 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
503 set_urgent(m, d, n, !n->client->urgent);
505 #define HANDLE_WM_STATE(s) \
506 } else if (state == ewmh->_NET_WM_STATE_##s) { \
507 if (action == XCB_EWMH_WM_STATE_ADD) { \
508 n->client->wm_flags |= WM_FLAG_##s; \
509 } else if (action == XCB_EWMH_WM_STATE_REMOVE) { \
510 n->client->wm_flags &= ~WM_FLAG_##s; \
511 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) { \
512 n->client->wm_flags ^= WM_FLAG_##s; \
514 ewmh_wm_state_update(n);
515 HANDLE_WM_STATE(MODAL)
516 HANDLE_WM_STATE(MAXIMIZED_VERT)
517 HANDLE_WM_STATE(MAXIMIZED_HORZ)
518 HANDLE_WM_STATE(SHADED)
519 HANDLE_WM_STATE(SKIP_TASKBAR)
520 HANDLE_WM_STATE(SKIP_PAGER)
522 #undef HANDLE_WM_STATE
525 void mapping_notify(xcb_generic_event_t *evt)
527 if (mapping_events_count == 0) {
531 xcb_mapping_notify_event_t *e = (xcb_mapping_notify_event_t *) evt;
533 if (e->request == XCB_MAPPING_POINTER) {
537 if (mapping_events_count > 0) {
538 mapping_events_count--;
545 void process_error(xcb_generic_event_t *evt)
547 xcb_request_error_t *e = (xcb_request_error_t *) evt;
548 /* Ignore unavoidable failed requests */
549 if (e->error_code == ERROR_CODE_BAD_WINDOW) {
552 warn("Failed request: %s, %s: 0x%08X.\n", xcb_event_get_request_label(e->major_opcode), xcb_event_get_error_label(e->error_code), e->bad_value);