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