-
-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathdbus.c
More file actions
1460 lines (1278 loc) · 46.6 KB
/
Copy pathdbus.c
File metadata and controls
1460 lines (1278 loc) · 46.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
/*
* Compton - a compositor for X11
*
* Based on `xcompmgr` - Copyright (c) 2003, Keith Packard
*
* Copyright (c) 2011-2013, Christopher Jeffrey
* See LICENSE-mit for more information.
*
*/
#include <X11/Xlib.h>
#include <ctype.h>
#include <dbus/dbus.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <xcb/xcb.h>
#include "common.h"
#include "compiler.h"
#include "config.h"
#include "list.h"
#include "log.h"
#include "string_utils.h"
#include "transition.h"
#include "types.h"
#include "uthash_extra.h"
#include "utils.h"
#include "win.h"
#include "win_defs.h"
#include "wm.h"
#include "dbus.h"
struct cdbus_data {
/// Mainloop
struct ev_loop *loop;
/// DBus connection.
DBusConnection *dbus_conn;
/// DBus service name.
char *dbus_service;
};
// Window type
typedef uint32_t cdbus_window_t;
#define CDBUS_TYPE_WINDOW DBUS_TYPE_UINT32
#define CDBUS_TYPE_WINDOW_STR DBUS_TYPE_UINT32_AS_STRING
typedef uint32_t cdbus_enum_t;
#define CDBUS_TYPE_ENUM DBUS_TYPE_UINT32
#define CDBUS_TYPE_ENUM_STR DBUS_TYPE_UINT32_AS_STRING
#define CDBUS_SERVICE_NAME "com.github.chjj.compton"
#define CDBUS_INTERFACE_NAME CDBUS_SERVICE_NAME
#define CDBUS_OBJECT_NAME "/com/github/chjj/compton"
#define CDBUS_ERROR_PREFIX CDBUS_INTERFACE_NAME ".error"
#define CDBUS_ERROR_UNKNOWN CDBUS_ERROR_PREFIX ".unknown"
#define CDBUS_ERROR_UNKNOWN_S "Well, I don't know what happened. Do you?"
#define CDBUS_ERROR_BADMSG CDBUS_ERROR_PREFIX ".bad_message"
#define CDBUS_ERROR_BADMSG_S \
"Unrecognized command. Beware compton " \
"cannot make you a sandwich."
#define CDBUS_ERROR_BADARG CDBUS_ERROR_PREFIX ".bad_argument"
#define CDBUS_ERROR_BADARG_S "Failed to parse argument %d: %s"
#define CDBUS_ERROR_BADWIN CDBUS_ERROR_PREFIX ".bad_window"
#define CDBUS_ERROR_BADWIN_S "Requested window %#010x not found."
#define CDBUS_ERROR_BADTGT CDBUS_ERROR_PREFIX ".bad_target"
#define CDBUS_ERROR_BADTGT_S "Target \"%s\" not found."
#define CDBUS_ERROR_FORBIDDEN CDBUS_ERROR_PREFIX ".forbidden"
#define CDBUS_ERROR_FORBIDDEN_S "Incorrect password, access denied."
#define CDBUS_ERROR_CUSTOM CDBUS_ERROR_PREFIX ".custom"
#define CDBUS_ERROR_CUSTOM_S "%s"
#define cdbus_reply_err(conn, srcmsg, err_name, err_format, ...) \
cdbus_reply_errm(conn, dbus_message_new_error_printf( \
(srcmsg), (err_name), (err_format), ##__VA_ARGS__))
#define PICOM_WINDOW_INTERFACE "picom.Window"
#define PICOM_COMPOSITOR_INTERFACE "picom.Compositor"
static DBusHandlerResult cdbus_process(DBusConnection *conn, DBusMessage *m, void *);
static DBusHandlerResult cdbus_process_windows(DBusConnection *c, DBusMessage *msg, void *ud);
static dbus_bool_t cdbus_callback_add_timeout(DBusTimeout *timeout, void *data);
static void cdbus_callback_remove_timeout(DBusTimeout *timeout, void *data);
static void cdbus_callback_timeout_toggled(DBusTimeout *timeout, void *data);
static dbus_bool_t cdbus_callback_add_watch(DBusWatch *watch, void *data);
static void cdbus_callback_remove_watch(DBusWatch *watch, void *data);
static void cdbus_callback_watch_toggled(DBusWatch *watch, void *data);
/**
* Initialize D-Bus connection.
*/
struct cdbus_data *cdbus_init(session_t *ps, const char *uniq) {
auto cd = cmalloc(struct cdbus_data);
cd->dbus_service = NULL;
DBusError err = {};
// Initialize
dbus_error_init(&err);
// Connect to D-Bus
// Use dbus_bus_get_private() so we can fully recycle it ourselves
cd->dbus_conn = dbus_bus_get_private(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
log_error("D-Bus connection failed.");
goto fail;
}
if (!cd->dbus_conn) {
log_error("D-Bus connection failed for unknown reason.");
goto fail;
}
// Avoid exiting on disconnect
dbus_connection_set_exit_on_disconnect(cd->dbus_conn, false);
// Request service name
{
// Build service name
size_t service_len = strlen(CDBUS_SERVICE_NAME) + strlen(uniq) + 2;
char *service = ccalloc(service_len, char);
snprintf(service, service_len, "%s.%s", CDBUS_SERVICE_NAME, uniq);
// Make a valid dbus name by converting non alphanumeric characters to
// underscore
char *tmp = service + strlen(CDBUS_SERVICE_NAME) + 1;
while (*tmp) {
if (!isalnum((unsigned char)*tmp)) {
*tmp = '_';
}
tmp++;
}
cd->dbus_service = service;
// Request for the name
int ret = dbus_bus_request_name(cd->dbus_conn, service,
DBUS_NAME_FLAG_DO_NOT_QUEUE, &err);
if (dbus_error_is_set(&err)) {
log_error("Failed to obtain D-Bus name.");
goto fail;
}
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER &&
ret != DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER) {
log_error("Failed to become the primary owner of requested D-Bus "
"name (%d).",
ret);
goto fail;
}
}
// Add watch handlers
cd->loop = ps->loop;
if (!dbus_connection_set_watch_functions(cd->dbus_conn, cdbus_callback_add_watch,
cdbus_callback_remove_watch,
cdbus_callback_watch_toggled, cd, NULL)) {
log_error("Failed to add D-Bus watch functions.");
goto fail;
}
// Add timeout handlers
if (!dbus_connection_set_timeout_functions(
cd->dbus_conn, cdbus_callback_add_timeout, cdbus_callback_remove_timeout,
cdbus_callback_timeout_toggled, cd, NULL)) {
log_error("Failed to add D-Bus timeout functions.");
goto fail;
}
// Add match
dbus_bus_add_match(cd->dbus_conn,
"type='method_call',interface='" CDBUS_INTERFACE_NAME "'", &err);
if (dbus_error_is_set(&err)) {
log_error("Failed to add D-Bus match.");
goto fail;
}
dbus_connection_register_object_path(
cd->dbus_conn, CDBUS_OBJECT_NAME,
(DBusObjectPathVTable[]){{NULL, cdbus_process}}, ps);
dbus_connection_register_fallback(
cd->dbus_conn, CDBUS_OBJECT_NAME "/windows",
(DBusObjectPathVTable[]){{NULL, cdbus_process_windows}}, ps);
return cd;
fail:
if (dbus_error_is_set(&err)) {
log_error("D-Bus error %s: %s", err.name, err.message);
dbus_error_free(&err);
}
free(cd->dbus_service);
free(cd);
return NULL;
}
/**
* Destroy D-Bus connection.
*/
void cdbus_destroy(struct cdbus_data *cd) {
if (cd->dbus_conn) {
// Release DBus name firstly
if (cd->dbus_service) {
DBusError err = {};
dbus_error_init(&err);
dbus_bus_release_name(cd->dbus_conn, cd->dbus_service, &err);
if (dbus_error_is_set(&err)) {
log_error("Failed to release DBus name (%s).", err.message);
dbus_error_free(&err);
}
free(cd->dbus_service);
}
// Close and unref the connection
dbus_connection_close(cd->dbus_conn);
dbus_connection_unref(cd->dbus_conn);
}
free(cd);
}
/** @name DBusTimeout handling
*/
///@{
typedef struct ev_dbus_timer {
ev_timer w;
DBusTimeout *t;
} ev_dbus_timer;
/**
* Callback for handling a D-Bus timeout.
*/
static void
cdbus_callback_handle_timeout(EV_P attr_unused, ev_timer *w, int revents attr_unused) {
ev_dbus_timer *t = (void *)w;
dbus_timeout_handle(t->t);
}
/**
* Callback for adding D-Bus timeout.
*/
static dbus_bool_t cdbus_callback_add_timeout(DBusTimeout *timeout, void *data) {
struct cdbus_data *cd = data;
auto t = ccalloc(1, ev_dbus_timer);
double i = dbus_timeout_get_interval(timeout) / 1000.0;
ev_timer_init(&t->w, cdbus_callback_handle_timeout, i, i);
t->t = timeout;
dbus_timeout_set_data(timeout, t, NULL);
if (dbus_timeout_get_enabled(timeout)) {
ev_timer_start(cd->loop, &t->w);
}
return true;
}
/**
* Callback for removing D-Bus timeout.
*/
static void cdbus_callback_remove_timeout(DBusTimeout *timeout, void *data) {
struct cdbus_data *cd = data;
ev_dbus_timer *t = dbus_timeout_get_data(timeout);
assert(t);
ev_timer_stop(cd->loop, &t->w);
free(t);
}
/**
* Callback for toggling a D-Bus timeout.
*/
static void cdbus_callback_timeout_toggled(DBusTimeout *timeout, void *data) {
struct cdbus_data *cd = data;
ev_dbus_timer *t = dbus_timeout_get_data(timeout);
assert(t);
ev_timer_stop(cd->loop, &t->w);
if (dbus_timeout_get_enabled(timeout)) {
double i = dbus_timeout_get_interval(timeout) / 1000.0;
ev_timer_set(&t->w, i, i);
ev_timer_start(cd->loop, &t->w);
}
}
///@}
/** @name DBusWatch handling
*/
///@{
typedef struct ev_dbus_io {
ev_io w;
struct cdbus_data *cd;
DBusWatch *dw;
} ev_dbus_io;
void cdbus_io_callback(EV_P attr_unused, ev_io *w, int revents) {
ev_dbus_io *dw = (void *)w;
DBusWatchFlags flags = 0;
if (revents & EV_READ) {
flags |= DBUS_WATCH_READABLE;
}
if (revents & EV_WRITE) {
flags |= DBUS_WATCH_WRITABLE;
}
dbus_watch_handle(dw->dw, flags);
while (dbus_connection_dispatch(dw->cd->dbus_conn) != DBUS_DISPATCH_COMPLETE)
;
}
/**
* Determine the poll condition of a DBusWatch.
*/
static inline int cdbus_get_watch_cond(DBusWatch *watch) {
const unsigned flags = dbus_watch_get_flags(watch);
int condition = 0;
if (flags & DBUS_WATCH_READABLE) {
condition |= EV_READ;
}
if (flags & DBUS_WATCH_WRITABLE) {
condition |= EV_WRITE;
}
return condition;
}
/**
* Callback for adding D-Bus watch.
*/
static dbus_bool_t cdbus_callback_add_watch(DBusWatch *watch, void *data) {
struct cdbus_data *cd = data;
auto w = ccalloc(1, ev_dbus_io);
w->dw = watch;
w->cd = cd;
ev_io_init(&w->w, cdbus_io_callback, dbus_watch_get_unix_fd(watch),
cdbus_get_watch_cond(watch));
// Leave disabled watches alone
if (dbus_watch_get_enabled(watch)) {
ev_io_start(cd->loop, &w->w);
}
dbus_watch_set_data(watch, w, NULL);
// Always return true
return true;
}
/**
* Callback for removing D-Bus watch.
*/
static void cdbus_callback_remove_watch(DBusWatch *watch, void *data) {
struct cdbus_data *cd = data;
ev_dbus_io *w = dbus_watch_get_data(watch);
ev_io_stop(cd->loop, &w->w);
free(w);
}
/**
* Callback for toggling D-Bus watch status.
*/
static void cdbus_callback_watch_toggled(DBusWatch *watch, void *data) {
struct cdbus_data *cd = data;
ev_io *w = dbus_watch_get_data(watch);
if (dbus_watch_get_enabled(watch)) {
ev_io_start(cd->loop, w);
} else {
ev_io_stop(cd->loop, w);
}
}
///@}
static bool cdbus_append_boolean(DBusMessage *msg, dbus_bool_t val) {
return dbus_message_append_args(msg, DBUS_TYPE_BOOLEAN, &val, DBUS_TYPE_INVALID);
}
static bool cdbus_append_int32(DBusMessage *msg, int32_t val) {
return dbus_message_append_args(msg, DBUS_TYPE_INT32, &val, DBUS_TYPE_INVALID);
}
static bool cdbus_append_uint32(DBusMessage *msg, uint32_t val) {
return dbus_message_append_args(msg, DBUS_TYPE_UINT32, &val, DBUS_TYPE_INVALID);
}
static bool cdbus_append_double(DBusMessage *msg, double val) {
return dbus_message_append_args(msg, DBUS_TYPE_DOUBLE, &val, DBUS_TYPE_INVALID);
}
static bool cdbus_append_wid(DBusMessage *msg, xcb_window_t val_) {
cdbus_window_t val = val_;
return dbus_message_append_args(msg, CDBUS_TYPE_WINDOW, &val, DBUS_TYPE_INVALID);
}
static bool cdbus_append_enum(DBusMessage *msg, cdbus_enum_t val) {
return dbus_message_append_args(msg, CDBUS_TYPE_ENUM, &val, DBUS_TYPE_INVALID);
}
static bool cdbus_append_string(DBusMessage *msg, const char *data) {
const char *str = data ? data : "";
return dbus_message_append_args(msg, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
}
/// Append a window ID to a D-Bus message as a variant
static bool cdbus_append_wid_variant(DBusMessage *msg, xcb_window_t val_) {
cdbus_window_t val = val_;
DBusMessageIter it, it2;
dbus_message_iter_init_append(msg, &it);
if (!dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT,
CDBUS_TYPE_WINDOW_STR, &it2)) {
return false;
}
if (!dbus_message_iter_append_basic(&it2, CDBUS_TYPE_WINDOW, &val)) {
return false;
}
if (!dbus_message_iter_close_container(&it, &it2)) {
return false;
}
return true;
}
/// Append a boolean to a D-Bus message as a variant
static bool cdbus_append_bool_variant(DBusMessage *msg, dbus_bool_t val) {
DBusMessageIter it, it2;
dbus_message_iter_init_append(msg, &it);
if (!dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT,
DBUS_TYPE_BOOLEAN_AS_STRING, &it2)) {
return false;
}
if (!dbus_message_iter_append_basic(&it2, DBUS_TYPE_BOOLEAN, &val)) {
return false;
}
if (!dbus_message_iter_close_container(&it, &it2)) {
return false;
}
return true;
}
/// Append a string to a D-Bus message as a variant
static bool cdbus_append_string_variant(DBusMessage *msg, const char *data) {
const char *str = data ? data : "";
DBusMessageIter it, it2;
dbus_message_iter_init_append(msg, &it);
if (!dbus_message_iter_open_container(&it, DBUS_TYPE_VARIANT,
DBUS_TYPE_STRING_AS_STRING, &it2)) {
return false;
}
if (!dbus_message_iter_append_basic(&it2, DBUS_TYPE_STRING, &str)) {
log_error("Failed to append argument.");
return false;
}
if (!dbus_message_iter_close_container(&it, &it2)) {
return false;
}
return true;
}
static int cdbus_append_wids_callback(struct win *w, void *data) {
DBusMessageIter *iter = data;
cdbus_window_t wid = w->id;
if (!dbus_message_iter_append_basic(iter, CDBUS_TYPE_WINDOW, &wid)) {
return 1;
}
return 0;
}
/// Append all window IDs in the window list of a session to a D-Bus message
static bool cdbus_append_wids(DBusMessage *msg, session_t *ps) {
// Get the number of wids we are to include
unsigned count = wm_num_windows(ps->wm);
if (!count) {
// Nothing to append
return true;
}
DBusMessageIter it, subit;
dbus_message_iter_init_append(msg, &it);
if (!dbus_message_iter_open_container(&it, DBUS_TYPE_ARRAY,
DBUS_TYPE_UINT32_AS_STRING, &subit)) {
log_error("Failed to open container.");
return false;
}
auto result = wm_foreach(ps->wm, cdbus_append_wids_callback, &subit);
if (!dbus_message_iter_close_container(&it, &subit)) {
log_error("Failed to close container.");
return false;
}
if (result != 0) {
log_error("Failed to append argument.");
return false;
}
return true;
}
/**
* Get n-th argument of a D-Bus message.
*
* @param count the position of the argument to get, starting from 0
* @param type libdbus type number of the type
* @param pdest pointer to the target
* @return true if successful, false otherwise.
*/
static bool cdbus_msg_get_arg(DBusMessage *msg, int count, const int type, void *pdest) {
assert(count >= 0);
DBusMessageIter iter = {};
if (!dbus_message_iter_init(msg, &iter)) {
log_error("Message has no argument.");
return false;
}
{
const int oldcount = count;
while (count) {
if (!dbus_message_iter_next(&iter)) {
log_error("Failed to find argument %d.", oldcount);
return false;
}
--count;
}
}
if (type != dbus_message_iter_get_arg_type(&iter)) {
log_error("Argument has incorrect type.");
return false;
}
dbus_message_iter_get_basic(&iter, pdest);
return true;
}
/** @name Message processing
*/
///@{
/**
* Process a list_win D-Bus request.
*/
static DBusHandlerResult
cdbus_process_list_win(session_t *ps, DBusMessage *msg attr_unused, DBusMessage *reply,
DBusError *err attr_unused) {
if (!cdbus_append_wids(reply, ps)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
/**
* Process a win_get D-Bus request.
*/
static DBusHandlerResult
cdbus_process_window_property_get(session_t *ps, DBusMessage *msg, cdbus_window_t wid,
DBusMessage *reply, DBusError *e) {
const char *target = NULL;
const char *interface = NULL;
if (reply == NULL) {
return DBUS_HANDLER_RESULT_HANDLED;
}
if (!dbus_message_get_args(msg, e, DBUS_TYPE_STRING, &interface, DBUS_TYPE_STRING,
&target, DBUS_TYPE_INVALID)) {
log_debug("Failed to parse argument of \"Get\" (%s).", e->message);
dbus_set_error_const(e, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (interface[0] != '\0' && strcmp(interface, PICOM_WINDOW_INTERFACE) != 0) {
dbus_set_error_const(e, DBUS_ERROR_UNKNOWN_INTERFACE, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
auto w = wm_find_managed(ps->wm, wid);
if (!w) {
log_debug("Window %#010x not found.", wid);
dbus_set_error(e, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
return DBUS_HANDLER_RESULT_HANDLED;
}
#define append(tgt, type, expr) \
if (!strcmp(#tgt, target)) { \
if (!cdbus_append_##type(reply, expr)) { \
return DBUS_HANDLER_RESULT_NEED_MEMORY; \
} \
return DBUS_HANDLER_RESULT_HANDLED; \
}
#define append_win_property(name, member, type) append(name, type, w->member)
append(Mapped, bool_variant, w->state == WSTATE_MAPPED);
append(Id, wid_variant, w->base.id);
append(Type, string_variant, WINTYPES[w->window_type].name);
append(RawFocused, bool_variant, win_is_focused_raw(w));
append_win_property(ClientWin, client_win, wid_variant);
append_win_property(Leader, leader, wid_variant);
append_win_property(Name, name, string_variant);
if (!strcmp("Next", target)) {
cdbus_window_t next_id = 0;
if (!list_node_is_last(wm_stack_end(ps->wm), &w->base.stack_neighbour)) {
next_id = list_entry(w->base.stack_neighbour.next, struct win,
stack_neighbour)
->id;
}
if (!cdbus_append_wid_variant(reply, next_id)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
#undef append_win_property
#undef append
log_debug(CDBUS_ERROR_BADTGT_S, target);
dbus_set_error(e, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult cdbus_process_reset(session_t *ps, DBusMessage *msg attr_unused,
DBusMessage *reply, DBusError *e attr_unused) {
// Reset the compositor
log_info("picom is resetting...");
ev_break(ps->loop, EVBREAK_ALL);
if (reply != NULL && !cdbus_append_boolean(reply, true)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult cdbus_process_repaint(session_t *ps, DBusMessage *msg attr_unused,
DBusMessage *reply, DBusError *e attr_unused) {
// Reset the compositor
force_repaint(ps);
if (reply != NULL && !cdbus_append_boolean(reply, true)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
/**
* Process a win_get D-Bus request.
*/
static DBusHandlerResult
cdbus_process_win_get(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
cdbus_window_t wid = XCB_NONE;
const char *target = NULL;
if (reply == NULL) {
return DBUS_HANDLER_RESULT_HANDLED;
}
if (!dbus_message_get_args(msg, err, CDBUS_TYPE_WINDOW, &wid, DBUS_TYPE_STRING,
&target, DBUS_TYPE_INVALID)) {
log_debug("Failed to parse argument of \"win_get\" (%s).", err->message);
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
auto w = wm_find_managed(ps->wm, wid);
if (!w) {
log_debug("Window %#010x not found.", wid);
dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
return DBUS_HANDLER_RESULT_HANDLED;
}
#define append(tgt, type, expr) \
if (strcmp(#tgt, target) == 0) { \
if (!cdbus_append_##type(reply, expr)) { \
return DBUS_HANDLER_RESULT_NEED_MEMORY; \
} \
return DBUS_HANDLER_RESULT_HANDLED; \
}
#define append_win_property(tgt, type) append(tgt, type, w->tgt)
if (!strcmp("next", target)) {
xcb_window_t next_id =
list_node_is_last(wm_stack_end(ps->wm), &w->base.stack_neighbour)
? 0
: list_entry(w->base.stack_neighbour.next, struct win, stack_neighbour)
->id;
if (!cdbus_append_wid(reply, next_id)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
append(id, boolean, w->base.id);
append(map_state, boolean, w->a.map_state);
append(wmwin, boolean, win_is_wmwin(w));
append(focused_raw, boolean, win_is_focused_raw(w));
append(opacity, double, animatable_get(&w->opacity));
append(left_width, int32, w->frame_extents.left);
append(right_width, int32, w->frame_extents.right);
append(top_width, int32, w->frame_extents.top);
append(bottom_width, int32, w->frame_extents.bottom);
append_win_property(mode, enum);
append_win_property(client_win, wid);
append_win_property(ever_damaged, boolean);
append_win_property(window_type, enum);
append_win_property(leader, wid);
append_win_property(fade_force, enum);
append_win_property(shadow_force, enum);
append_win_property(focused_force, enum);
append_win_property(invert_color_force, enum);
append_win_property(name, string);
append_win_property(class_instance, string);
append_win_property(class_general, string);
append_win_property(role, string);
append_win_property(opacity.target, double);
append_win_property(has_opacity_prop, boolean);
append_win_property(opacity_prop, uint32);
append_win_property(opacity_is_set, boolean);
append_win_property(opacity_set, double);
append_win_property(frame_opacity, double);
append_win_property(shadow, boolean);
append_win_property(invert_color, boolean);
append_win_property(blur_background, boolean);
#undef append_win_property
#undef append
log_debug(CDBUS_ERROR_BADTGT_S, target);
dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
return DBUS_HANDLER_RESULT_HANDLED;
}
/**
* Process a win_set D-Bus request.
*/
static DBusHandlerResult
cdbus_process_win_set(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
cdbus_window_t wid = XCB_NONE;
const char *target = NULL;
if (!dbus_message_get_args(msg, err, CDBUS_TYPE_WINDOW, &wid, DBUS_TYPE_STRING,
&target, DBUS_TYPE_INVALID)) {
log_debug("Failed to parse argument of \"win_set\" (%s).", err->message);
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
auto w = wm_find_managed(ps->wm, wid);
if (!w) {
log_debug("Window %#010x not found.", wid);
dbus_set_error(err, CDBUS_ERROR_BADWIN, CDBUS_ERROR_BADWIN_S, wid);
return DBUS_HANDLER_RESULT_HANDLED;
}
cdbus_enum_t val = UNSET;
if (!cdbus_msg_get_arg(msg, 2, CDBUS_TYPE_ENUM, &val)) {
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (!strcmp("shadow_force", target)) {
win_set_shadow_force(ps, w, val);
} else if (!strcmp("fade_force", target)) {
win_set_fade_force(w, val);
} else if (!strcmp("focused_force", target)) {
win_set_focused_force(ps, w, val);
} else if (!strcmp("invert_color_force", target)) {
win_set_invert_color_force(ps, w, val);
} else {
log_debug(CDBUS_ERROR_BADTGT_S, target);
dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (reply != NULL && !cdbus_append_boolean(reply, true)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
/**
* Process a find_win D-Bus request.
*/
static DBusHandlerResult
cdbus_process_find_win(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
if (reply == NULL) {
return DBUS_HANDLER_RESULT_HANDLED;
}
const char *target = NULL;
if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target)) {
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
xcb_window_t wid = XCB_NONE;
if (!strcmp("client", target)) {
// Find window by client window
cdbus_window_t client = XCB_NONE;
if (!cdbus_msg_get_arg(msg, 1, CDBUS_TYPE_WINDOW, &client)) {
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
auto w = wm_find_by_client(ps->wm, client);
if (w) {
wid = w->base.id;
}
} else if (!strcmp("focused", target)) {
// Find focused window
auto active_win = wm_active_win(ps->wm);
if (active_win && active_win->state != WSTATE_UNMAPPED) {
wid = active_win->base.id;
}
} else {
log_debug(CDBUS_ERROR_BADTGT_S, target);
dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
return DBUS_HANDLER_RESULT_HANDLED;
}
if (reply != NULL && !cdbus_append_wid(reply, wid)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}
/**
* Process a opts_get D-Bus request.
*/
static DBusHandlerResult
cdbus_process_opts_get(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
if (reply == NULL) {
return DBUS_HANDLER_RESULT_HANDLED;
}
const char *target = NULL;
if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target)) {
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
assert(ps->o.backend < sizeof(BACKEND_STRS) / sizeof(BACKEND_STRS[0]));
#define append(tgt, type, ret) \
if (!strcmp(#tgt, target)) { \
if (reply != NULL && !cdbus_append_##type(reply, ret)) { \
return DBUS_HANDLER_RESULT_NEED_MEMORY; \
} \
return DBUS_HANDLER_RESULT_HANDLED; \
}
#define append_session_option(tgt, type) append(tgt, type, ps->o.tgt)
append(version, string, PICOM_VERSION);
append(pid, int32, getpid());
append(display, string, DisplayString(ps->c.dpy));
append(config_file, string, "Unknown");
append(paint_on_overlay, boolean, ps->overlay != XCB_NONE);
append(paint_on_overlay_id, uint32, ps->overlay); // Sending ID of the X
// composite overlay
// window
append(unredir_if_possible_delay, int32, (int32_t)ps->o.unredir_if_possible_delay);
append(refresh_rate, int32, 0);
append(sw_opti, boolean, false);
append(backend, string, BACKEND_STRS[ps->o.backend]);
append_session_option(unredir_if_possible, boolean);
append_session_option(write_pid_path, string);
append_session_option(mark_wmwin_focused, boolean);
append_session_option(mark_ovredir_focused, boolean);
append_session_option(detect_rounded_corners, boolean);
append_session_option(redirected_force, enum);
append_session_option(stoppaint_force, enum);
append_session_option(logpath, string);
append_session_option(vsync, boolean);
append_session_option(shadow_red, double);
append_session_option(shadow_green, double);
append_session_option(shadow_blue, double);
append_session_option(shadow_radius, int32);
append_session_option(shadow_offset_x, int32);
append_session_option(shadow_offset_y, int32);
append_session_option(shadow_opacity, double);
append_session_option(crop_shadow_to_monitor, boolean);
append_session_option(fade_delta, int32);
append_session_option(fade_in_step, double);
append_session_option(fade_out_step, double);
append_session_option(no_fading_openclose, boolean);
append_session_option(blur_method, boolean);
append_session_option(blur_background_frame, boolean);
append_session_option(blur_background_fixed, boolean);
append_session_option(inactive_dim, double);
append_session_option(inactive_dim_fixed, boolean);
append_session_option(max_brightness, double);
append_session_option(use_ewmh_active_win, boolean);
append_session_option(detect_transient, boolean);
append_session_option(detect_client_leader, boolean);
append_session_option(use_damage, boolean);
#ifdef CONFIG_OPENGL
append_session_option(glx_no_stencil, boolean);
append_session_option(glx_no_rebind_pixmap, boolean);
#endif
#undef append_session_option
#undef append
log_debug(CDBUS_ERROR_BADTGT_S, target);
dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
return DBUS_HANDLER_RESULT_HANDLED;
}
// XXX Remove this after header clean up
void queue_redraw(session_t *ps);
/**
* Process a opts_set D-Bus request.
*/
static DBusHandlerResult
cdbus_process_opts_set(session_t *ps, DBusMessage *msg, DBusMessage *reply, DBusError *err) {
const char *target = NULL;
if (!cdbus_msg_get_arg(msg, 0, DBUS_TYPE_STRING, &target)) {
log_error("Failed to parse argument of \"opts_set\" (%s).", err->message);
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL);
return DBUS_HANDLER_RESULT_HANDLED;
}
#define get_msg_arg(type, val) \
if (!cdbus_msg_get_arg(msg, 1, DBUS_TYPE_##type, &(val))) { \
dbus_set_error_const(err, DBUS_ERROR_INVALID_ARGS, NULL); \
return DBUS_HANDLER_RESULT_HANDLED; \
};
#define opts_set_do(tgt, dbus_type, type, expr) \
if (strcmp(#tgt, target) == 0) { \
type val; \
get_msg_arg(dbus_type, val); \
ps->o.tgt = expr; \
goto cdbus_process_opts_set_success; \
}
if (!strcmp("clear_shadow", target) || !strcmp("track_focus", target)) {
goto cdbus_process_opts_set_success;
}
opts_set_do(fade_delta, INT32, int32_t, max2(val, 1));
opts_set_do(fade_in_step, DOUBLE, double, normalize_d(val));
opts_set_do(fade_out_step, DOUBLE, double, normalize_d(val));
opts_set_do(no_fading_openclose, BOOLEAN, bool, val);
opts_set_do(stoppaint_force, UINT32, cdbus_enum_t, val);
if (!strcmp("unredir_if_possible", target)) {
dbus_bool_t val = FALSE;
get_msg_arg(BOOLEAN, val);
if (ps->o.unredir_if_possible != val) {
ps->o.unredir_if_possible = val;
queue_redraw(ps);
}
goto cdbus_process_opts_set_success;
}
if (!strcmp("redirected_force", target)) {
cdbus_enum_t val = UNSET;
get_msg_arg(UINT32, val);
if (ps->o.redirected_force != val) {
ps->o.redirected_force = val;
force_repaint(ps);
}
goto cdbus_process_opts_set_success;
}
#undef get_msg_arg
log_error(CDBUS_ERROR_BADTGT_S, target);
dbus_set_error(err, CDBUS_ERROR_BADTGT, CDBUS_ERROR_BADTGT_S, target);
return DBUS_HANDLER_RESULT_HANDLED;
cdbus_process_opts_set_success:
if (reply != NULL && !cdbus_append_boolean(reply, true)) {
return DBUS_HANDLER_RESULT_NEED_MEMORY;
}
return DBUS_HANDLER_RESULT_HANDLED;
}