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 #include "container.h"
42 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
43 since it’d trigger an infinite loop of switching between the different windows when
44 changing workspaces */
45 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
47 static void add_ignore_event(const int sequence) {
48 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
50 event->sequence = sequence;
51 event->added = time(NULL);
53 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
57 * Checks if the given sequence is ignored and returns true if so.
60 static bool event_is_ignored(const int sequence) {
61 struct Ignore_Event *event;
62 time_t now = time(NULL);
63 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
64 if ((now - event->added) > 5) {
65 struct Ignore_Event *save = event;
66 event = SLIST_NEXT(event, ignore_events);
67 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
69 } else event = SLIST_NEXT(event, ignore_events);
72 SLIST_FOREACH(event, &ignore_events, ignore_events) {
73 if (event->sequence == sequence) {
74 SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
84 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
85 * Therefore, we just replay all key presses.
88 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
89 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
95 * There was a key press. We compare this key code with our bindings table and pass
96 * the bound action to parse_command().
99 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
100 DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
102 /* Remove the numlock bit, all other bits are modifiers we can bind to */
103 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
104 DLOG("(removed numlock, state = %d)\n", state_filtered);
105 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
106 * button masks are filtered out */
107 state_filtered &= 0xFF;
108 DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
111 /* We need to get the keysym group (There are group 1 to group 4, each holding
112 two keysyms (without shift and with shift) using Xkb because X fails to
113 provide them reliably (it works in Xephyr, it does not in real X) */
115 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
116 state_filtered |= BIND_MODE_SWITCH;
119 DLOG("(checked mode_switch, state %d)\n", state_filtered);
121 /* Find the binding */
123 TAILQ_FOREACH(bind, bindings, bindings) {
124 /* First compare the modifiers */
125 if (bind->mods != state_filtered)
128 /* If a symbol was specified by the user, we need to look in
129 * the array of translated keycodes for the event’s keycode */
130 if (bind->symbol != NULL) {
131 if (memmem(bind->translated_to,
132 bind->number_keycodes * sizeof(xcb_keycode_t),
133 &(event->detail), sizeof(xcb_keycode_t)) != NULL)
136 /* This case is easier: The user specified a keycode */
137 if (bind->keycode == event->detail)
142 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
143 the user did not press Mode_switch, so just pass it… */
144 if (bind == TAILQ_END(bindings)) {
145 xcb_allow_events(conn, ReplayKeyboard, event->time);
150 parse_command(conn, bind->command);
151 if (state_filtered & BIND_MODE_SWITCH) {
152 DLOG("Mode_switch -> allow_events(SyncKeyboard)\n");
153 xcb_allow_events(conn, SyncKeyboard, event->time);
160 * Called with coordinates of an enter_notify event or motion_notify event
161 * to check if the user crossed virtual screen boundaries and adjust the
162 * current workspace, if so.
165 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
168 if ((screen = get_screen_containing(x, y)) == NULL) {
169 ELOG("ERROR: No such screen\n");
172 if (screen == c_ws->screen)
175 c_ws->current_row = current_row;
176 c_ws->current_col = current_col;
177 c_ws = screen->current_workspace;
178 current_row = c_ws->current_row;
179 current_col = c_ws->current_col;
180 DLOG("We're now on virtual screen number %d\n", screen->num);
184 * When the user moves the mouse pointer onto a window, this callback gets called.
187 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
188 DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
189 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
190 DLOG("This was not a normal notify, ignoring\n");
193 /* Some events are not interesting, because they were not generated actively by the
194 user, but by reconfiguration of windows */
195 if (event_is_ignored(event->sequence))
198 /* This was either a focus for a client’s parent (= titlebar)… */
199 Client *client = table_get(&by_parent, event->event);
200 /* …or the client itself */
202 client = table_get(&by_child, event->event);
204 /* Check for stack windows */
205 if (client == NULL) {
206 struct Stack_Window *stack_win;
207 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
208 if (stack_win->window == event->event) {
209 client = stack_win->container->currently_focused;
215 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
216 if (client == NULL) {
217 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
218 check_crossing_screen_boundary(event->root_x, event->root_y);
222 /* Do plausibility checks: This event may be useless for us if it occurs on a window
223 which is in a stacked container but not the focused one */
224 if (client->container != NULL &&
225 client->container->mode == MODE_STACK &&
226 client->container->currently_focused != client) {
227 DLOG("Plausibility check says: no\n");
231 if (client->workspace != c_ws && client->workspace->screen == c_ws->screen) {
232 /* This can happen when a client gets assigned to a different workspace than
233 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
234 * an enter_notify will follow. */
235 DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
239 if (!config.disable_focus_follows_mouse)
240 set_focus(conn, client, false);
246 * When the user moves the mouse but does not change the active window
247 * (e.g. when having no windows opened but moving mouse on the root screen
248 * and crossing virtual screen boundaries), this callback gets called.
251 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
252 /* Skip events where the pointer was over a child window, we are only
253 * interested in events on the root window. */
254 if (event->child != 0)
257 check_crossing_screen_boundary(event->root_x, event->root_y);
263 * Called when the keyboard mapping changes (for example by using Xmodmap),
264 * we need to update our key bindings then (re-translate symbols).
267 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
268 if (event->request != XCB_MAPPING_KEYBOARD &&
269 event->request != XCB_MAPPING_MODIFIER)
272 DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
273 xcb_refresh_keyboard_mapping(keysyms, event);
275 xcb_get_numlock_mask(conn);
277 ungrab_all_keys(conn);
284 * A new window appeared on the screen (=was mapped), so let’s manage it.
287 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
288 xcb_get_window_attributes_cookie_t cookie;
290 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
292 DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
293 add_ignore_event(event->sequence);
295 manage_window(prophs, conn, event->window, cookie, false);
300 * Configure requests are received when the application wants to resize windows on their own.
302 * We generate a synthethic configure notify event to signalize the client its "new" position.
305 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
306 DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
307 event->window, event->x, event->y, event->width, event->height);
309 Client *client = table_get(&by_child, event->window);
310 if (client == NULL) {
314 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
315 if (event->value_mask & mask_member) { \
316 mask |= mask_member; \
317 values[c++] = event->event_member; \
321 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
322 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
323 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
324 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
325 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
326 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
327 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
329 xcb_configure_window(conn, event->window, mask, values);
335 if (client->fullscreen) {
336 DLOG("Client is in fullscreen mode\n");
338 Rect child_rect = client->workspace->rect;
339 child_rect.x = child_rect.y = 0;
340 fake_configure_notify(conn, child_rect, client->child);
345 /* Floating clients can be reconfigured */
346 if (client_is_floating(client)) {
347 i3Font *font = load_font(conn, config.font);
348 int mode = (client->container != NULL ? client->container->mode : MODE_DEFAULT);
350 if (event->value_mask & XCB_CONFIG_WINDOW_X)
351 client->rect.x = event->x;
352 if (event->value_mask & XCB_CONFIG_WINDOW_Y)
353 client->rect.y = event->y;
354 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
355 if (mode == MODE_STACK || mode == MODE_TABBED) {
356 client->rect.width = event->width + 2 + 2;
358 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
359 client->rect.width = event->width;
360 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
361 client->rect.width = event->width + (1 + 1);
362 else client->rect.width = event->width + (2 + 2);
365 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
366 if (mode == MODE_STACK || mode == MODE_TABBED) {
367 client->rect.height = event->height + 2;
369 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
370 client->rect.height = event->height;
371 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
372 client->rect.height = event->height + (1 + 1);
373 else client->rect.height = event->height + (font->height + 2 + 2) + 2;
377 DLOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
378 client->rect.x, client->rect.y, client->rect.width, client->rect.height);
380 /* Push the new position/size to X11 */
381 reposition_client(conn, client);
382 resize_client(conn, client);
388 /* Dock clients can be reconfigured in their height */
390 DLOG("Reconfiguring height of this dock client\n");
392 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
393 DLOG("Ignoring configure request, no height given\n");
397 client->desired_height = event->height;
398 render_workspace(conn, c_ws->screen, c_ws);
404 if (client->fullscreen) {
405 DLOG("Client is in fullscreen mode\n");
407 Rect child_rect = client->container->workspace->rect;
408 child_rect.x = child_rect.y = 0;
409 fake_configure_notify(conn, child_rect, client->child);
414 fake_absolute_configure_notify(conn, client);
420 * Configuration notifies are only handled because we need to set up ignore for the following
421 * enter notify events
424 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
425 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
427 /* We ignore this sequence twice because events for child and frame should be ignored */
428 add_ignore_event(event->sequence);
429 add_ignore_event(event->sequence);
431 if (event->event == root) {
432 DLOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
433 DLOG("reconfigure of the root window, need to xinerama\n");
434 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
435 but is there a better way? */
436 if (event->x == 0 && event->y == 0)
437 xinerama_requery_screens(conn);
445 * Our window decorations were unmapped. That means, the window will be killed now,
446 * so we better clean up before.
449 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
450 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
452 add_ignore_event(event->sequence);
454 Client *client = table_get(&by_child, event->window);
455 /* First, we need to check if the client is awaiting an unmap-request which
456 was generated by us reparenting the window. In that case, we just ignore it. */
457 if (client != NULL && client->awaiting_useless_unmap) {
458 client->awaiting_useless_unmap = false;
462 DLOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
463 DLOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
464 if (client == NULL) {
465 DLOG("not a managed window. Ignoring.\n");
467 /* This was most likely the destroyed frame of a client which is
468 * currently being unmapped, so we add this sequence (again!) to
469 * the ignore list (enter_notify events will get sent for both,
470 * the child and its frame). */
471 add_ignore_event(event->sequence);
476 client = table_remove(&by_child, event->window);
478 /* If this was the fullscreen client, we need to unset it */
479 if (client->fullscreen)
480 client->workspace->fullscreen_client = NULL;
482 /* Clients without a container are either floating or dock windows */
483 if (client->container != NULL) {
484 Container *con = client->container;
486 /* Remove the client from the list of clients */
487 client_remove_from_container(conn, client, con, true);
489 /* Set focus to the last focused client in this container */
490 con->currently_focused = get_last_focused_client(conn, con, NULL);
492 /* Only if this is the active container, we need to really change focus */
493 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
494 set_focus(conn, con->currently_focused, true);
495 } else if (client_is_floating(client)) {
496 DLOG("Removing from floating clients\n");
497 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
498 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
502 DLOG("Removing from dock clients\n");
503 SLIST_REMOVE(&(client->workspace->screen->dock_clients), client, Client, dock_clients);
506 DLOG("child of 0x%08x.\n", client->frame);
507 xcb_reparent_window(conn, client->child, root, 0, 0);
509 client_unmap(conn, client);
511 xcb_destroy_window(conn, client->frame);
513 table_remove(&by_parent, client->frame);
515 if (client->container != NULL) {
516 Workspace *workspace = client->container->workspace;
517 cleanup_table(conn, workspace);
518 fix_colrowspan(conn, workspace);
521 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
522 bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
523 bool workspace_active = false;
524 Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
526 /* If this workspace is currently active, we don’t delete it */
528 TAILQ_FOREACH(screen, virtual_screens, screens)
529 if (screen->current_workspace == client->workspace) {
530 workspace_active = true;
531 workspace_empty = false;
536 client->workspace->screen = NULL;
538 /* Remove the urgency flag if set */
539 client->urgent = false;
540 workspace_update_urgent_flag(client->workspace);
542 FREE(client->window_class_instance);
543 FREE(client->window_class_class);
549 /* Ensure the focus is set to the next client in the focus stack or to
550 * the screen itself (if we do not focus the screen, it can happen that
551 * the focus is "nowhere" and thus keypress events will not be received
552 * by i3, thus the user cannot use any hotkeys). */
553 if (workspace_active) {
554 if (to_focus != NULL)
555 set_focus(conn, to_focus, true);
557 DLOG("Restoring focus to root screen\n");
558 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
567 * Called when a window changes its title
570 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
571 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
572 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
573 DLOG("_NET_WM_NAME not specified, not changing\n");
576 Client *client = table_get(&by_child, window);
580 /* Save the old pointer to make the update atomic */
583 asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
584 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
585 char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
586 LOG("_NET_WM_NAME changed to \"%s\"\n", new_name);
589 /* Check if they are the same and don’t update if so.
590 Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
591 Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
592 but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
593 if ((new_len == client->name_len) &&
594 (client->name != NULL) &&
595 (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
600 char *old_name = client->name;
601 client->name = ucs2_name;
602 client->name_len = new_len;
603 client->uses_net_wm_name = true;
607 /* If the client is a dock window, we don’t need to render anything */
611 int mode = container_mode(client->container, true);
612 if (mode == MODE_STACK || mode == MODE_TABBED)
613 render_container(conn, client->container);
614 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
621 * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
622 * just pass them along, so when containing non-ASCII characters, those will be rendering
623 * incorrectly. In order to correctly render unicode window titles in i3, an application
624 * has to set _NET_WM_NAME, which is in UTF-8 encoding.
626 * On every update, a message is put out to the user, so he may improve the situation and
627 * update applications which display filenames in their title to correctly use
628 * _NET_WM_NAME and therefore support unicode.
631 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
632 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
633 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
634 DLOG("prop == NULL\n");
637 Client *client = table_get(&by_child, window);
641 /* Client capable of _NET_WM_NAME, ignore legacy name changes */
642 if (client->uses_net_wm_name)
645 /* Save the old pointer to make the update atomic */
647 if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
648 perror("Could not get old name");
649 DLOG("Could not get old name\n");
652 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
653 LOG("WM_NAME changed to \"%s\"\n", new_name);
655 /* Check if they are the same and don’t update if so. */
656 if (client->name != NULL &&
657 strlen(new_name) == strlen(client->name) &&
658 strcmp(client->name, new_name) == 0) {
663 LOG("Using legacy window title. Note that in order to get Unicode window titles in i3, "
664 "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
666 char *old_name = client->name;
667 client->name = new_name;
668 client->name_len = -1;
670 if (old_name != NULL)
673 /* If the client is a dock window, we don’t need to render anything */
677 if (client->container != NULL &&
678 (client->container->mode == MODE_STACK ||
679 client->container->mode == MODE_TABBED))
680 render_container(conn, client->container);
681 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
688 * Updates the client’s WM_CLASS property
691 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
692 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
693 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
694 DLOG("prop == NULL\n");
697 Client *client = table_get(&by_child, window);
701 /* We cannot use asprintf here since this property contains two
702 * null-terminated strings (for compatibility reasons). Instead, we
703 * use strdup() on both strings */
704 char *new_class = xcb_get_property_value(prop);
706 FREE(client->window_class_instance);
707 FREE(client->window_class_class);
709 client->window_class_instance = strdup(new_class);
710 if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop))
711 client->window_class_class = strdup(new_class + strlen(new_class) + 1);
712 else client->window_class_class = NULL;
713 LOG("WM_CLASS changed to %s (instance), %s (class)\n",
714 client->window_class_instance, client->window_class_class);
720 * Expose event means we should redraw our windows (= title bar)
723 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
724 /* event->count is the number of minimum remaining expose events for this window, so we
725 skip all events but the last one */
726 if (event->count != 0)
728 DLOG("window = %08x\n", event->window);
730 Client *client = table_get(&by_parent, event->window);
731 if (client == NULL) {
732 /* There was no client in the table, so this is probably an expose event for
733 one of our stack_windows. */
734 struct Stack_Window *stack_win;
735 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
736 if (stack_win->window == event->window) {
737 render_container(conn, stack_win->container);
741 /* …or one of the bars? */
743 TAILQ_FOREACH(screen, virtual_screens, screens)
744 if (screen->bar == event->window)
752 if (container_mode(client->container, true) == MODE_DEFAULT)
753 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
755 uint32_t background_color;
757 background_color = config.client.urgent.background;
758 /* Distinguish if the window is currently focused… */
759 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
760 background_color = config.client.focused.background;
761 /* …or if it is the focused window in a not focused container */
762 else background_color = config.client.focused_inactive.background;
764 /* Set foreground color to current focused color, line width to 2 */
765 uint32_t values[] = {background_color, 2};
766 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
768 /* Draw the border, the ±1 is for line width = 2 */
769 xcb_point_t points[] = {{1, 0}, /* left upper edge */
770 {1, client->rect.height-1}, /* left bottom edge */
771 {client->rect.width-1, client->rect.height-1}, /* right bottom edge */
772 {client->rect.width-1, 0}}; /* right upper edge */
773 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
775 /* Draw a black background */
776 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
777 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless) {
778 xcb_rectangle_t crect = {1, 0, client->rect.width - (1 + 1), client->rect.height - 1};
779 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
781 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
782 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
790 * Handle client messages (EWMH)
793 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
794 if (event->type == atoms[_NET_WM_STATE]) {
795 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
798 Client *client = table_get(&by_child, event->window);
802 /* Check if the fullscreen state should be toggled */
803 if ((client->fullscreen &&
804 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
805 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
806 (!client->fullscreen &&
807 (event->data.data32[0] == _NET_WM_STATE_ADD ||
808 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
809 client_toggle_fullscreen(conn, client);
811 ELOG("unhandled clientmessage\n");
818 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
819 xcb_atom_t atom, xcb_get_property_reply_t *property) {
820 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
821 before changing this property. */
822 ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
827 * Handles the size hints set by a window, but currently only the part necessary for displaying
828 * clients proportionally inside their frames (mplayer for example)
830 * See ICCCM 4.1.2.3 for more details
833 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
834 xcb_atom_t name, xcb_get_property_reply_t *reply) {
835 Client *client = table_get(&by_child, window);
836 if (client == NULL) {
837 DLOG("Received WM_SIZE_HINTS for unknown client\n");
840 xcb_size_hints_t size_hints;
844 /* If the hints were already in this event, use them, if not, request them */
846 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
848 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
850 if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
851 // TODO: Minimum size is not yet implemented
852 //LOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
855 if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
856 bool changed = false;
858 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
859 if (client->width_increment != size_hints.width_inc) {
860 client->width_increment = size_hints.width_inc;
863 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
864 if (client->height_increment != size_hints.height_inc) {
865 client->height_increment = size_hints.height_inc;
870 resize_client(conn, client);
875 int base_width = 0, base_height = 0;
877 /* base_width/height are the desired size of the window.
878 We check if either the program-specified size or the program-specified
879 min-size is available */
880 if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
881 base_width = size_hints.base_width;
882 base_height = size_hints.base_height;
883 } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
884 base_width = size_hints.min_width;
885 base_height = size_hints.min_height;
888 if (base_width != client->base_width ||
889 base_height != client->base_height) {
890 client->base_width = base_width;
891 client->base_height = base_height;
892 DLOG("client's base_height changed to %d\n", base_height);
893 if (client->fullscreen)
894 DLOG("Not resizing client, it is in fullscreen mode\n");
896 resize_client(conn, client);
899 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
900 if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
901 (size_hints.min_aspect_num <= 0) ||
902 (size_hints.min_aspect_den <= 0)) {
906 double width = client->rect.width - base_width;
907 double height = client->rect.height - base_height;
908 /* Convert numerator/denominator to a double */
909 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
910 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
912 DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
913 DLOG("width = %f, height = %f\n", width, height);
915 /* Sanity checks, this is user-input, in a way */
916 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
919 /* Check if we need to set proportional_* variables using the correct ratio */
920 if ((width / height) < min_aspect) {
921 client->proportional_width = width;
922 client->proportional_height = width / min_aspect;
923 } else if ((width / height) > max_aspect) {
924 client->proportional_width = width;
925 client->proportional_height = width / max_aspect;
928 client->force_reconfigure = true;
930 if (client->container != NULL) {
931 render_container(conn, client->container);
939 * Handles the WM_HINTS property for extracting the urgency state of the window.
942 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
943 xcb_atom_t name, xcb_get_property_reply_t *reply) {
944 Client *client = table_get(&by_child, window);
945 if (client == NULL) {
946 DLOG("Received WM_HINTS for unknown client\n");
949 xcb_wm_hints_t hints;
952 if (!xcb_get_wm_hints_from_reply(&hints, reply))
955 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
959 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
960 if (!client->urgent && client == last_focused) {
961 DLOG("Ignoring urgency flag for current client\n");
965 /* Update the flag on the client directly */
966 client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
968 LOG("Urgency flag changed to %d\n", client->urgent);
970 workspace_update_urgent_flag(client->workspace);
971 redecorate_window(conn, client);
973 /* If the workspace this client is on is not visible, we need to redraw
974 * the workspace bar */
975 if (!workspace_is_visible(client->workspace)) {
976 i3Screen *screen = client->workspace->screen;
977 render_workspace(conn, screen, screen->current_workspace);
985 * Handles the transient for hints set by a window, signalizing that this window is a popup window
986 * for some other window.
988 * See ICCCM 4.1.2.6 for more details
991 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
992 xcb_atom_t name, xcb_get_property_reply_t *reply) {
993 Client *client = table_get(&by_child, window);
994 if (client == NULL) {
995 DLOG("No such client\n");
999 xcb_window_t transient_for;
1001 if (reply != NULL) {
1002 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
1005 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
1006 &transient_for, NULL))
1010 if (client->floating == FLOATING_AUTO_OFF) {
1011 DLOG("This is a popup window, putting into floating\n");
1012 toggle_floating_mode(conn, client, true);
1019 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
1020 * toolwindow (or similar) and to which window it belongs (logical parent).
1023 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1024 xcb_atom_t name, xcb_get_property_reply_t *prop) {
1026 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1027 false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
1032 Client *client = table_get(&by_child, window);
1036 xcb_window_t *leader = xcb_get_property_value(prop);
1037 if (leader == NULL || *leader == 0)
1040 DLOG("Client leader changed to %08x\n", *leader);
1042 client->leader = *leader;