2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
11 #include <xcb/randr.h>
13 #include <X11/XKBlib.h>
19 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
20 since it’d trigger an infinite loop of switching between the different windows when
21 changing workspaces */
22 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
24 void add_ignore_event(const int sequence, const int response_type) {
25 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
27 event->sequence = sequence;
28 event->response_type = response_type;
29 event->added = time(NULL);
31 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
35 * Checks if the given sequence is ignored and returns true if so.
38 static bool event_is_ignored(const int sequence, const int response_type) {
39 struct Ignore_Event *event;
40 time_t now = time(NULL);
41 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
42 if ((now - event->added) > 5) {
43 struct Ignore_Event *save = event;
44 event = SLIST_NEXT(event, ignore_events);
45 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
47 } else event = SLIST_NEXT(event, ignore_events);
50 SLIST_FOREACH(event, &ignore_events, ignore_events) {
51 if (event->sequence != sequence)
54 if (event->response_type != 0 &&
55 event->response_type != response_type)
58 /* instead of removing a sequence number we better wait until it gets
59 * garbage collected. it may generate multiple events (there are multiple
60 * enter_notifies for one configure_request, for example). */
61 //SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
71 * There was a key press. We compare this key code with our bindings table and pass
72 * the bound action to parse_command().
75 static int handle_key_press(xcb_key_press_event_t *event) {
76 DLOG("Keypress %d, state raw = %d\n", event->detail, event->state);
78 /* Remove the numlock bit, all other bits are modifiers we can bind to */
79 uint16_t state_filtered = event->state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
80 DLOG("(removed numlock, state = %d)\n", state_filtered);
81 /* Only use the lower 8 bits of the state (modifier masks) so that mouse
82 * button masks are filtered out */
83 state_filtered &= 0xFF;
84 DLOG("(removed upper 8 bits, state = %d)\n", state_filtered);
86 if (xkb_current_group == XkbGroup2Index)
87 state_filtered |= BIND_MODE_SWITCH;
89 DLOG("(checked mode_switch, state %d)\n", state_filtered);
91 /* Find the binding */
92 Binding *bind = get_binding(state_filtered, event->detail);
94 /* No match? Then the user has Mode_switch enabled but does not have a
95 * specific keybinding. Fall back to the default keybindings (without
96 * Mode_switch). Makes it much more convenient for users of a hybrid
97 * layout (like us, ru). */
99 state_filtered &= ~(BIND_MODE_SWITCH);
100 DLOG("no match, new state_filtered = %d\n", state_filtered);
101 if ((bind = get_binding(state_filtered, event->detail)) == NULL) {
102 ELOG("Could not lookup key binding (modifiers %d, keycode %d)\n",
103 state_filtered, event->detail);
108 parse_cmd(bind->command);
113 * Called with coordinates of an enter_notify event or motion_notify event
114 * to check if the user crossed virtual screen boundaries and adjust the
115 * current workspace, if so.
118 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
121 /* If the user disable focus follows mouse, we have nothing to do here */
122 if (config.disable_focus_follows_mouse)
125 if ((output = get_output_containing(x, y)) == NULL) {
126 ELOG("ERROR: No such screen\n");
130 if (output->con == NULL) {
131 ELOG("ERROR: The screen is not recognized by i3 (no container associated)\n");
135 /* Focus the output on which the user moved his cursor */
136 Con *old_focused = focused;
137 con_focus(con_descend_focused(output_get_content(output->con)));
139 /* If the focus changed, we re-render to get updated decorations */
140 if (old_focused != focused)
145 * When the user moves the mouse pointer onto a window, this callback gets called.
148 static int handle_enter_notify(xcb_enter_notify_event_t *event) {
151 DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n",
152 event->event, event->mode, event->detail, event->sequence);
153 DLOG("coordinates %d, %d\n", event->event_x, event->event_y);
154 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
155 DLOG("This was not a normal notify, ignoring\n");
158 /* Some events are not interesting, because they were not generated
159 * actively by the user, but by reconfiguration of windows */
160 if (event_is_ignored(event->sequence, XCB_ENTER_NOTIFY)) {
161 DLOG("Event ignored\n");
165 bool enter_child = false;
166 /* Get container by frame or by child window */
167 if ((con = con_by_frame_id(event->event)) == NULL) {
168 con = con_by_window_id(event->event);
172 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
174 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
175 check_crossing_screen_boundary(event->root_x, event->root_y);
179 if (con->parent->type == CT_DOCKAREA) {
180 DLOG("Ignoring, this is a dock client\n");
184 /* see if the user entered the window on a certain window decoration */
185 int layout = (enter_child ? con->parent->layout : con->layout);
186 if (layout == L_DEFAULT) {
188 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
189 if (rect_contains(child->deco_rect, event->event_x, event->event_y)) {
190 LOG("using child %p / %s instead!\n", child, child->name);
197 if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
198 /* This can happen when a client gets assigned to a different workspace than
199 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
200 * an enter_notify will follow. */
201 DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
206 if (config.disable_focus_follows_mouse)
209 con_focus(con_descend_focused(con));
216 * When the user moves the mouse but does not change the active window
217 * (e.g. when having no windows opened but moving mouse on the root screen
218 * and crossing virtual screen boundaries), this callback gets called.
221 static int handle_motion_notify(xcb_motion_notify_event_t *event) {
222 /* Skip events where the pointer was over a child window, we are only
223 * interested in events on the root window. */
224 if (event->child != 0)
228 if ((con = con_by_frame_id(event->event)) == NULL) {
229 check_crossing_screen_boundary(event->root_x, event->root_y);
233 if (config.disable_focus_follows_mouse)
236 if (con->layout != L_DEFAULT)
239 /* see over which rect the user is */
241 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
242 if (!rect_contains(current->deco_rect, event->event_x, event->event_y))
245 /* We found the rect, let’s see if this window is focused */
246 if (TAILQ_FIRST(&(con->focus_head)) == current)
250 x_push_changes(croot);
258 * Called when the keyboard mapping changes (for example by using Xmodmap),
259 * we need to update our key bindings then (re-translate symbols).
262 static int handle_mapping_notify(xcb_mapping_notify_event_t *event) {
263 if (event->request != XCB_MAPPING_KEYBOARD &&
264 event->request != XCB_MAPPING_MODIFIER)
267 DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
268 xcb_refresh_keyboard_mapping(keysyms, event);
270 xcb_get_numlock_mask(conn);
272 ungrab_all_keys(conn);
274 grab_all_keys(conn, false);
280 * A new window appeared on the screen (=was mapped), so let’s manage it.
283 static int handle_map_request(xcb_map_request_event_t *event) {
284 xcb_get_window_attributes_cookie_t cookie;
286 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
288 DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
289 add_ignore_event(event->sequence, 0);
291 manage_window(event->window, cookie, false);
292 x_push_changes(croot);
297 * Configure requests are received when the application wants to resize windows on their own.
299 * We generate a synthethic configure notify event to signalize the client its "new" position.
302 static int handle_configure_request(xcb_configure_request_event_t *event) {
305 DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
306 event->window, event->x, event->y, event->width, event->height);
308 /* For unmanaged windows, we just execute the configure request. As soon as
309 * it gets mapped, we will take over anyways. */
310 if ((con = con_by_window_id(event->window)) == NULL) {
311 DLOG("Configure request for unmanaged window, can do that.\n");
316 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
317 if (event->value_mask & mask_member) { \
318 mask |= mask_member; \
319 values[c++] = event->event_member; \
323 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
324 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
325 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
326 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
327 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
328 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
329 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
331 xcb_configure_window(conn, event->window, mask, values);
337 DLOG("Configure request!\n");
338 if (con_is_floating(con) && con_is_leaf(con)) {
339 /* find the height for the decorations */
340 int deco_height = config.font.height + 5;
341 /* we actually need to apply the size/position changes to the *parent*
343 Rect bsr = con_border_style_rect(con);
344 if (con->border_style == BS_NORMAL) {
345 bsr.y += deco_height;
346 bsr.height -= deco_height;
349 DLOG("Container is a floating leaf node, will do that.\n");
350 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
351 con->rect.x = event->x + (-1) * bsr.x;
352 DLOG("proposed x = %d, new x is %d\n", event->x, con->rect.x);
354 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
355 con->rect.y = event->y + (-1) * bsr.y;
356 DLOG("proposed y = %d, new y is %d\n", event->y, con->rect.y);
358 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
359 con->rect.width = event->width + (-1) * bsr.width;
360 DLOG("proposed width = %d, new width is %d\n", event->width, con->rect.width);
362 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
363 con->rect.height = event->height + (-1) * bsr.height;
364 DLOG("proposed height = %d, new height is %d\n", event->height, con->rect.height);
369 fake_absolute_configure_notify(con);
373 /* Dock clients can be reconfigured in their height */
375 DLOG("Reconfiguring height of this dock client\n");
377 if (!(event->value_mask & XCB_CONFIG_WINDOW_HEIGHT)) {
378 DLOG("Ignoring configure request, no height given\n");
382 client->desired_height = event->height;
383 render_workspace(conn, c_ws->output, c_ws);
389 if (client->fullscreen) {
390 DLOG("Client is in fullscreen mode\n");
392 Rect child_rect = client->container->workspace->rect;
393 child_rect.x = child_rect.y = 0;
394 fake_configure_notify(conn, child_rect, client->child);
399 fake_absolute_configure_notify(conn, client);
407 * Configuration notifies are only handled because we need to set up ignore for
408 * the following enter notify events.
411 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
412 DLOG("configure_event, sequence %d\n", event->sequence);
413 /* We ignore this sequence twice because events for child and frame should be ignored */
414 add_ignore_event(event->sequence);
415 add_ignore_event(event->sequence);
422 * Gets triggered upon a RandR screen change event, that is when the user
423 * changes the screen configuration in any way (mode, position, …)
426 static int handle_screen_change(xcb_generic_event_t *e) {
427 DLOG("RandR screen change\n");
429 randr_query_outputs();
431 ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
437 * Our window decorations were unmapped. That means, the window will be killed
438 * now, so we better clean up before.
441 static int handle_unmap_notify_event(xcb_unmap_notify_event_t *event) {
442 /* we need to ignore EnterNotify events which will be generated because a
443 * different window is visible now */
444 add_ignore_event(event->sequence, XCB_ENTER_NOTIFY);
446 DLOG("UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
447 Con *con = con_by_window_id(event->window);
449 /* This could also be an UnmapNotify for the frame. We need to
450 * decrement the ignore_unmap counter. */
451 con = con_by_frame_id(event->window);
453 LOG("Not a managed window, ignoring UnmapNotify event\n");
456 if (con->ignore_unmap > 0)
458 DLOG("ignore_unmap = %d for frame of container %p\n", con->ignore_unmap, con);
462 if (con->ignore_unmap > 0) {
463 DLOG("ignore_unmap = %d, dec\n", con->ignore_unmap);
468 tree_close(con, DONT_KILL_WINDOW, false);
470 x_push_changes(croot);
474 if (client == NULL) {
475 DLOG("not a managed window. Ignoring.\n");
477 /* This was most likely the destroyed frame of a client which is
478 * currently being unmapped, so we add this sequence (again!) to
479 * the ignore list (enter_notify events will get sent for both,
480 * the child and its frame). */
481 add_ignore_event(event->sequence);
489 /* Let’s see how many clients there are left on the workspace to delete it if it’s empty */
490 bool workspace_empty = SLIST_EMPTY(&(client->workspace->focus_stack));
491 bool workspace_focused = (c_ws == client->workspace);
492 Client *to_focus = (!workspace_empty ? SLIST_FIRST(&(client->workspace->focus_stack)) : NULL);
494 /* If this workspace is currently visible, we don’t delete it */
495 if (workspace_is_visible(client->workspace))
496 workspace_empty = false;
498 if (workspace_empty) {
499 client->workspace->output = NULL;
500 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
503 /* Remove the urgency flag if set */
504 client->urgent = false;
505 workspace_update_urgent_flag(client->workspace);
514 * A destroy notify event is sent when the window is not unmapped, but
515 * immediately destroyed (for example when starting a window and immediately
516 * killing the program which started it).
518 * We just pass on the event to the unmap notify handler (by copying the
519 * important fields in the event data structure).
522 static int handle_destroy_notify_event(xcb_destroy_notify_event_t *event) {
523 DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
525 xcb_unmap_notify_event_t unmap;
526 unmap.sequence = event->sequence;
527 unmap.event = event->event;
528 unmap.window = event->window;
530 return handle_unmap_notify_event(&unmap);
534 * Called when a window changes its title
537 static int handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
538 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
540 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
543 window_update_name(con->window, prop, false);
545 x_push_changes(croot);
551 * Handles legacy window name updates (WM_NAME), see also src/window.c,
552 * window_update_name_legacy().
555 static int handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
556 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
558 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
561 window_update_name_legacy(con->window, prop, false);
563 x_push_changes(croot);
569 * Updates the client’s WM_CLASS property
572 static int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
573 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
575 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
578 window_update_class(con->window, prop, false);
584 * Expose event means we should redraw our windows (= title bar)
587 static int handle_expose_event(xcb_expose_event_t *event) {
590 /* event->count is the number of minimum remaining expose events for this
591 * window, so we skip all events but the last one */
592 if (event->count != 0)
595 DLOG("window = %08x\n", event->window);
597 if ((parent = con_by_frame_id(event->window)) == NULL) {
598 LOG("expose event for unknown window, ignoring\n");
603 x_draw_decoration(parent);
605 TAILQ_FOREACH(con, &(parent->nodes_head), nodes) {
606 DLOG("expose for con %p / %s\n", con, con->name);
608 x_draw_decoration(con);
611 /* We also need to render the decorations of other Cons nearby the Con
612 * itself to not get overlapping decorations */
613 TAILQ_FOREACH(con, &(parent->parent->nodes_head), nodes) {
614 DLOG("expose for con %p / %s\n", con, con->name);
616 x_draw_decoration(con);
624 * Handle client messages (EWMH)
627 static int handle_client_message(xcb_client_message_event_t *event) {
628 LOG("ClientMessage for window 0x%08x\n", event->window);
629 if (event->type == A__NET_WM_STATE) {
630 if (event->format != 32 || event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN) {
631 DLOG("atom in clientmessage is %d, fullscreen is %d\n",
632 event->data.data32[1], A__NET_WM_STATE_FULLSCREEN);
633 DLOG("not about fullscreen atom\n");
637 Con *con = con_by_window_id(event->window);
639 DLOG("Could not get window for client message\n");
643 /* Check if the fullscreen state should be toggled */
644 if ((con->fullscreen_mode != CF_NONE &&
645 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
646 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
647 (con->fullscreen_mode == CF_NONE &&
648 (event->data.data32[0] == _NET_WM_STATE_ADD ||
649 event->data.data32[0] == _NET_WM_STATE_TOGGLE))) {
650 DLOG("toggling fullscreen\n");
651 con_toggle_fullscreen(con);
655 x_push_changes(croot);
657 ELOG("unhandled clientmessage\n");
665 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
666 xcb_atom_t atom, xcb_get_property_reply_t *property) {
667 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
668 before changing this property. */
669 ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
675 * Handles the size hints set by a window, but currently only the part necessary for displaying
676 * clients proportionally inside their frames (mplayer for example)
678 * See ICCCM 4.1.2.3 for more details
681 static int handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
682 xcb_atom_t name, xcb_get_property_reply_t *reply) {
683 Con *con = con_by_window_id(window);
685 DLOG("Received WM_NORMAL_HINTS for unknown client\n");
689 xcb_size_hints_t size_hints;
691 //CLIENT_LOG(client);
693 /* If the hints were already in this event, use them, if not, request them */
695 xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
697 xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, con->window->id), &size_hints, NULL);
699 if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
700 // TODO: Minimum size is not yet implemented
701 DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
704 bool changed = false;
705 if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
706 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
707 if (con->width_increment != size_hints.width_inc) {
708 con->width_increment = size_hints.width_inc;
711 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
712 if (con->height_increment != size_hints.height_inc) {
713 con->height_increment = size_hints.height_inc;
718 DLOG("resize increments changed\n");
721 int base_width = 0, base_height = 0;
723 /* base_width/height are the desired size of the window.
724 We check if either the program-specified size or the program-specified
725 min-size is available */
726 if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
727 base_width = size_hints.base_width;
728 base_height = size_hints.base_height;
729 } else if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
730 /* TODO: is this right? icccm says not */
731 base_width = size_hints.min_width;
732 base_height = size_hints.min_height;
735 if (base_width != con->base_width ||
736 base_height != con->base_height) {
737 con->base_width = base_width;
738 con->base_height = base_height;
739 DLOG("client's base_height changed to %d\n", base_height);
740 DLOG("client's base_width changed to %d\n", base_width);
744 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
745 if (!(size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT) ||
746 (size_hints.min_aspect_num <= 0) ||
747 (size_hints.min_aspect_den <= 0)) {
748 goto render_and_return;
751 /* XXX: do we really use rect here, not window_rect? */
752 double width = con->rect.width - base_width;
753 double height = con->rect.height - base_height;
754 /* Convert numerator/denominator to a double */
755 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
756 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
758 DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
759 DLOG("width = %f, height = %f\n", width, height);
761 /* Sanity checks, this is user-input, in a way */
762 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
763 goto render_and_return;
765 /* Check if we need to set proportional_* variables using the correct ratio */
766 if ((width / height) < min_aspect) {
767 if (con->proportional_width != width ||
768 con->proportional_height != (width / min_aspect)) {
769 con->proportional_width = width;
770 con->proportional_height = width / min_aspect;
773 } else if ((width / height) > max_aspect) {
774 if (con->proportional_width != width ||
775 con->proportional_height != (width / max_aspect)) {
776 con->proportional_width = width;
777 con->proportional_height = width / max_aspect;
780 } else goto render_and_return;
789 * Handles the WM_HINTS property for extracting the urgency state of the window.
792 static int handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
793 xcb_atom_t name, xcb_get_property_reply_t *reply) {
794 Con *con = con_by_window_id(window);
796 DLOG("Received WM_HINTS for unknown client\n");
800 xcb_icccm_wm_hints_t hints;
803 if (!xcb_icccm_get_wm_hints_from_reply(&hints, reply))
806 if (!xcb_icccm_get_wm_hints_reply(conn, xcb_icccm_get_wm_hints_unchecked(conn, con->window->id), &hints, NULL))
810 if (!con->urgent && focused == con) {
811 DLOG("Ignoring urgency flag for current client\n");
815 /* Update the flag on the client directly */
816 con->urgent = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
818 LOG("Urgency flag changed to %d\n", con->urgent);
821 /* Set the urgency flag on the workspace, if a workspace could be found
822 * (for dock clients, that is not the case). */
823 if ((ws = con_get_workspace(con)) != NULL)
824 workspace_update_urgent_flag(ws);
829 /* If the workspace this client is on is not visible, we need to redraw
830 * the workspace bar */
831 if (!workspace_is_visible(client->workspace)) {
832 Output *output = client->workspace->output;
833 render_workspace(conn, output, output->current_workspace);
842 * Handles the transient for hints set by a window, signalizing that this window is a popup window
843 * for some other window.
845 * See ICCCM 4.1.2.6 for more details
848 static int handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
849 xcb_atom_t name, xcb_get_property_reply_t *prop) {
852 if ((con = con_by_window_id(window)) == NULL || con->window == NULL) {
853 DLOG("No such window\n");
858 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
859 false, window, A_WM_TRANSIENT_FOR, A_WINDOW, 0, 32), NULL);
864 window_update_transient_for(con->window, prop);
866 // TODO: put window in floating mode if con->window->transient_for != XCB_NONE:
868 if (client->floating == FLOATING_AUTO_OFF) {
869 DLOG("This is a popup window, putting into floating\n");
870 toggle_floating_mode(conn, client, true);
878 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
879 * toolwindow (or similar) and to which window it belongs (logical parent).
882 static int handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
883 xcb_atom_t name, xcb_get_property_reply_t *prop) {
885 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
889 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
890 false, window, A_WM_CLIENT_LEADER, A_WINDOW, 0, 32), NULL);
895 window_update_leader(con->window, prop);
901 * Handles FocusIn events which are generated by clients (i3’s focus changes
902 * don’t generate FocusIn events due to a different EventMask) and updates the
903 * decorations accordingly.
906 static int handle_focus_in(xcb_focus_in_event_t *event) {
907 DLOG("focus change in, for window 0x%08x\n", event->event);
909 if ((con = con_by_window_id(event->event)) == NULL || con->window == NULL)
911 DLOG("That is con %p / %s\n", con, con->name);
913 if (event->mode == XCB_NOTIFY_MODE_GRAB ||
914 event->mode == XCB_NOTIFY_MODE_UNGRAB) {
915 DLOG("FocusIn event for grab/ungrab, ignoring\n");
919 if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
920 DLOG("notify detail is pointer, ignoring this event\n");
924 if (focused_id == event->event) {
925 DLOG("focus matches the currently focused window, not doing anything\n");
929 DLOG("focus is different, updating decorations\n");
931 /* We update focused_id because we don’t need to set focus again */
932 focused_id = event->event;
933 x_push_changes(croot);
937 typedef int (*cb_property_handler_t)(void *data, xcb_connection_t *c, uint8_t state, xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *property);
939 struct property_handler_t {
942 cb_property_handler_t cb;
945 static struct property_handler_t property_handlers[] = {
946 { 0, 128, handle_windowname_change },
947 { 0, UINT_MAX, handle_hints },
948 { 0, 128, handle_windowname_change_legacy },
949 { 0, UINT_MAX, handle_normal_hints },
950 { 0, UINT_MAX, handle_clientleader_change },
951 { 0, UINT_MAX, handle_transient_for }
953 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
956 * Sets the appropriate atoms for the property handlers after the atoms were
960 void property_handlers_init() {
961 property_handlers[0].atom = A__NET_WM_NAME;
962 property_handlers[1].atom = A_WM_HINTS;
963 property_handlers[2].atom = A_WM_NAME;
964 property_handlers[3].atom = A_WM_NORMAL_HINTS;
965 property_handlers[4].atom = A_WM_CLIENT_LEADER;
966 property_handlers[5].atom = A_WM_TRANSIENT_FOR;
969 static int property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
970 struct property_handler_t *handler = NULL;
971 xcb_get_property_reply_t *propr = NULL;
974 for (int c = 0; c < sizeof(property_handlers) / sizeof(struct property_handler_t); c++) {
975 if (property_handlers[c].atom != atom)
978 handler = &property_handlers[c];
982 if (handler == NULL) {
983 DLOG("Unhandled property notify for atom %d (0x%08x)\n", atom, atom);
987 if (state != XCB_PROPERTY_DELETE) {
988 xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->long_len);
989 propr = xcb_get_property_reply(conn, cookie, 0);
992 ret = handler->cb(NULL, conn, state, window, atom, propr);
998 * Takes an xcb_generic_event_t and calls the appropriate handler, based on the
1002 void handle_event(int type, xcb_generic_event_t *event) {
1003 if (randr_base > -1 &&
1004 type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
1005 handle_screen_change(event);
1011 handle_key_press((xcb_key_press_event_t*)event);
1014 case XCB_BUTTON_PRESS:
1015 handle_button_press((xcb_button_press_event_t*)event);
1018 case XCB_MAP_REQUEST:
1019 handle_map_request((xcb_map_request_event_t*)event);
1022 case XCB_UNMAP_NOTIFY:
1023 handle_unmap_notify_event((xcb_unmap_notify_event_t*)event);
1026 case XCB_DESTROY_NOTIFY:
1027 handle_destroy_notify_event((xcb_destroy_notify_event_t*)event);
1031 handle_expose_event((xcb_expose_event_t*)event);
1034 case XCB_MOTION_NOTIFY:
1035 handle_motion_notify((xcb_motion_notify_event_t*)event);
1038 /* Enter window = user moved his mouse over the window */
1039 case XCB_ENTER_NOTIFY:
1040 handle_enter_notify((xcb_enter_notify_event_t*)event);
1043 /* Client message are sent to the root window. The only interesting
1044 * client message for us is _NET_WM_STATE, we honour
1045 * _NET_WM_STATE_FULLSCREEN */
1046 case XCB_CLIENT_MESSAGE:
1047 handle_client_message((xcb_client_message_event_t*)event);
1050 /* Configure request = window tried to change size on its own */
1051 case XCB_CONFIGURE_REQUEST:
1052 handle_configure_request((xcb_configure_request_event_t*)event);
1055 /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
1056 case XCB_MAPPING_NOTIFY:
1057 handle_mapping_notify((xcb_mapping_notify_event_t*)event);
1061 handle_focus_in((xcb_focus_in_event_t*)event);
1064 case XCB_PROPERTY_NOTIFY:
1065 DLOG("Property notify\n");
1066 xcb_property_notify_event_t *e = (xcb_property_notify_event_t*)event;
1067 property_notify(e->state, e->window, e->atom);
1071 DLOG("Unhandled event of type %d\n", type);