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.
35 void handle_event(xcb_generic_event_t *evt)
37 uint8_t resp_type = XCB_EVENT_RESPONSE_TYPE(evt);
42 case XCB_DESTROY_NOTIFY:
45 case XCB_UNMAP_NOTIFY:
48 case XCB_CLIENT_MESSAGE:
51 case XCB_CONFIGURE_REQUEST:
52 configure_request(evt);
54 case XCB_PROPERTY_NOTIFY:
57 case XCB_ENTER_NOTIFY:
60 case XCB_MOTION_NOTIFY:
70 if (randr && resp_type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY)
76 void map_request(xcb_generic_event_t *evt)
78 xcb_map_request_event_t *e = (xcb_map_request_event_t *) evt;
80 PRINTF("map request %X\n", e->window);
82 schedule_window(e->window);
85 void configure_request(xcb_generic_event_t *evt)
87 xcb_configure_request_event_t *e = (xcb_configure_request_event_t *) evt;
89 PRINTF("configure request %X\n", e->window);
92 bool is_managed = locate_window(e->window, &loc);
93 client_t *c = (is_managed ? loc.node->client : NULL);
96 if (is_managed && !is_floating(c)) {
97 if (e->value_mask & XCB_CONFIG_WINDOW_X)
98 c->floating_rectangle.x = e->x;
99 if (e->value_mask & XCB_CONFIG_WINDOW_Y)
100 c->floating_rectangle.y = e->y;
101 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH)
103 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
107 restrain_floating_width(c, &w);
108 c->floating_rectangle.width = w;
112 restrain_floating_height(c, &h);
113 c->floating_rectangle.height = h;
116 xcb_configure_notify_event_t evt;
117 xcb_rectangle_t rect;
118 xcb_window_t win = c->window;
119 unsigned int bw = c->border_width;
122 rect = loc.monitor->rectangle;
124 rect = c->tiled_rectangle;
126 evt.response_type = XCB_CONFIGURE_NOTIFY;
129 evt.above_sibling = XCB_NONE;
132 evt.width = rect.width;
133 evt.height = rect.height;
134 evt.border_width = bw;
135 evt.override_redirect = false;
137 xcb_send_event(dpy, false, win, XCB_EVENT_MASK_STRUCTURE_NOTIFY, (const char *) &evt);
140 arrange(loc.monitor, loc.desktop);
144 unsigned short i = 0;
146 if (e->value_mask & XCB_CONFIG_WINDOW_X) {
147 mask |= XCB_CONFIG_WINDOW_X;
150 c->floating_rectangle.x = e->x;
153 if (e->value_mask & XCB_CONFIG_WINDOW_Y) {
154 mask |= XCB_CONFIG_WINDOW_Y;
157 c->floating_rectangle.y = e->y;
160 if (e->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
161 mask |= XCB_CONFIG_WINDOW_WIDTH;
164 restrain_floating_width(c, &w);
165 c->floating_rectangle.width = w;
170 if (e->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
171 mask |= XCB_CONFIG_WINDOW_HEIGHT;
174 restrain_floating_height(c, &h);
175 c->floating_rectangle.height = h;
180 if (!is_managed && e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
181 mask |= XCB_CONFIG_WINDOW_BORDER_WIDTH;
182 values[i++] = e->border_width;
185 if (e->value_mask & XCB_CONFIG_WINDOW_SIBLING) {
186 mask |= XCB_CONFIG_WINDOW_SIBLING;
187 values[i++] = e->sibling;
190 if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
191 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
192 values[i++] = e->stack_mode;
195 xcb_configure_window(dpy, e->window, mask, values);
199 translate_client(monitor_from_client(c), loc.monitor, c);
202 void destroy_notify(xcb_generic_event_t *evt)
204 xcb_destroy_notify_event_t *e = (xcb_destroy_notify_event_t *) evt;
206 PRINTF("destroy notify %X\n", e->window);
208 unmanage_window(e->window);
211 void unmap_notify(xcb_generic_event_t *evt)
213 xcb_unmap_notify_event_t *e = (xcb_unmap_notify_event_t *) evt;
215 PRINTF("unmap notify %X\n", e->window);
217 unmanage_window(e->window);
220 void property_notify(xcb_generic_event_t *evt)
222 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *) evt;
224 /* PRINTF("property notify %X\n", e->window); */
226 if (e->atom != XCB_ATOM_WM_HINTS && e->atom != XCB_ATOM_WM_NORMAL_HINTS)
230 if (!locate_window(e->window, &loc))
233 if (e->atom == XCB_ATOM_WM_HINTS) {
234 xcb_icccm_wm_hints_t hints;
235 if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, e->window), &hints, NULL) == 1 &&
236 (hints.flags & XCB_ICCCM_WM_HINT_X_URGENCY))
237 set_urgency(loc.monitor, loc.desktop, loc.node, xcb_icccm_wm_hints_get_urgency(&hints));
238 } else if (e->atom == XCB_ATOM_WM_NORMAL_HINTS) {
239 client_t *c = loc.node->client;
240 xcb_size_hints_t size_hints;
241 if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, e->window), &size_hints, NULL) == 1 &&
242 (size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE | XCB_ICCCM_SIZE_HINT_P_MAX_SIZE))) {
243 c->min_width = size_hints.min_width;
244 c->max_width = size_hints.max_width;
245 c->min_height = size_hints.min_height;
246 c->max_height = size_hints.max_height;
247 int w = c->floating_rectangle.width;
248 int h = c->floating_rectangle.height;
249 restrain_floating_size(c, &w, &h);
250 c->floating_rectangle.width = w;
251 c->floating_rectangle.height = h;
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 PRINTF("client message %X %u\n", e->window, e->type);
263 if (e->type == ewmh->_NET_CURRENT_DESKTOP) {
265 if (ewmh_locate_desktop(e->data.data32[0], &loc))
266 focus_node(loc.monitor, loc.desktop, loc.desktop->focus);
271 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)
281 if (loc.desktop->focus->client->fullscreen && loc.desktop->focus != loc.node) {
282 set_fullscreen(loc.desktop->focus, false);
283 arrange(loc.monitor, loc.desktop);
285 focus_node(loc.monitor, loc.desktop, loc.node);
286 } else if (e->type == ewmh->_NET_WM_DESKTOP) {
288 if (ewmh_locate_desktop(e->data.data32[0], &dloc))
289 transfer_node(loc.monitor, loc.desktop, loc.node, dloc.monitor, dloc.desktop, dloc.desktop->focus);
290 } else if (e->type == ewmh->_NET_CLOSE_WINDOW) {
291 window_close(loc.node);
295 void focus_in(xcb_generic_event_t *evt)
297 xcb_focus_in_event_t *e = (xcb_focus_in_event_t *) evt;
299 /* PRINTF("focus in %X %d %d\n", e->event, e->mode, e->detail); */
301 if (e->mode == XCB_NOTIFY_MODE_GRAB ||
302 e->mode == XCB_NOTIFY_MODE_UNGRAB)
304 /* prevent focus stealing */
305 if ((e->detail == XCB_NOTIFY_DETAIL_ANCESTOR ||
306 e->detail == XCB_NOTIFY_DETAIL_INFERIOR ||
307 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL ||
308 e->detail == XCB_NOTIFY_DETAIL_NONLINEAR) &&
309 (mon->desk->focus == NULL ||
310 mon->desk->focus->client->window != e->event))
311 update_input_focus();
314 void enter_notify(xcb_generic_event_t *evt)
316 xcb_enter_notify_event_t *e = (xcb_enter_notify_event_t *) evt;
317 xcb_window_t win = e->event;
319 PRINTF("enter notify %X %d %d\n", win, e->mode, e->detail);
321 if (e->mode != XCB_NOTIFY_MODE_NORMAL ||
322 (mon->desk->focus != NULL &&
323 mon->desk->focus->client->window == win)) {
327 xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, motion_recorder), NULL);
333 if (wa->map_state == XCB_MAP_STATE_UNMAPPED) {
334 enable_motion_recorder();
336 disable_motion_recorder();
340 void motion_notify(xcb_generic_event_t *evt)
342 xcb_motion_notify_event_t *e = (xcb_motion_notify_event_t *) evt;
344 PRINTF("motion notify %X %i %i\n", e->event, e->root_x, e->root_y);
346 int dtime = e->time - last_motion_time;
348 last_motion_time = e->time;
349 last_motion_x = e->event_x;
350 last_motion_y = e->event_y;
354 int mdist = abs(e->event_x - last_motion_x) + abs(e->event_y - last_motion_y);
359 xcb_window_t win = XCB_NONE;
360 xcb_point_t pt = {e->root_x, e->root_y};
361 query_pointer(&win, NULL);
363 bool pfm_backup = pointer_follows_monitor;
364 bool pff_backup = pointer_follows_focus;
366 pointer_follows_monitor = false;
367 pointer_follows_focus = false;
369 if (locate_window(win, &loc)) {
370 if (loc.node != mon->desk->focus) {
371 focus_node(loc.monitor, loc.desktop, loc.node);
374 monitor_t *m = monitor_from_point(pt);
375 if (m != NULL && m != mon) {
376 focus_node(m, m->desk, m->desk->focus);
379 pointer_follows_monitor = pfm_backup;
380 pointer_follows_focus = pff_backup;
383 disable_motion_recorder();
386 void handle_state(monitor_t *m, desktop_t *d, node_t *n, xcb_atom_t state, unsigned int action)
388 if (state == ewmh->_NET_WM_STATE_FULLSCREEN) {
389 if (action == XCB_EWMH_WM_STATE_ADD)
390 set_fullscreen(n, true);
391 else if (action == XCB_EWMH_WM_STATE_REMOVE)
392 set_fullscreen(n, false);
393 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
394 set_fullscreen(n, !n->client->fullscreen);
396 } else if (state == ewmh->_NET_WM_STATE_STICKY) {
397 if (action == XCB_EWMH_WM_STATE_ADD)
398 set_sticky(m, d, n, true);
399 else if (action == XCB_EWMH_WM_STATE_REMOVE)
400 set_sticky(m, d, n, false);
401 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
402 set_sticky(m, d, n, !n->client->sticky);
403 } else if (state == ewmh->_NET_WM_STATE_DEMANDS_ATTENTION) {
404 if (action == XCB_EWMH_WM_STATE_ADD)
405 set_urgency(m, d, n, true);
406 else if (action == XCB_EWMH_WM_STATE_REMOVE)
407 set_urgency(m, d, n, false);
408 else if (action == XCB_EWMH_WM_STATE_TOGGLE)
409 set_urgency(m, d, n, !n->client->urgent);
413 void process_error(xcb_generic_event_t *evt)
415 xcb_request_error_t *e = (xcb_request_error_t *) evt;
416 warn("Failed request: %s, %s: %d.\n", xcb_event_get_request_label(e->major_opcode), xcb_event_get_error_label(e->error_code), e->bad_value);