3 * vim:ts=4:sw=4:expandtab
5 * i3 - an improved dynamic tiling window manager
6 * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
8 * handlers.c: Small handlers for various events (keypresses, focus changes,
16 #include <xcb/randr.h>
17 #include <X11/XKBlib.h>
18 #define SN_API_NOT_YET_FROZEN 1
19 #include <libsn/sn-monitor.h>
23 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
24 since it’d trigger an infinite loop of switching between the different windows when
25 changing workspaces */
26 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
29 * Adds the given sequence to the list of events which are ignored.
30 * If this ignore should only affect a specific response_type, pass
31 * response_type, otherwise, pass -1.
33 * Every ignored sequence number gets garbage collected after 5 seconds.
36 void add_ignore_event(const int sequence, const int response_type) {
37 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
39 event->sequence = sequence;
40 event->response_type = response_type;
41 event->added = time(NULL);
43 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
47 * Checks if the given sequence is ignored and returns true if so.
50 bool event_is_ignored(const int sequence, const int response_type) {
51 struct Ignore_Event *event;
52 time_t now = time(NULL);
53 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
54 if ((now - event->added) > 5) {
55 struct Ignore_Event *save = event;
56 event = SLIST_NEXT(event, ignore_events);
57 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
59 } else event = SLIST_NEXT(event, ignore_events);
62 SLIST_FOREACH(event, &ignore_events, ignore_events) {
63 if (event->sequence != sequence)
66 if (event->response_type != -1 &&
67 event->response_type != response_type)
70 /* instead of removing a sequence number we better wait until it gets
71 * garbage collected. it may generate multiple events (there are multiple
72 * enter_notifies for one configure_request, for example). */
73 //SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
82 * Called with coordinates of an enter_notify event or motion_notify event
83 * to check if the user crossed virtual screen boundaries and adjust the
84 * current workspace, if so.
87 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
90 /* If the user disable focus follows mouse, we have nothing to do here */
91 if (config.disable_focus_follows_mouse)
94 if ((output = get_output_containing(x, y)) == NULL) {
95 ELOG("ERROR: No such screen\n");
99 if (output->con == NULL) {
100 ELOG("ERROR: The screen is not recognized by i3 (no container associated)\n");
104 /* Focus the output on which the user moved his cursor */
105 Con *old_focused = focused;
106 Con *next = con_descend_focused(output_get_content(output->con));
107 /* Since we are switching outputs, this *must* be a different workspace, so
108 * call workspace_show() */
109 workspace_show(con_get_workspace(next));
112 /* If the focus changed, we re-render to get updated decorations */
113 if (old_focused != focused)
118 * When the user moves the mouse pointer onto a window, this callback gets called.
121 static void handle_enter_notify(xcb_enter_notify_event_t *event) {
124 last_timestamp = event->time;
126 DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n",
127 event->event, event->mode, event->detail, event->sequence);
128 DLOG("coordinates %d, %d\n", event->event_x, event->event_y);
129 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
130 DLOG("This was not a normal notify, ignoring\n");
133 /* Some events are not interesting, because they were not generated
134 * actively by the user, but by reconfiguration of windows */
135 if (event_is_ignored(event->sequence, XCB_ENTER_NOTIFY)) {
136 DLOG("Event ignored\n");
140 bool enter_child = false;
141 /* Get container by frame or by child window */
142 if ((con = con_by_frame_id(event->event)) == NULL) {
143 con = con_by_window_id(event->event);
147 /* If not, then the user moved his cursor to the root window. In that case, we adjust c_ws */
149 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
150 check_crossing_screen_boundary(event->root_x, event->root_y);
154 if (con->parent->type == CT_DOCKAREA) {
155 DLOG("Ignoring, this is a dock client\n");
159 /* see if the user entered the window on a certain window decoration */
160 int layout = (enter_child ? con->parent->layout : con->layout);
161 if (layout == L_DEFAULT) {
163 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
164 if (rect_contains(child->deco_rect, event->event_x, event->event_y)) {
165 LOG("using child %p / %s instead!\n", child, child->name);
172 if (client->workspace != c_ws && client->workspace->output == c_ws->output) {
173 /* This can happen when a client gets assigned to a different workspace than
174 * the current one (see src/mainx.c:reparent_window). Shortly after it was created,
175 * an enter_notify will follow. */
176 DLOG("enter_notify for a client on a different workspace but the same screen, ignoring\n");
181 if (config.disable_focus_follows_mouse)
184 /* Get the currently focused workspace to check if the focus change also
185 * involves changing workspaces. If so, we need to call workspace_show() to
186 * correctly update state and send the IPC event. */
187 Con *ws = con_get_workspace(con);
188 if (ws != con_get_workspace(focused))
191 focused_id = XCB_NONE;
192 con_focus(con_descend_focused(con));
199 * When the user moves the mouse but does not change the active window
200 * (e.g. when having no windows opened but moving mouse on the root screen
201 * and crossing virtual screen boundaries), this callback gets called.
204 static void handle_motion_notify(xcb_motion_notify_event_t *event) {
206 last_timestamp = event->time;
208 /* Skip events where the pointer was over a child window, we are only
209 * interested in events on the root window. */
210 if (event->child != 0)
214 if ((con = con_by_frame_id(event->event)) == NULL) {
215 DLOG("MotionNotify for an unknown container, checking if it crosses screen boundaries.\n");
216 check_crossing_screen_boundary(event->root_x, event->root_y);
220 if (config.disable_focus_follows_mouse)
223 if (con->layout != L_DEFAULT)
226 /* see over which rect the user is */
228 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
229 if (!rect_contains(current->deco_rect, event->event_x, event->event_y))
232 /* We found the rect, let’s see if this window is focused */
233 if (TAILQ_FIRST(&(con->focus_head)) == current)
237 x_push_changes(croot);
245 * Called when the keyboard mapping changes (for example by using Xmodmap),
246 * we need to update our key bindings then (re-translate symbols).
249 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
250 if (event->request != XCB_MAPPING_KEYBOARD &&
251 event->request != XCB_MAPPING_MODIFIER)
254 DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
255 xcb_refresh_keyboard_mapping(keysyms, event);
257 xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
259 ungrab_all_keys(conn);
261 grab_all_keys(conn, false);
267 * A new window appeared on the screen (=was mapped), so let’s manage it.
270 static void handle_map_request(xcb_map_request_event_t *event) {
271 xcb_get_window_attributes_cookie_t cookie;
273 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
275 DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
276 add_ignore_event(event->sequence, -1);
278 manage_window(event->window, cookie, false);
279 x_push_changes(croot);
284 * Configure requests are received when the application wants to resize windows
287 * We generate a synthethic configure notify event to signalize the client its
291 static void handle_configure_request(xcb_configure_request_event_t *event) {
294 DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
295 event->window, event->x, event->y, event->width, event->height);
297 /* For unmanaged windows, we just execute the configure request. As soon as
298 * it gets mapped, we will take over anyways. */
299 if ((con = con_by_window_id(event->window)) == NULL) {
300 DLOG("Configure request for unmanaged window, can do that.\n");
305 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
306 if (event->value_mask & mask_member) { \
307 mask |= mask_member; \
308 values[c++] = event->event_member; \
312 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
313 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
314 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
315 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
316 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
317 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
318 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
320 xcb_configure_window(conn, event->window, mask, values);
326 DLOG("Configure request!\n");
327 if (con_is_floating(con) && con_is_leaf(con)) {
328 /* find the height for the decorations */
329 int deco_height = config.font.height + 5;
330 /* we actually need to apply the size/position changes to the *parent*
332 Rect bsr = con_border_style_rect(con);
333 if (con->border_style == BS_NORMAL) {
334 bsr.y += deco_height;
335 bsr.height -= deco_height;
337 Con *floatingcon = con->parent;
339 Rect newrect = floatingcon->rect;
341 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
342 newrect.x = event->x + (-1) * bsr.x;
343 DLOG("proposed x = %d, new x is %d\n", event->x, newrect.x);
345 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
346 newrect.y = event->y + (-1) * bsr.y;
347 DLOG("proposed y = %d, new y is %d\n", event->y, newrect.y);
349 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
350 newrect.width = event->width + (-1) * bsr.width;
351 newrect.width += con->border_width * 2;
352 DLOG("proposed width = %d, new width is %d (x11 border %d)\n",
353 event->width, newrect.width, con->border_width);
355 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
356 newrect.height = event->height + (-1) * bsr.height;
357 newrect.height += con->border_width * 2;
358 DLOG("proposed height = %d, new height is %d (x11 border %d)\n",
359 event->height, newrect.height, con->border_width);
362 DLOG("Container is a floating leaf node, will do that.\n");
363 floating_reposition(floatingcon, newrect);
367 /* Dock windows can be reconfigured in their height */
368 if (con->parent && con->parent->type == CT_DOCKAREA) {
369 DLOG("Dock window, only height reconfiguration allowed\n");
370 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
371 DLOG("Height given, changing\n");
373 con->geometry.height = event->height;
378 fake_absolute_configure_notify(con);
385 * Configuration notifies are only handled because we need to set up ignore for
386 * the following enter notify events.
389 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
390 DLOG("configure_event, sequence %d\n", event->sequence);
391 /* We ignore this sequence twice because events for child and frame should be ignored */
392 add_ignore_event(event->sequence);
393 add_ignore_event(event->sequence);
400 * Gets triggered upon a RandR screen change event, that is when the user
401 * changes the screen configuration in any way (mode, position, …)
404 static void handle_screen_change(xcb_generic_event_t *e) {
405 DLOG("RandR screen change\n");
407 randr_query_outputs();
409 scratchpad_fix_resolution();
411 ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
417 * Our window decorations were unmapped. That means, the window will be killed
418 * now, so we better clean up before.
421 static void handle_unmap_notify_event(xcb_unmap_notify_event_t *event) {
422 DLOG("UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
423 xcb_get_input_focus_cookie_t cookie;
424 Con *con = con_by_window_id(event->window);
426 /* This could also be an UnmapNotify for the frame. We need to
427 * decrement the ignore_unmap counter. */
428 con = con_by_frame_id(event->window);
430 LOG("Not a managed window, ignoring UnmapNotify event\n");
434 if (con->ignore_unmap > 0)
436 /* See the end of this function. */
437 cookie = xcb_get_input_focus(conn);
438 DLOG("ignore_unmap = %d for frame of container %p\n", con->ignore_unmap, con);
442 /* See the end of this function. */
443 cookie = xcb_get_input_focus(conn);
445 if (con->ignore_unmap > 0) {
446 DLOG("ignore_unmap = %d, dec\n", con->ignore_unmap);
451 tree_close(con, DONT_KILL_WINDOW, false, false);
453 x_push_changes(croot);
456 /* If the client (as opposed to i3) destroyed or unmapped a window, an
457 * EnterNotify event will follow (indistinguishable from an EnterNotify
458 * event caused by moving your mouse), causing i3 to set focus to whichever
459 * window is now visible.
461 * In a complex stacked or tabbed layout (take two v-split containers in a
462 * tabbed container), when the bottom window in tab2 is closed, the bottom
463 * window of tab1 is visible instead. X11 will thus send an EnterNotify
464 * event for the bottom window of tab1, while the focus should be set to
465 * the remaining window of tab2.
467 * Therefore, we ignore all EnterNotify events which have the same sequence
468 * as an UnmapNotify event. */
469 add_ignore_event(event->sequence, XCB_ENTER_NOTIFY);
471 /* Since we just ignored the sequence of this UnmapNotify, we want to make
472 * sure that following events use a different sequence. When putting xterm
473 * into fullscreen and moving the pointer to a different window, without
474 * using GetInputFocus, subsequent (legitimate) EnterNotify events arrived
475 * with the same sequence and thus were ignored (see ticket #609). */
476 free(xcb_get_input_focus_reply(conn, cookie, NULL));
480 * A destroy notify event is sent when the window is not unmapped, but
481 * immediately destroyed (for example when starting a window and immediately
482 * killing the program which started it).
484 * We just pass on the event to the unmap notify handler (by copying the
485 * important fields in the event data structure).
488 static void handle_destroy_notify_event(xcb_destroy_notify_event_t *event) {
489 DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
491 xcb_unmap_notify_event_t unmap;
492 unmap.sequence = event->sequence;
493 unmap.event = event->event;
494 unmap.window = event->window;
496 handle_unmap_notify_event(&unmap);
500 * Called when a window changes its title
503 static bool handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
504 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
506 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
509 window_update_name(con->window, prop, false);
511 x_push_changes(croot);
517 * Handles legacy window name updates (WM_NAME), see also src/window.c,
518 * window_update_name_legacy().
521 static bool handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
522 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
524 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
527 window_update_name_legacy(con->window, prop, false);
529 x_push_changes(croot);
535 * Called when a window changes its WM_WINDOW_ROLE.
538 static bool handle_windowrole_change(void *data, xcb_connection_t *conn, uint8_t state,
539 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
541 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
544 window_update_role(con->window, prop, false);
551 * Updates the client’s WM_CLASS property
554 static int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
555 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
557 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
560 window_update_class(con->window, prop, false);
567 * Expose event means we should redraw our windows (= title bar)
570 static void handle_expose_event(xcb_expose_event_t *event) {
573 DLOG("window = %08x\n", event->window);
575 if ((parent = con_by_frame_id(event->window)) == NULL) {
576 LOG("expose event for unknown window, ignoring\n");
580 /* Since we render to our pixmap on every change anyways, expose events
581 * only tell us that the X server lost (parts of) the window contents. We
582 * can handle that by copying the appropriate part from our pixmap to the
584 xcb_copy_area(conn, parent->pixmap, parent->frame, parent->pm_gc,
585 event->x, event->y, event->x, event->y,
586 event->width, event->height);
593 * Handle client messages (EWMH)
596 static void handle_client_message(xcb_client_message_event_t *event) {
597 /* If this is a startup notification ClientMessage, the library will handle
598 * it and call our monitor_event() callback. */
599 if (sn_xcb_display_process_event(sndisplay, (xcb_generic_event_t*)event))
602 LOG("ClientMessage for window 0x%08x\n", event->window);
603 if (event->type == A__NET_WM_STATE) {
604 if (event->format != 32 || event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN) {
605 DLOG("atom in clientmessage is %d, fullscreen is %d\n",
606 event->data.data32[1], A__NET_WM_STATE_FULLSCREEN);
607 DLOG("not about fullscreen atom\n");
611 Con *con = con_by_window_id(event->window);
613 DLOG("Could not get window for client message\n");
617 /* Check if the fullscreen state should be toggled */
618 if ((con->fullscreen_mode != CF_NONE &&
619 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
620 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
621 (con->fullscreen_mode == CF_NONE &&
622 (event->data.data32[0] == _NET_WM_STATE_ADD ||
623 event->data.data32[0] == _NET_WM_STATE_TOGGLE))) {
624 DLOG("toggling fullscreen\n");
625 con_toggle_fullscreen(con, CF_OUTPUT);
629 } else if (event->type == A__NET_ACTIVE_WINDOW) {
630 DLOG("_NET_ACTIVE_WINDOW: Window 0x%08x should be activated\n", event->window);
631 Con *con = con_by_window_id(event->window);
633 DLOG("Could not get window for client message\n");
639 } else if (event->type == A_I3_SYNC) {
640 xcb_window_t window = event->data.data32[0];
641 uint32_t rnd = event->data.data32[1];
642 DLOG("[i3 sync protocol] Sending random value %d back to X11 window 0x%08x\n", rnd, window);
644 void *reply = scalloc(32);
645 xcb_client_message_event_t *ev = reply;
647 ev->response_type = XCB_CLIENT_MESSAGE;
649 ev->type = A_I3_SYNC;
651 ev->data.data32[0] = window;
652 ev->data.data32[1] = rnd;
654 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
658 DLOG("unhandled clientmessage\n");
664 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
665 xcb_atom_t atom, xcb_get_property_reply_t *property) {
666 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
667 before changing this property. */
668 ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
674 * Handles the size hints set by a window, but currently only the part necessary for displaying
675 * clients proportionally inside their frames (mplayer for example)
677 * See ICCCM 4.1.2.3 for more details
680 static bool handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
681 xcb_atom_t name, xcb_get_property_reply_t *reply) {
682 Con *con = con_by_window_id(window);
684 DLOG("Received WM_NORMAL_HINTS for unknown client\n");
688 xcb_size_hints_t size_hints;
690 //CLIENT_LOG(client);
692 /* If the hints were already in this event, use them, if not, request them */
694 xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
696 xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, con->window->id), &size_hints, NULL);
698 if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
699 // TODO: Minimum size is not yet implemented
700 DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
703 bool changed = false;
704 if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
705 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
706 if (con->width_increment != size_hints.width_inc) {
707 con->width_increment = size_hints.width_inc;
710 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
711 if (con->height_increment != size_hints.height_inc) {
712 con->height_increment = size_hints.height_inc;
717 DLOG("resize increments changed\n");
720 int base_width = 0, base_height = 0;
722 /* base_width/height are the desired size of the window.
723 We check if either the program-specified size or the program-specified
724 min-size is available */
725 if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
726 base_width = size_hints.base_width;
727 base_height = size_hints.base_height;
728 } else if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
729 /* TODO: is this right? icccm says not */
730 base_width = size_hints.min_width;
731 base_height = size_hints.min_height;
734 if (base_width != con->base_width ||
735 base_height != con->base_height) {
736 con->base_width = base_width;
737 con->base_height = base_height;
738 DLOG("client's base_height changed to %d\n", base_height);
739 DLOG("client's base_width changed to %d\n", base_width);
743 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
744 if (!(size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT) ||
745 (size_hints.min_aspect_num <= 0) ||
746 (size_hints.min_aspect_den <= 0)) {
747 goto render_and_return;
750 /* XXX: do we really use rect here, not window_rect? */
751 double width = con->rect.width - base_width;
752 double height = con->rect.height - base_height;
753 /* Convert numerator/denominator to a double */
754 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
755 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
757 DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
758 DLOG("width = %f, height = %f\n", width, height);
760 /* Sanity checks, this is user-input, in a way */
761 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
762 goto render_and_return;
764 /* Check if we need to set proportional_* variables using the correct ratio */
765 if ((width / height) < min_aspect) {
766 if (con->proportional_width != width ||
767 con->proportional_height != (width / min_aspect)) {
768 con->proportional_width = width;
769 con->proportional_height = width / min_aspect;
772 } else if ((width / height) > max_aspect) {
773 if (con->proportional_width != width ||
774 con->proportional_height != (width / max_aspect)) {
775 con->proportional_width = width;
776 con->proportional_height = width / max_aspect;
779 } else goto render_and_return;
789 * Handles the WM_HINTS property for extracting the urgency state of the window.
792 static bool 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 (!(reply = xcb_get_property_reply(conn, xcb_icccm_get_wm_hints(conn, window), NULL)))
806 if (!xcb_icccm_get_wm_hints_from_reply(&hints, reply))
809 if (!con->urgent && focused == con) {
810 DLOG("Ignoring urgency flag for current client\n");
811 con->window->urgent.tv_sec = 0;
812 con->window->urgent.tv_usec = 0;
816 /* Update the flag on the client directly */
817 con->urgent = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
821 gettimeofday(&con->window->urgent, NULL);
823 con->window->urgent.tv_sec = 0;
824 con->window->urgent.tv_usec = 0;
827 LOG("Urgency flag changed to %d\n", con->urgent);
830 /* Set the urgency flag on the workspace, if a workspace could be found
831 * (for dock clients, that is not the case). */
832 if ((ws = con_get_workspace(con)) != NULL)
833 workspace_update_urgent_flag(ws);
839 window_update_hints(con->window, reply);
845 * Handles the transient for hints set by a window, signalizing that this window is a popup window
846 * for some other window.
848 * See ICCCM 4.1.2.6 for more details
851 static bool handle_transient_for(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
852 xcb_atom_t name, xcb_get_property_reply_t *prop) {
855 if ((con = con_by_window_id(window)) == NULL || con->window == NULL) {
856 DLOG("No such window\n");
861 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
862 false, window, XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 0, 32), NULL);
867 window_update_transient_for(con->window, prop);
873 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
874 * toolwindow (or similar) and to which window it belongs (logical parent).
877 static bool handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
878 xcb_atom_t name, xcb_get_property_reply_t *prop) {
880 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
884 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
885 false, window, A_WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32), NULL);
890 window_update_leader(con->window, prop);
896 * Handles FocusIn events which are generated by clients (i3’s focus changes
897 * don’t generate FocusIn events due to a different EventMask) and updates the
898 * decorations accordingly.
901 static void handle_focus_in(xcb_focus_in_event_t *event) {
902 DLOG("focus change in, for window 0x%08x\n", event->event);
904 if ((con = con_by_window_id(event->event)) == NULL || con->window == NULL)
906 DLOG("That is con %p / %s\n", con, con->name);
908 if (event->mode == XCB_NOTIFY_MODE_GRAB ||
909 event->mode == XCB_NOTIFY_MODE_UNGRAB) {
910 DLOG("FocusIn event for grab/ungrab, ignoring\n");
914 if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
915 DLOG("notify detail is pointer, ignoring this event\n");
919 if (focused_id == event->event) {
920 DLOG("focus matches the currently focused window, not doing anything\n");
924 /* Skip dock clients, they cannot get the i3 focus. */
925 if (con->parent->type == CT_DOCKAREA) {
926 DLOG("This is a dock client, not focusing.\n");
930 DLOG("focus is different, updating decorations\n");
932 /* Get the currently focused workspace to check if the focus change also
933 * involves changing workspaces. If so, we need to call workspace_show() to
934 * correctly update state and send the IPC event. */
935 Con *ws = con_get_workspace(con);
936 if (ws != con_get_workspace(focused))
940 /* We update focused_id because we don’t need to set focus again */
941 focused_id = event->event;
942 x_push_changes(croot);
946 /* Returns false if the event could not be processed (e.g. the window could not
947 * be found), true otherwise */
948 typedef bool (*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);
950 struct property_handler_t {
953 cb_property_handler_t cb;
956 static struct property_handler_t property_handlers[] = {
957 { 0, 128, handle_windowname_change },
958 { 0, UINT_MAX, handle_hints },
959 { 0, 128, handle_windowname_change_legacy },
960 { 0, UINT_MAX, handle_normal_hints },
961 { 0, UINT_MAX, handle_clientleader_change },
962 { 0, UINT_MAX, handle_transient_for },
963 { 0, 128, handle_windowrole_change }
965 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
968 * Sets the appropriate atoms for the property handlers after the atoms were
972 void property_handlers_init(void) {
974 sn_monitor_context_new(sndisplay, conn_screen, startup_monitor_event, NULL, NULL);
976 property_handlers[0].atom = A__NET_WM_NAME;
977 property_handlers[1].atom = XCB_ATOM_WM_HINTS;
978 property_handlers[2].atom = XCB_ATOM_WM_NAME;
979 property_handlers[3].atom = XCB_ATOM_WM_NORMAL_HINTS;
980 property_handlers[4].atom = A_WM_CLIENT_LEADER;
981 property_handlers[5].atom = XCB_ATOM_WM_TRANSIENT_FOR;
982 property_handlers[6].atom = A_WM_WINDOW_ROLE;
985 static void property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
986 struct property_handler_t *handler = NULL;
987 xcb_get_property_reply_t *propr = NULL;
989 for (int c = 0; c < sizeof(property_handlers) / sizeof(struct property_handler_t); c++) {
990 if (property_handlers[c].atom != atom)
993 handler = &property_handlers[c];
997 if (handler == NULL) {
998 //DLOG("Unhandled property notify for atom %d (0x%08x)\n", atom, atom);
1002 if (state != XCB_PROPERTY_DELETE) {
1003 xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->long_len);
1004 propr = xcb_get_property_reply(conn, cookie, 0);
1007 /* the handler will free() the reply unless it returns false */
1008 if (!handler->cb(NULL, conn, state, window, atom, propr))
1013 * Takes an xcb_generic_event_t and calls the appropriate handler, based on the
1017 void handle_event(int type, xcb_generic_event_t *event) {
1018 if (randr_base > -1 &&
1019 type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
1020 handle_screen_change(event);
1026 handle_key_press((xcb_key_press_event_t*)event);
1029 case XCB_BUTTON_PRESS:
1030 handle_button_press((xcb_button_press_event_t*)event);
1033 case XCB_MAP_REQUEST:
1034 handle_map_request((xcb_map_request_event_t*)event);
1037 case XCB_UNMAP_NOTIFY:
1038 handle_unmap_notify_event((xcb_unmap_notify_event_t*)event);
1041 case XCB_DESTROY_NOTIFY:
1042 handle_destroy_notify_event((xcb_destroy_notify_event_t*)event);
1046 handle_expose_event((xcb_expose_event_t*)event);
1049 case XCB_MOTION_NOTIFY:
1050 handle_motion_notify((xcb_motion_notify_event_t*)event);
1053 /* Enter window = user moved his mouse over the window */
1054 case XCB_ENTER_NOTIFY:
1055 handle_enter_notify((xcb_enter_notify_event_t*)event);
1058 /* Client message are sent to the root window. The only interesting
1059 * client message for us is _NET_WM_STATE, we honour
1060 * _NET_WM_STATE_FULLSCREEN */
1061 case XCB_CLIENT_MESSAGE:
1062 handle_client_message((xcb_client_message_event_t*)event);
1065 /* Configure request = window tried to change size on its own */
1066 case XCB_CONFIGURE_REQUEST:
1067 handle_configure_request((xcb_configure_request_event_t*)event);
1070 /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
1071 case XCB_MAPPING_NOTIFY:
1072 handle_mapping_notify((xcb_mapping_notify_event_t*)event);
1076 handle_focus_in((xcb_focus_in_event_t*)event);
1079 case XCB_PROPERTY_NOTIFY: {
1080 xcb_property_notify_event_t *e = (xcb_property_notify_event_t*)event;
1081 last_timestamp = e->time;
1082 property_notify(e->state, e->window, e->atom);
1087 //DLOG("Unhandled event of type %d\n", type);