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