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