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
41 static Lisp_Object Qdbus_init_bus
;
42 static Lisp_Object Qdbus_get_unique_name
;
43 static Lisp_Object Qdbus_message_internal
;
45 /* D-Bus error symbol. */
46 static Lisp_Object Qdbus_error
;
48 /* Lisp symbols of the system and session buses. */
49 static Lisp_Object QCdbus_system_bus
, QCdbus_session_bus
;
51 /* Lisp symbol for method call timeout. */
52 static Lisp_Object QCdbus_timeout
;
54 /* Lisp symbols of D-Bus types. */
55 static Lisp_Object QCdbus_type_byte
, QCdbus_type_boolean
;
56 static Lisp_Object QCdbus_type_int16
, QCdbus_type_uint16
;
57 static Lisp_Object QCdbus_type_int32
, QCdbus_type_uint32
;
58 static Lisp_Object QCdbus_type_int64
, QCdbus_type_uint64
;
59 static Lisp_Object QCdbus_type_double
, QCdbus_type_string
;
60 static Lisp_Object QCdbus_type_object_path
, QCdbus_type_signature
;
61 #ifdef DBUS_TYPE_UNIX_FD
62 static Lisp_Object QCdbus_type_unix_fd
;
64 static Lisp_Object QCdbus_type_array
, QCdbus_type_variant
;
65 static Lisp_Object QCdbus_type_struct
, QCdbus_type_dict_entry
;
67 /* Lisp symbols of objects in `dbus-registered-objects-table'. */
68 static Lisp_Object QCdbus_registered_serial
, QCdbus_registered_method
;
69 static Lisp_Object QCdbus_registered_signal
;
71 /* Alist of D-Bus buses we are polling for messages.
72 The key is the symbol or string of the bus, and the value is the
73 connection address. */
74 static Lisp_Object xd_registered_buses
;
76 /* Whether we are reading a D-Bus event. */
77 static bool xd_in_read_queued_messages
= 0;
80 /* We use "xd_" and "XD_" as prefix for all internal symbols, because
81 we don't want to poison other namespaces with "dbus_". */
83 /* Raise a signal. If we are reading events, we cannot signal; we
84 throw to xd_read_queued_messages then. */
85 #define XD_SIGNAL1(arg) \
87 if (xd_in_read_queued_messages) \
88 Fthrow (Qdbus_error, Qnil); \
90 xsignal1 (Qdbus_error, arg); \
93 #define XD_SIGNAL2(arg1, arg2) \
95 if (xd_in_read_queued_messages) \
96 Fthrow (Qdbus_error, Qnil); \
98 xsignal2 (Qdbus_error, arg1, arg2); \
101 #define XD_SIGNAL3(arg1, arg2, arg3) \
103 if (xd_in_read_queued_messages) \
104 Fthrow (Qdbus_error, Qnil); \
106 xsignal3 (Qdbus_error, arg1, arg2, arg3); \
109 /* Raise a Lisp error from a D-Bus ERROR. */
110 #define XD_ERROR(error) \
112 /* Remove the trailing newline. */ \
113 char const *mess = error.message; \
114 char const *nl = strchr (mess, '\n'); \
115 Lisp_Object err = make_string (mess, nl ? nl - mess : strlen (mess)); \
116 dbus_error_free (&error); \
120 /* Macros for debugging. In order to enable them, build with
121 "env MYCPPFLAGS='-DDBUS_DEBUG -Wall' make". */
123 #define XD_DEBUG_MESSAGE(...) \
126 snprintf (s, sizeof s, __VA_ARGS__); \
127 if (!noninteractive) \
128 printf ("%s: %s\n", __func__, s); \
129 message ("%s: %s", __func__, s); \
131 #define XD_DEBUG_VALID_LISP_OBJECT_P(object) \
133 if (!valid_lisp_object_p (object)) \
135 XD_DEBUG_MESSAGE ("%d Assertion failure", __LINE__); \
136 XD_SIGNAL1 (build_string ("Assertion failure")); \
140 #else /* !DBUS_DEBUG */
141 #define XD_DEBUG_MESSAGE(...) \
143 if (!NILP (Vdbus_debug)) \
146 snprintf (s, sizeof s, __VA_ARGS__); \
147 message ("%s: %s", __func__, s); \
150 #define XD_DEBUG_VALID_LISP_OBJECT_P(object)
153 /* Check whether TYPE is a basic DBusType. */
154 #ifdef HAVE_DBUS_TYPE_IS_VALID
155 #define XD_BASIC_DBUS_TYPE(type) \
156 (dbus_type_is_valid (type) && dbus_type_is_basic (type))
158 #ifdef DBUS_TYPE_UNIX_FD
159 #define XD_BASIC_DBUS_TYPE(type) \
160 ((type == DBUS_TYPE_BYTE) \
161 || (type == DBUS_TYPE_BOOLEAN) \
162 || (type == DBUS_TYPE_INT16) \
163 || (type == DBUS_TYPE_UINT16) \
164 || (type == DBUS_TYPE_INT32) \
165 || (type == DBUS_TYPE_UINT32) \
166 || (type == DBUS_TYPE_INT64) \
167 || (type == DBUS_TYPE_UINT64) \
168 || (type == DBUS_TYPE_DOUBLE) \
169 || (type == DBUS_TYPE_STRING) \
170 || (type == DBUS_TYPE_OBJECT_PATH) \
171 || (type == DBUS_TYPE_SIGNATURE) \
172 || (type == DBUS_TYPE_UNIX_FD))
174 #define XD_BASIC_DBUS_TYPE(type) \
175 ((type == DBUS_TYPE_BYTE) \
176 || (type == DBUS_TYPE_BOOLEAN) \
177 || (type == DBUS_TYPE_INT16) \
178 || (type == DBUS_TYPE_UINT16) \
179 || (type == DBUS_TYPE_INT32) \
180 || (type == DBUS_TYPE_UINT32) \
181 || (type == DBUS_TYPE_INT64) \
182 || (type == DBUS_TYPE_UINT64) \
183 || (type == DBUS_TYPE_DOUBLE) \
184 || (type == DBUS_TYPE_STRING) \
185 || (type == DBUS_TYPE_OBJECT_PATH) \
186 || (type == DBUS_TYPE_SIGNATURE))
190 /* This was a macro. On Solaris 2.11 it was said to compile for
191 hours, when optimization is enabled. So we have transferred it into
193 /* Determine the DBusType of a given Lisp symbol. OBJECT must be one
194 of the predefined D-Bus type symbols. */
196 xd_symbol_to_dbus_type (Lisp_Object object
)
199 ((EQ (object
, QCdbus_type_byte
)) ? DBUS_TYPE_BYTE
200 : (EQ (object
, QCdbus_type_boolean
)) ? DBUS_TYPE_BOOLEAN
201 : (EQ (object
, QCdbus_type_int16
)) ? DBUS_TYPE_INT16
202 : (EQ (object
, QCdbus_type_uint16
)) ? DBUS_TYPE_UINT16
203 : (EQ (object
, QCdbus_type_int32
)) ? DBUS_TYPE_INT32
204 : (EQ (object
, QCdbus_type_uint32
)) ? DBUS_TYPE_UINT32
205 : (EQ (object
, QCdbus_type_int64
)) ? DBUS_TYPE_INT64
206 : (EQ (object
, QCdbus_type_uint64
)) ? DBUS_TYPE_UINT64
207 : (EQ (object
, QCdbus_type_double
)) ? DBUS_TYPE_DOUBLE
208 : (EQ (object
, QCdbus_type_string
)) ? DBUS_TYPE_STRING
209 : (EQ (object
, QCdbus_type_object_path
)) ? DBUS_TYPE_OBJECT_PATH
210 : (EQ (object
, QCdbus_type_signature
)) ? DBUS_TYPE_SIGNATURE
211 #ifdef DBUS_TYPE_UNIX_FD
212 : (EQ (object
, QCdbus_type_unix_fd
)) ? DBUS_TYPE_UNIX_FD
214 : (EQ (object
, QCdbus_type_array
)) ? DBUS_TYPE_ARRAY
215 : (EQ (object
, QCdbus_type_variant
)) ? DBUS_TYPE_VARIANT
216 : (EQ (object
, QCdbus_type_struct
)) ? DBUS_TYPE_STRUCT
217 : (EQ (object
, QCdbus_type_dict_entry
)) ? DBUS_TYPE_DICT_ENTRY
218 : DBUS_TYPE_INVALID
);
221 /* Check whether a Lisp symbol is a predefined D-Bus type symbol. */
222 #define XD_DBUS_TYPE_P(object) \
223 (SYMBOLP (object) && ((xd_symbol_to_dbus_type (object) != DBUS_TYPE_INVALID)))
225 /* Determine the DBusType of a given Lisp OBJECT. It is used to
226 convert Lisp objects, being arguments of `dbus-call-method' or
227 `dbus-send-signal', into corresponding C values appended as
228 arguments to a D-Bus message. */
229 #define XD_OBJECT_TO_DBUS_TYPE(object) \
230 ((EQ (object, Qt) || EQ (object, Qnil)) ? DBUS_TYPE_BOOLEAN \
231 : (NATNUMP (object)) ? DBUS_TYPE_UINT32 \
232 : (INTEGERP (object)) ? DBUS_TYPE_INT32 \
233 : (FLOATP (object)) ? DBUS_TYPE_DOUBLE \
234 : (STRINGP (object)) ? DBUS_TYPE_STRING \
235 : (XD_DBUS_TYPE_P (object)) ? xd_symbol_to_dbus_type (object) \
237 ? ((XD_DBUS_TYPE_P (CAR_SAFE (object))) \
238 ? ((XD_BASIC_DBUS_TYPE (xd_symbol_to_dbus_type (CAR_SAFE (object)))) \
240 : xd_symbol_to_dbus_type (CAR_SAFE (object))) \
244 /* Return a list pointer which does not have a Lisp symbol as car. */
245 #define XD_NEXT_VALUE(object) \
246 ((XD_DBUS_TYPE_P (CAR_SAFE (object))) ? CDR_SAFE (object) : object)
248 /* Transform the message type to its string representation for debug
250 #define XD_MESSAGE_TYPE_TO_STRING(mtype) \
251 ((mtype == DBUS_MESSAGE_TYPE_INVALID) \
252 ? "DBUS_MESSAGE_TYPE_INVALID" \
253 : (mtype == DBUS_MESSAGE_TYPE_METHOD_CALL) \
254 ? "DBUS_MESSAGE_TYPE_METHOD_CALL" \
255 : (mtype == DBUS_MESSAGE_TYPE_METHOD_RETURN) \
256 ? "DBUS_MESSAGE_TYPE_METHOD_RETURN" \
257 : (mtype == DBUS_MESSAGE_TYPE_ERROR) \
258 ? "DBUS_MESSAGE_TYPE_ERROR" \
259 : "DBUS_MESSAGE_TYPE_SIGNAL")
261 /* Transform the object to its string representation for debug
263 #define XD_OBJECT_TO_STRING(object) \
264 SDATA (format2 ("%s", object, Qnil))
266 #define XD_DBUS_VALIDATE_BUS_ADDRESS(bus) \
268 char const *session_bus_address = getenv ("DBUS_SESSION_BUS_ADDRESS"); \
271 DBusAddressEntry **entries; \
274 dbus_error_init (&derror); \
275 if (!dbus_parse_address (SSDATA (bus), &entries, &len, &derror)) \
278 dbus_error_free (&derror); \
279 dbus_address_entries_free (entries); \
280 /* Canonicalize session bus address. */ \
281 if ((session_bus_address != NULL) \
282 && (!NILP (Fstring_equal \
283 (bus, build_string (session_bus_address))))) \
284 bus = QCdbus_session_bus; \
289 CHECK_SYMBOL (bus); \
290 if (!(EQ (bus, QCdbus_system_bus) || EQ (bus, QCdbus_session_bus))) \
291 XD_SIGNAL2 (build_string ("Wrong bus name"), bus); \
292 /* We do not want to have an autolaunch for the session bus. */ \
293 if (EQ (bus, QCdbus_session_bus) && session_bus_address == NULL) \
294 XD_SIGNAL2 (build_string ("No connection to bus"), bus); \
298 #if (HAVE_DBUS_VALIDATE_BUS_NAME || HAVE_DBUS_VALIDATE_PATH \
299 || XD_DBUS_VALIDATE_OBJECT || HAVE_DBUS_VALIDATE_MEMBER)
300 #define XD_DBUS_VALIDATE_OBJECT(object, func) \
302 if (!NILP (object)) \
305 CHECK_STRING (object); \
306 dbus_error_init (&derror); \
307 if (!func (SSDATA (object), &derror)) \
310 dbus_error_free (&derror); \
315 #if HAVE_DBUS_VALIDATE_BUS_NAME
316 #define XD_DBUS_VALIDATE_BUS_NAME(bus_name) \
317 XD_DBUS_VALIDATE_OBJECT(bus_name, dbus_validate_bus_name);
319 #define XD_DBUS_VALIDATE_BUS_NAME(bus_name) \
320 if (!NILP (bus_name)) CHECK_STRING (bus_name);
323 #if HAVE_DBUS_VALIDATE_PATH
324 #define XD_DBUS_VALIDATE_PATH(path) \
325 XD_DBUS_VALIDATE_OBJECT(path, dbus_validate_path);
327 #define XD_DBUS_VALIDATE_PATH(path) \
328 if (!NILP (path)) CHECK_STRING (path);
331 #if HAVE_DBUS_VALIDATE_INTERFACE
332 #define XD_DBUS_VALIDATE_INTERFACE(interface) \
333 XD_DBUS_VALIDATE_OBJECT(interface, dbus_validate_interface);
335 #define XD_DBUS_VALIDATE_INTERFACE(interface) \
336 if (!NILP (interface)) CHECK_STRING (interface);
339 #if HAVE_DBUS_VALIDATE_MEMBER
340 #define XD_DBUS_VALIDATE_MEMBER(member) \
341 XD_DBUS_VALIDATE_OBJECT(member, dbus_validate_member);
343 #define XD_DBUS_VALIDATE_MEMBER(member) \
344 if (!NILP (member)) CHECK_STRING (member);
347 /* Append to SIGNATURE a copy of X, making sure SIGNATURE does
348 not become too long. */
350 xd_signature_cat (char *signature
, char const *x
)
352 ptrdiff_t siglen
= strlen (signature
);
353 ptrdiff_t xlen
= strlen (x
);
354 if (DBUS_MAXIMUM_SIGNATURE_LENGTH
- xlen
<= siglen
)
356 strcat (signature
, x
);
359 /* Compute SIGNATURE of OBJECT. It must have a form that it can be
360 used in dbus_message_iter_open_container. DTYPE is the DBusType
361 the object is related to. It is passed as argument, because it
362 cannot be detected in basic type objects, when they are preceded by
363 a type symbol. PARENT_TYPE is the DBusType of a container this
364 signature is embedded, or DBUS_TYPE_INVALID. It is needed for the
365 check that DBUS_TYPE_DICT_ENTRY occurs only as array element. */
367 xd_signature (char *signature
, int dtype
, int parent_type
, Lisp_Object object
)
373 char x
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
380 case DBUS_TYPE_UINT16
:
381 CHECK_NATNUM (object
);
382 sprintf (signature
, "%c", dtype
);
385 case DBUS_TYPE_BOOLEAN
:
386 if (!EQ (object
, Qt
) && !EQ (object
, Qnil
))
387 wrong_type_argument (intern ("booleanp"), object
);
388 sprintf (signature
, "%c", dtype
);
391 case DBUS_TYPE_INT16
:
392 CHECK_NUMBER (object
);
393 sprintf (signature
, "%c", dtype
);
396 case DBUS_TYPE_UINT32
:
397 case DBUS_TYPE_UINT64
:
398 #ifdef DBUS_TYPE_UNIX_FD
399 case DBUS_TYPE_UNIX_FD
:
401 case DBUS_TYPE_INT32
:
402 case DBUS_TYPE_INT64
:
403 case DBUS_TYPE_DOUBLE
:
404 CHECK_NUMBER_OR_FLOAT (object
);
405 sprintf (signature
, "%c", dtype
);
408 case DBUS_TYPE_STRING
:
409 case DBUS_TYPE_OBJECT_PATH
:
410 case DBUS_TYPE_SIGNATURE
:
411 CHECK_STRING (object
);
412 sprintf (signature
, "%c", dtype
);
415 case DBUS_TYPE_ARRAY
:
416 /* Check that all list elements have the same D-Bus type. For
417 complex element types, we just check the container type, not
418 the whole element's signature. */
421 /* Type symbol is optional. */
422 if (EQ (QCdbus_type_array
, CAR_SAFE (elt
)))
423 elt
= XD_NEXT_VALUE (elt
);
425 /* If the array is empty, DBUS_TYPE_STRING is the default
429 subtype
= DBUS_TYPE_STRING
;
430 subsig
= DBUS_TYPE_STRING_AS_STRING
;
434 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
435 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
439 /* If the element type is DBUS_TYPE_SIGNATURE, and this is the
440 only element, the value of this element is used as the
441 array's element signature. */
442 if ((subtype
== DBUS_TYPE_SIGNATURE
)
443 && STRINGP (CAR_SAFE (XD_NEXT_VALUE (elt
)))
444 && NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
445 subsig
= SSDATA (CAR_SAFE (XD_NEXT_VALUE (elt
)));
449 if (subtype
!= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
)))
450 wrong_type_argument (intern ("D-Bus"), CAR_SAFE (elt
));
451 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
454 subsiglen
= snprintf (signature
, DBUS_MAXIMUM_SIGNATURE_LENGTH
,
455 "%c%s", dtype
, subsig
);
456 if (! (0 <= subsiglen
&& subsiglen
< DBUS_MAXIMUM_SIGNATURE_LENGTH
))
460 case DBUS_TYPE_VARIANT
:
461 /* Check that there is exactly one list element. */
464 elt
= XD_NEXT_VALUE (elt
);
465 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
466 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
468 if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
469 wrong_type_argument (intern ("D-Bus"),
470 CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt
))));
472 sprintf (signature
, "%c", dtype
);
475 case DBUS_TYPE_STRUCT
:
476 /* A struct list might contain any number of elements with
477 different types. No further check needed. */
480 elt
= XD_NEXT_VALUE (elt
);
482 /* Compose the signature from the elements. It is enclosed by
484 sprintf (signature
, "%c", DBUS_STRUCT_BEGIN_CHAR
);
487 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
488 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
489 xd_signature_cat (signature
, x
);
490 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
492 xd_signature_cat (signature
, DBUS_STRUCT_END_CHAR_AS_STRING
);
495 case DBUS_TYPE_DICT_ENTRY
:
496 /* Check that there are exactly two list elements, and the first
497 one is of basic type. The dictionary entry itself must be an
498 element of an array. */
501 /* Check the parent object type. */
502 if (parent_type
!= DBUS_TYPE_ARRAY
)
503 wrong_type_argument (intern ("D-Bus"), object
);
505 /* Compose the signature from the elements. It is enclosed by
507 sprintf (signature
, "%c", DBUS_DICT_ENTRY_BEGIN_CHAR
);
510 elt
= XD_NEXT_VALUE (elt
);
511 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
512 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
513 xd_signature_cat (signature
, x
);
515 if (!XD_BASIC_DBUS_TYPE (subtype
))
516 wrong_type_argument (intern ("D-Bus"), CAR_SAFE (XD_NEXT_VALUE (elt
)));
518 /* Second element. */
519 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
520 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
521 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
522 xd_signature_cat (signature
, x
);
524 if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
525 wrong_type_argument (intern ("D-Bus"),
526 CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt
))));
528 /* Closing signature. */
529 xd_signature_cat (signature
, DBUS_DICT_ENTRY_END_CHAR_AS_STRING
);
533 wrong_type_argument (intern ("D-Bus"), object
);
536 XD_DEBUG_MESSAGE ("%s", signature
);
539 /* Convert X to a signed integer with bounds LO and HI. */
541 xd_extract_signed (Lisp_Object x
, intmax_t lo
, intmax_t hi
)
543 CHECK_NUMBER_OR_FLOAT (x
);
546 if (lo
<= XINT (x
) && XINT (x
) <= hi
)
551 double d
= XFLOAT_DATA (x
);
552 if (lo
<= d
&& d
<= hi
)
559 if (xd_in_read_queued_messages
)
560 Fthrow (Qdbus_error
, Qnil
);
562 args_out_of_range_3 (x
,
563 make_fixnum_or_float (lo
),
564 make_fixnum_or_float (hi
));
567 /* Convert X to an unsigned integer with bounds 0 and HI. */
569 xd_extract_unsigned (Lisp_Object x
, uintmax_t hi
)
571 CHECK_NUMBER_OR_FLOAT (x
);
574 if (0 <= XINT (x
) && XINT (x
) <= hi
)
579 double d
= XFLOAT_DATA (x
);
580 if (0 <= d
&& d
<= hi
)
587 if (xd_in_read_queued_messages
)
588 Fthrow (Qdbus_error
, Qnil
);
590 args_out_of_range_3 (x
, make_number (0), make_fixnum_or_float (hi
));
593 /* Append C value, extracted from Lisp OBJECT, to iteration ITER.
594 DTYPE must be a valid DBusType. It is used to convert Lisp
595 objects, being arguments of `dbus-call-method' or
596 `dbus-send-signal', into corresponding C values appended as
597 arguments to a D-Bus message. */
599 xd_append_arg (int dtype
, Lisp_Object object
, DBusMessageIter
*iter
)
601 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
602 DBusMessageIter subiter
;
604 if (XD_BASIC_DBUS_TYPE (dtype
))
608 CHECK_NATNUM (object
);
610 unsigned char val
= XFASTINT (object
) & 0xFF;
611 XD_DEBUG_MESSAGE ("%c %u", dtype
, val
);
612 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
613 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
617 case DBUS_TYPE_BOOLEAN
:
619 dbus_bool_t val
= (NILP (object
)) ? FALSE
: TRUE
;
620 XD_DEBUG_MESSAGE ("%c %s", dtype
, (val
== FALSE
) ? "false" : "true");
621 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
622 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
626 case DBUS_TYPE_INT16
:
629 xd_extract_signed (object
,
630 TYPE_MINIMUM (dbus_int16_t
),
631 TYPE_MAXIMUM (dbus_int16_t
));
633 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
634 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
635 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
639 case DBUS_TYPE_UINT16
:
642 xd_extract_unsigned (object
,
643 TYPE_MAXIMUM (dbus_uint16_t
));
644 unsigned int pval
= val
;
645 XD_DEBUG_MESSAGE ("%c %u", dtype
, pval
);
646 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
647 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
651 case DBUS_TYPE_INT32
:
654 xd_extract_signed (object
,
655 TYPE_MINIMUM (dbus_int32_t
),
656 TYPE_MAXIMUM (dbus_int32_t
));
658 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
659 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
660 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
664 case DBUS_TYPE_UINT32
:
665 #ifdef DBUS_TYPE_UNIX_FD
666 case DBUS_TYPE_UNIX_FD
:
670 xd_extract_unsigned (object
,
671 TYPE_MAXIMUM (dbus_uint32_t
));
672 unsigned int pval
= val
;
673 XD_DEBUG_MESSAGE ("%c %u", dtype
, pval
);
674 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
675 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
679 case DBUS_TYPE_INT64
:
682 xd_extract_signed (object
,
683 TYPE_MINIMUM (dbus_int64_t
),
684 TYPE_MAXIMUM (dbus_int64_t
));
685 printmax_t pval
= val
;
686 XD_DEBUG_MESSAGE ("%c %"pMd
, dtype
, pval
);
687 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
688 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
692 case DBUS_TYPE_UINT64
:
695 xd_extract_unsigned (object
,
696 TYPE_MAXIMUM (dbus_uint64_t
));
697 uprintmax_t pval
= val
;
698 XD_DEBUG_MESSAGE ("%c %"pMu
, dtype
, pval
);
699 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
700 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
704 case DBUS_TYPE_DOUBLE
:
706 double val
= extract_float (object
);
707 XD_DEBUG_MESSAGE ("%c %f", dtype
, val
);
708 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
709 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
713 case DBUS_TYPE_STRING
:
714 case DBUS_TYPE_OBJECT_PATH
:
715 case DBUS_TYPE_SIGNATURE
:
716 CHECK_STRING (object
);
718 /* We need to send a valid UTF-8 string. We could encode `object'
719 but by not encoding it, we guarantee it's valid utf-8, even if
720 it contains eight-bit-bytes. Of course, you can still send
721 manually-crafted junk by passing a unibyte string. */
722 char *val
= SSDATA (object
);
723 XD_DEBUG_MESSAGE ("%c %s", dtype
, val
);
724 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
725 XD_SIGNAL2 (build_string ("Unable to append argument"), object
);
730 else /* Compound types. */
733 /* All compound types except array have a type symbol. For
734 array, it is optional. Skip it. */
735 if (!XD_BASIC_DBUS_TYPE (XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
))))
736 object
= XD_NEXT_VALUE (object
);
738 /* Open new subiteration. */
741 case DBUS_TYPE_ARRAY
:
742 /* An array has only elements of the same type. So it is
743 sufficient to check the first element's signature
747 /* If the array is empty, DBUS_TYPE_STRING is the default
749 strcpy (signature
, DBUS_TYPE_STRING_AS_STRING
);
752 /* If the element type is DBUS_TYPE_SIGNATURE, and this is
753 the only element, the value of this element is used as
754 the array's element signature. */
755 if ((XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
))
756 == DBUS_TYPE_SIGNATURE
)
757 && STRINGP (CAR_SAFE (XD_NEXT_VALUE (object
)))
758 && NILP (CDR_SAFE (XD_NEXT_VALUE (object
))))
760 strcpy (signature
, SSDATA (CAR_SAFE (XD_NEXT_VALUE (object
))));
761 object
= CDR_SAFE (XD_NEXT_VALUE (object
));
765 xd_signature (signature
,
766 XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
)),
767 dtype
, CAR_SAFE (XD_NEXT_VALUE (object
)));
769 XD_DEBUG_MESSAGE ("%c %s %s", dtype
, signature
,
770 XD_OBJECT_TO_STRING (object
));
771 if (!dbus_message_iter_open_container (iter
, dtype
,
772 signature
, &subiter
))
773 XD_SIGNAL3 (build_string ("Cannot open container"),
774 make_number (dtype
), build_string (signature
));
777 case DBUS_TYPE_VARIANT
:
778 /* A variant has just one element. */
779 xd_signature (signature
, XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
)),
780 dtype
, CAR_SAFE (XD_NEXT_VALUE (object
)));
782 XD_DEBUG_MESSAGE ("%c %s %s", dtype
, signature
,
783 XD_OBJECT_TO_STRING (object
));
784 if (!dbus_message_iter_open_container (iter
, dtype
,
785 signature
, &subiter
))
786 XD_SIGNAL3 (build_string ("Cannot open container"),
787 make_number (dtype
), build_string (signature
));
790 case DBUS_TYPE_STRUCT
:
791 case DBUS_TYPE_DICT_ENTRY
:
792 /* These containers do not require a signature. */
793 XD_DEBUG_MESSAGE ("%c %s", dtype
, XD_OBJECT_TO_STRING (object
));
794 if (!dbus_message_iter_open_container (iter
, dtype
, NULL
, &subiter
))
795 XD_SIGNAL2 (build_string ("Cannot open container"),
796 make_number (dtype
));
800 /* Loop over list elements. */
801 while (!NILP (object
))
803 dtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
));
804 object
= XD_NEXT_VALUE (object
);
806 xd_append_arg (dtype
, CAR_SAFE (object
), &subiter
);
808 object
= CDR_SAFE (object
);
811 /* Close the subiteration. */
812 if (!dbus_message_iter_close_container (iter
, &subiter
))
813 XD_SIGNAL2 (build_string ("Cannot close container"),
814 make_number (dtype
));
818 /* Retrieve C value from a DBusMessageIter structure ITER, and return
819 a converted Lisp object. The type DTYPE of the argument of the
820 D-Bus message must be a valid DBusType. Compound D-Bus types
821 result always in a Lisp list. */
823 xd_retrieve_arg (int dtype
, DBusMessageIter
*iter
)
831 dbus_message_iter_get_basic (iter
, &val
);
833 XD_DEBUG_MESSAGE ("%c %u", dtype
, val
);
834 return make_number (val
);
837 case DBUS_TYPE_BOOLEAN
:
840 dbus_message_iter_get_basic (iter
, &val
);
841 XD_DEBUG_MESSAGE ("%c %s", dtype
, (val
== FALSE
) ? "false" : "true");
842 return (val
== FALSE
) ? Qnil
: Qt
;
845 case DBUS_TYPE_INT16
:
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_UINT16
:
859 dbus_message_iter_get_basic (iter
, &val
);
861 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
862 return make_number (val
);
865 case DBUS_TYPE_INT32
:
869 dbus_message_iter_get_basic (iter
, &val
);
871 XD_DEBUG_MESSAGE ("%c %d", dtype
, pval
);
872 return make_fixnum_or_float (val
);
875 case DBUS_TYPE_UINT32
:
876 #ifdef DBUS_TYPE_UNIX_FD
877 case DBUS_TYPE_UNIX_FD
:
881 unsigned int pval
= val
;
882 dbus_message_iter_get_basic (iter
, &val
);
884 XD_DEBUG_MESSAGE ("%c %u", dtype
, pval
);
885 return make_fixnum_or_float (val
);
888 case DBUS_TYPE_INT64
:
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_UINT64
:
902 dbus_message_iter_get_basic (iter
, &val
);
904 XD_DEBUG_MESSAGE ("%c %"pMd
, dtype
, pval
);
905 return make_fixnum_or_float (val
);
908 case DBUS_TYPE_DOUBLE
:
911 dbus_message_iter_get_basic (iter
, &val
);
912 XD_DEBUG_MESSAGE ("%c %f", dtype
, val
);
913 return make_float (val
);
916 case DBUS_TYPE_STRING
:
917 case DBUS_TYPE_OBJECT_PATH
:
918 case DBUS_TYPE_SIGNATURE
:
921 dbus_message_iter_get_basic (iter
, &val
);
922 XD_DEBUG_MESSAGE ("%c %s", dtype
, val
);
923 return build_string (val
);
926 case DBUS_TYPE_ARRAY
:
927 case DBUS_TYPE_VARIANT
:
928 case DBUS_TYPE_STRUCT
:
929 case DBUS_TYPE_DICT_ENTRY
:
933 DBusMessageIter subiter
;
937 dbus_message_iter_recurse (iter
, &subiter
);
938 while ((subtype
= dbus_message_iter_get_arg_type (&subiter
))
939 != DBUS_TYPE_INVALID
)
941 result
= Fcons (xd_retrieve_arg (subtype
, &subiter
), result
);
942 dbus_message_iter_next (&subiter
);
944 XD_DEBUG_MESSAGE ("%c %s", dtype
, XD_OBJECT_TO_STRING (result
));
945 RETURN_UNGCPRO (Fnreverse (result
));
949 XD_DEBUG_MESSAGE ("DBusType '%c' not supported", dtype
);
954 /* Return the number of references of the shared CONNECTION. */
956 xd_get_connection_references (DBusConnection
*connection
)
960 /* We cannot access the DBusConnection structure, it is not public.
961 But we know, that the reference counter is the first field in
963 refcount
= (void *) &connection
;
964 refcount
= (void *) *refcount
;
968 /* Return D-Bus connection address. BUS is either a Lisp symbol,
969 :system or :session, or a string denoting the bus address. */
970 static DBusConnection
*
971 xd_get_connection_address (Lisp_Object bus
)
973 DBusConnection
*connection
;
976 val
= CDR_SAFE (Fassoc (bus
, xd_registered_buses
));
978 XD_SIGNAL2 (build_string ("No connection to bus"), bus
);
980 connection
= (DBusConnection
*) (intptr_t) XFASTINT (val
);
982 if (!dbus_connection_get_is_connected (connection
))
983 XD_SIGNAL2 (build_string ("No connection to bus"), bus
);
988 /* Return the file descriptor for WATCH, -1 if not found. */
990 xd_find_watch_fd (DBusWatch
*watch
)
992 #if HAVE_DBUS_WATCH_GET_UNIX_FD
993 /* TODO: Reverse these on w32, which prefers the opposite. */
994 int fd
= dbus_watch_get_unix_fd (watch
);
996 fd
= dbus_watch_get_socket (watch
);
998 int fd
= dbus_watch_get_fd (watch
);
1004 static void xd_read_queued_messages (int fd
, void *data
);
1006 /* Start monitoring WATCH for possible I/O. */
1008 xd_add_watch (DBusWatch
*watch
, void *data
)
1010 unsigned int flags
= dbus_watch_get_flags (watch
);
1011 int fd
= xd_find_watch_fd (watch
);
1013 XD_DEBUG_MESSAGE ("fd %d, write %d, enabled %d",
1014 fd
, flags
& DBUS_WATCH_WRITABLE
,
1015 dbus_watch_get_enabled (watch
));
1020 if (dbus_watch_get_enabled (watch
))
1022 if (flags
& DBUS_WATCH_WRITABLE
)
1023 add_write_fd (fd
, xd_read_queued_messages
, data
);
1024 if (flags
& DBUS_WATCH_READABLE
)
1025 add_read_fd (fd
, xd_read_queued_messages
, data
);
1030 /* Stop monitoring WATCH for possible I/O.
1031 DATA is the used bus, either a string or QCdbus_system_bus or
1032 QCdbus_session_bus. */
1034 xd_remove_watch (DBusWatch
*watch
, void *data
)
1036 unsigned int flags
= dbus_watch_get_flags (watch
);
1037 int fd
= xd_find_watch_fd (watch
);
1039 XD_DEBUG_MESSAGE ("fd %d", fd
);
1044 /* Unset session environment. */
1046 if (XSYMBOL (QCdbus_session_bus
) == data
)
1048 XD_DEBUG_MESSAGE ("unsetenv DBUS_SESSION_BUS_ADDRESS");
1049 unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1053 if (flags
& DBUS_WATCH_WRITABLE
)
1054 delete_write_fd (fd
);
1055 if (flags
& DBUS_WATCH_READABLE
)
1056 delete_read_fd (fd
);
1059 /* Toggle monitoring WATCH for possible I/O. */
1061 xd_toggle_watch (DBusWatch
*watch
, void *data
)
1063 if (dbus_watch_get_enabled (watch
))
1064 xd_add_watch (watch
, data
);
1066 xd_remove_watch (watch
, data
);
1069 /* Close connection to D-Bus BUS. */
1071 xd_close_bus (Lisp_Object bus
)
1073 DBusConnection
*connection
;
1076 /* Check whether we are connected. */
1077 val
= Fassoc (bus
, xd_registered_buses
);
1081 /* Retrieve bus address. */
1082 connection
= xd_get_connection_address (bus
);
1084 if (xd_get_connection_references (connection
) == 1)
1086 /* Close connection, if there isn't another shared application. */
1087 XD_DEBUG_MESSAGE ("Close connection to bus %s",
1088 XD_OBJECT_TO_STRING (bus
));
1089 dbus_connection_close (connection
);
1091 xd_registered_buses
= Fdelete (val
, xd_registered_buses
);
1095 /* Decrement reference count. */
1096 dbus_connection_unref (connection
);
1102 DEFUN ("dbus-init-bus", Fdbus_init_bus
, Sdbus_init_bus
, 1, 2, 0,
1103 doc
: /* Establish the connection to D-Bus BUS.
1105 BUS can be either the symbol `:system' or the symbol `:session', or it
1106 can be a string denoting the address of the corresponding bus. For
1107 the system and session buses, this function is called when loading
1108 `dbus.el', there is no need to call it again.
1110 The function returns a number, which counts the connections this Emacs
1111 session has established to the BUS under the same unique name (see
1112 `dbus-get-unique-name'). It depends on the libraries Emacs is linked
1113 with, and on the environment Emacs is running. For example, if Emacs
1114 is linked with the gtk toolkit, and it runs in a GTK-aware environment
1115 like Gnome, another connection might already be established.
1117 When PRIVATE is non-nil, a new connection is established instead of
1118 reusing an existing one. It results in a new unique name at the bus.
1119 This can be used, if it is necessary to distinguish from another
1120 connection used in the same Emacs process, like the one established by
1121 GTK+. It should be used with care for at least the `:system' and
1122 `:session' buses, because other Emacs Lisp packages might already use
1123 this connection to those buses. */)
1124 (Lisp_Object bus
, Lisp_Object
private)
1126 DBusConnection
*connection
;
1131 /* Check parameter. */
1132 XD_DBUS_VALIDATE_BUS_ADDRESS (bus
);
1134 /* Close bus if it is already open. */
1137 /* Check, whether we are still connected. */
1138 val
= Fassoc (bus
, xd_registered_buses
);
1141 connection
= xd_get_connection_address (bus
);
1142 dbus_connection_ref (connection
);
1148 dbus_error_init (&derror
);
1150 /* Open the connection. */
1153 connection
= dbus_connection_open (SSDATA (bus
), &derror
);
1155 connection
= dbus_connection_open_private (SSDATA (bus
), &derror
);
1159 connection
= dbus_bus_get (EQ (bus
, QCdbus_system_bus
)
1160 ? DBUS_BUS_SYSTEM
: DBUS_BUS_SESSION
,
1163 connection
= dbus_bus_get_private (EQ (bus
, QCdbus_system_bus
)
1164 ? DBUS_BUS_SYSTEM
: DBUS_BUS_SESSION
,
1167 if (dbus_error_is_set (&derror
))
1170 if (connection
== NULL
)
1171 XD_SIGNAL2 (build_string ("No connection to bus"), bus
);
1173 /* If it is not the system or session bus, we must register
1174 ourselves. Otherwise, we have called dbus_bus_get, which has
1175 configured us to exit if the connection closes - we undo this
1178 dbus_bus_register (connection
, &derror
);
1180 dbus_connection_set_exit_on_disconnect (connection
, FALSE
);
1182 if (dbus_error_is_set (&derror
))
1185 /* Add the watch functions. We pass also the bus as data, in
1186 order to distinguish between the buses in xd_remove_watch. */
1187 if (!dbus_connection_set_watch_functions (connection
,
1192 ? (void *) XSYMBOL (bus
)
1193 : (void *) XSTRING (bus
),
1195 XD_SIGNAL1 (build_string ("Cannot add watch functions"));
1197 /* Add bus to list of registered buses. */
1198 XSETFASTINT (val
, (intptr_t) connection
);
1199 xd_registered_buses
= Fcons (Fcons (bus
, val
), xd_registered_buses
);
1201 /* We do not want to abort. */
1202 putenv ((char *) "DBUS_FATAL_WARNINGS=0");
1205 dbus_error_free (&derror
);
1208 /* Return reference counter. */
1209 refcount
= xd_get_connection_references (connection
);
1210 XD_DEBUG_MESSAGE ("Bus %s, Reference counter %"pD
"d",
1211 XD_OBJECT_TO_STRING (bus
), refcount
);
1212 return make_number (refcount
);
1215 DEFUN ("dbus-get-unique-name", Fdbus_get_unique_name
, Sdbus_get_unique_name
,
1217 doc
: /* Return the unique name of Emacs registered at D-Bus BUS. */)
1220 DBusConnection
*connection
;
1223 /* Check parameter. */
1224 XD_DBUS_VALIDATE_BUS_ADDRESS (bus
);
1226 /* Retrieve bus address. */
1227 connection
= xd_get_connection_address (bus
);
1229 /* Request the name. */
1230 name
= dbus_bus_get_unique_name (connection
);
1232 XD_SIGNAL1 (build_string ("No unique name available"));
1235 return build_string (name
);
1238 DEFUN ("dbus-message-internal", Fdbus_message_internal
, Sdbus_message_internal
,
1240 doc
: /* Send a D-Bus message.
1241 This is an internal function, it shall not be used outside dbus.el.
1243 The following usages are expected:
1245 `dbus-call-method', `dbus-call-method-asynchronously':
1246 \(dbus-message-internal
1247 dbus-message-type-method-call BUS SERVICE PATH INTERFACE METHOD HANDLER
1248 &optional :timeout TIMEOUT &rest ARGS)
1251 \(dbus-message-internal
1252 dbus-message-type-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS)
1254 `dbus-method-return-internal':
1255 \(dbus-message-internal
1256 dbus-message-type-method-return BUS SERVICE SERIAL &rest ARGS)
1258 `dbus-method-error-internal':
1259 \(dbus-message-internal
1260 dbus-message-type-error BUS SERVICE SERIAL &rest ARGS)
1262 usage: (dbus-message-internal &rest REST) */)
1263 (ptrdiff_t nargs
, Lisp_Object
*args
)
1265 Lisp_Object message_type
, bus
, service
, handler
;
1266 Lisp_Object path
= Qnil
;
1267 Lisp_Object interface
= Qnil
;
1268 Lisp_Object member
= Qnil
;
1270 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
, gcpro6
;
1271 DBusConnection
*connection
;
1272 DBusMessage
*dmessage
;
1273 DBusMessageIter iter
;
1276 dbus_uint32_t serial
= 0;
1277 unsigned int ui_serial
;
1280 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
1282 /* Initialize parameters. */
1283 message_type
= args
[0];
1288 CHECK_NATNUM (message_type
);
1289 if (! (DBUS_MESSAGE_TYPE_INVALID
< XFASTINT (message_type
)
1290 && XFASTINT (message_type
) < DBUS_NUM_MESSAGE_TYPES
))
1291 XD_SIGNAL2 (build_string ("Invalid message type"), message_type
);
1292 mtype
= XFASTINT (message_type
);
1294 if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1295 || (mtype
== DBUS_MESSAGE_TYPE_SIGNAL
))
1298 interface
= args
[4];
1300 if (mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1302 count
= (mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
) ? 7 : 6;
1304 else /* DBUS_MESSAGE_TYPE_METHOD_RETURN, DBUS_MESSAGE_TYPE_ERROR */
1306 serial
= xd_extract_unsigned (args
[3], TYPE_MAXIMUM (dbus_uint32_t
));
1310 /* Check parameters. */
1311 XD_DBUS_VALIDATE_BUS_ADDRESS (bus
);
1312 XD_DBUS_VALIDATE_BUS_NAME (service
);
1314 xsignal2 (Qwrong_number_of_arguments
,
1315 Qdbus_message_internal
,
1316 make_number (nargs
));
1318 if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1319 || (mtype
== DBUS_MESSAGE_TYPE_SIGNAL
))
1321 XD_DBUS_VALIDATE_PATH (path
);
1322 XD_DBUS_VALIDATE_INTERFACE (interface
);
1323 XD_DBUS_VALIDATE_MEMBER (member
);
1324 if (!NILP (handler
) && (!FUNCTIONP (handler
)))
1325 wrong_type_argument (Qinvalid_function
, handler
);
1328 /* Protect Lisp variables. */
1329 GCPRO6 (bus
, service
, path
, interface
, member
, handler
);
1331 /* Trace parameters. */
1334 case DBUS_MESSAGE_TYPE_METHOD_CALL
:
1335 XD_DEBUG_MESSAGE ("%s %s %s %s %s %s %s",
1336 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1337 XD_OBJECT_TO_STRING (bus
),
1338 XD_OBJECT_TO_STRING (service
),
1339 XD_OBJECT_TO_STRING (path
),
1340 XD_OBJECT_TO_STRING (interface
),
1341 XD_OBJECT_TO_STRING (member
),
1342 XD_OBJECT_TO_STRING (handler
));
1344 case DBUS_MESSAGE_TYPE_SIGNAL
:
1345 XD_DEBUG_MESSAGE ("%s %s %s %s %s %s",
1346 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1347 XD_OBJECT_TO_STRING (bus
),
1348 XD_OBJECT_TO_STRING (service
),
1349 XD_OBJECT_TO_STRING (path
),
1350 XD_OBJECT_TO_STRING (interface
),
1351 XD_OBJECT_TO_STRING (member
));
1353 default: /* DBUS_MESSAGE_TYPE_METHOD_RETURN, DBUS_MESSAGE_TYPE_ERROR */
1355 XD_DEBUG_MESSAGE ("%s %s %s %u",
1356 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1357 XD_OBJECT_TO_STRING (bus
),
1358 XD_OBJECT_TO_STRING (service
),
1362 /* Retrieve bus address. */
1363 connection
= xd_get_connection_address (bus
);
1365 /* Create the D-Bus message. */
1366 dmessage
= dbus_message_new (mtype
);
1367 if (dmessage
== NULL
)
1370 XD_SIGNAL1 (build_string ("Unable to create a new message"));
1373 if (STRINGP (service
))
1375 if (mtype
!= DBUS_MESSAGE_TYPE_SIGNAL
)
1376 /* Set destination. */
1378 if (!dbus_message_set_destination (dmessage
, SSDATA (service
)))
1381 XD_SIGNAL2 (build_string ("Unable to set the destination"),
1387 /* Set destination for unicast signals. */
1391 /* If it is the same unique name as we are registered at the
1392 bus or an unknown name, we regard it as broadcast message
1393 due to backward compatibility. */
1394 if (dbus_bus_name_has_owner (connection
, SSDATA (service
), NULL
))
1395 uname
= call2 (intern ("dbus-get-name-owner"), bus
, service
);
1400 && (strcmp (dbus_bus_get_unique_name (connection
), SSDATA (uname
))
1402 && (!dbus_message_set_destination (dmessage
, SSDATA (service
))))
1405 XD_SIGNAL2 (build_string ("Unable to set signal destination"),
1411 /* Set message parameters. */
1412 if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1413 || (mtype
== DBUS_MESSAGE_TYPE_SIGNAL
))
1415 if ((!dbus_message_set_path (dmessage
, SSDATA (path
)))
1416 || (!dbus_message_set_interface (dmessage
, SSDATA (interface
)))
1417 || (!dbus_message_set_member (dmessage
, SSDATA (member
))))
1420 XD_SIGNAL1 (build_string ("Unable to set the message parameter"));
1424 else /* DBUS_MESSAGE_TYPE_METHOD_RETURN, DBUS_MESSAGE_TYPE_ERROR */
1426 if (!dbus_message_set_reply_serial (dmessage
, serial
))
1429 XD_SIGNAL1 (build_string ("Unable to create a return message"));
1432 if ((mtype
== DBUS_MESSAGE_TYPE_ERROR
)
1433 && (!dbus_message_set_error_name (dmessage
, DBUS_ERROR_FAILED
)))
1436 XD_SIGNAL1 (build_string ("Unable to create a error message"));
1440 /* Check for timeout parameter. */
1441 if ((count
+2 <= nargs
) && (EQ ((args
[count
]), QCdbus_timeout
)))
1443 CHECK_NATNUM (args
[count
+1]);
1444 timeout
= min (XFASTINT (args
[count
+1]), INT_MAX
);
1448 /* Initialize parameter list of message. */
1449 dbus_message_iter_init_append (dmessage
, &iter
);
1451 /* Append parameters to the message. */
1452 for (; count
< nargs
; ++count
)
1454 dtype
= XD_OBJECT_TO_DBUS_TYPE (args
[count
]);
1455 if (XD_DBUS_TYPE_P (args
[count
]))
1457 XD_DEBUG_VALID_LISP_OBJECT_P (args
[count
]);
1458 XD_DEBUG_VALID_LISP_OBJECT_P (args
[count
+1]);
1459 XD_DEBUG_MESSAGE ("Parameter%"pD
"d %s %s", count
- 4,
1460 XD_OBJECT_TO_STRING (args
[count
]),
1461 XD_OBJECT_TO_STRING (args
[count
+1]));
1466 XD_DEBUG_VALID_LISP_OBJECT_P (args
[count
]);
1467 XD_DEBUG_MESSAGE ("Parameter%"pD
"d %s", count
- 4,
1468 XD_OBJECT_TO_STRING (args
[count
]));
1471 /* Check for valid signature. We use DBUS_TYPE_INVALID as
1472 indication that there is no parent type. */
1473 xd_signature (signature
, dtype
, DBUS_TYPE_INVALID
, args
[count
]);
1475 xd_append_arg (dtype
, args
[count
], &iter
);
1478 if (!NILP (handler
))
1480 /* Send the message. The message is just added to the outgoing
1482 if (!dbus_connection_send_with_reply (connection
, dmessage
,
1486 XD_SIGNAL1 (build_string ("Cannot send message"));
1489 /* The result is the key in Vdbus_registered_objects_table. */
1490 serial
= dbus_message_get_serial (dmessage
);
1491 result
= list3 (QCdbus_registered_serial
,
1492 bus
, make_fixnum_or_float (serial
));
1494 /* Create a hash table entry. */
1495 Fputhash (result
, handler
, Vdbus_registered_objects_table
);
1499 /* Send the message. The message is just added to the outgoing
1501 if (!dbus_connection_send (connection
, dmessage
, NULL
))
1504 XD_SIGNAL1 (build_string ("Cannot send message"));
1510 XD_DEBUG_MESSAGE ("Message sent: %s", XD_OBJECT_TO_STRING (result
));
1513 dbus_message_unref (dmessage
);
1515 /* Return the result. */
1516 RETURN_UNGCPRO (result
);
1519 /* Read one queued incoming message of the D-Bus BUS.
1520 BUS is either a Lisp symbol, :system or :session, or a string denoting
1523 xd_read_message_1 (DBusConnection
*connection
, Lisp_Object bus
)
1525 Lisp_Object args
, key
, value
;
1526 struct gcpro gcpro1
;
1527 struct input_event event
;
1528 DBusMessage
*dmessage
;
1529 DBusMessageIter iter
;
1532 dbus_uint32_t serial
;
1533 unsigned int ui_serial
;
1534 const char *uname
, *path
, *interface
, *member
;
1536 dmessage
= dbus_connection_pop_message (connection
);
1538 /* Return if there is no queued message. */
1539 if (dmessage
== NULL
)
1542 /* Collect the parameters. */
1546 /* Loop over the resulting parameters. Construct a list. */
1547 if (dbus_message_iter_init (dmessage
, &iter
))
1549 while ((dtype
= dbus_message_iter_get_arg_type (&iter
))
1550 != DBUS_TYPE_INVALID
)
1552 args
= Fcons (xd_retrieve_arg (dtype
, &iter
), args
);
1553 dbus_message_iter_next (&iter
);
1555 /* The arguments are stored in reverse order. Reorder them. */
1556 args
= Fnreverse (args
);
1559 /* Read message type, message serial, unique name, object path,
1560 interface and member from the message. */
1561 mtype
= dbus_message_get_type (dmessage
);
1562 ui_serial
= serial
=
1563 ((mtype
== DBUS_MESSAGE_TYPE_METHOD_RETURN
)
1564 || (mtype
== DBUS_MESSAGE_TYPE_ERROR
))
1565 ? dbus_message_get_reply_serial (dmessage
)
1566 : dbus_message_get_serial (dmessage
);
1567 uname
= dbus_message_get_sender (dmessage
);
1568 path
= dbus_message_get_path (dmessage
);
1569 interface
= dbus_message_get_interface (dmessage
);
1570 member
= dbus_message_get_member (dmessage
);
1572 XD_DEBUG_MESSAGE ("Event received: %s %u %s %s %s %s %s",
1573 XD_MESSAGE_TYPE_TO_STRING (mtype
),
1574 ui_serial
, uname
, path
, interface
, member
,
1575 XD_OBJECT_TO_STRING (args
));
1577 if (mtype
== DBUS_MESSAGE_TYPE_INVALID
)
1580 else if ((mtype
== DBUS_MESSAGE_TYPE_METHOD_RETURN
)
1581 || (mtype
== DBUS_MESSAGE_TYPE_ERROR
))
1583 /* Search for a registered function of the message. */
1584 key
= list3 (QCdbus_registered_serial
, bus
,
1585 make_fixnum_or_float (serial
));
1586 value
= Fgethash (key
, Vdbus_registered_objects_table
, Qnil
);
1588 /* There shall be exactly one entry. Construct an event. */
1592 /* Remove the entry. */
1593 Fremhash (key
, Vdbus_registered_objects_table
);
1595 /* Construct an event. */
1597 event
.kind
= DBUS_EVENT
;
1598 event
.frame_or_window
= Qnil
;
1599 event
.arg
= Fcons (value
, args
);
1602 else /* DBUS_MESSAGE_TYPE_METHOD_CALL, DBUS_MESSAGE_TYPE_SIGNAL. */
1604 /* Vdbus_registered_objects_table requires non-nil interface and
1606 if ((interface
== NULL
) || (member
== NULL
))
1609 /* Search for a registered function of the message. */
1610 key
= list4 ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
)
1611 ? QCdbus_registered_method
1612 : QCdbus_registered_signal
,
1613 bus
, build_string (interface
), build_string (member
));
1614 value
= Fgethash (key
, Vdbus_registered_objects_table
, Qnil
);
1616 /* Loop over the registered functions. Construct an event. */
1617 while (!NILP (value
))
1619 key
= CAR_SAFE (value
);
1620 /* key has the structure (UNAME SERVICE PATH HANDLER). */
1621 if (((uname
== NULL
)
1622 || (NILP (CAR_SAFE (key
)))
1623 || (strcmp (uname
, SSDATA (CAR_SAFE (key
))) == 0))
1625 || (NILP (CAR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))
1627 SSDATA (CAR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))
1629 && (!NILP (CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))))
1632 event
.kind
= DBUS_EVENT
;
1633 event
.frame_or_window
= Qnil
;
1635 = Fcons (CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (key
)))), args
);
1638 value
= CDR_SAFE (value
);
1645 /* Add type, serial, uname, path, interface and member to the event. */
1646 event
.arg
= Fcons ((member
== NULL
? Qnil
: build_string (member
)),
1648 event
.arg
= Fcons ((interface
== NULL
? Qnil
: build_string (interface
)),
1650 event
.arg
= Fcons ((path
== NULL
? Qnil
: build_string (path
)),
1652 event
.arg
= Fcons ((uname
== NULL
? Qnil
: build_string (uname
)),
1654 event
.arg
= Fcons (make_fixnum_or_float (serial
), event
.arg
);
1655 event
.arg
= Fcons (make_number (mtype
), event
.arg
);
1657 /* Add the bus symbol to the event. */
1658 event
.arg
= Fcons (bus
, event
.arg
);
1660 /* Store it into the input event queue. */
1661 kbd_buffer_store_event (&event
);
1663 XD_DEBUG_MESSAGE ("Event stored: %s", XD_OBJECT_TO_STRING (event
.arg
));
1667 dbus_message_unref (dmessage
);
1672 /* Read queued incoming messages of the D-Bus BUS.
1673 BUS is either a Lisp symbol, :system or :session, or a string denoting
1676 xd_read_message (Lisp_Object bus
)
1678 /* Retrieve bus address. */
1679 DBusConnection
*connection
= xd_get_connection_address (bus
);
1681 /* Non blocking read of the next available message. */
1682 dbus_connection_read_write (connection
, 0);
1684 while (dbus_connection_get_dispatch_status (connection
)
1685 != DBUS_DISPATCH_COMPLETE
)
1686 xd_read_message_1 (connection
, bus
);
1690 /* Callback called when something is ready to read or write. */
1692 xd_read_queued_messages (int fd
, void *data
)
1694 Lisp_Object busp
= xd_registered_buses
;
1695 Lisp_Object bus
= Qnil
;
1698 /* Find bus related to fd. */
1700 while (!NILP (busp
))
1702 key
= CAR_SAFE (CAR_SAFE (busp
));
1703 if ((SYMBOLP (key
) && XSYMBOL (key
) == data
)
1704 || (STRINGP (key
) && XSTRING (key
) == data
))
1706 busp
= CDR_SAFE (busp
);
1712 /* We ignore all Lisp errors during the call. */
1713 xd_in_read_queued_messages
= 1;
1714 internal_catch (Qdbus_error
, xd_read_message
, bus
);
1715 xd_in_read_queued_messages
= 0;
1720 syms_of_dbusbind (void)
1723 DEFSYM (Qdbus_init_bus
, "dbus-init-bus");
1724 defsubr (&Sdbus_init_bus
);
1726 DEFSYM (Qdbus_get_unique_name
, "dbus-get-unique-name");
1727 defsubr (&Sdbus_get_unique_name
);
1729 DEFSYM (Qdbus_message_internal
, "dbus-message-internal");
1730 defsubr (&Sdbus_message_internal
);
1732 DEFSYM (Qdbus_error
, "dbus-error");
1733 Fput (Qdbus_error
, Qerror_conditions
,
1734 list2 (Qdbus_error
, Qerror
));
1735 Fput (Qdbus_error
, Qerror_message
,
1736 build_pure_c_string ("D-Bus error"));
1738 DEFSYM (QCdbus_system_bus
, ":system");
1739 DEFSYM (QCdbus_session_bus
, ":session");
1740 DEFSYM (QCdbus_timeout
, ":timeout");
1741 DEFSYM (QCdbus_type_byte
, ":byte");
1742 DEFSYM (QCdbus_type_boolean
, ":boolean");
1743 DEFSYM (QCdbus_type_int16
, ":int16");
1744 DEFSYM (QCdbus_type_uint16
, ":uint16");
1745 DEFSYM (QCdbus_type_int32
, ":int32");
1746 DEFSYM (QCdbus_type_uint32
, ":uint32");
1747 DEFSYM (QCdbus_type_int64
, ":int64");
1748 DEFSYM (QCdbus_type_uint64
, ":uint64");
1749 DEFSYM (QCdbus_type_double
, ":double");
1750 DEFSYM (QCdbus_type_string
, ":string");
1751 DEFSYM (QCdbus_type_object_path
, ":object-path");
1752 DEFSYM (QCdbus_type_signature
, ":signature");
1753 #ifdef DBUS_TYPE_UNIX_FD
1754 DEFSYM (QCdbus_type_unix_fd
, ":unix-fd");
1756 DEFSYM (QCdbus_type_array
, ":array");
1757 DEFSYM (QCdbus_type_variant
, ":variant");
1758 DEFSYM (QCdbus_type_struct
, ":struct");
1759 DEFSYM (QCdbus_type_dict_entry
, ":dict-entry");
1760 DEFSYM (QCdbus_registered_serial
, ":serial");
1761 DEFSYM (QCdbus_registered_method
, ":method");
1762 DEFSYM (QCdbus_registered_signal
, ":signal");
1764 DEFVAR_LISP ("dbus-compiled-version",
1765 Vdbus_compiled_version
,
1766 doc
: /* The version of D-Bus Emacs is compiled against. */);
1767 #ifdef DBUS_VERSION_STRING
1768 Vdbus_compiled_version
= build_pure_c_string (DBUS_VERSION_STRING
);
1770 Vdbus_compiled_version
= Qnil
;
1773 DEFVAR_LISP ("dbus-runtime-version",
1774 Vdbus_runtime_version
,
1775 doc
: /* The version of D-Bus Emacs runs with. */);
1778 int major
, minor
, micro
;
1779 char s
[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
1780 dbus_get_version (&major
, &minor
, µ
);
1781 Vdbus_runtime_version
1782 = make_formatted_string (s
, "%d.%d.%d", major
, minor
, micro
);
1784 Vdbus_runtime_version
= Qnil
;
1788 DEFVAR_LISP ("dbus-message-type-invalid",
1789 Vdbus_message_type_invalid
,
1790 doc
: /* This value is never a valid message type. */);
1791 Vdbus_message_type_invalid
= make_number (DBUS_MESSAGE_TYPE_INVALID
);
1793 DEFVAR_LISP ("dbus-message-type-method-call",
1794 Vdbus_message_type_method_call
,
1795 doc
: /* Message type of a method call message. */);
1796 Vdbus_message_type_method_call
= make_number (DBUS_MESSAGE_TYPE_METHOD_CALL
);
1798 DEFVAR_LISP ("dbus-message-type-method-return",
1799 Vdbus_message_type_method_return
,
1800 doc
: /* Message type of a method return message. */);
1801 Vdbus_message_type_method_return
1802 = make_number (DBUS_MESSAGE_TYPE_METHOD_RETURN
);
1804 DEFVAR_LISP ("dbus-message-type-error",
1805 Vdbus_message_type_error
,
1806 doc
: /* Message type of an error reply message. */);
1807 Vdbus_message_type_error
= make_number (DBUS_MESSAGE_TYPE_ERROR
);
1809 DEFVAR_LISP ("dbus-message-type-signal",
1810 Vdbus_message_type_signal
,
1811 doc
: /* Message type of a signal message. */);
1812 Vdbus_message_type_signal
= make_number (DBUS_MESSAGE_TYPE_SIGNAL
);
1814 DEFVAR_LISP ("dbus-registered-objects-table",
1815 Vdbus_registered_objects_table
,
1816 doc
: /* Hash table of registered functions for D-Bus.
1818 There are two different uses of the hash table: for accessing
1819 registered interfaces properties, targeted by signals or method calls,
1820 and for calling handlers in case of non-blocking method call returns.
1822 In the first case, the key in the hash table is the list (TYPE BUS
1823 INTERFACE MEMBER). TYPE is one of the Lisp symbols `:method',
1824 `:signal' or `:property'. BUS is either a Lisp symbol, `:system' or
1825 `:session', or a string denoting the bus address. INTERFACE is a
1826 string which denotes a D-Bus interface, and MEMBER, also a string, is
1827 either a method, a signal or a property INTERFACE is offering. All
1828 arguments but BUS must not be nil.
1830 The value in the hash table is a list of quadruple lists \((UNAME
1831 SERVICE PATH OBJECT [RULE]) ...). SERVICE is the service name as
1832 registered, UNAME is the corresponding unique name. In case of
1833 registered methods and properties, UNAME is nil. PATH is the object
1834 path of the sending object. All of them can be nil, which means a
1835 wildcard then. OBJECT is either the handler to be called when a D-Bus
1836 message, which matches the key criteria, arrives (TYPE `:method' and
1837 `:signal'), or a cons cell containing the value of the property (TYPE
1840 For entries of type `:signal', there is also a fifth element RULE,
1841 which keeps the match string the signal is registered with.
1843 In the second case, the key in the hash table is the list (:serial BUS
1844 SERIAL). BUS is either a Lisp symbol, `:system' or `:session', or a
1845 string denoting the bus address. SERIAL is the serial number of the
1846 non-blocking method call, a reply is expected. Both arguments must
1847 not be nil. The value in the hash table is HANDLER, the function to
1848 be called when the D-Bus reply message arrives. */);
1850 Lisp_Object args
[2];
1853 Vdbus_registered_objects_table
= Fmake_hash_table (2, args
);
1856 DEFVAR_LISP ("dbus-debug", Vdbus_debug
,
1857 doc
: /* If non-nil, debug messages of D-Bus bindings are raised. */);
1860 /* We can also set environment variable DBUS_VERBOSE=1 in order to
1861 see more traces. This requires libdbus-1 to be configured with
1862 --enable-verbose-mode. */
1867 /* Initialize internal objects. */
1868 xd_registered_buses
= Qnil
;
1869 staticpro (&xd_registered_buses
);
1871 Fprovide (intern_c_string ("dbusbind"), Qnil
);
1875 #endif /* HAVE_DBUS */