4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
18 #include <xcb/xcb_atom.h>
19 #include <xcb/xcb_icccm.h>
21 #include <X11/XKBlib.h>
38 #include "workspace.h"
40 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
41 since it’d trigger an infinite loop of switching between the different windows when
42 changing workspaces */
43 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
45 static void add_ignore_event(const int sequence) {
46 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
48 event->sequence = sequence;
49 event->added = time(NULL);
51 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
55 * Checks if the given sequence is ignored and returns true if so.
58 static bool event_is_ignored(const int sequence) {
59 struct Ignore_Event *event;
60 time_t now = time(NULL);
61 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
62 if ((now - event->added) > 5) {
63 struct Ignore_Event *save = event;
64 event = SLIST_NEXT(event, ignore_events);
65 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
67 } else event = SLIST_NEXT(event, ignore_events);
70 SLIST_FOREACH(event, &ignore_events, ignore_events) {
71 if (event->sequence == sequence) {
72 SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
82 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
83 * Therefore, we just replay all key presses.
86 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
87 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
93 * There was a key press. We compare this key code with our bindings table and pass
94 * the bound action to parse_command().
97 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
98 LOG("Keypress %d, state raw = %d\n", event->detail, event->state);
100 /* Remove the numlock bit, all other bits are modifiers we can bind to */
101 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
102 LOG("(removed numlock, state = %d)\n", state_filtered);
103 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
104 * button masks are filtered out */
105 state_filtered &= 0xFF;
106 LOG("(removed upper 8 bits, state = %d)\n", state_filtered);
109 /* We need to get the keysym group (There are group 1 to group 4, each holding
110 two keysyms (without shift and with shift) using Xkb because X fails to
111 provide them reliably (it works in Xephyr, it does not in real X) */
113 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
114 state_filtered |= BIND_MODE_SWITCH;
117 LOG("(checked mode_switch, state %d)\n", state_filtered);
119 /* Find the binding */
121 TAILQ_FOREACH(bind, bindings, bindings) {
122 /* First compare the modifiers */
123 if (bind->mods != state_filtered)
126 /* If a symbol was specified by the user, we need to look in
127 * the array of translated keycodes for the event’s keycode */
128 if (bind->symbol != NULL) {
129 if (memmem(bind->translated_to,
130 bind->number_keycodes * sizeof(xcb_keycode_t),
131 &(event->detail), sizeof(xcb_keycode_t)) != NULL)
134 /* This case is easier: The user specified a keycode */
135 if (bind->keycode == event->detail)
140 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
141 the user did not press Mode_switch, so just pass it… */
142 if (bind == TAILQ_END(bindings)) {
143 xcb_allow_events(conn, ReplayKeyboard, event->time);
148 parse_command(conn, bind->command);
149 if (state_filtered & BIND_MODE_SWITCH) {
150 LOG("Mode_switch -> allow_events(SyncKeyboard)\n");
151 xcb_allow_events(conn, SyncKeyboard, event->time);
158 * Called with coordinates of an enter_notify event or motion_notify event
159 * to check if the user crossed virtual screen boundaries and adjust the
160 * current workspace, if so.
163 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
166 if ((screen = get_screen_containing(x, y)) == NULL) {
167 LOG("ERROR: No such screen\n");
170 if (screen == c_ws->screen)
173 c_ws->current_row = current_row;
174 c_ws->current_col = current_col;
175 c_ws = screen->current_workspace;
176 current_row = c_ws->current_row;
177 current_col = c_ws->current_col;
178 LOG("We're now on virtual screen number %d\n", screen->num);
182 * When the user moves the mouse pointer onto a window, this callback gets called.
185 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
186 LOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
187 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
188 LOG("This was not a normal notify, ignoring\n");
191 /* Some events are not interesting, because they were not generated actively by the
192 user, but by reconfiguration of windows */
193 if (event_is_ignored(event->sequence))
196 /* This was either a focus for a client’s parent (= titlebar)… */
197 Client *client = table_get(&by_parent, event->event);
198 /* …or the client itself */
200 client = table_get(&by_child, event->event);
202 /* Check for stack windows */
203 if (client == NULL) {
204 struct Stack_Window *stack_win;
205 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
206 if (stack_win->window == event->event) {
207 client = stack_win->container->currently_focused;
213 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
214 if (client == NULL) {
215 LOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
216 check_crossing_screen_boundary(event->root_x, event->root_y);
220 /* Do plausibility checks: This event may be useless for us if it occurs on a window
221 which is in a stacked container but not the focused one */
222 if (client->container != NULL &&
223 client->container->mode == MODE_STACK &&
224 client->container->currently_focused != client) {
225 LOG("Plausibility check says: no\n");
229 if (client->workspace != c_ws && client->workspace->screen == c_ws->screen) {
230 /* This can happen when a client gets assigned to a different workspace than
231 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
232 * an enter_notify will follow. */
233 LOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
237 set_focus(conn, client, false);
243 * When the user moves the mouse but does not change the active window
244 * (e.g. when having no windows opened but moving mouse on the root screen
245 * and crossing virtual screen boundaries), this callback gets called.
248 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
249 /* Skip events where the pointer was over a child window, we are only
250 * interested in events on the root window. */
251 if (event->child != 0)
254 check_crossing_screen_boundary(event->root_x, event->root_y);
260 * Called when the keyboard mapping changes (for example by using Xmodmap),
261 * we need to update our key bindings then (re-translate symbols).
264 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
265 if (event->request != XCB_MAPPING_KEYBOARD &&
266 event->request != XCB_MAPPING_MODIFIER)
269 LOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
270 xcb_refresh_keyboard_mapping(keysyms, event);
272 xcb_get_numlock_mask(conn);
274 ungrab_all_keys(conn);
281 * A new window appeared on the screen (=was mapped), so let’s manage it.
284 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
285 xcb_get_window_attributes_cookie_t cookie;
287 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
289 LOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
290 add_ignore_event(event->sequence);
292 manage_window(prophs, conn, event->window, cookie, false);
297 * Configure requests are received when the application wants to resize windows on their own.
299 * We generate a synthethic configure notify event to signalize the client its "new" position.
302 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
303 LOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
304 event->window, event->x, event->y, event->width, event->height);
306 Client *client = table_get(&by_child, event->window);
307 if (client == NULL) {
311 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
312 if (event->value_mask & mask_member) { \
313 mask |= mask_member; \
314 values[c++] = event->event_member; \
318 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
319 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
320 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
321 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
322 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
323 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
324 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
326 xcb_configure_window(conn, event->window, mask, values);
332 if (client->fullscreen) {
333 LOG("Client is in fullscreen mode\n");
335 Rect child_rect = client->workspace->rect;
336 child_rect.x = child_rect.y = 0;
337 fake_configure_notify(conn, child_rect, client->child);
342 /* Floating clients can be reconfigured */
343 if (client_is_floating(client)) {
344 i3Font *font = load_font(conn, config.font);
345 int mode = (client->container != NULL ? client->container->mode : MODE_DEFAULT);
347 if (event->value_mask & XCB_CONFIG_WINDOW_X)
348 client->rect.x = event->x;
349 if (event->value_mask & XCB_CONFIG_WINDOW_Y)
350 client->rect.y = event->y;
351 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
352 if (mode == MODE_STACK || mode == MODE_TABBED) {
353 client->rect.width = event->width + 2 + 2;
355 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
356 client->rect.width = event->width;
357 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
358 client->rect.width = event->width + (1 + 1);
359 else client->rect.width = event->width + (2 + 2);
362 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
363 if (mode == MODE_STACK || mode == MODE_TABBED) {
364 client->rect.height = event->height + 2;
366 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
367 client->rect.height = event->height;
368 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
369 client->rect.height = event->height + (1 + 1);
370 else client->rect.height = event->height + (font->height + 2 + 2) + 2;
374 LOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
375 client->rect.x, client->rect.y, client->rect.width, client->rect.height);
377 /* Push the new position/size to X11 */
378 reposition_client(conn, client);
379 resize_client(conn, client);
385 /* Dock clients can be reconfigured in their height */
387 LOG("Reconfiguring height of this dock client\n");
389 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
390 LOG("Ignoring configure request, no height given\n");
394 client->desired_height = event->height;
395 render_workspace(conn, c_ws->screen, c_ws);
401 if (client->fullscreen) {
402 LOG("Client is in fullscreen mode\n");
404 Rect child_rect = client->container->workspace->rect;
405 child_rect.x = child_rect.y = 0;
406 fake_configure_notify(conn, child_rect, client->child);
411 fake_absolute_configure_notify(conn, client);
417 * Configuration notifies are only handled because we need to set up ignore for the following
418 * enter notify events
421 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
422 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
424 /* We ignore this sequence twice because events for child and frame should be ignored */
425 add_ignore_event(event->sequence);
426 add_ignore_event(event->sequence);
428 if (event->event == root) {
429 LOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
430 LOG("reconfigure of the root window, need to xinerama\n");
431 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
432 but is there a better way? */
433 if (event->x == 0 && event->y == 0)
434 xinerama_requery_screens(conn);
442 * Our window decorations were unmapped. That means, the window will be killed now,
443 * so we better clean up before.
446 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
447 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
449 add_ignore_event(event->sequence);
451 Client *client = table_get(&by_child, event->window);
452 /* First, we need to check if the client is awaiting an unmap-request which
453 was generated by us reparenting the window. In that case, we just ignore it. */
454 if (client != NULL && client->awaiting_useless_unmap) {
455 client->awaiting_useless_unmap = false;
459 LOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
460 LOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
461 if (client == NULL) {
462 LOG("not a managed window. Ignoring.\n");
464 /* This was most likely the destroyed frame of a client which is
465 * currently being unmapped, so we add this sequence (again!) to
466 * the ignore list (enter_notify events will get sent for both,
467 * the child and its frame). */
468 add_ignore_event(event->sequence);
473 client = table_remove(&by_child, event->window);
475 /* If this was the fullscreen client, we need to unset it */
476 if (client->fullscreen)
477 client->workspace->fullscreen_client = NULL;
479 /* Clients without a container are either floating or dock windows */
480 if (client->container != NULL) {
481 Container *con = client->container;
483 /* Remove the client from the list of clients */
484 client_remove_from_container(conn, client, con, true);
486 /* Set focus to the last focused client in this container */
487 con->currently_focused = get_last_focused_client(conn, con, NULL);
489 /* Only if this is the active container, we need to really change focus */
490 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
491 set_focus(conn, con->currently_focused, true);
492 } else if (client_is_floating(client)) {
493 LOG("Removing from floating clients\n");
494 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
495 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
499 LOG("Removing from dock clients\n");
500 SLIST_REMOVE(&(client->workspace->screen->dock_clients), client, Client, dock_clients);
503 LOG("child of 0x%08x.\n", client->frame);
504 xcb_reparent_window(conn, client->child, root, 0, 0);
506 client_unmap(conn, client);
508 xcb_destroy_window(conn, client->frame);
510 table_remove(&by_parent, client->frame);
512 if (client->container != NULL) {
513 Workspace *workspace = client->container->workspace;
514 cleanup_table(conn, workspace);
515 fix_colrowspan(conn, workspace);
518 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
519 bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
520 bool workspace_active = false;
521 Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
523 /* If this workspace is currently active, we don’t delete it */
525 TAILQ_FOREACH(screen, virtual_screens, screens)
526 if (screen->current_workspace == client->workspace) {
527 workspace_active = true;
528 workspace_empty = false;
533 client->workspace->screen = NULL;
535 /* Remove the urgency flag if set */
536 client->urgent = false;
537 workspace_update_urgent_flag(client->workspace);
539 FREE(client->window_class);
545 /* Ensure the focus is set to the next client in the focus stack or to
546 * the screen itself (if we do not focus the screen, it can happen that
547 * the focus is "nowhere" and thus keypress events will not be received
548 * by i3, thus the user cannot use any hotkeys). */
549 if (workspace_active) {
550 if (to_focus != NULL)
551 set_focus(conn, to_focus, true);
553 LOG("Restoring focus to root screen\n");
554 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
563 * Called when a window changes its title
566 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
567 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
568 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
569 LOG("_NET_WM_NAME not specified, not changing\n");
572 Client *client = table_get(&by_child, window);
576 /* Save the old pointer to make the update atomic */
579 asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
580 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
581 char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
582 LOG("_NET_WM_NAME changed to \"%s\"\n", new_name);
585 /* Check if they are the same and don’t update if so.
586 Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
587 Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
588 but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
589 if ((new_len == client->name_len) &&
590 (client->name != NULL) &&
591 (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
596 char *old_name = client->name;
597 client->name = ucs2_name;
598 client->name_len = new_len;
599 client->uses_net_wm_name = true;
603 /* If the client is a dock window, we don’t need to render anything */
607 if (client->container != NULL &&
608 (client->container->mode == MODE_STACK ||
609 client->container->mode == MODE_TABBED))
610 render_container(conn, client->container);
611 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
618 * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
619 * just pass them along, so when containing non-ASCII characters, those will be rendering
620 * incorrectly. In order to correctly render unicode window titles in i3, an application
621 * has to set _NET_WM_NAME, which is in UTF-8 encoding.
623 * On every update, a message is put out to the user, so he may improve the situation and
624 * update applications which display filenames in their title to correctly use
625 * _NET_WM_NAME and therefore support unicode.
628 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
629 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
630 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
631 LOG("prop == NULL\n");
634 Client *client = table_get(&by_child, window);
638 /* Client capable of _NET_WM_NAME, ignore legacy name changes */
639 if (client->uses_net_wm_name)
642 /* Save the old pointer to make the update atomic */
644 if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
645 perror("Could not get old name");
646 LOG("Could not get old name\n");
649 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
650 LOG("WM_NAME changed to \"%s\"\n", new_name);
652 /* Check if they are the same and don’t update if so. */
653 if (client->name != NULL &&
654 strlen(new_name) == strlen(client->name) &&
655 strcmp(client->name, new_name) == 0) {
660 LOG("Using legacy window title. Note that in order to get Unicode window titles in i3, "
661 "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
663 char *old_name = client->name;
664 client->name = new_name;
665 client->name_len = -1;
667 if (old_name != NULL)
670 /* If the client is a dock window, we don’t need to render anything */
674 if (client->container != NULL &&
675 (client->container->mode == MODE_STACK ||
676 client->container->mode == MODE_TABBED))
677 render_container(conn, client->container);
678 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
685 * Updates the client’s WM_CLASS property
688 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
689 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
690 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
691 LOG("prop == NULL\n");
694 Client *client = table_get(&by_child, window);
698 if (asprintf(&new_class, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
699 perror("Could not get window class");
700 LOG("Could not get window class\n");
704 LOG("WM_CLASS changed to %s\n", new_class);
705 char *old_class = client->window_class;
706 client->window_class = new_class;
709 if (!client->initialized)
712 if (strcmp(new_class, "tools") == 0 || strcmp(new_class, "Dialog") == 0) {
713 LOG("tool/dialog window, should we put it floating?\n");
714 if (client->floating == FLOATING_AUTO_OFF)
715 toggle_floating_mode(conn, client, true);
722 * Expose event means we should redraw our windows (= title bar)
725 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
726 /* event->count is the number of minimum remaining expose events for this window, so we
727 skip all events but the last one */
728 if (event->count != 0)
730 LOG("window = %08x\n", event->window);
732 Client *client = table_get(&by_parent, event->window);
733 if (client == NULL) {
734 /* There was no client in the table, so this is probably an expose event for
735 one of our stack_windows. */
736 struct Stack_Window *stack_win;
737 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
738 if (stack_win->window == event->window) {
739 render_container(conn, stack_win->container);
743 /* …or one of the bars? */
745 TAILQ_FOREACH(screen, virtual_screens, screens)
746 if (screen->bar == event->window)
754 if (client->container == NULL ||
755 (client->container->mode != MODE_STACK &&
756 client->container->mode != MODE_TABBED))
757 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
759 uint32_t background_color;
761 background_color = config.client.urgent.background;
762 /* Distinguish if the window is currently focused… */
763 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
764 background_color = config.client.focused.background;
765 /* …or if it is the focused window in a not focused container */
766 else background_color = config.client.focused_inactive.background;
768 /* Set foreground color to current focused color, line width to 2 */
769 uint32_t values[] = {background_color, 2};
770 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
772 /* Draw the border, the ±1 is for line width = 2 */
773 xcb_point_t points[] = {{1, 0}, /* left upper edge */
774 {1, client->rect.height-1}, /* left bottom edge */
775 {client->rect.width-1, client->rect.height-1}, /* right bottom edge */
776 {client->rect.width-1, 0}}; /* right upper edge */
777 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
779 /* Draw a black background */
780 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
781 if (client->titlebar_position == TITLEBAR_OFF) {
782 xcb_rectangle_t crect = {1, 0, client->rect.width - (1 + 1), client->rect.height - 1};
783 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
785 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
786 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
794 * Handle client messages (EWMH)
797 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
798 if (event->type == atoms[_NET_WM_STATE]) {
799 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
802 Client *client = table_get(&by_child, event->window);
806 /* Check if the fullscreen state should be toggled */
807 if ((client->fullscreen &&
808 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
809 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
810 (!client->fullscreen &&
811 (event->data.data32[0] == _NET_WM_STATE_ADD ||
812 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
813 client_toggle_fullscreen(conn, client);
815 LOG("unhandled clientmessage\n");
822 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
823 xcb_atom_t atom, xcb_get_property_reply_t *property) {
824 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
825 before changing this property. */
826 LOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
831 * Handles the size hints set by a window, but currently only the part necessary for displaying
832 * clients proportionally inside their frames (mplayer for example)
834 * See ICCCM 4.1.2.3 for more details
837 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
838 xcb_atom_t name, xcb_get_property_reply_t *reply) {
839 Client *client = table_get(&by_child, window);
840 if (client == NULL) {
841 LOG("Received WM_SIZE_HINTS for unknown client\n");
844 xcb_size_hints_t size_hints;
848 /* If the hints were already in this event, use them, if not, request them */
850 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
852 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
854 if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
855 // TODO: Minimum size is not yet implemented
856 //LOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
859 if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
860 bool changed = false;
862 if (size_hints.width_inc > 0)
863 if (client->width_increment != size_hints.width_inc) {
864 client->width_increment = size_hints.width_inc;
867 if (size_hints.height_inc > 0)
868 if (client->height_increment != size_hints.height_inc) {
869 client->height_increment = size_hints.height_inc;
874 resize_client(conn, client);
879 int base_width = 0, base_height = 0;
881 /* base_width/height are the desired size of the window.
882 We check if either the program-specified size or the program-specified
883 min-size is available */
884 if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
885 base_width = size_hints.base_width;
886 base_height = size_hints.base_height;
887 } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
888 base_width = size_hints.min_width;
889 base_height = size_hints.min_height;
892 client->base_width = base_width;
893 client->base_height = base_height;
895 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
896 if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
897 (size_hints.min_aspect_num <= 0) ||
898 (size_hints.min_aspect_den <= 0)) {
902 double width = client->rect.width - base_width;
903 double height = client->rect.height - base_height;
904 /* Convert numerator/denominator to a double */
905 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
906 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
908 LOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
909 LOG("width = %f, height = %f\n", width, height);
911 /* Sanity checks, this is user-input, in a way */
912 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
915 /* Check if we need to set proportional_* variables using the correct ratio */
916 if ((width / height) < min_aspect) {
917 client->proportional_width = width;
918 client->proportional_height = width / min_aspect;
919 } else if ((width / height) > max_aspect) {
920 client->proportional_width = width;
921 client->proportional_height = width / max_aspect;
924 client->force_reconfigure = true;
926 if (client->container != NULL) {
927 render_container(conn, client->container);
935 * Handles the WM_HINTS property for extracting the urgency state of the window.
938 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
939 xcb_atom_t name, xcb_get_property_reply_t *reply) {
940 Client *client = table_get(&by_child, window);
941 if (client == NULL) {
942 LOG("Received WM_HINTS for unknown client\n");
945 xcb_wm_hints_t hints;
948 if (!xcb_get_wm_hints_from_reply(&hints, reply))
951 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
955 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
956 if (client == last_focused) {
957 LOG("Ignoring urgency flag for current client\n");
961 /* Update the flag on the client directly */
962 client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
964 LOG("Urgency flag changed to %d\n", client->urgent);
966 workspace_update_urgent_flag(client->workspace);
967 redecorate_window(conn, client);
969 /* If the workspace this client is on is not visible, we need to redraw
970 * the workspace bar */
971 if (!workspace_is_visible(client->workspace)) {
972 i3Screen *screen = client->workspace->screen;
973 render_workspace(conn, screen, screen->current_workspace);
981 * Handles the transient for hints set by a window, signalizing that this window is a popup window
982 * for some other window.
984 * See ICCCM 4.1.2.6 for more details
987 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
988 xcb_atom_t name, xcb_get_property_reply_t *reply) {
989 Client *client = table_get(&by_child, window);
990 if (client == NULL) {
991 LOG("No such client\n");
995 xcb_window_t transient_for;
998 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
1001 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
1002 &transient_for, NULL))
1006 if (client->floating == FLOATING_AUTO_OFF) {
1007 LOG("This is a popup window, putting into floating\n");
1008 toggle_floating_mode(conn, client, true);
1015 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
1016 * toolwindow (or similar) and to which window it belongs (logical parent).
1019 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1020 xcb_atom_t name, xcb_get_property_reply_t *prop) {
1022 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1023 false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
1028 Client *client = table_get(&by_child, window);
1032 xcb_window_t *leader = xcb_get_property_value(prop);
1033 if (leader == NULL || *leader == 0)
1036 LOG("Client leader changed to %08x\n", *leader);
1038 client->leader = *leader;