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 check_crossing_screen_boundary(event->root_x, event->root_y);
219 if (config.disable_focus_follows_mouse)
222 if (con->layout != L_DEFAULT)
225 /* see over which rect the user is */
227 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
228 if (!rect_contains(current->deco_rect, event->event_x, event->event_y))
231 /* We found the rect, let’s see if this window is focused */
232 if (TAILQ_FIRST(&(con->focus_head)) == current)
236 x_push_changes(croot);
244 * Called when the keyboard mapping changes (for example by using Xmodmap),
245 * we need to update our key bindings then (re-translate symbols).
248 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
249 if (event->request != XCB_MAPPING_KEYBOARD &&
250 event->request != XCB_MAPPING_MODIFIER)
253 DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
254 xcb_refresh_keyboard_mapping(keysyms, event);
256 xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
258 ungrab_all_keys(conn);
260 grab_all_keys(conn, false);
266 * A new window appeared on the screen (=was mapped), so let’s manage it.
269 static void handle_map_request(xcb_map_request_event_t *event) {
270 xcb_get_window_attributes_cookie_t cookie;
272 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
274 DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
275 add_ignore_event(event->sequence, -1);
277 manage_window(event->window, cookie, false);
278 x_push_changes(croot);
283 * Configure requests are received when the application wants to resize windows
286 * We generate a synthethic configure notify event to signalize the client its
290 static void handle_configure_request(xcb_configure_request_event_t *event) {
293 DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
294 event->window, event->x, event->y, event->width, event->height);
296 /* For unmanaged windows, we just execute the configure request. As soon as
297 * it gets mapped, we will take over anyways. */
298 if ((con = con_by_window_id(event->window)) == NULL) {
299 DLOG("Configure request for unmanaged window, can do that.\n");
304 #define COPY_MASK_MEMBER(mask_member, event_member) do { \
305 if (event->value_mask & mask_member) { \
306 mask |= mask_member; \
307 values[c++] = event->event_member; \
311 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
312 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
313 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
314 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
315 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
316 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
317 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
319 xcb_configure_window(conn, event->window, mask, values);
325 DLOG("Configure request!\n");
326 if (con_is_floating(con) && con_is_leaf(con)) {
327 /* find the height for the decorations */
328 int deco_height = config.font.height + 5;
329 /* we actually need to apply the size/position changes to the *parent*
331 Rect bsr = con_border_style_rect(con);
332 if (con->border_style == BS_NORMAL) {
333 bsr.y += deco_height;
334 bsr.height -= deco_height;
336 Con *floatingcon = con->parent;
338 Rect newrect = floatingcon->rect;
340 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
341 newrect.x = event->x + (-1) * bsr.x;
342 DLOG("proposed x = %d, new x is %d\n", event->x, newrect.x);
344 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
345 newrect.y = event->y + (-1) * bsr.y;
346 DLOG("proposed y = %d, new y is %d\n", event->y, newrect.y);
348 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
349 newrect.width = event->width + (-1) * bsr.width;
350 newrect.width += con->border_width * 2;
351 DLOG("proposed width = %d, new width is %d (x11 border %d)\n",
352 event->width, newrect.width, con->border_width);
354 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
355 newrect.height = event->height + (-1) * bsr.height;
356 newrect.height += con->border_width * 2;
357 DLOG("proposed height = %d, new height is %d (x11 border %d)\n",
358 event->height, newrect.height, con->border_width);
361 DLOG("Container is a floating leaf node, will do that.\n");
362 floating_reposition(floatingcon, newrect);
366 /* Dock windows can be reconfigured in their height */
367 if (con->parent && con->parent->type == CT_DOCKAREA) {
368 DLOG("Dock window, only height reconfiguration allowed\n");
369 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
370 DLOG("Height given, changing\n");
372 con->geometry.height = event->height;
377 fake_absolute_configure_notify(con);
384 * Configuration notifies are only handled because we need to set up ignore for
385 * the following enter notify events.
388 int handle_configure_event(void *prophs, xcb_connection_t *conn, xcb_configure_notify_event_t *event) {
389 DLOG("configure_event, sequence %d\n", event->sequence);
390 /* We ignore this sequence twice because events for child and frame should be ignored */
391 add_ignore_event(event->sequence);
392 add_ignore_event(event->sequence);
399 * Gets triggered upon a RandR screen change event, that is when the user
400 * changes the screen configuration in any way (mode, position, …)
403 static void handle_screen_change(xcb_generic_event_t *e) {
404 DLOG("RandR screen change\n");
406 randr_query_outputs();
408 ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
414 * Our window decorations were unmapped. That means, the window will be killed
415 * now, so we better clean up before.
418 static void handle_unmap_notify_event(xcb_unmap_notify_event_t *event) {
419 DLOG("UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
420 xcb_get_input_focus_cookie_t cookie;
421 Con *con = con_by_window_id(event->window);
423 /* This could also be an UnmapNotify for the frame. We need to
424 * decrement the ignore_unmap counter. */
425 con = con_by_frame_id(event->window);
427 LOG("Not a managed window, ignoring UnmapNotify event\n");
431 if (con->ignore_unmap > 0)
433 /* See the end of this function. */
434 cookie = xcb_get_input_focus(conn);
435 DLOG("ignore_unmap = %d for frame of container %p\n", con->ignore_unmap, con);
439 /* See the end of this function. */
440 cookie = xcb_get_input_focus(conn);
442 if (con->ignore_unmap > 0) {
443 DLOG("ignore_unmap = %d, dec\n", con->ignore_unmap);
448 tree_close(con, DONT_KILL_WINDOW, false, false);
450 x_push_changes(croot);
453 /* If the client (as opposed to i3) destroyed or unmapped a window, an
454 * EnterNotify event will follow (indistinguishable from an EnterNotify
455 * event caused by moving your mouse), causing i3 to set focus to whichever
456 * window is now visible.
458 * In a complex stacked or tabbed layout (take two v-split containers in a
459 * tabbed container), when the bottom window in tab2 is closed, the bottom
460 * window of tab1 is visible instead. X11 will thus send an EnterNotify
461 * event for the bottom window of tab1, while the focus should be set to
462 * the remaining window of tab2.
464 * Therefore, we ignore all EnterNotify events which have the same sequence
465 * as an UnmapNotify event. */
466 add_ignore_event(event->sequence, XCB_ENTER_NOTIFY);
468 /* Since we just ignored the sequence of this UnmapNotify, we want to make
469 * sure that following events use a different sequence. When putting xterm
470 * into fullscreen and moving the pointer to a different window, without
471 * using GetInputFocus, subsequent (legitimate) EnterNotify events arrived
472 * with the same sequence and thus were ignored (see ticket #609). */
473 free(xcb_get_input_focus_reply(conn, cookie, NULL));
477 * A destroy notify event is sent when the window is not unmapped, but
478 * immediately destroyed (for example when starting a window and immediately
479 * killing the program which started it).
481 * We just pass on the event to the unmap notify handler (by copying the
482 * important fields in the event data structure).
485 static void handle_destroy_notify_event(xcb_destroy_notify_event_t *event) {
486 DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
488 xcb_unmap_notify_event_t unmap;
489 unmap.sequence = event->sequence;
490 unmap.event = event->event;
491 unmap.window = event->window;
493 handle_unmap_notify_event(&unmap);
497 * Called when a window changes its title
500 static bool handle_windowname_change(void *data, xcb_connection_t *conn, uint8_t state,
501 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
503 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
506 window_update_name(con->window, prop, false);
508 x_push_changes(croot);
514 * Handles legacy window name updates (WM_NAME), see also src/window.c,
515 * window_update_name_legacy().
518 static bool handle_windowname_change_legacy(void *data, xcb_connection_t *conn, uint8_t state,
519 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
521 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
524 window_update_name_legacy(con->window, prop, false);
526 x_push_changes(croot);
532 * Called when a window changes its WM_WINDOW_ROLE.
535 static bool handle_windowrole_change(void *data, xcb_connection_t *conn, uint8_t state,
536 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
538 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
541 window_update_role(con->window, prop, false);
548 * Updates the client’s WM_CLASS property
551 static int handle_windowclass_change(void *data, xcb_connection_t *conn, uint8_t state,
552 xcb_window_t window, xcb_atom_t atom, xcb_get_property_reply_t *prop) {
554 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
557 window_update_class(con->window, prop, false);
564 * Expose event means we should redraw our windows (= title bar)
567 static void handle_expose_event(xcb_expose_event_t *event) {
570 DLOG("window = %08x\n", event->window);
572 if ((parent = con_by_frame_id(event->window)) == NULL) {
573 LOG("expose event for unknown window, ignoring\n");
577 /* Since we render to our pixmap on every change anyways, expose events
578 * only tell us that the X server lost (parts of) the window contents. We
579 * can handle that by copying the appropriate part from our pixmap to the
581 xcb_copy_area(conn, parent->pixmap, parent->frame, parent->pm_gc,
582 event->x, event->y, event->x, event->y,
583 event->width, event->height);
590 * Handle client messages (EWMH)
593 static void handle_client_message(xcb_client_message_event_t *event) {
594 /* If this is a startup notification ClientMessage, the library will handle
595 * it and call our monitor_event() callback. */
596 if (sn_xcb_display_process_event(sndisplay, (xcb_generic_event_t*)event))
599 LOG("ClientMessage for window 0x%08x\n", event->window);
600 if (event->type == A__NET_WM_STATE) {
601 if (event->format != 32 || event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN) {
602 DLOG("atom in clientmessage is %d, fullscreen is %d\n",
603 event->data.data32[1], A__NET_WM_STATE_FULLSCREEN);
604 DLOG("not about fullscreen atom\n");
608 Con *con = con_by_window_id(event->window);
610 DLOG("Could not get window for client message\n");
614 /* Check if the fullscreen state should be toggled */
615 if ((con->fullscreen_mode != CF_NONE &&
616 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
617 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
618 (con->fullscreen_mode == CF_NONE &&
619 (event->data.data32[0] == _NET_WM_STATE_ADD ||
620 event->data.data32[0] == _NET_WM_STATE_TOGGLE))) {
621 DLOG("toggling fullscreen\n");
622 con_toggle_fullscreen(con, CF_OUTPUT);
626 } else if (event->type == A__NET_ACTIVE_WINDOW) {
627 DLOG("_NET_ACTIVE_WINDOW: Window 0x%08x should be activated\n", event->window);
628 Con *con = con_by_window_id(event->window);
630 DLOG("Could not get window for client message\n");
636 } else if (event->type == A_I3_SYNC) {
637 xcb_window_t window = event->data.data32[0];
638 uint32_t rnd = event->data.data32[1];
639 DLOG("[i3 sync protocol] Sending random value %d back to X11 window 0x%08x\n", rnd, window);
641 void *reply = scalloc(32);
642 xcb_client_message_event_t *ev = reply;
644 ev->response_type = XCB_CLIENT_MESSAGE;
646 ev->type = A_I3_SYNC;
648 ev->data.data32[0] = window;
649 ev->data.data32[1] = rnd;
651 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
655 DLOG("unhandled clientmessage\n");
661 int handle_window_type(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
662 xcb_atom_t atom, xcb_get_property_reply_t *property) {
663 /* TODO: Implement this one. To do this, implement a little test program which sleep(1)s
664 before changing this property. */
665 ELOG("_NET_WM_WINDOW_TYPE changed, this is not yet implemented.\n");
671 * Handles the size hints set by a window, but currently only the part necessary for displaying
672 * clients proportionally inside their frames (mplayer for example)
674 * See ICCCM 4.1.2.3 for more details
677 static bool handle_normal_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
678 xcb_atom_t name, xcb_get_property_reply_t *reply) {
679 Con *con = con_by_window_id(window);
681 DLOG("Received WM_NORMAL_HINTS for unknown client\n");
685 xcb_size_hints_t size_hints;
687 //CLIENT_LOG(client);
689 /* If the hints were already in this event, use them, if not, request them */
691 xcb_icccm_get_wm_size_hints_from_reply(&size_hints, reply);
693 xcb_icccm_get_wm_normal_hints_reply(conn, xcb_icccm_get_wm_normal_hints_unchecked(conn, con->window->id), &size_hints, NULL);
695 if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE)) {
696 // TODO: Minimum size is not yet implemented
697 DLOG("Minimum size: %d (width) x %d (height)\n", size_hints.min_width, size_hints.min_height);
700 bool changed = false;
701 if ((size_hints.flags & XCB_ICCCM_SIZE_HINT_P_RESIZE_INC)) {
702 if (size_hints.width_inc > 0 && size_hints.width_inc < 0xFFFF)
703 if (con->width_increment != size_hints.width_inc) {
704 con->width_increment = size_hints.width_inc;
707 if (size_hints.height_inc > 0 && size_hints.height_inc < 0xFFFF)
708 if (con->height_increment != size_hints.height_inc) {
709 con->height_increment = size_hints.height_inc;
714 DLOG("resize increments changed\n");
717 int base_width = 0, base_height = 0;
719 /* base_width/height are the desired size of the window.
720 We check if either the program-specified size or the program-specified
721 min-size is available */
722 if (size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
723 base_width = size_hints.base_width;
724 base_height = size_hints.base_height;
725 } else if (size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
726 /* TODO: is this right? icccm says not */
727 base_width = size_hints.min_width;
728 base_height = size_hints.min_height;
731 if (base_width != con->base_width ||
732 base_height != con->base_height) {
733 con->base_width = base_width;
734 con->base_height = base_height;
735 DLOG("client's base_height changed to %d\n", base_height);
736 DLOG("client's base_width changed to %d\n", base_width);
740 /* If no aspect ratio was set or if it was invalid, we ignore the hints */
741 if (!(size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT) ||
742 (size_hints.min_aspect_num <= 0) ||
743 (size_hints.min_aspect_den <= 0)) {
744 goto render_and_return;
747 /* XXX: do we really use rect here, not window_rect? */
748 double width = con->rect.width - base_width;
749 double height = con->rect.height - base_height;
750 /* Convert numerator/denominator to a double */
751 double min_aspect = (double)size_hints.min_aspect_num / size_hints.min_aspect_den;
752 double max_aspect = (double)size_hints.max_aspect_num / size_hints.min_aspect_den;
754 DLOG("Aspect ratio set: minimum %f, maximum %f\n", min_aspect, max_aspect);
755 DLOG("width = %f, height = %f\n", width, height);
757 /* Sanity checks, this is user-input, in a way */
758 if (max_aspect <= 0 || min_aspect <= 0 || height == 0 || (width / height) <= 0)
759 goto render_and_return;
761 /* Check if we need to set proportional_* variables using the correct ratio */
762 if ((width / height) < min_aspect) {
763 if (con->proportional_width != width ||
764 con->proportional_height != (width / min_aspect)) {
765 con->proportional_width = width;
766 con->proportional_height = width / min_aspect;
769 } else if ((width / height) > max_aspect) {
770 if (con->proportional_width != width ||
771 con->proportional_height != (width / max_aspect)) {
772 con->proportional_width = width;
773 con->proportional_height = width / max_aspect;
776 } else goto render_and_return;
786 * Handles the WM_HINTS property for extracting the urgency state of the window.
789 static bool handle_hints(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
790 xcb_atom_t name, xcb_get_property_reply_t *reply) {
791 Con *con = con_by_window_id(window);
793 DLOG("Received WM_HINTS for unknown client\n");
797 xcb_icccm_wm_hints_t hints;
800 if (!(reply = xcb_get_property_reply(conn, xcb_icccm_get_wm_hints(conn, window), NULL)))
803 if (!xcb_icccm_get_wm_hints_from_reply(&hints, reply))
806 if (!con->urgent && focused == con) {
807 DLOG("Ignoring urgency flag for current client\n");
808 con->window->urgent.tv_sec = 0;
809 con->window->urgent.tv_usec = 0;
813 /* Update the flag on the client directly */
814 con->urgent = (xcb_icccm_wm_hints_get_urgency(&hints) != 0);
818 gettimeofday(&con->window->urgent, NULL);
820 con->window->urgent.tv_sec = 0;
821 con->window->urgent.tv_usec = 0;
824 LOG("Urgency flag changed to %d\n", con->urgent);
827 /* Set the urgency flag on the workspace, if a workspace could be found
828 * (for dock clients, that is not the case). */
829 if ((ws = con_get_workspace(con)) != NULL)
830 workspace_update_urgent_flag(ws);
836 window_update_hints(con->window, reply);
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 bool 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, XCB_ATOM_WM_TRANSIENT_FOR, XCB_ATOM_WINDOW, 0, 32), NULL);
864 window_update_transient_for(con->window, prop);
870 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
871 * toolwindow (or similar) and to which window it belongs (logical parent).
874 static bool handle_clientleader_change(void *data, xcb_connection_t *conn, uint8_t state, xcb_window_t window,
875 xcb_atom_t name, xcb_get_property_reply_t *prop) {
877 if ((con = con_by_window_id(window)) == NULL || con->window == NULL)
881 prop = xcb_get_property_reply(conn, xcb_get_property_unchecked(conn,
882 false, window, A_WM_CLIENT_LEADER, XCB_ATOM_WINDOW, 0, 32), NULL);
887 window_update_leader(con->window, prop);
893 * Handles FocusIn events which are generated by clients (i3’s focus changes
894 * don’t generate FocusIn events due to a different EventMask) and updates the
895 * decorations accordingly.
898 static void handle_focus_in(xcb_focus_in_event_t *event) {
899 DLOG("focus change in, for window 0x%08x\n", event->event);
901 if ((con = con_by_window_id(event->event)) == NULL || con->window == NULL)
903 DLOG("That is con %p / %s\n", con, con->name);
905 if (event->mode == XCB_NOTIFY_MODE_GRAB ||
906 event->mode == XCB_NOTIFY_MODE_UNGRAB) {
907 DLOG("FocusIn event for grab/ungrab, ignoring\n");
911 if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
912 DLOG("notify detail is pointer, ignoring this event\n");
916 if (focused_id == event->event) {
917 DLOG("focus matches the currently focused window, not doing anything\n");
921 /* Skip dock clients, they cannot get the i3 focus. */
922 if (con->parent->type == CT_DOCKAREA) {
923 DLOG("This is a dock client, not focusing.\n");
927 DLOG("focus is different, updating decorations\n");
929 /* Get the currently focused workspace to check if the focus change also
930 * involves changing workspaces. If so, we need to call workspace_show() to
931 * correctly update state and send the IPC event. */
932 Con *ws = con_get_workspace(con);
933 if (ws != con_get_workspace(focused))
937 /* We update focused_id because we don’t need to set focus again */
938 focused_id = event->event;
939 x_push_changes(croot);
943 /* Returns false if the event could not be processed (e.g. the window could not
944 * be found), true otherwise */
945 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);
947 struct property_handler_t {
950 cb_property_handler_t cb;
953 static struct property_handler_t property_handlers[] = {
954 { 0, 128, handle_windowname_change },
955 { 0, UINT_MAX, handle_hints },
956 { 0, 128, handle_windowname_change_legacy },
957 { 0, UINT_MAX, handle_normal_hints },
958 { 0, UINT_MAX, handle_clientleader_change },
959 { 0, UINT_MAX, handle_transient_for },
960 { 0, 128, handle_windowrole_change }
962 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
965 * Sets the appropriate atoms for the property handlers after the atoms were
969 void property_handlers_init(void) {
971 sn_monitor_context_new(sndisplay, conn_screen, startup_monitor_event, NULL, NULL);
973 property_handlers[0].atom = A__NET_WM_NAME;
974 property_handlers[1].atom = XCB_ATOM_WM_HINTS;
975 property_handlers[2].atom = XCB_ATOM_WM_NAME;
976 property_handlers[3].atom = XCB_ATOM_WM_NORMAL_HINTS;
977 property_handlers[4].atom = A_WM_CLIENT_LEADER;
978 property_handlers[5].atom = XCB_ATOM_WM_TRANSIENT_FOR;
979 property_handlers[6].atom = A_WM_WINDOW_ROLE;
982 static void property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
983 struct property_handler_t *handler = NULL;
984 xcb_get_property_reply_t *propr = NULL;
986 for (int c = 0; c < sizeof(property_handlers) / sizeof(struct property_handler_t); c++) {
987 if (property_handlers[c].atom != atom)
990 handler = &property_handlers[c];
994 if (handler == NULL) {
995 //DLOG("Unhandled property notify for atom %d (0x%08x)\n", atom, atom);
999 if (state != XCB_PROPERTY_DELETE) {
1000 xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->long_len);
1001 propr = xcb_get_property_reply(conn, cookie, 0);
1004 /* the handler will free() the reply unless it returns false */
1005 if (!handler->cb(NULL, conn, state, window, atom, propr))
1010 * Takes an xcb_generic_event_t and calls the appropriate handler, based on the
1014 void handle_event(int type, xcb_generic_event_t *event) {
1015 if (randr_base > -1 &&
1016 type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
1017 handle_screen_change(event);
1023 handle_key_press((xcb_key_press_event_t*)event);
1026 case XCB_BUTTON_PRESS:
1027 handle_button_press((xcb_button_press_event_t*)event);
1030 case XCB_MAP_REQUEST:
1031 handle_map_request((xcb_map_request_event_t*)event);
1034 case XCB_UNMAP_NOTIFY:
1035 handle_unmap_notify_event((xcb_unmap_notify_event_t*)event);
1038 case XCB_DESTROY_NOTIFY:
1039 handle_destroy_notify_event((xcb_destroy_notify_event_t*)event);
1043 handle_expose_event((xcb_expose_event_t*)event);
1046 case XCB_MOTION_NOTIFY:
1047 handle_motion_notify((xcb_motion_notify_event_t*)event);
1050 /* Enter window = user moved his mouse over the window */
1051 case XCB_ENTER_NOTIFY:
1052 handle_enter_notify((xcb_enter_notify_event_t*)event);
1055 /* Client message are sent to the root window. The only interesting
1056 * client message for us is _NET_WM_STATE, we honour
1057 * _NET_WM_STATE_FULLSCREEN */
1058 case XCB_CLIENT_MESSAGE:
1059 handle_client_message((xcb_client_message_event_t*)event);
1062 /* Configure request = window tried to change size on its own */
1063 case XCB_CONFIGURE_REQUEST:
1064 handle_configure_request((xcb_configure_request_event_t*)event);
1067 /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
1068 case XCB_MAPPING_NOTIFY:
1069 handle_mapping_notify((xcb_mapping_notify_event_t*)event);
1073 handle_focus_in((xcb_focus_in_event_t*)event);
1076 case XCB_PROPERTY_NOTIFY: {
1077 xcb_property_notify_event_t *e = (xcb_property_notify_event_t*)event;
1078 last_timestamp = e->time;
1079 property_notify(e->state, e->window, e->atom);
1084 //DLOG("Unhandled event of type %d\n", type);