forked from emacs-ng/emacs-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbusbind.c
More file actions
2067 lines (1795 loc) · 63 KB
/
dbusbind.c
File metadata and controls
2067 lines (1795 loc) · 63 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
/* Elisp bindings for D-Bus.
Copyright (C) 2007-2025 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#ifdef HAVE_DBUS
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>
#include "lisp.h"
#include "termhooks.h"
#include "keyboard.h"
#include "pdumper.h"
#include "process.h"
#ifndef DBUS_NUM_MESSAGE_TYPES
#define DBUS_NUM_MESSAGE_TYPES 5
#endif
/* Some platforms define the symbol "interface", but we want to use it
* as a variable name below. */
#ifdef interface
#undef interface
#endif
/* Alist of D-Bus buses we are polling for messages.
The key is the symbol or string of the bus, and the value is the
connection address. For every bus, just one connection is counted.
If there shall be a second connection to the same bus, a different
symbol or string for the bus must be chosen. On Lisp level, a bus
stands for the associated connection. */
static Lisp_Object xd_registered_buses;
/* Whether we are reading a D-Bus event. */
static bool xd_in_read_queued_messages = 0;
/* We use "xd_" and "XD_" as prefix for all internal symbols, because
we don't want to poison other namespaces with "dbus_". */
/* Raise a signal. If we are reading events, we cannot signal; we
throw to xd_read_queued_messages then. */
#define XD_SIGNAL1(arg) \
do { \
if (xd_in_read_queued_messages) \
Fthrow (Qdbus_error, Qnil); \
else \
xsignal1 (Qdbus_error, arg); \
} while (0)
#define XD_SIGNAL2(arg1, arg2) \
do { \
if (xd_in_read_queued_messages) \
Fthrow (Qdbus_error, Qnil); \
else \
xsignal2 (Qdbus_error, arg1, arg2); \
} while (0)
#define XD_SIGNAL3(arg1, arg2, arg3) \
do { \
if (xd_in_read_queued_messages) \
Fthrow (Qdbus_error, Qnil); \
else \
xsignal3 (Qdbus_error, arg1, arg2, arg3); \
} while (0)
/* Raise a Lisp error from a D-Bus ERROR. */
#define XD_ERROR(error) \
do { \
/* Remove the trailing newline. */ \
char const *mess = error.message; \
char const *nl = strchr (mess, '\n'); \
Lisp_Object err = make_string (mess, nl ? nl - mess : strlen (mess)); \
dbus_error_free (&error); \
XD_SIGNAL1 (err); \
} while (0)
/* Macros for debugging. In order to enable them, build with
"make MYCPPFLAGS='-DDBUS_DEBUG'". */
#ifdef DBUS_DEBUG
#define XD_DEBUG_MESSAGE(...) \
do { \
char s[1024]; \
snprintf (s, sizeof s, __VA_ARGS__); \
if (!noninteractive) \
printf ("%s: %s\n", __func__, s); \
message ("%s: %s", __func__, s); \
} while (0)
#define XD_DEBUG_VALID_LISP_OBJECT_P(object) \
do { \
if (!valid_lisp_object_p (object)) \
{ \
XD_DEBUG_MESSAGE ("%d Assertion failure", __LINE__); \
XD_SIGNAL1 (build_string ("Assertion failure")); \
} \
} while (0)
#else /* !DBUS_DEBUG */
# define XD_DEBUG_MESSAGE(...) \
do { \
if (!NILP (Vdbus_debug)) \
{ \
char s[1024]; \
snprintf (s, sizeof s, __VA_ARGS__); \
message ("%s: %s", __func__, s); \
} \
} while (0)
# define XD_DEBUG_VALID_LISP_OBJECT_P(object)
#endif
/* Check whether TYPE is a basic DBusType. */
#ifdef HAVE_DBUS_TYPE_IS_VALID
#define XD_BASIC_DBUS_TYPE(type) \
(dbus_type_is_valid (type) && dbus_type_is_basic (type))
#else
#ifdef DBUS_TYPE_UNIX_FD
#define XD_BASIC_DBUS_TYPE(type) \
((type == DBUS_TYPE_BYTE) \
|| (type == DBUS_TYPE_BOOLEAN) \
|| (type == DBUS_TYPE_INT16) \
|| (type == DBUS_TYPE_UINT16) \
|| (type == DBUS_TYPE_INT32) \
|| (type == DBUS_TYPE_UINT32) \
|| (type == DBUS_TYPE_INT64) \
|| (type == DBUS_TYPE_UINT64) \
|| (type == DBUS_TYPE_DOUBLE) \
|| (type == DBUS_TYPE_STRING) \
|| (type == DBUS_TYPE_OBJECT_PATH) \
|| (type == DBUS_TYPE_SIGNATURE) \
|| (type == DBUS_TYPE_UNIX_FD))
#else
#define XD_BASIC_DBUS_TYPE(type) \
((type == DBUS_TYPE_BYTE) \
|| (type == DBUS_TYPE_BOOLEAN) \
|| (type == DBUS_TYPE_INT16) \
|| (type == DBUS_TYPE_UINT16) \
|| (type == DBUS_TYPE_INT32) \
|| (type == DBUS_TYPE_UINT32) \
|| (type == DBUS_TYPE_INT64) \
|| (type == DBUS_TYPE_UINT64) \
|| (type == DBUS_TYPE_DOUBLE) \
|| (type == DBUS_TYPE_STRING) \
|| (type == DBUS_TYPE_OBJECT_PATH) \
|| (type == DBUS_TYPE_SIGNATURE))
#endif
#endif
/* This was a macro. On Solaris 2.11 it was said to compile for
hours, when optimization is enabled. So we have transferred it into
a function. */
/* Determine the DBusType of a given Lisp symbol. OBJECT must be one
of the predefined D-Bus type symbols. */
static int
xd_symbol_to_dbus_type (Lisp_Object object)
{
return
(EQ (object, QCbyte) ? DBUS_TYPE_BYTE
: EQ (object, QCboolean) ? DBUS_TYPE_BOOLEAN
: EQ (object, QCint16) ? DBUS_TYPE_INT16
: EQ (object, QCuint16) ? DBUS_TYPE_UINT16
: EQ (object, QCint32) ? DBUS_TYPE_INT32
: EQ (object, QCuint32) ? DBUS_TYPE_UINT32
: EQ (object, QCint64) ? DBUS_TYPE_INT64
: EQ (object, QCuint64) ? DBUS_TYPE_UINT64
: EQ (object, QCdouble) ? DBUS_TYPE_DOUBLE
: EQ (object, QCstring) ? DBUS_TYPE_STRING
: EQ (object, QCobject_path) ? DBUS_TYPE_OBJECT_PATH
: EQ (object, QCsignature) ? DBUS_TYPE_SIGNATURE
#ifdef DBUS_TYPE_UNIX_FD
: EQ (object, QCunix_fd) ? DBUS_TYPE_UNIX_FD
#endif
: EQ (object, QCarray) ? DBUS_TYPE_ARRAY
: EQ (object, QCvariant) ? DBUS_TYPE_VARIANT
: EQ (object, QCstruct) ? DBUS_TYPE_STRUCT
: EQ (object, QCdict_entry) ? DBUS_TYPE_DICT_ENTRY
: DBUS_TYPE_INVALID);
}
/* Determine the Lisp symbol of DBusType. */
static Lisp_Object
xd_dbus_type_to_symbol (int type)
{
return
(type == DBUS_TYPE_BYTE) ? QCbyte
: (type == DBUS_TYPE_BOOLEAN) ? QCboolean
: (type == DBUS_TYPE_INT16) ? QCint16
: (type == DBUS_TYPE_UINT16) ? QCuint16
: (type == DBUS_TYPE_INT32) ? QCint32
: (type == DBUS_TYPE_UINT32) ? QCuint32
: (type == DBUS_TYPE_INT64) ? QCint64
: (type == DBUS_TYPE_UINT64) ? QCuint64
: (type == DBUS_TYPE_DOUBLE) ? QCdouble
: (type == DBUS_TYPE_STRING) ? QCstring
: (type == DBUS_TYPE_OBJECT_PATH) ? QCobject_path
: (type == DBUS_TYPE_SIGNATURE) ? QCsignature
#ifdef DBUS_TYPE_UNIX_FD
: (type == DBUS_TYPE_UNIX_FD) ? QCunix_fd
#endif
: (type == DBUS_TYPE_ARRAY) ? QCarray
: (type == DBUS_TYPE_VARIANT) ? QCvariant
: (type == DBUS_TYPE_STRUCT) ? QCstruct
: (type == DBUS_TYPE_DICT_ENTRY) ? QCdict_entry
: Qnil;
}
#define XD_KEYWORDP(object) !NILP (Fkeywordp (object))
/* Check whether a Lisp symbol is a predefined D-Bus type symbol. */
#define XD_DBUS_TYPE_P(object) \
XD_KEYWORDP (object) && \
((xd_symbol_to_dbus_type (object) != DBUS_TYPE_INVALID))
/* Determine the DBusType of a given Lisp OBJECT. It is used to
convert Lisp objects, being arguments of `dbus-call-method' or
`dbus-send-signal', into corresponding C values appended as
arguments to a D-Bus message. */
#define XD_OBJECT_TO_DBUS_TYPE(object) \
((EQ (object, Qt) || NILP (object)) ? DBUS_TYPE_BOOLEAN \
: (FIXNATP (object)) ? DBUS_TYPE_UINT32 \
: (FIXNUMP (object)) ? DBUS_TYPE_INT32 \
: (FLOATP (object)) ? DBUS_TYPE_DOUBLE \
: (STRINGP (object)) ? DBUS_TYPE_STRING \
: (XD_DBUS_TYPE_P (object)) ? xd_symbol_to_dbus_type (object) \
: (CONSP (object)) \
? ((XD_DBUS_TYPE_P (XCAR (object))) \
? ((XD_BASIC_DBUS_TYPE (xd_symbol_to_dbus_type (XCAR (object)))) \
? DBUS_TYPE_ARRAY \
: xd_symbol_to_dbus_type (XCAR (object))) \
: DBUS_TYPE_ARRAY) \
: DBUS_TYPE_INVALID)
/* Return a list pointer which does not have a Lisp symbol as car. */
#define XD_NEXT_VALUE(object) \
((XD_DBUS_TYPE_P (CAR_SAFE (object))) ? CDR_SAFE (object) : object)
/* Transform the message type to its string representation for debug
messages. */
#define XD_MESSAGE_TYPE_TO_STRING(mtype) \
((mtype == DBUS_MESSAGE_TYPE_INVALID) \
? "DBUS_MESSAGE_TYPE_INVALID" \
: (mtype == DBUS_MESSAGE_TYPE_METHOD_CALL) \
? "DBUS_MESSAGE_TYPE_METHOD_CALL" \
: (mtype == DBUS_MESSAGE_TYPE_METHOD_RETURN) \
? "DBUS_MESSAGE_TYPE_METHOD_RETURN" \
: (mtype == DBUS_MESSAGE_TYPE_ERROR) \
? "DBUS_MESSAGE_TYPE_ERROR" \
: "DBUS_MESSAGE_TYPE_SIGNAL")
/* Transform the object to its string representation for debug
messages. */
static char *
XD_OBJECT_TO_STRING (Lisp_Object object)
{
AUTO_STRING (format, "%s");
return SSDATA (CALLN (Fformat, format, object));
}
#define XD_DBUS_VALIDATE_BUS_ADDRESS(bus) \
do { \
char const *session_bus_address = egetenv ("DBUS_SESSION_BUS_ADDRESS"); \
if (STRINGP (bus)) \
{ \
DBusAddressEntry **entries; \
int len; \
DBusError derror; \
dbus_error_init (&derror); \
if (!dbus_parse_address (SSDATA (bus), &entries, &len, &derror)) \
XD_ERROR (derror); \
/* Cleanup. */ \
dbus_error_free (&derror); \
dbus_address_entries_free (entries); \
/* Canonicalize session bus address. */ \
if ((session_bus_address != NULL) \
&& (!NILP (Fstring_equal \
(bus, build_string (session_bus_address))))) \
bus = QCsession; \
} \
\
else \
{ \
CHECK_SYMBOL (bus); \
if (!(EQ (bus, QCsystem) || EQ (bus, QCsession) \
|| EQ (bus, QCsystem_private) \
|| EQ (bus, QCsession_private))) \
XD_SIGNAL2 (build_string ("Wrong bus name"), bus); \
/* We do not want to have an autolaunch for the session bus. */ \
if ((EQ (bus, QCsession) || EQ (bus, QCsession_private)) \
&& session_bus_address == NULL) \
XD_SIGNAL2 (build_string ("No connection to bus"), bus); \
} \
} while (0)
#if (HAVE_DBUS_VALIDATE_BUS_NAME || HAVE_DBUS_VALIDATE_PATH \
|| HAVE_DBUS_VALIDATE_INTERFACE || HAVE_DBUS_VALIDATE_MEMBER)
#define XD_DBUS_VALIDATE_OBJECT(object, func) \
do { \
if (!NILP (object)) \
{ \
DBusError derror; \
CHECK_STRING (object); \
dbus_error_init (&derror); \
if (!func (SSDATA (object), &derror)) \
XD_ERROR (derror); \
/* Cleanup. */ \
dbus_error_free (&derror); \
} \
} while (0)
#endif
#if HAVE_DBUS_VALIDATE_BUS_NAME
#define XD_DBUS_VALIDATE_BUS_NAME(bus_name) \
XD_DBUS_VALIDATE_OBJECT(bus_name, dbus_validate_bus_name);
#else
#define XD_DBUS_VALIDATE_BUS_NAME(bus_name) \
if (!NILP (bus_name)) CHECK_STRING (bus_name);
#endif
#if HAVE_DBUS_VALIDATE_PATH
#define XD_DBUS_VALIDATE_PATH(path) \
XD_DBUS_VALIDATE_OBJECT(path, dbus_validate_path);
#else
#define XD_DBUS_VALIDATE_PATH(path) \
if (!NILP (path)) CHECK_STRING (path);
#endif
#if HAVE_DBUS_VALIDATE_INTERFACE
#define XD_DBUS_VALIDATE_INTERFACE(interface) \
XD_DBUS_VALIDATE_OBJECT(interface, dbus_validate_interface);
#else
#define XD_DBUS_VALIDATE_INTERFACE(interface) \
if (!NILP (interface)) CHECK_STRING (interface);
#endif
#if HAVE_DBUS_VALIDATE_MEMBER
#define XD_DBUS_VALIDATE_MEMBER(member) \
XD_DBUS_VALIDATE_OBJECT(member, dbus_validate_member);
#else
#define XD_DBUS_VALIDATE_MEMBER(member) \
if (!NILP (member)) CHECK_STRING (member);
#endif
/* Append to SIGNATURE a copy of X, making sure SIGNATURE does
not become too long. */
static void
xd_signature_cat (char *signature, char const *x)
{
ptrdiff_t siglen = strlen (signature);
ptrdiff_t xlen = strlen (x);
if (DBUS_MAXIMUM_SIGNATURE_LENGTH - xlen <= siglen)
string_overflow ();
strcpy (signature + siglen, x);
}
/* Compute SIGNATURE of OBJECT. It must have a form that it can be
used in dbus_message_iter_open_container. DTYPE is the DBusType
the object is related to. It is passed as argument, because it
cannot be detected in basic type objects, when they are preceded by
a type symbol. PARENT_TYPE is the DBusType of a container this
signature is embedded, or DBUS_TYPE_INVALID. It is needed for the
check that DBUS_TYPE_DICT_ENTRY occurs only as array element. */
static void
xd_signature (char *signature, int dtype, int parent_type, Lisp_Object object)
{
int subtype;
Lisp_Object elt;
char const *subsig;
char x[DBUS_MAXIMUM_SIGNATURE_LENGTH];
elt = object;
switch (dtype)
{
case DBUS_TYPE_BYTE:
case DBUS_TYPE_UINT16:
CHECK_FIXNAT (object);
sprintf (signature, "%c", dtype);
break;
case DBUS_TYPE_BOOLEAN:
/* There must be an argument. */
if (EQ (QCboolean, object))
wrong_type_argument (Qbooleanp, object);
sprintf (signature, "%c", dtype);
break;
case DBUS_TYPE_INT16:
CHECK_FIXNUM (object);
sprintf (signature, "%c", dtype);
break;
case DBUS_TYPE_UINT32:
case DBUS_TYPE_UINT64:
#ifdef DBUS_TYPE_UNIX_FD
case DBUS_TYPE_UNIX_FD:
#endif
case DBUS_TYPE_INT32:
case DBUS_TYPE_INT64:
case DBUS_TYPE_DOUBLE:
CHECK_NUMBER (object);
sprintf (signature, "%c", dtype);
break;
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
/* We don't check the syntax of signature. This will be done by
libdbus. */
if (dtype == DBUS_TYPE_OBJECT_PATH)
XD_DBUS_VALIDATE_PATH (object)
else
CHECK_STRING (object);
sprintf (signature, "%c", dtype);
break;
case DBUS_TYPE_ARRAY:
/* Check that all list elements have the same D-Bus type. For
complex element types, we just check the container type, not
the whole element's signature. */
CHECK_CONS (object);
/* Type symbol is optional. */
if (EQ (QCarray, XCAR (elt)))
elt = XD_NEXT_VALUE (elt);
/* If the array is empty, DBUS_TYPE_STRING is the default
element type. */
if (NILP (elt))
{
subtype = DBUS_TYPE_STRING;
subsig = DBUS_TYPE_STRING_AS_STRING;
}
else
{
subtype = XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt));
xd_signature (x, subtype, dtype, CAR_SAFE (XD_NEXT_VALUE (elt)));
subsig = x;
}
/* If the element type is DBUS_TYPE_SIGNATURE, and this is the
only element, the value of this element is used as the
array's element signature. */
if (subtype == DBUS_TYPE_SIGNATURE)
{
Lisp_Object elt1 = XD_NEXT_VALUE (elt);
if (CONSP (elt1) && STRINGP (XCAR (elt1)) && NILP (XCDR (elt1)))
{
subsig = SSDATA (XCAR (elt1));
elt = Qnil;
}
}
while (!NILP (elt))
{
char x[DBUS_MAXIMUM_SIGNATURE_LENGTH];
subtype = XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt));
xd_signature (x, subtype, dtype, CAR_SAFE (XD_NEXT_VALUE (elt)));
if (strcmp (subsig, x) != 0)
wrong_type_argument (QD_Bus, CAR_SAFE (elt));
elt = CDR_SAFE (XD_NEXT_VALUE (elt));
}
signature[0] = dtype;
signature[1] = '\0';
xd_signature_cat (signature, subsig);
break;
case DBUS_TYPE_VARIANT:
/* Check that there is exactly one list element. */
CHECK_CONS (object);
elt = XD_NEXT_VALUE (elt);
CHECK_CONS (elt);
subtype = XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt));
xd_signature (x, subtype, dtype, CAR_SAFE (XD_NEXT_VALUE (elt)));
if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt))))
wrong_type_argument (QD_Bus, CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt))));
sprintf (signature, "%c", dtype);
break;
case DBUS_TYPE_STRUCT:
/* A struct list might contain any (but zero) number of elements
with different types. No further check needed. */
CHECK_CONS (object);
elt = XD_NEXT_VALUE (elt);
CHECK_CONS (elt);
/* Compose the signature from the elements. It is enclosed by
parentheses. */
sprintf (signature, "%c", DBUS_STRUCT_BEGIN_CHAR );
while (!NILP (elt))
{
subtype = XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt));
xd_signature (x, subtype, dtype, CAR_SAFE (XD_NEXT_VALUE (elt)));
xd_signature_cat (signature, x);
elt = CDR_SAFE (XD_NEXT_VALUE (elt));
}
xd_signature_cat (signature, DBUS_STRUCT_END_CHAR_AS_STRING);
break;
case DBUS_TYPE_DICT_ENTRY:
/* Check that there are exactly two list elements, and the first
one is of basic type. The dictionary entry itself must be an
element of an array. */
CHECK_CONS (object);
/* Check the parent object type. */
if (parent_type != DBUS_TYPE_ARRAY)
wrong_type_argument (QD_Bus, object);
/* Compose the signature from the elements. It is enclosed by
curly braces. */
sprintf (signature, "%c", DBUS_DICT_ENTRY_BEGIN_CHAR);
/* First element. */
elt = XD_NEXT_VALUE (elt);
CHECK_CONS (elt);
subtype = XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt));
xd_signature (x, subtype, dtype, CAR_SAFE (XD_NEXT_VALUE (elt)));
xd_signature_cat (signature, x);
if (!XD_BASIC_DBUS_TYPE (subtype))
wrong_type_argument (QD_Bus, CAR_SAFE (XD_NEXT_VALUE (elt)));
/* Second element. */
elt = CDR_SAFE (XD_NEXT_VALUE (elt));
CHECK_CONS (elt);
subtype = XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt));
xd_signature (x, subtype, dtype, CAR_SAFE (XD_NEXT_VALUE (elt)));
xd_signature_cat (signature, x);
if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt))))
wrong_type_argument (QD_Bus, CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt))));
/* Closing signature. */
xd_signature_cat (signature, DBUS_DICT_ENTRY_END_CHAR_AS_STRING);
break;
default:
wrong_type_argument (QD_Bus, object);
}
XD_DEBUG_MESSAGE ("%s", signature);
}
/* Convert X to a signed integer with bounds LO and HI. */
static intmax_t
xd_extract_signed (Lisp_Object x, intmax_t lo, intmax_t hi)
{
CHECK_NUMBER (x);
if (INTEGERP (x))
{
intmax_t i;
if (integer_to_intmax (x, &i) && lo <= i && i <= hi)
return i;
}
else
{
double d = XFLOAT_DATA (x);
if (lo <= d && d < 1.0 + hi)
{
intmax_t n = d;
if (n == d)
return n;
}
}
if (xd_in_read_queued_messages)
Fthrow (Qdbus_error, Qnil);
else
args_out_of_range_3 (x, INT_TO_INTEGER (lo), INT_TO_INTEGER (hi));
}
/* Convert X to an unsigned integer with bounds 0 and HI. */
static uintmax_t
xd_extract_unsigned (Lisp_Object x, uintmax_t hi)
{
CHECK_NUMBER (x);
if (INTEGERP (x))
{
uintmax_t i;
if (integer_to_uintmax (x, &i) && i <= hi)
return i;
}
else
{
double d = XFLOAT_DATA (x);
if (0 <= d && d < 1.0 + hi)
{
uintmax_t n = d;
if (n == d)
return n;
}
}
if (xd_in_read_queued_messages)
Fthrow (Qdbus_error, Qnil);
else
args_out_of_range_3 (x, make_fixnum (0), INT_TO_INTEGER (hi));
}
/* Append C value, extracted from Lisp OBJECT, to iteration ITER.
DTYPE must be a valid DBusType. It is used to convert Lisp
objects, being arguments of `dbus-call-method' or
`dbus-send-signal', into corresponding C values appended as
arguments to a D-Bus message. */
static void
xd_append_arg (int dtype, Lisp_Object object, DBusMessageIter *iter)
{
char signature[DBUS_MAXIMUM_SIGNATURE_LENGTH];
DBusMessageIter subiter;
if (XD_BASIC_DBUS_TYPE (dtype))
switch (dtype)
{
case DBUS_TYPE_BYTE:
CHECK_FIXNAT (object);
{
unsigned char val = XFIXNAT (object) & 0xFF;
XD_DEBUG_MESSAGE ("%c %u", dtype, val);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_BOOLEAN:
/* There must be an argument. */
if (EQ (QCboolean, object))
wrong_type_argument (Qbooleanp, object);
{
dbus_bool_t val = (NILP (object)) ? FALSE : TRUE;
XD_DEBUG_MESSAGE ("%c %s", dtype, (val == FALSE) ? "false" : "true");
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_INT16:
{
dbus_int16_t val =
xd_extract_signed (object,
TYPE_MINIMUM (dbus_int16_t),
TYPE_MAXIMUM (dbus_int16_t));
int pval = val;
XD_DEBUG_MESSAGE ("%c %d", dtype, pval);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_UINT16:
{
dbus_uint16_t val =
xd_extract_unsigned (object,
TYPE_MAXIMUM (dbus_uint16_t));
unsigned int pval = val;
XD_DEBUG_MESSAGE ("%c %u", dtype, pval);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_INT32:
{
dbus_int32_t val =
xd_extract_signed (object,
TYPE_MINIMUM (dbus_int32_t),
TYPE_MAXIMUM (dbus_int32_t));
int pval = val;
XD_DEBUG_MESSAGE ("%c %d", dtype, pval);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_UINT32:
#ifdef DBUS_TYPE_UNIX_FD
case DBUS_TYPE_UNIX_FD:
#endif
{
dbus_uint32_t val =
xd_extract_unsigned (object,
TYPE_MAXIMUM (dbus_uint32_t));
unsigned int pval = val;
XD_DEBUG_MESSAGE ("%c %u", dtype, pval);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_INT64:
{
dbus_int64_t val =
xd_extract_signed (object,
TYPE_MINIMUM (dbus_int64_t),
TYPE_MAXIMUM (dbus_int64_t));
intmax_t pval = val;
XD_DEBUG_MESSAGE ("%c %"PRIdMAX, dtype, pval);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_UINT64:
{
dbus_uint64_t val =
xd_extract_unsigned (object,
TYPE_MAXIMUM (dbus_uint64_t));
uintmax_t pval = val;
XD_DEBUG_MESSAGE ("%c %"PRIuMAX, dtype, pval);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_DOUBLE:
{
double val = extract_float (object);
XD_DEBUG_MESSAGE ("%c %f", dtype, val);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
/* We don't check the syntax of signature. This will be done
by libdbus. */
if (dtype == DBUS_TYPE_OBJECT_PATH)
XD_DBUS_VALIDATE_PATH (object)
else
CHECK_STRING (object);
{
/* We need to send a valid UTF-8 string. We could encode `object'
but by not encoding it, we guarantee it's valid utf-8, even if
it contains eight-bit-bytes. Of course, you can still send
manually-crafted junk by passing a unibyte string. */
char *val = SSDATA (object);
XD_DEBUG_MESSAGE ("%c %s", dtype, val);
if (!dbus_message_iter_append_basic (iter, dtype, &val))
XD_SIGNAL2 (build_string ("Unable to append argument"), object);
return;
}
}
else /* Compound types. */
{
/* All compound types except array have a type symbol. For
array, it is optional. Skip it. */
if (!XD_BASIC_DBUS_TYPE (XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object))))
object = XD_NEXT_VALUE (object);
/* Open new subiteration. */
switch (dtype)
{
case DBUS_TYPE_ARRAY:
/* An array has only elements of the same type. So it is
sufficient to check the first element's signature
only. */
if (NILP (object))
/* If the array is empty, DBUS_TYPE_STRING is the default
element type. */
strcpy (signature, DBUS_TYPE_STRING_AS_STRING);
else
{
/* If the element type is DBUS_TYPE_SIGNATURE, and this is
the only element, the value of this element is used as
the array's element signature. */
if (CONSP (object) && (XD_OBJECT_TO_DBUS_TYPE (XCAR (object))
== DBUS_TYPE_SIGNATURE))
{
Lisp_Object val = XD_NEXT_VALUE (object);
if (CONSP (val) && STRINGP (XCAR (val)) && NILP (XCDR (val))
&& SBYTES (XCAR (val)) < DBUS_MAXIMUM_SIGNATURE_LENGTH)
{
lispstpcpy (signature, XCAR (val));
object = Qnil;
}
}
if (!NILP (object))
xd_signature (signature,
XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object)),
dtype, CAR_SAFE (XD_NEXT_VALUE (object)));
}
XD_DEBUG_MESSAGE ("%c %s %s", dtype, signature,
XD_OBJECT_TO_STRING (object));
if (!dbus_message_iter_open_container (iter, dtype,
signature, &subiter))
XD_SIGNAL3 (build_string ("Cannot open container"),
make_fixnum (dtype), build_string (signature));
break;
case DBUS_TYPE_VARIANT:
/* A variant has just one element. */
xd_signature (signature, XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object)),
dtype, CAR_SAFE (XD_NEXT_VALUE (object)));
XD_DEBUG_MESSAGE ("%c %s %s", dtype, signature,
XD_OBJECT_TO_STRING (object));
if (!dbus_message_iter_open_container (iter, dtype,
signature, &subiter))
XD_SIGNAL3 (build_string ("Cannot open container"),
make_fixnum (dtype), build_string (signature));
break;
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_DICT_ENTRY:
/* These containers do not require a signature. */
XD_DEBUG_MESSAGE ("%c %s", dtype, XD_OBJECT_TO_STRING (object));
if (!dbus_message_iter_open_container (iter, dtype, NULL, &subiter))
XD_SIGNAL2 (build_string ("Cannot open container"),
make_fixnum (dtype));
break;
}
/* Loop over list elements. */
while (!NILP (object))
{
dtype = XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object));
object = XD_NEXT_VALUE (object);
xd_append_arg (dtype, CAR_SAFE (object), &subiter);
object = CDR_SAFE (object);
}
/* Close the subiteration. */
if (!dbus_message_iter_close_container (iter, &subiter))
XD_SIGNAL2 (build_string ("Cannot close container"),
make_fixnum (dtype));
}
}
/* Retrieve C value from a DBusMessageIter structure ITER, and return
a converted Lisp object. The type DTYPE of the argument of the
D-Bus message must be a valid DBusType. Compound D-Bus types
result always in a Lisp list. */
static Lisp_Object
xd_retrieve_arg (int dtype, DBusMessageIter *iter)
{
switch (dtype)
{
case DBUS_TYPE_BYTE:
{
unsigned int val;
dbus_message_iter_get_basic (iter, &val);
val = val & 0xFF;
XD_DEBUG_MESSAGE ("%c %u", dtype, val);
return list2 (xd_dbus_type_to_symbol (dtype), make_fixnum (val));
}
case DBUS_TYPE_BOOLEAN:
{
dbus_bool_t val;
dbus_message_iter_get_basic (iter, &val);
XD_DEBUG_MESSAGE ("%c %s", dtype, (val == FALSE) ? "false" : "true");
return list2 (xd_dbus_type_to_symbol (dtype),
(val == FALSE) ? Qnil : Qt);
}
case DBUS_TYPE_INT16:
{
dbus_int16_t val;
int pval;
dbus_message_iter_get_basic (iter, &val);
pval = val;
XD_DEBUG_MESSAGE ("%c %d", dtype, pval);
return list2 (xd_dbus_type_to_symbol (dtype), make_fixnum (val));
}
case DBUS_TYPE_UINT16:
{
dbus_uint16_t val;
int pval;
dbus_message_iter_get_basic (iter, &val);
pval = val;
XD_DEBUG_MESSAGE ("%c %d", dtype, pval);
return list2 (xd_dbus_type_to_symbol (dtype), make_fixnum (val));
}
case DBUS_TYPE_INT32:
{
dbus_int32_t val;
int pval;
dbus_message_iter_get_basic (iter, &val);
pval = val;
XD_DEBUG_MESSAGE ("%c %d", dtype, pval);
return list2 (xd_dbus_type_to_symbol (dtype), INT_TO_INTEGER (val));
}
case DBUS_TYPE_UINT32:
#ifdef DBUS_TYPE_UNIX_FD
case DBUS_TYPE_UNIX_FD:
#endif
{
dbus_uint32_t val;
unsigned int pval;
dbus_message_iter_get_basic (iter, &val);
pval = val;
XD_DEBUG_MESSAGE ("%c %u", dtype, pval);
return list2 (xd_dbus_type_to_symbol (dtype), INT_TO_INTEGER (val));
}
case DBUS_TYPE_INT64:
{
dbus_int64_t val;
dbus_message_iter_get_basic (iter, &val);
intmax_t pval = val;
XD_DEBUG_MESSAGE ("%c %"PRIdMAX, dtype, pval);
return list2 (xd_dbus_type_to_symbol (dtype), INT_TO_INTEGER (val));
}
case DBUS_TYPE_UINT64:
{
dbus_uint64_t val;
dbus_message_iter_get_basic (iter, &val);
uintmax_t pval = val;
XD_DEBUG_MESSAGE ("%c %"PRIuMAX, dtype, pval);
return list2 (xd_dbus_type_to_symbol (dtype), INT_TO_INTEGER (val));
}
case DBUS_TYPE_DOUBLE:
{
double val;
dbus_message_iter_get_basic (iter, &val);
XD_DEBUG_MESSAGE ("%c %f", dtype, val);
return list2 (xd_dbus_type_to_symbol (dtype), make_float (val));
}
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
{
char *val;
dbus_message_iter_get_basic (iter, &val);
XD_DEBUG_MESSAGE ("%c %s", dtype, val);
return list2 (xd_dbus_type_to_symbol (dtype), build_string (val));
}
case DBUS_TYPE_ARRAY:
case DBUS_TYPE_VARIANT:
case DBUS_TYPE_STRUCT:
case DBUS_TYPE_DICT_ENTRY:
{
Lisp_Object result;
DBusMessageIter subiter;
int subtype;
result = Qnil;
dbus_message_iter_recurse (iter, &subiter);
while ((subtype = dbus_message_iter_get_arg_type (&subiter))
!= DBUS_TYPE_INVALID)
{
result = Fcons (xd_retrieve_arg (subtype, &subiter), result);
dbus_message_iter_next (&subiter);
}
XD_DEBUG_MESSAGE ("%c %s", dtype, XD_OBJECT_TO_STRING (result));
return Fcons (xd_dbus_type_to_symbol (dtype), Fnreverse (result));
}
default:
XD_DEBUG_MESSAGE ("DBusType '%c' not supported", dtype);
return Qnil;
}
}
/* Return the number of references of the shared CONNECTION. */
static ptrdiff_t
xd_get_connection_references (DBusConnection *connection)
{
ptrdiff_t *refcount;
/* We cannot access the DBusConnection structure, it is not public.