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>
39 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
40 since it’d trigger an infinite loop of switching between the different windows when
41 changing workspaces */
42 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
44 static void add_ignore_event(const int sequence) {
45 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
47 event->sequence = sequence;
48 event->added = time(NULL);
50 LOG("Adding sequence %d to ignorelist\n", sequence);
52 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
56 * Checks if the given sequence is ignored and returns true if so.
59 static bool event_is_ignored(const int sequence) {
60 struct Ignore_Event *event;
61 time_t now = time(NULL);
62 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
63 if ((now - event->added) > 5) {
64 struct Ignore_Event *save = event;
65 event = SLIST_NEXT(event, ignore_events);
66 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
68 } else event = SLIST_NEXT(event, ignore_events);
71 SLIST_FOREACH(event, &ignore_events, ignore_events) {
72 if (event->sequence == sequence) {
73 LOG("Ignoring event (sequence %d)\n", 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 LOG("got key release, just passing\n");
90 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
96 * There was a key press. We compare this key code with our bindings table and pass
97 * the bound action to parse_command().
100 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
101 LOG("Keypress %d, state raw = %d\n", event->detail, event->state);
103 /* Remove the numlock bit, all other bits are modifiers we can bind to */
104 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
105 LOG("(removed numlock, state = %d)\n", state_filtered);
106 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
107 * button masks are filtered out */
108 state_filtered &= 0xFF;
109 LOG("(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;
118 LOG("(checked mode_switch, state %d)\n", state_filtered);
120 /* Find the binding */
122 TAILQ_FOREACH(bind, &bindings, bindings)
123 if (bind->keycode == event->detail && bind->mods == state_filtered)
126 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
127 the user did not press Mode_switch, so just pass it… */
128 if (bind == TAILQ_END(&bindings)) {
129 xcb_allow_events(conn, ReplayKeyboard, event->time);
134 parse_command(conn, bind->command);
135 if (state_filtered & BIND_MODE_SWITCH) {
136 LOG("Mode_switch -> allow_events(SyncKeyboard)\n");
137 xcb_allow_events(conn, SyncKeyboard, event->time);
145 * When the user moves the mouse pointer onto a window, this callback gets called.
148 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
149 LOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
150 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
151 LOG("This was not a normal notify, ignoring\n");
154 /* Some events are not interesting, because they were not generated actively by the
155 user, but by reconfiguration of windows */
156 if (event_is_ignored(event->sequence))
159 /* This was either a focus for a client’s parent (= titlebar)… */
160 Client *client = table_get(&by_parent, event->event);
161 /* …or the client itself */
163 client = table_get(&by_child, event->event);
165 /* Check for stack windows */
166 if (client == NULL) {
167 struct Stack_Window *stack_win;
168 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
169 if (stack_win->window == event->event) {
170 client = stack_win->container->currently_focused;
176 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
177 if (client == NULL) {
178 LOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
179 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
180 if (screen == NULL) {
181 LOG("ERROR: No such screen\n");
184 c_ws->current_row = current_row;
185 c_ws->current_col = current_col;
186 c_ws = &workspaces[screen->current_workspace];
187 current_row = c_ws->current_row;
188 current_col = c_ws->current_col;
189 LOG("We're now on virtual screen number %d\n", screen->num);
193 /* Do plausibility checks: This event may be useless for us if it occurs on a window
194 which is in a stacked container but not the focused one */
195 if (client->container != NULL &&
196 client->container->mode == MODE_STACK &&
197 client->container->currently_focused != client) {
198 LOG("Plausibility check says: no\n");
202 if (client->workspace != c_ws && client->workspace->screen == c_ws->screen) {
203 /* This can happen when a client gets assigned to a different workspace than
204 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
205 * an enter_notify will follow. */
206 LOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
210 set_focus(conn, client, false);
216 * Checks if the button press was on a stack window, handles focus setting and returns true
217 * if so, or false otherwise.
220 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
221 struct Stack_Window *stack_win;
222 SLIST_FOREACH(stack_win, &stack_wins, stack_windows) {
223 if (stack_win->window != event->event)
226 /* A stack window was clicked, we check if it was button4 or button5
227 which are scroll up / scroll down. */
228 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
229 direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN);
230 focus_window_in_container(conn, CUR_CELL, direction);
234 /* It was no scrolling, so we calculate the destination client by
235 dividing the Y position of the event through the height of a window
236 decoration and then set the focus to this client. */
237 i3Font *font = load_font(conn, config.font);
238 int decoration_height = (font->height + 2 + 2);
239 int destination = (event->event_y / decoration_height),
243 LOG("Click on stack_win for client %d\n", destination);
244 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
245 if (c++ == destination) {
246 set_focus(conn, client, true);
257 * Checks if the button press was on a bar, switches to the workspace and returns true
258 * if so, or false otherwise.
261 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
263 TAILQ_FOREACH(screen, virtual_screens, screens) {
264 if (screen->bar != event->event)
267 LOG("Click on a bar\n");
269 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
270 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
271 int add = (event->detail == XCB_BUTTON_INDEX_4 ? -1 : 1);
272 for (int i = c_ws->num + add; (i >= 0) && (i < 10); i += add)
273 if (workspaces[i].screen == screen) {
274 show_workspace(conn, i+1);
280 /* Because workspaces can be on different screens, we need to loop
281 through all of them and decide to count it based on its ->screen */
282 for (int i = 0; i < 10; i++) {
283 if (workspaces[i].screen != screen)
285 LOG("Checking if click was on workspace %d with drawn = %d, tw = %d\n",
286 i, drawn, workspaces[i].text_width);
287 if (event->event_x > (drawn + 1) &&
288 event->event_x <= (drawn + 1 + workspaces[i].text_width + 5 + 5)) {
289 show_workspace(conn, i+1);
293 drawn += workspaces[i].text_width + 5 + 5 + 2;
301 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
302 LOG("button press!\n");
303 LOG("state = %d\n", event->state);
304 /* This was either a focus for a client’s parent (= titlebar)… */
305 Client *client = table_get(&by_child, event->event);
306 bool border_click = false;
307 if (client == NULL) {
308 client = table_get(&by_parent, event->event);
311 /* See if this was a click with the configured modifier. If so, we need
312 * to move around the client if it was floating. if not, we just process
314 if (config.floating_modifier != 0 &&
315 (event->state & config.floating_modifier) != 0) {
316 if (client == NULL) {
317 LOG("Not handling, floating_modifier was pressed and no client found\n");
320 if (client_is_floating(client)) {
321 floating_drag_window(conn, client, event);
326 if (client == NULL) {
327 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
328 if (button_press_stackwin(conn, event))
332 if (button_press_bar(conn, event))
335 LOG("Could not handle this button press\n");
339 /* Set focus in any case */
340 set_focus(conn, client, true);
342 /* Let’s see if this was on the borders (= resize). If not, we’re done */
343 LOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
344 resize_orientation_t orientation = O_VERTICAL;
345 Container *con = client->container;
349 LOG("dock. done.\n");
350 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
355 LOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
357 /* Some clients (xfontsel for example) seem to pass clicks on their
358 * window to the parent window, thus we receive an event here which in
359 * reality is a border_click. Check for the position and fix state. */
361 event->event_x >= client->child_rect.x &&
362 event->event_x <= (client->child_rect.x + client->child_rect.width) &&
363 event->event_y >= client->child_rect.y &&
364 event->event_y <= (client->child_rect.y + client->child_rect.height)) {
365 LOG("Fixing border_click = false because of click in child\n");
366 border_click = false;
370 LOG("client. done.\n");
371 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
372 /* Floating clients should be raised on click */
373 if (client_is_floating(client))
374 xcb_raise_window(conn, client->frame);
379 /* Don’t handle events inside the titlebar, only borders are interesting */
380 i3Font *font = load_font(conn, config.font);
381 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
382 LOG("click on titlebar\n");
384 /* Floating clients can be dragged by grabbing their titlebar */
385 if (client_is_floating(client)) {
386 /* Firstly, we raise it. Maybe the user just wanted to raise it without grabbing */
387 xcb_raise_window(conn, client->frame);
390 floating_drag_window(conn, client, event);
395 if (client_is_floating(client))
396 return floating_border_click(conn, client, event);
398 if (event->event_y < 2) {
399 /* This was a press on the top border */
402 first = con->row - 1;
404 orientation = O_HORIZONTAL;
405 } else if (event->event_y >= (client->rect.height - 2)) {
407 first = con->row + (con->rowspan - 1);
408 if (!cell_exists(con->col, first) ||
409 (first == (con->workspace->rows-1)))
413 orientation = O_HORIZONTAL;
414 } else if (event->event_x <= 2) {
419 first = con->col - 1;
421 } else if (event->event_x > 2) {
423 first = con->col + (con->colspan - 1);
424 LOG("column %d\n", first);
426 if (!cell_exists(first, con->row) ||
427 (first == (con->workspace->cols-1)))
433 return resize_graphical_handler(conn, con->workspace, first, second, orientation, event);
437 * A new window appeared on the screen (=was mapped), so let’s manage it.
440 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
441 xcb_get_window_attributes_cookie_t cookie;
443 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
445 LOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
446 add_ignore_event(event->sequence);
448 manage_window(prophs, conn, event->window, cookie, false);
453 * Configure requests are received when the application wants to resize windows on their own.
455 * We generate a synthethic configure notify event to signalize the client its "new" position.
458 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
459 LOG("configure-request, serial %d\n", event->sequence);
460 LOG("event->window = %08x\n", event->window);
461 LOG("application wants to be at %dx%d with %dx%d\n", event->x, event->y, event->width, event->height);
463 Client *client = table_get(&by_child, event->window);
464 if (client == NULL) {
465 LOG("This client is not mapped, so we don't care and just tell the client that he will get its size\n");
469 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
470 if (event->value_mask & mask_member) { \
471 mask |= mask_member; \
472 values[c++] = event->event_member; \
476 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
477 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
478 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
479 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
480 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
481 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
482 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
484 xcb_configure_window(conn, event->window, mask, values);
490 if (client->fullscreen) {
491 LOG("Client is in fullscreen mode\n");
493 Rect child_rect = client->workspace->rect;
494 child_rect.x = child_rect.y = 0;
495 fake_configure_notify(conn, child_rect, client->child);
500 /* Floating clients can be reconfigured */
501 if (client_is_floating(client)) {
502 i3Font *font = load_font(conn, config.font);
504 if (event->value_mask & XCB_CONFIG_WINDOW_X)
505 client->rect.x = event->x;
506 if (event->value_mask & XCB_CONFIG_WINDOW_Y)
507 client->rect.y = event->y;
508 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH)
509 client->rect.width = event->width + 2 + 2;
510 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)
511 client->rect.height = event->height + (font->height + 2 + 2) + 2;
513 LOG("Accepted new position/size for floating client: (%d, %d) size %d x %d\n",
514 client->rect.x, client->rect.y, client->rect.width, client->rect.height);
516 /* Push the new position/size to X11 */
517 reposition_client(conn, client);
518 resize_client(conn, client);
524 if (client->fullscreen) {
525 LOG("Client is in fullscreen mode\n");
527 Rect child_rect = client->container->workspace->rect;
528 child_rect.x = child_rect.y = 0;
529 fake_configure_notify(conn, child_rect, client->child);
534 fake_absolute_configure_notify(conn, client);
540 * Configuration notifies are only handled because we need to set up ignore for the following
541 * enter notify events
544 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
545 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
547 LOG("handle_configure_event for window %08x\n", event->window);
548 LOG("event->type = %d, \n", event->response_type);
549 LOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
551 /* We ignore this sequence twice because events for child and frame should be ignored */
552 add_ignore_event(event->sequence);
553 add_ignore_event(event->sequence);
555 if (event->event == root) {
556 LOG("reconfigure of the root window, need to xinerama\n");
557 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
558 but is there a better way? */
559 if (event->x == 0 && event->y == 0)
560 xinerama_requery_screens(conn);
568 * Our window decorations were unmapped. That means, the window will be killed now,
569 * so we better clean up before.
572 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
573 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
575 add_ignore_event(event->sequence);
577 Client *client = table_get(&by_child, event->window);
578 /* First, we need to check if the client is awaiting an unmap-request which
579 was generated by us reparenting the window. In that case, we just ignore it. */
580 if (client != NULL && client->awaiting_useless_unmap) {
581 LOG("Dropping this unmap request, it was generated by reparenting\n");
582 client->awaiting_useless_unmap = false;
586 LOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
587 LOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
588 if (client == NULL) {
589 LOG("not a managed window. Ignoring.\n");
591 /* This was most likely the destroyed frame of a client which is
592 * currently being unmapped, so we add this sequence (again!) to
593 * the ignore list (enter_notify events will get sent for both,
594 * the child and its frame). */
595 add_ignore_event(event->sequence);
600 client = table_remove(&by_child, event->window);
602 /* If this was the fullscreen client, we need to unset it */
603 if (client->fullscreen)
604 client->workspace->fullscreen_client = NULL;
606 /* Clients without a container are either floating or dock windows */
607 if (client->container != NULL) {
608 Container *con = client->container;
610 /* Remove the client from the list of clients */
611 client_remove_from_container(conn, client, con, true);
613 /* Set focus to the last focused client in this container */
614 con->currently_focused = get_last_focused_client(conn, con, NULL);
616 /* Only if this is the active container, we need to really change focus */
617 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
618 set_focus(conn, con->currently_focused, true);
619 } else if (client_is_floating(client)) {
620 LOG("Removing from floating clients\n");
621 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
622 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
626 LOG("Removing from dock clients\n");
627 SLIST_REMOVE(&(client->workspace->screen->dock_clients), client, Client, dock_clients);
630 LOG("child of 0x%08x.\n", client->frame);
631 xcb_reparent_window(conn, client->child, root, 0, 0);
632 xcb_destroy_window(conn, client->frame);
634 table_remove(&by_parent, client->frame);
636 if (client->container != NULL) {
637 Workspace *workspace = client->container->workspace;
638 cleanup_table(conn, workspace);
639 fix_colrowspan(conn, workspace);
642 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
643 bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
644 bool workspace_active = false;
645 Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
647 /* If this workspace is currently active, we don’t delete it */
649 TAILQ_FOREACH(screen, virtual_screens, screens)
650 if (screen->current_workspace == client->workspace->num) {
651 workspace_active = true;
652 workspace_empty = false;
656 if (workspace_empty) {
657 LOG("setting ws to NULL for workspace %d (%p)\n", client->workspace->num,
659 client->workspace->screen = NULL;
662 FREE(client->window_class);
668 /* Ensure the focus is set to the next client in the focus stack */
669 if (workspace_active && to_focus != NULL)
670 set_focus(conn, to_focus, true);
676 * Called when a window changes its title
679 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
680 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
681 LOG("window's name changed.\n");
682 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
683 LOG("_NET_WM_NAME not specified, not changing\n");
686 Client *client = table_get(&by_child, window);
690 /* Save the old pointer to make the update atomic */
693 asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
694 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
695 char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
696 LOG("Name should change to \"%s\"\n", new_name);
699 /* Check if they are the same and don’t update if so.
700 Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
701 Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
702 but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
703 if ((new_len == client->name_len) &&
704 (client->name != NULL) &&
705 (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
706 LOG("Name did not change, not updating\n");
711 char *old_name = client->name;
712 client->name = ucs2_name;
713 client->name_len = new_len;
714 client->uses_net_wm_name = true;
718 /* If the client is a dock window, we don’t need to render anything */
722 if (client->container != NULL && client->container->mode == MODE_STACK)
723 render_container(conn, client->container);
724 else decorate_window(conn, client, client->frame, client->titlegc, 0);
731 * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
732 * just pass them along, so when containing non-ASCII characters, those will be rendering
733 * incorrectly. In order to correctly render unicode window titles in i3, an application
734 * has to set _NET_WM_NAME, which is in UTF-8 encoding.
736 * On every update, a message is put out to the user, so he may improve the situation and
737 * update applications which display filenames in their title to correctly use
738 * _NET_WM_NAME and therefore support unicode.
741 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
742 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
743 LOG("window's name changed (legacy).\n");
744 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
745 LOG("prop == NULL\n");
748 Client *client = table_get(&by_child, window);
752 if (client->uses_net_wm_name) {
753 LOG("This client is capable of _NET_WM_NAME, ignoring legacy name\n");
757 /* Save the old pointer to make the update atomic */
759 if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
760 perror("Could not get old name");
761 LOG("Could not get old name\n");
764 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
765 LOG("Name should change to \"%s\"\n", new_name);
767 /* Check if they are the same and don’t update if so. */
768 if (client->name != NULL &&
769 strlen(new_name) == strlen(client->name) &&
770 strcmp(client->name, new_name) == 0) {
771 LOG("Name did not change, not updating\n");
776 LOG("Using legacy window title. Note that in order to get Unicode window titles in i3,"
777 "the application has to set _NET_WM_NAME which is in UTF-8 encoding.\n");
779 char *old_name = client->name;
780 client->name = new_name;
781 client->name_len = -1;
783 if (old_name != NULL)
786 /* If the client is a dock window, we don’t need to render anything */
790 if (client->container != NULL && client->container->mode == MODE_STACK)
791 render_container(conn, client->container);
792 else decorate_window(conn, client, client->frame, client->titlegc, 0);
799 * Updates the client’s WM_CLASS property
802 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
803 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
804 LOG("window class changed\n");
805 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
806 LOG("prop == NULL\n");
809 Client *client = table_get(&by_child, window);
813 if (asprintf(&new_class, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
814 perror("Could not get window class");
815 LOG("Could not get window class\n");
819 LOG("changed to %s\n", new_class);
820 char *old_class = client->window_class;
821 client->window_class = new_class;
824 if (!client->initialized) {
825 LOG("Client is not yet initialized, not putting it to floating\n");
829 if (strcmp(new_class, "tools") == 0 || strcmp(new_class, "Dialog") == 0) {
830 LOG("tool/dialog window, should we put it floating?\n");
831 if (client->floating == FLOATING_AUTO_OFF)
832 toggle_floating_mode(conn, client, true);
839 * Expose event means we should redraw our windows (= title bar)
842 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
843 /* event->count is the number of minimum remaining expose events for this window, so we
844 skip all events but the last one */
845 if (event->count != 0)
847 LOG("window = %08x\n", event->window);
849 Client *client = table_get(&by_parent, event->window);
850 if (client == NULL) {
851 /* There was no client in the table, so this is probably an expose event for
852 one of our stack_windows. */
853 struct Stack_Window *stack_win;
854 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
855 if (stack_win->window == event->window) {
856 render_container(conn, stack_win->container);
860 /* …or one of the bars? */
862 TAILQ_FOREACH(screen, virtual_screens, screens)
863 if (screen->bar == event->window)
868 LOG("got client %s\n", client->name);
870 LOG("this is a dock\n");
874 if (client->container == NULL || client->container->mode != MODE_STACK)
875 decorate_window(conn, client, client->frame, client->titlegc, 0);
877 uint32_t background_color;
878 /* Distinguish if the window is currently focused… */
879 if (CUR_CELL->currently_focused == client)
880 background_color = config.client.focused.background;
881 /* …or if it is the focused window in a not focused container */
882 else background_color = config.client.focused_inactive.background;
884 /* Set foreground color to current focused color, line width to 2 */
885 uint32_t values[] = {background_color, 2};
886 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
888 /* Draw the border, the ±1 is for line width = 2 */
889 xcb_point_t points[] = {{1, 0}, /* left upper edge */
890 {1, client->rect.height-1}, /* left bottom edge */
891 {client->rect.width-1, client->rect.height-1}, /* right bottom edge */
892 {client->rect.width-1, 0}}; /* right upper edge */
893 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
895 /* Draw a black background */
896 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
897 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
898 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
905 * Handle client messages (EWMH)
908 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
909 LOG("client_message\n");
911 if (event->type == atoms[_NET_WM_STATE]) {
912 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
917 Client *client = table_get(&by_child, event->window);
921 /* Check if the fullscreen state should be toggled */
922 if ((client->fullscreen &&
923 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
924 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
925 (!client->fullscreen &&
926 (event->data.data32[0] == _NET_WM_STATE_ADD ||
927 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
928 client_toggle_fullscreen(conn, client);
930 LOG("unhandled clientmessage\n");
937 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
938 xcb_atom_t atom, xcb_get_property_reply_t *property) {
939 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
940 before changing this property. */
941 LOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
946 * Handles the size hints set by a window, but currently only the part necessary for displaying
947 * clients proportionally inside their frames (mplayer for example)
949 * See ICCCM 4.1.2.3 for more details
952 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
953 xcb_atom_t name, xcb_get_property_reply_t *reply) {
954 LOG("handle_normal_hints\n");
955 Client *client = table_get(&by_child, window);
956 if (client == NULL) {
957 LOG("No such client\n");
960 xcb_size_hints_t size_hints;
961 LOG("client is %08x / child %08x\n", client->frame, client->child);
963 /* If the hints were already in this event, use them, if not, request them */
965 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
967 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
969 if ((size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE)) {
970 LOG("min size set\n");
971 LOG("gots min_width = %d, min_height = %d\n", size_hints.min_width, size_hints.min_height);
974 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
975 if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
976 (size_hints.min_aspect_num <= 0) ||
977 (size_hints.min_aspect_den <= 0)) {
978 LOG("No aspect ratio set, ignoring\n");
982 LOG("window is %08x / %s\n", client->child, client->name);
984 int base_width = 0, base_height = 0;
986 /* base_width/height are the desired size of the window.
987 We check if either the program-specified size or the program-specified
988 min-size is available */
989 if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
990 base_width = size_hints.base_width;
991 base_height = size_hints.base_height;
992 } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
993 base_width = size_hints.min_width;
994 base_height = size_hints.min_height;
997 double width = client->rect.width - base_width;
998 double height = client->rect.height - base_height;
999 /* Convert numerator/denominator to a double */
1000 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
1001 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
1003 LOG("min_aspect = %f, max_aspect = %f\n", min_aspect, max_aspect);
1004 LOG("width = %f, height = %f\n", width, height);
1006 /* Sanity checks, this is user-input, in a way */
1007 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
1010 /* Check if we need to set proportional_* variables using the correct ratio */
1011 if ((width / height) < min_aspect) {
1012 client->proportional_width = width;
1013 client->proportional_height = width / min_aspect;
1014 } else if ((width / height) > max_aspect) {
1015 client->proportional_width = width;
1016 client->proportional_height = width / max_aspect;
1019 client->force_reconfigure = true;
1021 if (client->container != NULL) {
1022 render_container(conn, client->container);
1030 * Handles the transient for hints set by a window, signalizing that this window is a popup window
1031 * for some other window.
1033 * See ICCCM 4.1.2.6 for more details
1036 int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
1037 xcb_atom_t name, xcb_get_property_reply_t *reply) {
1038 LOG("Transient hint!\n");
1039 Client *client = table_get(&by_child, window);
1040 if (client == NULL) {
1041 LOG("No such client\n");
1045 xcb_window_t transient_for;
1047 if (reply != NULL) {
1048 if (!xcb_get_wm_transient_for_from_reply(&transient_for, reply)) {
1049 LOG("Not transient for any window\n");
1053 if (!xcb_get_wm_transient_for_reply(conn, xcb_get_wm_transient_for_unchecked(conn, window),
1054 &transient_for, NULL)) {
1055 LOG("Not transient for any window\n");
1060 if (client->floating == FLOATING_AUTO_OFF) {
1061 LOG("This is a popup window, putting into floating\n");
1062 toggle_floating_mode(conn, client, true);