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.
32 #include "subscribe.h"
38 void handle_event(xcb_generic_event_t *evt)
40 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
45 case XCB_DESTROY_NOTIFY:
48 case XCB_UNMAP_NOTIFY:
51 case XCB_CLIENT_MESSAGE:
54 case XCB_CONFIGURE_REQUEST:
55 configure_request(evt);
57 case XCB_PROPERTY_NOTIFY:
60 case XCB_ENTER_NOTIFY:
63 case XCB_BUTTON_PRESS:
66 case XCB_MOTION_NOTIFY:
76 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
83 void map_request(xcb_generic_event_t *evt)
85 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
87 schedule_window(e->window);
90 void configure_request(xcb_generic_event_t *evt)
92 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
95 bool is_managed = locate_window(e->window, &loc);
96 client_t *c = (is_managed ? loc.node->client : NULL);
97 uint16_t width, height;
102 unsigned short i = 0;
104 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
105 mask |= XCB_CONFIG_WINDOW_X;
109 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
110 mask |= XCB_CONFIG_WINDOW_Y;
114 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
115 mask |= XCB_CONFIG_WINDOW_WIDTH;
116 values[i++] = e->width;
119 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
120 mask |= XCB_CONFIG_WINDOW_HEIGHT;
121 values[i++] = e->height;
124 if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
125 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
126 values[i++] = e->border_width;
129 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
130 mask |= XCB_CONFIG_WINDOW_SIBLING;
131 values[i++] = e->sibling;
134 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
135 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
136 values[i++] = e->stack_mode;
139 xcb_configure_window(dpy, e->window, mask, values);
141 } else if (IS_FLOATING(c)) {
142 width = c->floating_rectangle.width;
143 height = c->floating_rectangle.height;
145 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
146 c->floating_rectangle.x = e->x;
149 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
150 c->floating_rectangle.y = e->y;
153 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
157 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
161 apply_size_hints(c, &width, &height);
162 c->floating_rectangle.width = width;
163 c->floating_rectangle.height = height;
164 xcb_rectangle_t r = c->floating_rectangle;
166 window_move_resize(e->window, r.x, r.y, r.width, r.height);
168 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);
170 monitor_t *m = monitor_from_client(c);
171 if (m != loc.monitor) {
172 transfer_node(loc.monitor, loc.desktop, loc.node, m, m->desk, m->desk->focus);
175 if (c->state == STATE_PSEUDO_TILED) {
176 width = c->floating_rectangle.width;
177 height = c->floating_rectangle.height;
178 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
181 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
184 apply_size_hints(c, &width, &height);
185 if (width != c->floating_rectangle.width || height != c->floating_rectangle.height) {
186 c->floating_rectangle.width = width;
187 c->floating_rectangle.height = height;
188 arrange(loc.monitor, loc.desktop);
193 xcb_configure_notify_event_t evt;
194 unsigned int bw = c->border_width;
196 xcb_rectangle_t r = IS_FULLSCREEN(c) ? loc.monitor->rectangle : c->tiled_rectangle;
198 evt.response_type = XCB_CONFIGURE_NOTIFY;
199 evt.event = e->window;
200 evt.window = e->window;
201 evt.above_sibling = XCB_NONE;
205 evt.height = r.height;
206 evt.border_width = bw;
207 evt.override_redirect = false;
209 xcb_send_event(dpy, false, e->window, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
213 void destroy_notify(xcb_generic_event_t *evt)
215 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
217 unmanage_window(e->window);
220 void unmap_notify(xcb_generic_event_t *evt)
222 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
224 unmanage_window(e->window);
227 void property_notify(xcb_generic_event_t *evt)
229 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
231 if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS) {
236 if (!locate_window(e->window, &loc)) {
240 if (e->atom == XCB_ATOM_WM_HINTS) {
241 xcb_icccm_wm_hints_t hints;
242 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
243 (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
244 set_urgent(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
245 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
246 client_t *c = loc.node->client;
247 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &c->size_hints, NULL) == 1) {
248 arrange(loc.monitor, loc.desktop);
253 void client_message(xcb_generic_event_t *evt)
255 xcb_client_message_event_t *e = (xcb_client_message_event_t *) evt;
257 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
259 if (ewmh_locate_desktop(e->data.data32[0], &loc)) {
260 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
266 if (!locate_window(e->window, &loc)) {
270 if (e->type == ewmh->_NET_WM_STATE) {
271 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[1], e->data.data32[0]);
272 handle_state(loc.monitor, loc.desktop, loc.node, e->data.data32[2], e->data.data32[0]);
273 } else if (e->type == ewmh->_NET_ACTIVE_WINDOW) {
274 if ((ignore_ewmh_focus && e->data.data32[0] == XCB_EWMH_CLIENT_SOURCE_TYPE_NORMAL) ||
275 loc.node == mon->desk->focus) {
278 focus_node(loc.monitor, loc.desktop, loc.node);
279 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
281 if (ewmh_locate_desktop(e->data.data32[0], &dloc)) {
282 transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
284 } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
285 close_node(loc.node);
289 void focus_in(xcb_generic_event_t *evt)
291 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
293 if (e->mode == XCB_NOTIFY_MODE_GRAB || e->mode == XCB_NOTIFY_MODE_UNGRAB
294 || e->detail == XCB_NOTIFY_DETAIL_POINTER || e->detail == XCB_NOTIFY_DETAIL_POINTER_ROOT
295 || e->detail == XCB_NOTIFY_DETAIL_NONE) {
299 if (mon->desk->focus != NULL && e->event == mon->desk->focus->id) {
304 if (locate_window(e->event, &loc)) {
305 // prevent input focus stealing
306 update_input_focus();
310 void button_press(xcb_generic_event_t *evt)
312 xcb_button_press_event_t *e = (xcb_button_press_event_t *) evt;
315 case XCB_BUTTON_INDEX_1:
316 if (click_to_focus && cleaned_mask(e->state) == XCB_NONE) {
318 grab_pointer(ACTION_FOCUS);
320 grab_pointer(pointer_actions[0]);
323 case XCB_BUTTON_INDEX_2:
324 grab_pointer(pointer_actions[1]);
326 case XCB_BUTTON_INDEX_3:
327 grab_pointer(pointer_actions[2]);
330 xcb_allow_events(dpy, replay ? XCB_ALLOW_REPLAY_POINTER : XCB_ALLOW_SYNC_POINTER, e->time);
334 void enter_notify(xcb_generic_event_t *evt)
336 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
337 xcb_window_t win = e->event;
339 if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
340 (mon->desk->focus != NULL &&
341 mon->desk->focus->id == win)) {
345 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, motion_recorder), NULL);
351 if (wa->map_state == XCB_MAP_STATE_UNMAPPED) {
352 enable_motion_recorder();
354 disable_motion_recorder();
358 void motion_notify(xcb_generic_event_t *evt)
360 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
362 static uint16_t last_motion_x = 0, last_motion_y = 0;
363 static xcb_timestamp_t last_motion_time = 0;
365 int64_t dtime = e->time - last_motion_time;
368 last_motion_time = e->time;
369 last_motion_x = e->event_x;
370 last_motion_y = e->event_y;
374 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
379 xcb_window_t win = XCB_NONE;
380 xcb_point_t pt = {e->root_x, e->root_y};
381 query_pointer(&win, NULL);
383 bool pfm_backup = pointer_follows_monitor;
384 bool pff_backup = pointer_follows_focus;
386 pointer_follows_monitor = false;
387 pointer_follows_focus = false;
389 if (locate_window(win, &loc)) {
390 if (loc.node != mon->desk->focus) {
391 focus_node(loc.monitor, loc.desktop, loc.node);
394 monitor_t *m = monitor_from_point(pt);
395 if (m != NULL && m != mon) {
396 focus_node(m, m->desk, m->desk->focus);
399 pointer_follows_monitor = pfm_backup;
400 pointer_follows_focus = pff_backup;
403 disable_motion_recorder();
406 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
408 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
409 if (action == XCB_EWMH_WM_STATE_ADD) {
410 set_state(m, d, n, STATE_FULLSCREEN);
411 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
412 if (n->client->state == STATE_FULLSCREEN) {
413 set_state(m, d, n, n->client->last_state);
415 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
416 set_state(m, d, n, IS_FULLSCREEN(n->client) ? n->client->last_state : STATE_FULLSCREEN);
419 } else if (state == ewmh->_NET_WM_STATE_BELOW) {
420 if (action == XCB_EWMH_WM_STATE_ADD) {
421 set_layer(m, d, n, LAYER_BELOW);
422 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
423 if (n->client->layer == LAYER_BELOW) {
424 set_layer(m, d, n, n->client->last_layer);
426 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
427 set_layer(m, d, n, n->client->layer == LAYER_BELOW ? n->client->last_layer : LAYER_BELOW);
429 } else if (state == ewmh->_NET_WM_STATE_ABOVE) {
430 if (action == XCB_EWMH_WM_STATE_ADD) {
431 set_layer(m, d, n, LAYER_ABOVE);
432 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
433 if (n->client->layer == LAYER_ABOVE) {
434 set_layer(m, d, n, n->client->last_layer);
436 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
437 set_layer(m, d, n, n->client->layer == LAYER_ABOVE ? n->client->last_layer : LAYER_ABOVE);
439 } else if (state == ewmh->_NET_WM_STATE_STICKY) {
440 if (action == XCB_EWMH_WM_STATE_ADD) {
441 set_sticky(m, d, n, true);
442 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
443 set_sticky(m, d, n, false);
444 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
445 set_sticky(m, d, n, !n->sticky);
447 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
448 if (action == XCB_EWMH_WM_STATE_ADD) {
449 set_urgent(m, d, n, true);
450 } else if (action == XCB_EWMH_WM_STATE_REMOVE) {
451 set_urgent(m, d, n, false);
452 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) {
453 set_urgent(m, d, n, !n->client->urgent);
455 #define HANDLE_WM_STATE(s) \
456 } else if (state == ewmh->_NET_WM_STATE_##s) { \
457 if (action == XCB_EWMH_WM_STATE_ADD) { \
458 n->client->wm_flags |= WM_FLAG_##s; \
459 } else if (action == XCB_EWMH_WM_STATE_REMOVE) { \
460 n->client->wm_flags &= ~WM_FLAG_##s; \
461 } else if (action == XCB_EWMH_WM_STATE_TOGGLE) { \
462 n->client->wm_flags ^= WM_FLAG_##s; \
464 ewmh_wm_state_update(n);
465 HANDLE_WM_STATE(MODAL)
466 HANDLE_WM_STATE(MAXIMIZED_VERT)
467 HANDLE_WM_STATE(MAXIMIZED_HORZ)
468 HANDLE_WM_STATE(SHADED)
469 HANDLE_WM_STATE(SKIP_TASKBAR)
470 HANDLE_WM_STATE(SKIP_PAGER)
471 HANDLE_WM_STATE(HIDDEN)
473 #undef HANDLE_WM_STATE
476 void process_error(xcb_generic_event_t *evt)
478 xcb_request_error_t *e = (xcb_request_error_t *) evt;
479 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);