]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
da5564596a023dbca1a3d28e6c42060ecde5742c
[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
38 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
39    since it’d trigger an infinite loop of switching between the different windows when
40    changing workspaces */
41 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
42
43 static void add_ignore_event(const int sequence) {
44         struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
45
46         event->sequence = sequence;
47         event->added = time(NULL);
48
49         LOG("Adding sequence %d to ignorelist\n", sequence);
50
51         SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
52 }
53
54 /*
55  * Checks if the given sequence is ignored and returns true if so.
56  *
57  */
58 static bool event_is_ignored(const int sequence) {
59         struct Ignore_Event *event;
60         time_t now = time(NULL);
61         for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
62                 if ((now - event->added) > 5) {
63                         struct Ignore_Event *save = event;
64                         event = SLIST_NEXT(event, ignore_events);
65                         SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
66                         free(save);
67                 } else event = SLIST_NEXT(event, ignore_events);
68         }
69
70         SLIST_FOREACH(event, &ignore_events, ignore_events) {
71                 if (event->sequence == sequence) {
72                         LOG("Ignoring event (sequence %d)\n", 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         LOG("got key release, just passing\n");
89         xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
90         xcb_flush(conn);
91         return 1;
92 }
93
94 /*
95  * There was a key press. We compare this key code with our bindings table and pass
96  * the bound action to parse_command().
97  *
98  */
99 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
100         LOG("Keypress %d, state raw = %d\n", event->detail, event->state);
101
102         /* Remove the numlock bit, all other bits are modifiers we can bind to */
103         uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
104         LOG("(removed numlock, state = %d)\n", state_filtered);
105         /* Only use the lower 8 bits of the state (modifier masks) so that mouse
106          * button masks are filtered out */
107         state_filtered &= 0xFF;
108         LOG("(removed upper 8 bits, state = %d)\n", state_filtered);
109
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         LOG("(checked mode_switch, state %d)\n", state_filtered);
118
119         /* Find the binding */
120         Binding *bind;
121         TAILQ_FOREACH(bind, &bindings, bindings)
122                 if (bind->keycode == event->detail && bind->mods == state_filtered)
123                         break;
124
125         /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
126            the user did not press Mode_switch, so just pass it… */
127         if (bind == TAILQ_END(&bindings)) {
128                 xcb_allow_events(conn, ReplayKeyboard, event->time);
129                 xcb_flush(conn);
130                 return 1;
131         }
132
133         parse_command(conn, bind->command);
134         if (state_filtered & BIND_MODE_SWITCH) {
135                 LOG("Mode_switch -> allow_events(SyncKeyboard)\n");
136                 xcb_allow_events(conn, SyncKeyboard, event->time);
137                 xcb_flush(conn);
138         }
139         return 1;
140 }
141
142
143 /*
144  * When the user moves the mouse pointer onto a window, this callback gets called.
145  *
146  */
147 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
148         LOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
149         if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
150                 LOG("This was not a normal notify, ignoring\n");
151                 return 1;
152         }
153         /* Some events are not interesting, because they were not generated actively by the
154            user, but by reconfiguration of windows */
155         if (event_is_ignored(event->sequence))
156                 return 1;
157
158         /* This was either a focus for a client’s parent (= titlebar)… */
159         Client *client = table_get(&by_parent, event->event);
160         /* …or the client itself */
161         if (client == NULL)
162                 client = table_get(&by_child, event->event);
163
164         /* Check for stack windows */
165         if (client == NULL) {
166                 struct Stack_Window *stack_win;
167                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
168                         if (stack_win->window == event->event) {
169                                 client = stack_win->container->currently_focused;
170                                 break;
171                         }
172         }
173
174
175         /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
176         if (client == NULL) {
177                 LOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
178                 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
179                 if (screen == NULL) {
180                         LOG("ERROR: No such screen\n");
181                         return 0;
182                 }
183                 c_ws->current_row = current_row;
184                 c_ws->current_col = current_col;
185                 c_ws = &workspaces[screen->current_workspace];
186                 current_row = c_ws->current_row;
187                 current_col = c_ws->current_col;
188                 LOG("We're now on virtual screen number %d\n", screen->num);
189                 return 1;
190         }
191
192         /* Do plausibility checks: This event may be useless for us if it occurs on a window
193            which is in a stacked container but not the focused one */
194         if (client->container != NULL &&
195             client->container->mode == MODE_STACK &&
196             client->container->currently_focused != client) {
197                 LOG("Plausibility check says: no\n");
198                 return 1;
199         }
200
201         if (client->container != NULL && client->container->workspace != c_ws) {
202                 /* This can happen when a client gets assigned to a different workspace than
203                  * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
204                  * an enter_notify will follow. */
205                 LOG("enter_notify for a client on a different workspace, ignoring\n");
206                 return 1;
207         }
208
209         set_focus(conn, client, false);
210
211         return 1;
212 }
213
214 /*
215  * Checks if the button press was on a stack window, handles focus setting and returns true
216  * if so, or false otherwise.
217  *
218  */
219 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
220         struct Stack_Window *stack_win;
221         SLIST_FOREACH(stack_win, &stack_wins, stack_windows) {
222                 if (stack_win->window != event->event)
223                         continue;
224
225                 /* A stack window was clicked, we check if it was button4 or button5
226                    which are scroll up / scroll down. */
227                 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
228                         direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN);
229                         focus_window_in_container(conn, CUR_CELL, direction);
230                         return true;
231                 }
232
233                 /* It was no scrolling, so we calculate the destination client by
234                    dividing the Y position of the event through the height of a window
235                    decoration and then set the focus to this client. */
236                 i3Font *font = load_font(conn, config.font);
237                 int decoration_height = (font->height + 2 + 2);
238                 int destination = (event->event_y / decoration_height),
239                     c = 0;
240                 Client *client;
241
242                 LOG("Click on stack_win for client %d\n", destination);
243                 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
244                         if (c++ == destination) {
245                                 set_focus(conn, client, true);
246                                 return true;
247                         }
248
249                 return true;
250         }
251
252         return false;
253 }
254
255 /*
256  * Checks if the button press was on a bar, switches to the workspace and returns true
257  * if so, or false otherwise.
258  *
259  */
260 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
261         i3Screen *screen;
262         TAILQ_FOREACH(screen, virtual_screens, screens) {
263                 if (screen->bar != event->event)
264                         continue;
265
266                 LOG("Click on a bar\n");
267
268                 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
269                 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
270                         int add = (event->detail == XCB_BUTTON_INDEX_4 ? -1 : 1);
271                         for (int i = c_ws->num + add; (i >= 0) && (i < 10); i += add)
272                                 if (workspaces[i].screen == screen) {
273                                         show_workspace(conn, i+1);
274                                         return true;
275                                 }
276                         return true;
277                 }
278                 i3Font *font = load_font(conn, config.font);
279                 int workspace = event->event_x / (font->height + 6),
280                     c = 0;
281                 /* Because workspaces can be on different screens, we need to loop
282                    through all of them and decide to count it based on its ->screen */
283                 for (int i = 0; i < 10; i++)
284                         if ((workspaces[i].screen == screen) && (c++ == workspace)) {
285                                 show_workspace(conn, i+1);
286                                 return true;
287                         }
288                 return true;
289         }
290
291         return false;
292 }
293
294 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
295         LOG("button press!\n");
296         /* This was either a focus for a client’s parent (= titlebar)… */
297         Client *client = table_get(&by_child, event->event);
298         bool border_click = false;
299         if (client == NULL) {
300                 client = table_get(&by_parent, event->event);
301                 border_click = true;
302         }
303         if (client == NULL) {
304                 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
305                 if (button_press_stackwin(conn, event))
306                         return 1;
307
308                 /* Or on a bar? */
309                 if (button_press_bar(conn, event))
310                         return 1;
311
312                 LOG("Could not handle this button press\n");
313                 return 1;
314         }
315
316         /* Set focus in any case */
317         set_focus(conn, client, true);
318
319         /* Let’s see if this was on the borders (= resize). If not, we’re done */
320         LOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
321         resize_orientation_t orientation = O_VERTICAL;
322         Container *con = client->container;
323         int first, second;
324
325         if (con == NULL) {
326                 LOG("dock. done.\n");
327                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
328                 xcb_flush(conn);
329                 return 1;
330         }
331
332         LOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
333
334         if (!border_click) {
335                 LOG("client. done.\n");
336                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
337                 xcb_flush(conn);
338                 return 1;
339         }
340
341         /* Don’t handle events inside the titlebar, only borders are interesting */
342         i3Font *font = load_font(conn, config.font);
343         if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
344                 LOG("click on titlebar\n");
345                 return 1;
346         }
347
348         if (event->event_y < 2) {
349                 /* This was a press on the top border */
350                 if (con->row == 0)
351                         return 1;
352                 first = con->row - 1;
353                 second = con->row;
354                 orientation = O_HORIZONTAL;
355         } else if (event->event_y >= (client->rect.height - 2)) {
356                 /* …bottom border */
357                 first = con->row + (con->rowspan - 1);
358                 if (!cell_exists(con->col, first) ||
359                     (first == (con->workspace->rows-1)))
360                         return 1;
361
362                 second = first + 1;
363                 orientation = O_HORIZONTAL;
364         } else if (event->event_x <= 2) {
365                 /* …left border */
366                 if (con->col == 0)
367                         return 1;
368
369                 first = con->col - 1;
370                 second = con->col;
371         } else if (event->event_x > 2) {
372                 /* …right border */
373                 first = con->col + (con->colspan - 1);
374                 LOG("column %d\n", first);
375
376                 if (!cell_exists(first, con->row) ||
377                     (first == (con->workspace->cols-1)))
378                         return 1;
379
380                 second = first + 1;
381         }
382
383         return resize_graphical_handler(conn, con->workspace, first, second, orientation, event);
384 }
385
386 /*
387  * A new window appeared on the screen (=was mapped), so let’s manage it.
388  *
389  */
390 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
391         xcb_get_window_attributes_cookie_t cookie;
392         xcb_get_window_attributes_reply_t *reply;
393
394         cookie = xcb_get_window_attributes_unchecked(conn, event->window);
395
396         if ((reply = xcb_get_window_attributes_reply(conn, cookie, NULL)) == NULL) {
397                 LOG("Could not get window attributes\n");
398                 return -1;
399         }
400
401         window_attributes_t wa = { TAG_VALUE };
402         LOG("override_redirect = %d\n", reply->override_redirect);
403         wa.u.override_redirect = reply->override_redirect;
404         LOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
405         add_ignore_event(event->sequence);
406
407         manage_window(prophs, conn, event->window, wa);
408         return 1;
409 }
410
411 /*
412  * Configure requests are received when the application wants to resize windows on their own.
413  *
414  * We generate a synthethic configure notify event to signalize the client its "new" position.
415  *
416  */
417 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
418         LOG("configure-request, serial %d\n", event->sequence);
419         LOG("event->window = %08x\n", event->window);
420         LOG("application wants to be at %dx%d with %dx%d\n", event->x, event->y, event->width, event->height);
421
422         Client *client = table_get(&by_child, event->window);
423         if (client == NULL) {
424                 LOG("This client is not mapped, so we don't care and just tell the client that he will get its size\n");
425                 Rect rect = {event->x, event->y, event->width, event->height};
426                 fake_configure_notify(conn, rect, event->window);
427                 return 1;
428         }
429
430         if (client->fullscreen) {
431                 LOG("Client is in fullscreen mode\n");
432
433                 Rect child_rect = client->container->workspace->rect;
434                 child_rect.x = child_rect.y = 0;
435                 fake_configure_notify(conn, child_rect, client->child);
436
437                 return 1;
438         }
439
440         fake_absolute_configure_notify(conn, client);
441
442         return 1;
443 }
444
445 /*
446  * Configuration notifies are only handled because we need to set up ignore for the following
447  * enter notify events
448  *
449  */
450 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
451         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
452
453         LOG("handle_configure_event for window %08x\n", event->window);
454         LOG("event->type = %d, \n", event->response_type);
455         LOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
456
457         /* We ignore this sequence twice because events for child and frame should be ignored */
458         add_ignore_event(event->sequence);
459         add_ignore_event(event->sequence);
460
461         if (event->event == root) {
462                 LOG("reconfigure of the root window, need to xinerama\n");
463                 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
464                    but is there a better way? */
465                 if (event->x == 0 && event->y == 0)
466                         xinerama_requery_screens(conn);
467                 return 1;
468         }
469
470         return 1;
471 }
472
473 /*
474  * Our window decorations were unmapped. That means, the window will be killed now,
475  * so we better clean up before.
476  *
477  */
478 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
479         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
480
481         add_ignore_event(event->sequence);
482
483         Client *client = table_get(&by_child, event->window);
484         /* First, we need to check if the client is awaiting an unmap-request which
485            was generated by us reparenting the window. In that case, we just ignore it. */
486         if (client != NULL && client->awaiting_useless_unmap) {
487                 LOG("Dropping this unmap request, it was generated by reparenting\n");
488                 client->awaiting_useless_unmap = false;
489                 return 1;
490         }
491
492         LOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
493         LOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
494         if (client == NULL) {
495                 LOG("not a managed window. Ignoring.\n");
496
497                 /* This was most likely the destroyed frame of a client which is
498                  * currently being unmapped, so we add this sequence (again!) to
499                  * the ignore list (enter_notify events will get sent for both,
500                  * the child and its frame). */
501                 add_ignore_event(event->sequence);
502
503                 return 0;
504         }
505
506         client = table_remove(&by_child, event->window);
507
508         if (client->name != NULL)
509                 free(client->name);
510
511         if (client->container != NULL) {
512                 Container *con = client->container;
513
514                 /* If this was the fullscreen client, we need to unset it */
515                 if (client->fullscreen)
516                         con->workspace->fullscreen_client = NULL;
517
518                 /* Remove the client from the list of clients */
519                 client_remove_from_container(conn, client, con);
520
521                 /* Set focus to the last focused client in this container */
522                 con->currently_focused = get_last_focused_client(conn, con, NULL);
523
524                 /* Only if this is the active container, we need to really change focus */
525                 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
526                         set_focus(conn, con->currently_focused, true);
527         }
528
529         if (client->dock) {
530                 LOG("Removing from dock clients\n");
531                 SLIST_REMOVE(&(client->workspace->screen->dock_clients), client, Client, dock_clients);
532         }
533
534         LOG("child of 0x%08x.\n", client->frame);
535         xcb_reparent_window(conn, client->child, root, 0, 0);
536         xcb_destroy_window(conn, client->frame);
537         xcb_flush(conn);
538         table_remove(&by_parent, client->frame);
539
540         if (client->container != NULL) {
541                 cleanup_table(conn, client->container->workspace);
542                 fix_colrowspan(conn, client->container->workspace);
543         }
544
545         /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
546         bool workspace_empty = true;
547         FOR_TABLE(client->workspace)
548                 if (!CIRCLEQ_EMPTY(&(client->workspace->table[cols][rows]->clients))) {
549                         workspace_empty = false;
550                         break;
551                 }
552
553         i3Screen *screen;
554         TAILQ_FOREACH(screen, virtual_screens, screens)
555                 if (screen->current_workspace == client->workspace->num) {
556                         workspace_empty = false;
557                         break;
558                 }
559
560         if (workspace_empty)
561                 client->workspace->screen = NULL;
562
563         free(client);
564
565         render_layout(conn);
566
567         return 1;
568 }
569
570 /*
571  * Called when a window changes its title
572  *
573  */
574 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
575                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
576         LOG("window's name changed.\n");
577         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
578                 LOG("_NET_WM_NAME not specified, not changing\n");
579                 return 1;
580         }
581         Client *client = table_get(&by_child, window);
582         if (client == NULL)
583                 return 1;
584
585         /* Save the old pointer to make the update atomic */
586         char *new_name;
587         int new_len;
588         asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
589         /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
590         char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
591         LOG("Name should change to \"%s\"\n", new_name);
592         free(new_name);
593
594         /* Check if they are the same and don’t update if so.
595            Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
596            Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
597            but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
598         if ((new_len == client->name_len) &&
599             (client->name != NULL) &&
600             (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
601                 LOG("Name did not change, not updating\n");
602                 free(ucs2_name);
603                 return 1;
604         }
605
606         char *old_name = client->name;
607         client->name = ucs2_name;
608         client->name_len = new_len;
609         client->uses_net_wm_name = true;
610
611         FREE(old_name);
612
613         /* If the client is a dock window, we don’t need to render anything */
614         if (client->dock)
615                 return 1;
616
617         if (client->container->mode == MODE_STACK)
618                 render_container(conn, client->container);
619         else decorate_window(conn, client, client->frame, client->titlegc, 0);
620         xcb_flush(conn);
621
622         return 1;
623 }
624
625 /*
626  * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
627  * just pass them along, so when containing non-ASCII characters, those will be rendering
628  * incorrectly. In order to correctly render unicode window titles in i3, an application
629  * has to set _NET_WM_NAME, which is in UTF-8 encoding.
630  *
631  * On every update, a message is put out to the user, so he may improve the situation and
632  * update applications which display filenames in their title to correctly use
633  * _NET_WM_NAME and therefore support unicode.
634  *
635  */
636 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
637                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
638         LOG("window's name changed (legacy).\n");
639         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
640                 LOG("prop == NULL\n");
641                 return 1;
642         }
643         Client *client = table_get(&by_child, window);
644         if (client == NULL)
645                 return 1;
646
647         if (client->uses_net_wm_name) {
648                 LOG("This client is capable of _NET_WM_NAME, ignoring legacy name\n");
649                 return 1;
650         }
651
652         /* Save the old pointer to make the update atomic */
653         char *new_name;
654         if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
655                 perror("Could not get old name");
656                 LOG("Could not get old name\n");
657                 return 1;
658         }
659         /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
660         LOG("Name should change to \"%s\"\n", new_name);
661
662         /* Check if they are the same and don’t update if so. */
663         if (client->name != NULL &&
664             strlen(new_name) == strlen(client->name) &&
665             strcmp(client->name, new_name) == 0) {
666                 LOG("Name did not change, not updating\n");
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->mode == MODE_STACK)
686                 render_container(conn, client->container);
687         else decorate_window(conn, client, client->frame, client->titlegc, 0);
688         xcb_flush(conn);
689
690         return 1;
691 }
692
693 /*
694  * Updates the client’s WM_CLASS property
695  *
696  */
697 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
698                              xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
699         LOG("window class changed\n");
700         if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
701                 LOG("prop == NULL\n");
702                 return 1;
703         }
704         Client *client = table_get(&by_child, window);
705         if (client == NULL)
706                 return 1;
707         char *new_class;
708         if (asprintf(&new_class, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
709                 perror("Could not get window class");
710                 LOG("Could not get window class\n");
711                 return 1;
712         }
713
714         LOG("changed to %s\n", new_class);
715         char *old_class = client->window_class;
716         client->window_class = new_class;
717         FREE(old_class);
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         LOG("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         LOG("got client %s\n", client->name);
753         if (client->dock) {
754                 LOG("this is a dock\n");
755                 return 1;
756         }
757
758         if (client->container->mode != MODE_STACK)
759                 decorate_window(conn, client, client->frame, client->titlegc, 0);
760         else {
761                 uint32_t background_color;
762                 /* Distinguish if the window is currently focused… */
763                 if (CUR_CELL->currently_focused == client)
764                         background_color = get_colorpixel(conn, "#285577");
765                 /* …or if it is the focused window in a not focused container */
766                 else background_color = get_colorpixel(conn, "#555555");
767
768                 /* Set foreground color to current focused color, line width to 2 */
769                 uint32_t values[] = {background_color, 2};
770                 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
771
772                 /* Draw the border, the ±1 is for line width = 2 */
773                 xcb_point_t points[] = {{1, 0},                                           /* left upper edge */
774                                         {1, client->rect.height-1},                       /* left bottom edge */
775                                         {client->rect.width-1, client->rect.height-1},    /* right bottom edge */
776                                         {client->rect.width-1, 0}};                       /* right upper edge */
777                 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
778
779                 /* Draw a black background */
780                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
781                 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
782                 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
783         }
784         xcb_flush(conn);
785         return 1;
786 }
787
788 /*
789  * Handle client messages (EWMH)
790  *
791  */
792 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
793         LOG("client_message\n");
794
795         if (event->type == atoms[_NET_WM_STATE]) {
796                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
797                         return 0;
798
799                 LOG("fullscreen\n");
800
801                 Client *client = table_get(&by_child, event->window);
802                 if (client == NULL)
803                         return 0;
804
805                 /* Check if the fullscreen state should be toggled */
806                 if ((client->fullscreen &&
807                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
808                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
809                     (!client->fullscreen &&
810                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
811                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
812                         toggle_fullscreen(conn, client);
813         } else {
814                 LOG("unhandled clientmessage\n");
815                 return 0;
816         }
817
818         return 1;
819 }
820
821 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
822                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
823         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
824          before changing this property. */
825         LOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
826         return 0;
827 }
828
829 /*
830  * Handles the size hints set by a window, but currently only the part necessary for displaying
831  * clients proportionally inside their frames (mplayer for example)
832  *
833  * See ICCCM 4.1.2.3 for more details
834  *
835  */
836 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
837                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
838         LOG("handle_normal_hints\n");
839         Client *client = table_get(&by_child, window);
840         if (client == NULL) {
841                 LOG("No such client\n");
842                 return 1;
843         }
844         xcb_size_hints_t size_hints;
845
846         /* If the hints were already in this event, use them, if not, request them */
847         if (reply != NULL)
848                 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
849         else
850                 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
851
852         /* If no aspect ratio was set or if it was invalid, we ignore the hints */
853         if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
854             (size_hints.min_aspect_num <= 0) ||
855             (size_hints.min_aspect_den <= 0)) {
856                 LOG("No aspect ratio set, ignoring\n");
857                 return 1;
858         }
859
860         LOG("window is %08x / %s\n", client->child, client->name);
861
862         int base_width = 0, base_height = 0,
863             min_width = 0, min_height = 0;
864
865         /* base_width/height are the desired size of the window.
866            We check if either the program-specified size or the program-specified
867            min-size is available */
868         if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
869                 base_width = size_hints.base_width;
870                 base_height = size_hints.base_height;
871         } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
872                 base_width = size_hints.min_width;
873                 base_height = size_hints.min_height;
874         }
875
876         if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
877                 min_width = size_hints.min_width;
878                 min_height = size_hints.min_height;
879         } else if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
880                 min_width = size_hints.base_width;
881                 min_height = size_hints.base_height;
882         }
883
884         double width = client->rect.width - base_width;
885         double height = client->rect.height - base_height;
886         /* Convert numerator/denominator to a double */
887         double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
888         double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
889
890         LOG("min_aspect = %f, max_aspect = %f\n", min_aspect, max_aspect);
891         LOG("width = %f, height = %f\n", width, height);
892
893         /* Sanity checks, this is user-input, in a way */
894         if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
895                 return 1;
896
897         /* Check if we need to set proportional_* variables using the correct ratio */
898         if ((width / height) < min_aspect) {
899                 client->proportional_width = width;
900                 client->proportional_height = width / min_aspect;
901         } else if ((width / height) > max_aspect) {
902                 client->proportional_width = width;
903                 client->proportional_height = width / max_aspect;
904         } else return 1;
905
906         client->force_reconfigure = true;
907
908         if (client->container != NULL) {
909                 render_container(conn, client->container);
910                 xcb_flush(conn);
911         }
912
913         return 1;
914 }