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_PROPERTY_NOTIFY:
59 case XCB_ENTER_NOTIFY:
62 case XCB_BUTTON_PRESS:
72 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
79 void map_request(xcb_generic_event_t *evt)
81 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
83 schedule_window(e->window);
86 void configure_request(xcb_generic_event_t *evt)
88 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
91 bool is_managed = locate_window(e->window, &loc);
92 client_t *c = (is_managed ? loc.node->client : NULL);
93 uint16_t width, height;
100 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
101 mask |= XCB_CONFIG_WINDOW_X;
105 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
106 mask |= XCB_CONFIG_WINDOW_Y;
110 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
111 mask |= XCB_CONFIG_WINDOW_WIDTH;
112 values[i++] = e->width;
115 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
116 mask |= XCB_CONFIG_WINDOW_HEIGHT;
117 values[i++] = e->height;
120 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
121 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
122 values[i++] = e->border_width;
125 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
126 mask |= XCB_CONFIG_WINDOW_SIBLING;
127 values[i++] = e->sibling;
130 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
131 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
132 values[i++] = e->stack_mode;
135 xcb_configure_window(dpy, e->window, mask, values);
137 } else if (IS_FLOATING(c)) {
138 width = c->floating_rectangle.width;
139 height = c->floating_rectangle.height;
141 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
142 c->floating_rectangle.x = e->x;
145 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
146 c->floating_rectangle.y = e->y;
149 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
153 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
157 apply_size_hints(c, &width, &height);
158 c->floating_rectangle.width = width;
159 c->floating_rectangle.height = height;
160 xcb_rectangle_t r = c->floating_rectangle;
162 if (focus_follows_pointer) {
163 listen_enter_notify(loc.desktop->root, false);
166 window_move_resize(e->window, r.x, r.y, r.width, r.height);
168 if (focus_follows_pointer) {
169 listen_enter_notify(loc.desktop->root, true);
172 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);
174 monitor_t *m = monitor_from_client(c);
175 if (m != loc.monitor) {
176 transfer_node(loc.monitor, loc.desktop, loc.node, m, m->desk, m->desk->focus);
179 if (c->state == STATE_PSEUDO_TILED) {
180 width = c->floating_rectangle.width;
181 height = c->floating_rectangle.height;
182 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
185 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
188 apply_size_hints(c, &width, &height);
189 if (width != c->floating_rectangle.width || height != c->floating_rectangle.height) {
190 c->floating_rectangle.width = width;
191 c->floating_rectangle.height = height;
192 arrange(loc.monitor, loc.desktop);
197 xcb_configure_notify_event_t evt;
198 unsigned int bw = c->border_width;
200 xcb_rectangle_t r = IS_FULLSCREEN(c) ? loc.monitor->rectangle : c->tiled_rectangle;
202 evt.response_type = XCB_CONFIGURE_NOTIFY;
203 evt.event = e->window;
204 evt.window = e->window;
205 evt.above_sibling = XCB_NONE;
209 evt.height = r.height;
210 evt.border_width = bw;
211 evt.override_redirect = false;
213 xcb_send_event(dpy, false, e->window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
217 void destroy_notify(xcb_generic_event_t *evt)
219 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
221 unmanage_window(e->window);
224 void unmap_notify(xcb_generic_event_t *evt)
226 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
228 unmanage_window(e->window);
231 void property_notify(xcb_generic_event_t *evt)
233 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
235 if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS) {
240 if (!locate_window(e->window, &loc)) {
244 if (e->atom == XCB_ATOM_WM_HINTS) {
245 xcb_icccm_wm_hints_t hints;
246 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
247 (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
248 set_urgent(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
249 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
250 client_t *c = loc.node->client;
251 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &c->size_hints, NULL) == 1) {
252 arrange(loc.monitor, loc.desktop);
257 void client_message(xcb_generic_event_t *evt)
259 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
261 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
263 if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
264 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
270 if (!locate_window(e->window, &loc)) {
274 if (e->type == ewmh->_NET_WM_STATE) {
275 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
276 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
277 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
278 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
279 loc.node == mon->desk->focus) {
282 focus_node(loc.monitor, loc.desktop, loc.node);
283 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
285 if (ewmh_locate_desktop(e->data.data32[0], &dloc)) {
286 transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
288 } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
289 close_node(loc.node);
293 void focus_in(xcb_generic_event_t *evt)
295 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
297 if (e->mode == XCB_NOTIFY_MODE_GRAB || e->mode == XCB_NOTIFY_MODE_UNGRAB
298 || e->detail == XCB_NOTIFY_DETAIL_POINTER || e->detail == XCB_NOTIFY_DETAIL_POINTER_ROOT
299 || e->detail == XCB_NOTIFY_DETAIL_NONE) {
303 if (mon->desk->focus != NULL && e->event == mon->desk->focus->id) {
308 if (locate_window(e->event, &loc)) {
309 // prevent input focus stealing
310 update_input_focus();
314 void button_press(xcb_generic_event_t *evt)
316 xcb_button_press_event_t *e = (xcb_button_press_event_t *) evt;
319 case XCB_BUTTON_INDEX_1:
320 if (click_to_focus && cleaned_mask(e->state) == XCB_NONE) {
322 bool pff = pointer_follows_focus;
323 bool pfm = pointer_follows_monitor;
324 pointer_follows_focus = false;
325 pointer_follows_monitor = false;
326 grab_pointer(ACTION_FOCUS);
327 pointer_follows_focus = pff;
328 pointer_follows_monitor = pfm;
330 grab_pointer(pointer_actions[0]);
333 case XCB_BUTTON_INDEX_2:
334 grab_pointer(pointer_actions[1]);
336 case XCB_BUTTON_INDEX_3:
337 grab_pointer(pointer_actions[2]);
340 xcb_allow_events(dpy, replay ? XCB_ALLOW_REPLAY_POINTER : XCB_ALLOW_SYNC_POINTER, e->time);
344 void enter_notify(xcb_generic_event_t *evt)
346 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
347 xcb_window_t win = e->event;
349 if (e->mode != XCB_NOTIFY_MODE_NORMAL || e->detail == XCB_NOTIFY_DETAIL_INFERIOR) {
353 xcb_point_t pt = {e->root_x, e->root_y};
354 monitor_t *m = monitor_from_point(pt);
360 bool pff = pointer_follows_focus;
361 bool pfm = pointer_follows_monitor;
362 pointer_follows_focus = false;
363 pointer_follows_monitor = false;
366 desktop_t *d = m->desk;
369 for (n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
370 if (n->id == win || (n->presel != NULL && n->presel->feedback == win)) {
375 if (n != mon->desk->focus) {
378 } else if (m != mon) {
379 focus_node(m, m->desk, m->desk->focus);
383 pointer_follows_focus = pff;
384 pointer_follows_monitor = pfm;
388 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
390 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
391 if (action == XCB_EWMH_WM_STATE_ADD) {
392 set_state(m, d, n, STATE_FULLSCREEN);
393 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
394 if (n->client->state == STATE_FULLSCREEN) {
395 set_state(m, d, n, n->client->last_state);
397 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
398 set_state(m, d, n, IS_FULLSCREEN(n->client) ? n->client->last_state : STATE_FULLSCREEN);
401 } else if (state == ewmh->_NET_WM_STATE_BELOW) {
402 if (action == XCB_EWMH_WM_STATE_ADD) {
403 set_layer(m, d, n, LAYER_BELOW);
404 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
405 if (n->client->layer == LAYER_BELOW) {
406 set_layer(m, d, n, n->client->last_layer);
408 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
409 set_layer(m, d, n, n->client->layer == LAYER_BELOW ? n->client->last_layer : LAYER_BELOW);
411 } else if (state == ewmh->_NET_WM_STATE_ABOVE) {
412 if (action == XCB_EWMH_WM_STATE_ADD) {
413 set_layer(m, d, n, LAYER_ABOVE);
414 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
415 if (n->client->layer == LAYER_ABOVE) {
416 set_layer(m, d, n, n->client->last_layer);
418 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
419 set_layer(m, d, n, n->client->layer == LAYER_ABOVE ? n->client->last_layer : LAYER_ABOVE);
421 } else if (state == ewmh->_NET_WM_STATE_HIDDEN) {
422 if (action == XCB_EWMH_WM_STATE_ADD) {
423 set_hidden(m, d, n, true);
424 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
425 set_hidden(m, d, n, false);
426 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
427 set_hidden(m, d, n, !n->hidden);
429 } else if (state == ewmh->_NET_WM_STATE_STICKY) {
430 if (action == XCB_EWMH_WM_STATE_ADD) {
431 set_sticky(m, d, n, true);
432 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
433 set_sticky(m, d, n, false);
434 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
435 set_sticky(m, d, n, !n->sticky);
437 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
438 if (action == XCB_EWMH_WM_STATE_ADD) {
439 set_urgent(m, d, n, true);
440 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
441 set_urgent(m, d, n, false);
442 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
443 set_urgent(m, d, n, !n->client->urgent);
445 #define HANDLE_WM_STATE(s) \
446 } else if (state == ewmh->_NET_WM_STATE_##s) { \
447 if (action == XCB_EWMH_WM_STATE_ADD) { \
448 n->client->wm_flags |= WM_FLAG_##s; \
449 } else if (action == XCB_EWMH_WM_STATE_REMOVE) { \
450 n->client->wm_flags &= ~WM_FLAG_##s; \
451 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) { \
452 n->client->wm_flags ^= WM_FLAG_##s; \
454 ewmh_wm_state_update(n);
455 HANDLE_WM_STATE(MODAL)
456 HANDLE_WM_STATE(MAXIMIZED_VERT)
457 HANDLE_WM_STATE(MAXIMIZED_HORZ)
458 HANDLE_WM_STATE(SHADED)
459 HANDLE_WM_STATE(SKIP_TASKBAR)
460 HANDLE_WM_STATE(SKIP_PAGER)
462 #undef HANDLE_WM_STATE
465 void process_error(xcb_generic_event_t *evt)
467 xcb_request_error_t *e = (xcb_request_error_t *) evt;
468 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);