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