]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
c9f953451f058ad26763356430344f898e544b91
[i3/i3] / src / handlers.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  */
11 #include <stdio.h>
12 #include <assert.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <time.h>
16
17 #include <xcb/xcb.h>
18 #include <xcb/xcb_atom.h>
19 #include <xcb/xcb_icccm.h>
20
21 #include <X11/XKBlib.h>
22
23 #include "i3.h"
24 #include "debug.h"
25 #include "table.h"
26 #include "layout.h"
27 #include "commands.h"
28 #include "data.h"
29 #include "xcb.h"
30 #include "util.h"
31 #include "xinerama.h"
32 #include "config.h"
33 #include "queue.h"
34 #include "resize.h"
35 #include "client.h"
36 #include "manage.h"
37 #include "floating.h"
38 #include "workspace.h"
39 #include "log.h"
40 #include "container.h"
41
42 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
43    since it’d trigger an infinite loop of switching between the different windows when
44    changing workspaces */
45 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
46
47 static void add_ignore_event(const int sequence) {
48         struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
49
50         event->sequence = sequence;
51         event->added = time(NULL);
52
53         SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
54 }
55
56 /*
57  * Checks if the given sequence is ignored and returns true if so.
58  *
59  */
60 static bool event_is_ignored(const int sequence) {
61         struct Ignore_Event *event;
62         time_t now = time(NULL);
63         for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
64                 if ((now - event->added) > 5) {
65                         struct Ignore_Event *save = event;
66                         event = SLIST_NEXT(event, ignore_events);
67                         SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
68                         free(save);
69                 } else event = SLIST_NEXT(event, ignore_events);
70         }
71
72         SLIST_FOREACH(event, &ignore_events, ignore_events) {
73                 if (event->sequence == sequence) {
74                         SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
75                         free(event);
76                         return true;
77                 }
78         }
79
80         return false;
81 }
82
83 /*
84  * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
85  * Therefore, we just replay all key presses.
86  *
87  */
88 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
89         xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
90         xcb_flush(conn);
91         return 1;
92 }
93
94 /*
95  * There was a key press. We compare this key code with our bindings table and pass
96  * the bound action to parse_command().
97  *
98  */
99 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
100         DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
101
102         /* Remove the numlock bit, all other bits are modifiers we can bind to */
103         uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
104         DLOG("(removed numlock, state = %d)\n", state_filtered);
105         /* Only use the lower 8 bits of the state (modifier masks) so that mouse
106          * button masks are filtered out */
107         state_filtered &= 0xFF;
108         DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
109
110         if (xkb_supported) {
111                 /* We need to get the keysym group (There are group 1 to group 4, each holding
112                    two keysyms (without shift and with shift) using Xkb because X fails to
113                    provide them reliably (it works in Xephyr, it does not in real X) */
114                 XkbStateRec state;
115                 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
116                         state_filtered |= BIND_MODE_SWITCH;
117         }
118
119         DLOG("(checked mode_switch, state %d)\n", state_filtered);
120
121         /* Find the binding */
122         Binding *bind;
123         TAILQ_FOREACH(bind, bindings, bindings) {
124                 /* First compare the modifiers */
125                 if (bind->mods != state_filtered)
126                         continue;
127
128                 /* If a symbol was specified by the user, we need to look in
129                  * the array of translated keycodes for the event’s keycode */
130                 if (bind->symbol != NULL) {
131                         if (memmem(bind->translated_to,
132                                    bind->number_keycodes * sizeof(xcb_keycode_t),
133                                    &(event->detail), sizeof(xcb_keycode_t)) != NULL)
134                                 break;
135                 } else {
136                         /* This case is easier: The user specified a keycode */
137                         if (bind->keycode == event->detail)
138                                 break;
139                 }
140         }
141
142         /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
143            the user did not press Mode_switch, so just pass it… */
144         if (bind == TAILQ_END(bindings)) {
145                 xcb_allow_events(conn, ReplayKeyboard, event->time);
146                 xcb_flush(conn);
147                 return 1;
148         }
149
150         parse_command(conn, bind->command);
151         if (state_filtered & BIND_MODE_SWITCH) {
152                 DLOG("Mode_switch -> allow_events(SyncKeyboard)\n");
153                 xcb_allow_events(conn, SyncKeyboard, event->time);
154                 xcb_flush(conn);
155         }
156         return 1;
157 }
158
159 /*
160  * Called with coordinates of an enter_notify event or motion_notify event
161  * to check if the user crossed virtual screen boundaries and adjust the
162  * current workspace, if so.
163  *
164  */
165 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
166         i3Screen *screen;
167
168         if ((screen = get_screen_containing(x, y)) == NULL) {
169                 ELOG("ERROR: No such screen\n");
170                 return;
171         }
172         if (screen == c_ws->screen)
173                 return;
174
175         c_ws->current_row = current_row;
176         c_ws->current_col = current_col;
177         c_ws = screen->current_workspace;
178         current_row = c_ws->current_row;
179         current_col = c_ws->current_col;
180         DLOG("We're now on virtual screen number %d\n", screen->num);
181 }
182
183 /*
184  * When the user moves the mouse pointer onto a window, this callback gets called.
185  *
186  */
187 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
188         DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
189         if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
190                 DLOG("This was not a normal notify, ignoring\n");
191                 return 1;
192         }
193         /* Some events are not interesting, because they were not generated actively by the
194            user, but by reconfiguration of windows */
195         if (event_is_ignored(event->sequence))
196                 return 1;
197
198         /* This was either a focus for a client’s parent (= titlebar)… */
199         Client *client = table_get(&by_parent, event->event);
200         /* …or the client itself */
201         if (client == NULL)
202                 client = table_get(&by_child, event->event);
203
204         /* Check for stack windows */
205         if (client == NULL) {
206                 struct Stack_Window *stack_win;
207                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
208                         if (stack_win->window == event->event) {
209                                 client = stack_win->container->currently_focused;
210                                 break;
211                         }
212         }
213
214
215         /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
216         if (client == NULL) {
217                 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
218                 check_crossing_screen_boundary(event->root_x, event->root_y);
219                 return 1;
220         }
221
222         /* Do plausibility checks: This event may be useless for us if it occurs on a window
223            which is in a stacked container but not the focused one */
224         if (client->container != NULL &&
225             client->container->mode == MODE_STACK &&
226             client->container->currently_focused != client) {
227                 DLOG("Plausibility check says: no\n");
228                 return 1;
229         }
230
231         if (client->workspace != c_ws && client->workspace->screen == c_ws->screen) {
232                 /* This can happen when a client gets assigned to a different workspace than
233                  * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
234                  * an enter_notify will follow. */
235                 DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
236                 return 1;
237         }
238
239         set_focus(conn, client, false);
240
241         return 1;
242 }
243
244 /*
245  * When the user moves the mouse but does not change the active window
246  * (e.g. when having no windows opened but moving mouse on the root screen
247  * and crossing virtual screen boundaries), this callback gets called.
248  *
249  */
250 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
251         /* Skip events where the pointer was over a child window, we are only
252          * interested in events on the root window. */
253         if (event->child != 0)
254                 return 1;
255
256         check_crossing_screen_boundary(event->root_x, event->root_y);
257
258         return 1;
259 }
260
261 /*
262  * Called when the keyboard mapping changes (for example by using Xmodmap),
263  * we need to update our key bindings then (re-translate symbols).
264  *
265  */
266 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
267         if (event->request != XCB_MAPPING_KEYBOARD &&
268             event->request != XCB_MAPPING_MODIFIER)
269                 return 0;
270
271         DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
272         xcb_refresh_keyboard_mapping(keysyms, event);
273
274         xcb_get_numlock_mask(conn);
275
276         ungrab_all_keys(conn);
277         grab_all_keys(conn);
278
279         return 0;
280 }
281
282 /*
283  * A new window appeared on the screen (=was mapped), so let’s manage it.
284  *
285  */
286 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
287         xcb_get_window_attributes_cookie_t cookie;
288
289         cookie = xcb_get_window_attributes_unchecked(conn, event->window);
290
291         DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
292         add_ignore_event(event->sequence);
293
294         manage_window(prophs, conn, event->window, cookie, false);
295         return 1;
296 }
297
298 /*
299  * Configure requests are received when the application wants to resize windows on their own.
300  *
301  * We generate a synthethic configure notify event to signalize the client its "new" position.
302  *
303  */
304 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
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         Client *client = table_get(&by_child, event->window);
309         if (client == NULL) {
310                 uint32_t mask = 0;
311                 uint32_t values[7];
312                 int c = 0;
313 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
314                 if (event->value_mask & mask_member) { \
315                         mask |= mask_member; \
316                         values[c++] = event->event_member; \
317                 } \
318 } while (0)
319
320                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
321                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
322                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
323                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
324                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
325                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
326                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
327
328                 xcb_configure_window(conn, event->window, mask, values);
329                 xcb_flush(conn);
330
331                 return 1;
332         }
333
334         if (client->fullscreen) {
335                 DLOG("Client is in fullscreen mode\n");
336
337                 Rect child_rect = client->workspace->rect;
338                 child_rect.x = child_rect.y = 0;
339                 fake_configure_notify(conn, child_rect, client->child);
340
341                 return 1;
342         }
343
344         /* Floating clients can be reconfigured */
345         if (client_is_floating(client)) {
346                 i3Font *font = load_font(conn, config.font);
347                 int mode = (client->container != NULL ? client->container->mode : MODE_DEFAULT);
348
349                 if (event->value_mask & XCB_CONFIG_WINDOW_X)
350                         client->rect.x = event->x;
351                 if (event->value_mask & XCB_CONFIG_WINDOW_Y)
352                         client->rect.y = event->y;
353                 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
354                         if (mode == MODE_STACK || mode == MODE_TABBED) {
355                                 client->rect.width = event->width + 2 + 2;
356                         } else {
357                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
358                                         client->rect.width = event->width;
359                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
360                                         client->rect.width = event->width + (1 + 1);
361                                 else client->rect.width = event->width + (2 + 2);
362                         }
363                 }
364                 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
365                         if (mode == MODE_STACK || mode == MODE_TABBED) {
366                                 client->rect.height = event->height + 2;
367                         } else {
368                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
369                                         client->rect.height = event->height;
370                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
371                                         client->rect.height = event->height + (1 + 1);
372                                 else client->rect.height = event->height + (font->height + 2 + 2) + 2;
373                         }
374                 }
375
376                 DLOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
377                     client->rect.x, client->rect.y, client->rect.width, client->rect.height);
378
379                 /* Push the new position/size to X11 */
380                 reposition_client(conn, client);
381                 resize_client(conn, client);
382                 xcb_flush(conn);
383
384                 return 1;
385         }
386
387         /* Dock clients can be reconfigured in their height */
388         if (client->dock) {
389                 DLOG("Reconfiguring height of this dock client\n");
390
391                 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
392                         DLOG("Ignoring configure request, no height given\n");
393                         return 1;
394                 }
395
396                 client->desired_height = event->height;
397                 render_workspace(conn, c_ws->screen, c_ws);
398                 xcb_flush(conn);
399
400                 return 1;
401         }
402
403         if (client->fullscreen) {
404                 DLOG("Client is in fullscreen mode\n");
405
406                 Rect child_rect = client->container->workspace->rect;
407                 child_rect.x = child_rect.y = 0;
408                 fake_configure_notify(conn, child_rect, client->child);
409
410                 return 1;
411         }
412
413         fake_absolute_configure_notify(conn, client);
414
415         return 1;
416 }
417
418 /*
419  * Configuration notifies are only handled because we need to set up ignore for the following
420  * enter notify events
421  *
422  */
423 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
424         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
425
426         /* We ignore this sequence twice because events for child and frame should be ignored */
427         add_ignore_event(event->sequence);
428         add_ignore_event(event->sequence);
429
430         if (event->event == root) {
431                 DLOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
432                 DLOG("reconfigure of the root window, need to xinerama\n");
433                 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
434                    but is there a better way? */
435                 if (event->x == 0 && event->y == 0)
436                         xinerama_requery_screens(conn);
437                 return 1;
438         }
439
440         return 1;
441 }
442
443 /*
444  * Our window decorations were unmapped. That means, the window will be killed now,
445  * so we better clean up before.
446  *
447  */
448 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
449         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
450
451         add_ignore_event(event->sequence);
452
453         Client *client = table_get(&by_child, event->window);
454         /* First, we need to check if the client is awaiting an unmap-request which
455            was generated by us reparenting the window. In that case, we just ignore it. */
456         if (client != NULL && client->awaiting_useless_unmap) {
457                 client->awaiting_useless_unmap = false;
458                 return 1;
459         }
460
461         DLOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
462         DLOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
463         if (client == NULL) {
464                 DLOG("not a managed window. Ignoring.\n");
465
466                 /* This was most likely the destroyed frame of a client which is
467                  * currently being unmapped, so we add this sequence (again!) to
468                  * the ignore list (enter_notify events will get sent for both,
469                  * the child and its frame). */
470                 add_ignore_event(event->sequence);
471
472                 return 0;
473         }
474
475         client = table_remove(&by_child, event->window);
476
477         /* If this was the fullscreen client, we need to unset it */
478         if (client->fullscreen)
479                 client->workspace->fullscreen_client = NULL;
480
481         /* Clients without a container are either floating or dock windows */
482         if (client->container != NULL) {
483                 Container *con = client->container;
484
485                 /* Remove the client from the list of clients */
486                 client_remove_from_container(conn, client, con, true);
487
488                 /* Set focus to the last focused client in this container */
489                 con->currently_focused = get_last_focused_client(conn, con, NULL);
490
491                 /* Only if this is the active container, we need to really change focus */
492                 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
493                         set_focus(conn, con->currently_focused, true);
494         } else if (client_is_floating(client)) {
495                 DLOG("Removing from floating clients\n");
496                 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
497                 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
498         }
499
500         if (client->dock) {
501                 DLOG("Removing from dock clients\n");
502                 SLIST_REMOVE(&(client->workspace->screen->dock_clients), client, Client, dock_clients);
503         }
504
505         DLOG("child of 0x%08x.\n", client->frame);
506         xcb_reparent_window(conn, client->child, root, 0, 0);
507
508         client_unmap(conn, client);
509
510         xcb_destroy_window(conn, client->frame);
511         xcb_flush(conn);
512         table_remove(&by_parent, client->frame);
513
514         if (client->container != NULL) {
515                 Workspace *workspace = client->container->workspace;
516                 cleanup_table(conn, workspace);
517                 fix_colrowspan(conn, workspace);
518         }
519
520         /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
521         bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
522         bool workspace_active = false;
523         Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
524
525         /* If this workspace is currently active, we don’t delete it */
526         i3Screen *screen;
527         TAILQ_FOREACH(screen, virtual_screens, screens)
528                 if (screen->current_workspace == client->workspace) {
529                         workspace_active = true;
530                         workspace_empty = false;
531                         break;
532                 }
533
534         if (workspace_empty)
535                 client->workspace->screen = NULL;
536
537         /* Remove the urgency flag if set */
538         client->urgent = false;
539         workspace_update_urgent_flag(client->workspace);
540
541         FREE(client->window_class);
542         FREE(client->name);
543         free(client);
544
545         render_layout(conn);
546
547         /* Ensure the focus is set to the next client in the focus stack or to
548          * the screen itself (if we do not focus the screen, it can happen that
549          * the focus is "nowhere" and thus keypress events will not be received
550          * by i3, thus the user cannot use any hotkeys). */
551         if (workspace_active) {
552                 if (to_focus != NULL)
553                         set_focus(conn, to_focus, true);
554                 else {
555                         DLOG("Restoring focus to root screen\n");
556                         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
557                         xcb_flush(conn);
558                 }
559         }
560
561         return 1;
562 }
563
564 /*
565  * Called when a window changes its title
566  *
567  */
568 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
569                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
570         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
571                 DLOG("_NET_WM_NAME not specified, not changing\n");
572                 return 1;
573         }
574         Client *client = table_get(&by_child, window);
575         if (client == NULL)
576                 return 1;
577
578         /* Save the old pointer to make the update atomic */
579         char *new_name;
580         int new_len;
581         asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
582         /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
583         char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
584         LOG("_NET_WM_NAME changed to \"%s\"\n", new_name);
585         free(new_name);
586
587         /* Check if they are the same and don’t update if so.
588            Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
589            Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
590            but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
591         if ((new_len == client->name_len) &&
592             (client->name != NULL) &&
593             (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
594                 free(ucs2_name);
595                 return 1;
596         }
597
598         char *old_name = client->name;
599         client->name = ucs2_name;
600         client->name_len = new_len;
601         client->uses_net_wm_name = true;
602
603         FREE(old_name);
604
605         /* If the client is a dock window, we don’t need to render anything */
606         if (client->dock)
607                 return 1;
608
609         int mode = container_mode(client->container, true);
610         if (mode == MODE_STACK || mode == MODE_TABBED)
611                 render_container(conn, client->container);
612         else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
613         xcb_flush(conn);
614
615         return 1;
616 }
617
618 /*
619  * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
620  * just pass them along, so when containing non-ASCII characters, those will be rendering
621  * incorrectly. In order to correctly render unicode window titles in i3, an application
622  * has to set _NET_WM_NAME, which is in UTF-8 encoding.
623  *
624  * On every update, a message is put out to the user, so he may improve the situation and
625  * update applications which display filenames in their title to correctly use
626  * _NET_WM_NAME and therefore support unicode.
627  *
628  */
629 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
630                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
631         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
632                 DLOG("prop == NULL\n");
633                 return 1;
634         }
635         Client *client = table_get(&by_child, window);
636         if (client == NULL)
637                 return 1;
638
639         /* Client capable of _NET_WM_NAME, ignore legacy name changes */
640         if (client->uses_net_wm_name)
641                 return 1;
642
643         /* Save the old pointer to make the update atomic */
644         char *new_name;
645         if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
646                 perror("Could not get old name");
647                 DLOG("Could not get old name\n");
648                 return 1;
649         }
650         /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
651         LOG("WM_NAME changed to \"%s\"\n", new_name);
652
653         /* Check if they are the same and don’t update if so. */
654         if (client->name != NULL &&
655             strlen(new_name) == strlen(client->name) &&
656             strcmp(client->name, new_name) == 0) {
657                 free(new_name);
658                 return 1;
659         }
660
661         LOG("Using legacy window title. Note that in order to get Unicode window titles in i3, "
662             "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
663
664         char *old_name = client->name;
665         client->name = new_name;
666         client->name_len = -1;
667
668         if (old_name != NULL)
669                 free(old_name);
670
671         /* If the client is a dock window, we don’t need to render anything */
672         if (client->dock)
673                 return 1;
674
675         if (client->container != NULL &&
676             (client->container->mode == MODE_STACK ||
677              client->container->mode == MODE_TABBED))
678                 render_container(conn, client->container);
679         else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
680         xcb_flush(conn);
681
682         return 1;
683 }
684
685 /*
686  * Updates the client’s WM_CLASS property
687  *
688  */
689 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
690                              xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
691         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
692                 DLOG("prop == NULL\n");
693                 return 1;
694         }
695         Client *client = table_get(&by_child, window);
696         if (client == NULL)
697                 return 1;
698         char *new_class;
699         if (asprintf(&new_class, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
700                 perror("Could not get window class");
701                 DLOG("Could not get window class\n");
702                 return 1;
703         }
704
705         LOG("WM_CLASS changed to %s\n", new_class);
706         char *old_class = client->window_class;
707         client->window_class = new_class;
708         FREE(old_class);
709
710         if (!client->initialized)
711                 return 1;
712
713         if (strcmp(new_class, "tools") == 0 || strcmp(new_class, "Dialog") == 0) {
714                 DLOG("tool/dialog window, should we put it floating?\n");
715                 if (client->floating == FLOATING_AUTO_OFF)
716                         toggle_floating_mode(conn, client, true);
717         }
718
719         return 1;
720 }
721
722 /*
723  * Expose event means we should redraw our windows (= title bar)
724  *
725  */
726 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
727         /* event->count is the number of minimum remaining expose events for this window, so we
728            skip all events but the last one */
729         if (event->count != 0)
730                 return 1;
731         DLOG("window = %08x\n", event->window);
732
733         Client *client = table_get(&by_parent, event->window);
734         if (client == NULL) {
735                 /* There was no client in the table, so this is probably an expose event for
736                    one of our stack_windows. */
737                 struct Stack_Window *stack_win;
738                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
739                         if (stack_win->window == event->window) {
740                                 render_container(conn, stack_win->container);
741                                 return 1;
742                         }
743
744                 /* …or one of the bars? */
745                 i3Screen *screen;
746                 TAILQ_FOREACH(screen, virtual_screens, screens)
747                         if (screen->bar == event->window)
748                                 render_layout(conn);
749                 return 1;
750         }
751
752         if (client->dock)
753                 return 1;
754
755         if (container_mode(client->container, true) == MODE_DEFAULT)
756                 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
757         else {
758                 uint32_t background_color;
759                 if (client->urgent)
760                         background_color = config.client.urgent.background;
761                 /* Distinguish if the window is currently focused… */
762                 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
763                         background_color = config.client.focused.background;
764                 /* …or if it is the focused window in a not focused container */
765                 else background_color = config.client.focused_inactive.background;
766
767                 /* Set foreground color to current focused color, line width to 2 */
768                 uint32_t values[] = {background_color, 2};
769                 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
770
771                 /* Draw the border, the ±1 is for line width = 2 */
772                 xcb_point_t points[] = {{1, 0},                                           /* left upper edge */
773                                         {1, client->rect.height-1},                       /* left bottom edge */
774                                         {client->rect.width-1, client->rect.height-1},    /* right bottom edge */
775                                         {client->rect.width-1, 0}};                       /* right upper edge */
776                 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
777
778                 /* Draw a black background */
779                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
780                 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless) {
781                         xcb_rectangle_t crect = {1, 0, client->rect.width - (1 + 1), client->rect.height - 1};
782                         xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
783                 } else {
784                         xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
785                         xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
786                 }
787         }
788         xcb_flush(conn);
789         return 1;
790 }
791
792 /*
793  * Handle client messages (EWMH)
794  *
795  */
796 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
797         if (event->type == atoms[_NET_WM_STATE]) {
798                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
799                         return 0;
800
801                 Client *client = table_get(&by_child, event->window);
802                 if (client == NULL)
803                         return 0;
804
805                 /* Check if the fullscreen state should be toggled */
806                 if ((client->fullscreen &&
807                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
808                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
809                     (!client->fullscreen &&
810                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
811                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
812                         client_toggle_fullscreen(conn, client);
813         } else {
814                 ELOG("unhandled clientmessage\n");
815                 return 0;
816         }
817
818         return 1;
819 }
820
821 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
822                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
823         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
824          before changing this property. */
825         ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
826         return 0;
827 }
828
829 /*
830  * Handles the size hints set by a window, but currently only the part necessary for displaying
831  * clients proportionally inside their frames (mplayer for example)
832  *
833  * See ICCCM 4.1.2.3 for more details
834  *
835  */
836 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
837                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
838         Client *client = table_get(&by_child, window);
839         if (client == NULL) {
840                 DLOG("Received WM_SIZE_HINTS for unknown client\n");
841                 return 1;
842         }
843         xcb_size_hints_t size_hints;
844
845         CLIENT_LOG(client);
846
847         /* If the hints were already in this event, use them, if not, request them */
848         if (reply != NULL)
849                 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
850         else
851                 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
852
853         if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
854                 // TODO: Minimum size is not yet implemented
855                 //LOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
856         }
857
858         if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
859                 bool changed = false;
860
861                 if (size_hints.width_inc > 0)
862                         if (client->width_increment != size_hints.width_inc) {
863                                 client->width_increment = size_hints.width_inc;
864                                 changed = true;
865                         }
866                 if (size_hints.height_inc > 0)
867                         if (client->height_increment != size_hints.height_inc) {
868                                 client->height_increment = size_hints.height_inc;
869                                 changed = true;
870                         }
871
872                 if (changed) {
873                         resize_client(conn, client);
874                         xcb_flush(conn);
875                 }
876         }
877
878         int base_width = 0, base_height = 0;
879
880         /* base_width/height are the desired size of the window.
881            We check if either the program-specified size or the program-specified
882            min-size is available */
883         if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
884                 base_width = size_hints.base_width;
885                 base_height = size_hints.base_height;
886         } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
887                 base_width = size_hints.min_width;
888                 base_height = size_hints.min_height;
889         }
890
891         if (base_width != client->base_width ||
892             base_height != client->base_height) {
893                 client->base_width = base_width;
894                 client->base_height = base_height;
895                 DLOG("client's base_height changed to %d\n", base_height);
896                 if (client->fullscreen)
897                         DLOG("Not resizing client, it is in fullscreen mode\n");
898                 else
899                         resize_client(conn, client);
900         }
901
902         /* If no aspect ratio was set or if it was invalid, we ignore the hints */
903         if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
904             (size_hints.min_aspect_num <= 0) ||
905             (size_hints.min_aspect_den <= 0)) {
906                 return 1;
907         }
908
909         double width = client->rect.width - base_width;
910         double height = client->rect.height - base_height;
911         /* Convert numerator/denominator to a double */
912         double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
913         double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
914
915         DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
916         DLOG("width = %f, height = %f\n", width, height);
917
918         /* Sanity checks, this is user-input, in a way */
919         if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
920                 return 1;
921
922         /* Check if we need to set proportional_* variables using the correct ratio */
923         if ((width / height) < min_aspect) {
924                 client->proportional_width = width;
925                 client->proportional_height = width / min_aspect;
926         } else if ((width / height) > max_aspect) {
927                 client->proportional_width = width;
928                 client->proportional_height = width / max_aspect;
929         } else return 1;
930
931         client->force_reconfigure = true;
932
933         if (client->container != NULL) {
934                 render_container(conn, client->container);
935                 xcb_flush(conn);
936         }
937
938         return 1;
939 }
940
941 /*
942  * Handles the WM_HINTS property for extracting the urgency state of the window.
943  *
944  */
945 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
946                   xcb_atom_t name, xcb_get_property_reply_t *reply) {
947         Client *client = table_get(&by_child, window);
948         if (client == NULL) {
949                 DLOG("Received WM_HINTS for unknown client\n");
950                 return 1;
951         }
952         xcb_wm_hints_t hints;
953
954         if (reply != NULL) {
955                 if (!xcb_get_wm_hints_from_reply(&hints, reply))
956                         return 1;
957         } else {
958                 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
959                         return 1;
960         }
961
962         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
963         if (!client->urgent && client == last_focused) {
964                 DLOG("Ignoring urgency flag for current client\n");
965                 return 1;
966         }
967
968         /* Update the flag on the client directly */
969         client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
970         CLIENT_LOG(client);
971         LOG("Urgency flag changed to %d\n", client->urgent);
972
973         workspace_update_urgent_flag(client->workspace);
974         redecorate_window(conn, client);
975
976         /* If the workspace this client is on is not visible, we need to redraw
977          * the workspace bar */
978         if (!workspace_is_visible(client->workspace)) {
979                 i3Screen *screen = client->workspace->screen;
980                 render_workspace(conn, screen, screen->current_workspace);
981                 xcb_flush(conn);
982         }
983
984         return 1;
985 }
986
987 /*
988  * Handles the transient for hints set by a window, signalizing that this window is a popup window
989  * for some other window.
990  *
991  * See ICCCM 4.1.2.6 for more details
992  *
993  */
994 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
995                          xcb_atom_t name, xcb_get_property_reply_t *reply) {
996         Client *client = table_get(&by_child, window);
997         if (client == NULL) {
998                 DLOG("No such client\n");
999                 return 1;
1000         }
1001
1002         xcb_window_t transient_for;
1003
1004         if (reply != NULL) {
1005                 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
1006                         return 1;
1007         } else {
1008                 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
1009                                                     &transient_for, NULL))
1010                         return 1;
1011         }
1012
1013         if (client->floating == FLOATING_AUTO_OFF) {
1014                 DLOG("This is a popup window, putting into floating\n");
1015                 toggle_floating_mode(conn, client, true);
1016         }
1017
1018         return 1;
1019 }
1020
1021 /*
1022  * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
1023  * toolwindow (or similar) and to which window it belongs (logical parent).
1024  *
1025  */
1026 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1027                         xcb_atom_t name, xcb_get_property_reply_t *prop) {
1028         if (prop == NULL) {
1029                 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1030                                         false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
1031                 if (prop == NULL)
1032                         return 1;
1033         }
1034
1035         Client *client = table_get(&by_child, window);
1036         if (client == NULL)
1037                 return 1;
1038
1039         xcb_window_t *leader = xcb_get_property_value(prop);
1040         if (leader == NULL || *leader == 0)
1041                 return 1;
1042
1043         DLOG("Client leader changed to %08x\n", *leader);
1044
1045         client->leader = *leader;
1046
1047         return 1;
1048 }