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 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
39 since it’d trigger an infinite loop of switching between the different windows when
40 changing workspaces */
41 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
43 static void add_ignore_event(const int sequence) {
44 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
46 event->sequence = sequence;
47 event->added = time(NULL);
49 LOG("Adding sequence %d to ignorelist\n", sequence);
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 LOG("Ignoring event (sequence %d)\n", sequence);
73 SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
83 * Due to bindings like Mode_switch + <a>, we need to bind some keys in XCB_GRAB_MODE_SYNC.
84 * Therefore, we just replay all key presses.
87 int handle_key_release(void *ignored, xcb_connection_t *conn, xcb_key_release_event_t *event) {
88 LOG("got key release, just passing\n");
89 xcb_allow_events(conn, XCB_ALLOW_REPLAY_KEYBOARD, event->time);
95 * There was a key press. We compare this key code with our bindings table and pass
96 * the bound action to parse_command().
99 int handle_key_press(void *ignored, xcb_connection_t *conn, xcb_key_press_event_t *event) {
100 LOG("Keypress %d, state raw = %d\n", event->detail, event->state);
102 /* Remove the numlock bit, all other bits are modifiers we can bind to */
103 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
104 LOG("(removed numlock, state = %d)\n", state_filtered);
105 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
106 * button masks are filtered out */
107 state_filtered &= 0xFF;
108 LOG("(removed upper 8 bits, state = %d)\n", state_filtered);
110 /* We need to get the keysym group (There are group 1 to group 4, each holding
111 two keysyms (without shift and with shift) using Xkb because X fails to
112 provide them reliably (it works in Xephyr, it does not in real X) */
114 if (XkbGetState(xkbdpy, XkbUseCoreKbd, &state) == Success && (state.group+1) == 2)
115 state_filtered |= BIND_MODE_SWITCH;
117 LOG("(checked mode_switch, state %d)\n", state_filtered);
119 /* Find the binding */
121 TAILQ_FOREACH(bind, &bindings, bindings)
122 if (bind->keycode == event->detail && bind->mods == state_filtered)
125 /* No match? Then it was an actively grabbed key, that is with Mode_switch, and
126 the user did not press Mode_switch, so just pass it… */
127 if (bind == TAILQ_END(&bindings)) {
128 xcb_allow_events(conn, ReplayKeyboard, event->time);
133 parse_command(conn, bind->command);
134 if (state_filtered & BIND_MODE_SWITCH) {
135 LOG("Mode_switch -> allow_events(SyncKeyboard)\n");
136 xcb_allow_events(conn, SyncKeyboard, event->time);
144 * When the user moves the mouse pointer onto a window, this callback gets called.
147 int handle_enter_notify(void *ignored, xcb_connection_t *conn, xcb_enter_notify_event_t *event) {
148 LOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n", event->event, event->mode, event->detail, event->sequence);
149 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
150 LOG("This was not a normal notify, ignoring\n");
153 /* Some events are not interesting, because they were not generated actively by the
154 user, but by reconfiguration of windows */
155 if (event_is_ignored(event->sequence))
158 /* This was either a focus for a client’s parent (= titlebar)… */
159 Client *client = table_get(&by_parent, event->event);
160 /* …or the client itself */
162 client = table_get(&by_child, event->event);
164 /* Check for stack windows */
165 if (client == NULL) {
166 struct Stack_Window *stack_win;
167 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
168 if (stack_win->window == event->event) {
169 client = stack_win->container->currently_focused;
175 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
176 if (client == NULL) {
177 LOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
178 i3Screen *screen = get_screen_containing(event->root_x, event->root_y);
179 if (screen == NULL) {
180 LOG("ERROR: No such screen\n");
183 c_ws->current_row = current_row;
184 c_ws->current_col = current_col;
185 c_ws = &workspaces[screen->current_workspace];
186 current_row = c_ws->current_row;
187 current_col = c_ws->current_col;
188 LOG("We're now on virtual screen number %d\n", screen->num);
192 /* Do plausibility checks: This event may be useless for us if it occurs on a window
193 which is in a stacked container but not the focused one */
194 if (client->container != NULL &&
195 client->container->mode == MODE_STACK &&
196 client->container->currently_focused != client) {
197 LOG("Plausibility check says: no\n");
201 if (client->container != NULL && client->container->workspace != c_ws) {
202 /* This can happen when a client gets assigned to a different workspace than
203 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
204 * an enter_notify will follow. */
205 LOG("enter_notify for a client on a different workspace, ignoring\n");
209 set_focus(conn, client, false);
215 * Checks if the button press was on a stack window, handles focus setting and returns true
216 * if so, or false otherwise.
219 static bool button_press_stackwin(xcb_connection_t *conn, xcb_button_press_event_t *event) {
220 struct Stack_Window *stack_win;
221 SLIST_FOREACH(stack_win, &stack_wins, stack_windows) {
222 if (stack_win->window != event->event)
225 /* A stack window was clicked, we check if it was button4 or button5
226 which are scroll up / scroll down. */
227 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
228 direction_t direction = (event->detail == XCB_BUTTON_INDEX_4 ? D_UP : D_DOWN);
229 focus_window_in_container(conn, CUR_CELL, direction);
233 /* It was no scrolling, so we calculate the destination client by
234 dividing the Y position of the event through the height of a window
235 decoration and then set the focus to this client. */
236 i3Font *font = load_font(conn, config.font);
237 int decoration_height = (font->height + 2 + 2);
238 int destination = (event->event_y / decoration_height),
242 LOG("Click on stack_win for client %d\n", destination);
243 CIRCLEQ_FOREACH(client, &(stack_win->container->clients), clients)
244 if (c++ == destination) {
245 set_focus(conn, client, true);
256 * Checks if the button press was on a bar, switches to the workspace and returns true
257 * if so, or false otherwise.
260 static bool button_press_bar(xcb_connection_t *conn, xcb_button_press_event_t *event) {
262 TAILQ_FOREACH(screen, virtual_screens, screens) {
263 if (screen->bar != event->event)
266 LOG("Click on a bar\n");
268 /* Check if the button was one of button4 or button5 (scroll up / scroll down) */
269 if (event->detail == XCB_BUTTON_INDEX_4 || event->detail == XCB_BUTTON_INDEX_5) {
270 int add = (event->detail == XCB_BUTTON_INDEX_4 ? -1 : 1);
271 for (int i = c_ws->num + add; (i >= 0) && (i < 10); i += add)
272 if (workspaces[i].screen == screen) {
273 show_workspace(conn, i+1);
278 i3Font *font = load_font(conn, config.font);
279 int workspace = event->event_x / (font->height + 6),
281 /* Because workspaces can be on different screens, we need to loop
282 through all of them and decide to count it based on its ->screen */
283 for (int i = 0; i < 10; i++)
284 if ((workspaces[i].screen == screen) && (c++ == workspace)) {
285 show_workspace(conn, i+1);
294 int handle_button_press(void *ignored, xcb_connection_t *conn, xcb_button_press_event_t *event) {
295 LOG("button press!\n");
296 /* This was either a focus for a client’s parent (= titlebar)… */
297 Client *client = table_get(&by_child, event->event);
298 bool border_click = false;
299 if (client == NULL) {
300 client = table_get(&by_parent, event->event);
303 if (client == NULL) {
304 /* The client was neither on a client’s titlebar nor on a client itself, maybe on a stack_window? */
305 if (button_press_stackwin(conn, event))
309 if (button_press_bar(conn, event))
312 LOG("Could not handle this button press\n");
316 /* Set focus in any case */
317 set_focus(conn, client, true);
319 /* Let’s see if this was on the borders (= resize). If not, we’re done */
320 LOG("press button on x=%d, y=%d\n", event->event_x, event->event_y);
321 resize_orientation_t orientation = O_VERTICAL;
322 Container *con = client->container;
326 LOG("dock. done.\n");
327 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
332 LOG("event->event_x = %d, client->rect.width = %d\n", event->event_x, client->rect.width);
335 LOG("client. done.\n");
336 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
341 /* Don’t handle events inside the titlebar, only borders are interesting */
342 i3Font *font = load_font(conn, config.font);
343 if (event->event_y >= 2 && event->event_y <= (font->height + 2 + 2)) {
344 LOG("click on titlebar\n");
348 if (event->event_y < 2) {
349 /* This was a press on the top border */
352 first = con->row - 1;
354 orientation = O_HORIZONTAL;
355 } else if (event->event_y >= (client->rect.height - 2)) {
357 first = con->row + (con->rowspan - 1);
358 if (!cell_exists(con->col, first) ||
359 (first == (con->workspace->rows-1)))
363 orientation = O_HORIZONTAL;
364 } else if (event->event_x <= 2) {
369 first = con->col - 1;
371 } else if (event->event_x > 2) {
373 first = con->col + (con->colspan - 1);
374 LOG("column %d\n", first);
376 if (!cell_exists(first, con->row) ||
377 (first == (con->workspace->cols-1)))
383 return resize_graphical_handler(conn, con->workspace, first, second, orientation, event);
387 * A new window appeared on the screen (=was mapped), so let’s manage it.
390 int handle_map_request(void *prophs, xcb_connection_t *conn, xcb_map_request_event_t *event) {
391 xcb_get_window_attributes_cookie_t cookie;
392 xcb_get_window_attributes_reply_t *reply;
394 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
396 if ((reply = xcb_get_window_attributes_reply(conn, cookie, NULL)) == NULL) {
397 LOG("Could not get window attributes\n");
401 window_attributes_t wa = { TAG_VALUE };
402 LOG("override_redirect = %d\n", reply->override_redirect);
403 wa.u.override_redirect = reply->override_redirect;
404 LOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
405 add_ignore_event(event->sequence);
407 manage_window(prophs, conn, event->window, wa);
412 * Configure requests are received when the application wants to resize windows on their own.
414 * We generate a synthethic configure notify event to signalize the client its "new" position.
417 int handle_configure_request(void *prophs, xcb_connection_t *conn, xcb_configure_request_event_t *event) {
418 LOG("configure-request, serial %d\n", event->sequence);
419 LOG("event->window = %08x\n", event->window);
420 LOG("application wants to be at %dx%d with %dx%d\n", event->x, event->y, event->width, event->height);
422 Client *client = table_get(&by_child, event->window);
423 if (client == NULL) {
424 LOG("This client is not mapped, so we don't care and just tell the client that he will get its size\n");
425 Rect rect = {event->x, event->y, event->width, event->height};
426 fake_configure_notify(conn, rect, event->window);
430 if (client->fullscreen) {
431 LOG("Client is in fullscreen mode\n");
433 Rect child_rect = client->container->workspace->rect;
434 child_rect.x = child_rect.y = 0;
435 fake_configure_notify(conn, child_rect, client->child);
440 fake_absolute_configure_notify(conn, client);
446 * Configuration notifies are only handled because we need to set up ignore for the following
447 * enter notify events
450 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
451 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
453 LOG("handle_configure_event for window %08x\n", event->window);
454 LOG("event->type = %d, \n", event->response_type);
455 LOG("event->x = %d, ->y = %d, ->width = %d, ->height = %d\n", event->x, event->y, event->width, event->height);
457 /* We ignore this sequence twice because events for child and frame should be ignored */
458 add_ignore_event(event->sequence);
459 add_ignore_event(event->sequence);
461 if (event->event == root) {
462 LOG("reconfigure of the root window, need to xinerama\n");
463 /* FIXME: Somehow, this is occuring too often. Therefore, we check for 0/0,
464 but is there a better way? */
465 if (event->x == 0 && event->y == 0)
466 xinerama_requery_screens(conn);
474 * Our window decorations were unmapped. That means, the window will be killed now,
475 * so we better clean up before.
478 int handle_unmap_notify_event(void *data, xcb_connection_t *conn, xcb_unmap_notify_event_t *event) {
479 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
481 add_ignore_event(event->sequence);
483 Client *client = table_get(&by_child, event->window);
484 /* First, we need to check if the client is awaiting an unmap-request which
485 was generated by us reparenting the window. In that case, we just ignore it. */
486 if (client != NULL && client->awaiting_useless_unmap) {
487 LOG("Dropping this unmap request, it was generated by reparenting\n");
488 client->awaiting_useless_unmap = false;
492 LOG("event->window = %08x, event->event = %08x\n", event->window, event->event);
493 LOG("UnmapNotify for 0x%08x (received from 0x%08x)\n", event->window, event->event);
494 if (client == NULL) {
495 LOG("not a managed window. Ignoring.\n");
497 /* This was most likely the destroyed frame of a client which is
498 * currently being unmapped, so we add this sequence (again!) to
499 * the ignore list (enter_notify events will get sent for both,
500 * the child and its frame). */
501 add_ignore_event(event->sequence);
506 client = table_remove(&by_child, event->window);
508 if (client->name != NULL)
511 if (client->container != NULL) {
512 Container *con = client->container;
514 /* If this was the fullscreen client, we need to unset it */
515 if (client->fullscreen)
516 con->workspace->fullscreen_client = NULL;
518 /* Remove the client from the list of clients */
519 client_remove_from_container(conn, client, con);
521 /* Set focus to the last focused client in this container */
522 con->currently_focused = get_last_focused_client(conn, con, NULL);
524 /* Only if this is the active container, we need to really change focus */
525 if ((con->currently_focused != NULL) && ((con == CUR_CELL) || client->fullscreen))
526 set_focus(conn, con->currently_focused, true);
530 LOG("Removing from dock clients\n");
531 SLIST_REMOVE(&(client->workspace->screen->dock_clients), client, Client, dock_clients);
534 LOG("child of 0x%08x.\n", client->frame);
535 xcb_reparent_window(conn, client->child, root, 0, 0);
536 xcb_destroy_window(conn, client->frame);
538 table_remove(&by_parent, client->frame);
540 if (client->container != NULL) {
541 cleanup_table(conn, client->container->workspace);
542 fix_colrowspan(conn, client->container->workspace);
545 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
546 bool workspace_empty = true;
547 FOR_TABLE(client->workspace)
548 if (!CIRCLEQ_EMPTY(&(client->workspace->table[cols][rows]->clients))) {
549 workspace_empty = false;
554 TAILQ_FOREACH(screen, virtual_screens, screens)
555 if (screen->current_workspace == client->workspace->num) {
556 workspace_empty = false;
561 client->workspace->screen = NULL;
571 * Called when a window changes its title
574 int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
575 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
576 LOG("window's name changed.\n");
577 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
578 LOG("_NET_WM_NAME not specified, not changing\n");
581 Client *client = table_get(&by_child, window);
585 /* Save the old pointer to make the update atomic */
588 asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop));
589 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
590 char *ucs2_name = convert_utf8_to_ucs2(new_name, &new_len);
591 LOG("Name should change to \"%s\"\n", new_name);
594 /* Check if they are the same and don’t update if so.
595 Note the use of new_len * 2 to check all bytes as each glyph takes 2 bytes.
596 Also note the use of memcmp() instead of strncmp() because the latter stops on nullbytes,
597 but UCS-2 uses nullbytes to fill up glyphs which only use one byte. */
598 if ((new_len == client->name_len) &&
599 (client->name != NULL) &&
600 (memcmp(client->name, ucs2_name, new_len * 2) == 0)) {
601 LOG("Name did not change, not updating\n");
606 char *old_name = client->name;
607 client->name = ucs2_name;
608 client->name_len = new_len;
609 client->uses_net_wm_name = true;
613 /* If the client is a dock window, we don’t need to render anything */
617 if (client->container->mode == MODE_STACK)
618 render_container(conn, client->container);
619 else decorate_window(conn, client, client->frame, client->titlegc, 0);
626 * We handle legacy window names (titles) which are in COMPOUND_TEXT encoding. However, we
627 * just pass them along, so when containing non-ASCII characters, those will be rendering
628 * incorrectly. In order to correctly render unicode window titles in i3, an application
629 * has to set _NET_WM_NAME, which is in UTF-8 encoding.
631 * On every update, a message is put out to the user, so he may improve the situation and
632 * update applications which display filenames in their title to correctly use
633 * _NET_WM_NAME and therefore support unicode.
636 int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
637 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
638 LOG("window's name changed (legacy).\n");
639 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
640 LOG("prop == NULL\n");
643 Client *client = table_get(&by_child, window);
647 if (client->uses_net_wm_name) {
648 LOG("This client is capable of _NET_WM_NAME, ignoring legacy name\n");
652 /* Save the old pointer to make the update atomic */
654 if (asprintf(&new_name, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
655 perror("Could not get old name");
656 LOG("Could not get old name\n");
659 /* Convert it to UCS-2 here for not having to convert it later every time we want to pass it to X */
660 LOG("Name should change to \"%s\"\n", new_name);
662 /* Check if they are the same and don’t update if so. */
663 if (client->name != NULL &&
664 strlen(new_name) == strlen(client->name) &&
665 strcmp(client->name, new_name) == 0) {
666 LOG("Name did not change, not updating\n");
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->mode == MODE_STACK)
686 render_container(conn, client->container);
687 else decorate_window(conn, client, client->frame, client->titlegc, 0);
694 * Updates the client’s WM_CLASS property
697 int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
698 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
699 LOG("window class changed\n");
700 if (prop == NULL || xcb_get_property_value_length(prop) == 0) {
701 LOG("prop == NULL\n");
704 Client *client = table_get(&by_child, window);
708 if (asprintf(&new_class, "%.*s", xcb_get_property_value_length(prop), (char*)xcb_get_property_value(prop)) == -1) {
709 perror("Could not get window class");
710 LOG("Could not get window class\n");
714 LOG("changed to %s\n", new_class);
715 char *old_class = client->window_class;
716 client->window_class = new_class;
723 * Expose event means we should redraw our windows (= title bar)
726 int handle_expose_event(void *data, xcb_connection_t *conn, xcb_expose_event_t *event) {
727 /* event->count is the number of minimum remaining expose events for this window, so we
728 skip all events but the last one */
729 if (event->count != 0)
731 LOG("window = %08x\n", event->window);
733 Client *client = table_get(&by_parent, event->window);
734 if (client == NULL) {
735 /* There was no client in the table, so this is probably an expose event for
736 one of our stack_windows. */
737 struct Stack_Window *stack_win;
738 SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
739 if (stack_win->window == event->window) {
740 render_container(conn, stack_win->container);
744 /* …or one of the bars? */
746 TAILQ_FOREACH(screen, virtual_screens, screens)
747 if (screen->bar == event->window)
752 LOG("got client %s\n", client->name);
754 LOG("this is a dock\n");
758 if (client->container->mode != MODE_STACK)
759 decorate_window(conn, client, client->frame, client->titlegc, 0);
761 uint32_t background_color;
762 /* Distinguish if the window is currently focused… */
763 if (CUR_CELL->currently_focused == client)
764 background_color = get_colorpixel(conn, "#285577");
765 /* …or if it is the focused window in a not focused container */
766 else background_color = get_colorpixel(conn, "#555555");
768 /* Set foreground color to current focused color, line width to 2 */
769 uint32_t values[] = {background_color, 2};
770 xcb_change_gc(conn, client->titlegc, XCB_GC_FOREGROUND | XCB_GC_LINE_WIDTH, values);
772 /* Draw the border, the ±1 is for line width = 2 */
773 xcb_point_t points[] = {{1, 0}, /* left upper edge */
774 {1, client->rect.height-1}, /* left bottom edge */
775 {client->rect.width-1, client->rect.height-1}, /* right bottom edge */
776 {client->rect.width-1, 0}}; /* right upper edge */
777 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, client->frame, client->titlegc, 4, points);
779 /* Draw a black background */
780 xcb_change_gc_single(conn, client->titlegc, XCB_GC_FOREGROUND, get_colorpixel(conn, "#000000"));
781 xcb_rectangle_t crect = {2, 0, client->rect.width - (2 + 2), client->rect.height - 2};
782 xcb_poly_fill_rectangle(conn, client->frame, client->titlegc, 1, &crect);
789 * Handle client messages (EWMH)
792 int handle_client_message(void *data, xcb_connection_t *conn, xcb_client_message_event_t *event) {
793 LOG("client_message\n");
795 if (event->type == atoms[_NET_WM_STATE]) {
796 if (event->format != 32 || event->data.data32[1] != atoms[_NET_WM_STATE_FULLSCREEN])
801 Client *client = table_get(&by_child, event->window);
805 /* Check if the fullscreen state should be toggled */
806 if ((client->fullscreen &&
807 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
808 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
809 (!client->fullscreen &&
810 (event->data.data32[0] == _NET_WM_STATE_ADD ||
811 event->data.data32[0] == _NET_WM_STATE_TOGGLE)))
812 toggle_fullscreen(conn, client);
814 LOG("unhandled clientmessage\n");
821 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
822 xcb_atom_t atom, xcb_get_property_reply_t *property) {
823 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
824 before changing this property. */
825 LOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
830 * Handles the size hints set by a window, but currently only the part necessary for displaying
831 * clients proportionally inside their frames (mplayer for example)
833 * See ICCCM 4.1.2.3 for more details
836 int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
837 xcb_atom_t name, xcb_get_property_reply_t *reply) {
838 LOG("handle_normal_hints\n");
839 Client *client = table_get(&by_child, window);
840 if (client == NULL) {
841 LOG("No such client\n");
844 xcb_size_hints_t size_hints;
846 /* If the hints were already in this event, use them, if not, request them */
848 xcb_get_wm_size_hints_from_reply(&size_hints, reply);
850 xcb_get_wm_normal_hints_reply(conn, xcb_get_wm_normal_hints_unchecked(conn, client->child), &size_hints, NULL);
852 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
853 if (!(size_hints.flags & XCB_SIZE_HINT_P_ASPECT) ||
854 (size_hints.min_aspect_num <= 0) ||
855 (size_hints.min_aspect_den <= 0)) {
856 LOG("No aspect ratio set, ignoring\n");
860 LOG("window is %08x / %s\n", client->child, client->name);
862 int base_width = 0, base_height = 0,
863 min_width = 0, min_height = 0;
865 /* base_width/height are the desired size of the window.
866 We check if either the program-specified size or the program-specified
867 min-size is available */
868 if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
869 base_width = size_hints.base_width;
870 base_height = size_hints.base_height;
871 } else if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
872 base_width = size_hints.min_width;
873 base_height = size_hints.min_height;
876 if (size_hints.flags & XCB_SIZE_HINT_P_MIN_SIZE) {
877 min_width = size_hints.min_width;
878 min_height = size_hints.min_height;
879 } else if (size_hints.flags & XCB_SIZE_HINT_P_SIZE) {
880 min_width = size_hints.base_width;
881 min_height = size_hints.base_height;
884 double width = client->rect.width - base_width;
885 double height = client->rect.height - base_height;
886 /* Convert numerator/denominator to a double */
887 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
888 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
890 LOG("min_aspect = %f, max_aspect = %f\n", min_aspect, max_aspect);
891 LOG("width = %f, height = %f\n", width, height);
893 /* Sanity checks, this is user-input, in a way */
894 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
897 /* Check if we need to set proportional_* variables using the correct ratio */
898 if ((width / height) < min_aspect) {
899 client->proportional_width = width;
900 client->proportional_height = width / min_aspect;
901 } else if ((width / height) > max_aspect) {
902 client->proportional_width = width;
903 client->proportional_height = width / max_aspect;
906 client->force_reconfigure = true;
908 if (client->container != NULL) {
909 render_container(conn, client->container);