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