]> git.sur5r.net Git - i3/i3/blob - src/handlers.c
b56095409440fdcfc8ef26c885e6877d76de4bea
[i3/i3] / src / handlers.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #include <time.h>
9
10 #include <xcb/xcb_atom.h>
11 #include <xcb/randr.h>
12
13 #include <X11/XKBlib.h>
14
15 #include "all.h"
16
17 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
18    since it’d trigger an infinite loop of switching between the different windows when
19    changing workspaces */
20 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
21
22 static void add_ignore_event(const int sequence) {
23         struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
24
25         event->sequence = sequence;
26         event->added = time(NULL);
27
28         SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
29 }
30
31 /*
32  * Checks if the given sequence is ignored and returns true if so.
33  *
34  */
35 static bool event_is_ignored(const int sequence) {
36         struct Ignore_Event *event;
37         time_t now = time(NULL);
38         for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
39                 if ((now - event->added) > 5) {
40                         struct Ignore_Event *save = event;
41                         event = SLIST_NEXT(event, ignore_events);
42                         SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
43                         free(save);
44                 } else event = SLIST_NEXT(event, ignore_events);
45         }
46
47         SLIST_FOREACH(event, &ignore_events, ignore_events) {
48                 if (event->sequence == sequence) {
49                         SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
50                         free(event);
51                         return true;
52                 }
53         }
54
55         return false;
56 }
57
58 /*
59  * There was a key press. We compare this key code with our bindings table and pass
60  * the bound action to parse_command().
61  *
62  */
63 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
64     DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
65
66     /* Remove the numlock bit, all other bits are modifiers we can bind to */
67     uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
68     DLOG("(removed numlock, state = %d)\n", state_filtered);
69     /* Only use the lower 8 bits of the state (modifier masks) so that mouse
70      * button masks are filtered out */
71     state_filtered &= 0xFF;
72     DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
73
74     if (xkb_current_group == XkbGroup2Index)
75         state_filtered |= BIND_MODE_SWITCH;
76
77     DLOG("(checked mode_switch, state %d)\n", state_filtered);
78
79     /* Find the binding */
80     Binding *bind = get_binding(state_filtered, event->detail);
81
82     /* No match? Then the user has Mode_switch enabled but does not have a
83      * specific keybinding. Fall back to the default keybindings (without
84      * Mode_switch). Makes it much more convenient for users of a hybrid
85      * layout (like us, ru). */
86     if (bind == NULL) {
87         state_filtered &= ~(BIND_MODE_SWITCH);
88         DLOG("no match, new state_filtered = %d\n", state_filtered);
89         if ((bind = get_binding(state_filtered, event->detail)) == NULL) {
90             ELOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
91                  state_filtered, event->detail);
92             return 1;
93         }
94     }
95
96     parse_command(bind->command);
97     return 1;
98 }
99
100 #if 0
101
102 /*
103  * Called with coordinates of an enter_notify event or motion_notify event
104  * to check if the user crossed virtual screen boundaries and adjust the
105  * current workspace, if so.
106  *
107  */
108 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
109         Output *output;
110
111         if ((output = get_output_containing(x, y)) == NULL) {
112                 ELOG("ERROR: No such screen\n");
113                 return;
114         }
115         if (output == c_ws->output)
116                 return;
117
118         c_ws->current_row = current_row;
119         c_ws->current_col = current_col;
120         c_ws = output->current_workspace;
121         current_row = c_ws->current_row;
122         current_col = c_ws->current_col;
123         DLOG("We're now on output %p\n", output);
124
125         /* While usually this function is only called when the user switches
126          * to a different output using his mouse (and thus the output is
127          * empty), it may be that the following race condition occurs:
128          * 1) the user actives a new output (say VGA1).
129          * 2) the cursor is sent to the first pixel of the new VGA1, thus
130          *    generating an enter_notify for the screen (the enter_notify
131          *    is not yet received by i3).
132          * 3) i3 requeries screen configuration and maps a workspace onto the
133          *    new output.
134          * 4) the enter_notify event arrives and c_ws is set to the new
135          *    workspace but the existing windows on the new workspace are not
136          *    focused.
137          *
138          * Therefore, we re-set the focus here to be sure it’s correct. */
139         Client *first_client = SLIST_FIRST(&(c_ws->focus_stack));
140         if (first_client != NULL)
141                 set_focus(global_conn, first_client, true);
142 }
143
144 /*
145  * When the user moves the mouse pointer onto a window, this callback gets called.
146  *
147  */
148 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
149         DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
150         if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
151                 DLOG("This was not a normal notify, ignoring\n");
152                 return 1;
153         }
154         /* Some events are not interesting, because they were not generated actively by the
155            user, but by reconfiguration of windows */
156         if (event_is_ignored(event->sequence))
157                 return 1;
158
159         /* This was either a focus for a client’s parent (= titlebar)… */
160         Client *client = table_get(&by_parent, event->event);
161         /* …or the client itself */
162         if (client == NULL)
163                 client = table_get(&by_child, event->event);
164
165         /* Check for stack windows */
166         if (client == NULL) {
167                 struct Stack_Window *stack_win;
168                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
169                         if (stack_win->window == event->event) {
170                                 client = stack_win->container->currently_focused;
171                                 break;
172                         }
173         }
174
175
176         /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
177         if (client == NULL) {
178                 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
179                 check_crossing_screen_boundary(event->root_x, event->root_y);
180                 return 1;
181         }
182
183         /* Do plausibility checks: This event may be useless for us if it occurs on a window
184            which is in a stacked container but not the focused one */
185         if (client->container != NULL &&
186             client->container->mode == MODE_STACK &&
187             client->container->currently_focused != client) {
188                 DLOG("Plausibility check says: no\n");
189                 return 1;
190         }
191
192         if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
193                 /* This can happen when a client gets assigned to a different workspace than
194                  * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
195                  * an enter_notify will follow. */
196                 DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
197                 return 1;
198         }
199
200         if (!config.disable_focus_follows_mouse)
201                 set_focus(conn, client, false);
202
203         return 1;
204 }
205
206 /*
207  * When the user moves the mouse but does not change the active window
208  * (e.g. when having no windows opened but moving mouse on the root screen
209  * and crossing virtual screen boundaries), this callback gets called.
210  *
211  */
212 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
213         /* Skip events where the pointer was over a child window, we are only
214          * interested in events on the root window. */
215         if (event->child != 0)
216                 return 1;
217
218         check_crossing_screen_boundary(event->root_x, event->root_y);
219
220         return 1;
221 }
222
223 /*
224  * Called when the keyboard mapping changes (for example by using Xmodmap),
225  * we need to update our key bindings then (re-translate symbols).
226  *
227  */
228 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
229         if (event->request != XCB_MAPPING_KEYBOARD &&
230             event->request != XCB_MAPPING_MODIFIER)
231                 return 0;
232
233         DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
234         xcb_refresh_keyboard_mapping(keysyms, event);
235
236         xcb_get_numlock_mask(conn);
237
238         ungrab_all_keys(conn);
239         translate_keysyms();
240         grab_all_keys(conn, false);
241
242         return 0;
243 }
244
245 /*
246  * A new window appeared on the screen (=was mapped), so let’s manage it.
247  *
248  */
249 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
250         xcb_get_window_attributes_cookie_t cookie;
251
252         cookie = xcb_get_window_attributes_unchecked(conn, event->window);
253
254         DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
255         add_ignore_event(event->sequence);
256
257         manage_window(prophs, conn, event->window, cookie, false);
258         return 1;
259 }
260
261 /*
262  * Configure requests are received when the application wants to resize windows on their own.
263  *
264  * We generate a synthethic configure notify event to signalize the client its "new" position.
265  *
266  */
267 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
268         DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
269             event->window, event->x, event->y, event->width, event->height);
270
271         Client *client = table_get(&by_child, event->window);
272         if (client == NULL) {
273                 uint32_t mask = 0;
274                 uint32_t values[7];
275                 int c = 0;
276 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
277                 if (event->value_mask & mask_member) { \
278                         mask |= mask_member; \
279                         values[c++] = event->event_member; \
280                 } \
281 } while (0)
282
283                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
284                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
285                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
286                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
287                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
288                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
289                 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
290
291                 xcb_configure_window(conn, event->window, mask, values);
292                 xcb_flush(conn);
293
294                 return 1;
295         }
296
297         if (client->fullscreen) {
298                 DLOG("Client is in fullscreen mode\n");
299
300                 Rect child_rect = client->workspace->rect;
301                 child_rect.x = child_rect.y = 0;
302                 fake_configure_notify(conn, child_rect, client->child);
303
304                 return 1;
305         }
306
307         /* Floating clients can be reconfigured */
308         if (client_is_floating(client)) {
309                 i3Font *font = load_font(conn, config.font);
310                 int mode = (client->container != NULL ? client->container->mode : MODE_DEFAULT);
311                 /* TODO: refactor this code. we need a function to translate
312                  * coordinates of child_rect/rect. */
313
314                 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
315                         if (mode == MODE_STACK || mode == MODE_TABBED) {
316                                 client->rect.x = event->x - 2;
317                         } else {
318                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
319                                         client->rect.x = event->x;
320                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
321                                         client->rect.x = event->x - 1;
322                                 else client->rect.x = event->x - 2;
323                         }
324                 }
325                 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
326                         if (mode == MODE_STACK || mode == MODE_TABBED) {
327                                 client->rect.y = event->y - 2;
328                         } else {
329                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
330                                         client->rect.y = event->y;
331                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
332                                         client->rect.y = event->y - 1;
333                                 else client->rect.y = event->y - font->height - 2 - 2;
334                         }
335                 }
336                 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
337                         if (mode == MODE_STACK || mode == MODE_TABBED) {
338                                 client->rect.width = event->width + 2 + 2;
339                         } else {
340                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
341                                         client->rect.width = event->width;
342                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
343                                         client->rect.width = event->width + (1 + 1);
344                                 else client->rect.width = event->width + (2 + 2);
345                         }
346                 }
347                 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
348                         if (mode == MODE_STACK || mode == MODE_TABBED) {
349                                 client->rect.height = event->height + 2;
350                         } else {
351                                 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
352                                         client->rect.height = event->height;
353                                 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
354                                         client->rect.height = event->height + (1 + 1);
355                                 else client->rect.height = event->height + (font->height + 2 + 2) + 2;
356                         }
357                 }
358
359                 DLOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
360                     client->rect.x, client->rect.y, client->rect.width, client->rect.height);
361
362                 /* Push the new position/size to X11 */
363                 reposition_client(conn, client);
364                 resize_client(conn, client);
365                 xcb_flush(conn);
366
367                 return 1;
368         }
369
370         /* Dock clients can be reconfigured in their height */
371         if (client->dock) {
372                 DLOG("Reconfiguring height of this dock client\n");
373
374                 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
375                         DLOG("Ignoring configure request, no height given\n");
376                         return 1;
377                 }
378
379                 client->desired_height = event->height;
380                 render_workspace(conn, c_ws->output, c_ws);
381                 xcb_flush(conn);
382
383                 return 1;
384         }
385
386         if (client->fullscreen) {
387                 DLOG("Client is in fullscreen mode\n");
388
389                 Rect child_rect = client->container->workspace->rect;
390                 child_rect.x = child_rect.y = 0;
391                 fake_configure_notify(conn, child_rect, client->child);
392
393                 return 1;
394         }
395
396         fake_absolute_configure_notify(conn, client);
397
398         return 1;
399 }
400
401 /*
402  * Configuration notifies are only handled because we need to set up ignore for
403  * the following enter notify events.
404  *
405  */
406 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
407         /* We ignore this sequence twice because events for child and frame should be ignored */
408         add_ignore_event(event->sequence);
409         add_ignore_event(event->sequence);
410
411         return 1;
412 }
413
414 /*
415  * Gets triggered upon a RandR screen change event, that is when the user
416  * changes the screen configuration in any way (mode, position, …)
417  *
418  */
419 int handle_screen_change(void *prophs, xcb_connection_t *conn,
420                          xcb_generic_event_t *e) {
421         DLOG("RandR screen change\n");
422
423         randr_query_outputs(conn);
424
425         ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
426
427         return 1;
428 }
429
430 /*
431  * Our window decorations were unmapped. That means, the window will be killed now,
432  * so we better clean up before.
433  *
434  */
435 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
436         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
437
438         add_ignore_event(event->sequence);
439
440         Client *client = table_get(&by_child, event->window);
441         /* First, we need to check if the client is awaiting an unmap-request which
442            was generated by us reparenting the window. In that case, we just ignore it. */
443         if (client != NULL && client->awaiting_useless_unmap) {
444                 client->awaiting_useless_unmap = false;
445                 return 1;
446         }
447
448         DLOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
449         DLOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
450         if (client == NULL) {
451                 DLOG("not a managed window. Ignoring.\n");
452
453                 /* This was most likely the destroyed frame of a client which is
454                  * currently being unmapped, so we add this sequence (again!) to
455                  * the ignore list (enter_notify events will get sent for both,
456                  * the child and its frame). */
457                 add_ignore_event(event->sequence);
458
459                 return 0;
460         }
461
462         client = table_remove(&by_child, event->window);
463
464         /* If this was the fullscreen client, we need to unset it */
465         if (client->fullscreen)
466                 client->workspace->fullscreen_client = NULL;
467
468         /* Clients without a container are either floating or dock windows */
469         if (client->container != NULL) {
470                 Container *con = client->container;
471
472                 /* Remove the client from the list of clients */
473                 client_remove_from_container(conn, client, con, true);
474
475                 /* Set focus to the last focused client in this container */
476                 con->currently_focused = get_last_focused_client(conn, con, NULL);
477
478                 /* Only if this is the active container, we need to really change focus */
479                 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
480                         set_focus(conn, con->currently_focused, true);
481         } else if (client_is_floating(client)) {
482                 DLOG("Removing from floating clients\n");
483                 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
484                 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
485         }
486
487         if (client->dock) {
488                 DLOG("Removing from dock clients\n");
489                 SLIST_REMOVE(&(client->workspace->output->dock_clients), client, Client, dock_clients);
490         }
491
492         DLOG("child of 0x%08x.\n", client->frame);
493         xcb_reparent_window(conn, client->child, root, 0, 0);
494
495         client_unmap(conn, client);
496
497         xcb_destroy_window(conn, client->frame);
498         xcb_flush(conn);
499         table_remove(&by_parent, client->frame);
500
501         if (client->container != NULL) {
502                 Workspace *workspace = client->container->workspace;
503                 cleanup_table(conn, workspace);
504                 fix_colrowspan(conn, workspace);
505         }
506
507         /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
508         bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
509         bool workspace_focused = (c_ws == client->workspace);
510         Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
511
512         /* If this workspace is currently visible, we don’t delete it */
513         if (workspace_is_visible(client->workspace))
514                 workspace_empty = false;
515
516         if (workspace_empty) {
517                 client->workspace->output = NULL;
518                 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
519         }
520
521         /* Remove the urgency flag if set */
522         client->urgent = false;
523         workspace_update_urgent_flag(client->workspace);
524
525         FREE(client->window_class_instance);
526         FREE(client->window_class_class);
527         FREE(client->name);
528         free(client);
529
530         render_layout(conn);
531
532         /* Ensure the focus is set to the next client in the focus stack or to
533          * the screen itself (if we do not focus the screen, it can happen that
534          * the focus is "nowhere" and thus keypress events will not be received
535          * by i3, thus the user cannot use any hotkeys). */
536         if (workspace_focused) {
537                 if (to_focus != NULL)
538                         set_focus(conn, to_focus, true);
539                 else {
540                         DLOG("Restoring focus to root screen\n");
541                         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
542                         xcb_flush(conn);
543                 }
544         }
545
546         return 1;
547 }
548
549 /*
550  * A destroy notify event is sent when the window is not unmapped, but
551  * immediately destroyed (for example when starting a window and immediately
552  * killing the program which started it).
553  *
554  * We just pass on the event to the unmap notify handler (by copying the
555  * important fields in the event data structure).
556  *
557  */
558 int handle_destroy_notify_event(void *data, xcb_connection_t *conn, xcb_destroy_notify_event_t *event) {
559         DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
560
561         xcb_unmap_notify_event_t unmap;
562         unmap.sequence = event->sequence;
563         unmap.event = event->event;
564         unmap.window = event->window;
565
566         return handle_unmap_notify_event(NULL, conn, &unmap);
567 }
568 #endif
569 /*
570  * Called when a window changes its title
571  *
572  */
573 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
574                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
575     Con *con;
576     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
577         return 1;
578
579     window_update_name(con->window, prop);
580
581     x_push_changes(croot);
582
583     return 1;
584 }
585
586 /*
587  * Handles legacy window name updates (WM_NAME), see also src/window.c,
588  * window_update_name_legacy().
589  *
590  */
591 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
592                                 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
593     Con *con;
594     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
595         return 1;
596
597     window_update_name_legacy(con->window, prop);
598
599     x_push_changes(croot);
600
601     return 1;
602 }
603
604 /*
605  * Updates the client’s WM_CLASS property
606  *
607  */
608 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
609                              xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
610     Con *con;
611     if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
612         return 1;
613
614     window_update_class(con->window, prop);
615
616     return 0;
617 }
618
619 #if 0
620 /*
621  * Expose event means we should redraw our windows (= title bar)
622  *
623  */
624 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
625         /* event->count is the number of minimum remaining expose events for this window, so we
626            skip all events but the last one */
627         if (event->count != 0)
628                 return 1;
629         DLOG("window = %08x\n", event->window);
630
631         Client *client = table_get(&by_parent, event->window);
632         if (client == NULL) {
633                 /* There was no client in the table, so this is probably an expose event for
634                    one of our stack_windows. */
635                 struct Stack_Window *stack_win;
636                 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
637                         if (stack_win->window == event->window) {
638                                 render_container(conn, stack_win->container);
639                                 return 1;
640                         }
641
642                 /* …or one of the bars? */
643                 Output *output;
644                 TAILQ_FOREACH(output, &outputs, outputs)
645                         if (output->bar == event->window)
646                                 render_layout(conn);
647                 return 1;
648         }
649
650         if (client->dock)
651                 return 1;
652
653         if (container_mode(client->container, true) == MODE_DEFAULT)
654                 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
655         else {
656                 uint32_t background_color;
657                 if (client->urgent)
658                         background_color = config.client.urgent.background;
659                 /* Distinguish if the window is currently focused… */
660                 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
661                         background_color = config.client.focused.background;
662                 /* …or if it is the focused window in a not focused container */
663                 else background_color = config.client.focused_inactive.background;
664
665                 /* Set foreground color to current focused color, line width to 2 */
666                 uint32_t values[] = {background_color, 2};
667                 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
668
669                 /* Draw the border, the ±1 is for line width = 2 */
670                 xcb_point_t points[] = {{1, 0},                                           /* left upper edge */
671                                         {1, client->rect.height-1},                       /* left bottom edge */
672                                         {client->rect.width-1, client->rect.height-1},    /* right bottom edge */
673                                         {client->rect.width-1, 0}};                       /* right upper edge */
674                 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
675
676                 /* Draw a black background */
677                 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
678                 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless) {
679                         xcb_rectangle_t crect = {1, 0, client->rect.width - (1 + 1), client->rect.height - 1};
680                         xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
681                 } else {
682                         xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
683                         xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
684                 }
685         }
686         xcb_flush(conn);
687         return 1;
688 }
689
690 /*
691  * Handle client messages (EWMH)
692  *
693  */
694 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
695         if (event->type == atoms[_NET_WM_STATE]) {
696                 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
697                         return 0;
698
699                 Client *client = table_get(&by_child, event->window);
700                 if (client == NULL)
701                         return 0;
702
703                 /* Check if the fullscreen state should be toggled */
704                 if ((client->fullscreen &&
705                      (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
706                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
707                     (!client->fullscreen &&
708                      (event->data.data32[0] == _NET_WM_STATE_ADD ||
709                       event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
710                         client_toggle_fullscreen(conn, client);
711         } else {
712                 ELOG("unhandled clientmessage\n");
713                 return 0;
714         }
715
716         return 1;
717 }
718
719 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
720                         xcb_atom_t atom, xcb_get_property_reply_t *property) {
721         /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
722          before changing this property. */
723         ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
724         return 0;
725 }
726
727 /*
728  * Handles the size hints set by a window, but currently only the part necessary for displaying
729  * clients proportionally inside their frames (mplayer for example)
730  *
731  * See ICCCM 4.1.2.3 for more details
732  *
733  */
734 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
735                         xcb_atom_t name, xcb_get_property_reply_t *reply) {
736         Client *client = table_get(&by_child, window);
737         if (client == NULL) {
738                 DLOG("Received WM_SIZE_HINTS for unknown client\n");
739                 return 1;
740         }
741         xcb_size_hints_t size_hints;
742
743         CLIENT_LOG(client);
744
745         /* If the hints were already in this event, use them, if not, request them */
746         if (reply != NULL)
747                 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
748         else
749                 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
750
751         if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
752                 // TODO: Minimum size is not yet implemented
753                 DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
754         }
755
756         bool changed = false;
757         if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
758                 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
759                         if (client->width_increment != size_hints.width_inc) {
760                                 client->width_increment = size_hints.width_inc;
761                                 changed = true;
762                         }
763                 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
764                         if (client->height_increment != size_hints.height_inc) {
765                                 client->height_increment = size_hints.height_inc;
766                                 changed = true;
767                         }
768
769                 if (changed)
770                         DLOG("resize increments changed\n");
771         }
772
773         int base_width = 0, base_height = 0;
774
775         /* base_width/height are the desired size of the window.
776            We check if either the program-specified size or the program-specified
777            min-size is available */
778         if (size_hints.flags & XCB_SIZE_HINT_BASE_SIZE) {
779                 base_width = size_hints.base_width;
780                 base_height = size_hints.base_height;
781         } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
782                 /* TODO: is this right? icccm says not */
783                 base_width = size_hints.min_width;
784                 base_height = size_hints.min_height;
785         }
786
787         if (base_width != client->base_width ||
788             base_height != client->base_height) {
789                 client->base_width = base_width;
790                 client->base_height = base_height;
791                 DLOG("client's base_height changed to %d\n", base_height);
792                 DLOG("client's base_width changed to %d\n", base_width);
793                 changed = true;
794         }
795
796         if (changed) {
797                 if (client->fullscreen)
798                         DLOG("Not resizing client, it is in fullscreen mode\n");
799                 else {
800                         resize_client(conn, client);
801                         xcb_flush(conn);
802                 }
803         }
804
805         /* If no aspect ratio was set or if it was invalid, we ignore the hints */
806         if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
807             (size_hints.min_aspect_num <= 0) ||
808             (size_hints.min_aspect_den <= 0)) {
809                 return 1;
810         }
811
812         double width = client->rect.width - base_width;
813         double height = client->rect.height - base_height;
814         /* Convert numerator/denominator to a double */
815         double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
816         double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
817
818         DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
819         DLOG("width = %f, height = %f\n", width, height);
820
821         /* Sanity checks, this is user-input, in a way */
822         if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
823                 return 1;
824
825         /* Check if we need to set proportional_* variables using the correct ratio */
826         if ((width / height) < min_aspect) {
827                 client->proportional_width = width;
828                 client->proportional_height = width / min_aspect;
829         } else if ((width / height) > max_aspect) {
830                 client->proportional_width = width;
831                 client->proportional_height = width / max_aspect;
832         } else return 1;
833
834         client->force_reconfigure = true;
835
836         if (client->container != NULL) {
837                 render_container(conn, client->container);
838                 xcb_flush(conn);
839         }
840
841         return 1;
842 }
843
844 /*
845  * Handles the WM_HINTS property for extracting the urgency state of the window.
846  *
847  */
848 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
849                   xcb_atom_t name, xcb_get_property_reply_t *reply) {
850         Client *client = table_get(&by_child, window);
851         if (client == NULL) {
852                 DLOG("Received WM_HINTS for unknown client\n");
853                 return 1;
854         }
855         xcb_wm_hints_t hints;
856
857         if (reply != NULL) {
858                 if (!xcb_get_wm_hints_from_reply(&hints, reply))
859                         return 1;
860         } else {
861                 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
862                         return 1;
863         }
864
865         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
866         if (!client->urgent && client == last_focused) {
867                 DLOG("Ignoring urgency flag for current client\n");
868                 return 1;
869         }
870
871         /* Update the flag on the client directly */
872         client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
873         CLIENT_LOG(client);
874         LOG("Urgency flag changed to %d\n", client->urgent);
875
876         workspace_update_urgent_flag(client->workspace);
877         redecorate_window(conn, client);
878
879         /* If the workspace this client is on is not visible, we need to redraw
880          * the workspace bar */
881         if (!workspace_is_visible(client->workspace)) {
882                 Output *output = client->workspace->output;
883                 render_workspace(conn, output, output->current_workspace);
884                 xcb_flush(conn);
885         }
886
887         return 1;
888 }
889
890 /*
891  * Handles the transient for hints set by a window, signalizing that this window is a popup window
892  * for some other window.
893  *
894  * See ICCCM 4.1.2.6 for more details
895  *
896  */
897 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
898                          xcb_atom_t name, xcb_get_property_reply_t *reply) {
899         Client *client = table_get(&by_child, window);
900         if (client == NULL) {
901                 DLOG("No such client\n");
902                 return 1;
903         }
904
905         xcb_window_t transient_for;
906
907         if (reply != NULL) {
908                 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
909                         return 1;
910         } else {
911                 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
912                                                     &transient_for, NULL))
913                         return 1;
914         }
915
916         if (client->floating == FLOATING_AUTO_OFF) {
917                 DLOG("This is a popup window, putting into floating\n");
918                 toggle_floating_mode(conn, client, true);
919         }
920
921         return 1;
922 }
923
924 /*
925  * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
926  * toolwindow (or similar) and to which window it belongs (logical parent).
927  *
928  */
929 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
930                         xcb_atom_t name, xcb_get_property_reply_t *prop) {
931         if (prop == NULL) {
932                 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
933                                         false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
934                 if (prop == NULL)
935                         return 1;
936         }
937
938         Client *client = table_get(&by_child, window);
939         if (client == NULL)
940                 return 1;
941
942         xcb_window_t *leader = xcb_get_property_value(prop);
943         if (leader == NULL)
944                 return 1;
945
946         DLOG("Client leader changed to %08x\n", *leader);
947
948         client->leader = *leader;
949
950         return 1;
951 }
952 #endif