4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
18 #include <xcb/xcb_atom.h>
19 #include <xcb/xcb_icccm.h>
20 #include <xcb/randr.h>
22 #include <X11/XKBlib.h>
39 #include "workspace.h"
41 #include "container.h"
44 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
45 since it’d trigger an infinite loop of switching between the different windows when
46 changing workspaces */
47 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
49 static void add_ignore_event(const int sequence) {
50 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
52 event->sequence = sequence;
53 event->added = time(NULL);
55 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
59 * Checks if the given sequence is ignored and returns true if so.
62 static bool event_is_ignored(const int sequence) {
63 struct Ignore_Event *event;
64 time_t now = time(NULL);
65 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
66 if ((now - event->added) > 5) {
67 struct Ignore_Event *save = event;
68 event = SLIST_NEXT(event, ignore_events);
69 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
71 } else event = SLIST_NEXT(event, ignore_events);
74 SLIST_FOREACH(event, &ignore_events, ignore_events) {
75 if (event->sequence == sequence) {
76 SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
86 * There was a key press. We compare this key code with our bindings table and pass
87 * the bound action to parse_command().
90 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
91 DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
93 /* Remove the numlock bit, all other bits are modifiers we can bind to */
94 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
95 DLOG("(removed numlock, state = %d)\n", state_filtered);
96 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
97 * button masks are filtered out */
98 state_filtered &= 0xFF;
99 DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
101 if (xkb_current_group == XkbGroup2Index)
102 state_filtered |= BIND_MODE_SWITCH;
104 DLOG("(checked mode_switch, state %d)\n", state_filtered);
106 /* Find the binding */
107 Binding *bind = get_binding(state_filtered, event->detail);
109 /* No match? Then the user has Mode_switch enabled but does not have a
110 * specific keybinding. Fall back to the default keybindings (without
111 * Mode_switch). Makes it much more convenient for users of a hybrid
112 * layout (like us, ru). */
114 state_filtered &= ~(BIND_MODE_SWITCH);
115 DLOG("no match, new state_filtered = %d\n", state_filtered);
116 if ((bind = get_binding(state_filtered, event->detail)) == NULL) {
117 ELOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
118 state_filtered, event->detail);
123 parse_command(conn, bind->command);
128 * Called with coordinates of an enter_notify event or motion_notify event
129 * to check if the user crossed virtual screen boundaries and adjust the
130 * current workspace, if so.
133 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
136 if ((output = get_output_containing(x, y)) == NULL) {
137 ELOG("ERROR: No such screen\n");
140 if (output == c_ws->output)
143 c_ws->current_row = current_row;
144 c_ws->current_col = current_col;
145 c_ws = output->current_workspace;
146 current_row = c_ws->current_row;
147 current_col = c_ws->current_col;
148 DLOG("We're now on output %p\n", output);
150 /* While usually this function is only called when the user switches
151 * to a different output using his mouse (and thus the output is
152 * empty), it may be that the following race condition occurs:
153 * 1) the user actives a new output (say VGA1).
154 * 2) the cursor is sent to the first pixel of the new VGA1, thus
155 * generating an enter_notify for the screen (the enter_notify
156 * is not yet received by i3).
157 * 3) i3 requeries screen configuration and maps a workspace onto the
159 * 4) the enter_notify event arrives and c_ws is set to the new
160 * workspace but the existing windows on the new workspace are not
163 * Therefore, we re-set the focus here to be sure it’s correct. */
164 Client *first_client = SLIST_FIRST(&(c_ws->focus_stack));
165 if (first_client != NULL)
166 set_focus(global_conn, first_client, true);
170 * When the user moves the mouse pointer onto a window, this callback gets called.
173 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
174 DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
175 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
176 DLOG("This was not a normal notify, ignoring\n");
179 /* Some events are not interesting, because they were not generated actively by the
180 user, but by reconfiguration of windows */
181 if (event_is_ignored(event->sequence))
184 /* This was either a focus for a client’s parent (= titlebar)… */
185 Client *client = table_get(&by_parent, event->event);
186 /* …or the client itself */
188 client = table_get(&by_child, event->event);
190 /* Check for stack windows */
191 if (client == NULL) {
192 struct Stack_Window *stack_win;
193 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
194 if (stack_win->window == event->event) {
195 client = stack_win->container->currently_focused;
201 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
202 if (client == NULL) {
203 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
204 check_crossing_screen_boundary(event->root_x, event->root_y);
208 /* Do plausibility checks: This event may be useless for us if it occurs on a window
209 which is in a stacked container but not the focused one */
210 if (client->container != NULL &&
211 client->container->mode == MODE_STACK &&
212 client->container->currently_focused != client) {
213 DLOG("Plausibility check says: no\n");
217 if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
218 /* This can happen when a client gets assigned to a different workspace than
219 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
220 * an enter_notify will follow. */
221 DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
225 if (!config.disable_focus_follows_mouse)
226 set_focus(conn, client, false);
232 * When the user moves the mouse but does not change the active window
233 * (e.g. when having no windows opened but moving mouse on the root screen
234 * and crossing virtual screen boundaries), this callback gets called.
237 int handle_motion_notify(void *ignored, xcb_connection_t *conn, xcb_motion_notify_event_t *event) {
238 /* Skip events where the pointer was over a child window, we are only
239 * interested in events on the root window. */
240 if (event->child != 0)
243 check_crossing_screen_boundary(event->root_x, event->root_y);
249 * Called when the keyboard mapping changes (for example by using Xmodmap),
250 * we need to update our key bindings then (re-translate symbols).
253 int handle_mapping_notify(void *ignored, xcb_connection_t *conn, xcb_mapping_notify_event_t *event) {
254 if (event->request != XCB_MAPPING_KEYBOARD &&
255 event->request != XCB_MAPPING_MODIFIER)
258 DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
259 xcb_refresh_keyboard_mapping(keysyms, event);
261 xcb_get_numlock_mask(conn);
263 ungrab_all_keys(conn);
265 grab_all_keys(conn, false);
271 * A new window appeared on the screen (=was mapped), so let’s manage it.
274 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
275 xcb_get_window_attributes_cookie_t cookie;
277 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
279 DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
280 add_ignore_event(event->sequence);
282 manage_window(prophs, conn, event->window, cookie, false);
287 * Configure requests are received when the application wants to resize windows on their own.
289 * We generate a synthethic configure notify event to signalize the client its "new" position.
292 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
293 DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
294 event->window, event->x, event->y, event->width, event->height);
296 Client *client = table_get(&by_child, event->window);
297 if (client == NULL) {
301 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
302 if (event->value_mask & mask_member) { \
303 mask |= mask_member; \
304 values[c++] = event->event_member; \
308 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
309 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
310 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
311 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
312 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
313 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
314 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
316 xcb_configure_window(conn, event->window, mask, values);
322 if (client->fullscreen) {
323 DLOG("Client is in fullscreen mode\n");
325 Rect child_rect = client->workspace->rect;
326 child_rect.x = child_rect.y = 0;
327 fake_configure_notify(conn, child_rect, client->child);
332 /* Floating clients can be reconfigured */
333 if (client_is_floating(client)) {
334 i3Font *font = load_font(conn, config.font);
335 int mode = (client->container != NULL ? client->container->mode : MODE_DEFAULT);
336 /* TODO: refactor this code. we need a function to translate
337 * coordinates of child_rect/rect. */
339 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
340 if (mode == MODE_STACK || mode == MODE_TABBED) {
341 client->rect.x = event->x - 2;
343 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
344 client->rect.x = event->x;
345 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
346 client->rect.x = event->x - 1;
347 else client->rect.x = event->x - 2;
350 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
351 if (mode == MODE_STACK || mode == MODE_TABBED) {
352 client->rect.y = event->y - 2;
354 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
355 client->rect.y = event->y;
356 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
357 client->rect.y = event->y - 1;
358 else client->rect.y = event->y - font->height - 2 - 2;
361 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
362 if (mode == MODE_STACK || mode == MODE_TABBED) {
363 client->rect.width = event->width + 2 + 2;
365 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
366 client->rect.width = event->width;
367 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
368 client->rect.width = event->width + (1 + 1);
369 else client->rect.width = event->width + (2 + 2);
372 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
373 if (mode == MODE_STACK || mode == MODE_TABBED) {
374 client->rect.height = event->height + 2;
376 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
377 client->rect.height = event->height;
378 else if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
379 client->rect.height = event->height + (1 + 1);
380 else client->rect.height = event->height + (font->height + 2 + 2) + 2;
384 DLOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
385 client->rect.x, client->rect.y, client->rect.width, client->rect.height);
387 /* Push the new position/size to X11 */
388 reposition_client(conn, client);
389 resize_client(conn, client);
395 /* Dock clients can be reconfigured in their height */
397 DLOG("Reconfiguring height of this dock client\n");
399 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
400 DLOG("Ignoring configure request, no height given\n");
404 client->desired_height = event->height;
405 render_workspace(conn, c_ws->output, c_ws);
411 if (client->fullscreen) {
412 DLOG("Client is in fullscreen mode\n");
414 Rect child_rect = client->container->workspace->rect;
415 child_rect.x = child_rect.y = 0;
416 fake_configure_notify(conn, child_rect, client->child);
421 fake_absolute_configure_notify(conn, client);
427 * Configuration notifies are only handled because we need to set up ignore for
428 * the following enter notify events.
431 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
432 /* We ignore this sequence twice because events for child and frame should be ignored */
433 add_ignore_event(event->sequence);
434 add_ignore_event(event->sequence);
440 * Gets triggered upon a RandR screen change event, that is when the user
441 * changes the screen configuration in any way (mode, position, …)
444 int handle_screen_change(void *prophs, xcb_connection_t *conn,
445 xcb_generic_event_t *e) {
446 DLOG("RandR screen change\n");
448 randr_query_outputs(conn);
450 ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
456 * Our window decorations were unmapped. That means, the window will be killed now,
457 * so we better clean up before.
460 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
461 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
463 add_ignore_event(event->sequence);
465 Client *client = table_get(&by_child, event->window);
466 /* First, we need to check if the client is awaiting an unmap-request which
467 was generated by us reparenting the window. In that case, we just ignore it. */
468 if (client != NULL && client->awaiting_useless_unmap) {
469 client->awaiting_useless_unmap = false;
473 DLOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
474 DLOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
475 if (client == NULL) {
476 DLOG("not a managed window. Ignoring.\n");
478 /* This was most likely the destroyed frame of a client which is
479 * currently being unmapped, so we add this sequence (again!) to
480 * the ignore list (enter_notify events will get sent for both,
481 * the child and its frame). */
482 add_ignore_event(event->sequence);
487 client = table_remove(&by_child, event->window);
489 /* If this was the fullscreen client, we need to unset it */
490 if (client->fullscreen)
491 client->workspace->fullscreen_client = NULL;
493 /* Clients without a container are either floating or dock windows */
494 if (client->container != NULL) {
495 Container *con = client->container;
497 /* Remove the client from the list of clients */
498 client_remove_from_container(conn, client, con, true);
500 /* Set focus to the last focused client in this container */
501 con->currently_focused = get_last_focused_client(conn, con, NULL);
503 /* Only if this is the active container, we need to really change focus */
504 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
505 set_focus(conn, con->currently_focused, true);
506 } else if (client_is_floating(client)) {
507 DLOG("Removing from floating clients\n");
508 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
509 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
513 DLOG("Removing from dock clients\n");
514 SLIST_REMOVE(&(client->workspace->output->dock_clients), client, Client, dock_clients);
517 DLOG("child of 0x%08x.\n", client->frame);
518 xcb_reparent_window(conn, client->child, root, 0, 0);
520 client_unmap(conn, client);
522 xcb_destroy_window(conn, client->frame);
524 table_remove(&by_parent, client->frame);
526 if (client->container != NULL) {
527 Workspace *workspace = client->container->workspace;
528 cleanup_table(conn, workspace);
529 fix_colrowspan(conn, workspace);
532 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
533 bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
534 bool workspace_focused = (c_ws == client->workspace);
535 Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
537 /* If this workspace is currently visible, we don’t delete it */
538 if (workspace_is_visible(client->workspace))
539 workspace_empty = false;
541 if (workspace_empty) {
542 client->workspace->output = NULL;
543 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
546 /* Remove the urgency flag if set */
547 client->urgent = false;
548 workspace_update_urgent_flag(client->workspace);
550 FREE(client->window_class_instance);
551 FREE(client->window_class_class);
557 /* Ensure the focus is set to the next client in the focus stack or to
558 * the screen itself (if we do not focus the screen, it can happen that
559 * the focus is "nowhere" and thus keypress events will not be received
560 * by i3, thus the user cannot use any hotkeys). */
561 if (workspace_focused) {
562 if (to_focus != NULL)
563 set_focus(conn, to_focus, true);
565 DLOG("Restoring focus to root screen\n");
566 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
575 * Called when a window changes its title
578 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
579 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
580 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
581 DLOG("_NET_WM_NAME not specified, not changing\n");
584 Client *client = table_get(&by_child, window);
588 /* Save the old pointer to make the update atomic */
591 asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
592 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
593 char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
594 LOG("_NET_WM_NAME changed to \"%s\"\n", new_name);
597 /* Check if they are the same and don’t update if so.
598 Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
599 Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
600 but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
601 if ((new_len == client->name_len) &&
602 (client->name != NULL) &&
603 (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
608 char *old_name = client->name;
609 client->name = ucs2_name;
610 client->name_len = new_len;
611 client->uses_net_wm_name = true;
615 /* If the client is a dock window, we don’t need to render anything */
619 int mode = container_mode(client->container, true);
620 if (mode == MODE_STACK || mode == MODE_TABBED)
621 render_container(conn, client->container);
622 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
629 * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
630 * just pass them along, so when containing non-ASCII characters, those will be rendering
631 * incorrectly. In order to correctly render unicode window titles in i3, an application
632 * has to set _NET_WM_NAME, which is in UTF-8 encoding.
634 * On every update, a message is put out to the user, so he may improve the situation and
635 * update applications which display filenames in their title to correctly use
636 * _NET_WM_NAME and therefore support unicode.
639 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
640 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
641 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
642 DLOG("prop == NULL\n");
645 Client *client = table_get(&by_child, window);
649 /* Client capable of _NET_WM_NAME, ignore legacy name changes */
650 if (client->uses_net_wm_name)
653 /* Save the old pointer to make the update atomic */
655 if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
656 perror("Could not get old name");
657 DLOG("Could not get old name\n");
660 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
661 LOG("WM_NAME changed to \"%s\"\n", new_name);
663 /* Check if they are the same and don’t update if so. */
664 if (client->name != NULL &&
665 strlen(new_name) == strlen(client->name) &&
666 strcmp(client->name, new_name) == 0) {
671 LOG("Using legacy window title. Note that in order to get Unicode window titles in i3, "
672 "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
674 char *old_name = client->name;
675 client->name = new_name;
676 client->name_len = -1;
678 if (old_name != NULL)
681 /* If the client is a dock window, we don’t need to render anything */
685 if (client->container != NULL &&
686 (client->container->mode == MODE_STACK ||
687 client->container->mode == MODE_TABBED))
688 render_container(conn, client->container);
689 else decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
696 * Updates the client’s WM_CLASS property
699 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
700 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
701 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
702 DLOG("prop == NULL\n");
705 Client *client = table_get(&by_child, window);
709 /* We cannot use asprintf here since this property contains two
710 * null-terminated strings (for compatibility reasons). Instead, we
711 * use strdup() on both strings */
712 char *new_class = xcb_get_property_value(prop);
714 FREE(client->window_class_instance);
715 FREE(client->window_class_class);
717 client->window_class_instance = strdup(new_class);
718 if ((strlen(new_class) + 1) < xcb_get_property_value_length(prop))
719 client->window_class_class = strdup(new_class + strlen(new_class) + 1);
720 else client->window_class_class = NULL;
721 LOG("WM_CLASS changed to %s (instance), %s (class)\n",
722 client->window_class_instance, client->window_class_class);
728 * Expose event means we should redraw our windows (= title bar)
731 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
732 /* event->count is the number of minimum remaining expose events for this window, so we
733 skip all events but the last one */
734 if (event->count != 0)
736 DLOG("window = %08x\n", event->window);
738 Client *client = table_get(&by_parent, event->window);
739 if (client == NULL) {
740 /* There was no client in the table, so this is probably an expose event for
741 one of our stack_windows. */
742 struct Stack_Window *stack_win;
743 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
744 if (stack_win->window == event->window) {
745 render_container(conn, stack_win->container);
749 /* …or one of the bars? */
751 TAILQ_FOREACH(output, &outputs, outputs)
752 if (output->bar == event->window)
760 if (container_mode(client->container, true) == MODE_DEFAULT)
761 decorate_window(conn, client, client->frame, client->titlegc, 0, 0);
763 uint32_t background_color;
765 background_color = config.client.urgent.background;
766 /* Distinguish if the window is currently focused… */
767 else if (CUR_CELL != NULL && CUR_CELL->currently_focused == client)
768 background_color = config.client.focused.background;
769 /* …or if it is the focused window in a not focused container */
770 else background_color = config.client.focused_inactive.background;
772 /* Set foreground color to current focused color, line width to 2 */
773 uint32_t values[] = {background_color, 2};
774 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
776 /* Draw the border, the ±1 is for line width = 2 */
777 xcb_point_t points[] = {{1, 0}, /* left upper edge */
778 {1, client->rect.height-1}, /* left bottom edge */
779 {client->rect.width-1, client->rect.height-1}, /* right bottom edge */
780 {client->rect.width-1, 0}}; /* right upper edge */
781 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
783 /* Draw a black background */
784 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
785 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless) {
786 xcb_rectangle_t crect = {1, 0, client->rect.width - (1 + 1), client->rect.height - 1};
787 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
789 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
790 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
798 * Handle client messages (EWMH)
801 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
802 if (event->type == atoms[_NET_WM_STATE]) {
803 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
806 Client *client = table_get(&by_child, event->window);
810 /* Check if the fullscreen state should be toggled */
811 if ((client->fullscreen &&
812 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
813 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
814 (!client->fullscreen &&
815 (event->data.data32[0] == _NET_WM_STATE_ADD ||
816 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
817 client_toggle_fullscreen(conn, client);
819 ELOG("unhandled clientmessage\n");
826 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
827 xcb_atom_t atom, xcb_get_property_reply_t *property) {
828 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
829 before changing this property. */
830 ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
835 * Handles the size hints set by a window, but currently only the part necessary for displaying
836 * clients proportionally inside their frames (mplayer for example)
838 * See ICCCM 4.1.2.3 for more details
841 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
842 xcb_atom_t name, xcb_get_property_reply_t *reply) {
843 Client *client = table_get(&by_child, window);
844 if (client == NULL) {
845 DLOG("Received WM_SIZE_HINTS for unknown client\n");
848 xcb_size_hints_t size_hints;
852 /* If the hints were already in this event, use them, if not, request them */
854 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
856 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
858 if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
859 // TODO: Minimum size is not yet implemented
860 DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
863 bool changed = false;
864 if ((size_hints.flags & XCB_SIZE_HINT_P_RESIZE_INC)) {
865 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
866 if (client->width_increment != size_hints.width_inc) {
867 client->width_increment = size_hints.width_inc;
870 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
871 if (client->height_increment != size_hints.height_inc) {
872 client->height_increment = size_hints.height_inc;
877 DLOG("resize increments changed\n");
880 int base_width = 0, base_height = 0;
882 /* base_width/height are the desired size of the window.
883 We check if either the program-specified size or the program-specified
884 min-size is available */
885 if (size_hints.flags & XCB_SIZE_HINT_BASE_SIZE) {
886 base_width = size_hints.base_width;
887 base_height = size_hints.base_height;
888 } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
889 /* TODO: is this right? icccm says not */
890 base_width = size_hints.min_width;
891 base_height = size_hints.min_height;
894 if (base_width != client->base_width ||
895 base_height != client->base_height) {
896 client->base_width = base_width;
897 client->base_height = base_height;
898 DLOG("client's base_height changed to %d\n", base_height);
899 DLOG("client's base_width changed to %d\n", base_width);
904 if (client->fullscreen)
905 DLOG("Not resizing client, it is in fullscreen mode\n");
907 resize_client(conn, client);
912 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
913 if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
914 (size_hints.min_aspect_num <= 0) ||
915 (size_hints.min_aspect_den <= 0)) {
919 double width = client->rect.width - base_width;
920 double height = client->rect.height - base_height;
921 /* Convert numerator/denominator to a double */
922 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
923 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
925 DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
926 DLOG("width = %f, height = %f\n", width, height);
928 /* Sanity checks, this is user-input, in a way */
929 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
932 /* Check if we need to set proportional_* variables using the correct ratio */
933 if ((width / height) < min_aspect) {
934 client->proportional_width = width;
935 client->proportional_height = width / min_aspect;
936 } else if ((width / height) > max_aspect) {
937 client->proportional_width = width;
938 client->proportional_height = width / max_aspect;
941 client->force_reconfigure = true;
943 if (client->container != NULL) {
944 render_container(conn, client->container);
952 * Handles the WM_HINTS property for extracting the urgency state of the window.
955 int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
956 xcb_atom_t name, xcb_get_property_reply_t *reply) {
957 Client *client = table_get(&by_child, window);
958 if (client == NULL) {
959 DLOG("Received WM_HINTS for unknown client\n");
962 xcb_wm_hints_t hints;
965 if (!xcb_get_wm_hints_from_reply(&hints, reply))
968 if (!xcb_get_wm_hints_reply(conn, xcb_get_wm_hints_unchecked(conn, client->child), &hints, NULL))
972 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
973 if (!client->urgent && client == last_focused) {
974 DLOG("Ignoring urgency flag for current client\n");
978 /* Update the flag on the client directly */
979 client->urgent = (xcb_wm_hints_get_urgency(&hints) != 0);
981 LOG("Urgency flag changed to %d\n", client->urgent);
983 workspace_update_urgent_flag(client->workspace);
984 redecorate_window(conn, client);
986 /* If the workspace this client is on is not visible, we need to redraw
987 * the workspace bar */
988 if (!workspace_is_visible(client->workspace)) {
989 Output *output = client->workspace->output;
990 render_workspace(conn, output, output->current_workspace);
998 * Handles the transient for hints set by a window, signalizing that this window is a popup window
999 * for some other window.
1001 * See ICCCM 4.1.2.6 for more details
1004 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1005 xcb_atom_t name, xcb_get_property_reply_t *reply) {
1006 Client *client = table_get(&by_child, window);
1007 if (client == NULL) {
1008 DLOG("No such client\n");
1012 xcb_window_t transient_for;
1014 if (reply != NULL) {
1015 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply))
1018 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
1019 &transient_for, NULL))
1023 if (client->floating == FLOATING_AUTO_OFF) {
1024 DLOG("This is a popup window, putting into floating\n");
1025 toggle_floating_mode(conn, client, true);
1032 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
1033 * toolwindow (or similar) and to which window it belongs (logical parent).
1036 int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1037 xcb_atom_t name, xcb_get_property_reply_t *prop) {
1039 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
1040 false, window, WM_CLIENT_LEADER, WINDOW, 0, 32), NULL);
1045 Client *client = table_get(&by_child, window);
1049 xcb_window_t *leader = xcb_get_property_value(prop);
1053 DLOG("Client leader changed to %08x\n", *leader);
1055 client->leader = *leader;