Debian Codegraph @

src/event.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) 2019, Yuxuan Shui <[email protected]>
3
4 #include <stdint.h>
5 #include <stdio.h>
6
7 #include <X11/Xlibint.h>
8 #include <xcb/damage.h>
9 #include <xcb/randr.h>
10 #include <xcb/xcb_event.h>
11 #include <xcb/xproto.h>
12
13 #include <picom/types.h>
14
15 #include "atom.h"
16 #include "c2.h"
17 #include "common.h"
18 #include "compiler.h"
19 #include "config.h"
20 #include "event.h"
21 #include "log.h"
22 #include "picom.h"
23 #include "region.h"
24 #include "wm/defs.h"
25 #include "wm/wm.h"
26 #include "x.h"
27
28 /// Event handling with X is complicated. Handling events with other events possibly
29 /// in-flight is no good. Because your internal state won't be up to date. Also, querying
30 /// the server while events are in-flight is not good. Because events later in the queue
31 /// might container information you are querying. Thus those events will cause you to do
32 /// unnecessary updates even when you already have the latest information (remember, you
33 /// made the query when those events were already in the queue. so the reply you got is
34 /// more up-to-date than the events). Also, handling events when other client are making
35 /// concurrent requests is not good. Because the server states are changing without you
36 /// knowing them. This is super racy, and can cause lots of potential problems.
37 ///
38 /// All of above mandates we do these things:
39 /// 1. Grab server when handling events
40 /// 2. Make sure the event queue is empty before we make any query to the server
41 ///
42 /// Notice (2) has a dependency circle. To handle events, you sometimes need to make
43 /// queries. But to make queries you have to first handle events.
44 ///
45 /// To break that circle, we split all event handling into top and bottom halves. The
46 /// bottom half will just look at the event itself, update as much state as they can
47 /// without making queries, then queue up necessary works need to be done by the top half.
48 /// The top half will do all the other necessary updates. Before entering the top half, we
49 /// grab the server and make sure the event queue is empty.
50 ///
51 /// When top half finished, we enter the render stage, where no server state should be
52 /// queried. All rendering should be done with our internal knowledge of the server state.
53 ///
54 /// P.S. There is another reason to avoid sending any request to the server as much as
55 /// possible. To make sure requests are sent, flushes are needed. And `xcb_flush`/`XFlush`
56 /// functions may read more events from the server into their queues. This is
57 /// undesirable, see the comments on `handle_queued_x_events` in picom.c for more details.
58
59 // TODO(yshui) the things described above. This is mostly done, maybe some of
60 // the functions here is still making unnecessary queries, we need
61 // to do some auditing to be sure.
62
63 /**
64 * Get a window's name from window ID.
65 */
66 static inline const char *ev_window_name(session_t *ps, xcb_window_t wid) {
67 char *name = "";
68 if (wid) {
69 name = "(Failed to get title)";
70 if (ps->c.screen_info->root == wid) {
71 name = "(Root window)";
72 } else if (ps->overlay == wid) {
73 name = "(Overlay)";
74 } else {
75 auto cursor = wm_find(ps->wm, wid);
76 if (!cursor || !wm_ref_deref(cursor)) {
77 cursor = wm_find_by_client(ps->wm, wid);
78 }
79
80 auto w = cursor ? wm_ref_deref(cursor) : NULL;
81 if (w && w->name) {
82 name = w->name;
83 }
84 }
85 }
86 return name;
87 }
88
89 static inline xcb_window_t attr_pure ev_window(session_t *ps, xcb_generic_event_t *ev) {
90 switch (ev->response_type) {
91 case XCB_FOCUS_IN:
92 case XCB_FOCUS_OUT: return ((xcb_focus_in_event_t *)ev)->event;
93 case XCB_CREATE_NOTIFY: return ((xcb_create_notify_event_t *)ev)->window;
94 case XCB_CONFIGURE_NOTIFY: return ((xcb_configure_notify_event_t *)ev)->window;
95 case XCB_DESTROY_NOTIFY: return ((xcb_destroy_notify_event_t *)ev)->window;
96 case XCB_MAP_NOTIFY: return ((xcb_map_notify_event_t *)ev)->window;
97 case XCB_UNMAP_NOTIFY: return ((xcb_unmap_notify_event_t *)ev)->window;
98 case XCB_REPARENT_NOTIFY: return ((xcb_reparent_notify_event_t *)ev)->window;
99 case XCB_CIRCULATE_NOTIFY: return ((xcb_circulate_notify_event_t *)ev)->window;
100 case XCB_EXPOSE: return ((xcb_expose_event_t *)ev)->window;
101 case XCB_PROPERTY_NOTIFY: return ((xcb_property_notify_event_t *)ev)->window;
102 case XCB_CLIENT_MESSAGE: return ((xcb_client_message_event_t *)ev)->window;
103 default:
104 if (ps->c.e.damage_event + XCB_DAMAGE_NOTIFY == ev->response_type) {
105 return ((xcb_damage_notify_event_t *)ev)->drawable;
106 }
107
108 if (ps->c.e.has_shape && ev->response_type == ps->c.e.shape_event) {
109 return ((xcb_shape_notify_event_t *)ev)->affected_window;
110 }
111
112 return 0;
113 }
114 }
115
116 #define CASESTRRET(s) \
117 case XCB_##s: return #s;
118
119 static inline const char *ev_name(session_t *ps, xcb_generic_event_t *ev) {
120 static char buf[128];
121 switch (XCB_EVENT_RESPONSE_TYPE(ev)) {
122 CASESTRRET(FOCUS_IN);
123 CASESTRRET(FOCUS_OUT);
124 CASESTRRET(CREATE_NOTIFY);
125 CASESTRRET(CONFIGURE_NOTIFY);
126 CASESTRRET(DESTROY_NOTIFY);
127 CASESTRRET(MAP_NOTIFY);
128 CASESTRRET(UNMAP_NOTIFY);
129 CASESTRRET(REPARENT_NOTIFY);
130 CASESTRRET(CIRCULATE_NOTIFY);
131 CASESTRRET(EXPOSE);
132 CASESTRRET(PROPERTY_NOTIFY);
133 CASESTRRET(CLIENT_MESSAGE);
134 }
135
136 if (ps->c.e.damage_event + XCB_DAMAGE_NOTIFY == ev->response_type) {
137 return "DAMAGE_NOTIFY";
138 }
139
140 if (ps->c.e.has_shape && ev->response_type == ps->c.e.shape_event) {
141 return "SHAPE_NOTIFY";
142 }
143
144 if (ps->c.e.has_sync) {
145 int o = ev->response_type - ps->c.e.sync_event;
146 switch (o) {
147 CASESTRRET(SYNC_COUNTER_NOTIFY);
148 CASESTRRET(SYNC_ALARM_NOTIFY);
149 }
150 }
151
152 sprintf(buf, "Event %d", ev->response_type);
153
154 return buf;
155 }
156
157 static inline const char *attr_pure ev_focus_mode_name(xcb_focus_in_event_t *ev) {
158 #undef CASESTRRET
159 #define CASESTRRET(s) \
160 case XCB_NOTIFY_MODE_##s: return #s
161
162 switch (ev->mode) {
163 CASESTRRET(NORMAL);
164 CASESTRRET(WHILE_GRABBED);
165 CASESTRRET(GRAB);
166 CASESTRRET(UNGRAB);
167 }
168
169 return "Unknown";
170 }
171
172 static inline const char *attr_pure ev_focus_detail_name(xcb_focus_in_event_t *ev) {
173 #undef CASESTRRET
174 #define CASESTRRET(s) \
175 case XCB_NOTIFY_DETAIL_##s: return #s
176 switch (ev->detail) {
177 CASESTRRET(ANCESTOR);
178 CASESTRRET(VIRTUAL);
179 CASESTRRET(INFERIOR);
180 CASESTRRET(NONLINEAR);
181 CASESTRRET(NONLINEAR_VIRTUAL);
182 CASESTRRET(POINTER);
183 CASESTRRET(POINTER_ROOT);
184 CASESTRRET(NONE);
185 }
186
187 return "Unknown";
188 }
189
190 #undef CASESTRRET
191
192 struct ev_ewmh_active_win_request {
193 struct x_async_request_base base;
194 session_t *ps;
195 };
196
197 /// Update current active window based on EWMH _NET_ACTIVE_WIN.
198 ///
199 /// Does not change anything if we fail to get the attribute or the window
200 /// returned could not be found.
201 static void
202 update_ewmh_active_win(struct x_connection *c, struct x_async_request_base *req_base,
203 const xcb_raw_generic_event_t *reply_or_error) {
204 auto ps = ((struct ev_ewmh_active_win_request *)req_base)->ps;
205 free(req_base);
206
207 ps->pending_focus_check = false;
208
209 if (reply_or_error == NULL) {
210 // Connection shutdown
211 return;
212 }
213
214 if (reply_or_error->response_type == 0) {
215 log_error("Failed to get _NET_ACTIVE_WINDOW: %s",
216 x_strerror(c, (xcb_generic_error_t *)reply_or_error));
217 return;
218 }
219
220 // Search for the window
221 auto reply = (const xcb_get_property_reply_t *)reply_or_error;
222 if (reply->type == XCB_NONE || xcb_get_property_value_length(reply) < 4) {
223 log_debug("EWMH _NET_ACTIVE_WINDOW not set.");
224 return;
225 }
226
227 auto wid = *(xcb_window_t *)xcb_get_property_value(reply);
228 log_debug("EWMH _NET_ACTIVE_WINDOW is %#010x", wid);
229
230 // Mark the window focused. No need to unfocus the previous one.
231 auto cursor = wm_find_by_client(ps->wm, wid);
232 wm_ref_set_focused(ps->wm, cursor);
233 ps->pending_updates = true;
234 log_debug("%#010x (%s) focused.", wid, win_wm_ref_name(cursor));
235 }
236
237 struct ev_recheck_focus_request {
238 struct x_async_request_base base;
239 session_t *ps;
240 };
241
242 /**
243 * Recheck currently focused window and set its <code>w->focused</code>
244 * to true.
245 *
246 * @param ps current session
247 * @return struct _win of currently focused window, NULL if not found
248 */
249 static void recheck_focus(struct x_connection *c, struct x_async_request_base *req_base,
250 const xcb_raw_generic_event_t *reply_or_error) {
251 auto ps = ((struct ev_ewmh_active_win_request *)req_base)->ps;
252 free(req_base);
253
254 ps->pending_focus_check = false;
255
256 if (reply_or_error == NULL) {
257 // Connection shutdown
258 return;
259 }
260
261 // Determine the currently focused window so we can apply appropriate
262 // opacity on it
263 if (reply_or_error->response_type == 0) {
264 // Not able to get input focus means very not good things...
265 auto e = (xcb_generic_error_t *)reply_or_error;
266 log_error_x_error(c, e, "Failed to get focused window.");
267 return;
268 }
269
270 auto reply = (const xcb_get_input_focus_reply_t *)reply_or_error;
271 xcb_window_t wid = reply->focus;
272 log_debug("Current focused window is %#010x", wid);
273 if (wid == XCB_NONE || wid == XCB_INPUT_FOCUS_POINTER_ROOT ||
274 wid == ps->c.screen_info->root) {
275 // Focus is not on a toplevel.
276 return;
277 }
278
279 auto cursor = wm_find(ps->wm, wid);
280 assert(cursor != NULL || !wm_is_consistent(ps->wm));
281
282 if (cursor != NULL) {
283 cursor = wm_ref_toplevel_of(ps->wm, cursor);
284 assert(cursor != NULL || !wm_is_consistent(ps->wm));
285 }
286
287 // And we set the focus state here
288 wm_ref_set_focused(ps->wm, cursor);
289 ps->pending_updates = true;
290 log_debug("%#010x (%s) focused.", wid, win_wm_ref_name(cursor));
291 }
292
293 void ev_update_focused(struct session *ps) {
294 if (ps->pending_focus_check) {
295 return;
296 }
297
298 if (ps->o.use_ewmh_active_win) {
299 auto req = ccalloc(1, struct ev_ewmh_active_win_request);
300 req->base.sequence =
301 xcb_get_property(ps->c.c, 0, ps->c.screen_info->root,
302 ps->atoms->a_NET_ACTIVE_WINDOW, XCB_ATOM_WINDOW, 0, 1)
303 .sequence;
304 req->base.callback = update_ewmh_active_win;
305 req->ps = ps;
306 x_await_request(&ps->c, &req->base);
307 log_debug("Started async request to get _NET_ACTIVE_WINDOW");
308 } else {
309 auto req = ccalloc(1, struct ev_recheck_focus_request);
310 req->base.sequence = xcb_get_input_focus(ps->c.c).sequence;
311 req->base.callback = recheck_focus;
312 req->ps = ps;
313 x_await_request(&ps->c, &req->base);
314 log_debug("Started async request to recheck focus");
315 }
316
317 ps->pending_focus_check = true;
318 }
319
320 static inline void ev_focus_change(session_t *ps) {
321 if (ps->o.use_ewmh_active_win) {
322 // Not using focus_in/focus_out events.
323 return;
324 }
325 ev_update_focused(ps);
326 }
327
328 static inline void ev_focus_in(session_t *ps, xcb_focus_in_event_t *ev) {
329 log_debug("{ mode: %s, detail: %s }", ev_focus_mode_name(ev),
330 ev_focus_detail_name(ev));
331 ev_focus_change(ps);
332 }
333
334 static inline void ev_focus_out(session_t *ps, xcb_focus_out_event_t *ev) {
335 log_debug("{ mode: %s, detail: %s }", ev_focus_mode_name(ev),
336 ev_focus_detail_name(ev));
337 ev_focus_change(ps);
338 }
339
340 static inline void ev_create_notify(session_t *ps, xcb_create_notify_event_t *ev) {
341 auto parent = wm_find(ps->wm, ev->parent);
342 if (parent == NULL) {
343 log_error("Create notify received for window %#010x, but its parent "
344 "window %#010x is not in our tree. Expect malfunction.",
345 ev->window, ev->parent);
346 assert(false);
347 }
348 wm_import_start(ps->wm, &ps->c, ps->atoms, ev->window, parent);
349 }
350
351 /// Handle configure event of a regular window
352 static void configure_win(session_t *ps, xcb_configure_notify_event_t *ce) {
353 auto cursor = wm_find(ps->wm, ce->window);
354
355 if (!cursor) {
356 if (wm_is_consistent(ps->wm)) {
357 log_error("Configure event received for unknown window %#010x",
358 ce->window);
359 assert(false);
360 }
361 return;
362 }
363
364 wm_stack_move_to_above(ps->wm, cursor, ce->above_sibling);
365
366 auto w = wm_ref_deref(cursor);
367 if (!w) {
368 log_debug("Window %#010x is unmanaged.", ce->window);
369 return;
370 }
371
372 bool changed = win_set_pending_geometry(w, win_geometry_from_configure_notify(ce)) |
373 (w->a.override_redirect != ce->override_redirect);
374 w->a.override_redirect = ce->override_redirect;
375
376 if (w->state == WSTATE_MAPPED) {
377 ps->pending_updates |= changed;
378 }
379 }
380
381 static inline void ev_configure_notify(session_t *ps, xcb_configure_notify_event_t *ev) {
382 log_debug("{ event: %#010x, id: %#010x, above: %#010x, override_redirect: %d }",
383 ev->event, ev->window, ev->above_sibling, ev->override_redirect);
384
385 if (ps->overlay && ev->window == ps->overlay) {
386 return;
387 }
388
389 if (ev->window == ps->c.screen_info->root) {
390 configure_root(ps);
391 } else {
392 if (ev->window == ev->event) {
393 return;
394 }
395
396 configure_win(ps, ev);
397 }
398 }
399
400 static inline void ev_destroy_notify(session_t *ps, xcb_destroy_notify_event_t *ev) {
401 log_debug("{ event: %#010x, id: %#010x }", ev->event, ev->window);
402 // If we hit an ABA problem, it is possible to get a DestroyNotify event from a
403 // parent for its child, but not from the child for itself.
404 if (ev->event != ev->window) {
405 wm_disconnect(ps->wm, ev->window, ev->event, XCB_NONE);
406 } else {
407 wm_destroy(ps->wm, ev->window);
408 }
409 }
410
411 static inline void ev_map_notify(session_t *ps, xcb_map_notify_event_t *ev) {
412 if (ev->window == ev->event) {
413 return;
414 }
415
416 // Unmap overlay window if it got mapped but we are currently not
417 // in redirected state.
418 if (ps->overlay && ev->window == ps->overlay) {
419 if (!ps->redirected) {
420 log_debug("Overlay is mapped while we are not redirected");
421 auto succeeded =
422 XCB_AWAIT_VOID(xcb_unmap_window, &ps->c, ps->overlay);
423 if (!succeeded) {
424 log_error("Failed to unmap the overlay window");
425 }
426 }
427 // We don't track the overlay window, so we can return
428 return;
429 }
430
431 auto cursor = wm_find(ps->wm, ev->window);
432 if (cursor == NULL) {
433 if (wm_is_consistent(ps->wm)) {
434 log_debug("Map event received for unknown window %#010x, overlay "
435 "is %#010x",
436 ev->window, ps->overlay);
437 assert(false);
438 }
439 return;
440 }
441
442 auto w = wm_ref_deref(cursor);
443 if (w == NULL) {
444 return;
445 }
446 win_set_flags(w, WIN_FLAGS_MAPPED);
447 // We set `ever_damaged` to false here, instead of in `map_win_start`,
448 // because we might receive damage events before that function is called
449 // (which is called when we handle the `WIN_FLAGS_MAPPED` flag), in
450 // which case `repair_win` will be called, which uses `ever_damaged` so
451 // it needs to be correct. This also covers the case where the window is
452 // unmapped before `map_win_start` is called.
453 w->ever_damaged = false;
454
455 // FocusIn/Out may be ignored when the window is unmapped, so we must
456 // recheck focus here
457 ps->pending_updates = true; // to update focus
458 }
459
460 static inline void ev_unmap_notify(session_t *ps, xcb_unmap_notify_event_t *ev) {
461 if (ps->overlay && ev->window == ps->overlay) {
462 return;
463 }
464
465 if (ev->event == ev->window) {
466 return;
467 }
468
469 auto cursor = wm_find(ps->wm, ev->window);
470 if (cursor == NULL) {
471 if (wm_is_consistent(ps->wm)) {
472 log_error("Unmap event received for unknown window %#010x", ev->window);
473 assert(false);
474 }
475 return;
476 }
477 auto w = wm_ref_deref(cursor);
478 if (w != NULL) {
479 unmap_win_start(w);
480 }
481 }
482
483 static inline void ev_reparent_notify(session_t *ps, xcb_reparent_notify_event_t *ev) {
484 log_debug("Window %#010x has new parent: %#010x, override_redirect: %d, "
485 "send_event: %#010x",
486 ev->window, ev->parent, ev->override_redirect, ev->event);
487 if (ev->event == ev->window) {
488 return;
489 }
490 if (ev->parent != ev->event) {
491 wm_disconnect(ps->wm, ev->window, ev->event, ev->parent);
492 } else {
493 wm_reparent(ps->wm, &ps->c, ps->atoms, ev->window, ev->parent);
494 }
495 }
496
497 static inline void ev_circulate_notify(session_t *ps, xcb_circulate_notify_event_t *ev) {
498 if (ev->event == ev->window) {
499 return;
500 }
501
502 auto cursor = wm_find(ps->wm, ev->window);
503
504 if (cursor == NULL) {
505 if (wm_is_consistent(ps->wm)) {
506 log_debug("Circulate event received for unknown window %#010x",
507 ev->window);
508 assert(false);
509 }
510 return;
511 }
512
513 log_debug("Moving window %#010x (%s) to the %s", ev->window,
514 ev_window_name(ps, ev->window), ev->place == PlaceOnTop ? "top" : "bottom");
515 wm_stack_move_to_end(ps->wm, cursor, ev->place == XCB_PLACE_ON_BOTTOM);
516 }
517
518 static inline void ev_expose(session_t *ps, xcb_expose_event_t *ev) {
519 if (ev->window != ps->c.screen_info->root &&
520 (ps->overlay == XCB_NONE || ev->window != ps->overlay)) {
521 return;
522 }
523
524 if (ev->count == 0) {
525 force_repaint(ps);
526 }
527 }
528
529 static inline void ev_property_notify(session_t *ps, xcb_property_notify_event_t *ev) {
530 log_debug("{ atom = %#010x, window = %#010x, state = %d }", ev->atom, ev->window,
531 ev->state);
532 if (unlikely(log_get_level_tls() <= LOG_LEVEL_TRACE)) {
533 // Print out changed atom
534 xcb_get_atom_name_reply_t *reply = xcb_get_atom_name_reply(
535 ps->c.c, xcb_get_atom_name(ps->c.c, ev->atom), NULL);
536 const char *name = "?";
537 int name_len = 1;
538 if (reply) {
539 name = xcb_get_atom_name_name(reply);
540 name_len = xcb_get_atom_name_name_length(reply);
541 }
542
543 log_debug("{ atom = %.*s }", name_len, name);
544 free(reply);
545 }
546
547 if (ps->c.screen_info->root == ev->window) {
548 if (ps->o.use_ewmh_active_win && ps->atoms->a_NET_ACTIVE_WINDOW == ev->atom) {
549 ev_update_focused(ps);
550 } else {
551 // Destroy the root "image" if the wallpaper probably changed
552 if (x_is_root_back_pixmap_atom(ps->atoms, ev->atom)) {
553 root_damaged(ps);
554 }
555 }
556
557 // Unconcerned about any other properties on root window
558 return;
559 }
560
561 ps->pending_updates = true;
562 auto cursor = wm_find(ps->wm, ev->window);
563 if (cursor == NULL) {
564 if (wm_is_consistent(ps->wm)) {
565 log_error("Property notify received for unknown window %#010x",
566 ev->window);
567 assert(false);
568 }
569 return;
570 }
571
572 auto toplevel_cursor = wm_ref_toplevel_of(ps->wm, cursor);
573 if (ev->atom == ps->atoms->aWM_STATE) {
574 log_debug("WM_STATE changed for window %#010x (%s): %s", ev->window,
575 ev_window_name(ps, ev->window),
576 ev->state == XCB_PROPERTY_DELETE ? "deleted" : "set");
577 wm_set_has_wm_state(ps->wm, cursor, ev->state != XCB_PROPERTY_DELETE);
578 }
579
580 if (toplevel_cursor == NULL) {
581 assert(!wm_is_consistent(ps->wm));
582 return;
583 }
584
585 // We only care if the property is set on the toplevel itself, or on its
586 // client window if it has one. WM_STATE is an exception, it is handled
587 // always because it is what determines if a window is a client window.
588 auto client_cursor = wm_ref_client_of(toplevel_cursor) ?: toplevel_cursor;
589 if (cursor != client_cursor && cursor != toplevel_cursor) {
590 return;
591 }
592
593 if (ev->atom == ps->atoms->a_NET_WM_BYPASS_COMPOSITOR) {
594 // Unnecessary until we remove the queue_redraw in ev_handle
595 queue_redraw(ps);
596 }
597
598 auto toplevel = wm_ref_deref(toplevel_cursor);
599 if (toplevel) {
600 win_set_property_stale(toplevel, ev->atom);
601 }
602
603 if (ev->atom == ps->atoms->a_NET_WM_WINDOW_OPACITY && toplevel != NULL) {
604 // We already handle if this is set on the client window, check
605 // if this is set on the frame window as well.
606 // TODO(yshui) do we really need this?
607 win_set_property_stale(toplevel, ev->atom);
608 }
609
610 // Check for other atoms we are tracking
611 if (c2_state_is_property_tracked(ps->c2_state, ev->atom)) {
612 bool change_is_on_client = cursor == client_cursor;
613 if (toplevel) {
614 c2_window_state_mark_dirty(ps->c2_state, &toplevel->c2_state,
615 ev->atom, change_is_on_client);
616 // Set FACTOR_CHANGED so rules based on properties will be
617 // re-evaluated.
618 // Don't need to set property stale here, since that only
619 // concerns properties we explicitly check.
620 win_set_flags(toplevel, WIN_FLAGS_FACTOR_CHANGED);
621 }
622 }
623 }
624
625 static inline void repair_win(session_t *ps, struct win *w) {
626 // Only mapped window can receive damages
627 assert(w->state == WSTATE_MAPPED || win_check_flags_all(w, WIN_FLAGS_MAPPED));
628
629 region_t parts;
630 pixman_region32_init(&parts);
631
632 // If this is the first time this window is damaged, we would redraw the
633 // whole window, so we don't need to fetch the damage region. But we still need
634 // to make sure the X server receives the DamageSubtract request, hence the
635 // `xcb_request_check` here.
636 // Otherwise, we fetch the damage regions. That means we will receive a reply
637 // from the X server, which implies it has received our DamageSubtract request.
638 if (!w->ever_damaged) {
639 auto e = xcb_request_check(
640 ps->c.c,
641 xcb_damage_subtract_checked(ps->c.c, w->damage, XCB_NONE, XCB_NONE));
642 if (e) {
643 if (ps->o.show_all_xerrors) {
644 x_print_error(&ps->c, e->sequence, e->major_code,
645 e->minor_code, e->error_code);
646 }
647 free(e);
648 }
649 win_extents(w, &parts);
650 log_debug("Window %#010x (%s) has been damaged the first time", win_id(w),
651 w->name);
652 } else {
653 auto cookie = xcb_damage_subtract(ps->c.c, w->damage, XCB_NONE, ps->x_region);
654 if (!ps->o.show_all_xerrors) {
655 x_set_error_action_ignore(&ps->c, cookie);
656 }
657 x_fetch_region(&ps->c, ps->x_region, &parts);
658 pixman_region32_translate(&parts, w->g.x + w->g.border_width,
659 w->g.y + w->g.border_width);
660 }
661
662 log_trace("Mark window %#010x (%s) as having received damage", win_id(w), w->name);
663 w->ever_damaged = true;
664 w->pixmap_damaged = true;
665
666 // Why care about damage when screen is unredirected?
667 // We will force full-screen repaint on redirection.
668 if (!ps->redirected) {
669 pixman_region32_fini(&parts);
670 return;
671 }
672
673 pixman_region32_translate(&parts, -w->g.x, -w->g.y);
674 pixman_region32_union(&w->damaged, &w->damaged, &parts);
675 pixman_region32_fini(&parts);
676 }
677
678 static inline void ev_damage_notify(session_t *ps, xcb_damage_notify_event_t *de) {
679 auto cursor = wm_find(ps->wm, de->drawable);
680
681 if (cursor == NULL) {
682 log_error("Damage notify received for unknown window %#010x", de->drawable);
683 return;
684 }
685
686 auto w = wm_ref_deref(cursor);
687 if (w != NULL) {
688 repair_win(ps, w);
689 }
690 }
691
692 static inline void ev_shape_notify(session_t *ps, xcb_shape_notify_event_t *ev) {
693 auto cursor = wm_find(ps->wm, ev->affected_window);
694 if (cursor == NULL) {
695 log_error("Shape notify received for unknown window %#010x",
696 ev->affected_window);
697 return;
698 }
699
700 auto w = wm_ref_deref(cursor);
701 if (w == NULL || w->a.map_state == XCB_MAP_STATE_UNMAPPED) {
702 return;
703 }
704
705 win_set_flags(w, WIN_FLAGS_SIZE_STALE);
706 ps->pending_updates = true;
707 }
708
709 static inline void
710 ev_selection_clear(session_t *ps, xcb_selection_clear_event_t attr_unused *ev) {
711 // The only selection we own is the _NET_WM_CM_Sn selection.
712 // If we lose that one, we should exit.
713 log_fatal("Another composite manager started and took the _NET_WM_CM_Sn "
714 "selection.");
715 quit(ps);
716 }
717
718 void ev_handle(session_t *ps, xcb_generic_event_t *ev) {
719 xcb_window_t wid = ev_window(ps, ev);
720 if (ev->response_type != ps->c.e.damage_event + XCB_DAMAGE_NOTIFY) {
721 log_debug("event %10.10s serial %#010x window %#010x \"%s\"",
722 ev_name(ps, ev), ev->full_sequence, wid, ev_window_name(ps, wid));
723 } else {
724 log_trace("event %10.10s serial %#010x window %#010x \"%s\"",
725 ev_name(ps, ev), ev->full_sequence, wid, ev_window_name(ps, wid));
726 }
727
728 // Check if a custom XEvent constructor was registered in xlib for this event
729 // type, and call it discarding the constructed XEvent if any. XESetWireToEvent
730 // might be used by libraries to intercept messages from the X server e.g. the
731 // OpenGL lib waiting for DRI2 events.
732
733 // XXX This exists to workaround compton issue #33, #34, #47
734 // For even more details, see:
735 // https://bugs.freedesktop.org/show_bug.cgi?id=35945
736 // https://lists.freedesktop.org/archives/xcb/2011-November/007337.html
737 auto response_type = XCB_EVENT_RESPONSE_TYPE(ev);
738 auto proc = XESetWireToEvent(ps->c.dpy, response_type, 0);
739 if (proc) {
740 XESetWireToEvent(ps->c.dpy, response_type, proc);
741 XEvent dummy;
742
743 // Stop Xlib from complaining about lost sequence numbers.
744 // proc might also just be Xlib internal event processing functions, and
745 // because they probably won't see all X replies, they will complain about
746 // missing sequence numbers.
747 //
748 // We only need the low 16 bits
749 uint16_t seq = ev->sequence;
750 ev->sequence = (uint16_t)(LastKnownRequestProcessed(ps->c.dpy) & 0xffff);
751 proc(ps->c.dpy, &dummy, (xEvent *)ev);
752 // Restore the sequence number
753 ev->sequence = seq;
754 }
755
756 // XXX redraw needs to be more fine grained
757 queue_redraw(ps);
758
759 // We intentionally ignore events sent via SendEvent. Those events has the 8th bit
760 // of response_type set, meaning they will match none of the cases below.
761 switch (ev->response_type) {
762 case XCB_FOCUS_IN: ev_focus_in(ps, (xcb_focus_in_event_t *)ev); break;
763 case XCB_FOCUS_OUT: ev_focus_out(ps, (xcb_focus_out_event_t *)ev); break;
764 case XCB_CREATE_NOTIFY:
765 ev_create_notify(ps, (xcb_create_notify_event_t *)ev);
766 break;
767 case XCB_CONFIGURE_NOTIFY:
768 ev_configure_notify(ps, (xcb_configure_notify_event_t *)ev);
769 break;
770 case XCB_DESTROY_NOTIFY:
771 ev_destroy_notify(ps, (xcb_destroy_notify_event_t *)ev);
772 break;
773 case XCB_MAP_NOTIFY: ev_map_notify(ps, (xcb_map_notify_event_t *)ev); break;
774 case XCB_UNMAP_NOTIFY: ev_unmap_notify(ps, (xcb_unmap_notify_event_t *)ev); break;
775 case XCB_REPARENT_NOTIFY:
776 ev_reparent_notify(ps, (xcb_reparent_notify_event_t *)ev);
777 break;
778 case XCB_CIRCULATE_NOTIFY:
779 ev_circulate_notify(ps, (xcb_circulate_notify_event_t *)ev);
780 break;
781 case XCB_EXPOSE: ev_expose(ps, (xcb_expose_event_t *)ev); break;
782 case XCB_PROPERTY_NOTIFY:
783 ev_property_notify(ps, (xcb_property_notify_event_t *)ev);
784 break;
785 case XCB_SELECTION_CLEAR:
786 ev_selection_clear(ps, (xcb_selection_clear_event_t *)ev);
787 break;
788 default:
789 if (ps->c.e.has_shape && ev->response_type == ps->c.e.shape_event) {
790 ev_shape_notify(ps, (xcb_shape_notify_event_t *)ev);
791 break;
792 }
793 if (ps->c.e.has_randr &&
794 ev->response_type ==
795 (ps->c.e.randr_event + XCB_RANDR_SCREEN_CHANGE_NOTIFY)) {
796 x_update_monitors_async(&ps->c, &ps->monitors);
797 break;
798 }
799 if (ps->c.e.damage_event + XCB_DAMAGE_NOTIFY == ev->response_type) {
800 ev_damage_notify(ps, (xcb_damage_notify_event_t *)ev);
801 break;
802 }
803 }
804 }