1 /* Elisp bindings for D-Bus.
2 Copyright (C) 2007, 2008 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/>. */
24 #include <dbus/dbus.h>
27 #include "termhooks.h"
32 Lisp_Object Qdbus_get_unique_name
;
33 Lisp_Object Qdbus_call_method
;
34 Lisp_Object Qdbus_method_return_internal
;
35 Lisp_Object Qdbus_send_signal
;
36 Lisp_Object Qdbus_register_signal
;
37 Lisp_Object Qdbus_register_method
;
39 /* D-Bus error symbol. */
40 Lisp_Object Qdbus_error
;
42 /* Lisp symbols of the system and session buses. */
43 Lisp_Object QCdbus_system_bus
, QCdbus_session_bus
;
45 /* Lisp symbol for method call timeout. */
46 Lisp_Object QCdbus_timeout
;
48 /* Lisp symbols of D-Bus types. */
49 Lisp_Object QCdbus_type_byte
, QCdbus_type_boolean
;
50 Lisp_Object QCdbus_type_int16
, QCdbus_type_uint16
;
51 Lisp_Object QCdbus_type_int32
, QCdbus_type_uint32
;
52 Lisp_Object QCdbus_type_int64
, QCdbus_type_uint64
;
53 Lisp_Object QCdbus_type_double
, QCdbus_type_string
;
54 Lisp_Object QCdbus_type_object_path
, QCdbus_type_signature
;
55 Lisp_Object QCdbus_type_array
, QCdbus_type_variant
;
56 Lisp_Object QCdbus_type_struct
, QCdbus_type_dict_entry
;
58 /* Hash table which keeps function definitions. */
59 Lisp_Object Vdbus_registered_functions_table
;
61 /* Whether to debug D-Bus. */
62 Lisp_Object Vdbus_debug
;
65 /* We use "xd_" and "XD_" as prefix for all internal symbols, because
66 we don't want to poison other namespaces with "dbus_". */
68 /* Raise a Lisp error from a D-Bus ERROR. */
69 #define XD_ERROR(error) \
72 strcpy (s, error.message); \
73 dbus_error_free (&error); \
74 /* Remove the trailing newline. */ \
75 if (strchr (s, '\n') != NULL) \
76 s[strlen (s) - 1] = '\0'; \
77 xsignal1 (Qdbus_error, build_string (s)); \
80 /* Macros for debugging. In order to enable them, build with
81 "make MYCPPFLAGS='-DDBUS_DEBUG -Wall'". */
83 #define XD_DEBUG_MESSAGE(...) \
86 sprintf (s, __VA_ARGS__); \
87 printf ("%s: %s\n", __func__, s); \
88 message ("%s: %s", __func__, s); \
90 #define XD_DEBUG_VALID_LISP_OBJECT_P(object) \
92 if (!valid_lisp_object_p (object)) \
94 XD_DEBUG_MESSAGE ("%d Assertion failure", __LINE__); \
95 xsignal1 (Qdbus_error, build_string ("Assertion failure")); \
99 #else /* !DBUS_DEBUG */
100 #define XD_DEBUG_MESSAGE(...) \
102 if (!NILP (Vdbus_debug)) \
105 sprintf (s, __VA_ARGS__); \
106 message ("%s: %s", __func__, s); \
109 #define XD_DEBUG_VALID_LISP_OBJECT_P(object)
112 /* Check whether TYPE is a basic DBusType. */
113 #define XD_BASIC_DBUS_TYPE(type) \
114 ((type == DBUS_TYPE_BYTE) \
115 || (type == DBUS_TYPE_BOOLEAN) \
116 || (type == DBUS_TYPE_INT16) \
117 || (type == DBUS_TYPE_UINT16) \
118 || (type == DBUS_TYPE_INT32) \
119 || (type == DBUS_TYPE_UINT32) \
120 || (type == DBUS_TYPE_INT64) \
121 || (type == DBUS_TYPE_UINT64) \
122 || (type == DBUS_TYPE_DOUBLE) \
123 || (type == DBUS_TYPE_STRING) \
124 || (type == DBUS_TYPE_OBJECT_PATH) \
125 || (type == DBUS_TYPE_SIGNATURE))
127 /* Determine the DBusType of a given Lisp symbol. OBJECT must be one
128 of the predefined D-Bus type symbols. */
129 #define XD_SYMBOL_TO_DBUS_TYPE(object) \
130 ((EQ (object, QCdbus_type_byte)) ? DBUS_TYPE_BYTE \
131 : (EQ (object, QCdbus_type_boolean)) ? DBUS_TYPE_BOOLEAN \
132 : (EQ (object, QCdbus_type_int16)) ? DBUS_TYPE_INT16 \
133 : (EQ (object, QCdbus_type_uint16)) ? DBUS_TYPE_UINT16 \
134 : (EQ (object, QCdbus_type_int32)) ? DBUS_TYPE_INT32 \
135 : (EQ (object, QCdbus_type_uint32)) ? DBUS_TYPE_UINT32 \
136 : (EQ (object, QCdbus_type_int64)) ? DBUS_TYPE_INT64 \
137 : (EQ (object, QCdbus_type_uint64)) ? DBUS_TYPE_UINT64 \
138 : (EQ (object, QCdbus_type_double)) ? DBUS_TYPE_DOUBLE \
139 : (EQ (object, QCdbus_type_string)) ? DBUS_TYPE_STRING \
140 : (EQ (object, QCdbus_type_object_path)) ? DBUS_TYPE_OBJECT_PATH \
141 : (EQ (object, QCdbus_type_signature)) ? DBUS_TYPE_SIGNATURE \
142 : (EQ (object, QCdbus_type_array)) ? DBUS_TYPE_ARRAY \
143 : (EQ (object, QCdbus_type_variant)) ? DBUS_TYPE_VARIANT \
144 : (EQ (object, QCdbus_type_struct)) ? DBUS_TYPE_STRUCT \
145 : (EQ (object, QCdbus_type_dict_entry)) ? DBUS_TYPE_DICT_ENTRY \
148 /* Check whether a Lisp symbol is a predefined D-Bus type symbol. */
149 #define XD_DBUS_TYPE_P(object) \
150 (SYMBOLP (object) && ((XD_SYMBOL_TO_DBUS_TYPE (object) != DBUS_TYPE_INVALID)))
152 /* Determine the DBusType of a given Lisp OBJECT. It is used to
153 convert Lisp objects, being arguments of `dbus-call-method' or
154 `dbus-send-signal', into corresponding C values appended as
155 arguments to a D-Bus message. */
156 #define XD_OBJECT_TO_DBUS_TYPE(object) \
157 ((EQ (object, Qt) || EQ (object, Qnil)) ? DBUS_TYPE_BOOLEAN \
158 : (NATNUMP (object)) ? DBUS_TYPE_UINT32 \
159 : (INTEGERP (object)) ? DBUS_TYPE_INT32 \
160 : (FLOATP (object)) ? DBUS_TYPE_DOUBLE \
161 : (STRINGP (object)) ? DBUS_TYPE_STRING \
162 : (XD_DBUS_TYPE_P (object)) ? XD_SYMBOL_TO_DBUS_TYPE (object) \
163 : (CONSP (object)) ? ((XD_DBUS_TYPE_P (CAR_SAFE (object))) \
164 ? XD_SYMBOL_TO_DBUS_TYPE (CAR_SAFE (object)) \
168 /* Return a list pointer which does not have a Lisp symbol as car. */
169 #define XD_NEXT_VALUE(object) \
170 ((XD_DBUS_TYPE_P (CAR_SAFE (object))) ? CDR_SAFE (object) : object)
172 /* Compute SIGNATURE of OBJECT. It must have a form that it can be
173 used in dbus_message_iter_open_container. DTYPE is the DBusType
174 the object is related to. It is passed as argument, because it
175 cannot be detected in basic type objects, when they are preceded by
176 a type symbol. PARENT_TYPE is the DBusType of a container this
177 signature is embedded, or DBUS_TYPE_INVALID. It is needed for the
178 check that DBUS_TYPE_DICT_ENTRY occurs only as array element. */
180 xd_signature(signature
, dtype
, parent_type
, object
)
182 unsigned int dtype
, parent_type
;
185 unsigned int subtype
;
187 char x
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
194 case DBUS_TYPE_UINT16
:
195 case DBUS_TYPE_UINT32
:
196 case DBUS_TYPE_UINT64
:
197 CHECK_NATNUM (object
);
198 sprintf (signature
, "%c", dtype
);
201 case DBUS_TYPE_BOOLEAN
:
202 if (!EQ (object
, Qt
) && !EQ (object
, Qnil
))
203 wrong_type_argument (intern ("booleanp"), object
);
204 sprintf (signature
, "%c", dtype
);
207 case DBUS_TYPE_INT16
:
208 case DBUS_TYPE_INT32
:
209 case DBUS_TYPE_INT64
:
210 CHECK_NUMBER (object
);
211 sprintf (signature
, "%c", dtype
);
214 case DBUS_TYPE_DOUBLE
:
215 CHECK_FLOAT (object
);
216 sprintf (signature
, "%c", dtype
);
219 case DBUS_TYPE_STRING
:
220 case DBUS_TYPE_OBJECT_PATH
:
221 case DBUS_TYPE_SIGNATURE
:
222 CHECK_STRING (object
);
223 sprintf (signature
, "%c", dtype
);
226 case DBUS_TYPE_ARRAY
:
227 /* Check that all list elements have the same D-Bus type. For
228 complex element types, we just check the container type, not
229 the whole element's signature. */
232 /* Type symbol is optional. */
233 if (EQ (QCdbus_type_array
, CAR_SAFE (elt
)))
234 elt
= XD_NEXT_VALUE (elt
);
236 /* If the array is empty, DBUS_TYPE_STRING is the default
240 subtype
= DBUS_TYPE_STRING
;
241 strcpy (x
, DBUS_TYPE_STRING_AS_STRING
);
245 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
246 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
249 /* If the element type is DBUS_TYPE_SIGNATURE, and this is the
250 only element, the value of this element is used as he array's
251 element signature. */
252 if ((subtype
== DBUS_TYPE_SIGNATURE
)
253 && STRINGP (CAR_SAFE (XD_NEXT_VALUE (elt
)))
254 && NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
255 strcpy (x
, SDATA (CAR_SAFE (XD_NEXT_VALUE (elt
))));
259 if (subtype
!= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
)))
260 wrong_type_argument (intern ("D-Bus"), CAR_SAFE (elt
));
261 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
264 sprintf (signature
, "%c%s", dtype
, x
);
267 case DBUS_TYPE_VARIANT
:
268 /* Check that there is exactly one list element. */
271 elt
= XD_NEXT_VALUE (elt
);
272 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
273 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
275 if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
276 wrong_type_argument (intern ("D-Bus"),
277 CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt
))));
279 sprintf (signature
, "%c", dtype
);
282 case DBUS_TYPE_STRUCT
:
283 /* A struct list might contain any number of elements with
284 different types. No further check needed. */
287 elt
= XD_NEXT_VALUE (elt
);
289 /* Compose the signature from the elements. It is enclosed by
291 sprintf (signature
, "%c", DBUS_STRUCT_BEGIN_CHAR
);
294 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
295 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
296 strcat (signature
, x
);
297 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
299 sprintf (signature
, "%s%c", signature
, DBUS_STRUCT_END_CHAR
);
302 case DBUS_TYPE_DICT_ENTRY
:
303 /* Check that there are exactly two list elements, and the first
304 one is of basic type. The dictionary entry itself must be an
305 element of an array. */
308 /* Check the parent object type. */
309 if (parent_type
!= DBUS_TYPE_ARRAY
)
310 wrong_type_argument (intern ("D-Bus"), object
);
312 /* Compose the signature from the elements. It is enclosed by
314 sprintf (signature
, "%c", DBUS_DICT_ENTRY_BEGIN_CHAR
);
317 elt
= XD_NEXT_VALUE (elt
);
318 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
319 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
320 strcat (signature
, x
);
322 if (!XD_BASIC_DBUS_TYPE (subtype
))
323 wrong_type_argument (intern ("D-Bus"), CAR_SAFE (XD_NEXT_VALUE (elt
)));
325 /* Second element. */
326 elt
= CDR_SAFE (XD_NEXT_VALUE (elt
));
327 subtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (elt
));
328 xd_signature (x
, subtype
, dtype
, CAR_SAFE (XD_NEXT_VALUE (elt
)));
329 strcat (signature
, x
);
331 if (!NILP (CDR_SAFE (XD_NEXT_VALUE (elt
))))
332 wrong_type_argument (intern ("D-Bus"),
333 CAR_SAFE (CDR_SAFE (XD_NEXT_VALUE (elt
))));
335 /* Closing signature. */
336 sprintf (signature
, "%s%c", signature
, DBUS_DICT_ENTRY_END_CHAR
);
340 wrong_type_argument (intern ("D-Bus"), object
);
343 XD_DEBUG_MESSAGE ("%s", signature
);
346 /* Append C value, extracted from Lisp OBJECT, to iteration ITER.
347 DTYPE must be a valid DBusType. It is used to convert Lisp
348 objects, being arguments of `dbus-call-method' or
349 `dbus-send-signal', into corresponding C values appended as
350 arguments to a D-Bus message. */
352 xd_append_arg (dtype
, object
, iter
)
355 DBusMessageIter
*iter
;
357 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
358 DBusMessageIter subiter
;
360 if (XD_BASIC_DBUS_TYPE (dtype
))
365 unsigned char val
= XUINT (object
) & 0xFF;
366 XD_DEBUG_MESSAGE ("%c %d", dtype
, val
);
367 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
368 xsignal2 (Qdbus_error
,
369 build_string ("Unable to append argument"), object
);
373 case DBUS_TYPE_BOOLEAN
:
375 dbus_bool_t val
= (NILP (object
)) ? FALSE
: TRUE
;
376 XD_DEBUG_MESSAGE ("%c %s", dtype
, (val
== FALSE
) ? "false" : "true");
377 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
378 xsignal2 (Qdbus_error
,
379 build_string ("Unable to append argument"), object
);
383 case DBUS_TYPE_INT16
:
385 dbus_int16_t val
= XINT (object
);
386 XD_DEBUG_MESSAGE ("%c %d", dtype
, (int) val
);
387 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
388 xsignal2 (Qdbus_error
,
389 build_string ("Unable to append argument"), object
);
393 case DBUS_TYPE_UINT16
:
395 dbus_uint16_t val
= XUINT (object
);
396 XD_DEBUG_MESSAGE ("%c %u", dtype
, (unsigned int) val
);
397 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
398 xsignal2 (Qdbus_error
,
399 build_string ("Unable to append argument"), object
);
403 case DBUS_TYPE_INT32
:
405 dbus_int32_t val
= XINT (object
);
406 XD_DEBUG_MESSAGE ("%c %d", dtype
, val
);
407 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
408 xsignal2 (Qdbus_error
,
409 build_string ("Unable to append argument"), object
);
413 case DBUS_TYPE_UINT32
:
415 dbus_uint32_t val
= XUINT (object
);
416 XD_DEBUG_MESSAGE ("%c %u", dtype
, val
);
417 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
418 xsignal2 (Qdbus_error
,
419 build_string ("Unable to append argument"), object
);
423 case DBUS_TYPE_INT64
:
425 dbus_int64_t val
= XINT (object
);
426 XD_DEBUG_MESSAGE ("%c %d", dtype
, (int) val
);
427 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
428 xsignal2 (Qdbus_error
,
429 build_string ("Unable to append argument"), object
);
433 case DBUS_TYPE_UINT64
:
435 dbus_uint64_t val
= XUINT (object
);
436 XD_DEBUG_MESSAGE ("%c %u", dtype
, (unsigned int) val
);
437 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
438 xsignal2 (Qdbus_error
,
439 build_string ("Unable to append argument"), object
);
443 case DBUS_TYPE_DOUBLE
:
444 XD_DEBUG_MESSAGE ("%c %f", dtype
, XFLOAT_DATA (object
));
445 if (!dbus_message_iter_append_basic (iter
, dtype
,
446 &XFLOAT_DATA (object
)))
447 xsignal2 (Qdbus_error
,
448 build_string ("Unable to append argument"), object
);
451 case DBUS_TYPE_STRING
:
452 case DBUS_TYPE_OBJECT_PATH
:
453 case DBUS_TYPE_SIGNATURE
:
455 char *val
= SDATA (object
);
456 XD_DEBUG_MESSAGE ("%c %s", dtype
, val
);
457 if (!dbus_message_iter_append_basic (iter
, dtype
, &val
))
458 xsignal2 (Qdbus_error
,
459 build_string ("Unable to append argument"), object
);
464 else /* Compound types. */
467 /* All compound types except array have a type symbol. For
468 array, it is optional. Skip it. */
469 if (!XD_BASIC_DBUS_TYPE (XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
))))
470 object
= XD_NEXT_VALUE (object
);
472 /* Open new subiteration. */
475 case DBUS_TYPE_ARRAY
:
476 /* An array has only elements of the same type. So it is
477 sufficient to check the first element's signature
481 /* If the array is empty, DBUS_TYPE_STRING is the default
483 strcpy (signature
, DBUS_TYPE_STRING_AS_STRING
);
486 /* If the element type is DBUS_TYPE_SIGNATURE, and this is
487 the only element, the value of this element is used as
488 the array's element signature. */
489 if ((XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
))
490 == DBUS_TYPE_SIGNATURE
)
491 && STRINGP (CAR_SAFE (XD_NEXT_VALUE (object
)))
492 && NILP (CDR_SAFE (XD_NEXT_VALUE (object
))))
494 strcpy (signature
, SDATA (CAR_SAFE (XD_NEXT_VALUE (object
))));
495 object
= CDR_SAFE (XD_NEXT_VALUE (object
));
499 xd_signature (signature
,
500 XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
)),
501 dtype
, CAR_SAFE (XD_NEXT_VALUE (object
)));
503 XD_DEBUG_MESSAGE ("%c %s %s", dtype
, signature
,
504 SDATA (format2 ("%s", object
, Qnil
)));
505 if (!dbus_message_iter_open_container (iter
, dtype
,
506 signature
, &subiter
))
507 xsignal3 (Qdbus_error
,
508 build_string ("Cannot open container"),
509 make_number (dtype
), build_string (signature
));
512 case DBUS_TYPE_VARIANT
:
513 /* A variant has just one element. */
514 xd_signature (signature
, XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
)),
515 dtype
, CAR_SAFE (XD_NEXT_VALUE (object
)));
517 XD_DEBUG_MESSAGE ("%c %s %s", dtype
, signature
,
518 SDATA (format2 ("%s", object
, Qnil
)));
519 if (!dbus_message_iter_open_container (iter
, dtype
,
520 signature
, &subiter
))
521 xsignal3 (Qdbus_error
,
522 build_string ("Cannot open container"),
523 make_number (dtype
), build_string (signature
));
526 case DBUS_TYPE_STRUCT
:
527 case DBUS_TYPE_DICT_ENTRY
:
528 /* These containers do not require a signature. */
529 XD_DEBUG_MESSAGE ("%c %s", dtype
,
530 SDATA (format2 ("%s", object
, Qnil
)));
531 if (!dbus_message_iter_open_container (iter
, dtype
, NULL
, &subiter
))
532 xsignal2 (Qdbus_error
,
533 build_string ("Cannot open container"),
534 make_number (dtype
));
538 /* Loop over list elements. */
539 while (!NILP (object
))
541 dtype
= XD_OBJECT_TO_DBUS_TYPE (CAR_SAFE (object
));
542 object
= XD_NEXT_VALUE (object
);
544 xd_append_arg (dtype
, CAR_SAFE (object
), &subiter
);
546 object
= CDR_SAFE (object
);
549 /* Close the subiteration. */
550 if (!dbus_message_iter_close_container (iter
, &subiter
))
551 xsignal2 (Qdbus_error
,
552 build_string ("Cannot close container"),
553 make_number (dtype
));
557 /* Retrieve C value from a DBusMessageIter structure ITER, and return
558 a converted Lisp object. The type DTYPE of the argument of the
559 D-Bus message must be a valid DBusType. Compound D-Bus types
560 result always in a Lisp list. */
562 xd_retrieve_arg (dtype
, iter
)
564 DBusMessageIter
*iter
;
572 dbus_message_iter_get_basic (iter
, &val
);
574 XD_DEBUG_MESSAGE ("%c %d", dtype
, val
);
575 return make_number (val
);
578 case DBUS_TYPE_BOOLEAN
:
581 dbus_message_iter_get_basic (iter
, &val
);
582 XD_DEBUG_MESSAGE ("%c %s", dtype
, (val
== FALSE
) ? "false" : "true");
583 return (val
== FALSE
) ? Qnil
: Qt
;
586 case DBUS_TYPE_INT16
:
587 case DBUS_TYPE_UINT16
:
590 dbus_message_iter_get_basic (iter
, &val
);
591 XD_DEBUG_MESSAGE ("%c %d", dtype
, val
);
592 return make_number (val
);
595 case DBUS_TYPE_INT32
:
596 case DBUS_TYPE_UINT32
:
598 /* Assignment to EMACS_INT stops GCC whining about limited
599 range of data type. */
602 dbus_message_iter_get_basic (iter
, &val
);
603 XD_DEBUG_MESSAGE ("%c %d", dtype
, val
);
605 return make_fixnum_or_float (val1
);
608 case DBUS_TYPE_INT64
:
609 case DBUS_TYPE_UINT64
:
612 dbus_message_iter_get_basic (iter
, &val
);
613 XD_DEBUG_MESSAGE ("%c %d", dtype
, (int) val
);
614 return make_fixnum_or_float (val
);
617 case DBUS_TYPE_DOUBLE
:
620 dbus_message_iter_get_basic (iter
, &val
);
621 XD_DEBUG_MESSAGE ("%c %f", dtype
, val
);
622 return make_float (val
);
625 case DBUS_TYPE_STRING
:
626 case DBUS_TYPE_OBJECT_PATH
:
627 case DBUS_TYPE_SIGNATURE
:
630 dbus_message_iter_get_basic (iter
, &val
);
631 XD_DEBUG_MESSAGE ("%c %s", dtype
, val
);
632 return build_string (val
);
635 case DBUS_TYPE_ARRAY
:
636 case DBUS_TYPE_VARIANT
:
637 case DBUS_TYPE_STRUCT
:
638 case DBUS_TYPE_DICT_ENTRY
:
644 DBusMessageIter subiter
;
646 dbus_message_iter_recurse (iter
, &subiter
);
647 while ((subtype
= dbus_message_iter_get_arg_type (&subiter
))
648 != DBUS_TYPE_INVALID
)
650 result
= Fcons (xd_retrieve_arg (subtype
, &subiter
), result
);
651 dbus_message_iter_next (&subiter
);
653 XD_DEBUG_MESSAGE ("%c %s", dtype
, SDATA (format2 ("%s", result
, Qnil
)));
654 RETURN_UNGCPRO (Fnreverse (result
));
658 XD_DEBUG_MESSAGE ("DBusType '%c' not supported", dtype
);
663 /* Initialize D-Bus connection. BUS is a Lisp symbol, either :system
664 or :session. It tells which D-Bus to be initialized. */
669 DBusConnection
*connection
;
672 /* Parameter check. */
674 if (!((EQ (bus
, QCdbus_system_bus
)) || (EQ (bus
, QCdbus_session_bus
))))
675 xsignal2 (Qdbus_error
, build_string ("Wrong bus name"), bus
);
677 /* Open a connection to the bus. */
678 dbus_error_init (&derror
);
680 if (EQ (bus
, QCdbus_system_bus
))
681 connection
= dbus_bus_get (DBUS_BUS_SYSTEM
, &derror
);
683 connection
= dbus_bus_get (DBUS_BUS_SESSION
, &derror
);
685 if (dbus_error_is_set (&derror
))
688 if (connection
== NULL
)
689 xsignal2 (Qdbus_error
, build_string ("No connection"), bus
);
691 /* Return the result. */
695 DEFUN ("dbus-get-unique-name", Fdbus_get_unique_name
, Sdbus_get_unique_name
,
697 doc
: /* Return the unique name of Emacs registered at D-Bus BUS. */)
701 DBusConnection
*connection
;
704 /* Check parameters. */
707 /* Open a connection to the bus. */
708 connection
= xd_initialize (bus
);
710 /* Request the name. */
711 name
= dbus_bus_get_unique_name (connection
);
713 xsignal1 (Qdbus_error
, build_string ("No unique name available"));
716 return build_string (name
);
719 DEFUN ("dbus-call-method", Fdbus_call_method
, Sdbus_call_method
, 5, MANY
, 0,
720 doc
: /* Call METHOD on the D-Bus BUS.
722 BUS is either the symbol `:system' or the symbol `:session'.
724 SERVICE is the D-Bus service name to be used. PATH is the D-Bus
725 object path SERVICE is registered at. INTERFACE is an interface
726 offered by SERVICE. It must provide METHOD.
728 If the parameter `:timeout' is given, the following integer TIMEOUT
729 specifies the maximun number of milliseconds the method call must
730 return. The default value is 25.000. If the method call doesn't
731 return in time, a D-Bus error is raised.
733 All other arguments ARGS are passed to METHOD as arguments. They are
734 converted into D-Bus types via the following rules:
736 t and nil => DBUS_TYPE_BOOLEAN
737 number => DBUS_TYPE_UINT32
738 integer => DBUS_TYPE_INT32
739 float => DBUS_TYPE_DOUBLE
740 string => DBUS_TYPE_STRING
741 list => DBUS_TYPE_ARRAY
743 All arguments can be preceded by a type symbol. For details about
744 type symbols, see Info node `(dbus)Type Conversion'.
746 `dbus-call-method' returns the resulting values of METHOD as a list of
747 Lisp objects. The type conversion happens the other direction as for
748 input arguments. It follows the mapping rules:
750 DBUS_TYPE_BOOLEAN => t or nil
751 DBUS_TYPE_BYTE => number
752 DBUS_TYPE_UINT16 => number
753 DBUS_TYPE_INT16 => integer
754 DBUS_TYPE_UINT32 => number or float
755 DBUS_TYPE_INT32 => integer or float
756 DBUS_TYPE_UINT64 => number or float
757 DBUS_TYPE_INT64 => integer or float
758 DBUS_TYPE_DOUBLE => float
759 DBUS_TYPE_STRING => string
760 DBUS_TYPE_OBJECT_PATH => string
761 DBUS_TYPE_SIGNATURE => string
762 DBUS_TYPE_ARRAY => list
763 DBUS_TYPE_VARIANT => list
764 DBUS_TYPE_STRUCT => list
765 DBUS_TYPE_DICT_ENTRY => list
770 :session "org.gnome.seahorse" "/org/gnome/seahorse/keys/openpgp"
771 "org.gnome.seahorse.Keys" "GetKeyField"
772 "openpgp:657984B8C7A966DD" "simple-name")
774 => (t ("Philip R. Zimmermann"))
776 If the result of the METHOD call is just one value, the converted Lisp
777 object is returned instead of a list containing this single Lisp object.
780 :system "org.freedesktop.Hal" "/org/freedesktop/Hal/devices/computer"
781 "org.freedesktop.Hal.Device" "GetPropertyString"
782 "system.kernel.machine")
786 usage: (dbus-call-method
787 BUS SERVICE PATH INTERFACE METHOD
788 &optional :timeout TIMEOUT &rest ARGS) */)
791 register Lisp_Object
*args
;
793 Lisp_Object bus
, service
, path
, interface
, method
;
795 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
796 DBusConnection
*connection
;
797 DBusMessage
*dmessage
;
799 DBusMessageIter iter
;
804 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
806 /* Check parameters. */
814 CHECK_STRING (service
);
816 CHECK_STRING (interface
);
817 CHECK_STRING (method
);
818 GCPRO5 (bus
, service
, path
, interface
, method
);
820 XD_DEBUG_MESSAGE ("%s %s %s %s",
826 /* Open a connection to the bus. */
827 connection
= xd_initialize (bus
);
829 /* Create the message. */
830 dmessage
= dbus_message_new_method_call (SDATA (service
),
835 if (dmessage
== NULL
)
836 xsignal1 (Qdbus_error
, build_string ("Unable to create a new message"));
838 /* Check for timeout parameter. */
839 if ((i
+2 <= nargs
) && (EQ ((args
[i
]), QCdbus_timeout
)))
841 CHECK_NATNUM (args
[i
+1]);
842 timeout
= XUINT (args
[i
+1]);
846 /* Initialize parameter list of message. */
847 dbus_message_iter_init_append (dmessage
, &iter
);
849 /* Append parameters to the message. */
850 for (; i
< nargs
; ++i
)
852 dtype
= XD_OBJECT_TO_DBUS_TYPE (args
[i
]);
853 if (XD_DBUS_TYPE_P (args
[i
]))
855 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
]);
856 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
+1]);
857 XD_DEBUG_MESSAGE ("Parameter%d %s %s", i
-4,
858 SDATA (format2 ("%s", args
[i
], Qnil
)),
859 SDATA (format2 ("%s", args
[i
+1], Qnil
)));
864 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
]);
865 XD_DEBUG_MESSAGE ("Parameter%d %s", i
-4,
866 SDATA (format2 ("%s", args
[i
], Qnil
)));
869 /* Check for valid signature. We use DBUS_TYPE_INVALID as
870 indication that there is no parent type. */
871 xd_signature (signature
, dtype
, DBUS_TYPE_INVALID
, args
[i
]);
873 xd_append_arg (dtype
, args
[i
], &iter
);
876 /* Send the message. */
877 dbus_error_init (&derror
);
878 reply
= dbus_connection_send_with_reply_and_block (connection
,
883 if (dbus_error_is_set (&derror
))
887 xsignal1 (Qdbus_error
, build_string ("No reply"));
889 XD_DEBUG_MESSAGE ("Message sent");
891 /* Collect the results. */
895 if (dbus_message_iter_init (reply
, &iter
))
897 /* Loop over the parameters of the D-Bus reply message. Construct a
898 Lisp list, which is returned by `dbus-call-method'. */
899 while ((dtype
= dbus_message_iter_get_arg_type (&iter
))
900 != DBUS_TYPE_INVALID
)
902 result
= Fcons (xd_retrieve_arg (dtype
, &iter
), result
);
903 dbus_message_iter_next (&iter
);
908 /* No arguments: just return nil. */
912 dbus_message_unref (dmessage
);
913 dbus_message_unref (reply
);
915 /* Return the result. If there is only one single Lisp object,
916 return it as-it-is, otherwise return the reversed list. */
917 if (XUINT (Flength (result
)) == 1)
918 RETURN_UNGCPRO (CAR_SAFE (result
));
920 RETURN_UNGCPRO (Fnreverse (result
));
923 DEFUN ("dbus-method-return-internal", Fdbus_method_return_internal
,
924 Sdbus_method_return_internal
,
926 doc
: /* Return for message SERIAL on the D-Bus BUS.
927 This is an internal function, it shall not be used outside dbus.el.
929 usage: (dbus-method-return-internal BUS SERIAL SERVICE &rest ARGS) */)
932 register Lisp_Object
*args
;
934 Lisp_Object bus
, serial
, service
;
935 struct gcpro gcpro1
, gcpro2
, gcpro3
;
936 DBusConnection
*connection
;
937 DBusMessage
*dmessage
;
938 DBusMessageIter iter
;
941 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
943 /* Check parameters. */
949 CHECK_NUMBER (serial
);
950 CHECK_STRING (service
);
951 GCPRO3 (bus
, serial
, service
);
953 XD_DEBUG_MESSAGE ("%d %s ", XUINT (serial
), SDATA (service
));
955 /* Open a connection to the bus. */
956 connection
= xd_initialize (bus
);
958 /* Create the message. */
959 dmessage
= dbus_message_new (DBUS_MESSAGE_TYPE_METHOD_RETURN
);
960 if ((dmessage
== NULL
)
961 || (!dbus_message_set_reply_serial (dmessage
, XUINT (serial
)))
962 || (!dbus_message_set_destination (dmessage
, SDATA (service
))))
965 xsignal1 (Qdbus_error
,
966 build_string ("Unable to create a return message"));
971 /* Initialize parameter list of message. */
972 dbus_message_iter_init_append (dmessage
, &iter
);
974 /* Append parameters to the message. */
975 for (i
= 3; i
< nargs
; ++i
)
977 dtype
= XD_OBJECT_TO_DBUS_TYPE (args
[i
]);
978 if (XD_DBUS_TYPE_P (args
[i
]))
980 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
]);
981 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
+1]);
982 XD_DEBUG_MESSAGE ("Parameter%d %s %s", i
-2,
983 SDATA (format2 ("%s", args
[i
], Qnil
)),
984 SDATA (format2 ("%s", args
[i
+1], Qnil
)));
989 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
]);
990 XD_DEBUG_MESSAGE ("Parameter%d %s", i
-2,
991 SDATA (format2 ("%s", args
[i
], Qnil
)));
994 /* Check for valid signature. We use DBUS_TYPE_INVALID as
995 indication that there is no parent type. */
996 xd_signature (signature
, dtype
, DBUS_TYPE_INVALID
, args
[i
]);
998 xd_append_arg (dtype
, args
[i
], &iter
);
1001 /* Send the message. The message is just added to the outgoing
1003 if (!dbus_connection_send (connection
, dmessage
, NULL
))
1004 xsignal1 (Qdbus_error
, build_string ("Cannot send message"));
1006 /* Flush connection to ensure the message is handled. */
1007 dbus_connection_flush (connection
);
1009 XD_DEBUG_MESSAGE ("Message sent");
1012 dbus_message_unref (dmessage
);
1018 DEFUN ("dbus-send-signal", Fdbus_send_signal
, Sdbus_send_signal
, 5, MANY
, 0,
1019 doc
: /* Send signal SIGNAL on the D-Bus BUS.
1021 BUS is either the symbol `:system' or the symbol `:session'.
1023 SERVICE is the D-Bus service name SIGNAL is sent from. PATH is the
1024 D-Bus object path SERVICE is registered at. INTERFACE is an interface
1025 offered by SERVICE. It must provide signal SIGNAL.
1027 All other arguments ARGS are passed to SIGNAL as arguments. They are
1028 converted into D-Bus types via the following rules:
1030 t and nil => DBUS_TYPE_BOOLEAN
1031 number => DBUS_TYPE_UINT32
1032 integer => DBUS_TYPE_INT32
1033 float => DBUS_TYPE_DOUBLE
1034 string => DBUS_TYPE_STRING
1035 list => DBUS_TYPE_ARRAY
1037 All arguments can be preceded by a type symbol. For details about
1038 type symbols, see Info node `(dbus)Type Conversion'.
1043 :session "org.gnu.Emacs" "/org/gnu/Emacs"
1044 "org.gnu.Emacs.FileManager" "FileModified" "/home/albinus/.emacs")
1046 usage: (dbus-send-signal BUS SERVICE PATH INTERFACE SIGNAL &rest ARGS) */)
1049 register Lisp_Object
*args
;
1051 Lisp_Object bus
, service
, path
, interface
, signal
;
1052 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
1053 DBusConnection
*connection
;
1054 DBusMessage
*dmessage
;
1055 DBusMessageIter iter
;
1058 char signature
[DBUS_MAXIMUM_SIGNATURE_LENGTH
];
1060 /* Check parameters. */
1064 interface
= args
[3];
1068 CHECK_STRING (service
);
1069 CHECK_STRING (path
);
1070 CHECK_STRING (interface
);
1071 CHECK_STRING (signal
);
1072 GCPRO5 (bus
, service
, path
, interface
, signal
);
1074 XD_DEBUG_MESSAGE ("%s %s %s %s",
1080 /* Open a connection to the bus. */
1081 connection
= xd_initialize (bus
);
1083 /* Create the message. */
1084 dmessage
= dbus_message_new_signal (SDATA (path
),
1088 if (dmessage
== NULL
)
1089 xsignal1 (Qdbus_error
, build_string ("Unable to create a new message"));
1091 /* Initialize parameter list of message. */
1092 dbus_message_iter_init_append (dmessage
, &iter
);
1094 /* Append parameters to the message. */
1095 for (i
= 5; i
< nargs
; ++i
)
1097 dtype
= XD_OBJECT_TO_DBUS_TYPE (args
[i
]);
1098 if (XD_DBUS_TYPE_P (args
[i
]))
1100 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
]);
1101 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
+1]);
1102 XD_DEBUG_MESSAGE ("Parameter%d %s %s", i
-4,
1103 SDATA (format2 ("%s", args
[i
], Qnil
)),
1104 SDATA (format2 ("%s", args
[i
+1], Qnil
)));
1109 XD_DEBUG_VALID_LISP_OBJECT_P (args
[i
]);
1110 XD_DEBUG_MESSAGE ("Parameter%d %s", i
-4,
1111 SDATA (format2 ("%s", args
[i
], Qnil
)));
1114 /* Check for valid signature. We use DBUS_TYPE_INVALID as
1115 indication that there is no parent type. */
1116 xd_signature (signature
, dtype
, DBUS_TYPE_INVALID
, args
[i
]);
1118 xd_append_arg (dtype
, args
[i
], &iter
);
1121 /* Send the message. The message is just added to the outgoing
1123 if (!dbus_connection_send (connection
, dmessage
, NULL
))
1124 xsignal1 (Qdbus_error
, build_string ("Cannot send message"));
1126 /* Flush connection to ensure the message is handled. */
1127 dbus_connection_flush (connection
);
1129 XD_DEBUG_MESSAGE ("Signal sent");
1132 dbus_message_unref (dmessage
);
1138 /* Read queued incoming message of the D-Bus BUS. BUS is a Lisp
1139 symbol, either :system or :session. */
1141 xd_read_message (bus
)
1144 Lisp_Object args
, key
, value
;
1145 struct gcpro gcpro1
;
1146 struct input_event event
;
1147 DBusConnection
*connection
;
1148 DBusMessage
*dmessage
;
1149 DBusMessageIter iter
;
1152 const char *uname
, *path
, *interface
, *member
;
1154 /* Open a connection to the bus. */
1155 connection
= xd_initialize (bus
);
1157 /* Non blocking read of the next available message. */
1158 dbus_connection_read_write (connection
, 0);
1159 dmessage
= dbus_connection_pop_message (connection
);
1161 /* Return if there is no queued message. */
1162 if (dmessage
== NULL
)
1165 /* Collect the parameters. */
1169 /* Loop over the resulting parameters. Construct a list. */
1170 if (dbus_message_iter_init (dmessage
, &iter
))
1172 while ((dtype
= dbus_message_iter_get_arg_type (&iter
))
1173 != DBUS_TYPE_INVALID
)
1175 args
= Fcons (xd_retrieve_arg (dtype
, &iter
), args
);
1176 dbus_message_iter_next (&iter
);
1178 /* The arguments are stored in reverse order. Reorder them. */
1179 args
= Fnreverse (args
);
1182 /* Read message type, unique name, object path, interface and member
1183 from the message. */
1184 mtype
= dbus_message_get_type (dmessage
);
1185 uname
= dbus_message_get_sender (dmessage
);
1186 path
= dbus_message_get_path (dmessage
);
1187 interface
= dbus_message_get_interface (dmessage
);
1188 member
= dbus_message_get_member (dmessage
);
1190 /* Vdbus_registered_functions_table requires non-nil interface and member. */
1191 if ((NULL
== interface
) || (NULL
== member
))
1194 XD_DEBUG_MESSAGE ("Event received: %d %s %s %s %s %s",
1195 mtype
, uname
, path
, interface
, member
,
1196 SDATA (format2 ("%s", args
, Qnil
)));
1198 /* Search for a registered function of the message. */
1199 key
= list3 (bus
, build_string (interface
), build_string (member
));
1200 value
= Fgethash (key
, Vdbus_registered_functions_table
, Qnil
);
1202 /* Loop over the registered functions. Construct an event. */
1203 while (!NILP (value
))
1205 key
= CAR_SAFE (value
);
1206 /* key has the structure (UNAME SERVICE PATH HANDLER). */
1207 if (((uname
== NULL
)
1208 || (NILP (CAR_SAFE (key
)))
1209 || (strcmp (uname
, SDATA (CAR_SAFE (key
))) == 0))
1211 || (NILP (CAR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))
1212 || (strcmp (path
, SDATA (CAR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))
1214 && (!NILP (CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (key
)))))))
1217 event
.kind
= DBUS_EVENT
;
1218 event
.frame_or_window
= Qnil
;
1219 event
.arg
= Fcons (CAR_SAFE (CDR_SAFE (CDR_SAFE (CDR_SAFE (key
)))),
1222 /* Add uname, path, interface and member to the event. */
1223 event
.arg
= Fcons (build_string (member
), event
.arg
);
1224 event
.arg
= Fcons (build_string (interface
), event
.arg
);
1225 event
.arg
= Fcons ((path
== NULL
? Qnil
: build_string (path
)),
1227 event
.arg
= Fcons ((uname
== NULL
? Qnil
: build_string (uname
)),
1230 /* Add the message serial if needed, or nil. */
1231 event
.arg
= Fcons ((mtype
== DBUS_MESSAGE_TYPE_METHOD_CALL
1232 ? make_number (dbus_message_get_serial (dmessage
))
1236 /* Add the bus symbol to the event. */
1237 event
.arg
= Fcons (bus
, event
.arg
);
1239 /* Store it into the input event queue. */
1240 kbd_buffer_store_event (&event
);
1242 value
= CDR_SAFE (value
);
1246 dbus_message_unref (dmessage
);
1247 RETURN_UNGCPRO (Qnil
);
1250 /* Read queued incoming messages from the system and session buses. */
1252 xd_read_queued_messages ()
1255 /* Vdbus_registered_functions_table will be initialized as hash
1256 table in dbus.el. When this package isn't loaded yet, it doesn't
1257 make sense to handle D-Bus messages. Furthermore, we ignore all
1258 Lisp errors during the call. */
1259 if (HASH_TABLE_P (Vdbus_registered_functions_table
))
1261 internal_condition_case_1 (xd_read_message
, QCdbus_system_bus
,
1263 internal_condition_case_1 (xd_read_message
, QCdbus_session_bus
,
1268 DEFUN ("dbus-register-signal", Fdbus_register_signal
, Sdbus_register_signal
,
1270 doc
: /* Register for signal SIGNAL on the D-Bus BUS.
1272 BUS is either the symbol `:system' or the symbol `:session'.
1274 SERVICE is the D-Bus service name used by the sending D-Bus object.
1275 It can be either a known name or the unique name of the D-Bus object
1276 sending the signal. When SERVICE is nil, related signals from all
1277 D-Bus objects shall be accepted.
1279 PATH is the D-Bus object path SERVICE is registered. It can also be
1280 nil if the path name of incoming signals shall not be checked.
1282 INTERFACE is an interface offered by SERVICE. It must provide SIGNAL.
1283 HANDLER is a Lisp function to be called when the signal is received.
1284 It must accept as arguments the values SIGNAL is sending.
1286 All other arguments ARGS, if specified, must be strings. They stand
1287 for the respective arguments of the signal in their order, and are
1288 used for filtering as well. A nil argument might be used to preserve
1291 INTERFACE, SIGNAL and HANDLER must not be nil. Example:
1293 \(defun my-signal-handler (device)
1294 (message "Device %s added" device))
1296 \(dbus-register-signal
1297 :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1298 "org.freedesktop.Hal.Manager" "DeviceAdded" 'my-signal-handler)
1300 => ((:system "org.freedesktop.Hal.Manager" "DeviceAdded")
1301 ("org.freedesktop.Hal" "/org/freedesktop/Hal/Manager" my-signal-handler))
1303 `dbus-register-signal' returns an object, which can be used in
1304 `dbus-unregister-object' for removing the registration.
1306 usage: (dbus-register-signal BUS SERVICE PATH INTERFACE SIGNAL HANDLER &rest ARGS) */)
1309 register Lisp_Object
*args
;
1311 Lisp_Object bus
, service
, path
, interface
, signal
, handler
;
1312 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
, gcpro6
;
1313 Lisp_Object uname
, key
, key1
, value
;
1314 DBusConnection
*connection
;
1316 char rule
[DBUS_MAXIMUM_MATCH_RULE_LENGTH
];
1317 char x
[DBUS_MAXIMUM_MATCH_RULE_LENGTH
];
1320 /* Check parameters. */
1324 interface
= args
[3];
1329 if (!NILP (service
)) CHECK_STRING (service
);
1330 if (!NILP (path
)) CHECK_STRING (path
);
1331 CHECK_STRING (interface
);
1332 CHECK_STRING (signal
);
1333 if (!FUNCTIONP (handler
))
1334 wrong_type_argument (intern ("functionp"), handler
);
1335 GCPRO6 (bus
, service
, path
, interface
, signal
, handler
);
1337 /* Retrieve unique name of service. If service is a known name, we
1338 will register for the corresponding unique name, if any. Signals
1339 are sent always with the unique name as sender. Note: the unique
1340 name of "org.freedesktop.DBus" is that string itself. */
1341 if ((STRINGP (service
))
1342 && (SBYTES (service
) > 0)
1343 && (strcmp (SDATA (service
), DBUS_SERVICE_DBUS
) != 0)
1344 && (strncmp (SDATA (service
), ":", 1) != 0))
1346 uname
= call2 (intern ("dbus-get-name-owner"), bus
, service
);
1347 /* When there is no unique name, we mark it with an empty
1350 uname
= build_string ("");
1355 /* Create a matching rule if the unique name exists (when no
1357 if (NILP (uname
) || (SBYTES (uname
) > 0))
1359 /* Open a connection to the bus. */
1360 connection
= xd_initialize (bus
);
1362 /* Create a rule to receive related signals. */
1364 "type='signal',interface='%s',member='%s'",
1368 /* Add unique name and path to the rule if they are non-nil. */
1371 sprintf (x
, ",sender='%s'", SDATA (uname
));
1377 sprintf (x
, ",path='%s'", SDATA (path
));
1381 /* Add arguments to the rule if they are non-nil. */
1382 for (i
= 6; i
< nargs
; ++i
)
1383 if (!NILP (args
[i
]))
1385 CHECK_STRING (args
[i
]);
1386 sprintf (x
, ",arg%d='%s'", i
-6, SDATA (args
[i
]));
1390 /* Add the rule to the bus. */
1391 dbus_error_init (&derror
);
1392 dbus_bus_add_match (connection
, rule
, &derror
);
1393 if (dbus_error_is_set (&derror
))
1399 XD_DEBUG_MESSAGE ("Matching rule \"%s\" created", rule
);
1402 /* Create a hash table entry. */
1403 key
= list3 (bus
, interface
, signal
);
1404 key1
= list4 (uname
, service
, path
, handler
);
1405 value
= Fgethash (key
, Vdbus_registered_functions_table
, Qnil
);
1407 if (NILP (Fmember (key1
, value
)))
1408 Fputhash (key
, Fcons (key1
, value
), Vdbus_registered_functions_table
);
1410 /* Return object. */
1411 RETURN_UNGCPRO (list2 (key
, list3 (service
, path
, handler
)));
1414 DEFUN ("dbus-register-method", Fdbus_register_method
, Sdbus_register_method
,
1416 doc
: /* Register for method METHOD on the D-Bus BUS.
1418 BUS is either the symbol `:system' or the symbol `:session'.
1420 SERVICE is the D-Bus service name of the D-Bus object METHOD is
1421 registered for. It must be a known name.
1423 PATH is the D-Bus object path SERVICE is registered. INTERFACE is the
1424 interface offered by SERVICE. It must provide METHOD. HANDLER is a
1425 Lisp function to be called when a method call is received. It must
1426 accept the input arguments of METHOD. The return value of HANDLER is
1427 used for composing the returning D-Bus message. */)
1428 (bus
, service
, path
, interface
, method
, handler
)
1429 Lisp_Object bus
, service
, path
, interface
, method
, handler
;
1431 Lisp_Object key
, key1
, value
;
1432 DBusConnection
*connection
;
1436 /* Check parameters. */
1438 CHECK_STRING (service
);
1439 CHECK_STRING (path
);
1440 CHECK_STRING (interface
);
1441 CHECK_STRING (method
);
1442 if (!FUNCTIONP (handler
))
1443 wrong_type_argument (intern ("functionp"), handler
);
1444 /* TODO: We must check for a valid service name, otherwise there is
1445 a segmentation fault. */
1447 /* Open a connection to the bus. */
1448 connection
= xd_initialize (bus
);
1450 /* Request the known name from the bus. We can ignore the result,
1451 it is set to -1 if there is an error - kind of redundancy. */
1452 dbus_error_init (&derror
);
1453 result
= dbus_bus_request_name (connection
, SDATA (service
), 0, &derror
);
1454 if (dbus_error_is_set (&derror
))
1457 /* Create a hash table entry. */
1458 key
= list3 (bus
, interface
, method
);
1459 key1
= list4 (Qnil
, service
, path
, handler
);
1460 value
= Fgethash (key
, Vdbus_registered_functions_table
, Qnil
);
1462 /* We use nil for the unique name, because the method might be
1463 called from everybody. */
1464 if (NILP (Fmember (key1
, value
)))
1465 Fputhash (key
, Fcons (key1
, value
), Vdbus_registered_functions_table
);
1467 /* Return object. */
1468 return list2 (key
, list3 (service
, path
, handler
));
1476 Qdbus_get_unique_name
= intern ("dbus-get-unique-name");
1477 staticpro (&Qdbus_get_unique_name
);
1478 defsubr (&Sdbus_get_unique_name
);
1480 Qdbus_call_method
= intern ("dbus-call-method");
1481 staticpro (&Qdbus_call_method
);
1482 defsubr (&Sdbus_call_method
);
1484 Qdbus_method_return_internal
= intern ("dbus-method-return-internal");
1485 staticpro (&Qdbus_method_return_internal
);
1486 defsubr (&Sdbus_method_return_internal
);
1488 Qdbus_send_signal
= intern ("dbus-send-signal");
1489 staticpro (&Qdbus_send_signal
);
1490 defsubr (&Sdbus_send_signal
);
1492 Qdbus_register_signal
= intern ("dbus-register-signal");
1493 staticpro (&Qdbus_register_signal
);
1494 defsubr (&Sdbus_register_signal
);
1496 Qdbus_register_method
= intern ("dbus-register-method");
1497 staticpro (&Qdbus_register_method
);
1498 defsubr (&Sdbus_register_method
);
1500 Qdbus_error
= intern ("dbus-error");
1501 staticpro (&Qdbus_error
);
1502 Fput (Qdbus_error
, Qerror_conditions
,
1503 list2 (Qdbus_error
, Qerror
));
1504 Fput (Qdbus_error
, Qerror_message
,
1505 build_string ("D-Bus error"));
1507 QCdbus_system_bus
= intern (":system");
1508 staticpro (&QCdbus_system_bus
);
1510 QCdbus_session_bus
= intern (":session");
1511 staticpro (&QCdbus_session_bus
);
1513 QCdbus_timeout
= intern (":timeout");
1514 staticpro (&QCdbus_timeout
);
1516 QCdbus_type_byte
= intern (":byte");
1517 staticpro (&QCdbus_type_byte
);
1519 QCdbus_type_boolean
= intern (":boolean");
1520 staticpro (&QCdbus_type_boolean
);
1522 QCdbus_type_int16
= intern (":int16");
1523 staticpro (&QCdbus_type_int16
);
1525 QCdbus_type_uint16
= intern (":uint16");
1526 staticpro (&QCdbus_type_uint16
);
1528 QCdbus_type_int32
= intern (":int32");
1529 staticpro (&QCdbus_type_int32
);
1531 QCdbus_type_uint32
= intern (":uint32");
1532 staticpro (&QCdbus_type_uint32
);
1534 QCdbus_type_int64
= intern (":int64");
1535 staticpro (&QCdbus_type_int64
);
1537 QCdbus_type_uint64
= intern (":uint64");
1538 staticpro (&QCdbus_type_uint64
);
1540 QCdbus_type_double
= intern (":double");
1541 staticpro (&QCdbus_type_double
);
1543 QCdbus_type_string
= intern (":string");
1544 staticpro (&QCdbus_type_string
);
1546 QCdbus_type_object_path
= intern (":object-path");
1547 staticpro (&QCdbus_type_object_path
);
1549 QCdbus_type_signature
= intern (":signature");
1550 staticpro (&QCdbus_type_signature
);
1552 QCdbus_type_array
= intern (":array");
1553 staticpro (&QCdbus_type_array
);
1555 QCdbus_type_variant
= intern (":variant");
1556 staticpro (&QCdbus_type_variant
);
1558 QCdbus_type_struct
= intern (":struct");
1559 staticpro (&QCdbus_type_struct
);
1561 QCdbus_type_dict_entry
= intern (":dict-entry");
1562 staticpro (&QCdbus_type_dict_entry
);
1564 DEFVAR_LISP ("dbus-registered-functions-table",
1565 &Vdbus_registered_functions_table
,
1566 doc
: /* Hash table of registered functions for D-Bus.
1567 The key in the hash table is the list (BUS INTERFACE MEMBER). BUS is
1568 either the symbol `:system' or the symbol `:session'. INTERFACE is a
1569 string which denotes a D-Bus interface, and MEMBER, also a string, is
1570 either a method or a signal INTERFACE is offering. All arguments but
1571 BUS must not be nil.
1573 The value in the hash table is a list of quadruple lists
1574 \((UNAME SERVICE PATH HANDLER) (UNAME SERVICE PATH HANDLER) ...).
1575 SERVICE is the service name as registered, UNAME is the corresponding
1576 unique name. PATH is the object path of the sending object. All of
1577 them can be nil, which means a wildcard then. HANDLER is the function
1578 to be called when a D-Bus message, which matches the key criteria,
1580 /* We initialize Vdbus_registered_functions_table in dbus.el,
1581 because we need to define a hash table function first. */
1582 Vdbus_registered_functions_table
= Qnil
;
1584 DEFVAR_LISP ("dbus-debug", &Vdbus_debug
,
1585 doc
: /* If non-nil, debug messages of D-Bus bindings are raised. */);
1592 Fprovide (intern ("dbusbind"), Qnil
);
1596 #endif /* HAVE_DBUS */
1598 /* arch-tag: 0e828477-b571-4fe4-b559-5c9211bc14b8
1599 (do not change this comment) */