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);
108 /* We need to get the keysym group (There are group 1 to group 4, each holding
109 two keysyms (without shift and with shift) using Xkb because X fails to
110 provide them reliably (it works in Xephyr, it does not in real X) */
112 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
113 state_filtered |= BIND_MODE_SWITCH;
115 LOG("(checked mode_switch, state %d)\n", state_filtered);
117 /* Find the binding */
119 TAILQ_FOREACH(bind, bindings, bindings) {
120 /* First compare the modifiers */
121 if (bind->mods != state_filtered)
124 /* If a symbol was specified by the user, we need to look in
125 * the array of translated keycodes for the event’s keycode */
126 if (bind->symbol != NULL) {
127 if (memmem(bind->translated_to,
128 bind->number_keycodes * sizeof(xcb_keycode_t),
129 &(event->detail), sizeof(xcb_keycode_t)) != NULL)
132 /* This case is easier: The user specified a keycode */
133 if (bind->keycode == event->detail)
138 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
139 the user did not press Mode_switch, so just pass it… */
140 if (bind == TAILQ_END(bindings)) {
141 xcb_allow_events(conn, ReplayKeyboard, event->time);
146 parse_command(conn, bind->command);
147 if (state_filtered & BIND_MODE_SWITCH) {
148 LOG("Mode_switch -> allow_events(SyncKeyboard)\n");
149 xcb_allow_events(conn, SyncKeyboard, event->time);
156 * Called with coordinates of an enter_notify event or motion_notify event
157 * to check if the user crossed virtual screen boundaries and adjust the
158 * current workspace, if so.
161 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
164 if ((screen = get_screen_containing(x, y)) == NULL) {
165 LOG("ERROR: No such screen\n");
168 if (screen == c_ws->screen)
171 c_ws->current_row = current_row;
172 c_ws->current_col = current_col;
173 c_ws = screen->current_workspace;
174 current_row = c_ws->current_row;
175 current_col = c_ws->current_col;
176 LOG("We're now on virtual screen number %d\n", screen->num);
180 * When the user moves the mouse pointer onto a window, this callback gets called.
183 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
184 LOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
185 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
186 LOG("This was not a normal notify, ignoring\n");
189 /* Some events are not interesting, because they were not generated actively by the
190 user, but by reconfiguration of windows */
191 if (event_is_ignored(event->sequence))
194 /* This was either a focus for a client’s parent (= titlebar)… */
195 Client *client = table_get(&by_parent, event->event);
196 /* …or the client itself */
198 client = table_get(&by_child, event->event);
200 /* Check for stack windows */
201 if (client == NULL) {
202 struct Stack_Window *stack_win;
203 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
204 if (stack_win->window == event->event) {
205 client = stack_win->container->currently_focused;
211 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
212 if (client == NULL) {
213 LOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
214 check_crossing_screen_boundary(event->root_x, event->root_y);
218 /* Do plausibility checks: This event may be useless for us if it occurs on a window
219 which is in a stacked container but not the focused one */
220 if (client->container != NULL &&
221 client->container->mode == MODE_STACK &&
222 client->container->currently_focused != client) {
223 LOG("Plausibility check says: no\n");
227 if (client->workspace != c_ws && client->workspace->screen == c_ws->screen) {
228 /* This can happen when a client gets assigned to a different workspace than
229 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
230 * an enter_notify will follow. */
231 LOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
235 set_focus(conn, client, false);
241 * When the user moves the mouse but does not change the active window
242 * (e.g. when having no windows opened but moving mouse on the root screen
243 * and crossing virtual screen boundaries), this callback gets called.
246 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
247 /* Skip events where the pointer was over a child window, we are only
248 * interested in events on the root window. */
249 if (event->child != 0)
252 check_crossing_screen_boundary(event->root_x, event->root_y);
258 * Called when the keyboard mapping changes (for example by using Xmodmap),
259 * we need to update our key bindings then (re-translate symbols).
262 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
263 if (event->request != XCB_MAPPING_KEYBOARD &&
264 event->request != XCB_MAPPING_MODIFIER)
267 LOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
268 xcb_refresh_keyboard_mapping(keysyms, event);
270 xcb_get_numlock_mask(conn);
272 ungrab_all_keys(conn);
279 * A new window appeared on the screen (=was mapped), so let’s manage it.
282 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
283 xcb_get_window_attributes_cookie_t cookie;
285 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
287 LOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
288 add_ignore_event(event->sequence);
290 manage_window(prophs, conn, event->window, cookie, false);
295 * Configure requests are received when the application wants to resize windows on their own.
297 * We generate a synthethic configure notify event to signalize the client its "new" position.
300 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
301 LOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
302 event->window, event->x, event->y, event->width, event->height);
304 Client *client = table_get(&by_child, event->window);
305 if (client == NULL) {
309 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
310 if (event->value_mask & mask_member) { \
311 mask |= mask_member; \
312 values[c++] = event->event_member; \
316 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
317 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
318 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
319 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
320 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
321 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
322 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
324 xcb_configure_window(conn, event->window, mask, values);
330 if (client->fullscreen) {
331 LOG("Client is in fullscreen mode\n");
333 Rect child_rect = client->workspace->rect;
334 child_rect.x = child_rect.y = 0;
335 fake_configure_notify(conn, child_rect, client->child);
340 /* Floating clients can be reconfigured */
341 if (client_is_floating(client)) {
342 i3Font *font = load_font(conn, config.font);
344 if (event->value_mask & XCB_CONFIG_WINDOW_X)
345 client->rect.x = event->x;
346 if (event->value_mask & XCB_CONFIG_WINDOW_Y)
347 client->rect.y = event->y;
348 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH)
349 client->rect.width = event->width + 2 + 2;
350 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
351 client->rect.height = event->height + (font->height + 2 + 2) + 2;
353 LOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
354 client->rect.x, client->rect.y, client->rect.width, client->rect.height);
356 /* Push the new position/size to X11 */
357 reposition_client(conn, client);
358 resize_client(conn, client);
364 /* Dock clients can be reconfigured in their height */
366 LOG("Reconfiguring height of this dock client\n");
368 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
369 LOG("Ignoring configure request, no height given\n");
373 client->desired_height = event->height;
374 render_workspace(conn, c_ws->screen, c_ws);
380 if (client->fullscreen) {
381 LOG("Client is in fullscreen mode\n");
383 Rect child_rect = client->container->workspace->rect;
384 child_rect.x = child_rect.y = 0;
385 fake_configure_notify(conn, child_rect, client->child);
390 fake_absolute_configure_notify(conn, client);
396 * Configuration notifies are only handled because we need to set up ignore for the following
397 * enter notify events
400 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
401 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
403 /* We ignore this sequence twice because events for child and frame should be ignored */
404 add_ignore_event(event->sequence);
405 add_ignore_event(event->sequence);
407 if (event->event == root) {
408 LOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
409 LOG("reconfigure of the root window, need to xinerama\n");
410 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
411 but is there a better way? */
412 if (event->x == 0 && event->y == 0)
413 xinerama_requery_screens(conn);
421 * Our window decorations were unmapped. That means, the window will be killed now,
422 * so we better clean up before.
425 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
426 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
428 add_ignore_event(event->sequence);
430 Client *client = table_get(&by_child, event->window);
431 /* First, we need to check if the client is awaiting an unmap-request which
432 was generated by us reparenting the window. In that case, we just ignore it. */
433 if (client != NULL && client->awaiting_useless_unmap) {
434 client->awaiting_useless_unmap = false;
438 LOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
439 LOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
440 if (client == NULL) {
441 LOG("not a managed window. Ignoring.\n");
443 /* This was most likely the destroyed frame of a client which is
444 * currently being unmapped, so we add this sequence (again!) to
445 * the ignore list (enter_notify events will get sent for both,
446 * the child and its frame). */
447 add_ignore_event(event->sequence);
452 client = table_remove(&by_child, event->window);
454 /* If this was the fullscreen client, we need to unset it */
455 if (client->fullscreen)
456 client->workspace->fullscreen_client = NULL;
458 /* Clients without a container are either floating or dock windows */
459 if (client->container != NULL) {
460 Container *con = client->container;
462 /* Remove the client from the list of clients */
463 client_remove_from_container(conn, client, con, true);
465 /* Set focus to the last focused client in this container */
466 con->currently_focused = get_last_focused_client(conn, con, NULL);
468 /* Only if this is the active container, we need to really change focus */
469 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
470 set_focus(conn, con->currently_focused, true);
471 } else if (client_is_floating(client)) {
472 LOG("Removing from floating clients\n");
473 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
474 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
478 LOG("Removing from dock clients\n");
479 SLIST_REMOVE(&(client->workspace->screen->dock_clients), client, Client, dock_clients);
482 LOG("child of 0x%08x.\n", client->frame);
483 xcb_reparent_window(conn, client->child, root, 0, 0);
485 client_unmap(conn, client);
487 xcb_destroy_window(conn, client->frame);
489 table_remove(&by_parent, client->frame);
491 if (client->container != NULL) {
492 Workspace *workspace = client->container->workspace;
493 cleanup_table(conn, workspace);
494 fix_colrowspan(conn, workspace);
497 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
498 bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
499 bool workspace_active = false;
500 Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
502 /* If this workspace is currently active, we don’t delete it */
504 TAILQ_FOREACH(screen, virtual_screens, screens)
505 if (screen->current_workspace == client->workspace) {
506 workspace_active = true;
507 workspace_empty = false;
512 client->workspace->screen = NULL;
514 FREE(client->window_class);
520 /* Ensure the focus is set to the next client in the focus stack or to
521 * the screen itself (if we do not focus the screen, it can happen that
522 * the focus is "nowhere" and thus keypress events will not be received
523 * by i3, thus the user cannot use any hotkeys). */
524 if (workspace_active) {
525 if (to_focus != NULL)
526 set_focus(conn, to_focus, true);
528 LOG("Restoring focus to root screen\n");
529 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
538 * Called when a window changes its title
541 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
542 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
543 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
544 LOG("_NET_WM_NAME not specified, not changing\n");
547 Client *client = table_get(&by_child, window);
551 /* Save the old pointer to make the update atomic */
554 asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
555 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
556 char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
557 LOG("_NET_WM_NAME changed to \"%s\"\n", new_name);
560 /* Check if they are the same and don’t update if so.
561 Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
562 Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
563 but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
564 if ((new_len == client->name_len) &&
565 (client->name != NULL) &&
566 (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
571 char *old_name = client->name;
572 client->name = ucs2_name;
573 client->name_len = new_len;
574 client->uses_net_wm_name = true;
578 /* If the client is a dock window, we don’t need to render anything */
582 if (client->container != NULL &&
583 (client->container->mode == MODE_STACK ||
584 client->container->mode == MODE_TABBED))
585 render_container(conn, client->container);
586 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
593 * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
594 * just pass them along, so when containing non-ASCII characters, those will be rendering
595 * incorrectly. In order to correctly render unicode window titles in i3, an application
596 * has to set _NET_WM_NAME, which is in UTF-8 encoding.
598 * On every update, a message is put out to the user, so he may improve the situation and
599 * update applications which display filenames in their title to correctly use
600 * _NET_WM_NAME and therefore support unicode.
603 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
604 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
605 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
606 LOG("prop == NULL\n");
609 Client *client = table_get(&by_child, window);
613 /* Client capable of _NET_WM_NAME, ignore legacy name changes */
614 if (client->uses_net_wm_name)
617 /* Save the old pointer to make the update atomic */
619 if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
620 perror("Could not get old name");
621 LOG("Could not get old name\n");
624 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
625 LOG("WM_NAME changed to \"%s\"\n", new_name);
627 /* Check if they are the same and don’t update if so. */
628 if (client->name != NULL &&
629 strlen(new_name) == strlen(client->name) &&
630 strcmp(client->name, new_name) == 0) {
635 LOG("Using legacy window title. Note that in order to get Unicode window titles in i3, "
636 "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
638 char *old_name = client->name;
639 client->name = new_name;
640 client->name_len = -1;
642 if (old_name != NULL)
645 /* If the client is a dock window, we don’t need to render anything */
649 if (client->container != NULL &&
650 (client->container->mode == MODE_STACK ||
651 client->container->mode == MODE_TABBED))
652 render_container(conn, client->container);
653 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
660 * Updates the client’s WM_CLASS property
663 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
664 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
665 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
666 LOG("prop == NULL\n");
669 Client *client = table_get(&by_child, window);
673 if (asprintf(&new_class, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
674 perror("Could not get window class");
675 LOG("Could not get window class\n");
679 LOG("WM_CLASS changed to %s\n", new_class);
680 char *old_class = client->window_class;
681 client->window_class = new_class;
684 if (!client->initialized)
687 if (strcmp(new_class, "tools") == 0 || strcmp(new_class, "Dialog") == 0) {
688 LOG("tool/dialog window, should we put it floating?\n");
689 if (client->floating == FLOATING_AUTO_OFF)
690 toggle_floating_mode(conn, client, true);
697 * Expose event means we should redraw our windows (= title bar)
700 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
701 /* event->count is the number of minimum remaining expose events for this window, so we
702 skip all events but the last one */
703 if (event->count != 0)
705 LOG("window = %08x\n", event->window);
707 Client *client = table_get(&by_parent, event->window);
708 if (client == NULL) {
709 /* There was no client in the table, so this is probably an expose event for
710 one of our stack_windows. */
711 struct Stack_Window *stack_win;
712 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
713 if (stack_win->window == event->window) {
714 render_container(conn, stack_win->container);
718 /* …or one of the bars? */
720 TAILQ_FOREACH(screen, virtual_screens, screens)
721 if (screen->bar == event->window)
729 if (client->container == NULL ||
730 (client->container->mode != MODE_STACK &&
731 client->container->mode != MODE_TABBED))
732 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
734 uint32_t background_color;
736 background_color = config.client.urgent.background;
737 /* Distinguish if the window is currently focused… */
738 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
739 background_color = config.client.focused.background;
740 /* …or if it is the focused window in a not focused container */
741 else background_color = config.client.focused_inactive.background;
743 /* Set foreground color to current focused color, line width to 2 */
744 uint32_t values[] = {background_color, 2};
745 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
747 /* Draw the border, the ±1 is for line width = 2 */
748 xcb_point_t points[] = {{1, 0}, /* left upper edge */
749 {1, client->rect.height-1}, /* left bottom edge */
750 {client->rect.width-1, client->rect.height-1}, /* right bottom edge */
751 {client->rect.width-1, 0}}; /* right upper edge */
752 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
754 /* Draw a black background */
755 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
756 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
757 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
764 * Handle client messages (EWMH)
767 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
768 if (event->type == atoms[_NET_WM_STATE]) {
769 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
772 Client *client = table_get(&by_child, event->window);
776 /* Check if the fullscreen state should be toggled */
777 if ((client->fullscreen &&
778 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
779 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
780 (!client->fullscreen &&
781 (event->data.data32[0] == _NET_WM_STATE_ADD ||
782 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
783 client_toggle_fullscreen(conn, client);
785 LOG("unhandled clientmessage\n");
792 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
793 xcb_atom_t atom, xcb_get_property_reply_t *property) {
794 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
795 before changing this property. */
796 LOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
801 * Handles the size hints set by a window, but currently only the part necessary for displaying
802 * clients proportionally inside their frames (mplayer for example)
804 * See ICCCM 4.1.2.3 for more details
807 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
808 xcb_atom_t name, xcb_get_property_reply_t *reply) {
809 Client *client = table_get(&by_child, window);
810 if (client == NULL) {
811 LOG("Received WM_SIZE_HINTS for unknown client\n");
814 xcb_size_hints_t size_hints;
818 /* If the hints were already in this event, use them, if not, request them */
820 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
822 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
824 if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
825 // TODO: Minimum size is not yet implemented
826 //LOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
829 if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
830 bool changed = false;
832 if (size_hints.width_inc > 0)
833 if (client->width_increment != size_hints.width_inc) {
834 client->width_increment = size_hints.width_inc;
837 if (size_hints.height_inc > 0)
838 if (client->height_increment != size_hints.height_inc) {
839 client->height_increment = size_hints.height_inc;
844 resize_client(conn, client);
849 int base_width = 0, base_height = 0;
851 /* base_width/height are the desired size of the window.
852 We check if either the program-specified size or the program-specified
853 min-size is available */
854 if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
855 base_width = size_hints.base_width;
856 base_height = size_hints.base_height;
857 } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
858 base_width = size_hints.min_width;
859 base_height = size_hints.min_height;
862 client->base_width = base_width;
863 client->base_height = base_height;
865 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
866 if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
867 (size_hints.min_aspect_num <= 0) ||
868 (size_hints.min_aspect_den <= 0)) {
872 double width = client->rect.width - base_width;
873 double height = client->rect.height - base_height;
874 /* Convert numerator/denominator to a double */
875 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
876 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
878 LOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
879 LOG("width = %f, height = %f\n", width, height);
881 /* Sanity checks, this is user-input, in a way */
882 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
885 /* Check if we need to set proportional_* variables using the correct ratio */
886 if ((width / height) < min_aspect) {
887 client->proportional_width = width;
888 client->proportional_height = width / min_aspect;
889 } else if ((width / height) > max_aspect) {
890 client->proportional_width = width;
891 client->proportional_height = width / max_aspect;
894 client->force_reconfigure = true;
896 if (client->container != NULL) {
897 render_container(conn, client->container);
905 * Handles the WM_HINTS property for extracting the urgency state of the window.
908 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
909 xcb_atom_t name, xcb_get_property_reply_t *reply) {
910 Client *client = table_get(&by_child, window);
911 if (client == NULL) {
912 LOG("Received WM_HINTS for unknown client\n");
915 xcb_wm_hints_t hints;
918 if (!xcb_get_wm_hints_from_reply(&hints, reply))
921 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
925 /* Update the flag on the client directly */
926 client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
928 LOG("Urgency flag changed to %d\n", client->urgent);
930 workspace_update_urgent_flag(client->workspace);
931 redecorate_window(conn, client);
933 /* If the workspace this client is on is not visible, we need to redraw
934 * the workspace bar */
935 if (!workspace_is_visible(client->workspace)) {
936 i3Screen *screen = client->workspace->screen;
937 render_workspace(conn, screen, screen->current_workspace);
945 * Handles the transient for hints set by a window, signalizing that this window is a popup window
946 * for some other window.
948 * See ICCCM 4.1.2.6 for more details
951 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
952 xcb_atom_t name, xcb_get_property_reply_t *reply) {
953 Client *client = table_get(&by_child, window);
954 if (client == NULL) {
955 LOG("No such client\n");
959 xcb_window_t transient_for;
962 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
965 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
966 &transient_for, NULL))
970 if (client->floating == FLOATING_AUTO_OFF) {
971 LOG("This is a popup window, putting into floating\n");
972 toggle_floating_mode(conn, client, true);
979 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
980 * toolwindow (or similar) and to which window it belongs (logical parent).
983 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
984 xcb_atom_t name, xcb_get_property_reply_t *prop) {
986 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
987 false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
992 Client *client = table_get(&by_child, window);
996 xcb_window_t *leader = xcb_get_property_value(prop);
997 if (leader == NULL || *leader == 0)
1000 LOG("Client leader changed to %08x\n", *leader);
1002 client->leader = *leader;