]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
explicitly set filenames to $(basename __FILE__)
[i3/i3] / src / handlers.c
1 #line 2 "handlers.c"
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  * i3 - an improved dynamic tiling window manager
6  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
7  *
8  * handlers.c: Small handlers for various events (keypresses, focus changes,
9  *             …).
10  *
11  */
12 #include "all.h"
13
14 #include <time.h>
15 #include <sys/time.h>
16 #include <xcb/randr.h>
17 #include <X11/XKBlib.h>
18 #define SN_API_NOT_YET_FROZEN 1
19 #include <libsn/sn-monitor.h>
20
21 int randr_base = -1;
22
23 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
24    since it’d trigger an infinite loop of switching between the different windows when
25    changing workspaces */
26 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
27
28 /*
29  * Adds the given sequence to the list of events which are ignored.
30  * If this ignore should only affect a specific response_type, pass
31  * response_type, otherwise, pass -1.
32  *
33  * Every ignored sequence number gets garbage collected after 5 seconds.
34  *
35  */
36 void add_ignore_event(const int sequence, const int response_type) {
37     struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
38
39     event->sequence = sequence;
40     event->response_type = response_type;
41     event->added = time(NULL);
42
43     SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
44 }
45
46 /*
47  * Checks if the given sequence is ignored and returns true if so.
48  *
49  */
50 bool event_is_ignored(const int sequence, const int response_type) {
51     struct Ignore_Event *event;
52     time_t now = time(NULL);
53     for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
54         if ((now - event->added) > 5) {
55             struct Ignore_Event *save = event;
56             event = SLIST_NEXT(event, ignore_events);
57             SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
58             free(save);
59         } else event = SLIST_NEXT(event, ignore_events);
60     }
61
62     SLIST_FOREACH(event, &ignore_events, ignore_events) {
63         if (event->sequence != sequence)
64             continue;
65
66         if (event->response_type != -1 &&
67             event->response_type != response_type)
68             continue;
69
70         /* instead of removing a sequence number we better wait until it gets
71          * garbage collected. it may generate multiple events (there are multiple
72          * enter_notifies for one configure_request, for example). */
73         //SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
74         //free(event);
75         return true;
76     }
77
78     return false;
79 }
80
81 /*
82  * Called with coordinates of an enter_notify event or motion_notify event
83  * to check if the user crossed virtual screen boundaries and adjust the
84  * current workspace, if so.
85  *
86  */
87 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
88     Output *output;
89
90     /* If the user disable focus follows mouse, we have nothing to do here */
91     if (config.disable_focus_follows_mouse)
92         return;
93
94     if ((output = get_output_containing(x, y)) == NULL) {
95         ELOG("ERROR: No such screen\n");
96         return;
97     }
98
99     if (output->con == NULL) {
100         ELOG("ERROR: The screen is not recognized by i3 (no container associated)\n");
101         return;
102     }
103
104     /* Focus the output on which the user moved his cursor */
105     Con *old_focused = focused;
106     Con *next = con_descend_focused(output_get_content(output->con));
107     /* Since we are switching outputs, this *must* be a different workspace, so
108      * call workspace_show() */
109     workspace_show(con_get_workspace(next));
110     con_focus(next);
111
112     /* If the focus changed, we re-render to get updated decorations */
113     if (old_focused != focused)
114         tree_render();
115 }
116
117 /*
118  * When the user moves the mouse pointer onto a window, this callback gets called.
119  *
120  */
121 static void handle_enter_notify(xcb_enter_notify_event_t *event) {
122     Con *con;
123
124     last_timestamp = event->time;
125
126     DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n",
127          event->event, event->mode, event->detail, event->sequence);
128     DLOG("coordinates %d, %d\n", event->event_x, event->event_y);
129     if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
130         DLOG("This was not a normal notify, ignoring\n");
131         return;
132     }
133     /* Some events are not interesting, because they were not generated
134      * actively by the user, but by reconfiguration of windows */
135     if (event_is_ignored(event->sequence, XCB_ENTER_NOTIFY)) {
136         DLOG("Event ignored\n");
137         return;
138     }
139
140     bool enter_child = false;
141     /* Get container by frame or by child window */
142     if ((con = con_by_frame_id(event->event)) == NULL) {
143         con = con_by_window_id(event->event);
144         enter_child = true;
145     }
146
147     /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
148     if (con == NULL) {
149         DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
150         check_crossing_screen_boundary(event->root_x, event->root_y);
151         return;
152     }
153
154     if (con->parent->type == CT_DOCKAREA) {
155         DLOG("Ignoring, this is a dock client\n");
156         return;
157     }
158
159     /* see if the user entered the window on a certain window decoration */
160     int layout = (enter_child ? con->parent->layout : con->layout);
161     if (layout == L_DEFAULT) {
162         Con *child;
163         TAILQ_FOREACH(child, &(con->nodes_head), nodes)
164             if (rect_contains(child->deco_rect, event->event_x, event->event_y)) {
165                 LOG("using child %p / %s instead!\n", child, child->name);
166                 con = child;
167                 break;
168             }
169     }
170
171 #if 0
172     if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
173             /* This can happen when a client gets assigned to a different workspace than
174              * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
175              * an enter_notify will follow. */
176             DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
177             return 1;
178     }
179 #endif
180
181     if (config.disable_focus_follows_mouse)
182         return;
183
184     /* Get the currently focused workspace to check if the focus change also
185      * involves changing workspaces. If so, we need to call workspace_show() to
186      * correctly update state and send the IPC event. */
187     Con *ws = con_get_workspace(con);
188     if (ws != con_get_workspace(focused))
189         workspace_show(ws);
190
191     focused_id = XCB_NONE;
192     con_focus(con_descend_focused(con));
193     tree_render();
194
195     return;
196 }
197
198 /*
199  * When the user moves the mouse but does not change the active window
200  * (e.g. when having no windows opened but moving mouse on the root screen
201  * and crossing virtual screen boundaries), this callback gets called.
202  *
203  */
204 static void handle_motion_notify(xcb_motion_notify_event_t *event) {
205
206     last_timestamp = event->time;
207
208     /* Skip events where the pointer was over a child window, we are only
209      * interested in events on the root window. */
210     if (event->child != 0)
211         return;
212
213     Con *con;
214     if ((con = con_by_frame_id(event->event)) == NULL) {
215         check_crossing_screen_boundary(event->root_x, event->root_y);
216         return;
217     }
218
219     if (config.disable_focus_follows_mouse)
220         return;
221
222     if (con->layout != L_DEFAULT)
223         return;
224
225     /* see over which rect the user is */
226     Con *current;
227     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
228         if (!rect_contains(current->deco_rect, event->event_x, event->event_y))
229             continue;
230
231         /* We found the rect, let’s see if this window is focused */
232         if (TAILQ_FIRST(&(con->focus_head)) == current)
233             return;
234
235         con_focus(current);
236         x_push_changes(croot);
237         return;
238     }
239
240     return;
241 }
242
243 /*
244  * Called when the keyboard mapping changes (for example by using Xmodmap),
245  * we need to update our key bindings then (re-translate symbols).
246  *
247  */
248 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
249     if (event->request != XCB_MAPPING_KEYBOARD &&
250         event->request != XCB_MAPPING_MODIFIER)
251         return;
252
253     DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
254     xcb_refresh_keyboard_mapping(keysyms, event);
255
256     xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
257
258     ungrab_all_keys(conn);
259     translate_keysyms();
260     grab_all_keys(conn, false);
261
262     return;
263 }
264
265 /*
266  * A new window appeared on the screen (=was mapped), so let’s manage it.
267  *
268  */
269 static void handle_map_request(xcb_map_request_event_t *event) {
270     xcb_get_window_attributes_cookie_t cookie;
271
272     cookie = xcb_get_window_attributes_unchecked(conn, event->window);
273
274     DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
275     add_ignore_event(event->sequence, -1);
276
277     manage_window(event->window, cookie, false);
278     x_push_changes(croot);
279     return;
280 }
281
282 /*
283  * Configure requests are received when the application wants to resize windows
284  * on their own.
285  *
286  * We generate a synthethic configure notify event to signalize the client its
287  * "new" position.
288  *
289  */
290 static void handle_configure_request(xcb_configure_request_event_t *event) {
291     Con *con;
292
293     DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
294         event->window, event->x, event->y, event->width, event->height);
295
296     /* For unmanaged windows, we just execute the configure request. As soon as
297      * it gets mapped, we will take over anyways. */
298     if ((con = con_by_window_id(event->window)) == NULL) {
299         DLOG("Configure request for unmanaged window, can do that.\n");
300
301         uint32_t mask = 0;
302         uint32_t values[7];
303         int c = 0;
304 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
305         if (event->value_mask & mask_member) { \
306             mask |= mask_member; \
307             values[c++] = event->event_member; \
308         } \
309 } while (0)
310
311         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
312         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
313         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
314         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
315         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
316         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
317         COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
318
319         xcb_configure_window(conn, event->window, mask, values);
320         xcb_flush(conn);
321
322         return;
323     }
324
325     DLOG("Configure request!\n");
326     if (con_is_floating(con) && con_is_leaf(con)) {
327         /* find the height for the decorations */
328         int deco_height = config.font.height + 5;
329         /* we actually need to apply the size/position changes to the *parent*
330          * container */
331         Rect bsr = con_border_style_rect(con);
332         if (con->border_style == BS_NORMAL) {
333             bsr.y += deco_height;
334             bsr.height -= deco_height;
335         }
336         Con *floatingcon = con->parent;
337
338         Rect newrect = floatingcon->rect;
339
340         if (event->value_mask & XCB_CONFIG_WINDOW_X) {
341             newrect.x = event->x + (-1) * bsr.x;
342             DLOG("proposed x = %d, new x is %d\n", event->x, newrect.x);
343         }
344         if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
345             newrect.y = event->y + (-1) * bsr.y;
346             DLOG("proposed y = %d, new y is %d\n", event->y, newrect.y);
347         }
348         if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
349             newrect.width = event->width + (-1) * bsr.width;
350             newrect.width += con->border_width * 2;
351             DLOG("proposed width = %d, new width is %d (x11 border %d)\n",
352                  event->width, newrect.width, con->border_width);
353         }
354         if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
355             newrect.height = event->height + (-1) * bsr.height;
356             newrect.height += con->border_width * 2;
357             DLOG("proposed height = %d, new height is %d (x11 border %d)\n",
358                  event->height, newrect.height, con->border_width);
359         }
360
361         DLOG("Container is a floating leaf node, will do that.\n");
362         floating_reposition(floatingcon, newrect);
363         return;
364     }
365
366     /* Dock windows can be reconfigured in their height */
367     if (con->parent && con->parent->type == CT_DOCKAREA) {
368         DLOG("Dock window, only height reconfiguration allowed\n");
369         if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
370             DLOG("Height given, changing\n");
371
372             con->geometry.height = event->height;
373             tree_render();
374         }
375     }
376
377     fake_absolute_configure_notify(con);
378
379     return;
380 }
381 #if 0
382
383 /*
384  * Configuration notifies are only handled because we need to set up ignore for
385  * the following enter notify events.
386  *
387  */
388 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
389     DLOG("configure_event, sequence %d\n", event->sequence);
390         /* We ignore this sequence twice because events for child and frame should be ignored */
391         add_ignore_event(event->sequence);
392         add_ignore_event(event->sequence);
393
394         return 1;
395 }
396 #endif
397
398 /*
399  * Gets triggered upon a RandR screen change event, that is when the user
400  * changes the screen configuration in any way (mode, position, …)
401  *
402  */
403 static void handle_screen_change(xcb_generic_event_t *e) {
404     DLOG("RandR screen change\n");
405
406     randr_query_outputs();
407
408     ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
409
410     return;
411 }
412
413 /*
414  * Our window decorations were unmapped. That means, the window will be killed
415  * now, so we better clean up before.
416  *
417  */
418 static void handle_unmap_notify_event(xcb_unmap_notify_event_t *event) {
419     DLOG("UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
420     xcb_get_input_focus_cookie_t cookie;
421     Con *con = con_by_window_id(event->window);
422     if (con == NULL) {
423         /* This could also be an UnmapNotify for the frame. We need to
424          * decrement the ignore_unmap counter. */
425         con = con_by_frame_id(event->window);
426         if (con == NULL) {
427             LOG("Not a managed window, ignoring UnmapNotify event\n");
428             return;
429         }
430
431         if (con->ignore_unmap > 0)
432             con->ignore_unmap--;
433         /* See the end of this function. */
434         cookie = xcb_get_input_focus(conn);
435         DLOG("ignore_unmap = %d for frame of container %p\n", con->ignore_unmap, con);
436         goto ignore_end;
437     }
438
439     /* See the end of this function. */
440     cookie = xcb_get_input_focus(conn);
441
442     if (con->ignore_unmap > 0) {
443         DLOG("ignore_unmap = %d, dec\n", con->ignore_unmap);
444         con->ignore_unmap--;
445         goto ignore_end;
446     }
447
448     tree_close(con, DONT_KILL_WINDOW, false, false);
449     tree_render();
450     x_push_changes(croot);
451
452 ignore_end:
453     /* If the client (as opposed to i3) destroyed or unmapped a window, an
454      * EnterNotify event will follow (indistinguishable from an EnterNotify
455      * event caused by moving your mouse), causing i3 to set focus to whichever
456      * window is now visible.
457      *
458      * In a complex stacked or tabbed layout (take two v-split containers in a
459      * tabbed container), when the bottom window in tab2 is closed, the bottom
460      * window of tab1 is visible instead. X11 will thus send an EnterNotify
461      * event for the bottom window of tab1, while the focus should be set to
462      * the remaining window of tab2.
463      *
464      * Therefore, we ignore all EnterNotify events which have the same sequence
465      * as an UnmapNotify event. */
466     add_ignore_event(event->sequence, XCB_ENTER_NOTIFY);
467
468     /* Since we just ignored the sequence of this UnmapNotify, we want to make
469      * sure that following events use a different sequence. When putting xterm
470      * into fullscreen and moving the pointer to a different window, without
471      * using GetInputFocus, subsequent (legitimate) EnterNotify events arrived
472      * with the same sequence and thus were ignored (see ticket #609). */
473     free(xcb_get_input_focus_reply(conn, cookie, NULL));
474 }
475
476 /*
477  * A destroy notify event is sent when the window is not unmapped, but
478  * immediately destroyed (for example when starting a window and immediately
479  * killing the program which started it).
480  *
481  * We just pass on the event to the unmap notify handler (by copying the
482  * important fields in the event data structure).
483  *
484  */
485 static void handle_destroy_notify_event(xcb_destroy_notify_event_t *event) {
486     DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
487
488     xcb_unmap_notify_event_t unmap;
489     unmap.sequence = event->sequence;
490     unmap.event = event->event;
491     unmap.window = event->window;
492
493     handle_unmap_notify_event(&unmap);
494 }
495
496 /*
497  * Called when a window changes its title
498  *
499  */
500 static bool handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
501                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
502     Con *con;
503     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
504         return false;
505
506     window_update_name(con->window, prop, false);
507
508     x_push_changes(croot);
509
510     return true;
511 }
512
513 /*
514  * Handles legacy window name updates (WM_NAME), see also src/window.c,
515  * window_update_name_legacy().
516  *
517  */
518 static bool handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
519                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
520     Con *con;
521     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
522         return false;
523
524     window_update_name_legacy(con->window, prop, false);
525
526     x_push_changes(croot);
527
528     return true;
529 }
530
531 /*
532  * Called when a window changes its WM_WINDOW_ROLE.
533  *
534  */
535 static bool handle_windowrole_change(void *data, xcb_connection_t *conn, uint8_t state,
536                                      xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
537     Con *con;
538     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
539         return false;
540
541     window_update_role(con->window, prop, false);
542
543     return true;
544 }
545
546 #if 0
547 /*
548  * Updates the client’s WM_CLASS property
549  *
550  */
551 static int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
552                              xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
553     Con *con;
554     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
555         return 1;
556
557     window_update_class(con->window, prop, false);
558
559     return 0;
560 }
561 #endif
562
563 /*
564  * Expose event means we should redraw our windows (= title bar)
565  *
566  */
567 static void handle_expose_event(xcb_expose_event_t *event) {
568     Con *parent;
569
570     DLOG("window = %08x\n", event->window);
571
572     if ((parent = con_by_frame_id(event->window)) == NULL) {
573         LOG("expose event for unknown window, ignoring\n");
574         return;
575     }
576
577     /* Since we render to our pixmap on every change anyways, expose events
578      * only tell us that the X server lost (parts of) the window contents. We
579      * can handle that by copying the appropriate part from our pixmap to the
580      * window. */
581     xcb_copy_area(conn, parent->pixmap, parent->frame, parent->pm_gc,
582                   event->x, event->y, event->x, event->y,
583                   event->width, event->height);
584     xcb_flush(conn);
585
586     return;
587 }
588
589 /*
590  * Handle client messages (EWMH)
591  *
592  */
593 static void handle_client_message(xcb_client_message_event_t *event) {
594     /* If this is a startup notification ClientMessage, the library will handle
595      * it and call our monitor_event() callback. */
596     if (sn_xcb_display_process_event(sndisplay, (xcb_generic_event_t*)event))
597         return;
598
599     LOG("ClientMessage for window 0x%08x\n", event->window);
600     if (event->type == A__NET_WM_STATE) {
601         if (event->format != 32 || event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN) {
602             DLOG("atom in clientmessage is %d, fullscreen is %d\n",
603                     event->data.data32[1], A__NET_WM_STATE_FULLSCREEN);
604             DLOG("not about fullscreen atom\n");
605             return;
606         }
607
608         Con *con = con_by_window_id(event->window);
609         if (con == NULL) {
610             DLOG("Could not get window for client message\n");
611             return;
612         }
613
614         /* Check if the fullscreen state should be toggled */
615         if ((con->fullscreen_mode != CF_NONE &&
616              (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
617               event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
618             (con->fullscreen_mode == CF_NONE &&
619              (event->data.data32[0] == _NET_WM_STATE_ADD ||
620               event->data.data32[0] == _NET_WM_STATE_TOGGLE))) {
621             DLOG("toggling fullscreen\n");
622             con_toggle_fullscreen(con, CF_OUTPUT);
623         }
624
625         tree_render();
626     } else if (event->type == A__NET_ACTIVE_WINDOW) {
627         DLOG("_NET_ACTIVE_WINDOW: Window 0x%08x should be activated\n", event->window);
628         Con *con = con_by_window_id(event->window);
629         if (con == NULL) {
630             DLOG("Could not get window for client message\n");
631             return;
632         }
633
634         con_focus(con);
635         tree_render();
636     } else if (event->type == A_I3_SYNC) {
637         xcb_window_t window = event->data.data32[0];
638         uint32_t rnd = event->data.data32[1];
639         DLOG("[i3 sync protocol] Sending random value %d back to X11 window 0x%08x\n", rnd, window);
640
641         void *reply = scalloc(32);
642         xcb_client_message_event_t *ev = reply;
643
644         ev->response_type = XCB_CLIENT_MESSAGE;
645         ev->window = window;
646         ev->type = A_I3_SYNC;
647         ev->format = 32;
648         ev->data.data32[0] = window;
649         ev->data.data32[1] = rnd;
650
651         xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
652         xcb_flush(conn);
653         free(reply);
654     } else {
655         DLOG("unhandled clientmessage\n");
656         return;
657     }
658 }
659
660 #if 0
661 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
662                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
663         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
664          before changing this property. */
665         ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
666         return 0;
667 }
668 #endif
669
670 /*
671  * Handles the size hints set by a window, but currently only the part necessary for displaying
672  * clients proportionally inside their frames (mplayer for example)
673  *
674  * See ICCCM 4.1.2.3 for more details
675  *
676  */
677 static bool handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
678                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
679     Con *con = con_by_window_id(window);
680     if (con == NULL) {
681         DLOG("Received WM_NORMAL_HINTS for unknown client\n");
682         return false;
683     }
684
685     xcb_size_hints_t size_hints;
686
687         //CLIENT_LOG(client);
688
689     /* If the hints were already in this event, use them, if not, request them */
690     if (reply != NULL)
691         xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
692     else
693         xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, con->window->id), &size_hints, NULL);
694
695     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
696         // TODO: Minimum size is not yet implemented
697         DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
698     }
699
700     bool changed = false;
701     if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
702         if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
703             if (con->width_increment != size_hints.width_inc) {
704                 con->width_increment = size_hints.width_inc;
705                 changed = true;
706             }
707         if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
708             if (con->height_increment != size_hints.height_inc) {
709                 con->height_increment = size_hints.height_inc;
710                 changed = true;
711             }
712
713         if (changed)
714             DLOG("resize increments changed\n");
715     }
716
717     int base_width = 0, base_height = 0;
718
719     /* base_width/height are the desired size of the window.
720        We check if either the program-specified size or the program-specified
721        min-size is available */
722     if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
723         base_width = size_hints.base_width;
724         base_height = size_hints.base_height;
725     } else if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
726         /* TODO: is this right? icccm says not */
727         base_width = size_hints.min_width;
728         base_height = size_hints.min_height;
729     }
730
731     if (base_width != con->base_width ||
732         base_height != con->base_height) {
733         con->base_width = base_width;
734         con->base_height = base_height;
735         DLOG("client's base_height changed to %d\n", base_height);
736         DLOG("client's base_width changed to %d\n", base_width);
737         changed = true;
738     }
739
740     /* If no aspect ratio was set or if it was invalid, we ignore the hints */
741     if (!(size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT) ||
742         (size_hints.min_aspect_num <= 0) ||
743         (size_hints.min_aspect_den <= 0)) {
744         goto render_and_return;
745     }
746
747     /* XXX: do we really use rect here, not window_rect? */
748     double width = con->rect.width - base_width;
749     double height = con->rect.height - base_height;
750     /* Convert numerator/denominator to a double */
751     double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
752     double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
753
754     DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
755     DLOG("width = %f, height = %f\n", width, height);
756
757     /* Sanity checks, this is user-input, in a way */
758     if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
759         goto render_and_return;
760
761     /* Check if we need to set proportional_* variables using the correct ratio */
762     if ((width / height) < min_aspect) {
763         if (con->proportional_width != width ||
764             con->proportional_height != (width / min_aspect)) {
765             con->proportional_width = width;
766             con->proportional_height = width / min_aspect;
767             changed = true;
768         }
769     } else if ((width / height) > max_aspect) {
770         if (con->proportional_width != width ||
771             con->proportional_height != (width / max_aspect)) {
772             con->proportional_width = width;
773             con->proportional_height = width / max_aspect;
774             changed = true;
775         }
776     } else goto render_and_return;
777
778 render_and_return:
779     if (changed)
780         tree_render();
781     FREE(reply);
782     return true;
783 }
784
785 /*
786  * Handles the WM_HINTS property for extracting the urgency state of the window.
787  *
788  */
789 static bool handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
790                   xcb_atom_t name, xcb_get_property_reply_t *reply) {
791     Con *con = con_by_window_id(window);
792     if (con == NULL) {
793         DLOG("Received WM_HINTS for unknown client\n");
794         return false;
795     }
796
797     xcb_icccm_wm_hints_t hints;
798
799     if (reply == NULL)
800         if (!(reply = xcb_get_property_reply(conn, xcb_icccm_get_wm_hints(conn, window), NULL)))
801             return false;
802
803     if (!xcb_icccm_get_wm_hints_from_reply(&hints, reply))
804         return false;
805
806     if (!con->urgent && focused == con) {
807         DLOG("Ignoring urgency flag for current client\n");
808         con->window->urgent.tv_sec = 0;
809         con->window->urgent.tv_usec = 0;
810         goto end;
811     }
812
813     /* Update the flag on the client directly */
814     con->urgent = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
815     //CLIENT_LOG(con);
816     if (con->window) {
817         if (con->urgent) {
818             gettimeofday(&con->window->urgent, NULL);
819         } else {
820             con->window->urgent.tv_sec = 0;
821             con->window->urgent.tv_usec = 0;
822         }
823     }
824     LOG("Urgency flag changed to %d\n", con->urgent);
825
826     Con *ws;
827     /* Set the urgency flag on the workspace, if a workspace could be found
828      * (for dock clients, that is not the case). */
829     if ((ws = con_get_workspace(con)) != NULL)
830         workspace_update_urgent_flag(ws);
831
832     tree_render();
833
834 end:
835     if (con->window)
836         window_update_hints(con->window, reply);
837     else free(reply);
838     return true;
839 }
840
841 /*
842  * Handles the transient for hints set by a window, signalizing that this window is a popup window
843  * for some other window.
844  *
845  * See ICCCM 4.1.2.6 for more details
846  *
847  */
848 static bool handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
849                          xcb_atom_t name, xcb_get_property_reply_t *prop) {
850     Con *con;
851
852     if ((con = con_by_window_id(window)) == NULL || con->window == NULL) {
853         DLOG("No such window\n");
854         return false;
855     }
856
857     if (prop == NULL) {
858         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
859                                 false, window, XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 0, 32), NULL);
860         if (prop == NULL)
861             return false;
862     }
863
864     window_update_transient_for(con->window, prop);
865
866     return true;
867 }
868
869 /*
870  * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
871  * toolwindow (or similar) and to which window it belongs (logical parent).
872  *
873  */
874 static bool handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
875                         xcb_atom_t name, xcb_get_property_reply_t *prop) {
876     Con *con;
877     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
878         return false;
879
880     if (prop == NULL) {
881         prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
882                                 false, window, A_WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32), NULL);
883         if (prop == NULL)
884             return false;
885     }
886
887     window_update_leader(con->window, prop);
888
889     return true;
890 }
891
892 /*
893  * Handles FocusIn events which are generated by clients (i3’s focus changes
894  * don’t generate FocusIn events due to a different EventMask) and updates the
895  * decorations accordingly.
896  *
897  */
898 static void handle_focus_in(xcb_focus_in_event_t *event) {
899     DLOG("focus change in, for window 0x%08x\n", event->event);
900     Con *con;
901     if ((con = con_by_window_id(event->event)) == NULL || con->window == NULL)
902         return;
903     DLOG("That is con %p / %s\n", con, con->name);
904
905     if (event->mode == XCB_NOTIFY_MODE_GRAB ||
906         event->mode == XCB_NOTIFY_MODE_UNGRAB) {
907         DLOG("FocusIn event for grab/ungrab, ignoring\n");
908         return;
909     }
910
911     if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
912         DLOG("notify detail is pointer, ignoring this event\n");
913         return;
914     }
915
916     if (focused_id == event->event) {
917         DLOG("focus matches the currently focused window, not doing anything\n");
918         return;
919     }
920
921     /* Skip dock clients, they cannot get the i3 focus. */
922     if (con->parent->type == CT_DOCKAREA) {
923         DLOG("This is a dock client, not focusing.\n");
924         return;
925     }
926
927     DLOG("focus is different, updating decorations\n");
928
929     /* Get the currently focused workspace to check if the focus change also
930      * involves changing workspaces. If so, we need to call workspace_show() to
931      * correctly update state and send the IPC event. */
932     Con *ws = con_get_workspace(con);
933     if (ws != con_get_workspace(focused))
934         workspace_show(ws);
935
936     con_focus(con);
937     /* We update focused_id because we don’t need to set focus again */
938     focused_id = event->event;
939     x_push_changes(croot);
940     return;
941 }
942
943 /* Returns false if the event could not be processed (e.g. the window could not
944  * be found), true otherwise */
945 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);
946
947 struct property_handler_t {
948     xcb_atom_t atom;
949     uint32_t long_len;
950     cb_property_handler_t cb;
951 };
952
953 static struct property_handler_t property_handlers[] = {
954     { 0, 128, handle_windowname_change },
955     { 0, UINT_MAX, handle_hints },
956     { 0, 128, handle_windowname_change_legacy },
957     { 0, UINT_MAX, handle_normal_hints },
958     { 0, UINT_MAX, handle_clientleader_change },
959     { 0, UINT_MAX, handle_transient_for },
960     { 0, 128, handle_windowrole_change }
961 };
962 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
963
964 /*
965  * Sets the appropriate atoms for the property handlers after the atoms were
966  * received from X11
967  *
968  */
969 void property_handlers_init(void) {
970
971     sn_monitor_context_new(sndisplay, conn_screen, startup_monitor_event, NULL, NULL);
972
973     property_handlers[0].atom = A__NET_WM_NAME;
974     property_handlers[1].atom = XCB_ATOM_WM_HINTS;
975     property_handlers[2].atom = XCB_ATOM_WM_NAME;
976     property_handlers[3].atom = XCB_ATOM_WM_NORMAL_HINTS;
977     property_handlers[4].atom = A_WM_CLIENT_LEADER;
978     property_handlers[5].atom = XCB_ATOM_WM_TRANSIENT_FOR;
979     property_handlers[6].atom = A_WM_WINDOW_ROLE;
980 }
981
982 static void property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
983     struct property_handler_t *handler = NULL;
984     xcb_get_property_reply_t *propr = NULL;
985
986     for (int c = 0; c < sizeof(property_handlers) / sizeof(struct property_handler_t); c++) {
987         if (property_handlers[c].atom != atom)
988             continue;
989
990         handler = &property_handlers[c];
991         break;
992     }
993
994     if (handler == NULL) {
995         //DLOG("Unhandled property notify for atom %d (0x%08x)\n", atom, atom);
996         return;
997     }
998
999     if (state != XCB_PROPERTY_DELETE) {
1000         xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->long_len);
1001         propr = xcb_get_property_reply(conn, cookie, 0);
1002     }
1003
1004     /* the handler will free() the reply unless it returns false */
1005     if (!handler->cb(NULL, conn, state, window, atom, propr))
1006         FREE(propr);
1007 }
1008
1009 /*
1010  * Takes an xcb_generic_event_t and calls the appropriate handler, based on the
1011  * event type.
1012  *
1013  */
1014 void handle_event(int type, xcb_generic_event_t *event) {
1015     if (randr_base > -1 &&
1016         type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
1017         handle_screen_change(event);
1018         return;
1019     }
1020
1021     switch (type) {
1022         case XCB_KEY_PRESS:
1023             handle_key_press((xcb_key_press_event_t*)event);
1024             break;
1025
1026         case XCB_BUTTON_PRESS:
1027             handle_button_press((xcb_button_press_event_t*)event);
1028             break;
1029
1030         case XCB_MAP_REQUEST:
1031             handle_map_request((xcb_map_request_event_t*)event);
1032             break;
1033
1034         case XCB_UNMAP_NOTIFY:
1035             handle_unmap_notify_event((xcb_unmap_notify_event_t*)event);
1036             break;
1037
1038         case XCB_DESTROY_NOTIFY:
1039             handle_destroy_notify_event((xcb_destroy_notify_event_t*)event);
1040             break;
1041
1042         case XCB_EXPOSE:
1043             handle_expose_event((xcb_expose_event_t*)event);
1044             break;
1045
1046         case XCB_MOTION_NOTIFY:
1047             handle_motion_notify((xcb_motion_notify_event_t*)event);
1048             break;
1049
1050         /* Enter window = user moved his mouse over the window */
1051         case XCB_ENTER_NOTIFY:
1052             handle_enter_notify((xcb_enter_notify_event_t*)event);
1053             break;
1054
1055         /* Client message are sent to the root window. The only interesting
1056          * client message for us is _NET_WM_STATE, we honour
1057          * _NET_WM_STATE_FULLSCREEN */
1058         case XCB_CLIENT_MESSAGE:
1059             handle_client_message((xcb_client_message_event_t*)event);
1060             break;
1061
1062         /* Configure request = window tried to change size on its own */
1063         case XCB_CONFIGURE_REQUEST:
1064             handle_configure_request((xcb_configure_request_event_t*)event);
1065             break;
1066
1067         /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
1068         case XCB_MAPPING_NOTIFY:
1069             handle_mapping_notify((xcb_mapping_notify_event_t*)event);
1070             break;
1071
1072         case XCB_FOCUS_IN:
1073             handle_focus_in((xcb_focus_in_event_t*)event);
1074             break;
1075
1076         case XCB_PROPERTY_NOTIFY: {
1077             xcb_property_notify_event_t *e = (xcb_property_notify_event_t*)event;
1078             last_timestamp = e->time;
1079             property_notify(e->state, e->window, e->atom);
1080             break;
1081         }
1082
1083         default:
1084             //DLOG("Unhandled event of type %d\n", type);
1085             break;
1086     }
1087 }