]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
Check output crossing on ENTER_NOTIFY to dockarea. (#2477)
[i3/i3] / src / handlers.c
1 #undef I3__FILE__
2 #define I3__FILE__ "handlers.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * handlers.c: Small handlers for various events (keypresses, focus changes,
10  *             …).
11  *
12  */
13 #include "all.h"
14
15 #include <time.h>
16 #include <float.h>
17 #include <sys/time.h>
18 #include <xcb/randr.h>
19 #define SN_API_NOT_YET_FROZEN 1
20 #include <libsn/sn-monitor.h>
21
22 int randr_base = -1;
23 int xkb_base = -1;
24 int xkb_current_group;
25
26 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
27    since it’d trigger an infinite loop of switching between the different windows when
28    changing workspaces */
29 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
30
31 /*
32  * Adds the given sequence to the list of events which are ignored.
33  * If this ignore should only affect a specific response_type, pass
34  * response_type, otherwise, pass -1.
35  *
36  * Every ignored sequence number gets garbage collected after 5 seconds.
37  *
38  */
39 void add_ignore_event(const int sequence, const int response_type) {
40     struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
41
42     event->sequence = sequence;
43     event->response_type = response_type;
44     event->added = time(NULL);
45
46     SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
47 }
48
49 /*
50  * Checks if the given sequence is ignored and returns true if so.
51  *
52  */
53 bool event_is_ignored(const int sequence, const int response_type) {
54     struct Ignore_Event *event;
55     time_t now = time(NULL);
56     for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
57         if ((now - event->added) > 5) {
58             struct Ignore_Event *save = event;
59             event = SLIST_NEXT(event, ignore_events);
60             SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
61             free(save);
62         } else
63             event = SLIST_NEXT(event, ignore_events);
64     }
65
66     SLIST_FOREACH(event, &ignore_events, ignore_events) {
67         if (event->sequence != sequence)
68             continue;
69
70         if (event->response_type != -1 &&
71             event->response_type != response_type)
72             continue;
73
74         /* instead of removing a sequence number we better wait until it gets
75          * garbage collected. it may generate multiple events (there are multiple
76          * enter_notifies for one configure_request, for example). */
77         //SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
78         //free(event);
79         return true;
80     }
81
82     return false;
83 }
84
85 /*
86  * Called with coordinates of an enter_notify event or motion_notify event
87  * to check if the user crossed virtual screen boundaries and adjust the
88  * current workspace, if so.
89  *
90  */
91 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
92     Output *output;
93
94     /* If the user disable focus follows mouse, we have nothing to do here */
95     if (config.disable_focus_follows_mouse)
96         return;
97
98     if ((output = get_output_containing(x, y)) == NULL) {
99         ELOG("ERROR: No such screen\n");
100         return;
101     }
102
103     if (output->con == NULL) {
104         ELOG("ERROR: The screen is not recognized by i3 (no container associated)\n");
105         return;
106     }
107
108     /* Focus the output on which the user moved their cursor */
109     Con *old_focused = focused;
110     Con *next = con_descend_focused(output_get_content(output->con));
111     /* Since we are switching outputs, this *must* be a different workspace, so
112      * call workspace_show() */
113     workspace_show(con_get_workspace(next));
114     con_focus(next);
115
116     /* If the focus changed, we re-render to get updated decorations */
117     if (old_focused != focused)
118         tree_render();
119 }
120
121 /*
122  * When the user moves the mouse pointer onto a window, this callback gets called.
123  *
124  */
125 static void handle_enter_notify(xcb_enter_notify_event_t *event) {
126     Con *con;
127
128     last_timestamp = event->time;
129
130     DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n",
131          event->event, event->mode, event->detail, event->sequence);
132     DLOG("coordinates %d, %d\n", event->event_x, event->event_y);
133     if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
134         DLOG("This was not a normal notify, ignoring\n");
135         return;
136     }
137     /* Some events are not interesting, because they were not generated
138      * actively by the user, but by reconfiguration of windows */
139     if (event_is_ignored(event->sequence, XCB_ENTER_NOTIFY)) {
140         DLOG("Event ignored\n");
141         return;
142     }
143
144     bool enter_child = false;
145     /* Get container by frame or by child window */
146     if ((con = con_by_frame_id(event->event)) == NULL) {
147         con = con_by_window_id(event->event);
148         enter_child = true;
149     }
150
151     /* If we cannot find the container, the user moved their cursor to the root
152      * window. In this case and if they used it to a dock, we need to focus the
153      * workspace on the correct output. */
154     if (con == NULL || con->parent->type == CT_DOCKAREA) {
155         DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
156         check_crossing_screen_boundary(event->root_x, event->root_y);
157         return;
158     }
159
160     /* see if the user entered the window on a certain window decoration */
161     layout_t layout = (enter_child ? con->parent->layout : con->layout);
162     if (layout == L_DEFAULT) {
163         Con *child;
164         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
165         if (rect_contains(child->deco_rect, event->event_x, event->event_y)) {
166             LOG("using child %p / %s instead!\n", child, child->name);
167             con = child;
168             break;
169         }
170     }
171
172     if (config.disable_focus_follows_mouse)
173         return;
174
175     /* if this container is already focused, there is nothing to do. */
176     if (con == focused)
177         return;
178
179     /* Get the currently focused workspace to check if the focus change also
180      * involves changing workspaces. If so, we need to call workspace_show() to
181      * correctly update state and send the IPC event. */
182     Con *ws = con_get_workspace(con);
183     if (ws != con_get_workspace(focused))
184         workspace_show(ws);
185
186     focused_id = XCB_NONE;
187     con_focus(con_descend_focused(con));
188     tree_render();
189
190     return;
191 }
192
193 /*
194  * When the user moves the mouse but does not change the active window
195  * (e.g. when having no windows opened but moving mouse on the root screen
196  * and crossing virtual screen boundaries), this callback gets called.
197  *
198  */
199 static void handle_motion_notify(xcb_motion_notify_event_t *event) {
200     last_timestamp = event->time;
201
202     /* Skip events where the pointer was over a child window, we are only
203      * interested in events on the root window. */
204     if (event->child != XCB_NONE)
205         return;
206
207     Con *con;
208     if ((con = con_by_frame_id(event->event)) == NULL) {
209         DLOG("MotionNotify for an unknown container, checking if it crosses screen boundaries.\n");
210         check_crossing_screen_boundary(event->root_x, event->root_y);
211         return;
212     }
213
214     if (config.disable_focus_follows_mouse)
215         return;
216
217     if (con->layout != L_DEFAULT && con->layout != L_SPLITV && con->layout != L_SPLITH)
218         return;
219
220     /* see over which rect the user is */
221     Con *current;
222     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
223         if (!rect_contains(current->deco_rect, event->event_x, event->event_y))
224             continue;
225
226         /* We found the rect, let’s see if this window is focused */
227         if (TAILQ_FIRST(&(con->focus_head)) == current)
228             return;
229
230         con_focus(current);
231         x_push_changes(croot);
232         return;
233     }
234 }
235
236 /*
237  * Called when the keyboard mapping changes (for example by using Xmodmap),
238  * we need to update our key bindings then (re-translate symbols).
239  *
240  */
241 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
242     if (event->request != XCB_MAPPING_KEYBOARD &&
243         event->request != XCB_MAPPING_MODIFIER)
244         return;
245
246     DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
247     xcb_refresh_keyboard_mapping(keysyms, event);
248
249     xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
250
251     ungrab_all_keys(conn);
252     translate_keysyms();
253     grab_all_keys(conn);
254
255     return;
256 }
257
258 /*
259  * A new window appeared on the screen (=was mapped), so let’s manage it.
260  *
261  */
262 static void handle_map_request(xcb_map_request_event_t *event) {
263     xcb_get_window_attributes_cookie_t cookie;
264
265     cookie = xcb_get_window_attributes_unchecked(conn, event->window);
266
267     DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
268     add_ignore_event(event->sequence, -1);
269
270     manage_window(event->window, cookie, false);
271     return;
272 }
273
274 /*
275  * Configure requests are received when the application wants to resize windows
276  * on their own.
277  *
278  * We generate a synthethic configure notify event to signalize the client its
279  * "new" position.
280  *
281  */
282 static void handle_configure_request(xcb_configure_request_event_t *event) {
283     Con *con;
284
285     DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
286          event->window, event->x, event->y, event->width, event->height);
287
288     /* For unmanaged windows, we just execute the configure request. As soon as
289      * it gets mapped, we will take over anyways. */
290     if ((con = con_by_window_id(event->window)) == NULL) {
291         DLOG("Configure request for unmanaged window, can do that.\n");
292
293         uint32_t mask = 0;
294         uint32_t values[7];
295         int c = 0;
296 #define COPY_MASK_MEMBER(mask_member, event_member) \
297     do {                                            \
298         if (event->value_mask & mask_member) {      \
299             mask |= mask_member;                    \
300             values[c++] = event->event_member;      \
301         }                                           \
302     } while (0)
303
304         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
305         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
306         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
307         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
308         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
309         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
310         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
311
312         xcb_configure_window(conn, event->window, mask, values);
313         xcb_flush(conn);
314
315         return;
316     }
317
318     DLOG("Configure request!\n");
319
320     Con *workspace = con_get_workspace(con),
321         *fullscreen = NULL;
322
323     /* There might not be a corresponding workspace for dock cons, therefore we
324      * have to be careful here. */
325     if (workspace) {
326         fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
327         if (!fullscreen)
328             fullscreen = con_get_fullscreen_con(workspace, CF_GLOBAL);
329     }
330
331     if (fullscreen != con && con_is_floating(con) && con_is_leaf(con)) {
332         /* find the height for the decorations */
333         int deco_height = con->deco_rect.height;
334         /* we actually need to apply the size/position changes to the *parent*
335          * container */
336         Rect bsr = con_border_style_rect(con);
337         if (con->border_style == BS_NORMAL) {
338             bsr.y += deco_height;
339             bsr.height -= deco_height;
340         }
341         Con *floatingcon = con->parent;
342
343         if (strcmp(con_get_workspace(floatingcon)->name, "__i3_scratch") == 0) {
344             DLOG("This is a scratchpad container, ignoring ConfigureRequest\n");
345             return;
346         }
347
348         Rect newrect = floatingcon->rect;
349
350         if (event->value_mask & XCB_CONFIG_WINDOW_X) {
351             newrect.x = event->x + (-1) * bsr.x;
352             DLOG("proposed x = %d, new x is %d\n", event->x, newrect.x);
353         }
354         if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
355             newrect.y = event->y + (-1) * bsr.y;
356             DLOG("proposed y = %d, new y is %d\n", event->y, newrect.y);
357         }
358         if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
359             newrect.width = event->width + (-1) * bsr.width;
360             newrect.width += con->border_width * 2;
361             DLOG("proposed width = %d, new width is %d (x11 border %d)\n",
362                  event->width, newrect.width, con->border_width);
363         }
364         if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
365             newrect.height = event->height + (-1) * bsr.height;
366             newrect.height += con->border_width * 2;
367             DLOG("proposed height = %d, new height is %d (x11 border %d)\n",
368                  event->height, newrect.height, con->border_width);
369         }
370
371         DLOG("Container is a floating leaf node, will do that.\n");
372         floating_reposition(floatingcon, newrect);
373         return;
374     }
375
376     /* Dock windows can be reconfigured in their height and moved to another output. */
377     if (con->parent && con->parent->type == CT_DOCKAREA) {
378         DLOG("Reconfiguring dock window (con = %p).\n", con);
379         if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
380             DLOG("Dock client wants to change height to %d, we can do that.\n", event->height);
381
382             con->geometry.height = event->height;
383             tree_render();
384         }
385
386         if (event->value_mask & XCB_CONFIG_WINDOW_X || event->value_mask & XCB_CONFIG_WINDOW_Y) {
387             int16_t x = event->value_mask & XCB_CONFIG_WINDOW_X ? event->x : (int16_t)con->geometry.x;
388             int16_t y = event->value_mask & XCB_CONFIG_WINDOW_Y ? event->y : (int16_t)con->geometry.y;
389
390             Con *current_output = con_get_output(con);
391             Output *target = get_output_containing(x, y);
392             if (target != NULL && current_output != target->con) {
393                 DLOG("Dock client is requested to be moved to output %s, moving it there.\n", target->name);
394                 Match *match;
395                 Con *nc = con_for_window(target->con, con->window, &match);
396                 DLOG("Dock client will be moved to container %p.\n", nc);
397                 con_detach(con);
398                 con_attach(con, nc, false);
399
400                 tree_render();
401             } else {
402                 DLOG("Dock client will not be moved, we only support moving it to another output.\n");
403             }
404         }
405     }
406
407     fake_absolute_configure_notify(con);
408
409     return;
410 }
411
412 /*
413  * Gets triggered upon a RandR screen change event, that is when the user
414  * changes the screen configuration in any way (mode, position, …)
415  *
416  */
417 static void handle_screen_change(xcb_generic_event_t *e) {
418     DLOG("RandR screen change\n");
419
420     /* The geometry of the root window is used for “fullscreen global” and
421      * changes when new outputs are added. */
422     xcb_get_geometry_cookie_t cookie = xcb_get_geometry(conn, root);
423     xcb_get_geometry_reply_t *reply = xcb_get_geometry_reply(conn, cookie, NULL);
424     if (reply == NULL) {
425         ELOG("Could not get geometry of the root window, exiting\n");
426         exit(1);
427     }
428     DLOG("root geometry reply: (%d, %d) %d x %d\n", reply->x, reply->y, reply->width, reply->height);
429
430     croot->rect.width = reply->width;
431     croot->rect.height = reply->height;
432
433     randr_query_outputs();
434
435     scratchpad_fix_resolution();
436
437     ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
438
439     return;
440 }
441
442 /*
443  * Our window decorations were unmapped. That means, the window will be killed
444  * now, so we better clean up before.
445  *
446  */
447 static void handle_unmap_notify_event(xcb_unmap_notify_event_t *event) {
448     DLOG("UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
449     xcb_get_input_focus_cookie_t cookie;
450     Con *con = con_by_window_id(event->window);
451     if (con == NULL) {
452         /* This could also be an UnmapNotify for the frame. We need to
453          * decrement the ignore_unmap counter. */
454         con = con_by_frame_id(event->window);
455         if (con == NULL) {
456             LOG("Not a managed window, ignoring UnmapNotify event\n");
457             return;
458         }
459
460         if (con->ignore_unmap > 0)
461             con->ignore_unmap--;
462         /* See the end of this function. */
463         cookie = xcb_get_input_focus(conn);
464         DLOG("ignore_unmap = %d for frame of container %p\n", con->ignore_unmap, con);
465         goto ignore_end;
466     }
467
468     /* See the end of this function. */
469     cookie = xcb_get_input_focus(conn);
470
471     if (con->ignore_unmap > 0) {
472         DLOG("ignore_unmap = %d, dec\n", con->ignore_unmap);
473         con->ignore_unmap--;
474         goto ignore_end;
475     }
476
477     /* Since we close the container, we need to unset _NET_WM_DESKTOP and
478      * _NET_WM_STATE according to the spec. */
479     xcb_delete_property(conn, event->window, A__NET_WM_DESKTOP);
480     xcb_delete_property(conn, event->window, A__NET_WM_STATE);
481
482     tree_close_internal(con, DONT_KILL_WINDOW, false, false);
483     tree_render();
484
485 ignore_end:
486     /* If the client (as opposed to i3) destroyed or unmapped a window, an
487      * EnterNotify event will follow (indistinguishable from an EnterNotify
488      * event caused by moving your mouse), causing i3 to set focus to whichever
489      * window is now visible.
490      *
491      * In a complex stacked or tabbed layout (take two v-split containers in a
492      * tabbed container), when the bottom window in tab2 is closed, the bottom
493      * window of tab1 is visible instead. X11 will thus send an EnterNotify
494      * event for the bottom window of tab1, while the focus should be set to
495      * the remaining window of tab2.
496      *
497      * Therefore, we ignore all EnterNotify events which have the same sequence
498      * as an UnmapNotify event. */
499     add_ignore_event(event->sequence, XCB_ENTER_NOTIFY);
500
501     /* Since we just ignored the sequence of this UnmapNotify, we want to make
502      * sure that following events use a different sequence. When putting xterm
503      * into fullscreen and moving the pointer to a different window, without
504      * using GetInputFocus, subsequent (legitimate) EnterNotify events arrived
505      * with the same sequence and thus were ignored (see ticket #609). */
506     free(xcb_get_input_focus_reply(conn, cookie, NULL));
507 }
508
509 /*
510  * A destroy notify event is sent when the window is not unmapped, but
511  * immediately destroyed (for example when starting a window and immediately
512  * killing the program which started it).
513  *
514  * We just pass on the event to the unmap notify handler (by copying the
515  * important fields in the event data structure).
516  *
517  */
518 static void handle_destroy_notify_event(xcb_destroy_notify_event_t *event) {
519     DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
520
521     xcb_unmap_notify_event_t unmap;
522     unmap.sequence = event->sequence;
523     unmap.event = event->event;
524     unmap.window = event->window;
525
526     handle_unmap_notify_event(&unmap);
527 }
528
529 static bool window_name_changed(i3Window *window, char *old_name) {
530     if ((old_name == NULL) && (window->name == NULL))
531         return false;
532
533     /* Either the old or the new one is NULL, but not both. */
534     if ((old_name == NULL) ^ (window->name == NULL))
535         return true;
536
537     return (strcmp(old_name, i3string_as_utf8(window->name)) != 0);
538 }
539
540 /*
541  * Called when a window changes its title
542  *
543  */
544 static bool handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
545                                      xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
546     Con *con;
547     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
548         return false;
549
550     char *old_name = (con->window->name != NULL ? sstrdup(i3string_as_utf8(con->window->name)) : NULL);
551
552     window_update_name(con->window, prop, false);
553
554     x_push_changes(croot);
555
556     if (window_name_changed(con->window, old_name))
557         ipc_send_window_event("title", con);
558
559     FREE(old_name);
560
561     return true;
562 }
563
564 /*
565  * Handles legacy window name updates (WM_NAME), see also src/window.c,
566  * window_update_name_legacy().
567  *
568  */
569 static bool handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
570                                             xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
571     Con *con;
572     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
573         return false;
574
575     char *old_name = (con->window->name != NULL ? sstrdup(i3string_as_utf8(con->window->name)) : NULL);
576
577     window_update_name_legacy(con->window, prop, false);
578
579     x_push_changes(croot);
580
581     if (window_name_changed(con->window, old_name))
582         ipc_send_window_event("title", con);
583
584     FREE(old_name);
585
586     return true;
587 }
588
589 /*
590  * Called when a window changes its WM_WINDOW_ROLE.
591  *
592  */
593 static bool handle_windowrole_change(void *data, xcb_connection_t *conn, uint8_t state,
594                                      xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
595     Con *con;
596     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
597         return false;
598
599     window_update_role(con->window, prop, false);
600
601     return true;
602 }
603
604 /*
605  * Expose event means we should redraw our windows (= title bar)
606  *
607  */
608 static void handle_expose_event(xcb_expose_event_t *event) {
609     Con *parent;
610
611     DLOG("window = %08x\n", event->window);
612
613     if ((parent = con_by_frame_id(event->window)) == NULL) {
614         LOG("expose event for unknown window, ignoring\n");
615         return;
616     }
617
618     /* Since we render to our surface on every change anyways, expose events
619      * only tell us that the X server lost (parts of) the window contents. We
620      * can handle that by copying the appropriate part from our surface to the
621      * window. */
622     draw_util_copy_surface(conn, &(parent->frame_buffer), &(parent->frame),
623                            event->x, event->y, event->x, event->y,
624                            event->width, event->height);
625     xcb_flush(conn);
626     return;
627 }
628
629 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
630 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
631 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
632 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
633 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
634 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
635 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
636 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
637 #define _NET_WM_MOVERESIZE_MOVE 8           /* movement only */
638 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9  /* size via keyboard */
639 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
640 #define _NET_WM_MOVERESIZE_CANCEL 11        /* cancel operation */
641
642 /*
643  * Handle client messages (EWMH)
644  *
645  */
646 static void handle_client_message(xcb_client_message_event_t *event) {
647     /* If this is a startup notification ClientMessage, the library will handle
648      * it and call our monitor_event() callback. */
649     if (sn_xcb_display_process_event(sndisplay, (xcb_generic_event_t *)event))
650         return;
651
652     LOG("ClientMessage for window 0x%08x\n", event->window);
653     if (event->type == A__NET_WM_STATE) {
654         if (event->format != 32 ||
655             (event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN &&
656              event->data.data32[1] != A__NET_WM_STATE_DEMANDS_ATTENTION &&
657              event->data.data32[1] != A__NET_WM_STATE_STICKY)) {
658             DLOG("Unknown atom in clientmessage of type %d\n", event->data.data32[1]);
659             return;
660         }
661
662         Con *con = con_by_window_id(event->window);
663         if (con == NULL) {
664             DLOG("Could not get window for client message\n");
665             return;
666         }
667
668         if (event->data.data32[1] == A__NET_WM_STATE_FULLSCREEN) {
669             /* Check if the fullscreen state should be toggled */
670             if ((con->fullscreen_mode != CF_NONE &&
671                  (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
672                   event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
673                 (con->fullscreen_mode == CF_NONE &&
674                  (event->data.data32[0] == _NET_WM_STATE_ADD ||
675                   event->data.data32[0] == _NET_WM_STATE_TOGGLE))) {
676                 DLOG("toggling fullscreen\n");
677                 con_toggle_fullscreen(con, CF_OUTPUT);
678             }
679         } else if (event->data.data32[1] == A__NET_WM_STATE_DEMANDS_ATTENTION) {
680             /* Check if the urgent flag must be set or not */
681             if (event->data.data32[0] == _NET_WM_STATE_ADD)
682                 con_set_urgency(con, true);
683             else if (event->data.data32[0] == _NET_WM_STATE_REMOVE)
684                 con_set_urgency(con, false);
685             else if (event->data.data32[0] == _NET_WM_STATE_TOGGLE)
686                 con_set_urgency(con, !con->urgent);
687         } else if (event->data.data32[1] == A__NET_WM_STATE_STICKY) {
688             DLOG("Received a client message to modify _NET_WM_STATE_STICKY.\n");
689             if (event->data.data32[0] == _NET_WM_STATE_ADD)
690                 con->sticky = true;
691             else if (event->data.data32[0] == _NET_WM_STATE_REMOVE)
692                 con->sticky = false;
693             else if (event->data.data32[0] == _NET_WM_STATE_TOGGLE)
694                 con->sticky = !con->sticky;
695
696             DLOG("New sticky status for con = %p is %i.\n", con, con->sticky);
697             ewmh_update_sticky(con->window->id, con->sticky);
698             output_push_sticky_windows(focused);
699             ewmh_update_wm_desktop();
700         }
701
702         tree_render();
703     } else if (event->type == A__NET_ACTIVE_WINDOW) {
704         if (event->format != 32)
705             return;
706
707         DLOG("_NET_ACTIVE_WINDOW: Window 0x%08x should be activated\n", event->window);
708
709         Con *con = con_by_window_id(event->window);
710         if (con == NULL) {
711             DLOG("Could not get window for client message\n");
712             return;
713         }
714
715         Con *ws = con_get_workspace(con);
716
717         if (ws == NULL) {
718             DLOG("Window is not being managed, ignoring _NET_ACTIVE_WINDOW\n");
719             return;
720         }
721
722         if (con_is_internal(ws)) {
723             DLOG("Workspace is internal, ignoring _NET_ACTIVE_WINDOW\n");
724             return;
725         }
726
727         /* data32[0] indicates the source of the request (application or pager) */
728         if (event->data.data32[0] == 2) {
729             /* Always focus the con if it is from a pager, because this is most
730              * likely from some user action */
731             DLOG("This request came from a pager. Focusing con = %p\n", con);
732             workspace_show(ws);
733             con_focus(con);
734         } else {
735             /* Request is from an application. */
736
737             if (config.focus_on_window_activation == FOWA_FOCUS || (config.focus_on_window_activation == FOWA_SMART && workspace_is_visible(ws))) {
738                 DLOG("Focusing con = %p\n", con);
739                 workspace_show(ws);
740                 con_focus(con);
741             } else if (config.focus_on_window_activation == FOWA_URGENT || (config.focus_on_window_activation == FOWA_SMART && !workspace_is_visible(ws))) {
742                 DLOG("Marking con = %p urgent\n", con);
743                 con_set_urgency(con, true);
744             } else
745                 DLOG("Ignoring request for con = %p.\n", con);
746         }
747
748         tree_render();
749     } else if (event->type == A_I3_SYNC) {
750         xcb_window_t window = event->data.data32[0];
751         uint32_t rnd = event->data.data32[1];
752         DLOG("[i3 sync protocol] Sending random value %d back to X11 window 0x%08x\n", rnd, window);
753
754         void *reply = scalloc(32, 1);
755         xcb_client_message_event_t *ev = reply;
756
757         ev->response_type = XCB_CLIENT_MESSAGE;
758         ev->window = window;
759         ev->type = A_I3_SYNC;
760         ev->format = 32;
761         ev->data.data32[0] = window;
762         ev->data.data32[1] = rnd;
763
764         xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
765         xcb_flush(conn);
766         free(reply);
767     } else if (event->type == A__NET_REQUEST_FRAME_EXTENTS) {
768         /*
769          * A client can request an estimate for the frame size which the window
770          * manager will put around it before actually mapping its window. Java
771          * does this (as of openjdk-7).
772          *
773          * Note that the calculation below is not entirely accurate — once you
774          * set a different border type, it’s off. We _could_ request all the
775          * window properties (which have to be set up at this point according
776          * to EWMH), but that seems rather elaborate. The standard explicitly
777          * says the application must cope with an estimate that is not entirely
778          * accurate.
779          */
780         DLOG("_NET_REQUEST_FRAME_EXTENTS for window 0x%08x\n", event->window);
781
782         /* The reply data: approximate frame size */
783         Rect r = {
784             config.default_border_width, /* left */
785             config.default_border_width, /* right */
786             config.font.height + 5,      /* top */
787             config.default_border_width  /* bottom */
788         };
789         xcb_change_property(
790             conn,
791             XCB_PROP_MODE_REPLACE,
792             event->window,
793             A__NET_FRAME_EXTENTS,
794             XCB_ATOM_CARDINAL, 32, 4,
795             &r);
796         xcb_flush(conn);
797     } else if (event->type == A__NET_CURRENT_DESKTOP) {
798         /* This request is used by pagers and bars to change the current
799          * desktop likely as a result of some user action. We interpret this as
800          * a request to focus the given workspace. See
801          * http://standards.freedesktop.org/wm-spec/latest/ar01s03.html#idm140251368135008
802          * */
803         DLOG("Request to change current desktop to index %d\n", event->data.data32[0]);
804         Con *ws = ewmh_get_workspace_by_index(event->data.data32[0]);
805         if (ws == NULL) {
806             ELOG("Could not determine workspace for this index, ignoring request.\n");
807             return;
808         }
809
810         DLOG("Handling request to focus workspace %s\n", ws->name);
811         workspace_show(ws);
812         tree_render();
813     } else if (event->type == A__NET_WM_DESKTOP) {
814         uint32_t index = event->data.data32[0];
815         DLOG("Request to move window %d to EWMH desktop index %d\n", event->window, index);
816
817         Con *con = con_by_window_id(event->window);
818         if (con == NULL) {
819             DLOG("Couldn't find con for window %d, ignoring the request.\n", event->window);
820             return;
821         }
822
823         if (index == NET_WM_DESKTOP_ALL) {
824             /* The window is requesting to be visible on all workspaces, so
825              * let's float it and make it sticky. */
826             DLOG("The window was requested to be visible on all workspaces, making it sticky and floating.\n");
827
828             floating_enable(con, false);
829
830             con->sticky = true;
831             ewmh_update_sticky(con->window->id, true);
832             output_push_sticky_windows(focused);
833         } else {
834             Con *ws = ewmh_get_workspace_by_index(index);
835             if (ws == NULL) {
836                 ELOG("Could not determine workspace for this index, ignoring request.\n");
837                 return;
838             }
839
840             con_move_to_workspace(con, ws, true, false, false);
841         }
842
843         tree_render();
844         ewmh_update_wm_desktop();
845     } else if (event->type == A__NET_CLOSE_WINDOW) {
846         /*
847          * Pagers wanting to close a window MUST send a _NET_CLOSE_WINDOW
848          * client message request to the root window.
849          * http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#idm140200472668896
850          */
851         Con *con = con_by_window_id(event->window);
852         if (con) {
853             DLOG("Handling _NET_CLOSE_WINDOW request (con = %p)\n", con);
854
855             if (event->data.data32[0])
856                 last_timestamp = event->data.data32[0];
857
858             tree_close_internal(con, KILL_WINDOW, false, false);
859             tree_render();
860         } else {
861             DLOG("Couldn't find con for _NET_CLOSE_WINDOW request. (window = %d)\n", event->window);
862         }
863     } else if (event->type == A__NET_WM_MOVERESIZE) {
864         /*
865          * Client-side decorated Gtk3 windows emit this signal when being
866          * dragged by their GtkHeaderBar
867          */
868         Con *con = con_by_window_id(event->window);
869         if (!con || !con_is_floating(con)) {
870             DLOG("Couldn't find con for _NET_WM_MOVERESIZE request, or con not floating (window = %d)\n", event->window);
871             return;
872         }
873         DLOG("Handling _NET_WM_MOVERESIZE request (con = %p)\n", con);
874         uint32_t direction = event->data.data32[2];
875         uint32_t x_root = event->data.data32[0];
876         uint32_t y_root = event->data.data32[1];
877         /* construct fake xcb_button_press_event_t */
878         xcb_button_press_event_t fake = {
879             .root_x = x_root,
880             .root_y = y_root,
881             .event_x = x_root - (con->rect.x),
882             .event_y = y_root - (con->rect.y)};
883         switch (direction) {
884             case _NET_WM_MOVERESIZE_MOVE:
885                 floating_drag_window(con->parent, &fake);
886                 break;
887             case _NET_WM_MOVERESIZE_SIZE_TOPLEFT... _NET_WM_MOVERESIZE_SIZE_LEFT:
888                 floating_resize_window(con->parent, false, &fake);
889                 break;
890             default:
891                 DLOG("_NET_WM_MOVERESIZE direction %d not implemented\n", direction);
892                 break;
893         }
894     } else {
895         DLOG("Skipping client message for unhandled type %d\n", event->type);
896     }
897 }
898
899 bool handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
900                         xcb_atom_t atom, xcb_get_property_reply_t *reply) {
901     Con *con;
902     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
903         return false;
904
905     window_update_type(con->window, reply);
906     return true;
907 }
908
909 /*
910  * Handles the size hints set by a window, but currently only the part necessary for displaying
911  * clients proportionally inside their frames (mplayer for example)
912  *
913  * See ICCCM 4.1.2.3 for more details
914  *
915  */
916 static bool handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
917                                 xcb_atom_t name, xcb_get_property_reply_t *reply) {
918     Con *con = con_by_window_id(window);
919     if (con == NULL) {
920         DLOG("Received WM_NORMAL_HINTS for unknown client\n");
921         return false;
922     }
923
924     xcb_size_hints_t size_hints;
925
926     //CLIENT_LOG(client);
927
928     /* If the hints were already in this event, use them, if not, request them */
929     if (reply != NULL)
930         xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
931     else
932         xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, con->window->id), &size_hints, NULL);
933
934     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
935         // TODO: Minimum size is not yet implemented
936         DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
937     }
938
939     bool changed = false;
940     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
941         if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
942             if (con->window->width_increment != size_hints.width_inc) {
943                 con->window->width_increment = size_hints.width_inc;
944                 changed = true;
945             }
946         if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
947             if (con->window->height_increment != size_hints.height_inc) {
948                 con->window->height_increment = size_hints.height_inc;
949                 changed = true;
950             }
951
952         if (changed)
953             DLOG("resize increments changed\n");
954     }
955
956     int base_width = 0, base_height = 0;
957
958     /* base_width/height are the desired size of the window.
959        We check if either the program-specified size or the program-specified
960        min-size is available */
961     if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
962         base_width = size_hints.base_width;
963         base_height = size_hints.base_height;
964     } else if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
965         /* TODO: is this right? icccm says not */
966         base_width = size_hints.min_width;
967         base_height = size_hints.min_height;
968     }
969
970     if (base_width != con->window->base_width ||
971         base_height != con->window->base_height) {
972         con->window->base_width = base_width;
973         con->window->base_height = base_height;
974         DLOG("client's base_height changed to %d\n", base_height);
975         DLOG("client's base_width changed to %d\n", base_width);
976         changed = true;
977     }
978
979     /* If no aspect ratio was set or if it was invalid, we ignore the hints */
980     if (!(size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT) ||
981         (size_hints.min_aspect_num <= 0) ||
982         (size_hints.min_aspect_den <= 0)) {
983         goto render_and_return;
984     }
985
986     /* XXX: do we really use rect here, not window_rect? */
987     double width = con->rect.width - base_width;
988     double height = con->rect.height - base_height;
989     /* Convert numerator/denominator to a double */
990     double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
991     double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
992
993     DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
994     DLOG("width = %f, height = %f\n", width, height);
995
996     /* Sanity checks, this is user-input, in a way */
997     if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
998         goto render_and_return;
999
1000     /* Check if we need to set proportional_* variables using the correct ratio */
1001     double aspect_ratio = 0.0;
1002     if ((width / height) < min_aspect) {
1003         aspect_ratio = min_aspect;
1004     } else if ((width / height) > max_aspect) {
1005         aspect_ratio = max_aspect;
1006     } else
1007         goto render_and_return;
1008
1009     if (fabs(con->window->aspect_ratio - aspect_ratio) > DBL_EPSILON) {
1010         con->window->aspect_ratio = aspect_ratio;
1011         changed = true;
1012     }
1013
1014 render_and_return:
1015     if (changed)
1016         tree_render();
1017     FREE(reply);
1018     return true;
1019 }
1020
1021 /*
1022  * Handles the WM_HINTS property for extracting the urgency state of the window.
1023  *
1024  */
1025 static bool handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1026                          xcb_atom_t name, xcb_get_property_reply_t *reply) {
1027     Con *con = con_by_window_id(window);
1028     if (con == NULL) {
1029         DLOG("Received WM_HINTS for unknown client\n");
1030         return false;
1031     }
1032
1033     bool urgency_hint;
1034     if (reply == NULL)
1035         reply = xcb_get_property_reply(conn, xcb_icccm_get_wm_hints(conn, window), NULL);
1036     window_update_hints(con->window, reply, &urgency_hint);
1037     con_set_urgency(con, urgency_hint);
1038     tree_render();
1039
1040     return true;
1041 }
1042
1043 /*
1044  * Handles the transient for hints set by a window, signalizing that this window is a popup window
1045  * for some other window.
1046  *
1047  * See ICCCM 4.1.2.6 for more details
1048  *
1049  */
1050 static bool handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1051                                  xcb_atom_t name, xcb_get_property_reply_t *prop) {
1052     Con *con;
1053
1054     if ((con = con_by_window_id(window)) == NULL || con->window == NULL) {
1055         DLOG("No such window\n");
1056         return false;
1057     }
1058
1059     if (prop == NULL) {
1060         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1061                                                                        false, window, XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 0, 32),
1062                                       NULL);
1063         if (prop == NULL)
1064             return false;
1065     }
1066
1067     window_update_transient_for(con->window, prop);
1068
1069     return true;
1070 }
1071
1072 /*
1073  * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
1074  * toolwindow (or similar) and to which window it belongs (logical parent).
1075  *
1076  */
1077 static bool handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1078                                        xcb_atom_t name, xcb_get_property_reply_t *prop) {
1079     Con *con;
1080     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
1081         return false;
1082
1083     if (prop == NULL) {
1084         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1085                                                                        false, window, A_WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32),
1086                                       NULL);
1087         if (prop == NULL)
1088             return false;
1089     }
1090
1091     window_update_leader(con->window, prop);
1092
1093     return true;
1094 }
1095
1096 /*
1097  * Handles FocusIn events which are generated by clients (i3’s focus changes
1098  * don’t generate FocusIn events due to a different EventMask) and updates the
1099  * decorations accordingly.
1100  *
1101  */
1102 static void handle_focus_in(xcb_focus_in_event_t *event) {
1103     DLOG("focus change in, for window 0x%08x\n", event->event);
1104     Con *con;
1105     if ((con = con_by_window_id(event->event)) == NULL || con->window == NULL)
1106         return;
1107     DLOG("That is con %p / %s\n", con, con->name);
1108
1109     if (event->mode == XCB_NOTIFY_MODE_GRAB ||
1110         event->mode == XCB_NOTIFY_MODE_UNGRAB) {
1111         DLOG("FocusIn event for grab/ungrab, ignoring\n");
1112         return;
1113     }
1114
1115     if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
1116         DLOG("notify detail is pointer, ignoring this event\n");
1117         return;
1118     }
1119
1120     if (focused_id == event->event) {
1121         DLOG("focus matches the currently focused window, not doing anything\n");
1122         return;
1123     }
1124
1125     /* Skip dock clients, they cannot get the i3 focus. */
1126     if (con->parent->type == CT_DOCKAREA) {
1127         DLOG("This is a dock client, not focusing.\n");
1128         return;
1129     }
1130
1131     DLOG("focus is different, updating decorations\n");
1132
1133     /* Get the currently focused workspace to check if the focus change also
1134      * involves changing workspaces. If so, we need to call workspace_show() to
1135      * correctly update state and send the IPC event. */
1136     Con *ws = con_get_workspace(con);
1137     if (ws != con_get_workspace(focused))
1138         workspace_show(ws);
1139
1140     con_focus(con);
1141     /* We update focused_id because we don’t need to set focus again */
1142     focused_id = event->event;
1143     x_push_changes(croot);
1144     return;
1145 }
1146
1147 /*
1148  * Handles the WM_CLASS property for assignments and criteria selection.
1149  *
1150  */
1151 static bool handle_class_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1152                                 xcb_atom_t name, xcb_get_property_reply_t *prop) {
1153     Con *con;
1154     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
1155         return false;
1156
1157     if (prop == NULL) {
1158         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1159                                                                        false, window, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 0, 32),
1160                                       NULL);
1161
1162         if (prop == NULL)
1163             return false;
1164     }
1165
1166     window_update_class(con->window, prop, false);
1167
1168     return true;
1169 }
1170
1171 /*
1172  * Handles the _MOTIF_WM_HINTS property of specifing window deocration settings.
1173  *
1174  */
1175 static bool handle_motif_hints_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1176                                       xcb_atom_t name, xcb_get_property_reply_t *prop) {
1177     Con *con;
1178     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
1179         return false;
1180
1181     if (prop == NULL) {
1182         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1183                                                                        false, window, A__MOTIF_WM_HINTS, XCB_GET_PROPERTY_TYPE_ANY, 0, 5 * sizeof(uint64_t)),
1184                                       NULL);
1185
1186         if (prop == NULL)
1187             return false;
1188     }
1189
1190     border_style_t motif_border_style;
1191     window_update_motif_hints(con->window, prop, &motif_border_style);
1192
1193     if (motif_border_style != con->border_style && motif_border_style != BS_NORMAL) {
1194         DLOG("Update border style of con %p to %d\n", con, motif_border_style);
1195         con_set_border_style(con, motif_border_style, con->current_border_width);
1196
1197         x_push_changes(croot);
1198     }
1199
1200     return true;
1201 }
1202
1203 /*
1204  * Handles the _NET_WM_STRUT_PARTIAL property for allocating space for dock clients.
1205  *
1206  */
1207 static bool handle_strut_partial_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1208                                         xcb_atom_t name, xcb_get_property_reply_t *prop) {
1209     DLOG("strut partial change for window 0x%08x\n", window);
1210
1211     Con *con;
1212     if ((con = con_by_window_id(window)) == NULL || con->window == NULL) {
1213         return false;
1214     }
1215
1216     if (prop == NULL) {
1217         xcb_generic_error_t *err = NULL;
1218         xcb_get_property_cookie_t strut_cookie = xcb_get_property(conn, false, window, A__NET_WM_STRUT_PARTIAL,
1219                                                                   XCB_GET_PROPERTY_TYPE_ANY, 0, UINT32_MAX);
1220         prop = xcb_get_property_reply(conn, strut_cookie, &err);
1221
1222         if (err != NULL) {
1223             DLOG("got error when getting strut partial property: %d\n", err->error_code);
1224             free(err);
1225             return false;
1226         }
1227
1228         if (prop == NULL) {
1229             return false;
1230         }
1231     }
1232
1233     DLOG("That is con %p / %s\n", con, con->name);
1234
1235     window_update_strut_partial(con->window, prop);
1236
1237     /* we only handle this change for dock clients */
1238     if (con->parent == NULL || con->parent->type != CT_DOCKAREA) {
1239         return true;
1240     }
1241
1242     Con *search_at = croot;
1243     Con *output = con_get_output(con);
1244     if (output != NULL) {
1245         DLOG("Starting search at output %s\n", output->name);
1246         search_at = output;
1247     }
1248
1249     /* find out the desired position of this dock window */
1250     if (con->window->reserved.top > 0 && con->window->reserved.bottom == 0) {
1251         DLOG("Top dock client\n");
1252         con->window->dock = W_DOCK_TOP;
1253     } else if (con->window->reserved.top == 0 && con->window->reserved.bottom > 0) {
1254         DLOG("Bottom dock client\n");
1255         con->window->dock = W_DOCK_BOTTOM;
1256     } else {
1257         DLOG("Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n");
1258         if (con->geometry.y < (search_at->rect.height / 2)) {
1259             DLOG("geom->y = %d < rect.height / 2 = %d, it is a top dock client\n",
1260                  con->geometry.y, (search_at->rect.height / 2));
1261             con->window->dock = W_DOCK_TOP;
1262         } else {
1263             DLOG("geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n",
1264                  con->geometry.y, (search_at->rect.height / 2));
1265             con->window->dock = W_DOCK_BOTTOM;
1266         }
1267     }
1268
1269     /* find the dockarea */
1270     Con *dockarea = con_for_window(search_at, con->window, NULL);
1271     assert(dockarea != NULL);
1272
1273     /* attach the dock to the dock area */
1274     con_detach(con);
1275     con->parent = dockarea;
1276     TAILQ_INSERT_HEAD(&(dockarea->focus_head), con, focused);
1277     TAILQ_INSERT_HEAD(&(dockarea->nodes_head), con, nodes);
1278
1279     tree_render();
1280
1281     return true;
1282 }
1283
1284 /* Returns false if the event could not be processed (e.g. the window could not
1285  * be found), true otherwise */
1286 typedef bool (*cb_property_handler_t)(void *data, xcb_connection_t *c, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *property);
1287
1288 struct property_handler_t {
1289     xcb_atom_t atom;
1290     uint32_t long_len;
1291     cb_property_handler_t cb;
1292 };
1293
1294 static struct property_handler_t property_handlers[] = {
1295     {0, 128, handle_windowname_change},
1296     {0, UINT_MAX, handle_hints},
1297     {0, 128, handle_windowname_change_legacy},
1298     {0, UINT_MAX, handle_normal_hints},
1299     {0, UINT_MAX, handle_clientleader_change},
1300     {0, UINT_MAX, handle_transient_for},
1301     {0, 128, handle_windowrole_change},
1302     {0, 128, handle_class_change},
1303     {0, UINT_MAX, handle_strut_partial_change},
1304     {0, UINT_MAX, handle_window_type},
1305     {0, 5 * sizeof(uint64_t), handle_motif_hints_change}};
1306 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
1307
1308 /*
1309  * Sets the appropriate atoms for the property handlers after the atoms were
1310  * received from X11
1311  *
1312  */
1313 void property_handlers_init(void) {
1314     sn_monitor_context_new(sndisplay, conn_screen, startup_monitor_event, NULL, NULL);
1315
1316     property_handlers[0].atom = A__NET_WM_NAME;
1317     property_handlers[1].atom = XCB_ATOM_WM_HINTS;
1318     property_handlers[2].atom = XCB_ATOM_WM_NAME;
1319     property_handlers[3].atom = XCB_ATOM_WM_NORMAL_HINTS;
1320     property_handlers[4].atom = A_WM_CLIENT_LEADER;
1321     property_handlers[5].atom = XCB_ATOM_WM_TRANSIENT_FOR;
1322     property_handlers[6].atom = A_WM_WINDOW_ROLE;
1323     property_handlers[7].atom = XCB_ATOM_WM_CLASS;
1324     property_handlers[8].atom = A__NET_WM_STRUT_PARTIAL;
1325     property_handlers[9].atom = A__NET_WM_WINDOW_TYPE;
1326     property_handlers[10].atom = A__MOTIF_WM_HINTS;
1327 }
1328
1329 static void property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
1330     struct property_handler_t *handler = NULL;
1331     xcb_get_property_reply_t *propr = NULL;
1332
1333     for (size_t c = 0; c < sizeof(property_handlers) / sizeof(struct property_handler_t); c++) {
1334         if (property_handlers[c].atom != atom)
1335             continue;
1336
1337         handler = &property_handlers[c];
1338         break;
1339     }
1340
1341     if (handler == NULL) {
1342         //DLOG("Unhandled property notify for atom %d (0x%08x)\n", atom, atom);
1343         return;
1344     }
1345
1346     if (state != XCB_PROPERTY_DELETE) {
1347         xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->long_len);
1348         propr = xcb_get_property_reply(conn, cookie, 0);
1349     }
1350
1351     /* the handler will free() the reply unless it returns false */
1352     if (!handler->cb(NULL, conn, state, window, atom, propr))
1353         FREE(propr);
1354 }
1355
1356 /*
1357  * Takes an xcb_generic_event_t and calls the appropriate handler, based on the
1358  * event type.
1359  *
1360  */
1361 void handle_event(int type, xcb_generic_event_t *event) {
1362     if (type != XCB_MOTION_NOTIFY)
1363         DLOG("event type %d, xkb_base %d\n", type, xkb_base);
1364
1365     if (randr_base > -1 &&
1366         type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
1367         handle_screen_change(event);
1368         return;
1369     }
1370
1371     if (xkb_base > -1 && type == xkb_base) {
1372         DLOG("xkb event, need to handle it.\n");
1373
1374         xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
1375         if (state->xkbType == XCB_XKB_NEW_KEYBOARD_NOTIFY) {
1376             DLOG("xkb new keyboard notify, sequence %d, time %d\n", state->sequence, state->time);
1377             xcb_key_symbols_free(keysyms);
1378             keysyms = xcb_key_symbols_alloc(conn);
1379             if (((xcb_xkb_new_keyboard_notify_event_t *)event)->changed & XCB_XKB_NKN_DETAIL_KEYCODES)
1380                 (void)load_keymap();
1381             ungrab_all_keys(conn);
1382             translate_keysyms();
1383             grab_all_keys(conn);
1384         } else if (state->xkbType == XCB_XKB_MAP_NOTIFY) {
1385             if (event_is_ignored(event->sequence, type)) {
1386                 DLOG("Ignoring map notify event for sequence %d.\n", state->sequence);
1387             } else {
1388                 DLOG("xkb map notify, sequence %d, time %d\n", state->sequence, state->time);
1389                 add_ignore_event(event->sequence, type);
1390                 xcb_key_symbols_free(keysyms);
1391                 keysyms = xcb_key_symbols_alloc(conn);
1392                 ungrab_all_keys(conn);
1393                 translate_keysyms();
1394                 grab_all_keys(conn);
1395                 (void)load_keymap();
1396             }
1397         } else if (state->xkbType == XCB_XKB_STATE_NOTIFY) {
1398             DLOG("xkb state group = %d\n", state->group);
1399             if (xkb_current_group == state->group)
1400                 return;
1401             xkb_current_group = state->group;
1402             ungrab_all_keys(conn);
1403             grab_all_keys(conn);
1404         }
1405
1406         return;
1407     }
1408
1409     switch (type) {
1410         case XCB_KEY_PRESS:
1411         case XCB_KEY_RELEASE:
1412             handle_key_press((xcb_key_press_event_t *)event);
1413             break;
1414
1415         case XCB_BUTTON_PRESS:
1416         case XCB_BUTTON_RELEASE:
1417             handle_button_press((xcb_button_press_event_t *)event);
1418             break;
1419
1420         case XCB_MAP_REQUEST:
1421             handle_map_request((xcb_map_request_event_t *)event);
1422             break;
1423
1424         case XCB_UNMAP_NOTIFY:
1425             handle_unmap_notify_event((xcb_unmap_notify_event_t *)event);
1426             break;
1427
1428         case XCB_DESTROY_NOTIFY:
1429             handle_destroy_notify_event((xcb_destroy_notify_event_t *)event);
1430             break;
1431
1432         case XCB_EXPOSE:
1433             handle_expose_event((xcb_expose_event_t *)event);
1434             break;
1435
1436         case XCB_MOTION_NOTIFY:
1437             handle_motion_notify((xcb_motion_notify_event_t *)event);
1438             break;
1439
1440         /* Enter window = user moved their mouse over the window */
1441         case XCB_ENTER_NOTIFY:
1442             handle_enter_notify((xcb_enter_notify_event_t *)event);
1443             break;
1444
1445         /* Client message are sent to the root window. The only interesting
1446          * client message for us is _NET_WM_STATE, we honour
1447          * _NET_WM_STATE_FULLSCREEN and _NET_WM_STATE_DEMANDS_ATTENTION */
1448         case XCB_CLIENT_MESSAGE:
1449             handle_client_message((xcb_client_message_event_t *)event);
1450             break;
1451
1452         /* Configure request = window tried to change size on its own */
1453         case XCB_CONFIGURE_REQUEST:
1454             handle_configure_request((xcb_configure_request_event_t *)event);
1455             break;
1456
1457         /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
1458         case XCB_MAPPING_NOTIFY:
1459             handle_mapping_notify((xcb_mapping_notify_event_t *)event);
1460             break;
1461
1462         case XCB_FOCUS_IN:
1463             handle_focus_in((xcb_focus_in_event_t *)event);
1464             break;
1465
1466         case XCB_PROPERTY_NOTIFY: {
1467             xcb_property_notify_event_t *e = (xcb_property_notify_event_t *)event;
1468             last_timestamp = e->time;
1469             property_notify(e->state, e->window, e->atom);
1470             break;
1471         }
1472
1473         default:
1474             //DLOG("Unhandled event of type %d\n", type);
1475             break;
1476     }
1477 }