1 /*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
7 * handlers.c: Small handlers for various events (keypresses, focus changes,
8 * …).
9 *
10 */
11 #include "all.h"
12
13 #include <sys/time.h>
14 #include <time.h>
15
16 #include <xcb/randr.h>
17 #define SN_API_NOT_YET_FROZEN 1
18 #include <libsn/sn-monitor.h>
19
20 int randr_base = -1;
21 int xkb_base = -1;
22 int xkb_current_group;
23 int shape_base = -1;
24
25 /* After mapping/unmapping windows, a notify event is generated. However, we don’t want it,
26 since it’d trigger an infinite loop of switching between the different windows when
27 changing workspaces */
28 static SLIST_HEAD(ignore_head, Ignore_Event) ignore_events;
29
30 /*
31 * Adds the given sequence to the list of events which are ignored.
32 * If this ignore should only affect a specific response_type, pass
33 * response_type, otherwise, pass -1.
34 *
35 * Every ignored sequence number gets garbage collected after 5 seconds.
36 *
37 */
add_ignore_event(const int sequence,const int response_type)38 void add_ignore_event(const int sequence, const int response_type) {
39 struct Ignore_Event *event = smalloc(sizeof(struct Ignore_Event));
40
41 event->sequence = sequence;
42 event->response_type = response_type;
43 event->added = time(NULL);
44
45 SLIST_INSERT_HEAD(&ignore_events, event, ignore_events);
46 }
47
48 /*
49 * Checks if the given sequence is ignored and returns true if so.
50 *
51 */
event_is_ignored(const int sequence,const int response_type)52 bool event_is_ignored(const int sequence, const int response_type) {
53 struct Ignore_Event *event;
54 time_t now = time(NULL);
55 for (event = SLIST_FIRST(&ignore_events); event != SLIST_END(&ignore_events);) {
56 if ((now - event->added) > 5) {
57 struct Ignore_Event *save = event;
58 event = SLIST_NEXT(event, ignore_events);
59 SLIST_REMOVE(&ignore_events, save, Ignore_Event, ignore_events);
60 free(save);
61 } else
62 event = SLIST_NEXT(event, ignore_events);
63 }
64
65 SLIST_FOREACH (event, &ignore_events, ignore_events) {
66 if (event->sequence != sequence)
67 continue;
68
69 if (event->response_type != -1 &&
70 event->response_type != response_type)
71 continue;
72
73 /* instead of removing a sequence number we better wait until it gets
74 * garbage collected. it may generate multiple events (there are multiple
75 * enter_notifies for one configure_request, for example). */
76 //SLIST_REMOVE(&ignore_events, event, Ignore_Event, ignore_events);
77 //free(event);
78 return true;
79 }
80
81 return false;
82 }
83
84 /*
85 * Called with coordinates of an enter_notify event or motion_notify event
86 * to check if the user crossed virtual screen boundaries and adjust the
87 * current workspace, if so.
88 *
89 */
check_crossing_screen_boundary(uint32_t x,uint32_t y)90 static void check_crossing_screen_boundary(uint32_t x, uint32_t y) {
91 Output *output;
92
93 /* If the user disable focus follows mouse, we have nothing to do here */
94 if (config.disable_focus_follows_mouse)
95 return;
96
97 if ((output = get_output_containing(x, y)) == NULL) {
98 ELOG("ERROR: No such screen\n");
99 return;
100 }
101
102 if (output->con == NULL) {
103 ELOG("ERROR: The screen is not recognized by i3 (no container associated)\n");
104 return;
105 }
106
107 /* Focus the output on which the user moved their cursor */
108 Con *old_focused = focused;
109 Con *next = con_descend_focused(output_get_content(output->con));
110 /* Since we are switching outputs, this *must* be a different workspace, so
111 * call workspace_show() */
112 workspace_show(con_get_workspace(next));
113 con_focus(next);
114
115 /* If the focus changed, we re-render to get updated decorations */
116 if (old_focused != focused)
117 tree_render();
118 }
119
120 /*
121 * When the user moves the mouse pointer onto a window, this callback gets called.
122 *
123 */
handle_enter_notify(xcb_enter_notify_event_t * event)124 static void handle_enter_notify(xcb_enter_notify_event_t *event) {
125 Con *con;
126
127 last_timestamp = event->time;
128
129 DLOG("enter_notify for %08x, mode = %d, detail %d, serial %d\n",
130 event->event, event->mode, event->detail, event->sequence);
131 DLOG("coordinates %d, %d\n", event->event_x, event->event_y);
132 if (event->mode != XCB_NOTIFY_MODE_NORMAL) {
133 DLOG("This was not a normal notify, ignoring\n");
134 return;
135 }
136 /* Some events are not interesting, because they were not generated
137 * actively by the user, but by reconfiguration of windows */
138 if (event_is_ignored(event->sequence, XCB_ENTER_NOTIFY)) {
139 DLOG("Event ignored\n");
140 return;
141 }
142
143 bool enter_child = false;
144 /* Get container by frame or by child window */
145 if ((con = con_by_frame_id(event->event)) == NULL) {
146 con = con_by_window_id(event->event);
147 enter_child = true;
148 }
149
150 /* If we cannot find the container, the user moved their cursor to the root
151 * window. In this case and if they used it to a dock, we need to focus the
152 * workspace on the correct output. */
153 if (con == NULL || con->parent->type == CT_DOCKAREA) {
154 DLOG("Getting screen at %d x %d\n", event->root_x, event->root_y);
155 check_crossing_screen_boundary(event->root_x, event->root_y);
156 return;
157 }
158
159 /* see if the user entered the window on a certain window decoration */
160 layout_t layout = (enter_child ? con->parent->layout : con->layout);
161 if (layout == L_DEFAULT) {
162 Con *child;
163 TAILQ_FOREACH_REVERSE (child, &(con->nodes_head), 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);
166 con = child;
167 break;
168 }
169 }
170 }
171
172 if (config.disable_focus_follows_mouse)
173 return;
174
175 /* if this container is already focused, there is nothing to do. */
176 if (con == focused)
177 return;
178
179 /* Get the currently focused workspace to check if the focus change also
180 * involves changing workspaces. If so, we need to call workspace_show() to
181 * correctly update state and send the IPC event. */
182 Con *ws = con_get_workspace(con);
183 if (ws != con_get_workspace(focused))
184 workspace_show(ws);
185
186 focused_id = XCB_NONE;
187 con_focus(con_descend_focused(con));
188 tree_render();
189 }
190
191 /*
192 * When the user moves the mouse but does not change the active window
193 * (e.g. when having no windows opened but moving mouse on the root screen
194 * and crossing virtual screen boundaries), this callback gets called.
195 *
196 */
handle_motion_notify(xcb_motion_notify_event_t * event)197 static void handle_motion_notify(xcb_motion_notify_event_t *event) {
198 last_timestamp = event->time;
199
200 /* Skip events where the pointer was over a child window, we are only
201 * interested in events on the root window. */
202 if (event->child != XCB_NONE)
203 return;
204
205 Con *con;
206 if ((con = con_by_frame_id(event->event)) == NULL) {
207 DLOG("MotionNotify for an unknown container, checking if it crosses screen boundaries.\n");
208 check_crossing_screen_boundary(event->root_x, event->root_y);
209 return;
210 }
211
212 if (config.disable_focus_follows_mouse)
213 return;
214
215 if (con->layout != L_DEFAULT && con->layout != L_SPLITV && con->layout != L_SPLITH)
216 return;
217
218 /* see over which rect the user is */
219 Con *current;
220 TAILQ_FOREACH_REVERSE (current, &(con->nodes_head), nodes_head, nodes) {
221 if (!rect_contains(current->deco_rect, event->event_x, event->event_y))
222 continue;
223
224 /* We found the rect, let’s see if this window is focused */
225 if (TAILQ_FIRST(&(con->focus_head)) == current)
226 return;
227
228 con_focus(current);
229 x_push_changes(croot);
230 return;
231 }
232 }
233
234 /*
235 * Called when the keyboard mapping changes (for example by using Xmodmap),
236 * we need to update our key bindings then (re-translate symbols).
237 *
238 */
handle_mapping_notify(xcb_mapping_notify_event_t * event)239 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
240 if (event->request != XCB_MAPPING_KEYBOARD &&
241 event->request != XCB_MAPPING_MODIFIER)
242 return;
243
244 DLOG("Received mapping_notify for keyboard or modifier mapping, re-grabbing keys\n");
245 xcb_refresh_keyboard_mapping(keysyms, event);
246
247 xcb_numlock_mask = aio_get_mod_mask_for(XCB_NUM_LOCK, keysyms);
248
249 ungrab_all_keys(conn);
250 translate_keysyms();
251 grab_all_keys(conn);
252 }
253
254 /*
255 * A new window appeared on the screen (=was mapped), so let’s manage it.
256 *
257 */
handle_map_request(xcb_map_request_event_t * event)258 static void handle_map_request(xcb_map_request_event_t *event) {
259 xcb_get_window_attributes_cookie_t cookie;
260
261 cookie = xcb_get_window_attributes_unchecked(conn, event->window);
262
263 DLOG("window = 0x%08x, serial is %d.\n", event->window, event->sequence);
264 add_ignore_event(event->sequence, -1);
265
266 manage_window(event->window, cookie, false);
267 }
268
269 /*
270 * Configure requests are received when the application wants to resize windows
271 * on their own.
272 *
273 * We generate a synthethic configure notify event to signalize the client its
274 * "new" position.
275 *
276 */
handle_configure_request(xcb_configure_request_event_t * event)277 static void handle_configure_request(xcb_configure_request_event_t *event) {
278 Con *con;
279
280 DLOG("window 0x%08x wants to be at %dx%d with %dx%d\n",
281 event->window, event->x, event->y, event->width, event->height);
282
283 /* For unmanaged windows, we just execute the configure request. As soon as
284 * it gets mapped, we will take over anyways. */
285 if ((con = con_by_window_id(event->window)) == NULL) {
286 DLOG("Configure request for unmanaged window, can do that.\n");
287
288 uint32_t mask = 0;
289 uint32_t values[7];
290 int c = 0;
291 #define COPY_MASK_MEMBER(mask_member, event_member) \
292 do { \
293 if (event->value_mask & mask_member) { \
294 mask |= mask_member; \
295 values[c++] = event->event_member; \
296 } \
297 } while (0)
298
299 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_X, x);
300 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_Y, y);
301 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_WIDTH, width);
302 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_HEIGHT, height);
303 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_BORDER_WIDTH, border_width);
304 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_SIBLING, sibling);
305 COPY_MASK_MEMBER(XCB_CONFIG_WINDOW_STACK_MODE, stack_mode);
306
307 xcb_configure_window(conn, event->window, mask, values);
308 xcb_flush(conn);
309
310 return;
311 }
312
313 DLOG("Configure request!\n");
314
315 Con *workspace = con_get_workspace(con);
316 if (workspace && (strcmp(workspace->name, "__i3_scratch") == 0)) {
317 DLOG("This is a scratchpad container, ignoring ConfigureRequest\n");
318 goto out;
319 }
320 Con *fullscreen = con_get_fullscreen_covering_ws(workspace);
321
322 if (fullscreen != con && con_is_floating(con) && con_is_leaf(con)) {
323 /* find the height for the decorations */
324 int deco_height = con->deco_rect.height;
325 /* we actually need to apply the size/position changes to the *parent*
326 * container */
327 Rect bsr = con_border_style_rect(con);
328 if (con->border_style == BS_NORMAL) {
329 bsr.y += deco_height;
330 bsr.height -= deco_height;
331 }
332 Con *floatingcon = con->parent;
333 Rect newrect = floatingcon->rect;
334
335 if (event->value_mask & XCB_CONFIG_WINDOW_X) {
336 newrect.x = event->x + (-1) * bsr.x;
337 DLOG("proposed x = %d, new x is %d\n", event->x, newrect.x);
338 }
339 if (event->value_mask & XCB_CONFIG_WINDOW_Y) {
340 newrect.y = event->y + (-1) * bsr.y;
341 DLOG("proposed y = %d, new y is %d\n", event->y, newrect.y);
342 }
343 if (event->value_mask & XCB_CONFIG_WINDOW_WIDTH) {
344 newrect.width = event->width + (-1) * bsr.width;
345 newrect.width += con->border_width * 2;
346 DLOG("proposed width = %d, new width is %d (x11 border %d)\n",
347 event->width, newrect.width, con->border_width);
348 }
349 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
350 newrect.height = event->height + (-1) * bsr.height;
351 newrect.height += con->border_width * 2;
352 DLOG("proposed height = %d, new height is %d (x11 border %d)\n",
353 event->height, newrect.height, con->border_width);
354 }
355
356 DLOG("Container is a floating leaf node, will do that.\n");
357 floating_reposition(floatingcon, newrect);
358 return;
359 }
360
361 /* Dock windows can be reconfigured in their height and moved to another output. */
362 if (con->parent && con->parent->type == CT_DOCKAREA) {
363 DLOG("Reconfiguring dock window (con = %p).\n", con);
364 if (event->value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
365 DLOG("Dock client wants to change height to %d, we can do that.\n", event->height);
366
367 con->geometry.height = event->height;
368 tree_render();
369 }
370
371 if (event->value_mask & XCB_CONFIG_WINDOW_X || event->value_mask & XCB_CONFIG_WINDOW_Y) {
372 int16_t x = event->value_mask & XCB_CONFIG_WINDOW_X ? event->x : (int16_t)con->geometry.x;
373 int16_t y = event->value_mask & XCB_CONFIG_WINDOW_Y ? event->y : (int16_t)con->geometry.y;
374
375 Con *current_output = con_get_output(con);
376 Output *target = get_output_containing(x, y);
377 if (target != NULL && current_output != target->con) {
378 DLOG("Dock client is requested to be moved to output %s, moving it there.\n", output_primary_name(target));
379 Match *match;
380 Con *nc = con_for_window(target->con, con->window, &match);
381 DLOG("Dock client will be moved to container %p.\n", nc);
382 con_detach(con);
383 con_attach(con, nc, false);
384
385 tree_render();
386 } else {
387 DLOG("Dock client will not be moved, we only support moving it to another output.\n");
388 }
389 }
390 goto out;
391 }
392
393 if (event->value_mask & XCB_CONFIG_WINDOW_STACK_MODE) {
394 DLOG("window 0x%08x wants to be stacked %d\n", event->window, event->stack_mode);
395
396 /* Emacs and IntelliJ Idea “request focus” by stacking their window
397 * above all others. */
398 if (event->stack_mode != XCB_STACK_MODE_ABOVE) {
399 DLOG("stack_mode != XCB_STACK_MODE_ABOVE, ignoring ConfigureRequest\n");
400 goto out;
401 }
402
403 if (fullscreen || !con_is_leaf(con)) {
404 DLOG("fullscreen or not a leaf, ignoring ConfigureRequest\n");
405 goto out;
406 }
407
408 if (workspace == NULL) {
409 DLOG("Window is not being managed, ignoring ConfigureRequest\n");
410 goto out;
411 }
412
413 if (config.focus_on_window_activation == FOWA_FOCUS || (config.focus_on_window_activation == FOWA_SMART && workspace_is_visible(workspace))) {
414 DLOG("Focusing con = %p\n", con);
415 workspace_show(workspace);
416 con_activate_unblock(con);
417 tree_render();
418 } else if (config.focus_on_window_activation == FOWA_URGENT || (config.focus_on_window_activation == FOWA_SMART && !workspace_is_visible(workspace))) {
419 DLOG("Marking con = %p urgent\n", con);
420 con_set_urgency(con, true);
421 tree_render();
422 } else {
423 DLOG("Ignoring request for con = %p.\n", con);
424 }
425 }
426
427 out:
428 fake_absolute_configure_notify(con);
429 }
430
431 /*
432 * Gets triggered upon a RandR screen change event, that is when the user
433 * changes the screen configuration in any way (mode, position, …)
434 *
435 */
handle_screen_change(xcb_generic_event_t * e)436 static void handle_screen_change(xcb_generic_event_t *e) {
437 DLOG("RandR screen change\n");
438
439 /* The geometry of the root window is used for “fullscreen global” and
440 * changes when new outputs are added. */
441 xcb_get_geometry_cookie_t cookie = xcb_get_geometry(conn, root);
442 xcb_get_geometry_reply_t *reply = xcb_get_geometry_reply(conn, cookie, NULL);
443 if (reply == NULL) {
444 ELOG("Could not get geometry of the root window, exiting\n");
445 exit(EXIT_FAILURE);
446 }
447 DLOG("root geometry reply: (%d, %d) %d x %d\n", reply->x, reply->y, reply->width, reply->height);
448
449 croot->rect.width = reply->width;
450 croot->rect.height = reply->height;
451
452 randr_query_outputs();
453
454 scratchpad_fix_resolution();
455
456 ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
457 }
458
459 /*
460 * Our window decorations were unmapped. That means, the window will be killed
461 * now, so we better clean up before.
462 *
463 */
handle_unmap_notify_event(xcb_unmap_notify_event_t * event)464 static void handle_unmap_notify_event(xcb_unmap_notify_event_t *event) {
465 DLOG("UnmapNotify for 0x%08x (received from 0x%08x), serial %d\n", event->window, event->event, event->sequence);
466 xcb_get_input_focus_cookie_t cookie;
467 Con *con = con_by_window_id(event->window);
468 if (con == NULL) {
469 /* This could also be an UnmapNotify for the frame. We need to
470 * decrement the ignore_unmap counter. */
471 con = con_by_frame_id(event->window);
472 if (con == NULL) {
473 LOG("Not a managed window, ignoring UnmapNotify event\n");
474 return;
475 }
476
477 if (con->ignore_unmap > 0)
478 con->ignore_unmap--;
479 /* See the end of this function. */
480 cookie = xcb_get_input_focus(conn);
481 DLOG("ignore_unmap = %d for frame of container %p\n", con->ignore_unmap, con);
482 goto ignore_end;
483 }
484
485 /* See the end of this function. */
486 cookie = xcb_get_input_focus(conn);
487
488 if (con->ignore_unmap > 0) {
489 DLOG("ignore_unmap = %d, dec\n", con->ignore_unmap);
490 con->ignore_unmap--;
491 goto ignore_end;
492 }
493
494 /* Since we close the container, we need to unset _NET_WM_DESKTOP and
495 * _NET_WM_STATE according to the spec. */
496 xcb_delete_property(conn, event->window, A__NET_WM_DESKTOP);
497 xcb_delete_property(conn, event->window, A__NET_WM_STATE);
498
499 tree_close_internal(con, DONT_KILL_WINDOW, false);
500 tree_render();
501
502 ignore_end:
503 /* If the client (as opposed to i3) destroyed or unmapped a window, an
504 * EnterNotify event will follow (indistinguishable from an EnterNotify
505 * event caused by moving your mouse), causing i3 to set focus to whichever
506 * window is now visible.
507 *
508 * In a complex stacked or tabbed layout (take two v-split containers in a
509 * tabbed container), when the bottom window in tab2 is closed, the bottom
510 * window of tab1 is visible instead. X11 will thus send an EnterNotify
511 * event for the bottom window of tab1, while the focus should be set to
512 * the remaining window of tab2.
513 *
514 * Therefore, we ignore all EnterNotify events which have the same sequence
515 * as an UnmapNotify event. */
516 add_ignore_event(event->sequence, XCB_ENTER_NOTIFY);
517
518 /* Since we just ignored the sequence of this UnmapNotify, we want to make
519 * sure that following events use a different sequence. When putting xterm
520 * into fullscreen and moving the pointer to a different window, without
521 * using GetInputFocus, subsequent (legitimate) EnterNotify events arrived
522 * with the same sequence and thus were ignored (see ticket #609). */
523 free(xcb_get_input_focus_reply(conn, cookie, NULL));
524 }
525
526 /*
527 * A destroy notify event is sent when the window is not unmapped, but
528 * immediately destroyed (for example when starting a window and immediately
529 * killing the program which started it).
530 *
531 * We just pass on the event to the unmap notify handler (by copying the
532 * important fields in the event data structure).
533 *
534 */
handle_destroy_notify_event(xcb_destroy_notify_event_t * event)535 static void handle_destroy_notify_event(xcb_destroy_notify_event_t *event) {
536 DLOG("destroy notify for 0x%08x, 0x%08x\n", event->event, event->window);
537
538 xcb_unmap_notify_event_t unmap;
539 unmap.sequence = event->sequence;
540 unmap.event = event->event;
541 unmap.window = event->window;
542
543 handle_unmap_notify_event(&unmap);
544 }
545
window_name_changed(i3Window * window,char * old_name)546 static bool window_name_changed(i3Window *window, char *old_name) {
547 if ((old_name == NULL) && (window->name == NULL))
548 return false;
549
550 /* Either the old or the new one is NULL, but not both. */
551 if ((old_name == NULL) ^ (window->name == NULL))
552 return true;
553
554 return (strcmp(old_name, i3string_as_utf8(window->name)) != 0);
555 }
556
557 /*
558 * Called when a window changes its title
559 *
560 */
handle_windowname_change(Con * con,xcb_get_property_reply_t * prop)561 static bool handle_windowname_change(Con *con, xcb_get_property_reply_t *prop) {
562 char *old_name = (con->window->name != NULL ? sstrdup(i3string_as_utf8(con->window->name)) : NULL);
563
564 window_update_name(con->window, prop);
565
566 con = remanage_window(con);
567
568 x_push_changes(croot);
569
570 if (window_name_changed(con->window, old_name))
571 ipc_send_window_event("title", con);
572
573 FREE(old_name);
574
575 return true;
576 }
577
578 /*
579 * Handles legacy window name updates (WM_NAME), see also src/window.c,
580 * window_update_name_legacy().
581 *
582 */
handle_windowname_change_legacy(Con * con,xcb_get_property_reply_t * prop)583 static bool handle_windowname_change_legacy(Con *con, xcb_get_property_reply_t *prop) {
584 char *old_name = (con->window->name != NULL ? sstrdup(i3string_as_utf8(con->window->name)) : NULL);
585
586 window_update_name_legacy(con->window, prop);
587
588 con = remanage_window(con);
589
590 x_push_changes(croot);
591
592 if (window_name_changed(con->window, old_name))
593 ipc_send_window_event("title", con);
594
595 FREE(old_name);
596
597 return true;
598 }
599
600 /*
601 * Called when a window changes its WM_WINDOW_ROLE.
602 *
603 */
handle_windowrole_change(Con * con,xcb_get_property_reply_t * prop)604 static bool handle_windowrole_change(Con *con, xcb_get_property_reply_t *prop) {
605 window_update_role(con->window, prop);
606
607 con = remanage_window(con);
608
609 return true;
610 }
611
612 /*
613 * Expose event means we should redraw our windows (= title bar)
614 *
615 */
handle_expose_event(xcb_expose_event_t * event)616 static void handle_expose_event(xcb_expose_event_t *event) {
617 Con *parent;
618
619 DLOG("window = %08x\n", event->window);
620
621 if ((parent = con_by_frame_id(event->window)) == NULL) {
622 LOG("expose event for unknown window, ignoring\n");
623 return;
624 }
625
626 /* Since we render to our surface on every change anyways, expose events
627 * only tell us that the X server lost (parts of) the window contents. */
628 draw_util_copy_surface(&(parent->frame_buffer), &(parent->frame),
629 0, 0, 0, 0, parent->rect.width, parent->rect.height);
630 xcb_flush(conn);
631 }
632
633 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
634 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
635 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
636 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
637 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
638 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
639 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
640 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
641 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
642 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
643 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
644 #define _NET_WM_MOVERESIZE_CANCEL 11 /* cancel operation */
645
646 #define _NET_MOVERESIZE_WINDOW_X (1 << 8)
647 #define _NET_MOVERESIZE_WINDOW_Y (1 << 9)
648 #define _NET_MOVERESIZE_WINDOW_WIDTH (1 << 10)
649 #define _NET_MOVERESIZE_WINDOW_HEIGHT (1 << 11)
650
651 /*
652 * Handle client messages (EWMH)
653 *
654 */
handle_client_message(xcb_client_message_event_t * event)655 static void handle_client_message(xcb_client_message_event_t *event) {
656 /* If this is a startup notification ClientMessage, the library will handle
657 * it and call our monitor_event() callback. */
658 if (sn_xcb_display_process_event(sndisplay, (xcb_generic_event_t *)event))
659 return;
660
661 LOG("ClientMessage for window 0x%08x\n", event->window);
662 if (event->type == A__NET_WM_STATE) {
663 if (event->format != 32 ||
664 (event->data.data32[1] != A__NET_WM_STATE_FULLSCREEN &&
665 event->data.data32[1] != A__NET_WM_STATE_DEMANDS_ATTENTION &&
666 event->data.data32[1] != A__NET_WM_STATE_STICKY)) {
667 DLOG("Unknown atom in clientmessage of type %d\n", event->data.data32[1]);
668 return;
669 }
670
671 Con *con = con_by_window_id(event->window);
672 if (con == NULL) {
673 DLOG("Could not get window for client message\n");
674 return;
675 }
676
677 if (event->data.data32[1] == A__NET_WM_STATE_FULLSCREEN) {
678 /* Check if the fullscreen state should be toggled */
679 if ((con->fullscreen_mode != CF_NONE &&
680 (event->data.data32[0] == _NET_WM_STATE_REMOVE ||
681 event->data.data32[0] == _NET_WM_STATE_TOGGLE)) ||
682 (con->fullscreen_mode == CF_NONE &&
683 (event->data.data32[0] == _NET_WM_STATE_ADD ||
684 event->data.data32[0] == _NET_WM_STATE_TOGGLE))) {
685 DLOG("toggling fullscreen\n");
686 con_toggle_fullscreen(con, CF_OUTPUT);
687 }
688 } else if (event->data.data32[1] == A__NET_WM_STATE_DEMANDS_ATTENTION) {
689 /* Check if the urgent flag must be set or not */
690 if (event->data.data32[0] == _NET_WM_STATE_ADD)
691 con_set_urgency(con, true);
692 else if (event->data.data32[0] == _NET_WM_STATE_REMOVE)
693 con_set_urgency(con, false);
694 else if (event->data.data32[0] == _NET_WM_STATE_TOGGLE)
695 con_set_urgency(con, !con->urgent);
696 } else if (event->data.data32[1] == A__NET_WM_STATE_STICKY) {
697 DLOG("Received a client message to modify _NET_WM_STATE_STICKY.\n");
698 if (event->data.data32[0] == _NET_WM_STATE_ADD)
699 con->sticky = true;
700 else if (event->data.data32[0] == _NET_WM_STATE_REMOVE)
701 con->sticky = false;
702 else if (event->data.data32[0] == _NET_WM_STATE_TOGGLE)
703 con->sticky = !con->sticky;
704
705 DLOG("New sticky status for con = %p is %i.\n", con, con->sticky);
706 ewmh_update_sticky(con->window->id, con->sticky);
707 output_push_sticky_windows(focused);
708 ewmh_update_wm_desktop();
709 }
710
711 tree_render();
712 } else if (event->type == A__NET_ACTIVE_WINDOW) {
713 if (event->format != 32)
714 return;
715
716 DLOG("_NET_ACTIVE_WINDOW: Window 0x%08x should be activated\n", event->window);
717
718 Con *con = con_by_window_id(event->window);
719 if (con == NULL) {
720 DLOG("Could not get window for client message\n");
721 return;
722 }
723
724 Con *ws = con_get_workspace(con);
725 if (ws == NULL) {
726 DLOG("Window is not being managed, ignoring _NET_ACTIVE_WINDOW\n");
727 return;
728 }
729
730 if (con_is_internal(ws) && ws != workspace_get("__i3_scratch")) {
731 DLOG("Workspace is internal but not scratchpad, ignoring _NET_ACTIVE_WINDOW\n");
732 return;
733 }
734
735 /* data32[0] indicates the source of the request (application or pager) */
736 if (event->data.data32[0] == 2) {
737 /* Always focus the con if it is from a pager, because this is most
738 * likely from some user action */
739 DLOG("This request came from a pager. Focusing con = %p\n", con);
740
741 if (con_is_internal(ws)) {
742 scratchpad_show(con);
743 } else {
744 workspace_show(ws);
745 /* Re-set focus, even if unchanged from i3’s perspective. */
746 focused_id = XCB_NONE;
747 con_activate_unblock(con);
748 }
749 } else {
750 /* Request is from an application. */
751 if (con_is_internal(ws)) {
752 DLOG("Ignoring request to make con = %p active because it's on an internal workspace.\n", con);
753 return;
754 }
755
756 if (config.focus_on_window_activation == FOWA_FOCUS || (config.focus_on_window_activation == FOWA_SMART && workspace_is_visible(ws))) {
757 DLOG("Focusing con = %p\n", con);
758 con_activate_unblock(con);
759 } else if (config.focus_on_window_activation == FOWA_URGENT || (config.focus_on_window_activation == FOWA_SMART && !workspace_is_visible(ws))) {
760 DLOG("Marking con = %p urgent\n", con);
761 con_set_urgency(con, true);
762 } else
763 DLOG("Ignoring request for con = %p.\n", con);
764 }
765
766 tree_render();
767 } else if (event->type == A_I3_SYNC) {
768 xcb_window_t window = event->data.data32[0];
769 uint32_t rnd = event->data.data32[1];
770 sync_respond(window, rnd);
771 } else if (event->type == A__NET_REQUEST_FRAME_EXTENTS) {
772 /*
773 * A client can request an estimate for the frame size which the window
774 * manager will put around it before actually mapping its window. Java
775 * does this (as of openjdk-7).
776 *
777 * Note that the calculation below is not entirely accurate — once you
778 * set a different border type, it’s off. We _could_ request all the
779 * window properties (which have to be set up at this point according
780 * to EWMH), but that seems rather elaborate. The standard explicitly
781 * says the application must cope with an estimate that is not entirely
782 * accurate.
783 */
784 DLOG("_NET_REQUEST_FRAME_EXTENTS for window 0x%08x\n", event->window);
785
786 /* The reply data: approximate frame size */
787 Rect r = {
788 config.default_border_width, /* left */
789 config.default_border_width, /* right */
790 render_deco_height(), /* top */
791 config.default_border_width /* bottom */
792 };
793 xcb_change_property(
794 conn,
795 XCB_PROP_MODE_REPLACE,
796 event->window,
797 A__NET_FRAME_EXTENTS,
798 XCB_ATOM_CARDINAL, 32, 4,
799 &r);
800 xcb_flush(conn);
801 } else if (event->type == A_WM_CHANGE_STATE) {
802 /* http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.4 */
803 if (event->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC) {
804 /* For compatiblity reasons, Wine will request iconic state and cannot ensure that the WM has agreed on it;
805 * immediately revert to normal to avoid being stuck in a paused state. */
806 DLOG("Client has requested iconic state, rejecting. (window = %08x)\n", event->window);
807 long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
808 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, event->window,
809 A_WM_STATE, A_WM_STATE, 32, 2, data);
810 } else {
811 DLOG("Not handling WM_CHANGE_STATE request. (window = %08x, state = %d)\n", event->window, event->data.data32[0]);
812 }
813 } else if (event->type == A__NET_CURRENT_DESKTOP) {
814 /* This request is used by pagers and bars to change the current
815 * desktop likely as a result of some user action. We interpret this as
816 * a request to focus the given workspace. See
817 * https://standards.freedesktop.org/wm-spec/latest/ar01s03.html#idm140251368135008
818 * */
819 DLOG("Request to change current desktop to index %d\n", event->data.data32[0]);
820 Con *ws = ewmh_get_workspace_by_index(event->data.data32[0]);
821 if (ws == NULL) {
822 ELOG("Could not determine workspace for this index, ignoring request.\n");
823 return;
824 }
825
826 DLOG("Handling request to focus workspace %s\n", ws->name);
827 workspace_show(ws);
828 tree_render();
829 } else if (event->type == A__NET_WM_DESKTOP) {
830 uint32_t index = event->data.data32[0];
831 DLOG("Request to move window %d to EWMH desktop index %d\n", event->window, index);
832
833 Con *con = con_by_window_id(event->window);
834 if (con == NULL) {
835 DLOG("Couldn't find con for window %d, ignoring the request.\n", event->window);
836 return;
837 }
838
839 if (index == NET_WM_DESKTOP_ALL) {
840 /* The window is requesting to be visible on all workspaces, so
841 * let's float it and make it sticky. */
842 DLOG("The window was requested to be visible on all workspaces, making it sticky and floating.\n");
843
844 if (floating_enable(con, false)) {
845 con->floating = FLOATING_AUTO_ON;
846
847 con->sticky = true;
848 ewmh_update_sticky(con->window->id, true);
849 output_push_sticky_windows(focused);
850 }
851 } else {
852 Con *ws = ewmh_get_workspace_by_index(index);
853 if (ws == NULL) {
854 ELOG("Could not determine workspace for this index, ignoring request.\n");
855 return;
856 }
857
858 con_move_to_workspace(con, ws, true, false, false);
859 }
860
861 tree_render();
862 ewmh_update_wm_desktop();
863 } else if (event->type == A__NET_CLOSE_WINDOW) {
864 /*
865 * Pagers wanting to close a window MUST send a _NET_CLOSE_WINDOW
866 * client message request to the root window.
867 * https://standards.freedesktop.org/wm-spec/wm-spec-latest.html#idm140200472668896
868 */
869 Con *con = con_by_window_id(event->window);
870 if (con) {
871 DLOG("Handling _NET_CLOSE_WINDOW request (con = %p)\n", con);
872
873 if (event->data.data32[0])
874 last_timestamp = event->data.data32[0];
875
876 tree_close_internal(con, KILL_WINDOW, false);
877 tree_render();
878 } else {
879 DLOG("Couldn't find con for _NET_CLOSE_WINDOW request. (window = %08x)\n", event->window);
880 }
881 } else if (event->type == A__NET_WM_MOVERESIZE) {
882 /*
883 * Client-side decorated Gtk3 windows emit this signal when being
884 * dragged by their GtkHeaderBar
885 */
886 Con *con = con_by_window_id(event->window);
887 if (!con || !con_is_floating(con)) {
888 DLOG("Couldn't find con for _NET_WM_MOVERESIZE request, or con not floating (window = %08x)\n", event->window);
889 return;
890 }
891 DLOG("Handling _NET_WM_MOVERESIZE request (con = %p)\n", con);
892 uint32_t direction = event->data.data32[2];
893 uint32_t x_root = event->data.data32[0];
894 uint32_t y_root = event->data.data32[1];
895 /* construct fake xcb_button_press_event_t */
896 xcb_button_press_event_t fake = {
897 .root_x = x_root,
898 .root_y = y_root,
899 .event_x = x_root - (con->rect.x),
900 .event_y = y_root - (con->rect.y)};
901 switch (direction) {
902 case _NET_WM_MOVERESIZE_MOVE:
903 floating_drag_window(con->parent, &fake, false);
904 break;
905 case _NET_WM_MOVERESIZE_SIZE_TOPLEFT ... _NET_WM_MOVERESIZE_SIZE_LEFT:
906 floating_resize_window(con->parent, false, &fake);
907 break;
908 default:
909 DLOG("_NET_WM_MOVERESIZE direction %d not implemented\n", direction);
910 break;
911 }
912 } else if (event->type == A__NET_MOVERESIZE_WINDOW) {
913 DLOG("Received _NET_MOVE_RESIZE_WINDOW. Handling by faking a configure request.\n");
914
915 void *_generated_event = scalloc(32, 1);
916 xcb_configure_request_event_t *generated_event = _generated_event;
917
918 generated_event->window = event->window;
919 generated_event->response_type = XCB_CONFIGURE_REQUEST;
920
921 generated_event->value_mask = 0;
922 if (event->data.data32[0] & _NET_MOVERESIZE_WINDOW_X) {
923 generated_event->value_mask |= XCB_CONFIG_WINDOW_X;
924 generated_event->x = event->data.data32[1];
925 }
926 if (event->data.data32[0] & _NET_MOVERESIZE_WINDOW_Y) {
927 generated_event->value_mask |= XCB_CONFIG_WINDOW_Y;
928 generated_event->y = event->data.data32[2];
929 }
930 if (event->data.data32[0] & _NET_MOVERESIZE_WINDOW_WIDTH) {
931 generated_event->value_mask |= XCB_CONFIG_WINDOW_WIDTH;
932 generated_event->width = event->data.data32[3];
933 }
934 if (event->data.data32[0] & _NET_MOVERESIZE_WINDOW_HEIGHT) {
935 generated_event->value_mask |= XCB_CONFIG_WINDOW_HEIGHT;
936 generated_event->height = event->data.data32[4];
937 }
938
939 handle_configure_request(generated_event);
940 FREE(generated_event);
941 } else {
942 DLOG("Skipping client message for unhandled type %d\n", event->type);
943 }
944 }
945
handle_window_type(Con * con,xcb_get_property_reply_t * reply)946 static bool handle_window_type(Con *con, xcb_get_property_reply_t *reply) {
947 window_update_type(con->window, reply);
948 return true;
949 }
950
951 /*
952 * Handles the size hints set by a window, but currently only the part necessary for displaying
953 * clients proportionally inside their frames (mplayer for example)
954 *
955 * See ICCCM 4.1.2.3 for more details
956 *
957 */
handle_normal_hints(Con * con,xcb_get_property_reply_t * reply)958 static bool handle_normal_hints(Con *con, xcb_get_property_reply_t *reply) {
959 bool changed = window_update_normal_hints(con->window, reply, NULL);
960
961 if (changed) {
962 Con *floating = con_inside_floating(con);
963 if (floating) {
964 floating_check_size(con, false);
965 tree_render();
966 }
967 }
968
969 FREE(reply);
970 return true;
971 }
972
973 /*
974 * Handles the WM_HINTS property for extracting the urgency state of the window.
975 *
976 */
handle_hints(Con * con,xcb_get_property_reply_t * reply)977 static bool handle_hints(Con *con, xcb_get_property_reply_t *reply) {
978 bool urgency_hint;
979 window_update_hints(con->window, reply, &urgency_hint);
980 con_set_urgency(con, urgency_hint);
981 tree_render();
982 return true;
983 }
984
985 /*
986 * Handles the transient for hints set by a window, signalizing that this window is a popup window
987 * for some other window.
988 *
989 * See ICCCM 4.1.2.6 for more details
990 *
991 */
handle_transient_for(Con * con,xcb_get_property_reply_t * prop)992 static bool handle_transient_for(Con *con, xcb_get_property_reply_t *prop) {
993 window_update_transient_for(con->window, prop);
994 return true;
995 }
996
997 /*
998 * Handles changes of the WM_CLIENT_LEADER atom which specifies if this is a
999 * toolwindow (or similar) and to which window it belongs (logical parent).
1000 *
1001 */
handle_clientleader_change(Con * con,xcb_get_property_reply_t * prop)1002 static bool handle_clientleader_change(Con *con, xcb_get_property_reply_t *prop) {
1003 window_update_leader(con->window, prop);
1004 return true;
1005 }
1006
1007 /*
1008 * Handles FocusIn events which are generated by clients (i3’s focus changes
1009 * don’t generate FocusIn events due to a different EventMask) and updates the
1010 * decorations accordingly.
1011 *
1012 */
handle_focus_in(xcb_focus_in_event_t * event)1013 static void handle_focus_in(xcb_focus_in_event_t *event) {
1014 DLOG("focus change in, for window 0x%08x\n", event->event);
1015
1016 if (event->event == root) {
1017 DLOG("Received focus in for root window, refocusing the focused window.\n");
1018 con_focus(focused);
1019 focused_id = XCB_NONE;
1020 x_push_changes(croot);
1021 }
1022
1023 Con *con;
1024 if ((con = con_by_window_id(event->event)) == NULL || con->window == NULL)
1025 return;
1026 DLOG("That is con %p / %s\n", con, con->name);
1027
1028 if (event->mode == XCB_NOTIFY_MODE_GRAB ||
1029 event->mode == XCB_NOTIFY_MODE_UNGRAB) {
1030 DLOG("FocusIn event for grab/ungrab, ignoring\n");
1031 return;
1032 }
1033
1034 if (event->detail == XCB_NOTIFY_DETAIL_POINTER) {
1035 DLOG("notify detail is pointer, ignoring this event\n");
1036 return;
1037 }
1038
1039 /* Floating windows should be refocused to ensure that they are on top of
1040 * other windows. */
1041 if (focused_id == event->event && !con_inside_floating(con)) {
1042 DLOG("focus matches the currently focused window, not doing anything\n");
1043 return;
1044 }
1045
1046 /* Skip dock clients, they cannot get the i3 focus. */
1047 if (con->parent->type == CT_DOCKAREA) {
1048 DLOG("This is a dock client, not focusing.\n");
1049 return;
1050 }
1051
1052 DLOG("focus is different / refocusing floating window: updating decorations\n");
1053
1054 con_activate_unblock(con);
1055
1056 /* We update focused_id because we don’t need to set focus again */
1057 focused_id = event->event;
1058 tree_render();
1059 }
1060
1061 /*
1062 * Log FocusOut events.
1063 *
1064 */
handle_focus_out(xcb_focus_in_event_t * event)1065 static void handle_focus_out(xcb_focus_in_event_t *event) {
1066 Con *con = con_by_window_id(event->event);
1067 const char *window_name, *mode, *detail;
1068
1069 if (con != NULL) {
1070 window_name = con->name;
1071 if (window_name == NULL) {
1072 window_name = "<unnamed con>";
1073 }
1074 } else if (event->event == root) {
1075 window_name = "<the root window>";
1076 } else {
1077 window_name = "<unknown window>";
1078 }
1079
1080 switch (event->mode) {
1081 case XCB_NOTIFY_MODE_NORMAL:
1082 mode = "Normal";
1083 break;
1084 case XCB_NOTIFY_MODE_GRAB:
1085 mode = "Grab";
1086 break;
1087 case XCB_NOTIFY_MODE_UNGRAB:
1088 mode = "Ungrab";
1089 break;
1090 case XCB_NOTIFY_MODE_WHILE_GRABBED:
1091 mode = "WhileGrabbed";
1092 break;
1093 default:
1094 mode = "<unknown>";
1095 break;
1096 }
1097
1098 switch (event->detail) {
1099 case XCB_NOTIFY_DETAIL_ANCESTOR:
1100 detail = "Ancestor";
1101 break;
1102 case XCB_NOTIFY_DETAIL_VIRTUAL:
1103 detail = "Virtual";
1104 break;
1105 case XCB_NOTIFY_DETAIL_INFERIOR:
1106 detail = "Inferior";
1107 break;
1108 case XCB_NOTIFY_DETAIL_NONLINEAR:
1109 detail = "Nonlinear";
1110 break;
1111 case XCB_NOTIFY_DETAIL_NONLINEAR_VIRTUAL:
1112 detail = "NonlinearVirtual";
1113 break;
1114 case XCB_NOTIFY_DETAIL_POINTER:
1115 detail = "Pointer";
1116 break;
1117 case XCB_NOTIFY_DETAIL_POINTER_ROOT:
1118 detail = "PointerRoot";
1119 break;
1120 case XCB_NOTIFY_DETAIL_NONE:
1121 detail = "NONE";
1122 break;
1123 default:
1124 detail = "unknown";
1125 break;
1126 }
1127
1128 DLOG("focus change out: window 0x%08x (con %p, %s) lost focus with detail=%s, mode=%s\n", event->event, con, window_name, detail, mode);
1129 }
1130
1131 /*
1132 * Handles ConfigureNotify events for the root window, which are generated when
1133 * the monitor configuration changed.
1134 *
1135 */
handle_configure_notify(xcb_configure_notify_event_t * event)1136 static void handle_configure_notify(xcb_configure_notify_event_t *event) {
1137 if (event->event != root) {
1138 DLOG("ConfigureNotify for non-root window 0x%08x, ignoring\n", event->event);
1139 return;
1140 }
1141 DLOG("ConfigureNotify for root window 0x%08x\n", event->event);
1142
1143 if (force_xinerama) {
1144 return;
1145 }
1146 randr_query_outputs();
1147
1148 ipc_send_event("output", I3_IPC_EVENT_OUTPUT, "{\"change\":\"unspecified\"}");
1149 }
1150
1151 /*
1152 * Handles SelectionClear events for the root window, which are generated when
1153 * we lose ownership of a selection.
1154 */
handle_selection_clear(xcb_selection_clear_event_t * event)1155 static void handle_selection_clear(xcb_selection_clear_event_t *event) {
1156 if (event->selection != wm_sn) {
1157 DLOG("SelectionClear for unknown selection %d, ignoring\n", event->selection);
1158 return;
1159 }
1160 LOG("Lost WM_Sn selection, exiting.\n");
1161 exit(EXIT_SUCCESS);
1162
1163 /* unreachable */
1164 }
1165
1166 /*
1167 * Handles the WM_CLASS property for assignments and criteria selection.
1168 *
1169 */
handle_class_change(Con * con,xcb_get_property_reply_t * prop)1170 static bool handle_class_change(Con *con, xcb_get_property_reply_t *prop) {
1171 window_update_class(con->window, prop);
1172 con = remanage_window(con);
1173 return true;
1174 }
1175
1176 /*
1177 * Handles the WM_CLIENT_MACHINE property for assignments and criteria selection.
1178 *
1179 */
handle_machine_change(Con * con,xcb_get_property_reply_t * prop)1180 static bool handle_machine_change(Con *con, xcb_get_property_reply_t *prop) {
1181 window_update_machine(con->window, prop);
1182 con = remanage_window(con);
1183 return true;
1184 }
1185
1186 /*
1187 * Handles the _MOTIF_WM_HINTS property of specifing window deocration settings.
1188 *
1189 */
handle_motif_hints_change(Con * con,xcb_get_property_reply_t * prop)1190 static bool handle_motif_hints_change(Con *con, xcb_get_property_reply_t *prop) {
1191 border_style_t motif_border_style;
1192 window_update_motif_hints(con->window, prop, &motif_border_style);
1193
1194 if (motif_border_style != con->border_style && motif_border_style != BS_NORMAL) {
1195 DLOG("Update border style of con %p to %d\n", con, motif_border_style);
1196 con_set_border_style(con, motif_border_style, con->current_border_width);
1197
1198 x_push_changes(croot);
1199 }
1200
1201 return true;
1202 }
1203
1204 /*
1205 * Handles the _NET_WM_STRUT_PARTIAL property for allocating space for dock clients.
1206 *
1207 */
handle_strut_partial_change(Con * con,xcb_get_property_reply_t * prop)1208 static bool handle_strut_partial_change(Con *con, xcb_get_property_reply_t *prop) {
1209 window_update_strut_partial(con->window, prop);
1210
1211 /* we only handle this change for dock clients */
1212 if (con->parent == NULL || con->parent->type != CT_DOCKAREA) {
1213 return true;
1214 }
1215
1216 Con *search_at = croot;
1217 Con *output = con_get_output(con);
1218 if (output != NULL) {
1219 DLOG("Starting search at output %s\n", output->name);
1220 search_at = output;
1221 }
1222
1223 /* find out the desired position of this dock window */
1224 if (con->window->reserved.top > 0 && con->window->reserved.bottom == 0) {
1225 DLOG("Top dock client\n");
1226 con->window->dock = W_DOCK_TOP;
1227 } else if (con->window->reserved.top == 0 && con->window->reserved.bottom > 0) {
1228 DLOG("Bottom dock client\n");
1229 con->window->dock = W_DOCK_BOTTOM;
1230 } else {
1231 DLOG("Ignoring invalid reserved edges (_NET_WM_STRUT_PARTIAL), using position as fallback:\n");
1232 if (con->geometry.y < (search_at->rect.height / 2)) {
1233 DLOG("geom->y = %d < rect.height / 2 = %d, it is a top dock client\n",
1234 con->geometry.y, (search_at->rect.height / 2));
1235 con->window->dock = W_DOCK_TOP;
1236 } else {
1237 DLOG("geom->y = %d >= rect.height / 2 = %d, it is a bottom dock client\n",
1238 con->geometry.y, (search_at->rect.height / 2));
1239 con->window->dock = W_DOCK_BOTTOM;
1240 }
1241 }
1242
1243 /* find the dockarea */
1244 Con *dockarea = con_for_window(search_at, con->window, NULL);
1245 assert(dockarea != NULL);
1246
1247 /* attach the dock to the dock area */
1248 con_detach(con);
1249 con_attach(con, dockarea, true);
1250
1251 tree_render();
1252
1253 return true;
1254 }
1255
1256 /*
1257 * Handles the _I3_FLOATING_WINDOW property to properly run assignments for
1258 * floating window changes.
1259 *
1260 * This is needed to correctly run the assignments after changes in floating
1261 * windows which are triggered by user commands (floating enable | disable). In
1262 * that case, we can't call run_assignments because it will modify the parser
1263 * state when it needs to parse the user-specified action, breaking the parser
1264 * state for the original command.
1265 *
1266 */
handle_i3_floating(Con * con,xcb_get_property_reply_t * prop)1267 static bool handle_i3_floating(Con *con, xcb_get_property_reply_t *prop) {
1268 DLOG("floating change for con %p\n", con);
1269
1270 remanage_window(con);
1271
1272 return true;
1273 }
1274
handle_windowicon_change(Con * con,xcb_get_property_reply_t * prop)1275 static bool handle_windowicon_change(Con *con, xcb_get_property_reply_t *prop) {
1276 window_update_icon(con->window, prop);
1277
1278 x_push_changes(croot);
1279
1280 return true;
1281 }
1282
1283 /* Returns false if the event could not be processed (e.g. the window could not
1284 * be found), true otherwise */
1285 typedef bool (*cb_property_handler_t)(Con *con, xcb_get_property_reply_t *property);
1286
1287 struct property_handler_t {
1288 xcb_atom_t atom;
1289 uint32_t long_len;
1290 cb_property_handler_t cb;
1291 };
1292
1293 static struct property_handler_t property_handlers[] = {
1294 {0, 128, handle_windowname_change},
1295 {0, UINT_MAX, handle_hints},
1296 {0, 128, handle_windowname_change_legacy},
1297 {0, UINT_MAX, handle_normal_hints},
1298 {0, UINT_MAX, handle_clientleader_change},
1299 {0, UINT_MAX, handle_transient_for},
1300 {0, 128, handle_windowrole_change},
1301 {0, 128, handle_class_change},
1302 {0, UINT_MAX, handle_strut_partial_change},
1303 {0, UINT_MAX, handle_window_type},
1304 {0, UINT_MAX, handle_i3_floating},
1305 {0, 128, handle_machine_change},
1306 {0, 5 * sizeof(uint64_t), handle_motif_hints_change},
1307 {0, UINT_MAX, handle_windowicon_change}};
1308 #define NUM_HANDLERS (sizeof(property_handlers) / sizeof(struct property_handler_t))
1309
1310 /*
1311 * Sets the appropriate atoms for the property handlers after the atoms were
1312 * received from X11
1313 *
1314 */
property_handlers_init(void)1315 void property_handlers_init(void) {
1316 sn_monitor_context_new(sndisplay, conn_screen, startup_monitor_event, NULL, NULL);
1317
1318 property_handlers[0].atom = A__NET_WM_NAME;
1319 property_handlers[1].atom = XCB_ATOM_WM_HINTS;
1320 property_handlers[2].atom = XCB_ATOM_WM_NAME;
1321 property_handlers[3].atom = XCB_ATOM_WM_NORMAL_HINTS;
1322 property_handlers[4].atom = A_WM_CLIENT_LEADER;
1323 property_handlers[5].atom = XCB_ATOM_WM_TRANSIENT_FOR;
1324 property_handlers[6].atom = A_WM_WINDOW_ROLE;
1325 property_handlers[7].atom = XCB_ATOM_WM_CLASS;
1326 property_handlers[8].atom = A__NET_WM_STRUT_PARTIAL;
1327 property_handlers[9].atom = A__NET_WM_WINDOW_TYPE;
1328 property_handlers[10].atom = A_I3_FLOATING_WINDOW;
1329 property_handlers[11].atom = XCB_ATOM_WM_CLIENT_MACHINE;
1330 property_handlers[12].atom = A__MOTIF_WM_HINTS;
1331 property_handlers[13].atom = A__NET_WM_ICON;
1332 }
1333
property_notify(uint8_t state,xcb_window_t window,xcb_atom_t atom)1334 static void property_notify(uint8_t state, xcb_window_t window, xcb_atom_t atom) {
1335 struct property_handler_t *handler = NULL;
1336 xcb_get_property_reply_t *propr = NULL;
1337 xcb_generic_error_t *err = NULL;
1338 Con *con;
1339
1340 for (size_t c = 0; c < NUM_HANDLERS; c++) {
1341 if (property_handlers[c].atom != atom)
1342 continue;
1343
1344 handler = &property_handlers[c];
1345 break;
1346 }
1347
1348 if (handler == NULL) {
1349 //DLOG("Unhandled property notify for atom %d (0x%08x)\n", atom, atom);
1350 return;
1351 }
1352
1353 if ((con = con_by_window_id(window)) == NULL || con->window == NULL) {
1354 DLOG("Received property for atom %d for unknown client\n", atom);
1355 return;
1356 }
1357
1358 if (state != XCB_PROPERTY_DELETE) {
1359 xcb_get_property_cookie_t cookie = xcb_get_property(conn, 0, window, atom, XCB_GET_PROPERTY_TYPE_ANY, 0, handler->long_len);
1360 propr = xcb_get_property_reply(conn, cookie, &err);
1361 if (err != NULL) {
1362 DLOG("got error %d when getting property of atom %d\n", err->error_code, atom);
1363 FREE(err);
1364 return;
1365 }
1366 }
1367
1368 /* the handler will free() the reply unless it returns false */
1369 if (!handler->cb(con, propr))
1370 FREE(propr);
1371 }
1372
1373 /*
1374 * Takes an xcb_generic_event_t and calls the appropriate handler, based on the
1375 * event type.
1376 *
1377 */
handle_event(int type,xcb_generic_event_t * event)1378 void handle_event(int type, xcb_generic_event_t *event) {
1379 if (type != XCB_MOTION_NOTIFY)
1380 DLOG("event type %d, xkb_base %d\n", type, xkb_base);
1381
1382 if (randr_base > -1 &&
1383 type == randr_base + XCB_RANDR_SCREEN_CHANGE_NOTIFY) {
1384 handle_screen_change(event);
1385 return;
1386 }
1387
1388 if (xkb_base > -1 && type == xkb_base) {
1389 DLOG("xkb event, need to handle it.\n");
1390
1391 xcb_xkb_state_notify_event_t *state = (xcb_xkb_state_notify_event_t *)event;
1392 if (state->xkbType == XCB_XKB_NEW_KEYBOARD_NOTIFY) {
1393 DLOG("xkb new keyboard notify, sequence %d, time %d\n", state->sequence, state->time);
1394 xcb_key_symbols_free(keysyms);
1395 keysyms = xcb_key_symbols_alloc(conn);
1396 if (((xcb_xkb_new_keyboard_notify_event_t *)event)->changed & XCB_XKB_NKN_DETAIL_KEYCODES)
1397 (void)load_keymap();
1398 ungrab_all_keys(conn);
1399 translate_keysyms();
1400 grab_all_keys(conn);
1401 } else if (state->xkbType == XCB_XKB_MAP_NOTIFY) {
1402 if (event_is_ignored(event->sequence, type)) {
1403 DLOG("Ignoring map notify event for sequence %d.\n", state->sequence);
1404 } else {
1405 DLOG("xkb map notify, sequence %d, time %d\n", state->sequence, state->time);
1406 add_ignore_event(event->sequence, type);
1407 xcb_key_symbols_free(keysyms);
1408 keysyms = xcb_key_symbols_alloc(conn);
1409 ungrab_all_keys(conn);
1410 translate_keysyms();
1411 grab_all_keys(conn);
1412 (void)load_keymap();
1413 }
1414 } else if (state->xkbType == XCB_XKB_STATE_NOTIFY) {
1415 DLOG("xkb state group = %d\n", state->group);
1416 if (xkb_current_group == state->group)
1417 return;
1418 xkb_current_group = state->group;
1419 ungrab_all_keys(conn);
1420 grab_all_keys(conn);
1421 }
1422
1423 return;
1424 }
1425
1426 if (shape_supported && type == shape_base + XCB_SHAPE_NOTIFY) {
1427 xcb_shape_notify_event_t *shape = (xcb_shape_notify_event_t *)event;
1428
1429 DLOG("shape_notify_event for window 0x%08x, shape_kind = %d, shaped = %d\n",
1430 shape->affected_window, shape->shape_kind, shape->shaped);
1431
1432 Con *con = con_by_window_id(shape->affected_window);
1433 if (con == NULL) {
1434 LOG("Not a managed window 0x%08x, ignoring shape_notify_event\n",
1435 shape->affected_window);
1436 return;
1437 }
1438
1439 if (shape->shape_kind == XCB_SHAPE_SK_BOUNDING ||
1440 shape->shape_kind == XCB_SHAPE_SK_INPUT) {
1441 x_set_shape(con, shape->shape_kind, shape->shaped);
1442 }
1443
1444 return;
1445 }
1446
1447 switch (type) {
1448 case XCB_KEY_PRESS:
1449 case XCB_KEY_RELEASE:
1450 handle_key_press((xcb_key_press_event_t *)event);
1451 break;
1452
1453 case XCB_BUTTON_PRESS:
1454 case XCB_BUTTON_RELEASE:
1455 handle_button_press((xcb_button_press_event_t *)event);
1456 break;
1457
1458 case XCB_MAP_REQUEST:
1459 handle_map_request((xcb_map_request_event_t *)event);
1460 break;
1461
1462 case XCB_UNMAP_NOTIFY:
1463 handle_unmap_notify_event((xcb_unmap_notify_event_t *)event);
1464 break;
1465
1466 case XCB_DESTROY_NOTIFY:
1467 handle_destroy_notify_event((xcb_destroy_notify_event_t *)event);
1468 break;
1469
1470 case XCB_EXPOSE:
1471 if (((xcb_expose_event_t *)event)->count == 0) {
1472 handle_expose_event((xcb_expose_event_t *)event);
1473 }
1474
1475 break;
1476
1477 case XCB_MOTION_NOTIFY:
1478 handle_motion_notify((xcb_motion_notify_event_t *)event);
1479 break;
1480
1481 /* Enter window = user moved their mouse over the window */
1482 case XCB_ENTER_NOTIFY:
1483 handle_enter_notify((xcb_enter_notify_event_t *)event);
1484 break;
1485
1486 /* Client message are sent to the root window. The only interesting
1487 * client message for us is _NET_WM_STATE, we honour
1488 * _NET_WM_STATE_FULLSCREEN and _NET_WM_STATE_DEMANDS_ATTENTION */
1489 case XCB_CLIENT_MESSAGE:
1490 handle_client_message((xcb_client_message_event_t *)event);
1491 break;
1492
1493 /* Configure request = window tried to change size on its own */
1494 case XCB_CONFIGURE_REQUEST:
1495 handle_configure_request((xcb_configure_request_event_t *)event);
1496 break;
1497
1498 /* Mapping notify = keyboard mapping changed (Xmodmap), re-grab bindings */
1499 case XCB_MAPPING_NOTIFY:
1500 handle_mapping_notify((xcb_mapping_notify_event_t *)event);
1501 break;
1502
1503 case XCB_FOCUS_IN:
1504 handle_focus_in((xcb_focus_in_event_t *)event);
1505 break;
1506
1507 case XCB_FOCUS_OUT:
1508 handle_focus_out((xcb_focus_out_event_t *)event);
1509 break;
1510
1511 case XCB_PROPERTY_NOTIFY: {
1512 xcb_property_notify_event_t *e = (xcb_property_notify_event_t *)event;
1513 last_timestamp = e->time;
1514 property_notify(e->state, e->window, e->atom);
1515 break;
1516 }
1517
1518 case XCB_CONFIGURE_NOTIFY:
1519 handle_configure_notify((xcb_configure_notify_event_t *)event);
1520 break;
1521
1522 case XCB_SELECTION_CLEAR:
1523 handle_selection_clear((xcb_selection_clear_event_t *)event);
1524 break;
1525
1526 default:
1527 //DLOG("Unhandled event of type %d\n", type);
1528 break;
1529 }
1530 }
1531