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