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