Debian Codegraph @

src/dbus.c

raw
1 /*
2 * Compton - a compositor for X11
3 *
4 * Based on `xcompmgr` - Copyright (c) 2003, Keith Packard
5 *
6 * Copyright (c) 2011-2013, Christopher Jeffrey
7 * See LICENSE for more information.
8 *
9 */
10
11 #include "dbus.h"
12
13 /**
14 * Initialize D-Bus connection.
15 */
16 bool
17 cdbus_init(session_t *ps) {
18 DBusError err = { };
19
20 // Initialize
21 dbus_error_init(&err);
22
23 // Connect to D-Bus
24 // Use dbus_bus_get_private() so we can fully recycle it ourselves
25 ps->dbus_conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
26 if (dbus_error_is_set(&err)) {
27 printf_errf("(): D-Bus connection failed (%s).", err.message);
28 dbus_error_free(&err);
29 return false;
30 }
31
32 if (!ps->dbus_conn) {
33 printf_errf("(): D-Bus connection failed for unknown reason.");
34 return false;
35 }
36
37 // Avoid exiting on disconnect
38 dbus_connection_set_exit_on_disconnect(ps->dbus_conn, false);
39
40 // Request service name
41 {
42 // Build service name
43 char *service = mstrjoin3(CDBUS_SERVICE_NAME, ".", ps->o.display_repr);
44 ps->dbus_service = service;
45
46 // Request for the name
47 int ret = dbus_bus_request_name(ps->dbus_conn, service,
48 DBUS_NAME_FLAG_DO_NOT_QUEUE, &err);
49
50 if (dbus_error_is_set(&err)) {
51 printf_errf("(): Failed to obtain D-Bus name (%s).", err.message);
52 dbus_error_free(&err);
53 }
54
55 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret
56 && DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER != ret) {
57 printf_errf("(): Failed to become the primary owner of requested "
58 "D-Bus name (%d).", ret);
59 }
60 }
61
62
63 // Add watch handlers
64 if (!dbus_connection_set_watch_functions(ps->dbus_conn,
65 cdbus_callback_add_watch, cdbus_callback_remove_watch,
66 cdbus_callback_watch_toggled, ps, NULL)) {
67 printf_errf("(): Failed to add D-Bus watch functions.");
68 return false;
69 }
70
71 // Add timeout handlers
72 if (!dbus_connection_set_timeout_functions(ps->dbus_conn,
73 cdbus_callback_add_timeout, cdbus_callback_remove_timeout,
74 cdbus_callback_timeout_toggled, ps, NULL)) {
75 printf_errf("(): Failed to add D-Bus timeout functions.");
76 return false;
77 }
78
79 // Add match
80 dbus_bus_add_match(ps->dbus_conn,
81 "type='method_call',interface='" CDBUS_INTERFACE_NAME "'", &err);
82 if (dbus_error_is_set(&err)) {
83 printf_errf("(): Failed to add D-Bus match.");
84 dbus_error_free(&err);
85 return false;
86 }
87
88 return true;
89 }
90
91 /**
92 * Destroy D-Bus connection.
93 */
94 void
95 cdbus_destroy(session_t *ps) {
96 if (ps->dbus_conn) {
97 // Release DBus name firstly
98 if (ps->dbus_service) {
99 DBusError err = { };
100 dbus_error_init(&err);
101
102 dbus_bus_release_name(ps->dbus_conn, ps->dbus_service, &err);
103 if (dbus_error_is_set(&err)) {
104 printf_errf("(): Failed to release DBus name (%s).",
105 err.message);
106 dbus_error_free(&err);
107 }
108 }
109
110 // Close and unref the connection
111 dbus_connection_close(ps->dbus_conn);
112 dbus_connection_unref(ps->dbus_conn);
113 }
114 }
115
116 /** @name DBusTimeout handling
117 */
118 ///@{
119
120 /**
121 * Callback for adding D-Bus timeout.
122 */
123 static dbus_bool_t
124 cdbus_callback_add_timeout(DBusTimeout *timeout, void *data) {
125 session_t *ps = data;
126
127 timeout_t *ptmout = timeout_insert(ps, dbus_timeout_get_interval(timeout),
128 cdbus_callback_handle_timeout, timeout);
129 if (ptmout)
130 dbus_timeout_set_data(timeout, ptmout, NULL);
131
132 return (bool) ptmout;
133 }
134
135 /**
136 * Callback for removing D-Bus timeout.
137 */
138 static void
139 cdbus_callback_remove_timeout(DBusTimeout *timeout, void *data) {
140 session_t *ps = data;
141
142 timeout_t *ptmout = dbus_timeout_get_data(timeout);
143 assert(ptmout);
144 if (ptmout)
145 timeout_drop(ps, ptmout);
146 }
147
148 /**
149 * Callback for toggling a D-Bus timeout.
150 */
151 static void
152 cdbus_callback_timeout_toggled(DBusTimeout *timeout, void *data) {
153 timeout_t *ptmout = dbus_timeout_get_data(timeout);
154
155 assert(ptmout);
156 if (ptmout) {
157 ptmout->enabled = dbus_timeout_get_enabled(timeout);
158 // Refresh interval as libdbus doc says: "Whenever a timeout is toggled,
159 // its interval may change."
160 ptmout->interval = dbus_timeout_get_interval(timeout);
161 }
162 }
163
164 /**
165 * Callback for handling a D-Bus timeout.
166 */
167 static bool
168 cdbus_callback_handle_timeout(session_t *ps, timeout_t *ptmout) {
169 assert(ptmout && ptmout->data);
170 if (ptmout && ptmout->data)
171 return dbus_timeout_handle(ptmout->data);
172
173 return false;
174 }
175
176 ///@}
177
178 /** @name DBusWatch handling
179 */
180 ///@{
181
182 /**
183 * Callback for adding D-Bus watch.
184 */
185 static dbus_bool_t
186 cdbus_callback_add_watch(DBusWatch *watch, void *data) {
187 // Leave disabled watches alone
188 if (!dbus_watch_get_enabled(watch))
189 return TRUE;
190
191 session_t *ps = data;
192
193 // Insert the file descriptor
194 fds_insert(ps, dbus_watch_get_unix_fd(watch),
195 cdbus_get_watch_cond(watch));
196
197 // Always return true
198 return TRUE;
199 }
200
201 /**
202 * Callback for removing D-Bus watch.
203 */
204 static void
205 cdbus_callback_remove_watch(DBusWatch *watch, void *data) {
206 session_t *ps = data;
207
208 fds_drop(ps, dbus_watch_get_unix_fd(watch),
209 cdbus_get_watch_cond(watch));
210 }
211
212 /**
213 * Callback for toggling D-Bus watch status.
214 */
215 static void
216 cdbus_callback_watch_toggled(DBusWatch *watch, void *data) {
217 if (dbus_watch_get_enabled(watch)) {
218 cdbus_callback_add_watch(watch, data);
219 }
220 else {
221 cdbus_callback_remove_watch(watch, data);
222 }
223 }
224
225 ///@}
226
227 /** @name Message argument appending callbacks
228 */
229 ///@{
230
231 /**
232 * Callback to append a bool argument to a message.
233 */
234 static bool
235 cdbus_apdarg_bool(session_t *ps, DBusMessage *msg, const void *data) {
236 assert(data);
237
238 dbus_bool_t val = *(const bool *) data;
239
240 if (!dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &val,
241 DBUS_TYPE_INVALID)) {
242 printf_errf("(): Failed to append argument.");
243 return false;
244 }
245
246 return true;
247 }
248
249 /**
250 * Callback to append an int32 argument to a message.
251 */
252 static bool
253 cdbus_apdarg_int32(session_t *ps, DBusMessage *msg, const void *data) {
254 if (!dbus_message_append_args(msg, DBUS_TYPE_INT32, data,
255 DBUS_TYPE_INVALID)) {
256 printf_errf("(): Failed to append argument.");
257 return false;
258 }
259
260 return true;
261 }
262
263 /**
264 * Callback to append an uint32 argument to a message.
265 */
266 static bool
267 cdbus_apdarg_uint32(session_t *ps, DBusMessage *msg, const void *data) {
268 if (!dbus_message_append_args(msg, DBUS_TYPE_UINT32, data,
269 DBUS_TYPE_INVALID)) {
270 printf_errf("(): Failed to append argument.");
271 return false;
272 }
273
274 return true;
275 }
276
277 /**
278 * Callback to append a double argument to a message.
279 */
280 static bool
281 cdbus_apdarg_double(session_t *ps, DBusMessage *msg, const void *data) {
282 if (!dbus_message_append_args(msg, DBUS_TYPE_DOUBLE, data,
283 DBUS_TYPE_INVALID)) {
284 printf_errf("(): Failed to append argument.");
285 return false;
286 }
287
288 return true;
289 }
290
291 /**
292 * Callback to append a Window argument to a message.
293 */
294 static bool
295 cdbus_apdarg_wid(session_t *ps, DBusMessage *msg, const void *data) {
296 assert(data);
297 cdbus_window_t val = *(const Window *)data;
298
299 if (!dbus_message_append_args(msg, CDBUS_TYPE_WINDOW, &val,
300 DBUS_TYPE_INVALID)) {
301 printf_errf("(): Failed to append argument.");
302 return false;
303 }
304
305 return true;
306 }
307
308 /**
309 * Callback to append an cdbus_enum_t argument to a message.
310 */
311 static bool
312 cdbus_apdarg_enum(session_t *ps, DBusMessage *msg, const void *data) {
313 assert(data);
314 if (!dbus_message_append_args(msg, CDBUS_TYPE_ENUM, data,
315 DBUS_TYPE_INVALID)) {
316 printf_errf("(): Failed to append argument.");
317 return false;
318 }
319
320 return true;
321 }
322
323 /**
324 * Callback to append a string argument to a message.
325 */
326 static bool
327 cdbus_apdarg_string(session_t *ps, DBusMessage *msg, const void *data) {
328 const char *str = data;
329 if (!str)
330 str = "";
331
332 if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &str,
333 DBUS_TYPE_INVALID)) {
334 printf_errf("(): Failed to append argument.");
335 return false;
336 }
337
338 return true;
339 }
340
341 /**
342 * Callback to append all window IDs to a message.
343 */
344 static bool
345 cdbus_apdarg_wids(session_t *ps, DBusMessage *msg, const void *data) {
346 // Get the number of wids we are to include
347 unsigned count = 0;
348 for (win *w = ps->list; w; w = w->next) {
349 if (!w->destroyed)
350 ++count;
351 }
352
353 // Allocate memory for an array of window IDs
354 cdbus_window_t *arr = malloc(sizeof(cdbus_window_t) * count);
355 if (!arr) {
356 printf_errf("(): Failed to allocate memory for window ID array.");
357 return false;
358 }
359
360 // Build the array
361 {
362 cdbus_window_t *pcur = arr;
363 for (win *w = ps->list; w; w = w->next) {
364 if (!w->destroyed) {
365 *pcur = w->id;
366 ++pcur;
367 assert(pcur <= arr + count);
368 }
369 }
370 assert(pcur == arr + count);
371 }
372
373 // Append arguments
374 if (!dbus_message_append_args(msg, DBUS_TYPE_ARRAY, CDBUS_TYPE_WINDOW,
375 &arr, count, DBUS_TYPE_INVALID)) {
376 printf_errf("(): Failed to append argument.");
377 free(arr);
378 return false;
379 }
380
381 free(arr);
382 return true;
383 }
384 ///@}
385
386 /**
387 * Send a D-Bus signal.
388 *
389 * @param ps current session
390 * @param name signal name
391 * @param func a function that modifies the built message, to, for example,
392 * add an argument
393 * @param data data pointer to pass to the function
394 */
395 static bool
396 cdbus_signal(session_t *ps, const char *name,
397 bool (*func)(session_t *ps, DBusMessage *msg, const void *data),
398 const void *data) {
399 DBusMessage* msg = NULL;
400
401 // Create a signal
402 msg = dbus_message_new_signal(CDBUS_OBJECT_NAME, CDBUS_INTERFACE_NAME,
403 name);
404 if (!msg) {
405 printf_errf("(): Failed to create D-Bus signal.");
406 return false;
407 }
408
409 // Append arguments onto message
410 if (func && !func(ps, msg, data)) {
411 dbus_message_unref(msg);
412 return false;
413 }
414
415 // Send the message and flush the connection
416 if (!dbus_connection_send(ps->dbus_conn, msg, NULL)) {
417 printf_errf("(): Failed to send D-Bus signal.");
418 dbus_message_unref(msg);
419 return false;
420 }
421 dbus_connection_flush(ps->dbus_conn);
422
423 // Free the message
424 dbus_message_unref(msg);
425
426 return true;
427 }
428
429 /**
430 * Send a D-Bus reply.
431 *
432 * @param ps current session
433 * @param srcmsg original message
434 * @param func a function that modifies the built message, to, for example,
435 * add an argument
436 * @param data data pointer to pass to the function
437 */
438 static bool
439 cdbus_reply(session_t *ps, DBusMessage *srcmsg,
440 bool (*func)(session_t *ps, DBusMessage *msg, const void *data),
441 const void *data) {
442 DBusMessage* msg = NULL;
443
444 // Create a reply
445 msg = dbus_message_new_method_return(srcmsg);
446 if (!msg) {
447 printf_errf("(): Failed to create D-Bus reply.");
448 return false;
449 }
450
451 // Append arguments onto message
452 if (func && !func(ps, msg, data)) {
453 dbus_message_unref(msg);
454 return false;
455 }
456
457 // Send the message and flush the connection
458 if (!dbus_connection_send(ps->dbus_conn, msg, NULL)) {
459 printf_errf("(): Failed to send D-Bus reply.");
460 dbus_message_unref(msg);
461 return false;
462 }
463 dbus_connection_flush(ps->dbus_conn);
464
465 // Free the message
466 dbus_message_unref(msg);
467
468 return true;
469 }
470
471 /**
472 * Send a D-Bus error reply.
473 *
474 * @param ps current session
475 * @param msg the new error DBusMessage
476 */
477 static bool
478 cdbus_reply_errm(session_t *ps, DBusMessage *msg) {
479 if (!msg) {
480 printf_errf("(): Failed to create D-Bus reply.");
481 return false;
482 }
483
484 // Send the message and flush the connection
485 if (!dbus_connection_send(ps->dbus_conn, msg, NULL)) {
486 printf_errf("(): Failed to send D-Bus reply.");
487 dbus_message_unref(msg);
488 return false;
489 }
490 dbus_connection_flush(ps->dbus_conn);
491
492 // Free the message
493 dbus_message_unref(msg);
494
495 return true;
496 }
497
498 /**
499 * Get n-th argument of a D-Bus message.
500 *
501 * @param count the position of the argument to get, starting from 0
502 * @param type libdbus type number of the type
503 * @param pdest pointer to the target
504 * @return true if successful, false otherwise.
505 */
506 static bool
507 cdbus_msg_get_arg(DBusMessage *msg, int count, const int type, void *pdest) {
508 assert(count >= 0);
509
510 DBusMessageIter iter = { };
511 if (!dbus_message_iter_init(msg, &iter)) {
512 printf_errf("(): Message has no argument.");
513 return false;
514 }
515
516 {
517 const int oldcount = count;
518 while (count) {
519 if (!dbus_message_iter_next(&iter)) {
520 printf_errf("(): Failed to find argument %d.", oldcount);
521 return false;
522 }
523 --count;
524 }
525 }
526
527 if (type != dbus_message_iter_get_arg_type(&iter)) {
528 printf_errf("(): Argument has incorrect type.");
529 return false;
530 }
531
532 dbus_message_iter_get_basic(&iter, pdest);
533
534 return true;
535 }
536
537 void
538 cdbus_loop(session_t *ps) {
539 dbus_connection_read_write(ps->dbus_conn, 0);
540 DBusMessage *msg = NULL;
541 while ((msg = dbus_connection_pop_message(ps->dbus_conn)))
542 cdbus_process(ps, msg);
543 }
544
545 /** @name Message processing
546 */
547 ///@{
548
549 /**
550 * Process a message from D-Bus.
551 */
552 static void
553 cdbus_process(session_t *ps, DBusMessage *msg) {
554 bool success = false;
555
556 #define cdbus_m_ismethod(method) \
557 dbus_message_is_method_call(msg, CDBUS_INTERFACE_NAME, method)
558
559 if (cdbus_m_ismethod("reset")) {
560 ps->reset = true;
561 if (!dbus_message_get_no_reply(msg))
562 cdbus_reply_bool(ps, msg, true);
563 success = true;
564 }
565 else if (cdbus_m_ismethod("repaint")) {
566 force_repaint(ps);
567 if (!dbus_message_get_no_reply(msg))
568 cdbus_reply_bool(ps, msg, true);
569 success = true;
570 }
571 else if (cdbus_m_ismethod("list_win")) {
572 success = cdbus_process_list_win(ps, msg);
573 }
574 else if (cdbus_m_ismethod("win_get")) {
575 success = cdbus_process_win_get(ps, msg);
576 }
577 else if (cdbus_m_ismethod("win_set")) {
578 success = cdbus_process_win_set(ps, msg);
579 }
580 else if (cdbus_m_ismethod("find_win")) {
581 success = cdbus_process_find_win(ps, msg);
582 }
583 else if (cdbus_m_ismethod("opts_get")) {
584 success = cdbus_process_opts_get(ps, msg);
585 }
586 else if (cdbus_m_ismethod("opts_set")) {
587 success = cdbus_process_opts_set(ps, msg);
588 }
589 #undef cdbus_m_ismethod
590 else if (dbus_message_is_method_call(msg,
591 "org.freedesktop.DBus.Introspectable", "Introspect")) {
592 success = cdbus_process_introspect(ps, msg);
593 }
594 else if (dbus_message_is_method_call(msg,
595 "org.freedesktop.DBus.Peer", "Ping")) {
596 cdbus_reply(ps, msg, NULL, NULL);
597 success = true;
598 }
599 else if (dbus_message_is_method_call(msg,
600 "org.freedesktop.DBus.Peer", "GetMachineId")) {
601 char *uuid = dbus_get_local_machine_id();
602 if (uuid) {
603 cdbus_reply_string(ps, msg, uuid);
604 dbus_free(uuid);
605 success = true;
606 }
607 }
608 else if (dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameAcquired")
609 || dbus_message_is_signal(msg, "org.freedesktop.DBus", "NameLost")) {
610 success = true;
611 }
612 else {
613 if (DBUS_MESSAGE_TYPE_ERROR == dbus_message_get_type(msg)) {
614 printf_errf("(): Error message of path \"%s\" "
615 "interface \"%s\", member \"%s\", error \"%s\"",
616 dbus_message_get_path(msg), dbus_message_get_interface(msg),
617 dbus_message_get_member(msg), dbus_message_get_error_name(msg));
618 }
619 else {
620 printf_errf("(): Illegal message of type \"%s\", path \"%s\" "
621 "interface \"%s\", member \"%s\"",
622 cdbus_repr_msgtype(msg), dbus_message_get_path(msg),
623 dbus_message_get_interface(msg), dbus_message_get_member(msg));
624 }
625 if (DBUS_MESSAGE_TYPE_METHOD_CALL == dbus_message_get_type(msg)
626 && !dbus_message_get_no_reply(msg))
627 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADMSG, CDBUS_ERROR_BADMSG_S);
628 success = true;
629 }
630
631 // If the message could not be processed, and an reply is expected, return
632 // an empty reply.
633 if (!success && DBUS_MESSAGE_TYPE_METHOD_CALL == dbus_message_get_type(msg)
634 && !dbus_message_get_no_reply(msg))
635 cdbus_reply_err(ps, msg, CDBUS_ERROR_UNKNOWN, CDBUS_ERROR_UNKNOWN_S);
636
637 // Free the message
638 dbus_message_unref(msg);
639 }
640
641 /**
642 * Process a list_win D-Bus request.
643 */
644 static bool
645 cdbus_process_list_win(session_t *ps, DBusMessage *msg) {
646 cdbus_reply(ps, msg, cdbus_apdarg_wids, NULL);
647
648 return true;
649 }
650
651 /**
652 * Process a win_get D-Bus request.
653 */
654 static bool
655 cdbus_process_win_get(session_t *ps, DBusMessage *msg) {
656 cdbus_window_t wid = None;
657 const char *target = NULL;
658 DBusError err = { };
659
660 if (!dbus_message_get_args(msg, &err,
661 CDBUS_TYPE_WINDOW, &wid,
662 DBUS_TYPE_STRING, &target,
663 DBUS_TYPE_INVALID)) {
664 printf_errf("(): Failed to parse argument of \"win_get\" (%s).",
665 err.message);
666 dbus_error_free(&err);
667 return false;
668 }
669
670 win *w = find_win(ps, wid);
671
672 if (!w) {
673 printf_errf("(): Window %#010x not found.", wid);
674 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
675 return true;
676 }
677
678 #define cdbus_m_win_get_do(tgt, apdarg_func) \
679 if (!strcmp(MSTR(tgt), target)) { \
680 apdarg_func(ps, msg, w->tgt); \
681 return true; \
682 }
683
684 cdbus_m_win_get_do(id, cdbus_reply_wid);
685
686 // next
687 if (!strcmp("next", target)) {
688 cdbus_reply_wid(ps, msg, (w->next ? w->next->id: 0));
689 return true;
690 }
691
692 // map_state
693 if (!strcmp("map_state", target)) {
694 cdbus_reply_bool(ps, msg, w->a.map_state);
695 return true;
696 }
697
698 cdbus_m_win_get_do(mode, cdbus_reply_enum);
699 cdbus_m_win_get_do(client_win, cdbus_reply_wid);
700 cdbus_m_win_get_do(damaged, cdbus_reply_bool);
701 cdbus_m_win_get_do(destroyed, cdbus_reply_bool);
702 cdbus_m_win_get_do(window_type, cdbus_reply_enum);
703 cdbus_m_win_get_do(wmwin, cdbus_reply_bool);
704 cdbus_m_win_get_do(leader, cdbus_reply_wid);
705 // focused_real
706 if (!strcmp("focused_real", target)) {
707 cdbus_reply_bool(ps, msg, win_is_focused_real(ps, w));
708 return true;
709 }
710 cdbus_m_win_get_do(fade_force, cdbus_reply_enum);
711 cdbus_m_win_get_do(shadow_force, cdbus_reply_enum);
712 cdbus_m_win_get_do(focused_force, cdbus_reply_enum);
713 cdbus_m_win_get_do(invert_color_force, cdbus_reply_enum);
714 cdbus_m_win_get_do(name, cdbus_reply_string);
715 cdbus_m_win_get_do(class_instance, cdbus_reply_string);
716 cdbus_m_win_get_do(class_general, cdbus_reply_string);
717 cdbus_m_win_get_do(role, cdbus_reply_string);
718
719 cdbus_m_win_get_do(opacity, cdbus_reply_uint32);
720 cdbus_m_win_get_do(opacity_tgt, cdbus_reply_uint32);
721 cdbus_m_win_get_do(has_opacity_prop, cdbus_reply_bool);
722 cdbus_m_win_get_do(opacity_prop, cdbus_reply_uint32);
723 cdbus_m_win_get_do(opacity_is_set, cdbus_reply_bool);
724 cdbus_m_win_get_do(opacity_set, cdbus_reply_uint32);
725
726 cdbus_m_win_get_do(frame_opacity, cdbus_reply_double);
727 if (!strcmp("left_width", target)) {
728 cdbus_reply_uint32(ps, msg, w->frame_extents.left);
729 return true;
730 }
731 if (!strcmp("right_width", target)) {
732 cdbus_reply_uint32(ps, msg, w->frame_extents.right);
733 return true;
734 }
735 if (!strcmp("top_width", target)) {
736 cdbus_reply_uint32(ps, msg, w->frame_extents.top);
737 return true;
738 }
739 if (!strcmp("bottom_width", target)) {
740 cdbus_reply_uint32(ps, msg, w->frame_extents.bottom);
741 return true;
742 }
743
744 cdbus_m_win_get_do(shadow, cdbus_reply_bool);
745 cdbus_m_win_get_do(fade, cdbus_reply_bool);
746 cdbus_m_win_get_do(invert_color, cdbus_reply_bool);
747 cdbus_m_win_get_do(blur_background, cdbus_reply_bool);
748 #undef cdbus_m_win_get_do
749
750 printf_errf("(): " CDBUS_ERROR_BADTGT_S, target);
751 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
752
753 return true;
754 }
755
756 /**
757 * Process a win_set D-Bus request.
758 */
759 static bool
760 cdbus_process_win_set(session_t *ps, DBusMessage *msg) {
761 cdbus_window_t wid = None;
762 const char *target = NULL;
763 DBusError err = { };
764
765 if (!dbus_message_get_args(msg, &err,
766 CDBUS_TYPE_WINDOW, &wid,
767 DBUS_TYPE_STRING, &target,
768 DBUS_TYPE_INVALID)) {
769 printf_errf("(): Failed to parse argument of \"win_set\" (%s).",
770 err.message);
771 dbus_error_free(&err);
772 return false;
773 }
774
775 win *w = find_win(ps, wid);
776
777 if (!w) {
778 printf_errf("(): Window %#010x not found.", wid);
779 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
780 return true;
781 }
782
783 #define cdbus_m_win_set_do(tgt, type, real_type) \
784 if (!strcmp(MSTR(tgt), target)) { \
785 real_type val; \
786 if (!cdbus_msg_get_arg(msg, 2, type, &val)) \
787 return false; \
788 w->tgt = val; \
789 goto cdbus_process_win_set_success; \
790 }
791
792 if (!strcmp("shadow_force", target)) {
793 cdbus_enum_t val = UNSET;
794 if (!cdbus_msg_get_arg(msg, 2, CDBUS_TYPE_ENUM, &val))
795 return false;
796 win_set_shadow_force(ps, w, val);
797 goto cdbus_process_win_set_success;
798 }
799
800 if (!strcmp("fade_force", target)) {
801 cdbus_enum_t val = UNSET;
802 if (!cdbus_msg_get_arg(msg, 2, CDBUS_TYPE_ENUM, &val))
803 return false;
804 win_set_fade_force(ps, w, val);
805 goto cdbus_process_win_set_success;
806 }
807
808 if (!strcmp("focused_force", target)) {
809 cdbus_enum_t val = UNSET;
810 if (!cdbus_msg_get_arg(msg, 2, CDBUS_TYPE_ENUM, &val))
811 return false;
812 win_set_focused_force(ps, w, val);
813 goto cdbus_process_win_set_success;
814 }
815
816 if (!strcmp("invert_color_force", target)) {
817 cdbus_enum_t val = UNSET;
818 if (!cdbus_msg_get_arg(msg, 2, CDBUS_TYPE_ENUM, &val))
819 return false;
820 win_set_invert_color_force(ps, w, val);
821 goto cdbus_process_win_set_success;
822 }
823 #undef cdbus_m_win_set_do
824
825 printf_errf("(): " CDBUS_ERROR_BADTGT_S, target);
826 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
827
828 return true;
829
830 cdbus_process_win_set_success:
831 if (!dbus_message_get_no_reply(msg))
832 cdbus_reply_bool(ps, msg, true);
833 return true;
834 }
835
836 /**
837 * Process a find_win D-Bus request.
838 */
839 static bool
840 cdbus_process_find_win(session_t *ps, DBusMessage *msg) {
841 const char *target = NULL;
842
843 if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target))
844 return false;
845
846 Window wid = None;
847
848 // Find window by client window
849 if (!strcmp("client", target)) {
850 cdbus_window_t client = None;
851 if (!cdbus_msg_get_arg(msg, 1, CDBUS_TYPE_WINDOW, &client))
852 return false;
853 win *w = find_toplevel(ps, client);
854 if (w)
855 wid = w->id;
856 }
857 // Find focused window
858 else if (!strcmp("focused", target)) {
859 win *w = find_focused(ps);
860 if (w)
861 wid = w->id;
862 }
863 else {
864 printf_errf("(): " CDBUS_ERROR_BADTGT_S, target);
865 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
866
867 return true;
868 }
869
870 cdbus_reply_wid(ps, msg, wid);
871
872 return true;
873 }
874
875 /**
876 * Process a opts_get D-Bus request.
877 */
878 static bool
879 cdbus_process_opts_get(session_t *ps, DBusMessage *msg) {
880 const char *target = NULL;
881
882 if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target))
883 return false;
884
885 #define cdbus_m_opts_get_do(tgt, apdarg_func) \
886 if (!strcmp(MSTR(tgt), target)) { \
887 apdarg_func(ps, msg, ps->o.tgt); \
888 return true; \
889 }
890
891 // version
892 if (!strcmp("version", target)) {
893 cdbus_reply_string(ps, msg, COMPTON_VERSION);
894 return true;
895 }
896
897 // pid
898 if (!strcmp("pid", target)) {
899 cdbus_reply_int32(ps, msg, getpid());
900 return true;
901 }
902
903 // display
904 if (!strcmp("display", target)) {
905 cdbus_reply_string(ps, msg, DisplayString(ps->dpy));
906 return true;
907 }
908
909 cdbus_m_opts_get_do(config_file, cdbus_reply_string);
910 cdbus_m_opts_get_do(display_repr, cdbus_reply_string);
911 cdbus_m_opts_get_do(write_pid_path, cdbus_reply_string);
912 cdbus_m_opts_get_do(mark_wmwin_focused, cdbus_reply_bool);
913 cdbus_m_opts_get_do(mark_ovredir_focused, cdbus_reply_bool);
914 cdbus_m_opts_get_do(fork_after_register, cdbus_reply_bool);
915 cdbus_m_opts_get_do(detect_rounded_corners, cdbus_reply_bool);
916 cdbus_m_opts_get_do(paint_on_overlay, cdbus_reply_bool);
917 // paint_on_overlay_id: Get ID of the X composite overlay window
918 if (!strcmp("paint_on_overlay_id", target)) {
919 cdbus_reply_uint32(ps, msg, ps->overlay);
920 return true;
921 }
922 cdbus_m_opts_get_do(unredir_if_possible, cdbus_reply_bool);
923 cdbus_m_opts_get_do(unredir_if_possible_delay, cdbus_reply_int32);
924 cdbus_m_opts_get_do(redirected_force, cdbus_reply_enum);
925 cdbus_m_opts_get_do(stoppaint_force, cdbus_reply_enum);
926 cdbus_m_opts_get_do(logpath, cdbus_reply_string);
927 cdbus_m_opts_get_do(synchronize, cdbus_reply_bool);
928
929 cdbus_m_opts_get_do(refresh_rate, cdbus_reply_int32);
930 cdbus_m_opts_get_do(sw_opti, cdbus_reply_bool);
931 if (!strcmp("vsync", target)) {
932 assert(ps->o.vsync < sizeof(VSYNC_STRS) / sizeof(VSYNC_STRS[0]));
933 cdbus_reply_string(ps, msg, VSYNC_STRS[ps->o.vsync]);
934 return true;
935 }
936 if (!strcmp("backend", target)) {
937 assert(ps->o.backend < sizeof(BACKEND_STRS) / sizeof(BACKEND_STRS[0]));
938 cdbus_reply_string(ps, msg, BACKEND_STRS[ps->o.backend]);
939 return true;
940 }
941 cdbus_m_opts_get_do(dbe, cdbus_reply_bool);
942 cdbus_m_opts_get_do(vsync_aggressive, cdbus_reply_bool);
943
944 cdbus_m_opts_get_do(shadow_red, cdbus_reply_double);
945 cdbus_m_opts_get_do(shadow_green, cdbus_reply_double);
946 cdbus_m_opts_get_do(shadow_blue, cdbus_reply_double);
947 cdbus_m_opts_get_do(shadow_radius, cdbus_reply_int32);
948 cdbus_m_opts_get_do(shadow_offset_x, cdbus_reply_int32);
949 cdbus_m_opts_get_do(shadow_offset_y, cdbus_reply_int32);
950 cdbus_m_opts_get_do(shadow_opacity, cdbus_reply_double);
951 cdbus_m_opts_get_do(clear_shadow, cdbus_reply_bool);
952 cdbus_m_opts_get_do(xinerama_shadow_crop, cdbus_reply_bool);
953
954 cdbus_m_opts_get_do(fade_delta, cdbus_reply_int32);
955 cdbus_m_opts_get_do(fade_in_step, cdbus_reply_int32);
956 cdbus_m_opts_get_do(fade_out_step, cdbus_reply_int32);
957 cdbus_m_opts_get_do(no_fading_openclose, cdbus_reply_bool);
958
959 cdbus_m_opts_get_do(blur_background, cdbus_reply_bool);
960 cdbus_m_opts_get_do(blur_background_frame, cdbus_reply_bool);
961 cdbus_m_opts_get_do(blur_background_fixed, cdbus_reply_bool);
962
963 cdbus_m_opts_get_do(inactive_dim, cdbus_reply_double);
964 cdbus_m_opts_get_do(inactive_dim_fixed, cdbus_reply_bool);
965
966 cdbus_m_opts_get_do(use_ewmh_active_win, cdbus_reply_bool);
967 cdbus_m_opts_get_do(detect_transient, cdbus_reply_bool);
968 cdbus_m_opts_get_do(detect_client_leader, cdbus_reply_bool);
969
970 #ifdef CONFIG_VSYNC_OPENGL
971 cdbus_m_opts_get_do(glx_no_stencil, cdbus_reply_bool);
972 cdbus_m_opts_get_do(glx_copy_from_front, cdbus_reply_bool);
973 cdbus_m_opts_get_do(glx_use_copysubbuffermesa, cdbus_reply_bool);
974 cdbus_m_opts_get_do(glx_no_rebind_pixmap, cdbus_reply_bool);
975 cdbus_m_opts_get_do(glx_swap_method, cdbus_reply_int32);
976 #endif
977
978 cdbus_m_opts_get_do(track_focus, cdbus_reply_bool);
979 cdbus_m_opts_get_do(track_wdata, cdbus_reply_bool);
980 cdbus_m_opts_get_do(track_leader, cdbus_reply_bool);
981 #undef cdbus_m_opts_get_do
982
983 printf_errf("(): " CDBUS_ERROR_BADTGT_S, target);
984 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
985
986 return true;
987 }
988
989 /**
990 * Process a opts_set D-Bus request.
991 */
992 static bool
993 cdbus_process_opts_set(session_t *ps, DBusMessage *msg) {
994 const char *target = NULL;
995
996 if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target))
997 return false;
998
999 #define cdbus_m_opts_set_do(tgt, type, real_type) \
1000 if (!strcmp(MSTR(tgt), target)) { \
1001 real_type val; \
1002 if (!cdbus_msg_get_arg(msg, 1, type, &val)) \
1003 return false; \
1004 ps->o.tgt = val; \
1005 goto cdbus_process_opts_set_success; \
1006 }
1007
1008 // fade_delta
1009 if (!strcmp("fade_delta", target)) {
1010 int32_t val = 0.0;
1011 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_INT32, &val))
1012 return false;
1013 ps->o.fade_delta = max_i(val, 1);
1014 goto cdbus_process_opts_set_success;
1015 }
1016
1017 // fade_in_step
1018 if (!strcmp("fade_in_step", target)) {
1019 double val = 0.0;
1020 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_DOUBLE, &val))
1021 return false;
1022 ps->o.fade_in_step = normalize_d(val) * OPAQUE;
1023 goto cdbus_process_opts_set_success;
1024 }
1025
1026 // fade_out_step
1027 if (!strcmp("fade_out_step", target)) {
1028 double val = 0.0;
1029 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_DOUBLE, &val))
1030 return false;
1031 ps->o.fade_out_step = normalize_d(val) * OPAQUE;
1032 goto cdbus_process_opts_set_success;
1033 }
1034
1035 // no_fading_openclose
1036 if (!strcmp("no_fading_openclose", target)) {
1037 dbus_bool_t val = FALSE;
1038 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val))
1039 return false;
1040 opts_set_no_fading_openclose(ps, val);
1041 goto cdbus_process_opts_set_success;
1042 }
1043
1044 // unredir_if_possible
1045 if (!strcmp("unredir_if_possible", target)) {
1046 dbus_bool_t val = FALSE;
1047 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val))
1048 return false;
1049 if (ps->o.unredir_if_possible != val) {
1050 ps->o.unredir_if_possible = val;
1051 ps->ev_received = true;
1052 }
1053 goto cdbus_process_opts_set_success;
1054 }
1055
1056 // clear_shadow
1057 if (!strcmp("clear_shadow", target)) {
1058 dbus_bool_t val = FALSE;
1059 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val))
1060 return false;
1061 if (ps->o.clear_shadow != val) {
1062 ps->o.clear_shadow = val;
1063 force_repaint(ps);
1064 }
1065 goto cdbus_process_opts_set_success;
1066 }
1067
1068 // track_focus
1069 if (!strcmp("track_focus", target)) {
1070 dbus_bool_t val = FALSE;
1071 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_BOOLEAN, &val))
1072 return false;
1073 // You could enable this option, but never turn if off
1074 if (val) {
1075 opts_init_track_focus(ps);
1076 }
1077 goto cdbus_process_opts_set_success;
1078 }
1079
1080 // vsync
1081 if (!strcmp("vsync", target)) {
1082 const char * val = NULL;
1083 if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_STRING, &val))
1084 return false;
1085 vsync_deinit(ps);
1086 if (!parse_vsync(ps, val)) {
1087 printf_errf("(): " CDBUS_ERROR_BADARG_S, 1, "Value invalid.");
1088 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADARG, CDBUS_ERROR_BADARG_S, 1, "Value invalid.");
1089 }
1090 else if (!vsync_init(ps)) {
1091 printf_errf("(): " CDBUS_ERROR_CUSTOM_S, "Failed to initialize specified VSync method.");
1092 cdbus_reply_err(ps, msg, CDBUS_ERROR_CUSTOM, CDBUS_ERROR_CUSTOM_S, "Failed to initialize specified VSync method.");
1093 }
1094 else
1095 goto cdbus_process_opts_set_success;
1096 return true;
1097 }
1098
1099 // redirected_force
1100 if (!strcmp("redirected_force", target)) {
1101 cdbus_enum_t val = UNSET;
1102 if (!cdbus_msg_get_arg(msg, 1, CDBUS_TYPE_ENUM, &val))
1103 return false;
1104 ps->o.redirected_force = val;
1105 force_repaint(ps);
1106 goto cdbus_process_opts_set_success;
1107 }
1108
1109 // stoppaint_force
1110 cdbus_m_opts_set_do(stoppaint_force, CDBUS_TYPE_ENUM, cdbus_enum_t);
1111
1112 #undef cdbus_m_opts_set_do
1113
1114 printf_errf("(): " CDBUS_ERROR_BADTGT_S, target);
1115 cdbus_reply_err(ps, msg, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
1116
1117 return true;
1118
1119 cdbus_process_opts_set_success:
1120 if (!dbus_message_get_no_reply(msg))
1121 cdbus_reply_bool(ps, msg, true);
1122 return true;
1123 }
1124
1125 /**
1126 * Process an Introspect D-Bus request.
1127 */
1128 static bool
1129 cdbus_process_introspect(session_t *ps, DBusMessage *msg) {
1130 const static char *str_introspect =
1131 "<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
1132 " \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
1133 "<node name='" CDBUS_OBJECT_NAME "'>\n"
1134 " <interface name='org.freedesktop.DBus.Introspectable'>\n"
1135 " <method name='Introspect'>\n"
1136 " <arg name='data' direction='out' type='s' />\n"
1137 " </method>\n"
1138 " </interface>\n"
1139 " <interface name='org.freedesktop.DBus.Peer'>\n"
1140 " <method name='Ping' />\n"
1141 " <method name='GetMachineId'>\n"
1142 " <arg name='machine_uuid' direction='out' type='s' />\n"
1143 " </method>\n"
1144 " </interface>\n"
1145 " <interface name='" CDBUS_INTERFACE_NAME "'>\n"
1146 " <signal name='win_added'>\n"
1147 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1148 " </signal>\n"
1149 " <signal name='win_destroyed'>\n"
1150 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1151 " </signal>\n"
1152 " <signal name='win_mapped'>\n"
1153 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1154 " </signal>\n"
1155 " <signal name='win_unmapped'>\n"
1156 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1157 " </signal>\n"
1158 " <signal name='win_focusin'>\n"
1159 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1160 " </signal>\n"
1161 " <signal name='win_focusout'>\n"
1162 " <arg name='wid' type='" CDBUS_TYPE_WINDOW_STR "'/>\n"
1163 " </signal>\n"
1164 " <method name='reset' />\n"
1165 " <method name='repaint' />\n"
1166 " </interface>\n"
1167 "</node>\n";
1168
1169 cdbus_reply_string(ps, msg, str_introspect);
1170
1171 return true;
1172 }
1173 ///@}
1174
1175 /** @name Core callbacks
1176 */
1177 ///@{
1178 void
1179 cdbus_ev_win_added(session_t *ps, win *w) {
1180 if (ps->dbus_conn)
1181 cdbus_signal_wid(ps, "win_added", w->id);
1182 }
1183
1184 void
1185 cdbus_ev_win_destroyed(session_t *ps, win *w) {
1186 if (ps->dbus_conn)
1187 cdbus_signal_wid(ps, "win_destroyed", w->id);
1188 }
1189
1190 void
1191 cdbus_ev_win_mapped(session_t *ps, win *w) {
1192 if (ps->dbus_conn)
1193 cdbus_signal_wid(ps, "win_mapped", w->id);
1194 }
1195
1196 void
1197 cdbus_ev_win_unmapped(session_t *ps, win *w) {
1198 if (ps->dbus_conn)
1199 cdbus_signal_wid(ps, "win_unmapped", w->id);
1200 }
1201
1202 void
1203 cdbus_ev_win_focusout(session_t *ps, win *w) {
1204 if (ps->dbus_conn)
1205 cdbus_signal_wid(ps, "win_focusout", w->id);
1206 }
1207
1208 void
1209 cdbus_ev_win_focusin(session_t *ps, win *w) {
1210 if (ps->dbus_conn)
1211 cdbus_signal_wid(ps, "win_focusin", w->id);
1212 }
1213 //!@}