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