Debian Codegraph @

src/dbus.c

raw symbols SCIP
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2011-2013, Christopher Jeffrey
3 // Copyright (c) 2018 Yuxuan Shui <[email protected]>
4
5 #include <X11/Xlib.h>
6 #include <ctype.h>
7 #include <dbus/dbus.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <xcb/xcb.h>
15
16 #include <picom/types.h>
17
18 #include "backend/backend.h"
19 #include "common.h"
20 #include "compiler.h"
21 #include "config.h"
22 #include "log.h"
23 #include "picom.h"
24 #include "utils/misc.h"
25 #include "utils/str.h"
26 #include "wm/defs.h"
27 #include "wm/win.h"
28 #include "wm/wm.h"
29
30 #include "dbus.h"
31
32 struct cdbus_data {
33 /// Mainloop
34 struct ev_loop *loop;
35 /// DBus connection.
36 DBusConnection *dbus_conn;
37 /// DBus service name.
38 char *dbus_service;
39 };
40
41 // Window type
42 typedef uint32_t cdbus_window_t;
43 #define CDBUS_TYPE_WINDOW DBUS_TYPE_UINT32
44 #define CDBUS_TYPE_WINDOW_STR DBUS_TYPE_UINT32_AS_STRING
45
46 typedef uint32_t cdbus_enum_t;
47 #define CDBUS_TYPE_ENUM DBUS_TYPE_UINT32
48 #define CDBUS_TYPE_ENUM_STR DBUS_TYPE_UINT32_AS_STRING
49
50 #define CDBUS_SERVICE_NAME "com.github.chjj.compton"
51 #define CDBUS_INTERFACE_NAME CDBUS_SERVICE_NAME
52 #define CDBUS_OBJECT_NAME "/com/github/chjj/compton"
53 #define CDBUS_ERROR_PREFIX CDBUS_INTERFACE_NAME ".error"
54 #define CDBUS_ERROR_UNKNOWN CDBUS_ERROR_PREFIX ".unknown"
55 #define CDBUS_ERROR_UNKNOWN_S "Well, I don't know what happened. Do you?"
56 #define CDBUS_ERROR_BADMSG CDBUS_ERROR_PREFIX ".bad_message"
57 #define CDBUS_ERROR_BADMSG_S \
58 "Unrecognized command. Beware compton " \
59 "cannot make you a sandwich."
60 #define CDBUS_ERROR_BADARG CDBUS_ERROR_PREFIX ".bad_argument"
61 #define CDBUS_ERROR_BADARG_S "Failed to parse argument %d: %s"
62 #define CDBUS_ERROR_BADWIN CDBUS_ERROR_PREFIX ".bad_window"
63 #define CDBUS_ERROR_BADWIN_S "Requested window %#010x not found."
64 #define CDBUS_ERROR_BADTGT CDBUS_ERROR_PREFIX ".bad_target"
65 #define CDBUS_ERROR_BADTGT_S "Target \"%s\" not found."
66 #define CDBUS_ERROR_FORBIDDEN CDBUS_ERROR_PREFIX ".forbidden"
67 #define CDBUS_ERROR_FORBIDDEN_S "Incorrect password, access denied."
68 #define CDBUS_ERROR_CUSTOM CDBUS_ERROR_PREFIX ".custom"
69 #define CDBUS_ERROR_CUSTOM_S "%s"
70
71 #define cdbus_reply_err(conn, srcmsg, err_name, err_format, ...) \
72 cdbus_reply_errm(conn, dbus_message_new_error_printf( \
73 (srcmsg), (err_name), (err_format), ##__VA_ARGS__))
74
75 #define PICOM_WINDOW_INTERFACE "picom.Window"
76 #define PICOM_COMPOSITOR_INTERFACE "picom.Compositor"
77
78 static DBusHandlerResult cdbus_process(DBusConnection *conn, DBusMessage *m, void *ud);
79 static DBusHandlerResult cdbus_process_windows(DBusConnection *c, DBusMessage *msg, void *ud);
80
81 static dbus_bool_t cdbus_callback_add_timeout(DBusTimeout *timeout, void *data);
82
83 static void cdbus_callback_remove_timeout(DBusTimeout *timeout, void *data);
84
85 static void cdbus_callback_timeout_toggled(DBusTimeout *timeout, void *data);
86
87 static dbus_bool_t cdbus_callback_add_watch(DBusWatch *watch, void *data);
88
89 static void cdbus_callback_remove_watch(DBusWatch *watch, void *data);
90
91 static void cdbus_callback_watch_toggled(DBusWatch *watch, void *data);
92
93 /**
94 * Initialize D-Bus connection.
95 */
96 struct cdbus_data *cdbus_init(session_t *ps, const char *uniq) {
97 auto cd = cmalloc(struct cdbus_data);
98 cd->dbus_service = NULL;
99
100 DBusError err = {};
101
102 // Initialize
103 dbus_error_init(&err);
104
105 // Connect to D-Bus
106 // Use dbus_bus_get_private() so we can fully recycle it ourselves
107 cd->dbus_conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
108 if (dbus_error_is_set(&err)) {
109 log_error("D-Bus connection failed.");
110 goto fail;
111 }
112
113 if (!cd->dbus_conn) {
114 log_error("D-Bus connection failed for unknown reason.");
115 goto fail;
116 }
117
118 // Avoid exiting on disconnect
119 dbus_connection_set_exit_on_disconnect(cd->dbus_conn, false);
120
121 // Request service name
122 {
123 // Build service name
124 size_t service_len = strlen(CDBUS_SERVICE_NAME) + strlen(uniq) + 2;
125 char *service = ccalloc(service_len, char);
126 snprintf(service, service_len, "%s.%s", CDBUS_SERVICE_NAME, uniq);
127
128 // Make a valid dbus name by converting non alphanumeric characters to
129 // underscore
130 char *tmp = service + strlen(CDBUS_SERVICE_NAME) + 1;
131 while (*tmp) {
132 if (!isalnum((unsigned char)*tmp)) {
133 *tmp = '_';
134 }
135 tmp++;
136 }
137 cd->dbus_service = service;
138
139 // Request for the name
140 int ret = dbus_bus_request_name(cd->dbus_conn, service,
141 DBUS_NAME_FLAG_DO_NOT_QUEUE, &err);
142
143 if (dbus_error_is_set(&err)) {
144 log_error("Failed to obtain D-Bus name.");
145 goto fail;
146 }
147
148 if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER &&
149 ret != DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER) {
150 log_error("Failed to become the primary owner of requested D-Bus "
151 "name (%d).",
152 ret);
153 goto fail;
154 }
155 }
156
157 // Add watch handlers
158 cd->loop = ps->loop;
159 if (!dbus_connection_set_watch_functions(cd->dbus_conn, cdbus_callback_add_watch,
160 cdbus_callback_remove_watch,
161 cdbus_callback_watch_toggled, cd, NULL)) {
162 log_error("Failed to add D-Bus watch functions.");
163 goto fail;
164 }
165
166 // Add timeout handlers
167 if (!dbus_connection_set_timeout_functions(
168 cd->dbus_conn, cdbus_callback_add_timeout, cdbus_callback_remove_timeout,
169 cdbus_callback_timeout_toggled, cd, NULL)) {
170 log_error("Failed to add D-Bus timeout functions.");
171 goto fail;
172 }
173
174 // Add match
175 dbus_bus_add_match(cd->dbus_conn,
176 "type='method_call',interface='" CDBUS_INTERFACE_NAME "'", &err);
177 if (dbus_error_is_set(&err)) {
178 log_error("Failed to add D-Bus match.");
179 goto fail;
180 }
181 dbus_connection_register_object_path(
182 cd->dbus_conn, CDBUS_OBJECT_NAME,
183 (DBusObjectPathVTable[]){{NULL, cdbus_process}}, ps);
184 dbus_connection_register_fallback(
185 cd->dbus_conn, CDBUS_OBJECT_NAME "/windows",
186 (DBusObjectPathVTable[]){{NULL, cdbus_process_windows}}, ps);
187 return cd;
188 fail:
189 if (dbus_error_is_set(&err)) {
190 log_error("D-Bus error %s: %s", err.name, err.message);
191 dbus_error_free(&err);
192 }
193 free(cd->dbus_service);
194 free(cd);
195 return NULL;
196 }
197
198 /**
199 * Destroy D-Bus connection.
200 */
201 void cdbus_destroy(struct cdbus_data *cd) {
202 if (cd->dbus_conn) {
203 // Release DBus name firstly
204 if (cd->dbus_service) {
205 DBusError err = {};
206 dbus_error_init(&err);
207
208 dbus_bus_release_name(cd->dbus_conn, cd->dbus_service, &err);
209 if (dbus_error_is_set(&err)) {
210 log_error("Failed to release DBus name (%s).", err.message);
211 dbus_error_free(&err);
212 }
213 free(cd->dbus_service);
214 }
215
216 // Close and unref the connection
217 dbus_connection_close(cd->dbus_conn);
218 dbus_connection_unref(cd->dbus_conn);
219 }
220 free(cd);
221 }
222
223 /** @name DBusTimeout handling
224 */
225 ///@{
226
227 typedef struct ev_dbus_timer {
228 ev_timer w;
229 DBusTimeout *t;
230 } ev_dbus_timer;
231
232 /**
233 * Callback for handling a D-Bus timeout.
234 */
235 static void
236 cdbus_callback_handle_timeout(EV_P attr_unused, ev_timer *w, int revents attr_unused) {
237 ev_dbus_timer *t = (void *)w;
238 dbus_timeout_handle(t->t);
239 }
240
241 /**
242 * Callback for adding D-Bus timeout.
243 */
244 static dbus_bool_t cdbus_callback_add_timeout(DBusTimeout *timeout, void *data) {
245 struct cdbus_data *cd = data;
246
247 auto t = ccalloc(1, ev_dbus_timer);
248 double i = dbus_timeout_get_interval(timeout) / 1000.0;
249 ev_timer_init(&t->w, cdbus_callback_handle_timeout, i, i);
250 t->t = timeout;
251 dbus_timeout_set_data(timeout, t, NULL);
252
253 if (dbus_timeout_get_enabled(timeout)) {
254 ev_timer_start(cd->loop, &t->w);
255 }
256
257 return true;
258 }
259
260 /**
261 * Callback for removing D-Bus timeout.
262 */
263 static void cdbus_callback_remove_timeout(DBusTimeout *timeout, void *data) {
264 struct cdbus_data *cd = data;
265
266 ev_dbus_timer *t = dbus_timeout_get_data(timeout);
267 assert(t);
268 ev_timer_stop(cd->loop, &t->w);
269 free(t);
270 }
271
272 /**
273 * Callback for toggling a D-Bus timeout.
274 */
275 static void cdbus_callback_timeout_toggled(DBusTimeout *timeout, void *data) {
276 struct cdbus_data *cd = data;
277 ev_dbus_timer *t = dbus_timeout_get_data(timeout);
278
279 assert(t);
280 ev_timer_stop(cd->loop, &t->w);
281 if (dbus_timeout_get_enabled(timeout)) {
282 double i = dbus_timeout_get_interval(timeout) / 1000.0;
283 ev_timer_set(&t->w, i, i);
284 ev_timer_start(cd->loop, &t->w);
285 }
286 }
287
288 ///@}
289
290 /** @name DBusWatch handling
291 */
292 ///@{
293
294 typedef struct ev_dbus_io {
295 ev_io w;
296 struct cdbus_data *cd;
297 DBusWatch *dw;
298 } ev_dbus_io;
299
300 void cdbus_io_callback(EV_P attr_unused, ev_io *w, int revents) {
301 ev_dbus_io *dw = (void *)w;
302 DBusWatchFlags flags = 0;
303 if (revents & EV_READ) {
304 flags |= DBUS_WATCH_READABLE;
305 }
306 if (revents & EV_WRITE) {
307 flags |= DBUS_WATCH_WRITABLE;
308 }
309 dbus_watch_handle(dw->dw, flags);
310 while (dbus_connection_dispatch(dw->cd->dbus_conn) != DBUS_DISPATCH_COMPLETE) {
311 }
312 }
313
314 /**
315 * Determine the poll condition of a DBusWatch.
316 */
317 static inline int cdbus_get_watch_cond(DBusWatch *watch) {
318 const unsigned flags = dbus_watch_get_flags(watch);
319 int condition = 0;
320 if (flags & DBUS_WATCH_READABLE) {
321 condition |= EV_READ;
322 }
323 if (flags & DBUS_WATCH_WRITABLE) {
324 condition |= EV_WRITE;
325 }
326
327 return condition;
328 }
329
330 /**
331 * Callback for adding D-Bus watch.
332 */
333 static dbus_bool_t cdbus_callback_add_watch(DBusWatch *watch, void *data) {
334 struct cdbus_data *cd = data;
335
336 auto w = ccalloc(1, ev_dbus_io);
337 w->dw = watch;
338 w->cd = cd;
339 ev_io_init(&w->w, cdbus_io_callback, dbus_watch_get_unix_fd(watch),
340 cdbus_get_watch_cond(watch));
341
342 // Leave disabled watches alone
343 if (dbus_watch_get_enabled(watch)) {
344 ev_io_start(cd->loop, &w->w);
345 }
346
347 dbus_watch_set_data(watch, w, NULL);
348
349 // Always return true
350 return true;
351 }
352
353 /**
354 * Callback for removing D-Bus watch.
355 */
356 static void cdbus_callback_remove_watch(DBusWatch *watch, void *data) {
357 struct cdbus_data *cd = data;
358 ev_dbus_io *w = dbus_watch_get_data(watch);
359 ev_io_stop(cd->loop, &w->w);
360 free(w);
361 }
362
363 /**
364 * Callback for toggling D-Bus watch status.
365 */
366 static void cdbus_callback_watch_toggled(DBusWatch *watch, void *data) {
367 struct cdbus_data *cd = data;
368 ev_io *w = dbus_watch_get_data(watch);
369 if (dbus_watch_get_enabled(watch)) {
370 ev_io_start(cd->loop, w);
371 } else {
372 ev_io_stop(cd->loop, w);
373 }
374 }
375
376 ///@}
377
378 static bool cdbus_append_boolean(DBusMessage *msg, dbus_bool_t val) {
379 return dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &val, DBUS_TYPE_INVALID);
380 }
381
382 static bool cdbus_append_int32(DBusMessage *msg, int32_t val) {
383 return dbus_message_append_args(msg, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
384 }
385
386 static bool cdbus_append_uint32(DBusMessage *msg, uint32_t val) {
387 return dbus_message_append_args(msg, DBUS_TYPE_UINT32, &val, DBUS_TYPE_INVALID);
388 }
389
390 static bool cdbus_append_double(DBusMessage *msg, double val) {
391 return dbus_message_append_args(msg, DBUS_TYPE_DOUBLE, &val, DBUS_TYPE_INVALID);
392 }
393
394 static bool cdbus_append_wid(DBusMessage *msg, xcb_window_t val_) {
395 cdbus_window_t val = val_;
396 return dbus_message_append_args(msg, CDBUS_TYPE_WINDOW, &val, DBUS_TYPE_INVALID);
397 }
398
399 static bool cdbus_append_enum(DBusMessage *msg, cdbus_enum_t val) {
400 return dbus_message_append_args(msg, CDBUS_TYPE_ENUM, &val, DBUS_TYPE_INVALID);
401 }
402
403 static bool cdbus_append_string(DBusMessage *msg, const char *data) {
404 const char *str = data ? data : "";
405 return dbus_message_append_args(msg, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
406 }
407
408 /// Append a window ID to a D-Bus message as a variant
409 static bool cdbus_append_wid_variant(DBusMessage *msg, xcb_window_t val_) {
410 cdbus_window_t val = val_;
411
412 DBusMessageIter it, it2;
413 dbus_message_iter_init_append(msg, &it);
414 if (!dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT,
415 CDBUS_TYPE_WINDOW_STR, &it2)) {
416 return false;
417 }
418 if (!dbus_message_iter_append_basic(&it2, CDBUS_TYPE_WINDOW, &val)) {
419 return false;
420 }
421 if (!dbus_message_iter_close_container(&it, &it2)) {
422 return false;
423 }
424
425 return true;
426 }
427
428 /// Append a boolean to a D-Bus message as a variant
429 static bool cdbus_append_bool_variant(DBusMessage *msg, dbus_bool_t val) {
430 DBusMessageIter it, it2;
431 dbus_message_iter_init_append(msg, &it);
432 if (!dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT,
433 DBUS_TYPE_BOOLEAN_AS_STRING, &it2)) {
434 return false;
435 }
436 if (!dbus_message_iter_append_basic(&it2, DBUS_TYPE_BOOLEAN, &val)) {
437 return false;
438 }
439 if (!dbus_message_iter_close_container(&it, &it2)) {
440 return false;
441 }
442
443 return true;
444 }
445
446 /// Append a string to a D-Bus message as a variant
447 static bool cdbus_append_string_variant(DBusMessage *msg, const char *data) {
448 const char *str = data ? data : "";
449
450 DBusMessageIter it, it2;
451 dbus_message_iter_init_append(msg, &it);
452 if (!dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT,
453 DBUS_TYPE_STRING_AS_STRING, &it2)) {
454 return false;
455 }
456 if (!dbus_message_iter_append_basic(&it2, DBUS_TYPE_STRING, &str)) {
457 log_error("Failed to append argument.");
458 return false;
459 }
460 if (!dbus_message_iter_close_container(&it, &it2)) {
461 return false;
462 }
463
464 return true;
465 }
466
467 /// Append all window IDs in the window list of a session to a D-Bus message
468 static bool cdbus_append_wids(DBusMessage *msg, session_t *ps) {
469 DBusMessageIter it, subit;
470 dbus_message_iter_init_append(msg, &it);
471 if (!dbus_message_iter_open_container(&it, DBUS_TYPE_ARRAY,
472 DBUS_TYPE_UINT32_AS_STRING, &subit)) {
473 log_error("Failed to open container.");
474 return false;
475 }
476
477 bool failed = false;
478 wm_stack_foreach(ps->wm, cursor) {
479 if (wm_ref_is_zombie(cursor)) {
480 continue;
481 }
482 auto w = wm_ref_deref(cursor);
483 if (w == NULL) {
484 continue;
485 }
486
487 auto wid = win_id(w);
488 if (!dbus_message_iter_append_basic(&subit, CDBUS_TYPE_WINDOW, &wid)) {
489 failed = true;
490 break;
491 }
492 }
493 if (!dbus_message_iter_close_container(&it, &subit)) {
494 log_error("Failed to close container.");
495 return false;
496 }
497 if (failed) {
498 log_error("Failed to append argument.");
499 return false;
500 }
501 return true;
502 }
503
504 /**
505 * Get n-th argument of a D-Bus message.
506 *
507 * @param count the position of the argument to get, starting from 0
508 * @param type libdbus type number of the type
509 * @param pdest pointer to the target
510 * @return true if successful, false otherwise.
511 */
512 static bool cdbus_msg_get_arg(DBusMessage *msg, int count, const int type, void *pdest) {
513 assert(count >= 0);
514
515 DBusMessageIter iter = {};
516 if (!dbus_message_iter_init(msg, &iter)) {
517 log_error("Message has no argument.");
518 return false;
519 }
520
521 {
522 const int oldcount = count;
523 while (count) {
524 if (!dbus_message_iter_next(&iter)) {
525 log_error("Failed to find argument %d.", oldcount);
526 return false;
527 }
528 --count;
529 }
530 }
531
532 if (type != dbus_message_iter_get_arg_type(&iter)) {
533 log_error("Argument has incorrect type.");
534 return false;
535 }
536
537 dbus_message_iter_get_basic(&iter, pdest);
538
539 return true;
540 }
541
542 /** @name Message processing
543 */
544 ///@{
545
546 /**
547 * Process a list_win D-Bus request.
548 */
549 static DBusHandlerResult
550 cdbus_process_list_win(session_t *ps, DBusMessage *msg attr_unused, DBusMessage *reply,
551 DBusError *err attr_unused) {
552 if (!cdbus_append_wids(reply, ps)) {
553 return DBUS_HANDLER_RESULT_NEED_MEMORY;
554 }
555 return DBUS_HANDLER_RESULT_HANDLED;
556 }
557
558 /// Process a property Get D-Bus request.
559 static DBusHandlerResult
560 cdbus_process_window_property_get(session_t *ps, DBusMessage *msg, cdbus_window_t wid,
561 DBusMessage *reply, DBusError *e) {
562 const char *target = NULL;
563 const char *interface = NULL;
564
565 if (reply == NULL) {
566 return DBUS_HANDLER_RESULT_HANDLED;
567 }
568
569 if (!dbus_message_get_args(msg, e, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING,
570 &target, DBUS_TYPE_INVALID)) {
571 log_debug("Failed to parse argument of \"Get\" (%s).", e->message);
572 dbus_set_error_const(e, DBUS_ERROR_INVALID_ARGS, NULL);
573 return DBUS_HANDLER_RESULT_HANDLED;
574 }
575
576 if (interface[0] != '\0' && strcmp(interface, PICOM_WINDOW_INTERFACE) != 0) {
577 dbus_set_error_const(e, DBUS_ERROR_UNKNOWN_INTERFACE, NULL);
578 return DBUS_HANDLER_RESULT_HANDLED;
579 }
580
581 auto cursor = wm_find(ps->wm, wid);
582 if (cursor == NULL) {
583 log_debug("Window %#010x not found.", wid);
584 dbus_set_error(e, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
585 return DBUS_HANDLER_RESULT_HANDLED;
586 }
587
588 auto w = wm_ref_deref(cursor);
589 if (w == NULL) {
590 log_debug("Window %#010x is not managed.", wid);
591 dbus_set_error(e, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
592 return DBUS_HANDLER_RESULT_HANDLED;
593 }
594
595 #define append(tgt, type, expr) \
596 if (strcmp(#tgt, target) == 0) { \
597 if (!cdbus_append_##type(reply, (expr))) { \
598 return DBUS_HANDLER_RESULT_NEED_MEMORY; \
599 } \
600 return DBUS_HANDLER_RESULT_HANDLED; \
601 }
602 #define append_win_property(name, member, type) append(name, type, w->member)
603
604 append(Mapped, bool_variant, w->state == WSTATE_MAPPED);
605 append(Id, wid_variant, win_id(w));
606 append(RawFocused, bool_variant,
607 w->a.map_state == XCB_MAP_STATE_VIEWABLE && w->is_focused);
608 append(ClientWin, wid_variant, win_client_id(w, /*fallback_to_self=*/true));
609 append(Leader, wid_variant, wm_ref_win_id(wm_ref_leader(w->tree_ref)));
610 append_win_property(Name, name, string_variant);
611
612 if (strcmp("Type", target) == 0) {
613 DBusMessageIter iter, sub;
614 dbus_message_iter_init_append(reply, &iter);
615 if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "s", &sub)) {
616 return DBUS_HANDLER_RESULT_NEED_MEMORY;
617 }
618 for (int i = 0; i < NUM_WINTYPES; i++) {
619 if ((w->window_types & (1 << i)) == 0) {
620 continue;
621 }
622 if (!dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING,
623 &WINTYPES[i].name)) {
624 return DBUS_HANDLER_RESULT_NEED_MEMORY;
625 }
626 }
627 if (!dbus_message_iter_close_container(&iter, &sub)) {
628 return DBUS_HANDLER_RESULT_NEED_MEMORY;
629 }
630 return DBUS_HANDLER_RESULT_HANDLED;
631 }
632
633 if (strcmp("Next", target) == 0) {
634 cdbus_window_t next_id = 0;
635 auto below = wm_ref_below(cursor);
636 if (below != NULL) {
637 next_id = wm_ref_win_id(below);
638 }
639 if (!cdbus_append_wid_variant(reply, next_id)) {
640 return DBUS_HANDLER_RESULT_NEED_MEMORY;
641 }
642 return DBUS_HANDLER_RESULT_HANDLED;
643 }
644
645 #undef append_win_property
646 #undef append
647
648 log_debug(CDBUS_ERROR_BADTGT_S, target);
649 dbus_set_error(e, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
650
651 return DBUS_HANDLER_RESULT_HANDLED;
652 }
653
654 static DBusHandlerResult cdbus_process_reset(session_t *ps, DBusMessage *msg attr_unused,
655 DBusMessage *reply, DBusError *e attr_unused) {
656 // Reset the compositor
657 log_info("picom is resetting...");
658 ev_break(ps->loop, EVBREAK_ALL);
659 if (reply != NULL && !cdbus_append_boolean(reply, true)) {
660 return DBUS_HANDLER_RESULT_NEED_MEMORY;
661 }
662 return DBUS_HANDLER_RESULT_HANDLED;
663 }
664
665 static DBusHandlerResult cdbus_process_repaint(session_t *ps, DBusMessage *msg attr_unused,
666 DBusMessage *reply, DBusError *e attr_unused) {
667 // Reset the compositor
668 force_repaint(ps);
669 if (reply != NULL && !cdbus_append_boolean(reply, true)) {
670 return DBUS_HANDLER_RESULT_NEED_MEMORY;
671 }
672 return DBUS_HANDLER_RESULT_HANDLED;
673 }
674
675 static inline cdbus_enum_t tristate_to_switch(enum tristate val) {
676 switch (val) {
677 case TRI_FALSE: return OFF;
678 case TRI_TRUE: return ON;
679 default: return UNSET;
680 }
681 }
682
683 /**
684 * Process a win_get D-Bus request.
685 */
686 static DBusHandlerResult
687 cdbus_process_win_get(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
688 cdbus_window_t wid = XCB_NONE;
689 const char *target = NULL;
690
691 if (reply == NULL) {
692 return DBUS_HANDLER_RESULT_HANDLED;
693 }
694
695 if (!dbus_message_get_args(msg, err, CDBUS_TYPE_WINDOW, &wid, DBUS_TYPE_STRING,
696 &target, DBUS_TYPE_INVALID)) {
697 log_debug("Failed to parse argument of \"win_get\" (%s).", err->message);
698 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
699 return DBUS_HANDLER_RESULT_HANDLED;
700 }
701
702 auto cursor = wm_find(ps->wm, wid);
703 if (cursor == NULL) {
704 log_debug("Window %#010x not found.", wid);
705 dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
706 return DBUS_HANDLER_RESULT_HANDLED;
707 }
708
709 auto w = wm_ref_deref(cursor);
710 if (w == NULL) {
711 log_debug("Window %#010x is not managed.", wid);
712 dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
713 return DBUS_HANDLER_RESULT_HANDLED;
714 }
715
716 #define append(tgt, type, expr) \
717 if (strcmp(#tgt, target) == 0) { \
718 if (!cdbus_append_##type(reply, (expr))) { \
719 return DBUS_HANDLER_RESULT_NEED_MEMORY; \
720 } \
721 return DBUS_HANDLER_RESULT_HANDLED; \
722 }
723 #define append_win_property(tgt, type) append(tgt, type, w->tgt)
724
725 if (strcmp("next", target) == 0) {
726 auto below = wm_ref_below(cursor);
727 xcb_window_t next_id = below ? wm_ref_win_id(below) : XCB_NONE;
728 if (!cdbus_append_wid(reply, next_id)) {
729 return DBUS_HANDLER_RESULT_NEED_MEMORY;
730 }
731 return DBUS_HANDLER_RESULT_HANDLED;
732 }
733
734 auto w_opts = win_options(w);
735
736 append(id, wid, win_id(w));
737 append(client_win, wid, win_client_id(w, /*fallback_to_self=*/true));
738 append(map_state, boolean, w->a.map_state);
739 append(wmwin, boolean, win_is_wmwin(w));
740 append(focused_raw, boolean, w->a.map_state == XCB_MAP_STATE_VIEWABLE && w->is_focused);
741 append(left_width, int32, w->frame_extents.left);
742 append(right_width, int32, w->frame_extents.right);
743 append(top_width, int32, w->frame_extents.top);
744 append(bottom_width, int32, w->frame_extents.bottom);
745 append(fade_force, enum, tristate_to_switch(w->options_override.fade));
746 append(shadow_force, enum, tristate_to_switch(w->options_override.shadow));
747 append(invert_color_force, enum, tristate_to_switch(w->options_override.invert_color));
748 append(opacity_is_set, boolean, !safe_isnan(w->options.opacity));
749 append(shadow, boolean, w_opts.shadow);
750 append(fade, boolean, w_opts.fade);
751 append(blur_background, boolean, w_opts.blur_background);
752 append(leader, wid, wm_ref_win_id(wm_ref_leader(w->tree_ref)));
753
754 append_win_property(mode, enum);
755 append_win_property(opacity, double);
756 append_win_property(ever_damaged, boolean);
757 append_win_property(window_types, enum);
758 append_win_property(name, string);
759 append_win_property(class_instance, string);
760 append_win_property(class_general, string);
761 append_win_property(role, string);
762 append_win_property(opacity, double);
763 append_win_property(has_opacity_prop, boolean);
764 append_win_property(opacity_prop, uint32);
765 append_win_property(opacity_set, double);
766 append_win_property(frame_opacity, double);
767
768 #undef append_win_property
769 #undef append
770
771 log_debug(CDBUS_ERROR_BADTGT_S, target);
772 dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
773 return DBUS_HANDLER_RESULT_HANDLED;
774 }
775
776 /**
777 * Process a win_set D-Bus request.
778 */
779 static DBusHandlerResult
780 cdbus_process_win_set(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
781 cdbus_window_t wid = XCB_NONE;
782 const char *target = NULL;
783
784 if (!dbus_message_get_args(msg, err, CDBUS_TYPE_WINDOW, &wid, DBUS_TYPE_STRING,
785 &target, DBUS_TYPE_INVALID)) {
786 log_debug("Failed to parse argument of \"win_set\" (%s).", err->message);
787 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
788 return DBUS_HANDLER_RESULT_HANDLED;
789 }
790
791 auto cursor = wm_find(ps->wm, wid);
792 if (cursor == NULL) {
793 log_debug("Window %#010x not found.", wid);
794 dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
795 return DBUS_HANDLER_RESULT_HANDLED;
796 }
797
798 auto w = wm_ref_deref(cursor);
799 if (w == NULL) {
800 log_debug("Window %#010x is not managed.", wid);
801 dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
802 return DBUS_HANDLER_RESULT_HANDLED;
803 }
804 cdbus_enum_t val = UNSET;
805 if (!cdbus_msg_get_arg(msg, 2, CDBUS_TYPE_ENUM, &val)) {
806 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
807 return DBUS_HANDLER_RESULT_HANDLED;
808 }
809
810 bool changed = false;
811 if (strcmp("shadow_force", target) == 0) {
812 w->options_override.shadow =
813 val == UNSET ? TRI_UNKNOWN : (val == ON ? TRI_TRUE : TRI_FALSE);
814 changed = true;
815 } else if (strcmp("fade_force", target) == 0) {
816 w->options_override.fade =
817 val == UNSET ? TRI_UNKNOWN : (val == ON ? TRI_TRUE : TRI_FALSE);
818 changed = true;
819 } else if (strcmp("invert_color_force", target) == 0) {
820 w->options_override.invert_color =
821 val == UNSET ? TRI_UNKNOWN : (val == ON ? TRI_TRUE : TRI_FALSE);
822 changed = true;
823 } else {
824 log_debug(CDBUS_ERROR_BADTGT_S, target);
825 dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
826 return DBUS_HANDLER_RESULT_HANDLED;
827 }
828 if (changed) {
829 queue_redraw(ps);
830 }
831
832 if (reply != NULL && !cdbus_append_boolean(reply, true)) {
833 return DBUS_HANDLER_RESULT_NEED_MEMORY;
834 }
835 return DBUS_HANDLER_RESULT_HANDLED;
836 }
837
838 /**
839 * Process a find_win D-Bus request.
840 */
841 static DBusHandlerResult
842 cdbus_process_find_win(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
843 if (reply == NULL) {
844 return DBUS_HANDLER_RESULT_HANDLED;
845 }
846
847 const char *target = NULL;
848 if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target)) {
849 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
850 return DBUS_HANDLER_RESULT_HANDLED;
851 }
852
853 xcb_window_t wid = XCB_NONE;
854
855 if (strcmp("client", target) == 0) {
856 // Find window by client window
857 cdbus_window_t client = XCB_NONE;
858 if (!cdbus_msg_get_arg(msg, 1, CDBUS_TYPE_WINDOW, &client)) {
859 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
860 return DBUS_HANDLER_RESULT_HANDLED;
861 }
862 auto w = wm_find_by_client(ps->wm, client);
863 if (w) {
864 wid = wm_ref_win_id(w);
865 }
866 } else if (strcmp("focused", target) == 0) {
867 // Find focused window
868 auto focused_win = wm_focused_win(ps->wm);
869 auto w = wm_ref_deref(focused_win);
870 if (focused_win && w && w->state == WSTATE_MAPPED) {
871 wid = wm_ref_win_id(focused_win);
872 }
873 } else {
874 log_debug(CDBUS_ERROR_BADTGT_S, target);
875 dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
876 return DBUS_HANDLER_RESULT_HANDLED;
877 }
878
879 if (reply != NULL && !cdbus_append_wid(reply, wid)) {
880 return DBUS_HANDLER_RESULT_NEED_MEMORY;
881 }
882 return DBUS_HANDLER_RESULT_HANDLED;
883 }
884
885 /**
886 * Process a opts_get D-Bus request.
887 */
888 static DBusHandlerResult
889 cdbus_process_opts_get(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
890 if (reply == NULL) {
891 return DBUS_HANDLER_RESULT_HANDLED;
892 }
893
894 const char *target = NULL;
895 if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target)) {
896 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
897 return DBUS_HANDLER_RESULT_HANDLED;
898 }
899
900 #define append(tgt, type, ret) \
901 if (strcmp(#tgt, target) == 0) { \
902 if (reply != NULL && !cdbus_append_##type(reply, ret)) { \
903 return DBUS_HANDLER_RESULT_NEED_MEMORY; \
904 } \
905 return DBUS_HANDLER_RESULT_HANDLED; \
906 }
907 #define append_session_option(tgt, type) append(tgt, type, ps->o.tgt)
908
909 if (strcmp("backend", target) == 0) {
910 const char *name = backend_name(ps->o.backend);
911 if (reply != NULL && !cdbus_append_string(reply, name)) {
912 return DBUS_HANDLER_RESULT_NEED_MEMORY;
913 }
914 return DBUS_HANDLER_RESULT_HANDLED;
915 }
916
917 append(version, string, PICOM_FULL_VERSION);
918 append(pid, int32, getpid());
919 append(display, string, DisplayString(ps->c.dpy));
920 append(config_file, string, "Unknown");
921 append(paint_on_overlay, boolean, ps->overlay != XCB_NONE);
922 append(paint_on_overlay_id, uint32, ps->overlay); // Sending ID of the X
923 // composite overlay
924 // window
925 append(unredir_if_possible_delay, int32, (int32_t)ps->o.unredir_if_possible_delay);
926 append(refresh_rate, int32, 0);
927 append(sw_opti, boolean, false);
928
929 append_session_option(unredir_if_possible, boolean);
930 append_session_option(write_pid_path, string);
931 append_session_option(mark_wmwin_focused, boolean);
932 append_session_option(mark_ovredir_focused, boolean);
933 append_session_option(detect_rounded_corners, boolean);
934 append_session_option(redirected_force, enum);
935 append_session_option(stoppaint_force, enum);
936 append_session_option(logpath, string);
937 append_session_option(vsync, boolean);
938 append_session_option(shadow_red, double);
939 append_session_option(shadow_green, double);
940 append_session_option(shadow_blue, double);
941 append_session_option(shadow_radius, int32);
942 append_session_option(shadow_offset_x, int32);
943 append_session_option(shadow_offset_y, int32);
944 append_session_option(shadow_opacity, double);
945 append_session_option(crop_shadow_to_monitor, boolean);
946
947 append_session_option(fade_delta, int32);
948 append_session_option(fade_in_step, double);
949 append_session_option(fade_out_step, double);
950 append_session_option(no_fading_openclose, boolean);
951
952 append_session_option(blur_method, boolean);
953 append_session_option(blur_background_frame, boolean);
954 append_session_option(blur_background_fixed, boolean);
955
956 append_session_option(inactive_dim, double);
957 append_session_option(inactive_dim_fixed, boolean);
958
959 append_session_option(max_brightness, double);
960
961 append_session_option(use_ewmh_active_win, boolean);
962 append_session_option(detect_transient, boolean);
963 append_session_option(detect_client_leader, boolean);
964 append_session_option(use_damage, boolean);
965
966 #ifdef CONFIG_OPENGL
967 append_session_option(glx_no_stencil, boolean);
968 append_session_option(glx_no_rebind_pixmap, boolean);
969 #endif
970
971 #undef append_session_option
972 #undef append
973
974 log_debug(CDBUS_ERROR_BADTGT_S, target);
975 dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
976
977 return DBUS_HANDLER_RESULT_HANDLED;
978 }
979
980 /**
981 * Process a opts_set D-Bus request.
982 */
983 static DBusHandlerResult
984 cdbus_process_opts_set(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
985 const char *target = NULL;
986
987 if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target)) {
988 log_error("Failed to parse argument of \"opts_set\" (%s).", err->message);
989 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
990 return DBUS_HANDLER_RESULT_HANDLED;
991 }
992
993 #define get_msg_arg(type, val) \
994 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_##type, &(val))) { \
995 dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL); \
996 return DBUS_HANDLER_RESULT_HANDLED; \
997 };
998 #define opts_set_do(tgt, dbus_type, type, expr) \
999 if (strcmp(#tgt, target) == 0) { \
1000 type val; \
1001 get_msg_arg(dbus_type, val); \
1002 ps->o.tgt = expr; \
1003 goto cdbus_process_opts_set_success; \
1004 }
1005
1006 if (strcmp("clear_shadow", target) == 0 || strcmp("track_focus", target) == 0) {
1007 goto cdbus_process_opts_set_success;
1008 }
1009
1010 opts_set_do(fade_delta, INT32, int32_t, max2(val, 1));
1011 opts_set_do(fade_in_step, DOUBLE, double, normalize_d(val));
1012 opts_set_do(fade_out_step, DOUBLE, double, normalize_d(val));
1013 opts_set_do(no_fading_openclose, BOOLEAN, bool, val);
1014 opts_set_do(stoppaint_force, UINT32, cdbus_enum_t, val);
1015
1016 if (strcmp("unredir_if_possible", target) == 0) {
1017 dbus_bool_t val = FALSE;
1018 get_msg_arg(BOOLEAN, val);
1019 if (ps->o.unredir_if_possible != val) {
1020 ps->o.unredir_if_possible = val;
1021 queue_redraw(ps);
1022 }
1023 goto cdbus_process_opts_set_success;
1024 }
1025
1026 if (strcmp("redirected_force", target) == 0) {
1027 cdbus_enum_t val = UNSET;
1028 get_msg_arg(UINT32, val);
1029 if (ps->o.redirected_force != val) {
1030 ps->o.redirected_force = val;
1031 force_repaint(ps);
1032 }
1033 goto cdbus_process_opts_set_success;
1034 }
1035 #undef get_msg_arg
1036
1037 log_error(CDBUS_ERROR_BADTGT_S, target);
1038 dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
1039 return DBUS_HANDLER_RESULT_HANDLED;
1040
1041 cdbus_process_opts_set_success:
1042 if (reply != NULL && !cdbus_append_boolean(reply, true)) {
1043 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1044 }
1045 return DBUS_HANDLER_RESULT_HANDLED;
1046 }
1047
1048 /**
1049 * Process an Introspect D-Bus request.
1050 */
1051 static DBusHandlerResult cdbus_process_introspect(DBusMessage *reply) {
1052 static const char *str_introspect =
1053 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection "
1054 "1.0//EN\"\n"
1055 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1056 "<node name='" CDBUS_OBJECT_NAME "'>\n"
1057 " <interface name='org.freedesktop.DBus.Introspectable'>\n"
1058 " <method name='Introspect'>\n"
1059 " <arg name='data' direction='out' type='s' />\n"
1060 " </method>\n"
1061 " </interface>\n"
1062 " <interface name='org.freedesktop.DBus.Peer'>\n"
1063 " <method name='Ping' />\n"
1064 " <method name='GetMachineId'>\n"
1065 " <arg name='machine_uuid' direction='out' type='s' />\n"
1066 " </method>\n"
1067 " </interface>\n"
1068 " <interface name='" CDBUS_INTERFACE_NAME "'>\n"
1069 " <signal name='win_added'>\n"
1070 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1071 " </signal>\n"
1072 " <signal name='win_destroyed'>\n"
1073 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1074 " </signal>\n"
1075 " <signal name='win_mapped'>\n"
1076 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1077 " </signal>\n"
1078 " <signal name='win_unmapped'>\n"
1079 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1080 " </signal>\n"
1081 " <signal name='win_focusin'>\n"
1082 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1083 " </signal>\n"
1084 " <signal name='win_focusout'>\n"
1085 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1086 " </signal>\n"
1087 " <method name='reset' />\n"
1088 " <method name='repaint' />\n"
1089 " <method name='list_win'>\n"
1090 " <arg name='wids' type='au' direction='out' />\n"
1091 " </method>\n"
1092 " </interface>\n"
1093 " <interface name='" PICOM_COMPOSITOR_INTERFACE "'>\n"
1094 " <signal name='WinAdded'>\n"
1095 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1096 " </signal>\n"
1097 " <signal name='WinDestroyed'>\n"
1098 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1099 " </signal>\n"
1100 " <signal name='WinMapped'>\n"
1101 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1102 " </signal>\n"
1103 " <signal name='WinUnmapped'>\n"
1104 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1105 " </signal>\n"
1106 " </interface>\n"
1107 " <node name='windows' />\n"
1108 "</node>\n";
1109
1110 if (reply != NULL && !cdbus_append_string(reply, str_introspect)) {
1111 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1112 }
1113 return DBUS_HANDLER_RESULT_HANDLED;
1114 }
1115 ///@}
1116
1117 /// Process an D-Bus Introspect request, for /windows.
1118 static DBusHandlerResult
1119 cdbus_process_windows_root_introspect(session_t *ps, DBusMessage *reply) {
1120 static const char *str_introspect =
1121 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection "
1122 "1.0//EN\"\n"
1123 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1124 "<node>\n"
1125 " <interface name='org.freedesktop.DBus.Introspectable'>\n"
1126 " <method name='Introspect'>\n"
1127 " <arg name='data' direction='out' type='s' />\n"
1128 " </method>\n"
1129 " </interface>\n";
1130
1131 if (reply == NULL) {
1132 return DBUS_HANDLER_RESULT_HANDLED;
1133 }
1134
1135 scoped_charp introspect = NULL;
1136 mstrextend(&introspect, str_introspect);
1137 wm_stack_foreach(ps->wm, cursor) {
1138 auto w = wm_ref_deref(cursor);
1139 if (w == NULL) {
1140 continue;
1141 }
1142 char *tmp = NULL;
1143 if (asprintf(&tmp, "<node name='%#010x'/>\n", win_id(w)) < 0) {
1144 log_fatal("Failed to allocate memory.");
1145 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1146 }
1147 mstrextend(&introspect, tmp);
1148 free(tmp);
1149 }
1150 mstrextend(&introspect, "</node>");
1151 if (!cdbus_append_string(reply, introspect)) {
1152 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1153 }
1154 return DBUS_HANDLER_RESULT_HANDLED;
1155 }
1156
1157 static bool cdbus_process_window_introspect(DBusMessage *reply) {
1158 // clang-format off
1159 static const char *str_introspect =
1160 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection "
1161 "1.0//EN\"\n"
1162 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1163 "<node>\n"
1164 " <interface name='org.freedesktop.DBus.Introspectable'>\n"
1165 " <method name='Introspect'>\n"
1166 " <arg name='data' direction='out' type='s' />\n"
1167 " </method>\n"
1168 " </interface>\n"
1169 " <interface name='org.freedesktop.DBus.Properties'>\n"
1170 " <method name='Get'>\n"
1171 " <arg type='s' name='interface_name' direction='in'/>\n"
1172 " <arg type='s' name='property_name' direction='in'/>\n"
1173 " <arg type='v' name='value' direction='out'/>\n"
1174 " </method>\n"
1175 " <method name='GetAll'>\n"
1176 " <arg type='s' name='interface_name' direction='in'/>\n"
1177 " <arg type='a{sv}' name='properties' direction='out'/>\n"
1178 " </method>\n"
1179 " <method name='Set'>\n"
1180 " <arg type='s' name='interface_name' direction='in'/>\n"
1181 " <arg type='s' name='property_name' direction='in'/>\n"
1182 " <arg type='v' name='value' direction='in'/>\n"
1183 " </method>\n"
1184 " <signal name='PropertiesChanged'>\n"
1185 " <arg type='s' name='interface_name'/>\n"
1186 " <arg type='a{sv}' name='changed_properties'/>\n"
1187 " <arg type='as' name='invalidated_properties'/>\n"
1188 " </signal>\n"
1189 " </interface>\n"
1190 " <interface name='" PICOM_WINDOW_INTERFACE "'>\n"
1191 " <property type='" CDBUS_TYPE_WINDOW_STR "' name='Leader' access='read'/>\n"
1192 " <property type='" CDBUS_TYPE_WINDOW_STR "' name='ClientWin' access='read'/>\n"
1193 " <property type='" CDBUS_TYPE_WINDOW_STR "' name='Id' access='read'/>\n"
1194 " <property type='" CDBUS_TYPE_WINDOW_STR "' name='Next' access='read'/>\n"
1195 " <property type='b' name='RawFocused' access='read'/>\n"
1196 " <property type='b' name='Mapped' access='read'/>\n"
1197 " <property type='s' name='Name' access='read'/>\n"
1198 " <property type='as' name='Type' access='read'/>\n"
1199 " <method name='BlockUnblockAnimation'>\n"
1200 " <arg type='s' name='trigger' direction='in'/>\n"
1201 " <arg type='b' name='block' direction='in'/>\n"
1202 " <arg type='u' name='count' direction='out'/>\n"
1203 " </method>\n"
1204 " </interface>\n"
1205 "</node>\n";
1206 // clang-format on
1207 if (reply != NULL && !cdbus_append_string(reply, str_introspect)) {
1208 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1209 }
1210 return DBUS_HANDLER_RESULT_HANDLED;
1211 }
1212
1213 /// Send a reply or an error message for request `msg`, appropriately based on the
1214 /// `result` and whether `err` is set. Frees the error message and the reply message, and
1215 /// flushes the connection.
1216 static inline DBusHandlerResult
1217 cdbus_send_reply_or_error(DBusConnection *conn, DBusHandlerResult result,
1218 DBusMessage *msg, DBusMessage *reply, DBusError *err) {
1219 if (dbus_error_is_set(err) && reply != NULL) {
1220 // If error is set, send the error instead of the reply
1221 dbus_message_unref(reply);
1222 reply = dbus_message_new_error(msg, err->name, err->message);
1223 if (reply == NULL) {
1224 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
1225 }
1226 }
1227 if (result != DBUS_HANDLER_RESULT_HANDLED && reply != NULL) {
1228 // We shouldn't send a reply if we didn't handle this message
1229 dbus_message_unref(reply);
1230 reply = NULL;
1231 }
1232 if (reply != NULL) {
1233 if (!dbus_connection_send(conn, reply, NULL)) {
1234 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
1235 }
1236 dbus_message_unref(reply);
1237 }
1238 dbus_error_free(err);
1239 dbus_connection_flush(conn);
1240 return result;
1241 }
1242
1243 /**
1244 * Process a message from D-Bus.
1245 */
1246 static DBusHandlerResult cdbus_process(DBusConnection *conn, DBusMessage *msg, void *ud) {
1247 session_t *ps = ud;
1248
1249 if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameAcquired") ||
1250 dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameLost")) {
1251 return DBUS_HANDLER_RESULT_HANDLED;
1252 }
1253
1254 if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_ERROR) {
1255 log_debug("Error message of path \"%s\" "
1256 "interface \"%s\", member \"%s\", error \"%s\"",
1257 dbus_message_get_path(msg), dbus_message_get_interface(msg),
1258 dbus_message_get_member(msg), dbus_message_get_error_name(msg));
1259 return DBUS_HANDLER_RESULT_HANDLED;
1260 }
1261
1262 if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_METHOD_CALL) {
1263 log_debug("Illegal message of type \"%s\", path \"%s\" "
1264 "interface \"%s\", member \"%s\"",
1265 cdbus_repr_msgtype(msg), dbus_message_get_path(msg),
1266 dbus_message_get_interface(msg), dbus_message_get_member(msg));
1267 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1268 }
1269
1270 DBusMessage *reply = NULL;
1271 DBusError err;
1272 DBusHandlerResult ret = DBUS_HANDLER_RESULT_HANDLED;
1273 const char *interface = dbus_message_get_interface(msg);
1274 const char *member = dbus_message_get_member(msg);
1275
1276 dbus_error_init(&err);
1277 if (!dbus_message_get_no_reply(msg)) {
1278 reply = dbus_message_new_method_return(msg);
1279 if (reply == NULL) {
1280 log_error("Failed to create reply message.");
1281 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1282 }
1283 }
1284
1285 if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) {
1286 ret = cdbus_process_introspect(reply);
1287 } else if (strcmp(interface, CDBUS_INTERFACE_NAME) != 0) {
1288 dbus_set_error_const(&err, DBUS_ERROR_UNKNOWN_INTERFACE, NULL);
1289 } else {
1290 static const struct {
1291 const char *name;
1292 DBusHandlerResult (*func)(session_t *ps, DBusMessage *msg,
1293 DBusMessage *reply, DBusError *err);
1294 } handlers[] = {
1295 {"reset", cdbus_process_reset},
1296 {"repaint", cdbus_process_repaint},
1297 {"list_win", cdbus_process_list_win},
1298 {"win_get", cdbus_process_win_get},
1299 {"win_set", cdbus_process_win_set},
1300 {"find_win", cdbus_process_find_win},
1301 {"opts_get", cdbus_process_opts_get},
1302 {"opts_set", cdbus_process_opts_set},
1303 };
1304
1305 size_t i;
1306 for (i = 0; i < ARR_SIZE(handlers); i++) {
1307 if (strcmp(handlers[i].name, member) == 0) {
1308 ret = handlers[i].func(ps, msg, reply, &err);
1309 break;
1310 }
1311 }
1312 if (i >= ARR_SIZE(handlers)) {
1313 log_debug("Unknown method \"%s\".", member);
1314 dbus_set_error_const(&err, CDBUS_ERROR_BADMSG, CDBUS_ERROR_BADMSG_S);
1315 }
1316 }
1317
1318 return cdbus_send_reply_or_error(conn, ret, msg, reply, &err);
1319 }
1320
1321 /**
1322 * Process a message from D-Bus, for /windows path.
1323 */
1324 static DBusHandlerResult
1325 cdbus_process_windows(DBusConnection *conn, DBusMessage *msg, void *ud) {
1326 session_t *ps = ud;
1327 DBusHandlerResult ret = DBUS_HANDLER_RESULT_HANDLED;
1328 const char *path = dbus_message_get_path(msg);
1329 const char *last_segment = strrchr(path, '/');
1330 DBusError err;
1331
1332 dbus_error_init(&err);
1333
1334 if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameAcquired") ||
1335 dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameLost")) {
1336 return DBUS_HANDLER_RESULT_HANDLED;
1337 }
1338
1339 if (dbus_message_get_type(msg) == DBUS_MESSAGE_TYPE_ERROR) {
1340 log_debug("Error message of path \"%s\" "
1341 "interface \"%s\", member \"%s\", error \"%s\"",
1342 dbus_message_get_path(msg), dbus_message_get_interface(msg),
1343 dbus_message_get_member(msg), dbus_message_get_error_name(msg));
1344 return DBUS_HANDLER_RESULT_HANDLED;
1345 }
1346
1347 if (dbus_message_get_type(msg) != DBUS_MESSAGE_TYPE_METHOD_CALL) {
1348 log_debug("Illegal message of type \"%s\", path \"%s\" "
1349 "interface \"%s\", member \"%s\"",
1350 cdbus_repr_msgtype(msg), dbus_message_get_path(msg),
1351 dbus_message_get_interface(msg), dbus_message_get_member(msg));
1352 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1353 }
1354
1355 DBusMessage *reply = NULL;
1356 const char *interface = dbus_message_get_interface(msg);
1357 const char *member = dbus_message_get_member(msg);
1358 if (!dbus_message_get_no_reply(msg)) {
1359 reply = dbus_message_new_method_return(msg);
1360 if (reply == NULL) {
1361 log_error("Failed to create reply message.");
1362 return DBUS_HANDLER_RESULT_NEED_MEMORY;
1363 }
1364 }
1365
1366 if (last_segment == NULL) {
1367 dbus_set_error_const(&err, CDBUS_ERROR_BADMSG, CDBUS_ERROR_BADMSG_S);
1368 goto finished;
1369 }
1370
1371 bool is_root = strncmp(last_segment, "/windows", 8) == 0;
1372 if (is_root) {
1373 if (strcmp(interface, DBUS_INTERFACE_INTROSPECTABLE) == 0 &&
1374 strcmp(member, "Introspect") == 0) {
1375 ret = cdbus_process_windows_root_introspect(ps, reply);
1376 } else {
1377 log_debug("Illegal message of type \"%s\", path \"%s\" "
1378 "interface \"%s\", member \"%s\"",
1379 cdbus_repr_msgtype(msg), dbus_message_get_path(msg),
1380 dbus_message_get_interface(msg),
1381 dbus_message_get_member(msg));
1382 dbus_set_error_const(&err, CDBUS_ERROR_BADMSG, CDBUS_ERROR_BADMSG_S);
1383 }
1384 goto finished;
1385 }
1386
1387 char *endptr = NULL;
1388 auto wid = (cdbus_window_t)strtol(last_segment + 1, &endptr, 0);
1389 if (*endptr != '\0') {
1390 log_error("Invalid window ID string \"%s\".", last_segment + 1);
1391 dbus_set_error_const(&err, DBUS_ERROR_INVALID_ARGS, NULL);
1392 } else if (strcmp(interface, DBUS_INTERFACE_INTROSPECTABLE) == 0 &&
1393 strcmp(member, "Introspect") == 0) {
1394 ret = cdbus_process_window_introspect(reply);
1395 } else if (strcmp(interface, DBUS_INTERFACE_PROPERTIES) == 0) {
1396 if (strcmp(member, "GetAll") == 0 || strcmp(member, "Set") == 0) {
1397 dbus_set_error_const(&err, DBUS_ERROR_NOT_SUPPORTED, NULL);
1398 } else if (strcmp(member, "Get") == 0) {
1399 ret = cdbus_process_window_property_get(ps, msg, wid, reply, &err);
1400 } else {
1401 log_debug(
1402 "Unexpected member \"%s\" of dbus properties interface.", member);
1403 dbus_set_error_const(&err, DBUS_ERROR_UNKNOWN_METHOD, NULL);
1404 }
1405 } else if (strcmp(interface, PICOM_WINDOW_INTERFACE) == 0 &&
1406 strcmp(member, "BlockUnblockAnimation") == 0) {
1407 bool block = false;
1408 const char *trigger_str = NULL;
1409 if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &trigger_str) ||
1410 !cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &block)) {
1411 dbus_set_error_const(&err, DBUS_ERROR_INVALID_ARGS, NULL);
1412 goto finished;
1413 }
1414 auto trigger = parse_animation_trigger(trigger_str);
1415 if (trigger >= ANIMATION_TRIGGER_INVALID) {
1416 dbus_set_error(&err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S,
1417 trigger_str);
1418 goto finished;
1419 }
1420 auto cursor = wm_find(ps->wm, wid);
1421 if (cursor == NULL) {
1422 dbus_set_error(&err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
1423 goto finished;
1424 }
1425 auto w = wm_ref_deref(cursor);
1426 unsigned count = 0;
1427 if (w != NULL) {
1428 if (block) {
1429 w->animation_block[trigger] += 1;
1430 } else if (w->animation_block[trigger] > 0) {
1431 w->animation_block[trigger] -= 1;
1432 }
1433 count = w->animation_block[trigger];
1434 }
1435 if (reply != NULL && !cdbus_append_uint32(reply, count)) {
1436 ret = DBUS_HANDLER_RESULT_NEED_MEMORY;
1437 }
1438 } else {
1439 log_debug("Illegal message of type \"%s\", path \"%s\" "
1440 "interface \"%s\", member \"%s\"",
1441 cdbus_repr_msgtype(msg), dbus_message_get_path(msg),
1442 dbus_message_get_interface(msg), dbus_message_get_member(msg));
1443 dbus_set_error_const(&err, CDBUS_ERROR_BADMSG, CDBUS_ERROR_BADMSG_S);
1444 }
1445
1446 finished:
1447 return cdbus_send_reply_or_error(conn, ret, msg, reply, &err);
1448 }
1449
1450 /**
1451 * Send a signal with a Window ID as argument.
1452 *
1453 * @param ps current session
1454 * @param name signal name
1455 * @param wid window ID
1456 */
1457 static bool cdbus_signal_wid(struct cdbus_data *cd, const char *interface,
1458 const char *name, xcb_window_t wid) {
1459 DBusMessage *msg = dbus_message_new_signal(CDBUS_OBJECT_NAME, interface, name);
1460 if (!msg) {
1461 log_error("Failed to create D-Bus signal.");
1462 return false;
1463 }
1464
1465 if (!cdbus_append_wid(msg, wid)) {
1466 dbus_message_unref(msg);
1467 return false;
1468 }
1469
1470 if (!dbus_connection_send(cd->dbus_conn, msg, NULL)) {
1471 log_error("Failed to send D-Bus signal.");
1472 dbus_message_unref(msg);
1473 return false;
1474 }
1475
1476 dbus_connection_flush(cd->dbus_conn);
1477 dbus_message_unref(msg);
1478 return true;
1479 }
1480
1481 /** @name Core callbacks
1482 */
1483 ///@{
1484 void cdbus_ev_win_added(struct cdbus_data *cd, struct win *w) {
1485 if (cd->dbus_conn) {
1486 cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_added", win_id(w));
1487 cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinAdded", win_id(w));
1488 }
1489 }
1490
1491 void cdbus_ev_win_destroyed(struct cdbus_data *cd, struct win *w) {
1492 if (cd->dbus_conn) {
1493 cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_destroyed", win_id(w));
1494 cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinDestroyed", win_id(w));
1495 }
1496 }
1497
1498 void cdbus_ev_win_mapped(struct cdbus_data *cd, struct win *w) {
1499 if (cd->dbus_conn) {
1500 cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_mapped", win_id(w));
1501 cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinMapped", win_id(w));
1502 }
1503 }
1504
1505 void cdbus_ev_win_unmapped(struct cdbus_data *cd, struct win *w) {
1506 if (cd->dbus_conn) {
1507 cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_unmapped", win_id(w));
1508 cdbus_signal_wid(cd, PICOM_COMPOSITOR_INTERFACE, "WinUnmapped", win_id(w));
1509 }
1510 }
1511
1512 void cdbus_ev_win_focusout(struct cdbus_data *cd, struct win *w) {
1513 if (cd->dbus_conn) {
1514 cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_focusout", win_id(w));
1515 }
1516 }
1517
1518 void cdbus_ev_win_focusin(struct cdbus_data *cd, struct win *w) {
1519 if (cd->dbus_conn) {
1520 cdbus_signal_wid(cd, CDBUS_INTERFACE_NAME, "win_focusin", win_id(w));
1521 }
1522 }
1523 //!@}