2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
13 #include <xcb/xcb_atom.h>
14 #include <xcb/randr.h>
16 #include <X11/XKBlib.h>
20 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
21 since it’d trigger an infinite loop of switching between the different windows when
22 changing workspaces */
23 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
25 static void add_ignore_event(const int sequence) {
26 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
28 event->sequence = sequence;
29 event->added = time(NULL);
31 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
35 * Checks if the given sequence is ignored and returns true if so.
38 static bool event_is_ignored(const int sequence) {
39 struct Ignore_Event *event;
40 time_t now = time(NULL);
41 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
42 if ((now - event->added) > 5) {
43 struct Ignore_Event *save = event;
44 event = SLIST_NEXT(event, ignore_events);
45 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
47 } else event = SLIST_NEXT(event, ignore_events);
50 SLIST_FOREACH(event, &ignore_events, ignore_events) {
51 if (event->sequence == sequence) {
52 SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
62 * There was a key press. We compare this key code with our bindings table and pass
63 * the bound action to parse_command().
66 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
67 DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
69 /* Remove the numlock bit, all other bits are modifiers we can bind to */
70 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
71 DLOG("(removed numlock, state = %d)\n", state_filtered);
72 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
73 * button masks are filtered out */
74 state_filtered &= 0xFF;
75 DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
77 if (xkb_current_group == XkbGroup2Index)
78 state_filtered |= BIND_MODE_SWITCH;
80 DLOG("(checked mode_switch, state %d)\n", state_filtered);
82 /* Find the binding */
83 Binding *bind = get_binding(state_filtered, event->detail);
85 /* No match? Then the user has Mode_switch enabled but does not have a
86 * specific keybinding. Fall back to the default keybindings (without
87 * Mode_switch). Makes it much more convenient for users of a hybrid
88 * layout (like us, ru). */
90 state_filtered &= ~(BIND_MODE_SWITCH);
91 DLOG("no match, new state_filtered = %d\n", state_filtered);
92 if ((bind = get_binding(state_filtered, event->detail)) == NULL) {
93 ELOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
94 state_filtered, event->detail);
99 parse_command(bind->command);
106 * Called with coordinates of an enter_notify event or motion_notify event
107 * to check if the user crossed virtual screen boundaries and adjust the
108 * current workspace, if so.
111 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
114 if ((output = get_output_containing(x, y)) == NULL) {
115 ELOG("ERROR: No such screen\n");
118 if (output == c_ws->output)
121 c_ws->current_row = current_row;
122 c_ws->current_col = current_col;
123 c_ws = output->current_workspace;
124 current_row = c_ws->current_row;
125 current_col = c_ws->current_col;
126 DLOG("We're now on output %p\n", output);
128 /* While usually this function is only called when the user switches
129 * to a different output using his mouse (and thus the output is
130 * empty), it may be that the following race condition occurs:
131 * 1) the user actives a new output (say VGA1).
132 * 2) the cursor is sent to the first pixel of the new VGA1, thus
133 * generating an enter_notify for the screen (the enter_notify
134 * is not yet received by i3).
135 * 3) i3 requeries screen configuration and maps a workspace onto the
137 * 4) the enter_notify event arrives and c_ws is set to the new
138 * workspace but the existing windows on the new workspace are not
141 * Therefore, we re-set the focus here to be sure it’s correct. */
142 Client *first_client = SLIST_FIRST(&(c_ws->focus_stack));
143 if (first_client != NULL)
144 set_focus(global_conn, first_client, true);
148 * When the user moves the mouse pointer onto a window, this callback gets called.
151 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
152 DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
153 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
154 DLOG("This was not a normal notify, ignoring\n");
157 /* Some events are not interesting, because they were not generated actively by the
158 user, but by reconfiguration of windows */
159 if (event_is_ignored(event->sequence))
162 /* This was either a focus for a client’s parent (= titlebar)… */
163 Client *client = table_get(&by_parent, event->event);
164 /* …or the client itself */
166 client = table_get(&by_child, event->event);
168 /* Check for stack windows */
169 if (client == NULL) {
170 struct Stack_Window *stack_win;
171 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
172 if (stack_win->window == event->event) {
173 client = stack_win->container->currently_focused;
179 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
180 if (client == NULL) {
181 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
182 check_crossing_screen_boundary(event->root_x, event->root_y);
186 /* Do plausibility checks: This event may be useless for us if it occurs on a window
187 which is in a stacked container but not the focused one */
188 if (client->container != NULL &&
189 client->container->mode == MODE_STACK &&
190 client->container->currently_focused != client) {
191 DLOG("Plausibility check says: no\n");
195 if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
196 /* This can happen when a client gets assigned to a different workspace than
197 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
198 * an enter_notify will follow. */
199 DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
203 if (!config.disable_focus_follows_mouse)
204 set_focus(conn, client, false);
210 * When the user moves the mouse but does not change the active window
211 * (e.g. when having no windows opened but moving mouse on the root screen
212 * and crossing virtual screen boundaries), this callback gets called.
215 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
216 /* Skip events where the pointer was over a child window, we are only
217 * interested in events on the root window. */
218 if (event->child != 0)
221 check_crossing_screen_boundary(event->root_x, event->root_y);
227 * Called when the keyboard mapping changes (for example by using Xmodmap),
228 * we need to update our key bindings then (re-translate symbols).
231 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
232 if (event->request != XCB_MAPPING_KEYBOARD &&
233 event->request != XCB_MAPPING_MODIFIER)
236 DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
237 xcb_refresh_keyboard_mapping(keysyms, event);
239 xcb_get_numlock_mask(conn);
241 ungrab_all_keys(conn);
243 grab_all_keys(conn, false);
249 * A new window appeared on the screen (=was mapped), so let’s manage it.
252 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
253 xcb_get_window_attributes_cookie_t cookie;
255 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
257 DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
258 add_ignore_event(event->sequence);
260 manage_window(prophs, conn, event->window, cookie, false);
265 * Configure requests are received when the application wants to resize windows on their own.
267 * We generate a synthethic configure notify event to signalize the client its "new" position.
270 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
271 DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
272 event->window, event->x, event->y, event->width, event->height);
274 Client *client = table_get(&by_child, event->window);
275 if (client == NULL) {
279 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
280 if (event->value_mask & mask_member) { \
281 mask |= mask_member; \
282 values[c++] = event->event_member; \
286 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
287 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
288 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
289 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
290 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
291 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
292 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
294 xcb_configure_window(conn, event->window, mask, values);
300 if (client->fullscreen) {
301 DLOG("Client is in fullscreen mode\n");
303 Rect child_rect = client->workspace->rect;
304 child_rect.x = child_rect.y = 0;
305 fake_configure_notify(conn, child_rect, client->child);
310 /* Floating clients can be reconfigured */
311 if (client_is_floating(client)) {
312 i3Font *font = load_font(conn, config.font);
313 int mode = (client->container != NULL ? client->container->mode : MODE_DEFAULT);
314 /* TODO: refactor this code. we need a function to translate
315 * coordinates of child_rect/rect. */
317 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
318 if (mode == MODE_STACK || mode == MODE_TABBED) {
319 client->rect.x = event->x - 2;
321 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
322 client->rect.x = event->x;
323 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
324 client->rect.x = event->x - 1;
325 else client->rect.x = event->x - 2;
328 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
329 if (mode == MODE_STACK || mode == MODE_TABBED) {
330 client->rect.y = event->y - 2;
332 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
333 client->rect.y = event->y;
334 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
335 client->rect.y = event->y - 1;
336 else client->rect.y = event->y - font->height - 2 - 2;
339 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
340 if (mode == MODE_STACK || mode == MODE_TABBED) {
341 client->rect.width = event->width + 2 + 2;
343 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
344 client->rect.width = event->width;
345 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
346 client->rect.width = event->width + (1 + 1);
347 else client->rect.width = event->width + (2 + 2);
350 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
351 if (mode == MODE_STACK || mode == MODE_TABBED) {
352 client->rect.height = event->height + 2;
354 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
355 client->rect.height = event->height;
356 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
357 client->rect.height = event->height + (1 + 1);
358 else client->rect.height = event->height + (font->height + 2 + 2) + 2;
362 DLOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
363 client->rect.x, client->rect.y, client->rect.width, client->rect.height);
365 /* Push the new position/size to X11 */
366 reposition_client(conn, client);
367 resize_client(conn, client);
373 /* Dock clients can be reconfigured in their height */
375 DLOG("Reconfiguring height of this dock client\n");
377 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
378 DLOG("Ignoring configure request, no height given\n");
382 client->desired_height = event->height;
383 render_workspace(conn, c_ws->output, c_ws);
389 if (client->fullscreen) {
390 DLOG("Client is in fullscreen mode\n");
392 Rect child_rect = client->container->workspace->rect;
393 child_rect.x = child_rect.y = 0;
394 fake_configure_notify(conn, child_rect, client->child);
399 fake_absolute_configure_notify(conn, client);
405 * Configuration notifies are only handled because we need to set up ignore for
406 * the following enter notify events.
409 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
410 /* We ignore this sequence twice because events for child and frame should be ignored */
411 add_ignore_event(event->sequence);
412 add_ignore_event(event->sequence);
418 * Gets triggered upon a RandR screen change event, that is when the user
419 * changes the screen configuration in any way (mode, position, …)
422 int handle_screen_change(void *prophs, xcb_connection_t *conn,
423 xcb_generic_event_t *e) {
424 DLOG("RandR screen change\n");
426 randr_query_outputs(conn);
428 ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
434 * Our window decorations were unmapped. That means, the window will be killed now,
435 * so we better clean up before.
438 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
439 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
441 add_ignore_event(event->sequence);
443 Client *client = table_get(&by_child, event->window);
444 /* First, we need to check if the client is awaiting an unmap-request which
445 was generated by us reparenting the window. In that case, we just ignore it. */
446 if (client != NULL && client->awaiting_useless_unmap) {
447 client->awaiting_useless_unmap = false;
451 DLOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
452 DLOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
453 if (client == NULL) {
454 DLOG("not a managed window. Ignoring.\n");
456 /* This was most likely the destroyed frame of a client which is
457 * currently being unmapped, so we add this sequence (again!) to
458 * the ignore list (enter_notify events will get sent for both,
459 * the child and its frame). */
460 add_ignore_event(event->sequence);
465 client = table_remove(&by_child, event->window);
467 /* If this was the fullscreen client, we need to unset it */
468 if (client->fullscreen)
469 client->workspace->fullscreen_client = NULL;
471 /* Clients without a container are either floating or dock windows */
472 if (client->container != NULL) {
473 Container *con = client->container;
475 /* Remove the client from the list of clients */
476 client_remove_from_container(conn, client, con, true);
478 /* Set focus to the last focused client in this container */
479 con->currently_focused = get_last_focused_client(conn, con, NULL);
481 /* Only if this is the active container, we need to really change focus */
482 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
483 set_focus(conn, con->currently_focused, true);
484 } else if (client_is_floating(client)) {
485 DLOG("Removing from floating clients\n");
486 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
487 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
491 DLOG("Removing from dock clients\n");
492 SLIST_REMOVE(&(client->workspace->output->dock_clients), client, Client, dock_clients);
495 DLOG("child of 0x%08x.\n", client->frame);
496 xcb_reparent_window(conn, client->child, root, 0, 0);
498 client_unmap(conn, client);
500 xcb_destroy_window(conn, client->frame);
502 table_remove(&by_parent, client->frame);
504 if (client->container != NULL) {
505 Workspace *workspace = client->container->workspace;
506 cleanup_table(conn, workspace);
507 fix_colrowspan(conn, workspace);
510 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
511 bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
512 bool workspace_focused = (c_ws == client->workspace);
513 Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
515 /* If this workspace is currently visible, we don’t delete it */
516 if (workspace_is_visible(client->workspace))
517 workspace_empty = false;
519 if (workspace_empty) {
520 client->workspace->output = NULL;
521 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
524 /* Remove the urgency flag if set */
525 client->urgent = false;
526 workspace_update_urgent_flag(client->workspace);
528 FREE(client->window_class_instance);
529 FREE(client->window_class_class);
535 /* Ensure the focus is set to the next client in the focus stack or to
536 * the screen itself (if we do not focus the screen, it can happen that
537 * the focus is "nowhere" and thus keypress events will not be received
538 * by i3, thus the user cannot use any hotkeys). */
539 if (workspace_focused) {
540 if (to_focus != NULL)
541 set_focus(conn, to_focus, true);
543 DLOG("Restoring focus to root screen\n");
544 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
553 * A destroy notify event is sent when the window is not unmapped, but
554 * immediately destroyed (for example when starting a window and immediately
555 * killing the program which started it).
557 * We just pass on the event to the unmap notify handler (by copying the
558 * important fields in the event data structure).
561 int handle_destroy_notify_event(void *data, xcb_connection_t *conn, xcb_destroy_notify_event_t *event) {
562 DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
564 xcb_unmap_notify_event_t unmap;
565 unmap.sequence = event->sequence;
566 unmap.event = event->event;
567 unmap.window = event->window;
569 return handle_unmap_notify_event(NULL, conn, &unmap);
573 * Called when a window changes its title
576 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
577 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
578 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
579 DLOG("_NET_WM_NAME not specified, not changing\n");
582 Client *client = table_get(&by_child, window);
586 /* Save the old pointer to make the update atomic */
589 asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
590 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
591 char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
592 LOG("_NET_WM_NAME changed to \"%s\"\n", new_name);
595 /* Check if they are the same and don’t update if so.
596 Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
597 Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
598 but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
599 if ((new_len == client->name_len) &&
600 (client->name != NULL) &&
601 (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
606 char *old_name = client->name;
607 client->name = ucs2_name;
608 client->name_len = new_len;
609 client->uses_net_wm_name = true;
613 /* If the client is a dock window, we don’t need to render anything */
617 int mode = container_mode(client->container, true);
618 if (mode == MODE_STACK || mode == MODE_TABBED)
619 render_container(conn, client->container);
620 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
627 * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
628 * just pass them along, so when containing non-ASCII characters, those will be rendering
629 * incorrectly. In order to correctly render unicode window titles in i3, an application
630 * has to set _NET_WM_NAME, which is in UTF-8 encoding.
632 * On every update, a message is put out to the user, so he may improve the situation and
633 * update applications which display filenames in their title to correctly use
634 * _NET_WM_NAME and therefore support unicode.
637 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
638 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
639 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
640 DLOG("prop == NULL\n");
643 Client *client = table_get(&by_child, window);
647 /* Client capable of _NET_WM_NAME, ignore legacy name changes */
648 if (client->uses_net_wm_name)
651 /* Save the old pointer to make the update atomic */
653 if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
654 perror("Could not get old name");
655 DLOG("Could not get old name\n");
658 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
659 LOG("WM_NAME changed to \"%s\"\n", new_name);
661 /* Check if they are the same and don’t update if so. */
662 if (client->name != NULL &&
663 strlen(new_name) == strlen(client->name) &&
664 strcmp(client->name, new_name) == 0) {
669 LOG("Using legacy window title. Note that in order to get Unicode window titles in i3, "
670 "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
672 char *old_name = client->name;
673 client->name = new_name;
674 client->name_len = -1;
676 if (old_name != NULL)
679 /* If the client is a dock window, we don’t need to render anything */
683 if (client->container != NULL &&
684 (client->container->mode == MODE_STACK ||
685 client->container->mode == MODE_TABBED))
686 render_container(conn, client->container);
687 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
694 * Updates the client’s WM_CLASS property
697 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
698 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
699 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
700 DLOG("prop == NULL\n");
703 Client *client = table_get(&by_child, window);
707 /* We cannot use asprintf here since this property contains two
708 * null-terminated strings (for compatibility reasons). Instead, we
709 * use strdup() on both strings */
710 char *new_class = xcb_get_property_value(prop);
712 FREE(client->window_class_instance);
713 FREE(client->window_class_class);
715 client->window_class_instance = strdup(new_class);
716 if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop))
717 client->window_class_class = strdup(new_class + strlen(new_class) + 1);
718 else client->window_class_class = NULL;
719 LOG("WM_CLASS changed to %s (instance), %s (class)\n",
720 client->window_class_instance, client->window_class_class);
726 * Expose event means we should redraw our windows (= title bar)
729 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
730 /* event->count is the number of minimum remaining expose events for this window, so we
731 skip all events but the last one */
732 if (event->count != 0)
734 DLOG("window = %08x\n", event->window);
736 Client *client = table_get(&by_parent, event->window);
737 if (client == NULL) {
738 /* There was no client in the table, so this is probably an expose event for
739 one of our stack_windows. */
740 struct Stack_Window *stack_win;
741 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
742 if (stack_win->window == event->window) {
743 render_container(conn, stack_win->container);
747 /* …or one of the bars? */
749 TAILQ_FOREACH(output, &outputs, outputs)
750 if (output->bar == event->window)
758 if (container_mode(client->container, true) == MODE_DEFAULT)
759 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
761 uint32_t background_color;
763 background_color = config.client.urgent.background;
764 /* Distinguish if the window is currently focused… */
765 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
766 background_color = config.client.focused.background;
767 /* …or if it is the focused window in a not focused container */
768 else background_color = config.client.focused_inactive.background;
770 /* Set foreground color to current focused color, line width to 2 */
771 uint32_t values[] = {background_color, 2};
772 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
774 /* Draw the border, the ±1 is for line width = 2 */
775 xcb_point_t points[] = {{1, 0}, /* left upper edge */
776 {1, client->rect.height-1}, /* left bottom edge */
777 {client->rect.width-1, client->rect.height-1}, /* right bottom edge */
778 {client->rect.width-1, 0}}; /* right upper edge */
779 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
781 /* Draw a black background */
782 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
783 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless) {
784 xcb_rectangle_t crect = {1, 0, client->rect.width - (1 + 1), client->rect.height - 1};
785 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
787 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
788 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
796 * Handle client messages (EWMH)
799 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
800 if (event->type == atoms[_NET_WM_STATE]) {
801 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
804 Client *client = table_get(&by_child, event->window);
808 /* Check if the fullscreen state should be toggled */
809 if ((client->fullscreen &&
810 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
811 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
812 (!client->fullscreen &&
813 (event->data.data32[0] == _NET_WM_STATE_ADD ||
814 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
815 client_toggle_fullscreen(conn, client);
817 ELOG("unhandled clientmessage\n");
824 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
825 xcb_atom_t atom, xcb_get_property_reply_t *property) {
826 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
827 before changing this property. */
828 ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
833 * Handles the size hints set by a window, but currently only the part necessary for displaying
834 * clients proportionally inside their frames (mplayer for example)
836 * See ICCCM 4.1.2.3 for more details
839 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
840 xcb_atom_t name, xcb_get_property_reply_t *reply) {
841 Client *client = table_get(&by_child, window);
842 if (client == NULL) {
843 DLOG("Received WM_SIZE_HINTS for unknown client\n");
846 xcb_size_hints_t size_hints;
850 /* If the hints were already in this event, use them, if not, request them */
852 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
854 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
856 if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
857 // TODO: Minimum size is not yet implemented
858 DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
861 bool changed = false;
862 if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
863 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
864 if (client->width_increment != size_hints.width_inc) {
865 client->width_increment = size_hints.width_inc;
868 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
869 if (client->height_increment != size_hints.height_inc) {
870 client->height_increment = size_hints.height_inc;
875 DLOG("resize increments changed\n");
878 int base_width = 0, base_height = 0;
880 /* base_width/height are the desired size of the window.
881 We check if either the program-specified size or the program-specified
882 min-size is available */
883 if (size_hints.flags & XCB_SIZE_HINT_BASE_SIZE) {
884 base_width = size_hints.base_width;
885 base_height = size_hints.base_height;
886 } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
887 /* TODO: is this right? icccm says not */
888 base_width = size_hints.min_width;
889 base_height = size_hints.min_height;
892 if (base_width != client->base_width ||
893 base_height != client->base_height) {
894 client->base_width = base_width;
895 client->base_height = base_height;
896 DLOG("client's base_height changed to %d\n", base_height);
897 DLOG("client's base_width changed to %d\n", base_width);
902 if (client->fullscreen)
903 DLOG("Not resizing client, it is in fullscreen mode\n");
905 resize_client(conn, client);
910 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
911 if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
912 (size_hints.min_aspect_num <= 0) ||
913 (size_hints.min_aspect_den <= 0)) {
917 double width = client->rect.width - base_width;
918 double height = client->rect.height - base_height;
919 /* Convert numerator/denominator to a double */
920 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
921 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
923 DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
924 DLOG("width = %f, height = %f\n", width, height);
926 /* Sanity checks, this is user-input, in a way */
927 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
930 /* Check if we need to set proportional_* variables using the correct ratio */
931 if ((width / height) < min_aspect) {
932 client->proportional_width = width;
933 client->proportional_height = width / min_aspect;
934 } else if ((width / height) > max_aspect) {
935 client->proportional_width = width;
936 client->proportional_height = width / max_aspect;
939 client->force_reconfigure = true;
941 if (client->container != NULL) {
942 render_container(conn, client->container);
950 * Handles the WM_HINTS property for extracting the urgency state of the window.
953 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
954 xcb_atom_t name, xcb_get_property_reply_t *reply) {
955 Client *client = table_get(&by_child, window);
956 if (client == NULL) {
957 DLOG("Received WM_HINTS for unknown client\n");
960 xcb_wm_hints_t hints;
963 if (!xcb_get_wm_hints_from_reply(&hints, reply))
966 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
970 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
971 if (!client->urgent && client == last_focused) {
972 DLOG("Ignoring urgency flag for current client\n");
976 /* Update the flag on the client directly */
977 client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
979 LOG("Urgency flag changed to %d\n", client->urgent);
981 workspace_update_urgent_flag(client->workspace);
982 redecorate_window(conn, client);
984 /* If the workspace this client is on is not visible, we need to redraw
985 * the workspace bar */
986 if (!workspace_is_visible(client->workspace)) {
987 Output *output = client->workspace->output;
988 render_workspace(conn, output, output->current_workspace);
996 * Handles the transient for hints set by a window, signalizing that this window is a popup window
997 * for some other window.
999 * See ICCCM 4.1.2.6 for more details
1002 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1003 xcb_atom_t name, xcb_get_property_reply_t *reply) {
1004 Client *client = table_get(&by_child, window);
1005 if (client == NULL) {
1006 DLOG("No such client\n");
1010 xcb_window_t transient_for;
1012 if (reply != NULL) {
1013 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
1016 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
1017 &transient_for, NULL))
1021 if (client->floating == FLOATING_AUTO_OFF) {
1022 DLOG("This is a popup window, putting into floating\n");
1023 toggle_floating_mode(conn, client, true);
1030 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
1031 * toolwindow (or similar) and to which window it belongs (logical parent).
1034 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1035 xcb_atom_t name, xcb_get_property_reply_t *prop) {
1037 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1038 false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
1043 Client *client = table_get(&by_child, window);
1047 xcb_window_t *leader = xcb_get_property_value(prop);
1051 DLOG("Client leader changed to %08x\n", *leader);
1053 client->leader = *leader;