1 /* Elisp bindings for D-Bus.
2 Copyright (C) 2007-2012 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <dbus/dbus.h>
27 #include "termhooks.h"
31 #ifndef DBUS_NUM_MESSAGE_TYPES
32 #define DBUS_NUM_MESSAGE_TYPES 5
37 static Lisp_Object Qdbus_init_bus
;
38 static Lisp_Object Qdbus_get_unique_name
;
39 static Lisp_Object Qdbus_message_internal
;
41 /* D-Bus error symbol. */
42 static Lisp_Object Qdbus_error
;
44 /* Lisp symbols of the system and session buses. */
45 static Lisp_Object QCdbus_system_bus
, QCdbus_session_bus
;
47 /* Lisp symbol for method call timeout. */
48 static Lisp_Object QCdbus_timeout
;
50 /* Lisp symbols of D-Bus types. */
51 static Lisp_Object QCdbus_type_byte
, QCdbus_type_boolean
;
52 static Lisp_Object QCdbus_type_int16
, QCdbus_type_uint16
;
53 static Lisp_Object QCdbus_type_int32
, QCdbus_type_uint32
;
54 static Lisp_Object QCdbus_type_int64
, QCdbus_type_uint64
;
55 static Lisp_Object QCdbus_type_double
, QCdbus_type_string
;
56 static Lisp_Object QCdbus_type_object_path
, QCdbus_type_signature
;
57 #ifdef DBUS_TYPE_UNIX_FD
58 static Lisp_Object QCdbus_type_unix_fd
;
60 static Lisp_Object QCdbus_type_array
, QCdbus_type_variant
;
61 static Lisp_Object QCdbus_type_struct
, QCdbus_type_dict_entry
;
63 /* Lisp symbols of objects in `dbus-registered-objects-table'. */
64 static Lisp_Object QCdbus_registered_serial
, QCdbus_registered_method
;
65 static Lisp_Object QCdbus_registered_signal
;
67 /* Alist of D-Bus buses we are polling for messages.
68 The key is the symbol or string of the bus, and the value is the
69 connection address. */
70 static Lisp_Object xd_registered_buses
;
72 /* Whether we are reading a D-Bus event. */
73 static int xd_in_read_queued_messages
= 0;
76 /* We use "xd_" and "XD_" as prefix for all internal symbols, because
77 we don't want to poison other namespaces with "dbus_". */
79 /* Raise a signal. If we are reading events, we cannot signal; we
80 throw to xd_read_queued_messages then. */
81 #define XD_SIGNAL1(arg) \
83 if (xd_in_read_queued_messages) \
84 Fthrow (Qdbus_error, Qnil); \
86 xsignal1 (Qdbus_error, arg); \
89 #define XD_SIGNAL2(arg1, arg2) \
91 if (xd_in_read_queued_messages) \
92 Fthrow (Qdbus_error, Qnil); \
94 xsignal2 (Qdbus_error, arg1, arg2); \
97 #define XD_SIGNAL3(arg1, arg2, arg3) \
99 if (xd_in_read_queued_messages) \
100 Fthrow (Qdbus_error, Qnil); \
102 xsignal3 (Qdbus_error, arg1, arg2, arg3); \
105 /* Raise a Lisp error from a D-Bus ERROR. */
106 #define XD_ERROR(error) \
108 /* Remove the trailing newline. */ \
109 char const *mess = error.message; \
110 char const *nl = strchr (mess, '\n'); \
111 Lisp_Object err = make_string (mess, nl ? nl - mess : strlen (mess)); \
112 dbus_error_free (&error); \
116 /* Macros for debugging. In order to enable them, build with
117 "env MYCPPFLAGS='-DDBUS_DEBUG -Wall' make". */
119 #define XD_DEBUG_MESSAGE(...) \
122 snprintf (s, sizeof s, __VA_ARGS__); \
123 if (!noninteractive) \
124 printf ("%s: %s\n", __func__, s); \
125 message ("%s: %s", __func__, s); \
127 #define XD_DEBUG_VALID_LISP_OBJECT_P(object) \
129 if (!valid_lisp_object_p (object)) \
131 XD_DEBUG_MESSAGE ("%d Assertion failure", __LINE__); \
132 XD_SIGNAL1 (build_string ("Assertion failure")); \
136 #else /* !DBUS_DEBUG */
137 #define XD_DEBUG_MESSAGE(...) \
139 if (!NILP (Vdbus_debug)) \
142 snprintf (s, sizeof s, __VA_ARGS__); \
143 message ("%s: %s", __func__, s); \
146 #define XD_DEBUG_VALID_LISP_OBJECT_P(object)
149 /* Check whether TYPE is a basic DBusType. */
150 #ifdef DBUS_TYPE_UNIX_FD
151 #define XD_BASIC_DBUS_TYPE(type) \
152 ((type == DBUS_TYPE_BYTE) \
153 || (type == DBUS_TYPE_BOOLEAN) \
154 || (type == DBUS_TYPE_INT16) \
155 || (type == DBUS_TYPE_UINT16) \
156 || (type == DBUS_TYPE_INT32) \
157 || (type == DBUS_TYPE_UINT32) \
158 || (type == DBUS_TYPE_INT64) \
159 || (type == DBUS_TYPE_UINT64) \
160 || (type == DBUS_TYPE_DOUBLE) \
161 || (type == DBUS_TYPE_STRING) \
162 || (type == DBUS_TYPE_OBJECT_PATH) \
163 || (type == DBUS_TYPE_SIGNATURE) \
164 || (type == DBUS_TYPE_UNIX_FD))
166 #define XD_BASIC_DBUS_TYPE(type) \
167 ((type == DBUS_TYPE_BYTE) \
168 || (type == DBUS_TYPE_BOOLEAN) \
169 || (type == DBUS_TYPE_INT16) \
170 || (type == DBUS_TYPE_UINT16) \
171 || (type == DBUS_TYPE_INT32) \
172 || (type == DBUS_TYPE_UINT32) \
173 || (type == DBUS_TYPE_INT64) \
174 || (type == DBUS_TYPE_UINT64) \
175 || (type == DBUS_TYPE_DOUBLE) \
176 || (type == DBUS_TYPE_STRING) \
177 || (type == DBUS_TYPE_OBJECT_PATH) \
178 || (type == DBUS_TYPE_SIGNATURE))
181 /* This was a macro. On Solaris 2.11 it was said to compile for
182 hours, when optimization is enabled. So we have transferred it into
184 /* Determine the DBusType of a given Lisp symbol. OBJECT must be one
185 of the predefined D-Bus type symbols. */
187 xd_symbol_to_dbus_type (Lisp_Object object
)
190 ((EQ (object
, QCdbus_type_byte
)) ? DBUS_TYPE_BYTE
191 : (EQ (object
, QCdbus_type_boolean
)) ? DBUS_TYPE_BOOLEAN
192 : (EQ (object
, QCdbus_type_int16
)) ? DBUS_TYPE_INT16
193 : (EQ (object
, QCdbus_type_uint16
)) ? DBUS_TYPE_UINT16
194 : (EQ (object
, QCdbus_type_int32
)) ? DBUS_TYPE_INT32
195 : (EQ (object
, QCdbus_type_uint32
)) ? DBUS_TYPE_UINT32
196 : (EQ (object
, QCdbus_type_int64
)) ? DBUS_TYPE_INT64
197 : (EQ (object
, QCdbus_type_uint64
)) ? DBUS_TYPE_UINT64
198 : (EQ (object
, QCdbus_type_double
)) ? DBUS_TYPE_DOUBLE
199 : (EQ (object
, QCdbus_type_string
)) ? DBUS_TYPE_STRING
200 : (EQ (object
, QCdbus_type_object_path
)) ? DBUS_TYPE_OBJECT_PATH
201 : (EQ (object
, QCdbus_type_signature
)) ? DBUS_TYPE_SIGNATURE
202 #ifdef DBUS_TYPE_UNIX_FD
203 : (EQ (object
, QCdbus_type_unix_fd
)) ? DBUS_TYPE_UNIX_FD
205 : (EQ (object
, QCdbus_type_array
)) ? DBUS_TYPE_ARRAY
206 : (EQ (object
, QCdbus_type_variant
)) ? DBUS_TYPE_VARIANT
207 : (EQ (object
, QCdbus_type_struct
)) ? DBUS_TYPE_STRUCT
208 : (EQ (object
, QCdbus_type_dict_entry
)) ? DBUS_TYPE_DICT_ENTRY
209 : DBUS_TYPE_INVALID
);
212 /* Check whether a Lisp symbol is a predefined D-Bus type symbol. */
213 #define XD_DBUS_TYPE_P(object) \
214 (SYMBOLP (object) && ((xd_symbol_to_dbus_type (object) != DBUS_TYPE_INVALID)))
216 /* Determine the DBusType of a given Lisp OBJECT. It is used to
217 convert Lisp objects, being arguments of `dbus-call-method' or
218 `dbus-send-signal', into corresponding C values appended as
219 arguments to a D-Bus message. */
220 #define XD_OBJECT_TO_DBUS_TYPE(object) \
221 ((EQ (object, Qt) || EQ (object, Qnil)) ? DBUS_TYPE_BOOLEAN \
222 : (NATNUMP (object)) ? DBUS_TYPE_UINT32 \
223 : (INTEGERP (object)) ? DBUS_TYPE_INT32 \
224 : (FLOATP (object)) ? DBUS_TYPE_DOUBLE \
225 : (STRINGP (object)) ? DBUS_TYPE_STRING \
226 : (XD_DBUS_TYPE_P (object)) ? xd_symbol_to_dbus_type (object) \
228 ? ((XD_DBUS_TYPE_P (CAR_SAFE (object))) \
229 ? ((XD_BASIC_DBUS_TYPE (xd_symbol_to_dbus_type (CAR_SAFE (object)))) \
231 : xd_symbol_to_dbus_type (CAR_SAFE (object))) \
235 /* Return a list pointer which does not have a Lisp symbol as car. */
236 #define XD_NEXT_VALUE(object) \
237 ((XD_DBUS_TYPE_P (CAR_SAFE (object))) ? CDR_SAFE (object) : object)
239 /* Transform the message type to its string representation for debug
241 #define XD_MESSAGE_TYPE_TO_STRING(mtype) \
242 ((mtype == DBUS_MESSAGE_TYPE_INVALID) \
243 ? "DBUS_MESSAGE_TYPE_INVALID" \
244 : (mtype == DBUS_MESSAGE_TYPE_METHOD_CALL) \
245 ? "DBUS_MESSAGE_TYPE_METHOD_CALL" \
246 : (mtype == DBUS_MESSAGE_TYPE_METHOD_RETURN) \
247 ? "DBUS_MESSAGE_TYPE_METHOD_RETURN" \
248 : (mtype == DBUS_MESSAGE_TYPE_ERROR) \
249 ? "DBUS_MESSAGE_TYPE_ERROR" \
250 : "DBUS_MESSAGE_TYPE_SIGNAL")
252 /* Transform the object to its string representation for debug
254 #define XD_OBJECT_TO_STRING(object) \
255 SDATA (format2 ("%s", object, Qnil))
257 /* Check whether X is a valid dbus serial number. If valid, set
258 SERIAL to its value. Otherwise, signal an error. */
259 #define XD_CHECK_DBUS_SERIAL(x, serial) \
261 dbus_uint32_t DBUS_SERIAL_MAX = -1; \
262 if (NATNUMP (x) && XINT (x) <= DBUS_SERIAL_MAX) \
264 else if (MOST_POSITIVE_FIXNUM < DBUS_SERIAL_MAX \
266 && 0 <= XFLOAT_DATA (x) \
267 && XFLOAT_DATA (x) <= DBUS_SERIAL_MAX) \
268 serial = XFLOAT_DATA (x); \
270 XD_SIGNAL2 (build_string ("Invalid dbus serial"), x); \
273 #define XD_DBUS_VALIDATE_BUS_ADDRESS(bus) \
277 DBusAddressEntry **entries; \
280 dbus_error_init (&derror); \
281 if (!dbus_parse_address (SSDATA (bus), &entries, &len, &derror)) \
284 dbus_error_free (&derror); \
285 dbus_address_entries_free (entries); \
290 CHECK_SYMBOL (bus); \
291 if (!(EQ (bus, QCdbus_system_bus) || EQ (bus, QCdbus_session_bus))) \
292 XD_SIGNAL2 (build_string ("Wrong bus name"), bus); \
293 /* We do not want to have an autolaunch for the session bus. */ \
294 if (EQ (bus, QCdbus_session_bus) \
295 && getenv ("DBUS_SESSION_BUS_ADDRESS") == NULL) \
296 XD_SIGNAL2 (build_string ("No connection to bus"), bus); \
300 #if (HAVE_DBUS_VALIDATE_BUS_NAME || HAVE_DBUS_VALIDATE_PATH \
301 || XD_DBUS_VALIDATE_OBJECT || HAVE_DBUS_VALIDATE_MEMBER)
302 #define XD_DBUS_VALIDATE_OBJECT(object, func) \
304 if (!NILP (object)) \
307 CHECK_STRING (object); \
308 dbus_error_init (&derror); \
309 if (!func (SSDATA (object), &derror)) \
312 dbus_error_free (&derror); \
317 #if HAVE_DBUS_VALIDATE_BUS_NAME
318 #define XD_DBUS_VALIDATE_BUS_NAME(bus_name) \
319 XD_DBUS_VALIDATE_OBJECT(bus_name, dbus_validate_bus_name);
321 #define XD_DBUS_VALIDATE_BUS_NAME(bus_name) \
322 if (!NILP (bus_name)) CHECK_STRING (bus_name);
325 #if HAVE_DBUS_VALIDATE_PATH
326 #define XD_DBUS_VALIDATE_PATH(path) \
327 XD_DBUS_VALIDATE_OBJECT(path, dbus_validate_path);
329 #define XD_DBUS_VALIDATE_PATH(path) \
330 if (!NILP (path)) CHECK_STRING (path);
333 #if HAVE_DBUS_VALIDATE_INTERFACE
334 #define XD_DBUS_VALIDATE_INTERFACE(interface) \
335 XD_DBUS_VALIDATE_OBJECT(interface, dbus_validate_interface);
337 #define XD_DBUS_VALIDATE_INTERFACE(interface) \
338 if (!NILP (interface)) CHECK_STRING (interface);
341 #if HAVE_DBUS_VALIDATE_MEMBER
342 #define XD_DBUS_VALIDATE_MEMBER(member) \
343 XD_DBUS_VALIDATE_OBJECT(member, dbus_validate_member);
345 #define XD_DBUS_VALIDATE_MEMBER(member) \
346 if (!NILP (member)) CHECK_STRING (member);
349 /* Append to SIGNATURE a copy of X, making sure SIGNATURE does
350 not become too long. */
352 xd_signature_cat (char *signature
, char const *x
)
354 ptrdiff_t siglen
= strlen (signature
);
355 ptrdiff_t xlen
= strlen (x
);
356 if (DBUS_MAXIMUM_SIGNATURE_LENGTH
- xlen
<= siglen
)
358 strcat (signature
, x
);
361 /* Compute SIGNATURE of OBJECT. It must have a form that it can be
362 used in dbus_message_iter_open_container. DTYPE is the DBusType
363 the object is related to. It is passed as argument, because it
364 cannot be detected in basic type objects, when they are preceded by
365 a type symbol. PARENT_TYPE is the DBusType of a container this
366 signature is embedded, or DBUS_TYPE_INVALID. It is needed for the
367 check that DBUS_TYPE_DICT_ENTRY occurs only as array element. */
369 xd_signature (char *signature
, unsigned int dtype
, unsigned int parent_type
, Lisp_Object object
)
371 unsigned int subtype
;
375 char x
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
382 case DBUS_TYPE_UINT16
:
383 CHECK_NATNUM (object
);
384 sprintf (signature
, "%c", dtype
);
387 case DBUS_TYPE_BOOLEAN
:
388 if (!EQ (object
, Qt
) && !EQ (object
, Qnil
))
389 wrong_type_argument (intern ("booleanp"), object
);
390 sprintf (signature
, "%c", dtype
);
393 case DBUS_TYPE_INT16
:
394 CHECK_NUMBER (object
);
395 sprintf (signature
, "%c", dtype
);
398 case DBUS_TYPE_UINT32
:
399 case DBUS_TYPE_UINT64
:
400 #ifdef DBUS_TYPE_UNIX_FD
401 case DBUS_TYPE_UNIX_FD
:
403 case DBUS_TYPE_INT32
:
404 case DBUS_TYPE_INT64
:
405 case DBUS_TYPE_DOUBLE
:
406 CHECK_NUMBER_OR_FLOAT (object
);
407 sprintf (signature
, "%c", dtype
);
410 case DBUS_TYPE_STRING
:
411 case DBUS_TYPE_OBJECT_PATH
:
412 case DBUS_TYPE_SIGNATURE
:
413 CHECK_STRING (object
);
414 sprintf (signature
, "%c", dtype
);
417 case DBUS_TYPE_ARRAY
:
418 /* Check that all list elements have the same D-Bus type. For
419 complex element types, we just check the container type, not
420 the whole element's signature. */
423 /* Type symbol is optional. */
424 if (EQ (QCdbus_type_array
, CAR_SAFE (elt
)))
425 elt
= XD_NEXT_VALUE (elt
);
427 /* If the array is empty, DBUS_TYPE_STRING is the default
431 subtype
= DBUS_TYPE_STRING
;
432 subsig
= DBUS_TYPE_STRING_AS_STRING
;
436 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
437 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
441 /* If the element type is DBUS_TYPE_SIGNATURE, and this is the
442 only element, the value of this element is used as the
443 array's element signature. */
444 if ((subtype
== DBUS_TYPE_SIGNATURE
)
445 && STRINGP (CAR_SAFE (XD_NEXT_VALUE (elt
)))
446 && NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
447 subsig
= SSDATA (CAR_SAFE (XD_NEXT_VALUE (elt
)));
451 if (subtype
!= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
)))
452 wrong_type_argument (intern ("D-Bus"), CAR_SAFE (elt
));
453 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
456 subsiglen
= snprintf (signature
, DBUS_MAXIMUM_SIGNATURE_LENGTH
,
457 "%c%s", dtype
, subsig
);
458 if (! (0 <= subsiglen
&& subsiglen
< DBUS_MAXIMUM_SIGNATURE_LENGTH
))
462 case DBUS_TYPE_VARIANT
:
463 /* Check that there is exactly one list element. */
466 elt
= XD_NEXT_VALUE (elt
);
467 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
468 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
470 if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
471 wrong_type_argument (intern ("D-Bus"),
472 CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt
))));
474 sprintf (signature
, "%c", dtype
);
477 case DBUS_TYPE_STRUCT
:
478 /* A struct list might contain any number of elements with
479 different types. No further check needed. */
482 elt
= XD_NEXT_VALUE (elt
);
484 /* Compose the signature from the elements. It is enclosed by
486 sprintf (signature
, "%c", DBUS_STRUCT_BEGIN_CHAR
);
489 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
490 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
491 xd_signature_cat (signature
, x
);
492 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
494 xd_signature_cat (signature
, DBUS_STRUCT_END_CHAR_AS_STRING
);
497 case DBUS_TYPE_DICT_ENTRY
:
498 /* Check that there are exactly two list elements, and the first
499 one is of basic type. The dictionary entry itself must be an
500 element of an array. */
503 /* Check the parent object type. */
504 if (parent_type
!= DBUS_TYPE_ARRAY
)
505 wrong_type_argument (intern ("D-Bus"), object
);
507 /* Compose the signature from the elements. It is enclosed by
509 sprintf (signature
, "%c", DBUS_DICT_ENTRY_BEGIN_CHAR
);
512 elt
= XD_NEXT_VALUE (elt
);
513 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
514 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
515 xd_signature_cat (signature
, x
);
517 if (!XD_BASIC_DBUS_TYPE (subtype
))
518 wrong_type_argument (intern ("D-Bus"), CAR_SAFE (XD_NEXT_VALUE (elt
)));
520 /* Second element. */
521 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
522 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
523 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
524 xd_signature_cat (signature
, x
);
526 if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
527 wrong_type_argument (intern ("D-Bus"),
528 CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt
))));
530 /* Closing signature. */
531 xd_signature_cat (signature
, DBUS_DICT_ENTRY_END_CHAR_AS_STRING
);
535 wrong_type_argument (intern ("D-Bus"), object
);
538 XD_DEBUG_MESSAGE ("%s", signature
);
541 /* Convert X to a signed integer with bounds LO and HI. */
543 extract_signed (Lisp_Object x
, intmax_t lo
, intmax_t hi
)
545 CHECK_NUMBER_OR_FLOAT (x
);
548 if (lo
<= XINT (x
) && XINT (x
) <= hi
)
553 double d
= XFLOAT_DATA (x
);
554 if (lo
<= d
&& d
<= hi
)
561 args_out_of_range_3 (x
,
562 make_fixnum_or_float (lo
),
563 make_fixnum_or_float (hi
));
566 /* Convert X to an unsigned integer with bounds 0 and HI. */
568 extract_unsigned (Lisp_Object x
, uintmax_t hi
)
570 CHECK_NUMBER_OR_FLOAT (x
);
573 if (0 <= XINT (x
) && XINT (x
) <= hi
)
578 double d
= XFLOAT_DATA (x
);
579 if (0 <= d
&& d
<= hi
)
586 args_out_of_range_2 (x
, make_fixnum_or_float (hi
));
589 /* Append C value, extracted from Lisp OBJECT, to iteration ITER.
590 DTYPE must be a valid DBusType. It is used to convert Lisp
591 objects, being arguments of `dbus-call-method' or
592 `dbus-send-signal', into corresponding C values appended as
593 arguments to a D-Bus message. */
595 xd_append_arg (unsigned int dtype
, Lisp_Object object
, DBusMessageIter
*iter
)
597 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
598 DBusMessageIter subiter
;
600 if (XD_BASIC_DBUS_TYPE (dtype
))
604 CHECK_NATNUM (object
);
606 unsigned char val
= XFASTINT (object
) & 0xFF;
607 XD_DEBUG_MESSAGE ("%c %u", dtype
, val
);
608 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
609 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
613 case DBUS_TYPE_BOOLEAN
:
615 dbus_bool_t val
= (NILP (object
)) ? FALSE
: TRUE
;
616 XD_DEBUG_MESSAGE ("%c %s", dtype
, (val
== FALSE
) ? "false" : "true");
617 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
618 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
622 case DBUS_TYPE_INT16
:
623 CHECK_TYPE_RANGED_INTEGER (dbus_int16_t
, object
);
625 dbus_int16_t val
= XINT (object
);
627 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
628 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
629 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
633 case DBUS_TYPE_UINT16
:
634 CHECK_TYPE_RANGED_INTEGER (dbus_uint16_t
, object
);
636 dbus_uint16_t val
= XFASTINT (object
);
637 unsigned int pval
= val
;
638 XD_DEBUG_MESSAGE ("%c %u", dtype
, pval
);
639 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
640 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
644 case DBUS_TYPE_INT32
:
646 dbus_int32_t val
= extract_signed (object
,
647 TYPE_MINIMUM (dbus_int32_t
),
648 TYPE_MAXIMUM (dbus_int32_t
));
650 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
651 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
652 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
656 case DBUS_TYPE_UINT32
:
657 #ifdef DBUS_TYPE_UNIX_FD
658 case DBUS_TYPE_UNIX_FD
:
661 dbus_uint32_t val
= extract_unsigned (object
,
662 TYPE_MAXIMUM (dbus_uint32_t
));
663 unsigned int pval
= val
;
664 XD_DEBUG_MESSAGE ("%c %u", dtype
, pval
);
665 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
666 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
670 case DBUS_TYPE_INT64
:
671 CHECK_TYPE_RANGED_INTEGER_OR_FLOAT (dbus_int64_t
, object
);
673 dbus_int64_t val
= extract_signed (object
,
674 TYPE_MINIMUM (dbus_int64_t
),
675 TYPE_MAXIMUM (dbus_int64_t
));
676 printmax_t pval
= val
;
677 XD_DEBUG_MESSAGE ("%c %"pMd
, dtype
, pval
);
678 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
679 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
683 case DBUS_TYPE_UINT64
:
685 dbus_uint64_t val
= extract_unsigned (object
,
686 TYPE_MAXIMUM (dbus_uint64_t
));
687 uprintmax_t pval
= val
;
688 XD_DEBUG_MESSAGE ("%c %"pMu
, dtype
, pval
);
689 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
690 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
694 case DBUS_TYPE_DOUBLE
:
696 double val
= extract_float (object
);
697 XD_DEBUG_MESSAGE ("%c %f", dtype
, val
);
698 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
699 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
703 case DBUS_TYPE_STRING
:
704 case DBUS_TYPE_OBJECT_PATH
:
705 case DBUS_TYPE_SIGNATURE
:
706 CHECK_STRING (object
);
708 /* We need to send a valid UTF-8 string. We could encode `object'
709 but by not encoding it, we guarantee it's valid utf-8, even if
710 it contains eight-bit-bytes. Of course, you can still send
711 manually-crafted junk by passing a unibyte string. */
712 char *val
= SSDATA (object
);
713 XD_DEBUG_MESSAGE ("%c %s", dtype
, val
);
714 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
715 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
720 else /* Compound types. */
723 /* All compound types except array have a type symbol. For
724 array, it is optional. Skip it. */
725 if (!XD_BASIC_DBUS_TYPE (XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
))))
726 object
= XD_NEXT_VALUE (object
);
728 /* Open new subiteration. */
731 case DBUS_TYPE_ARRAY
:
732 /* An array has only elements of the same type. So it is
733 sufficient to check the first element's signature
737 /* If the array is empty, DBUS_TYPE_STRING is the default
739 strcpy (signature
, DBUS_TYPE_STRING_AS_STRING
);
742 /* If the element type is DBUS_TYPE_SIGNATURE, and this is
743 the only element, the value of this element is used as
744 the array's element signature. */
745 if ((XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
))
746 == DBUS_TYPE_SIGNATURE
)
747 && STRINGP (CAR_SAFE (XD_NEXT_VALUE (object
)))
748 && NILP (CDR_SAFE (XD_NEXT_VALUE (object
))))
750 strcpy (signature
, SSDATA (CAR_SAFE (XD_NEXT_VALUE (object
))));
751 object
= CDR_SAFE (XD_NEXT_VALUE (object
));
755 xd_signature (signature
,
756 XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
)),
757 dtype
, CAR_SAFE (XD_NEXT_VALUE (object
)));
759 XD_DEBUG_MESSAGE ("%c %s %s", dtype
, signature
,
760 XD_OBJECT_TO_STRING (object
));
761 if (!dbus_message_iter_open_container (iter
, dtype
,
762 signature
, &subiter
))
763 XD_SIGNAL3 (build_string ("Cannot open container"),
764 make_number (dtype
), build_string (signature
));
767 case DBUS_TYPE_VARIANT
:
768 /* A variant has just one element. */
769 xd_signature (signature
, XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
)),
770 dtype
, CAR_SAFE (XD_NEXT_VALUE (object
)));
772 XD_DEBUG_MESSAGE ("%c %s %s", dtype
, signature
,
773 XD_OBJECT_TO_STRING (object
));
774 if (!dbus_message_iter_open_container (iter
, dtype
,
775 signature
, &subiter
))
776 XD_SIGNAL3 (build_string ("Cannot open container"),
777 make_number (dtype
), build_string (signature
));
780 case DBUS_TYPE_STRUCT
:
781 case DBUS_TYPE_DICT_ENTRY
:
782 /* These containers do not require a signature. */
783 XD_DEBUG_MESSAGE ("%c %s", dtype
, XD_OBJECT_TO_STRING (object
));
784 if (!dbus_message_iter_open_container (iter
, dtype
, NULL
, &subiter
))
785 XD_SIGNAL2 (build_string ("Cannot open container"),
786 make_number (dtype
));
790 /* Loop over list elements. */
791 while (!NILP (object
))
793 dtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
));
794 object
= XD_NEXT_VALUE (object
);
796 xd_append_arg (dtype
, CAR_SAFE (object
), &subiter
);
798 object
= CDR_SAFE (object
);
801 /* Close the subiteration. */
802 if (!dbus_message_iter_close_container (iter
, &subiter
))
803 XD_SIGNAL2 (build_string ("Cannot close container"),
804 make_number (dtype
));
808 /* Retrieve C value from a DBusMessageIter structure ITER, and return
809 a converted Lisp object. The type DTYPE of the argument of the
810 D-Bus message must be a valid DBusType. Compound D-Bus types
811 result always in a Lisp list. */
813 xd_retrieve_arg (unsigned int dtype
, DBusMessageIter
*iter
)
821 dbus_message_iter_get_basic (iter
, &val
);
823 XD_DEBUG_MESSAGE ("%c %u", dtype
, val
);
824 return make_number (val
);
827 case DBUS_TYPE_BOOLEAN
:
830 dbus_message_iter_get_basic (iter
, &val
);
831 XD_DEBUG_MESSAGE ("%c %s", dtype
, (val
== FALSE
) ? "false" : "true");
832 return (val
== FALSE
) ? Qnil
: Qt
;
835 case DBUS_TYPE_INT16
:
839 dbus_message_iter_get_basic (iter
, &val
);
841 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
842 return make_number (val
);
845 case DBUS_TYPE_UINT16
:
849 dbus_message_iter_get_basic (iter
, &val
);
851 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
852 return make_number (val
);
855 case DBUS_TYPE_INT32
:
859 dbus_message_iter_get_basic (iter
, &val
);
861 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
862 return make_fixnum_or_float (val
);
865 case DBUS_TYPE_UINT32
:
866 #ifdef DBUS_TYPE_UNIX_FD
867 case DBUS_TYPE_UNIX_FD
:
871 unsigned int pval
= val
;
872 dbus_message_iter_get_basic (iter
, &val
);
874 XD_DEBUG_MESSAGE ("%c %u", dtype
, pval
);
875 return make_fixnum_or_float (val
);
878 case DBUS_TYPE_INT64
:
882 dbus_message_iter_get_basic (iter
, &val
);
884 XD_DEBUG_MESSAGE ("%c %"pMd
, dtype
, pval
);
885 return make_fixnum_or_float (val
);
888 case DBUS_TYPE_UINT64
:
892 dbus_message_iter_get_basic (iter
, &val
);
894 XD_DEBUG_MESSAGE ("%c %"pMd
, dtype
, pval
);
895 return make_fixnum_or_float (val
);
898 case DBUS_TYPE_DOUBLE
:
901 dbus_message_iter_get_basic (iter
, &val
);
902 XD_DEBUG_MESSAGE ("%c %f", dtype
, val
);
903 return make_float (val
);
906 case DBUS_TYPE_STRING
:
907 case DBUS_TYPE_OBJECT_PATH
:
908 case DBUS_TYPE_SIGNATURE
:
911 dbus_message_iter_get_basic (iter
, &val
);
912 XD_DEBUG_MESSAGE ("%c %s", dtype
, val
);
913 return build_string (val
);
916 case DBUS_TYPE_ARRAY
:
917 case DBUS_TYPE_VARIANT
:
918 case DBUS_TYPE_STRUCT
:
919 case DBUS_TYPE_DICT_ENTRY
:
923 DBusMessageIter subiter
;
927 dbus_message_iter_recurse (iter
, &subiter
);
928 while ((subtype
= dbus_message_iter_get_arg_type (&subiter
))
929 != DBUS_TYPE_INVALID
)
931 result
= Fcons (xd_retrieve_arg (subtype
, &subiter
), result
);
932 dbus_message_iter_next (&subiter
);
934 XD_DEBUG_MESSAGE ("%c %s", dtype
, XD_OBJECT_TO_STRING (result
));
935 RETURN_UNGCPRO (Fnreverse (result
));
939 XD_DEBUG_MESSAGE ("DBusType '%c' not supported", dtype
);
944 /* Return the number of references of the shared CONNECTION. */
946 xd_get_connection_references (DBusConnection
*connection
)
950 /* We cannot access the DBusConnection structure, it is not public.
951 But we know, that the reference counter is the first field in
953 refcount
= (void *) &connection
;
954 refcount
= (void *) *refcount
;
958 /* Return D-Bus connection address. BUS is either a Lisp symbol,
959 :system or :session, or a string denoting the bus address. */
960 static DBusConnection
*
961 xd_get_connection_address (Lisp_Object bus
)
963 DBusConnection
*connection
;
966 val
= CDR_SAFE (Fassoc (bus
, xd_registered_buses
));
968 XD_SIGNAL2 (build_string ("No connection to bus"), bus
);
970 connection
= (DBusConnection
*) (intptr_t) XFASTINT (val
);
972 if (!dbus_connection_get_is_connected (connection
))
973 XD_SIGNAL2 (build_string ("No connection to bus"), bus
);
978 /* Return the file descriptor for WATCH, -1 if not found. */
980 xd_find_watch_fd (DBusWatch
*watch
)
982 #if HAVE_DBUS_WATCH_GET_UNIX_FD
983 /* TODO: Reverse these on Win32, which prefers the opposite. */
984 int fd
= dbus_watch_get_unix_fd (watch
);
986 fd
= dbus_watch_get_socket (watch
);
988 int fd
= dbus_watch_get_fd (watch
);
995 xd_read_queued_messages (int fd
, void *data
, int for_read
);
997 /* Start monitoring WATCH for possible I/O. */
999 xd_add_watch (DBusWatch
*watch
, void *data
)
1001 unsigned int flags
= dbus_watch_get_flags (watch
);
1002 int fd
= xd_find_watch_fd (watch
);
1004 XD_DEBUG_MESSAGE ("fd %d, write %d, enabled %d",
1005 fd
, flags
& DBUS_WATCH_WRITABLE
,
1006 dbus_watch_get_enabled (watch
));
1011 if (dbus_watch_get_enabled (watch
))
1013 if (flags
& DBUS_WATCH_WRITABLE
)
1014 add_write_fd (fd
, xd_read_queued_messages
, data
);
1015 if (flags
& DBUS_WATCH_READABLE
)
1016 add_read_fd (fd
, xd_read_queued_messages
, data
);
1021 /* Stop monitoring WATCH for possible I/O.
1022 DATA is the used bus, either a string or QCdbus_system_bus or
1023 QCdbus_session_bus. */
1025 xd_remove_watch (DBusWatch
*watch
, void *data
)
1027 unsigned int flags
= dbus_watch_get_flags (watch
);
1028 int fd
= xd_find_watch_fd (watch
);
1030 XD_DEBUG_MESSAGE ("fd %d", fd
);
1035 /* Unset session environment. */
1036 if (XSYMBOL (QCdbus_session_bus
) == data
)
1038 // XD_DEBUG_MESSAGE ("unsetenv DBUS_SESSION_BUS_ADDRESS");
1039 // unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1042 if (flags
& DBUS_WATCH_WRITABLE
)
1043 delete_write_fd (fd
);
1044 if (flags
& DBUS_WATCH_READABLE
)
1045 delete_read_fd (fd
);
1048 /* Toggle monitoring WATCH for possible I/O. */
1050 xd_toggle_watch (DBusWatch
*watch
, void *data
)
1052 if (dbus_watch_get_enabled (watch
))
1053 xd_add_watch (watch
, data
);
1055 xd_remove_watch (watch
, data
);
1058 /* Close connection to D-Bus BUS. */
1060 xd_close_bus (Lisp_Object bus
)
1062 DBusConnection
*connection
;
1065 /* Check whether we are connected. */
1066 val
= Fassoc (bus
, xd_registered_buses
);
1070 /* Retrieve bus address. */
1071 connection
= xd_get_connection_address (bus
);
1073 /* Close connection, if there isn't another shared application. */
1074 if (xd_get_connection_references (connection
) == 1)
1076 XD_DEBUG_MESSAGE ("Close connection to bus %s",
1077 XD_OBJECT_TO_STRING (bus
));
1078 dbus_connection_close (connection
);
1081 /* Decrement reference count. */
1082 dbus_connection_unref (connection
);
1084 /* Remove bus from list of registered buses. */
1085 xd_registered_buses
= Fdelete (val
, xd_registered_buses
);
1091 DEFUN ("dbus-init-bus", Fdbus_init_bus
, Sdbus_init_bus
, 1, 2, 0,
1092 doc
: /* Establish the connection to D-Bus BUS.
1094 BUS can be either the symbol `:system' or the symbol `:session', or it
1095 can be a string denoting the address of the corresponding bus. For
1096 the system and session buses, this function is called when loading
1097 `dbus.el', there is no need to call it again.
1099 The function returns a number, which counts the connections this Emacs
1100 session has established to the BUS under the same unique name (see
1101 `dbus-get-unique-name'). It depends on the libraries Emacs is linked
1102 with, and on the environment Emacs is running. For example, if Emacs
1103 is linked with the gtk toolkit, and it runs in a GTK-aware environment
1104 like Gnome, another connection might already be established.
1106 When PRIVATE is non-nil, a new connection is established instead of
1107 reusing an existing one. It results in a new unique name at the bus.
1108 This can be used, if it is necessary to distinguish from another
1109 connection used in the same Emacs process, like the one established by
1110 GTK+. It should be used with care for at least the `:system' and
1111 `:session' buses, because other Emacs Lisp packages might already use
1112 this connection to those buses. */)
1113 (Lisp_Object bus
, Lisp_Object
private)
1115 DBusConnection
*connection
;
1120 /* Check parameter. */
1121 XD_DBUS_VALIDATE_BUS_ADDRESS (bus
);
1123 /* Close bus if it is already open. */
1127 dbus_error_init (&derror
);
1129 /* Open the connection. */
1132 connection
= dbus_connection_open (SSDATA (bus
), &derror
);
1134 connection
= dbus_connection_open_private (SSDATA (bus
), &derror
);
1138 connection
= dbus_bus_get (EQ (bus
, QCdbus_system_bus
)
1139 ? DBUS_BUS_SYSTEM
: DBUS_BUS_SESSION
,
1142 connection
= dbus_bus_get_private (EQ (bus
, QCdbus_system_bus
)
1143 ? DBUS_BUS_SYSTEM
: DBUS_BUS_SESSION
,
1146 if (dbus_error_is_set (&derror
))
1149 if (connection
== NULL
)
1150 XD_SIGNAL2 (build_string ("No connection to bus"), bus
);
1152 /* If it is not the system or session bus, we must register
1153 ourselves. Otherwise, we have called dbus_bus_get, which has
1154 configured us to exit if the connection closes - we undo this
1157 dbus_bus_register (connection
, &derror
);
1159 dbus_connection_set_exit_on_disconnect (connection
, FALSE
);
1161 if (dbus_error_is_set (&derror
))
1164 /* Add the watch functions. We pass also the bus as data, in order
1165 to distinguish between the buses in xd_remove_watch. */
1166 if (!dbus_connection_set_watch_functions (connection
,
1171 ? (void *) XSYMBOL (bus
)
1172 : (void *) XSTRING (bus
),
1174 XD_SIGNAL1 (build_string ("Cannot add watch functions"));
1176 /* Add bus to list of registered buses. */
1177 XSETFASTINT (val
, (intptr_t) connection
);
1178 xd_registered_buses
= Fcons (Fcons (bus
, val
), xd_registered_buses
);
1180 /* We do not want to abort. */
1181 putenv ((char *) "DBUS_FATAL_WARNINGS=0");
1184 dbus_error_free (&derror
);
1186 /* Return reference counter. */
1187 refcount
= xd_get_connection_references (connection
);
1188 XD_DEBUG_MESSAGE ("Bus %s, Reference counter %d",
1189 XD_OBJECT_TO_STRING (bus
), refcount
);
1190 return make_number (refcount
);
1193 DEFUN ("dbus-get-unique-name", Fdbus_get_unique_name
, Sdbus_get_unique_name
,
1195 doc
: /* Return the unique name of Emacs registered at D-Bus BUS. */)
1198 DBusConnection
*connection
;
1201 /* Check parameter. */
1202 XD_DBUS_VALIDATE_BUS_ADDRESS (bus
);
1204 /* Retrieve bus address. */
1205 connection
= xd_get_connection_address (bus
);
1207 /* Request the name. */
1208 name
= dbus_bus_get_unique_name (connection
);
1210 XD_SIGNAL1 (build_string ("No unique name available"));
1213 return build_string (name
);
1216 DEFUN ("dbus-message-internal", Fdbus_message_internal
, Sdbus_message_internal
,
1218 doc
: /* Send a D-Bus message.
1219 This is an internal function, it shall not be used outside dbus.el.
1221 The following usages are expected:
1223 `dbus-call-method', `dbus-call-method-asynchronously':
1224 \(dbus-message-internal
1225 dbus-message-type-method-call BUS SERVICE PATH INTERFACE METHOD HANDLER
1226 &optional :timeout TIMEOUT &rest ARGS)
1229 \(dbus-message-internal
1230 dbus-message-type-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS)
1232 `dbus-method-return-internal':
1233 \(dbus-message-internal
1234 dbus-message-type-method-return BUS SERVICE SERIAL &rest ARGS)
1236 `dbus-method-error-internal':
1237 \(dbus-message-internal
1238 dbus-message-type-error BUS SERVICE SERIAL &rest ARGS)
1240 usage: (dbus-message-internal &rest REST) */)
1241 (ptrdiff_t nargs
, Lisp_Object
*args
)
1243 Lisp_Object message_type
, bus
, service
, handler
;
1244 Lisp_Object path
= Qnil
;
1245 Lisp_Object interface
= Qnil
;
1246 Lisp_Object member
= Qnil
;
1248 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
, gcpro6
;
1249 DBusConnection
*connection
;
1250 DBusMessage
*dmessage
;
1251 DBusMessageIter iter
;
1254 dbus_uint32_t serial
= 0;
1255 unsigned int ui_serial
;
1258 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
1260 /* Initialize parameters. */
1261 message_type
= args
[0];
1266 CHECK_NATNUM (message_type
);
1267 mtype
= XFASTINT (message_type
);
1268 if ((mtype
<= DBUS_MESSAGE_TYPE_INVALID
) || (mtype
>= DBUS_NUM_MESSAGE_TYPES
))
1269 XD_SIGNAL2 (build_string ("Invalid message type"), message_type
);
1271 if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1272 || (mtype
== DBUS_MESSAGE_TYPE_SIGNAL
))
1275 interface
= args
[4];
1277 if (mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1279 count
= (mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
) ? 7 : 6;
1281 else /* DBUS_MESSAGE_TYPE_METHOD_RETURN, DBUS_MESSAGE_TYPE_ERROR */
1283 XD_CHECK_DBUS_SERIAL (args
[3], serial
);
1287 /* Check parameters. */
1288 XD_DBUS_VALIDATE_BUS_ADDRESS (bus
);
1289 XD_DBUS_VALIDATE_BUS_NAME (service
);
1291 xsignal2 (Qwrong_number_of_arguments
,
1292 Qdbus_message_internal
,
1293 make_number (nargs
));
1295 if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1296 || (mtype
== DBUS_MESSAGE_TYPE_SIGNAL
))
1298 XD_DBUS_VALIDATE_PATH (path
);
1299 XD_DBUS_VALIDATE_INTERFACE (interface
);
1300 XD_DBUS_VALIDATE_MEMBER (member
);
1301 if (!NILP (handler
) && (!FUNCTIONP (handler
)))
1302 wrong_type_argument (Qinvalid_function
, handler
);
1305 /* Protect Lisp variables. */
1306 GCPRO6 (bus
, service
, path
, interface
, member
, handler
);
1308 /* Trace parameters. */
1311 case DBUS_MESSAGE_TYPE_METHOD_CALL
:
1312 XD_DEBUG_MESSAGE ("%s %s %s %s %s %s %s",
1313 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1314 XD_OBJECT_TO_STRING (bus
),
1315 XD_OBJECT_TO_STRING (service
),
1316 XD_OBJECT_TO_STRING (path
),
1317 XD_OBJECT_TO_STRING (interface
),
1318 XD_OBJECT_TO_STRING (member
),
1319 XD_OBJECT_TO_STRING (handler
));
1321 case DBUS_MESSAGE_TYPE_SIGNAL
:
1322 XD_DEBUG_MESSAGE ("%s %s %s %s %s %s",
1323 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1324 XD_OBJECT_TO_STRING (bus
),
1325 XD_OBJECT_TO_STRING (service
),
1326 XD_OBJECT_TO_STRING (path
),
1327 XD_OBJECT_TO_STRING (interface
),
1328 XD_OBJECT_TO_STRING (member
));
1330 default: /* DBUS_MESSAGE_TYPE_METHOD_RETURN, DBUS_MESSAGE_TYPE_ERROR */
1332 XD_DEBUG_MESSAGE ("%s %s %s %u",
1333 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1334 XD_OBJECT_TO_STRING (bus
),
1335 XD_OBJECT_TO_STRING (service
),
1339 /* Retrieve bus address. */
1340 connection
= xd_get_connection_address (bus
);
1342 /* Create the D-Bus message. */
1343 dmessage
= dbus_message_new (mtype
);
1344 if (dmessage
== NULL
)
1347 XD_SIGNAL1 (build_string ("Unable to create a new message"));
1350 if (STRINGP (service
))
1352 if (mtype
!= DBUS_MESSAGE_TYPE_SIGNAL
)
1353 /* Set destination. */
1355 if (!dbus_message_set_destination (dmessage
, SSDATA (service
)))
1358 XD_SIGNAL2 (build_string ("Unable to set the destination"),
1364 /* Set destination for unicast signals. */
1368 /* If it is the same unique name as we are registered at the
1369 bus or an unknown name, we regard it as broadcast message
1370 due to backward compatibility. */
1371 if (dbus_bus_name_has_owner (connection
, SSDATA (service
), NULL
))
1372 uname
= call2 (intern ("dbus-get-name-owner"), bus
, service
);
1377 && (strcmp (dbus_bus_get_unique_name (connection
), SSDATA (uname
))
1379 && (!dbus_message_set_destination (dmessage
, SSDATA (service
))))
1382 XD_SIGNAL2 (build_string ("Unable to set signal destination"),
1388 /* Set message parameters. */
1389 if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1390 || (mtype
== DBUS_MESSAGE_TYPE_SIGNAL
))
1392 if ((!dbus_message_set_path (dmessage
, SSDATA (path
)))
1393 || (!dbus_message_set_interface (dmessage
, SSDATA (interface
)))
1394 || (!dbus_message_set_member (dmessage
, SSDATA (member
))))
1397 XD_SIGNAL1 (build_string ("Unable to set the message parameter"));
1401 else /* DBUS_MESSAGE_TYPE_METHOD_RETURN, DBUS_MESSAGE_TYPE_ERROR */
1403 if (!dbus_message_set_reply_serial (dmessage
, serial
))
1406 XD_SIGNAL1 (build_string ("Unable to create a return message"));
1409 if ((mtype
== DBUS_MESSAGE_TYPE_ERROR
)
1410 && (!dbus_message_set_error_name (dmessage
, DBUS_ERROR_FAILED
)))
1413 XD_SIGNAL1 (build_string ("Unable to create a error message"));
1417 /* Check for timeout parameter. */
1418 if ((count
+2 <= nargs
) && (EQ ((args
[count
]), QCdbus_timeout
)))
1420 CHECK_NATNUM (args
[count
+1]);
1421 timeout
= min (XFASTINT (args
[count
+1]), INT_MAX
);
1425 /* Initialize parameter list of message. */
1426 dbus_message_iter_init_append (dmessage
, &iter
);
1428 /* Append parameters to the message. */
1429 for (; count
< nargs
; ++count
)
1431 dtype
= XD_OBJECT_TO_DBUS_TYPE (args
[count
]);
1432 if (XD_DBUS_TYPE_P (args
[count
]))
1434 XD_DEBUG_VALID_LISP_OBJECT_P (args
[count
]);
1435 XD_DEBUG_VALID_LISP_OBJECT_P (args
[count
+1]);
1436 XD_DEBUG_MESSAGE ("Parameter%"pD
"d %s %s", count
- 4,
1437 XD_OBJECT_TO_STRING (args
[count
]),
1438 XD_OBJECT_TO_STRING (args
[count
+1]));
1443 XD_DEBUG_VALID_LISP_OBJECT_P (args
[count
]);
1444 XD_DEBUG_MESSAGE ("Parameter%"pD
"d %s", count
- 4,
1445 XD_OBJECT_TO_STRING (args
[count
]));
1448 /* Check for valid signature. We use DBUS_TYPE_INVALID as
1449 indication that there is no parent type. */
1450 xd_signature (signature
, dtype
, DBUS_TYPE_INVALID
, args
[count
]);
1452 xd_append_arg (dtype
, args
[count
], &iter
);
1455 if (!NILP (handler
))
1457 /* Send the message. The message is just added to the outgoing
1459 if (!dbus_connection_send_with_reply (connection
, dmessage
,
1463 XD_SIGNAL1 (build_string ("Cannot send message"));
1466 /* The result is the key in Vdbus_registered_objects_table. */
1467 serial
= dbus_message_get_serial (dmessage
);
1468 result
= list3 (QCdbus_registered_serial
,
1469 bus
, make_fixnum_or_float (serial
));
1471 /* Create a hash table entry. */
1472 Fputhash (result
, handler
, Vdbus_registered_objects_table
);
1476 /* Send the message. The message is just added to the outgoing
1478 if (!dbus_connection_send (connection
, dmessage
, NULL
))
1481 XD_SIGNAL1 (build_string ("Cannot send message"));
1487 XD_DEBUG_MESSAGE ("Message sent: %s", XD_OBJECT_TO_STRING (result
));
1490 dbus_message_unref (dmessage
);
1492 /* Return the result. */
1493 RETURN_UNGCPRO (result
);
1496 /* Read one queued incoming message of the D-Bus BUS.
1497 BUS is either a Lisp symbol, :system or :session, or a string denoting
1500 xd_read_message_1 (DBusConnection
*connection
, Lisp_Object bus
)
1502 Lisp_Object args
, key
, value
;
1503 struct gcpro gcpro1
;
1504 struct input_event event
;
1505 DBusMessage
*dmessage
;
1506 DBusMessageIter iter
;
1509 dbus_uint32_t serial
;
1510 unsigned int ui_serial
;
1511 const char *uname
, *path
, *interface
, *member
;
1513 dmessage
= dbus_connection_pop_message (connection
);
1515 /* Return if there is no queued message. */
1516 if (dmessage
== NULL
)
1519 /* Collect the parameters. */
1523 /* Loop over the resulting parameters. Construct a list. */
1524 if (dbus_message_iter_init (dmessage
, &iter
))
1526 while ((dtype
= dbus_message_iter_get_arg_type (&iter
))
1527 != DBUS_TYPE_INVALID
)
1529 args
= Fcons (xd_retrieve_arg (dtype
, &iter
), args
);
1530 dbus_message_iter_next (&iter
);
1532 /* The arguments are stored in reverse order. Reorder them. */
1533 args
= Fnreverse (args
);
1536 /* Read message type, message serial, unique name, object path,
1537 interface and member from the message. */
1538 mtype
= dbus_message_get_type (dmessage
);
1539 ui_serial
= serial
=
1540 ((mtype
== DBUS_MESSAGE_TYPE_METHOD_RETURN
)
1541 || (mtype
== DBUS_MESSAGE_TYPE_ERROR
))
1542 ? dbus_message_get_reply_serial (dmessage
)
1543 : dbus_message_get_serial (dmessage
);
1544 uname
= dbus_message_get_sender (dmessage
);
1545 path
= dbus_message_get_path (dmessage
);
1546 interface
= dbus_message_get_interface (dmessage
);
1547 member
= dbus_message_get_member (dmessage
);
1549 XD_DEBUG_MESSAGE ("Event received: %s %u %s %s %s %s %s",
1550 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1551 ui_serial
, uname
, path
, interface
, member
,
1552 XD_OBJECT_TO_STRING (args
));
1554 if (mtype
== DBUS_MESSAGE_TYPE_INVALID
)
1557 else if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_RETURN
)
1558 || (mtype
== DBUS_MESSAGE_TYPE_ERROR
))
1560 /* Search for a registered function of the message. */
1561 key
= list3 (QCdbus_registered_serial
, bus
,
1562 make_fixnum_or_float (serial
));
1563 value
= Fgethash (key
, Vdbus_registered_objects_table
, Qnil
);
1565 /* There shall be exactly one entry. Construct an event. */
1569 /* Remove the entry. */
1570 Fremhash (key
, Vdbus_registered_objects_table
);
1572 /* Construct an event. */
1574 event
.kind
= DBUS_EVENT
;
1575 event
.frame_or_window
= Qnil
;
1576 event
.arg
= Fcons (value
, args
);
1579 else /* DBUS_MESSAGE_TYPE_METHOD_CALL, DBUS_MESSAGE_TYPE_SIGNAL. */
1581 /* Vdbus_registered_objects_table requires non-nil interface and
1583 if ((interface
== NULL
) || (member
== NULL
))
1586 /* Search for a registered function of the message. */
1587 key
= list4 ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1588 ? QCdbus_registered_method
1589 : QCdbus_registered_signal
,
1590 bus
, build_string (interface
), build_string (member
));
1591 value
= Fgethash (key
, Vdbus_registered_objects_table
, Qnil
);
1593 /* Loop over the registered functions. Construct an event. */
1594 while (!NILP (value
))
1596 key
= CAR_SAFE (value
);
1597 /* key has the structure (UNAME SERVICE PATH HANDLER). */
1598 if (((uname
== NULL
)
1599 || (NILP (CAR_SAFE (key
)))
1600 || (strcmp (uname
, SSDATA (CAR_SAFE (key
))) == 0))
1602 || (NILP (CAR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))
1604 SSDATA (CAR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))
1606 && (!NILP (CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))))
1609 event
.kind
= DBUS_EVENT
;
1610 event
.frame_or_window
= Qnil
;
1612 = Fcons (CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (key
)))), args
);
1615 value
= CDR_SAFE (value
);
1622 /* Add type, serial, uname, path, interface and member to the event. */
1623 event
.arg
= Fcons ((member
== NULL
? Qnil
: build_string (member
)),
1625 event
.arg
= Fcons ((interface
== NULL
? Qnil
: build_string (interface
)),
1627 event
.arg
= Fcons ((path
== NULL
? Qnil
: build_string (path
)),
1629 event
.arg
= Fcons ((uname
== NULL
? Qnil
: build_string (uname
)),
1631 event
.arg
= Fcons (make_fixnum_or_float (serial
), event
.arg
);
1632 event
.arg
= Fcons (make_number (mtype
), event
.arg
);
1634 /* Add the bus symbol to the event. */
1635 event
.arg
= Fcons (bus
, event
.arg
);
1637 /* Store it into the input event queue. */
1638 kbd_buffer_store_event (&event
);
1640 XD_DEBUG_MESSAGE ("Event stored: %s", XD_OBJECT_TO_STRING (event
.arg
));
1644 dbus_message_unref (dmessage
);
1649 /* Read queued incoming messages of the D-Bus BUS.
1650 BUS is either a Lisp symbol, :system or :session, or a string denoting
1653 xd_read_message (Lisp_Object bus
)
1655 /* Retrieve bus address. */
1656 DBusConnection
*connection
= xd_get_connection_address (bus
);
1658 /* Non blocking read of the next available message. */
1659 dbus_connection_read_write (connection
, 0);
1661 while (dbus_connection_get_dispatch_status (connection
)
1662 != DBUS_DISPATCH_COMPLETE
)
1663 xd_read_message_1 (connection
, bus
);
1667 /* Callback called when something is ready to read or write. */
1669 xd_read_queued_messages (int fd
, void *data
, int for_read
)
1671 Lisp_Object busp
= xd_registered_buses
;
1672 Lisp_Object bus
= Qnil
;
1675 /* Find bus related to fd. */
1677 while (!NILP (busp
))
1679 key
= CAR_SAFE (CAR_SAFE (busp
));
1680 if ((SYMBOLP (key
) && XSYMBOL (key
) == data
)
1681 || (STRINGP (key
) && XSTRING (key
) == data
))
1683 busp
= CDR_SAFE (busp
);
1689 /* We ignore all Lisp errors during the call. */
1690 xd_in_read_queued_messages
= 1;
1691 internal_catch (Qdbus_error
, xd_read_message
, bus
);
1692 xd_in_read_queued_messages
= 0;
1697 syms_of_dbusbind (void)
1700 DEFSYM (Qdbus_init_bus
, "dbus-init-bus");
1701 defsubr (&Sdbus_init_bus
);
1703 DEFSYM (Qdbus_get_unique_name
, "dbus-get-unique-name");
1704 defsubr (&Sdbus_get_unique_name
);
1706 DEFSYM (Qdbus_message_internal
, "dbus-message-internal");
1707 defsubr (&Sdbus_message_internal
);
1709 DEFSYM (Qdbus_error
, "dbus-error");
1710 Fput (Qdbus_error
, Qerror_conditions
,
1711 list2 (Qdbus_error
, Qerror
));
1712 Fput (Qdbus_error
, Qerror_message
,
1713 make_pure_c_string ("D-Bus error"));
1715 DEFSYM (QCdbus_system_bus
, ":system");
1716 DEFSYM (QCdbus_session_bus
, ":session");
1717 DEFSYM (QCdbus_timeout
, ":timeout");
1718 DEFSYM (QCdbus_type_byte
, ":byte");
1719 DEFSYM (QCdbus_type_boolean
, ":boolean");
1720 DEFSYM (QCdbus_type_int16
, ":int16");
1721 DEFSYM (QCdbus_type_uint16
, ":uint16");
1722 DEFSYM (QCdbus_type_int32
, ":int32");
1723 DEFSYM (QCdbus_type_uint32
, ":uint32");
1724 DEFSYM (QCdbus_type_int64
, ":int64");
1725 DEFSYM (QCdbus_type_uint64
, ":uint64");
1726 DEFSYM (QCdbus_type_double
, ":double");
1727 DEFSYM (QCdbus_type_string
, ":string");
1728 DEFSYM (QCdbus_type_object_path
, ":object-path");
1729 DEFSYM (QCdbus_type_signature
, ":signature");
1730 #ifdef DBUS_TYPE_UNIX_FD
1731 DEFSYM (QCdbus_type_unix_fd
, ":unix-fd");
1733 DEFSYM (QCdbus_type_array
, ":array");
1734 DEFSYM (QCdbus_type_variant
, ":variant");
1735 DEFSYM (QCdbus_type_struct
, ":struct");
1736 DEFSYM (QCdbus_type_dict_entry
, ":dict-entry");
1737 DEFSYM (QCdbus_registered_serial
, ":serial");
1738 DEFSYM (QCdbus_registered_method
, ":method");
1739 DEFSYM (QCdbus_registered_signal
, ":signal");
1741 DEFVAR_LISP ("dbus-compiled-version",
1742 Vdbus_compiled_version
,
1743 doc
: /* The version of D-Bus Emacs is compiled against. */);
1744 #ifdef DBUS_VERSION_STRING
1745 Vdbus_compiled_version
= make_pure_c_string (DBUS_VERSION_STRING
);
1747 Vdbus_compiled_version
= Qnil
;
1750 DEFVAR_LISP ("dbus-runtime-version",
1751 Vdbus_runtime_version
,
1752 doc
: /* The version of D-Bus Emacs runs with. */);
1755 int major
, minor
, micro
;
1757 dbus_get_version (&major
, &minor
, µ
);
1758 snprintf (s
, sizeof s
, "%d.%d.%d", major
, minor
, micro
);
1759 Vdbus_runtime_version
= make_string (s
, strlen (s
));
1761 Vdbus_runtime_version
= Qnil
;
1765 DEFVAR_LISP ("dbus-message-type-invalid",
1766 Vdbus_message_type_invalid
,
1767 doc
: /* This value is never a valid message type. */);
1768 Vdbus_message_type_invalid
= make_number (DBUS_MESSAGE_TYPE_INVALID
);
1770 DEFVAR_LISP ("dbus-message-type-method-call",
1771 Vdbus_message_type_method_call
,
1772 doc
: /* Message type of a method call message. */);
1773 Vdbus_message_type_method_call
= make_number (DBUS_MESSAGE_TYPE_METHOD_CALL
);
1775 DEFVAR_LISP ("dbus-message-type-method-return",
1776 Vdbus_message_type_method_return
,
1777 doc
: /* Message type of a method return message. */);
1778 Vdbus_message_type_method_return
1779 = make_number (DBUS_MESSAGE_TYPE_METHOD_RETURN
);
1781 DEFVAR_LISP ("dbus-message-type-error",
1782 Vdbus_message_type_error
,
1783 doc
: /* Message type of an error reply message. */);
1784 Vdbus_message_type_error
= make_number (DBUS_MESSAGE_TYPE_ERROR
);
1786 DEFVAR_LISP ("dbus-message-type-signal",
1787 Vdbus_message_type_signal
,
1788 doc
: /* Message type of a signal message. */);
1789 Vdbus_message_type_signal
= make_number (DBUS_MESSAGE_TYPE_SIGNAL
);
1791 DEFVAR_LISP ("dbus-registered-objects-table",
1792 Vdbus_registered_objects_table
,
1793 doc
: /* Hash table of registered functions for D-Bus.
1795 There are two different uses of the hash table: for accessing
1796 registered interfaces properties, targeted by signals or method calls,
1797 and for calling handlers in case of non-blocking method call returns.
1799 In the first case, the key in the hash table is the list (TYPE BUS
1800 INTERFACE MEMBER). TYPE is one of the Lisp symbols `:method',
1801 `:signal' or `:property'. BUS is either a Lisp symbol, `:system' or
1802 `:session', or a string denoting the bus address. INTERFACE is a
1803 string which denotes a D-Bus interface, and MEMBER, also a string, is
1804 either a method, a signal or a property INTERFACE is offering. All
1805 arguments but BUS must not be nil.
1807 The value in the hash table is a list of quadruple lists \((UNAME
1808 SERVICE PATH OBJECT [RULE]) ...). SERVICE is the service name as
1809 registered, UNAME is the corresponding unique name. In case of
1810 registered methods and properties, UNAME is nil. PATH is the object
1811 path of the sending object. All of them can be nil, which means a
1812 wildcard then. OBJECT is either the handler to be called when a D-Bus
1813 message, which matches the key criteria, arrives (TYPE `:method' and
1814 `:signal'), or a cons cell containing the value of the property (TYPE
1817 For entries of type `:signal', there is also a fifth element RULE,
1818 which keeps the match string the signal is registered with.
1820 In the second case, the key in the hash table is the list (:serial BUS
1821 SERIAL). BUS is either a Lisp symbol, `:system' or `:session', or a
1822 string denoting the bus address. SERIAL is the serial number of the
1823 non-blocking method call, a reply is expected. Both arguments must
1824 not be nil. The value in the hash table is HANDLER, the function to
1825 be called when the D-Bus reply message arrives. */);
1827 Lisp_Object args
[2];
1830 Vdbus_registered_objects_table
= Fmake_hash_table (2, args
);
1833 DEFVAR_LISP ("dbus-debug", Vdbus_debug
,
1834 doc
: /* If non-nil, debug messages of D-Bus bindings are raised. */);
1837 /* We can also set environment variable DBUS_VERBOSE=1 in order to
1838 see more traces. This requires libdbus-1 to be configured with
1839 --enable-verbose-mode. */
1844 /* Initialize internal objects. */
1845 xd_registered_buses
= Qnil
;
1846 staticpro (&xd_registered_buses
);
1848 Fprovide (intern_c_string ("dbusbind"), Qnil
);
1852 #endif /* HAVE_DBUS */