Improve dbus performance on synchronous calls
[bpt/emacs.git] / lisp / net / dbus.el
CommitLineData
3a993e3d
MA
1;;; dbus.el --- Elisp bindings for D-Bus.
2
ba318903 3;; Copyright (C) 2007-2014 Free Software Foundation, Inc.
3a993e3d
MA
4
5;; Author: Michael Albinus <michael.albinus@gmx.de>
6;; Keywords: comm, hardware
7
8;; This file is part of GNU Emacs.
9
874a927a 10;; GNU Emacs is free software: you can redistribute it and/or modify
3a993e3d 11;; it under the terms of the GNU General Public License as published by
874a927a
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
3a993e3d
MA
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
874a927a 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
3a993e3d
MA
22
23;;; Commentary:
24
25;; This package provides language bindings for the D-Bus API. D-Bus
26;; is a message bus system, a simple way for applications to talk to
27;; one another. See <http://dbus.freedesktop.org/> for details.
28
29;; Low-level language bindings are implemented in src/dbusbind.c.
30
dcbf5805
MA
31;; D-Bus support in the Emacs core can be disabled with configuration
32;; option "--without-dbus".
33
3a993e3d
MA
34;;; Code:
35
dcbf5805
MA
36;; Declare used subroutines and variables.
37(declare-function dbus-message-internal "dbusbind.c")
ba6f7d86 38(declare-function dbus-init-bus "dbusbind.c")
dcbf5805
MA
39(defvar dbus-message-type-invalid)
40(defvar dbus-message-type-method-call)
41(defvar dbus-message-type-method-return)
42(defvar dbus-message-type-error)
43(defvar dbus-message-type-signal)
6981d00a 44(defvar dbus-debug)
b172ed20 45(defvar dbus-registered-objects-table)
6981d00a
MA
46
47;; Pacify byte compiler.
f58e0fd5 48(eval-when-compile (require 'cl-lib))
7bb7efbd 49
3a993e3d
MA
50(require 'xml)
51
52(defconst dbus-service-dbus "org.freedesktop.DBus"
53 "The bus name used to talk to the bus itself.")
54
55(defconst dbus-path-dbus "/org/freedesktop/DBus"
56 "The object path used to talk to the bus itself.")
57
dcbf5805
MA
58;; Default D-Bus interfaces.
59
3a993e3d 60(defconst dbus-interface-dbus "org.freedesktop.DBus"
dcbf5805 61 "The interface exported by the service `dbus-service-dbus'.")
3a993e3d 62
4ba11bcb 63(defconst dbus-interface-peer (concat dbus-interface-dbus ".Peer")
dcbf5805
MA
64 "The interface for peer objects.
65See URL `http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-peer'.")
66
67;; <interface name="org.freedesktop.DBus.Peer">
68;; <method name="Ping">
69;; </method>
70;; <method name="GetMachineId">
71;; <arg name="machine_uuid" type="s" direction="out"/>
72;; </method>
73;; </interface>
4ba11bcb
MA
74
75(defconst dbus-interface-introspectable
76 (concat dbus-interface-dbus ".Introspectable")
dcbf5805
MA
77 "The interface supported by introspectable objects.
78See URL `http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-introspectable'.")
3a993e3d 79
dcbf5805
MA
80;; <interface name="org.freedesktop.DBus.Introspectable">
81;; <method name="Introspect">
82;; <arg name="data" type="s" direction="out"/>
83;; </method>
84;; </interface>
f636d3ca 85
dcbf5805
MA
86(defconst dbus-interface-properties (concat dbus-interface-dbus ".Properties")
87 "The interface for property objects.
88See URL `http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties'.")
89
90;; <interface name="org.freedesktop.DBus.Properties">
91;; <method name="Get">
92;; <arg name="interface" type="s" direction="in"/>
93;; <arg name="propname" type="s" direction="in"/>
94;; <arg name="value" type="v" direction="out"/>
95;; </method>
96;; <method name="Set">
97;; <arg name="interface" type="s" direction="in"/>
98;; <arg name="propname" type="s" direction="in"/>
99;; <arg name="value" type="v" direction="in"/>
100;; </method>
101;; <method name="GetAll">
102;; <arg name="interface" type="s" direction="in"/>
103;; <arg name="props" type="a{sv}" direction="out"/>
104;; </method>
105;; <signal name="PropertiesChanged">
106;; <arg name="interface" type="s"/>
107;; <arg name="changed_properties" type="a{sv}"/>
108;; <arg name="invalidated_properties" type="as"/>
109;; </signal>
110;; </interface>
111
112(defconst dbus-interface-objectmanager
113 (concat dbus-interface-dbus ".ObjectManager")
114 "The object manager interface.
115See URL `http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager'.")
116
117;; <interface name="org.freedesktop.DBus.ObjectManager">
118;; <method name="GetManagedObjects">
119;; <arg name="object_paths_interfaces_and_properties"
120;; type="a{oa{sa{sv}}}" direction="out"/>
121;; </method>
122;; <signal name="InterfacesAdded">
123;; <arg name="object_path" type="o"/>
124;; <arg name="interfaces_and_properties" type="a{sa{sv}}"/>
125;; </signal>
126;; <signal name="InterfacesRemoved">
127;; <arg name="object_path" type="o"/>
128;; <arg name="interfaces" type="as"/>
129;; </signal>
130;; </interface>
131
132;; Emacs defaults.
65b7cb2c
MA
133(defconst dbus-service-emacs "org.gnu.Emacs"
134 "The well known service name of Emacs.")
135
136(defconst dbus-path-emacs "/org/gnu/Emacs"
dcbf5805
MA
137 "The object path namespace used by Emacs.
138All object paths provided by the service `dbus-service-emacs'
139shall be subdirectories of this path.")
65b7cb2c 140
dcbf5805
MA
141(defconst dbus-interface-emacs "org.gnu.Emacs"
142 "The interface namespace used by Emacs.")
98c38bfc 143
dcbf5805 144;; D-Bus constants.
98c38bfc 145
246a286b
MA
146(defmacro dbus-ignore-errors (&rest body)
147 "Execute BODY; signal D-Bus error when `dbus-debug' is non-nil.
148Otherwise, return result of last form in BODY, or all other errors."
f291fe60 149 (declare (indent 0) (debug t))
246a286b
MA
150 `(condition-case err
151 (progn ,@body)
152 (dbus-error (when dbus-debug (signal (car err) (cdr err))))))
246a286b
MA
153(font-lock-add-keywords 'emacs-lisp-mode '("\\<dbus-ignore-errors\\>"))
154
d1069532
SM
155(define-obsolete-variable-alias 'dbus-event-error-hooks
156 'dbus-event-error-functions "24.3")
157(defvar dbus-event-error-functions nil
e12c189f 158 "Functions to be called when a D-Bus error happens in the event handler.
f213fc09 159Every function must accept two arguments, the event and the error variable
333f9019 160caught in `condition-case' by `dbus-error'.")
e12c189f 161
5363d8ea 162\f
dcbf5805 163;;; Basic D-Bus message functions.
5363d8ea 164
98c38bfc
MA
165(defvar dbus-return-values-table (make-hash-table :test 'equal)
166 "Hash table for temporary storing arguments of reply messages.
dcbf5805
MA
167A key in this hash table is a list (:serial BUS SERIAL), like in
168`dbus-registered-objects-table'. BUS is either a Lisp symbol,
169`:system' or `:session', or a string denoting the bus address.
170SERIAL is the serial number of the reply message.")
171
172(defun dbus-call-method-handler (&rest args)
173 "Handler for reply messages of asynchronous D-Bus message calls.
174It calls the function stored in `dbus-registered-objects-table'.
175The result will be made available in `dbus-return-values-table'."
176 (puthash (list :serial
177 (dbus-event-bus-name last-input-event)
178 (dbus-event-serial-number last-input-event))
179 (if (= (length args) 1) (car args) args)
180 dbus-return-values-table))
181
182(defun dbus-call-method (bus service path interface method &rest args)
183 "Call METHOD on the D-Bus BUS.
184
185BUS is either a Lisp symbol, `:system' or `:session', or a string
186denoting the bus address.
187
188SERVICE is the D-Bus service name to be used. PATH is the D-Bus
189object path SERVICE is registered at. INTERFACE is an interface
190offered by SERVICE. It must provide METHOD.
191
192If the parameter `:timeout' is given, the following integer TIMEOUT
193specifies the maximum number of milliseconds the method call must
194return. The default value is 25,000. If the method call doesn't
195return in time, a D-Bus error is raised.
196
197All other arguments ARGS are passed to METHOD as arguments. They are
198converted into D-Bus types via the following rules:
199
200 t and nil => DBUS_TYPE_BOOLEAN
201 number => DBUS_TYPE_UINT32
202 integer => DBUS_TYPE_INT32
203 float => DBUS_TYPE_DOUBLE
204 string => DBUS_TYPE_STRING
205 list => DBUS_TYPE_ARRAY
206
207All arguments can be preceded by a type symbol. For details about
208type symbols, see Info node `(dbus)Type Conversion'.
209
210`dbus-call-method' returns the resulting values of METHOD as a list of
211Lisp objects. The type conversion happens the other direction as for
212input arguments. It follows the mapping rules:
213
214 DBUS_TYPE_BOOLEAN => t or nil
215 DBUS_TYPE_BYTE => number
216 DBUS_TYPE_UINT16 => number
217 DBUS_TYPE_INT16 => integer
218 DBUS_TYPE_UINT32 => number or float
219 DBUS_TYPE_UNIX_FD => number or float
220 DBUS_TYPE_INT32 => integer or float
221 DBUS_TYPE_UINT64 => number or float
222 DBUS_TYPE_INT64 => integer or float
223 DBUS_TYPE_DOUBLE => float
224 DBUS_TYPE_STRING => string
225 DBUS_TYPE_OBJECT_PATH => string
226 DBUS_TYPE_SIGNATURE => string
227 DBUS_TYPE_ARRAY => list
228 DBUS_TYPE_VARIANT => list
229 DBUS_TYPE_STRUCT => list
230 DBUS_TYPE_DICT_ENTRY => list
231
232Example:
233
234\(dbus-call-method
235 :session \"org.gnome.seahorse\" \"/org/gnome/seahorse/keys/openpgp\"
236 \"org.gnome.seahorse.Keys\" \"GetKeyField\"
237 \"openpgp:657984B8C7A966DD\" \"simple-name\")
238
239 => (t (\"Philip R. Zimmermann\"))
240
241If the result of the METHOD call is just one value, the converted Lisp
242object is returned instead of a list containing this single Lisp object.
243
244\(dbus-call-method
245 :system \"org.freedesktop.Hal\" \"/org/freedesktop/Hal/devices/computer\"
246 \"org.freedesktop.Hal.Device\" \"GetPropertyString\"
247 \"system.kernel.machine\")
248
249 => \"i686\""
250
251 (or (memq bus '(:system :session)) (stringp bus)
252 (signal 'wrong-type-argument (list 'keywordp bus)))
253 (or (stringp service)
254 (signal 'wrong-type-argument (list 'stringp service)))
255 (or (stringp path)
256 (signal 'wrong-type-argument (list 'stringp path)))
257 (or (stringp interface)
258 (signal 'wrong-type-argument (list 'stringp interface)))
259 (or (stringp method)
260 (signal 'wrong-type-argument (list 'stringp method)))
261
262 (let ((timeout (plist-get args :timeout))
26ea164c 263 (check-interval 0.001)
dcbf5805
MA
264 (key
265 (apply
266 'dbus-message-internal dbus-message-type-method-call
267 bus service path interface method 'dbus-call-method-handler args)))
205a7391 268
dcbf5805
MA
269 ;; Wait until `dbus-call-method-handler' has put the result into
270 ;; `dbus-return-values-table'. If no timeout is given, use the
205a7391 271 ;; default 25". Events which are not from D-Bus must be restored.
ec518f1a
MA
272 ;; `read-event' performs a redisplay. This must be suppressed; it
273 ;; hurts when reading D-Bus events asynchronously.
26ea164c
DC
274
275 ;; Work around bug#16775 by busy-waiting with gradual backoff for
276 ;; dbus calls to complete. A better aproach would involve either
277 ;; adding arbitrary wait condition support to read-event or
278 ;; restructuring dbus as a kind of process object. Poll at most
279 ;; about once per second for completion.
280
dcbf5805
MA
281 (with-timeout ((if timeout (/ timeout 1000.0) 25))
282 (while (eq (gethash key dbus-return-values-table :ignore) :ignore)
26ea164c
DC
283 (let ((event (let ((inhibit-redisplay t) unread-command-events)
284 (read-event nil nil check-interval))))
285 (when event
286 (push event unread-command-events))
287 (when (< check-interval 1)
288 (setf check-interval (* check-interval 1.05))))))
dcbf5805
MA
289
290 ;; Cleanup `dbus-return-values-table'. Return the result.
291 (prog1
292 (gethash key dbus-return-values-table)
293 (remhash key dbus-return-values-table))))
294
295;; `dbus-call-method' works non-blocking now.
296(defalias 'dbus-call-method-non-blocking 'dbus-call-method)
2a1e2476 297(make-obsolete 'dbus-call-method-non-blocking 'dbus-call-method "24.3")
dcbf5805
MA
298
299(defun dbus-call-method-asynchronously
300 (bus service path interface method handler &rest args)
301 "Call METHOD on the D-Bus BUS asynchronously.
302
303BUS is either a Lisp symbol, `:system' or `:session', or a string
304denoting the bus address.
305
306SERVICE is the D-Bus service name to be used. PATH is the D-Bus
307object path SERVICE is registered at. INTERFACE is an interface
308offered by SERVICE. It must provide METHOD.
309
310HANDLER is a Lisp function, which is called when the corresponding
311return message has arrived. If HANDLER is nil, no return message
312will be expected.
313
314If the parameter `:timeout' is given, the following integer TIMEOUT
315specifies the maximum number of milliseconds the method call must
316return. The default value is 25,000. If the method call doesn't
317return in time, a D-Bus error is raised.
318
319All other arguments ARGS are passed to METHOD as arguments. They are
320converted into D-Bus types via the following rules:
321
322 t and nil => DBUS_TYPE_BOOLEAN
323 number => DBUS_TYPE_UINT32
324 integer => DBUS_TYPE_INT32
325 float => DBUS_TYPE_DOUBLE
326 string => DBUS_TYPE_STRING
327 list => DBUS_TYPE_ARRAY
328
329All arguments can be preceded by a type symbol. For details about
330type symbols, see Info node `(dbus)Type Conversion'.
331
332If HANDLER is a Lisp function, the function returns a key into the
333hash table `dbus-registered-objects-table'. The corresponding entry
334in the hash table is removed, when the return message has been arrived,
335and HANDLER is called.
336
337Example:
338
339\(dbus-call-method-asynchronously
340 :system \"org.freedesktop.Hal\" \"/org/freedesktop/Hal/devices/computer\"
341 \"org.freedesktop.Hal.Device\" \"GetPropertyString\" 'message
342 \"system.kernel.machine\")
343
344 => \(:serial :system 2)
345
346 -| i686"
347
348 (or (memq bus '(:system :session)) (stringp bus)
349 (signal 'wrong-type-argument (list 'keywordp bus)))
350 (or (stringp service)
351 (signal 'wrong-type-argument (list 'stringp service)))
352 (or (stringp path)
353 (signal 'wrong-type-argument (list 'stringp path)))
354 (or (stringp interface)
355 (signal 'wrong-type-argument (list 'stringp interface)))
356 (or (stringp method)
357 (signal 'wrong-type-argument (list 'stringp method)))
358 (or (null handler) (functionp handler)
359 (signal 'wrong-type-argument (list 'functionp handler)))
360
361 (apply 'dbus-message-internal dbus-message-type-method-call
362 bus service path interface method handler args))
363
364(defun dbus-send-signal (bus service path interface signal &rest args)
365 "Send signal SIGNAL on the D-Bus BUS.
366
367BUS is either a Lisp symbol, `:system' or `:session', or a string
368denoting the bus address. The signal is sent from the D-Bus object
369Emacs is registered at BUS.
370
371SERVICE is the D-Bus name SIGNAL is sent to. It can be either a known
372name or a unique name. If SERVICE is nil, the signal is sent as
373broadcast message. PATH is the D-Bus object path SIGNAL is sent from.
374INTERFACE is an interface available at PATH. It must provide signal
375SIGNAL.
376
377All other arguments ARGS are passed to SIGNAL as arguments. They are
378converted into D-Bus types via the following rules:
379
380 t and nil => DBUS_TYPE_BOOLEAN
381 number => DBUS_TYPE_UINT32
382 integer => DBUS_TYPE_INT32
383 float => DBUS_TYPE_DOUBLE
384 string => DBUS_TYPE_STRING
385 list => DBUS_TYPE_ARRAY
386
387All arguments can be preceded by a type symbol. For details about
388type symbols, see Info node `(dbus)Type Conversion'.
389
390Example:
391
392\(dbus-send-signal
393 :session nil \"/org/gnu/Emacs\" \"org.gnu.Emacs.FileManager\"
394 \"FileModified\" \"/home/albinus/.emacs\")"
395
396 (or (memq bus '(:system :session)) (stringp bus)
397 (signal 'wrong-type-argument (list 'keywordp bus)))
398 (or (null service) (stringp service)
399 (signal 'wrong-type-argument (list 'stringp service)))
400 (or (stringp path)
401 (signal 'wrong-type-argument (list 'stringp path)))
402 (or (stringp interface)
403 (signal 'wrong-type-argument (list 'stringp interface)))
404 (or (stringp signal)
405 (signal 'wrong-type-argument (list 'stringp signal)))
406
407 (apply 'dbus-message-internal dbus-message-type-signal
408 bus service path interface signal args))
409
410(defun dbus-method-return-internal (bus service serial &rest args)
411 "Return for message SERIAL on the D-Bus BUS.
412This is an internal function, it shall not be used outside dbus.el."
413
414 (or (memq bus '(:system :session)) (stringp bus)
415 (signal 'wrong-type-argument (list 'keywordp bus)))
416 (or (stringp service)
417 (signal 'wrong-type-argument (list 'stringp service)))
418 (or (natnump serial)
419 (signal 'wrong-type-argument (list 'natnump serial)))
420
421 (apply 'dbus-message-internal dbus-message-type-method-return
422 bus service serial args))
423
424(defun dbus-method-error-internal (bus service serial &rest args)
425 "Return error message for message SERIAL on the D-Bus BUS.
426This is an internal function, it shall not be used outside dbus.el."
427
428 (or (memq bus '(:system :session)) (stringp bus)
429 (signal 'wrong-type-argument (list 'keywordp bus)))
430 (or (stringp service)
431 (signal 'wrong-type-argument (list 'stringp service)))
432 (or (natnump serial)
433 (signal 'wrong-type-argument (list 'natnump serial)))
434
435 (apply 'dbus-message-internal dbus-message-type-error
436 bus service serial args))
437
438\f
439;;; Hash table of registered functions.
98c38bfc 440
ef6ce14c 441(defun dbus-list-hash-table ()
e49d337b 442 "Returns all registered member registrations to D-Bus.
ef6ce14c 443The return value is a list, with elements of kind (KEY . VALUE).
b172ed20 444See `dbus-registered-objects-table' for a description of the
ef6ce14c
MA
445hash table."
446 (let (result)
447 (maphash
4f91a816 448 (lambda (key value) (add-to-list 'result (cons key value) 'append))
b172ed20 449 dbus-registered-objects-table)
ef6ce14c
MA
450 result))
451
dcbf5805
MA
452(defun dbus-setenv (bus variable value)
453 "Set the value of the BUS environment variable named VARIABLE to VALUE.
b172ed20 454
dcbf5805
MA
455BUS is either a Lisp symbol, `:system' or `:session', or a string
456denoting the bus address. Both VARIABLE and VALUE should be strings.
246a286b 457
dcbf5805
MA
458Normally, services inherit the environment of the BUS daemon. This
459function adds to or modifies that environment when activating services.
b172ed20 460
dcbf5805
MA
461Some bus instances, such as `:system', may disable setting the environment."
462 (dbus-call-method
463 bus dbus-service-dbus dbus-path-dbus
464 dbus-interface-dbus "UpdateActivationEnvironment"
465 `(:array (:dict-entry ,variable ,value))))
466
467(defun dbus-register-service (bus service &rest flags)
468 "Register known name SERVICE on the D-Bus BUS.
469
470BUS is either a Lisp symbol, `:system' or `:session', or a string
471denoting the bus address.
472
473SERVICE is the D-Bus service name that should be registered. It must
474be a known name.
475
476FLAGS are keywords, which control how the service name is registered.
477The following keywords are recognized:
478
479`:allow-replacement': Allow another service to become the primary
480owner if requested.
481
482`:replace-existing': Request to replace the current primary owner.
483
484`:do-not-queue': If we can not become the primary owner do not place
485us in the queue.
486
487The function returns a keyword, indicating the result of the
488operation. One of the following keywords is returned:
489
490`:primary-owner': Service has become the primary owner of the
491requested name.
492
493`:in-queue': Service could not become the primary owner and has been
494placed in the queue.
495
496`:exists': Service is already in the queue.
497
498`:already-owner': Service is already the primary owner."
499
500 ;; Add ObjectManager handler.
501 (dbus-register-method
502 bus service nil dbus-interface-objectmanager "GetManagedObjects"
503 'dbus-managed-objects-handler 'dont-register)
504
505 (let ((arg 0)
506 reply)
507 (dolist (flag flags)
508 (setq arg
509 (+ arg
f58e0fd5 510 (pcase flag
dcbf5805
MA
511 (:allow-replacement 1)
512 (:replace-existing 2)
513 (:do-not-queue 4)
f58e0fd5 514 (_ (signal 'wrong-type-argument (list flag)))))))
dcbf5805
MA
515 (setq reply (dbus-call-method
516 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
517 "RequestName" service arg))
f58e0fd5 518 (pcase reply
dcbf5805
MA
519 (1 :primary-owner)
520 (2 :in-queue)
521 (3 :exists)
522 (4 :already-owner)
f58e0fd5 523 (_ (signal 'dbus-error (list "Could not register service" service))))))
246a286b 524
c0a39702
MA
525(defun dbus-unregister-service (bus service)
526 "Unregister all objects related to SERVICE from D-Bus BUS.
e73f184c 527BUS is either a Lisp symbol, `:system' or `:session', or a string
5c0b4070
MA
528denoting the bus address. SERVICE must be a known service name.
529
530The function returns a keyword, indicating the result of the
531operation. One of the following keywords is returned:
532
b85eff45 533`:released': We successfully released the service.
5c0b4070
MA
534
535`:non-existent': Service name does not exist on this bus.
536
537`:not-owner': We are neither the primary owner nor waiting in the
538queue of this service."
539
c0a39702
MA
540 (maphash
541 (lambda (key value)
b85eff45
MA
542 (unless (equal :serial (car key))
543 (dolist (elt value)
544 (ignore-errors
545 (when (and (equal bus (cadr key)) (string-equal service (cadr elt)))
546 (unless
547 (puthash key (delete elt value) dbus-registered-objects-table)
548 (remhash key dbus-registered-objects-table)))))))
c0a39702 549 dbus-registered-objects-table)
0a203b61
MA
550 (let ((reply (dbus-call-method
551 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
552 "ReleaseName" service)))
f58e0fd5 553 (pcase reply
0a203b61
MA
554 (1 :released)
555 (2 :non-existent)
556 (3 :not-owner)
f58e0fd5 557 (_ (signal 'dbus-error (list "Could not unregister service" service))))))
c0a39702 558
dcbf5805
MA
559(defun dbus-register-signal
560 (bus service path interface signal handler &rest args)
561 "Register for a signal on the D-Bus BUS.
98c38bfc 562
dcbf5805
MA
563BUS is either a Lisp symbol, `:system' or `:session', or a string
564denoting the bus address.
98c38bfc 565
dcbf5805
MA
566SERVICE is the D-Bus service name used by the sending D-Bus object.
567It can be either a known name or the unique name of the D-Bus object
568sending the signal.
569
570PATH is the D-Bus object path SERVICE is registered. INTERFACE
571is an interface offered by SERVICE. It must provide SIGNAL.
572HANDLER is a Lisp function to be called when the signal is
573received. It must accept as arguments the values SIGNAL is
574sending.
575
576SERVICE, PATH, INTERFACE and SIGNAL can be nil. This is
577interpreted as a wildcard for the respective argument.
578
579The remaining arguments ARGS can be keywords or keyword string pairs.
580The meaning is as follows:
581
582`:argN' STRING:
583`:pathN' STRING: This stands for the Nth argument of the
584signal. `:pathN' arguments can be used for object path wildcard
0ba2624f 585matches as specified by D-Bus, while an `:argN' argument
dcbf5805
MA
586requires an exact match.
587
588`:arg-namespace' STRING: Register for the signals, which first
589argument defines the service or interface namespace STRING.
590
591`:path-namespace' STRING: Register for the object path namespace
592STRING. All signals sent from an object path, which has STRING as
593the preceding string, are matched. This requires PATH to be nil.
594
595`:eavesdrop': Register for unicast signals which are not directed
596to the D-Bus object Emacs is registered at D-Bus BUS, if the
597security policy of BUS allows this.
598
599Example:
600
601\(defun my-signal-handler (device)
602 (message \"Device %s added\" device))
603
604\(dbus-register-signal
605 :system \"org.freedesktop.Hal\" \"/org/freedesktop/Hal/Manager\"
606 \"org.freedesktop.Hal.Manager\" \"DeviceAdded\" 'my-signal-handler)
607
608 => \(\(:signal :system \"org.freedesktop.Hal.Manager\" \"DeviceAdded\")
609 \(\"org.freedesktop.Hal\" \"/org/freedesktop/Hal/Manager\" my-signal-handler))
610
611`dbus-register-signal' returns an object, which can be used in
612`dbus-unregister-object' for removing the registration."
613
614 (let ((counter 0)
615 (rule "type='signal'")
616 uname key key1 value)
617
618 ;; Retrieve unique name of service. If service is a known name,
619 ;; we will register for the corresponding unique name, if any.
620 ;; Signals are sent always with the unique name as sender. Note:
621 ;; the unique name of `dbus-service-dbus' is that string itself.
622 (if (and (stringp service)
623 (not (zerop (length service)))
624 (not (string-equal service dbus-service-dbus))
625 (not (string-match "^:" service)))
626 (setq uname (dbus-get-name-owner bus service))
627 (setq uname service))
628
629 (setq rule (concat rule
630 (when uname (format ",sender='%s'" uname))
631 (when interface (format ",interface='%s'" interface))
632 (when signal (format ",member='%s'" signal))
633 (when path (format ",path='%s'" path))))
634
635 ;; Add arguments to the rule.
636 (if (or (stringp (car args)) (null (car args)))
637 ;; As backward compatibility option, we allow just strings.
638 (dolist (arg args)
639 (if (stringp arg)
640 (setq rule (concat rule (format ",arg%d='%s'" counter arg)))
641 (if arg (signal 'wrong-type-argument (list "Wrong argument" arg))))
642 (setq counter (1+ counter)))
643
644 ;; Parse keywords.
645 (while args
646 (setq
647 key (car args)
648 rule (concat
649 rule
650 (cond
651 ;; `:arg0' .. `:arg63', `:path0' .. `:path63'.
652 ((and (keywordp key)
653 (string-match
654 "^:\\(arg\\|path\\)\\([[:digit:]]+\\)$"
655 (symbol-name key)))
656 (setq counter (match-string 2 (symbol-name key))
657 args (cdr args)
658 value (car args))
659 (unless (and (<= counter 63) (stringp value))
660 (signal 'wrong-type-argument
661 (list "Wrong argument" key value)))
662 (format
663 ",arg%s%s='%s'"
664 counter
665 (if (string-equal (match-string 1 (symbol-name key)) "path")
666 "path" "")
667 value))
668 ;; `:arg-namespace', `:path-namespace'.
669 ((and (keywordp key)
670 (string-match
671 "^:\\(arg\\|path\\)-namespace$" (symbol-name key)))
672 (setq args (cdr args)
673 value (car args))
674 (unless (stringp value)
675 (signal 'wrong-type-argument
676 (list "Wrong argument" key value)))
677 (format
678 ",%s='%s'"
679 (if (string-equal (match-string 1 (symbol-name key)) "path")
680 "path_namespace" "arg0namespace")
681 value))
682 ;; `:eavesdrop'.
683 ((eq key :eavesdrop)
684 ",eavesdrop='true'")
685 (t (signal 'wrong-type-argument (list "Wrong argument" key)))))
686 args (cdr args))))
687
688 ;; Add the rule to the bus.
689 (condition-case err
690 (dbus-call-method
691 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
692 "AddMatch" rule)
693 (dbus-error
694 (if (not (string-match "eavesdrop" rule))
695 (signal (car err) (cdr err))
696 ;; The D-Bus spec says we shall fall back to a rule without eavesdrop.
697 (when dbus-debug (message "Removing eavesdrop from rule %s" rule))
698 (setq rule (replace-regexp-in-string ",eavesdrop='true'" "" rule))
699 (dbus-call-method
700 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
701 "AddMatch" rule))))
98c38bfc 702
dcbf5805 703 (when dbus-debug (message "Matching rule \"%s\" created" rule))
98c38bfc 704
dcbf5805
MA
705 ;; Create a hash table entry.
706 (setq key (list :signal bus interface signal)
707 key1 (list uname service path handler rule)
708 value (gethash key dbus-registered-objects-table))
709 (unless (member key1 value)
710 (puthash key (cons key1 value) dbus-registered-objects-table))
98c38bfc 711
dcbf5805
MA
712 ;; Return the object.
713 (list key (list service path handler))))
98c38bfc 714
dcbf5805
MA
715(defun dbus-register-method
716 (bus service path interface method handler &optional dont-register-service)
717 "Register for method METHOD on the D-Bus BUS.
718
719BUS is either a Lisp symbol, `:system' or `:session', or a string
720denoting the bus address.
721
722SERVICE is the D-Bus service name of the D-Bus object METHOD is
723registered for. It must be a known name (See discussion of
724DONT-REGISTER-SERVICE below).
725
726PATH is the D-Bus object path SERVICE is registered (See discussion of
727DONT-REGISTER-SERVICE below). INTERFACE is the interface offered by
728SERVICE. It must provide METHOD.
729
730HANDLER is a Lisp function to be called when a method call is
731received. It must accept the input arguments of METHOD. The return
732value of HANDLER is used for composing the returning D-Bus message.
733In case HANDLER shall return a reply message with an empty argument
734list, HANDLER must return the symbol `:ignore'.
735
736When DONT-REGISTER-SERVICE is non-nil, the known name SERVICE is not
737registered. This means that other D-Bus clients have no way of
738noticing the newly registered method. When interfaces are constructed
739incrementally by adding single methods or properties at a time,
740DONT-REGISTER-SERVICE can be used to prevent other clients from
741discovering the still incomplete interface."
742
743 ;; Register SERVICE.
744 (unless (or dont-register-service
745 (member service (dbus-list-names bus)))
746 (dbus-register-service bus service))
747
748 ;; Create a hash table entry. We use nil for the unique name,
749 ;; because the method might be called from anybody.
750 (let* ((key (list :method bus interface method))
751 (key1 (list nil service path handler))
752 (value (gethash key dbus-registered-objects-table)))
753
754 (unless (member key1 value)
755 (puthash key (cons key1 value) dbus-registered-objects-table))
756
757 ;; Return the object.
758 (list key (list service path handler))))
759
760(defun dbus-unregister-object (object)
761 "Unregister OBJECT from D-Bus.
762OBJECT must be the result of a preceding `dbus-register-method',
763`dbus-register-property' or `dbus-register-signal' call. It
764returns `t' if OBJECT has been unregistered, `nil' otherwise.
765
766When OBJECT identifies the last method or property, which is
767registered for the respective service, Emacs releases its
768association to the service from D-Bus."
769 ;; Check parameter.
770 (unless (and (consp object) (not (null (car object))) (consp (cdr object)))
771 (signal 'wrong-type-argument (list 'D-Bus object)))
772
773 ;; Find the corresponding entry in the hash table.
774 (let* ((key (car object))
775 (type (car key))
776 (bus (cadr key))
777 (value (cadr object))
778 (service (car value))
779 (entry (gethash key dbus-registered-objects-table))
780 ret)
781 ;; key has the structure (TYPE BUS INTERFACE MEMBER).
782 ;; value has the structure (SERVICE PATH [HANDLER]).
783 ;; entry has the structure ((UNAME SERVICE PATH MEMBER [RULE]) ...).
784 ;; MEMBER is either a string (the handler), or a cons cell (a
785 ;; property value). UNAME and property values are not taken into
786 ;; account for comparison.
787
788 ;; Loop over the registered functions.
789 (dolist (elt entry)
790 (when (equal
791 value
792 (butlast (cdr elt) (- (length (cdr elt)) (length value))))
793 (setq ret t)
794 ;; Compute new hash value. If it is empty, remove it from the
795 ;; hash table.
796 (unless (puthash key (delete elt entry) dbus-registered-objects-table)
797 (remhash key dbus-registered-objects-table))
798 ;; Remove match rule of signals.
799 (when (eq type :signal)
800 (dbus-call-method
801 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
802 "RemoveMatch" (nth 4 elt)))))
803
804 ;; Check, whether there is still a registered function or property
805 ;; for the given service. If not, unregister the service from the
806 ;; bus.
807 (when (and service (memq type '(:method :property))
808 (not (catch :found
809 (progn
810 (maphash
811 (lambda (k v)
812 (dolist (e v)
813 (ignore-errors
814 (and
815 ;; Bus.
816 (equal bus (cadr k))
817 ;; Service.
818 (string-equal service (cadr e))
819 ;; Non-empty object path.
f58e0fd5 820 (cl-caddr e)
dcbf5805
MA
821 (throw :found t)))))
822 dbus-registered-objects-table)
823 nil))))
824 (dbus-unregister-service bus service))
825 ;; Return.
826 ret))
ef6ce14c 827
5363d8ea 828\f
82697a45
MA
829;;; D-Bus type conversion.
830
831(defun dbus-string-to-byte-array (string)
832 "Transforms STRING to list (:array :byte c1 :byte c2 ...).
833STRING shall be UTF8 coded."
d665fff0
MA
834 (if (zerop (length string))
835 '(:array :signature "y")
836 (let (result)
837 (dolist (elt (string-to-list string) (append '(:array) result))
838 (setq result (append result (list :byte elt)))))))
82697a45 839
b85eff45 840(defun dbus-byte-array-to-string (byte-array &optional multibyte)
82697a45 841 "Transforms BYTE-ARRAY into UTF8 coded string.
81961e4c 842BYTE-ARRAY must be a list of structure (c1 c2 ...), or a byte
b85eff45
MA
843array as produced by `dbus-string-to-byte-array'. The resulting
844string is unibyte encoded, unless MULTIBYTE is non-nil."
81961e4c 845 (apply
b85eff45 846 (if multibyte 'string 'unibyte-string)
81961e4c
MA
847 (if (equal byte-array '(:array :signature "y"))
848 nil
849 (let (result)
850 (dolist (elt byte-array result)
851 (when (characterp elt) (setq result (append result `(,elt)))))))))
82697a45
MA
852
853(defun dbus-escape-as-identifier (string)
854 "Escape an arbitrary STRING so it follows the rules for a C identifier.
855The escaped string can be used as object path component, interface element
856component, bus name component or member name in D-Bus.
857
858The escaping consists of replacing all non-alphanumerics, and the
859first character if it's a digit, with an underscore and two
860lower-case hex digits:
861
862 \"0123abc_xyz\\x01\\xff\" -> \"_30123abc_5fxyz_01_ff\"
863
864i.e. similar to URI encoding, but with \"_\" taking the role of \"%\",
865and a smaller allowed set. As a special case, \"\" is escaped to
866\"_\".
867
868Returns the escaped string. Algorithm taken from
b85eff45 869telepathy-glib's `tp_escape_as_identifier'."
82697a45
MA
870 (if (zerop (length string))
871 "_"
872 (replace-regexp-in-string
873 "^[0-9]\\|[^A-Za-z0-9]"
874 (lambda (x) (format "_%2x" (aref x 0)))
875 string)))
876
877(defun dbus-unescape-from-identifier (string)
b85eff45
MA
878 "Retrieve the original string from the encoded STRING as unibyte string.
879STRING must have been encoded with `dbus-escape-as-identifier'."
82697a45
MA
880 (if (string-equal string "_")
881 ""
882 (replace-regexp-in-string
883 "_.."
81961e4c 884 (lambda (x) (byte-to-string (string-to-number (substring x 1) 16)))
82697a45
MA
885 string)))
886
887\f
5363d8ea
MA
888;;; D-Bus events.
889
3a993e3d
MA
890(defun dbus-check-event (event)
891 "Checks whether EVENT is a well formed D-Bus event.
892EVENT is a list which starts with symbol `dbus-event':
893
98c38bfc 894 (dbus-event BUS TYPE SERIAL SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS)
3a993e3d 895
e49d337b 896BUS identifies the D-Bus the message is coming from. It is
e73f184c
MA
897either a Lisp symbol, `:system' or `:session', or a string
898denoting the bus address. TYPE is the D-Bus message type which
899has caused the event, SERIAL is the serial number of the received
900D-Bus message. SERVICE and PATH are the unique name and the
901object path of the D-Bus object emitting the message. INTERFACE
902and MEMBER denote the message which has been sent. HANDLER is
903the function which has been registered for this message. ARGS
904are the arguments passed to HANDLER, when it is called during
905event handling in `dbus-handle-event'.
3a993e3d
MA
906
907This function raises a `dbus-error' signal in case the event is
908not well formed."
909 (when dbus-debug (message "DBus-Event %s" event))
910 (unless (and (listp event)
911 (eq (car event) 'dbus-event)
5363d8ea 912 ;; Bus symbol.
e73f184c
MA
913 (or (symbolp (nth 1 event))
914 (stringp (nth 1 event)))
98c38bfc
MA
915 ;; Type.
916 (and (natnump (nth 2 event))
917 (< dbus-message-type-invalid (nth 2 event)))
e49d337b 918 ;; Serial.
98c38bfc 919 (natnump (nth 3 event))
5363d8ea 920 ;; Service.
98c38bfc 921 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 922 (= dbus-message-type-error (nth 2 event))
98c38bfc 923 (stringp (nth 4 event)))
e49d337b 924 ;; Object path.
98c38bfc 925 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 926 (= dbus-message-type-error (nth 2 event))
98c38bfc 927 (stringp (nth 5 event)))
e49d337b 928 ;; Interface.
98c38bfc 929 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 930 (= dbus-message-type-error (nth 2 event))
98c38bfc 931 (stringp (nth 6 event)))
e49d337b 932 ;; Member.
98c38bfc 933 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 934 (= dbus-message-type-error (nth 2 event))
98c38bfc 935 (stringp (nth 7 event)))
ef6ce14c 936 ;; Handler.
98c38bfc 937 (functionp (nth 8 event)))
3a993e3d
MA
938 (signal 'dbus-error (list "Not a valid D-Bus event" event))))
939
940;;;###autoload
941(defun dbus-handle-event (event)
942 "Handle events from the D-Bus.
5363d8ea 943EVENT is a D-Bus event, see `dbus-check-event'. HANDLER, being
98c38bfc 944part of the event, is called with arguments ARGS.
35b148ee 945If the HANDLER returns a `dbus-error', it is propagated as return message."
3a993e3d 946 (interactive "e")
98c38bfc
MA
947 (condition-case err
948 (let (result)
ba0b66b0 949 ;; We ignore not well-formed events.
98c38bfc 950 (dbus-check-event event)
ba0b66b0
MA
951 ;; Error messages must be propagated.
952 (when (= dbus-message-type-error (nth 2 event))
953 (signal 'dbus-error (nthcdr 9 event)))
954 ;; Apply the handler.
98c38bfc
MA
955 (setq result (apply (nth 8 event) (nthcdr 9 event)))
956 ;; Return a message when it is a message call.
957 (when (= dbus-message-type-method-call (nth 2 event))
958 (dbus-ignore-errors
3dec5c36
MA
959 (if (eq result :ignore)
960 (dbus-method-return-internal
dcbf5805 961 (nth 1 event) (nth 4 event) (nth 3 event))
3dec5c36 962 (apply 'dbus-method-return-internal
dcbf5805 963 (nth 1 event) (nth 4 event) (nth 3 event)
3dec5c36 964 (if (consp result) result (list result)))))))
98c38bfc
MA
965 ;; Error handling.
966 (dbus-error
967 ;; Return an error message when it is a message call.
968 (when (= dbus-message-type-method-call (nth 2 event))
969 (dbus-ignore-errors
970 (dbus-method-error-internal
dcbf5805 971 (nth 1 event) (nth 4 event) (nth 3 event) (cadr err))))
ba0b66b0 972 ;; Propagate D-Bus error messages.
d1069532 973 (run-hook-with-args 'dbus-event-error-functions event err)
ba0b66b0
MA
974 (when (or dbus-debug (= dbus-message-type-error (nth 2 event)))
975 (signal (car err) (cdr err))))))
3a993e3d
MA
976
977(defun dbus-event-bus-name (event)
978 "Return the bus name the event is coming from.
e73f184c
MA
979The result is either a Lisp symbol, `:system' or `:session', or a
980string denoting the bus address. EVENT is a D-Bus event, see
981`dbus-check-event'. This function raises a `dbus-error' signal
982in case the event is not well formed."
3a993e3d 983 (dbus-check-event event)
ef6ce14c 984 (nth 1 event))
3a993e3d 985
98c38bfc
MA
986(defun dbus-event-message-type (event)
987 "Return the message type of the corresponding D-Bus message.
988The result is a number. EVENT is a D-Bus event, see
989`dbus-check-event'. This function raises a `dbus-error' signal
990in case the event is not well formed."
991 (dbus-check-event event)
992 (nth 2 event))
993
e49d337b
MA
994(defun dbus-event-serial-number (event)
995 "Return the serial number of the corresponding D-Bus message.
98c38bfc
MA
996The result is a number. The serial number is needed for
997generating a reply message. EVENT is a D-Bus event, see
998`dbus-check-event'. This function raises a `dbus-error' signal
999in case the event is not well formed."
e49d337b 1000 (dbus-check-event event)
98c38bfc 1001 (nth 3 event))
e49d337b 1002
3a993e3d 1003(defun dbus-event-service-name (event)
5363d8ea 1004 "Return the name of the D-Bus object the event is coming from.
3a993e3d
MA
1005The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
1006This function raises a `dbus-error' signal in case the event is
1007not well formed."
1008 (dbus-check-event event)
98c38bfc 1009 (nth 4 event))
3a993e3d
MA
1010
1011(defun dbus-event-path-name (event)
1012 "Return the object path of the D-Bus object the event is coming from.
1013The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
1014This function raises a `dbus-error' signal in case the event is
1015not well formed."
1016 (dbus-check-event event)
98c38bfc 1017 (nth 5 event))
3a993e3d
MA
1018
1019(defun dbus-event-interface-name (event)
1020 "Return the interface name of the D-Bus object the event is coming from.
1021The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
1022This function raises a `dbus-error' signal in case the event is
1023not well formed."
1024 (dbus-check-event event)
98c38bfc 1025 (nth 6 event))
3a993e3d
MA
1026
1027(defun dbus-event-member-name (event)
1028 "Return the member name the event is coming from.
58179cce 1029It is either a signal name or a method name. The result is a
3a993e3d
MA
1030string. EVENT is a D-Bus event, see `dbus-check-event'. This
1031function raises a `dbus-error' signal in case the event is not
1032well formed."
1033 (dbus-check-event event)
98c38bfc 1034 (nth 7 event))
5363d8ea
MA
1035
1036\f
1037;;; D-Bus registered names.
3a993e3d 1038
07e52e08 1039(defun dbus-list-activatable-names (&optional bus)
3a993e3d 1040 "Return the D-Bus service names which can be activated as list.
07e52e08
MA
1041If BUS is left nil, `:system' is assumed. The result is a list
1042of strings, which is `nil' when there are no activatable service
1043names at all."
246a286b
MA
1044 (dbus-ignore-errors
1045 (dbus-call-method
07e52e08 1046 (or bus :system) dbus-service-dbus
246a286b 1047 dbus-path-dbus dbus-interface-dbus "ListActivatableNames")))
3a993e3d
MA
1048
1049(defun dbus-list-names (bus)
1050 "Return the service names registered at D-Bus BUS.
f636d3ca
MA
1051The result is a list of strings, which is `nil' when there are no
1052registered service names at all. Well known names are strings
1053like \"org.freedesktop.DBus\". Names starting with \":\" are
1054unique names for services."
246a286b
MA
1055 (dbus-ignore-errors
1056 (dbus-call-method
1057 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus "ListNames")))
3a993e3d
MA
1058
1059(defun dbus-list-known-names (bus)
1060 "Retrieve all services which correspond to a known name in BUS.
1061A service has a known name if it doesn't start with \":\"."
1062 (let (result)
1063 (dolist (name (dbus-list-names bus) result)
1064 (unless (string-equal ":" (substring name 0 1))
1065 (add-to-list 'result name 'append)))))
1066
1067(defun dbus-list-queued-owners (bus service)
f636d3ca
MA
1068 "Return the unique names registered at D-Bus BUS and queued for SERVICE.
1069The result is a list of strings, or `nil' when there are no
1070queued name owners service names at all."
246a286b
MA
1071 (dbus-ignore-errors
1072 (dbus-call-method
1073 bus dbus-service-dbus dbus-path-dbus
1074 dbus-interface-dbus "ListQueuedOwners" service)))
3a993e3d
MA
1075
1076(defun dbus-get-name-owner (bus service)
1077 "Return the name owner of SERVICE registered at D-Bus BUS.
f636d3ca 1078The result is either a string, or `nil' if there is no name owner."
246a286b
MA
1079 (dbus-ignore-errors
1080 (dbus-call-method
1081 bus dbus-service-dbus dbus-path-dbus
1082 dbus-interface-dbus "GetNameOwner" service)))
3a993e3d 1083
93fb0645
MA
1084(defun dbus-ping (bus service &optional timeout)
1085 "Check whether SERVICE is registered for D-Bus BUS.
1086TIMEOUT, a nonnegative integer, specifies the maximum number of
1087milliseconds `dbus-ping' must return. The default value is 25,000.
1088
1089Note, that this autoloads SERVICE if it is not running yet. If
1090it shall be checked whether SERVICE is already running, one shall
1091apply
1092
1093 \(member service \(dbus-list-known-names bus))"
4ba11bcb
MA
1094 ;; "Ping" raises a D-Bus error if SERVICE does not exist.
1095 ;; Otherwise, it returns silently with `nil'.
1096 (condition-case nil
1097 (not
93fb0645
MA
1098 (if (natnump timeout)
1099 (dbus-call-method
1100 bus service dbus-path-dbus dbus-interface-peer
1101 "Ping" :timeout timeout)
1102 (dbus-call-method
1103 bus service dbus-path-dbus dbus-interface-peer "Ping")))
4ba11bcb
MA
1104 (dbus-error nil)))
1105
f636d3ca
MA
1106\f
1107;;; D-Bus introspection.
3a993e3d 1108
f636d3ca 1109(defun dbus-introspect (bus service path)
35b148ee 1110 "Return all interfaces and sub-nodes of SERVICE,
f636d3ca
MA
1111registered at object path PATH at bus BUS.
1112
e73f184c
MA
1113BUS is either a Lisp symbol, `:system' or `:session', or a string
1114denoting the bus address. SERVICE must be a known service name,
1115and PATH must be a valid object path. The last two parameters
1116are strings. The result, the introspection data, is a string in
1117XML format."
205a7391 1118 ;; We don't want to raise errors.
246a286b 1119 (dbus-ignore-errors
dcbf5805
MA
1120 (dbus-call-method
1121 bus service path dbus-interface-introspectable "Introspect"
1122 :timeout 1000)))
3a993e3d 1123
f636d3ca
MA
1124(defun dbus-introspect-xml (bus service path)
1125 "Return the introspection data of SERVICE in D-Bus BUS at object path PATH.
1126The data are a parsed list. The root object is a \"node\",
1127representing the object path PATH. The root object can contain
1128\"interface\" and further \"node\" objects."
1129 ;; We don't want to raise errors.
1130 (xml-node-name
1131 (ignore-errors
1132 (with-temp-buffer
1133 (insert (dbus-introspect bus service path))
1134 (xml-parse-region (point-min) (point-max))))))
1135
1136(defun dbus-introspect-get-attribute (object attribute)
1137 "Return the ATTRIBUTE value of D-Bus introspection OBJECT.
1138ATTRIBUTE must be a string according to the attribute names in
1139the D-Bus specification."
1140 (xml-get-attribute-or-nil object (intern attribute)))
1141
1142(defun dbus-introspect-get-node-names (bus service path)
1143 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
1144It returns a list of strings. The node names stand for further
1145object paths of the D-Bus service."
1146 (let ((object (dbus-introspect-xml bus service path))
1147 result)
1148 (dolist (elt (xml-get-children object 'node) result)
1149 (add-to-list
1150 'result (dbus-introspect-get-attribute elt "name") 'append))))
1151
1152(defun dbus-introspect-get-all-nodes (bus service path)
1153 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
1154It returns a list of strings, which are further object paths of SERVICE."
1155 (let ((result (list path)))
1156 (dolist (elt
1157 (dbus-introspect-get-node-names bus service path)
1158 result)
1159 (setq elt (expand-file-name elt path))
1160 (setq result
1161 (append result (dbus-introspect-get-all-nodes bus service elt))))))
1162
1163(defun dbus-introspect-get-interface-names (bus service path)
1164 "Return all interface names of SERVICE in D-Bus BUS at object path PATH.
1165It returns a list of strings.
1166
1167There will be always the default interface
1168\"org.freedesktop.DBus.Introspectable\". Another default
1169interface is \"org.freedesktop.DBus.Properties\". If present,
1170\"interface\" objects can also have \"property\" objects as
1171children, beside \"method\" and \"signal\" objects."
1172 (let ((object (dbus-introspect-xml bus service path))
1173 result)
1174 (dolist (elt (xml-get-children object 'interface) result)
1175 (add-to-list
1176 'result (dbus-introspect-get-attribute elt "name") 'append))))
1177
1178(defun dbus-introspect-get-interface (bus service path interface)
1179 "Return the INTERFACE of SERVICE in D-Bus BUS at object path PATH.
1180The return value is an XML object. INTERFACE must be a string,
35b148ee
JB
1181element of the list returned by `dbus-introspect-get-interface-names'.
1182The resulting \"interface\" object can contain \"method\", \"signal\",
f636d3ca
MA
1183\"property\" and \"annotation\" children."
1184 (let ((elt (xml-get-children
1185 (dbus-introspect-xml bus service path) 'interface)))
1186 (while (and elt
1187 (not (string-equal
1188 interface
1189 (dbus-introspect-get-attribute (car elt) "name"))))
1190 (setq elt (cdr elt)))
1191 (car elt)))
1192
1193(defun dbus-introspect-get-method-names (bus service path interface)
1194 "Return a list of strings of all method names of INTERFACE.
1195SERVICE is a service of D-Bus BUS at object path PATH."
1196 (let ((object (dbus-introspect-get-interface bus service path interface))
1197 result)
1198 (dolist (elt (xml-get-children object 'method) result)
1199 (add-to-list
1200 'result (dbus-introspect-get-attribute elt "name") 'append))))
1201
1202(defun dbus-introspect-get-method (bus service path interface method)
1203 "Return method METHOD of interface INTERFACE as XML object.
1204It must be located at SERVICE in D-Bus BUS at object path PATH.
1205METHOD must be a string, element of the list returned by
1206`dbus-introspect-get-method-names'. The resulting \"method\"
1207object can contain \"arg\" and \"annotation\" children."
1208 (let ((elt (xml-get-children
1209 (dbus-introspect-get-interface bus service path interface)
1210 'method)))
1211 (while (and elt
1212 (not (string-equal
1213 method (dbus-introspect-get-attribute (car elt) "name"))))
1214 (setq elt (cdr elt)))
1215 (car elt)))
1216
1217(defun dbus-introspect-get-signal-names (bus service path interface)
1218 "Return a list of strings of all signal names of INTERFACE.
1219SERVICE is a service of D-Bus BUS at object path PATH."
1220 (let ((object (dbus-introspect-get-interface bus service path interface))
1221 result)
1222 (dolist (elt (xml-get-children object 'signal) result)
1223 (add-to-list
1224 'result (dbus-introspect-get-attribute elt "name") 'append))))
1225
1226(defun dbus-introspect-get-signal (bus service path interface signal)
1227 "Return signal SIGNAL of interface INTERFACE as XML object.
1228It must be located at SERVICE in D-Bus BUS at object path PATH.
1229SIGNAL must be a string, element of the list returned by
1230`dbus-introspect-get-signal-names'. The resulting \"signal\"
1231object can contain \"arg\" and \"annotation\" children."
1232 (let ((elt (xml-get-children
1233 (dbus-introspect-get-interface bus service path interface)
1234 'signal)))
1235 (while (and elt
1236 (not (string-equal
1237 signal (dbus-introspect-get-attribute (car elt) "name"))))
1238 (setq elt (cdr elt)))
1239 (car elt)))
1240
1241(defun dbus-introspect-get-property-names (bus service path interface)
1242 "Return a list of strings of all property names of INTERFACE.
1243SERVICE is a service of D-Bus BUS at object path PATH."
1244 (let ((object (dbus-introspect-get-interface bus service path interface))
1245 result)
1246 (dolist (elt (xml-get-children object 'property) result)
1247 (add-to-list
1248 'result (dbus-introspect-get-attribute elt "name") 'append))))
1249
1250(defun dbus-introspect-get-property (bus service path interface property)
1251 "This function returns PROPERTY of INTERFACE as XML object.
1252It must be located at SERVICE in D-Bus BUS at object path PATH.
1253PROPERTY must be a string, element of the list returned by
1254`dbus-introspect-get-property-names'. The resulting PROPERTY
1255object can contain \"annotation\" children."
1256 (let ((elt (xml-get-children
1257 (dbus-introspect-get-interface bus service path interface)
1258 'property)))
1259 (while (and elt
1260 (not (string-equal
1261 property
1262 (dbus-introspect-get-attribute (car elt) "name"))))
1263 (setq elt (cdr elt)))
1264 (car elt)))
1265
1266(defun dbus-introspect-get-annotation-names
1267 (bus service path interface &optional name)
1268 "Return all annotation names as list of strings.
1269If NAME is `nil', the annotations are children of INTERFACE,
1270otherwise NAME must be a \"method\", \"signal\", or \"property\"
1271object, where the annotations belong to."
1272 (let ((object
1273 (if name
1274 (or (dbus-introspect-get-method bus service path interface name)
1275 (dbus-introspect-get-signal bus service path interface name)
1276 (dbus-introspect-get-property bus service path interface name))
1277 (dbus-introspect-get-interface bus service path interface)))
1278 result)
1279 (dolist (elt (xml-get-children object 'annotation) result)
1280 (add-to-list
1281 'result (dbus-introspect-get-attribute elt "name") 'append))))
1282
1283(defun dbus-introspect-get-annotation
1284 (bus service path interface name annotation)
1285 "Return ANNOTATION as XML object.
1286If NAME is `nil', ANNOTATION is a child of INTERFACE, otherwise
1287NAME must be the name of a \"method\", \"signal\", or
1288\"property\" object, where the ANNOTATION belongs to."
1289 (let ((elt (xml-get-children
1290 (if name
1291 (or (dbus-introspect-get-method
1292 bus service path interface name)
1293 (dbus-introspect-get-signal
1294 bus service path interface name)
1295 (dbus-introspect-get-property
1296 bus service path interface name))
1297 (dbus-introspect-get-interface bus service path interface))
1298 'annotation)))
1299 (while (and elt
1300 (not (string-equal
1301 annotation
1302 (dbus-introspect-get-attribute (car elt) "name"))))
1303 (setq elt (cdr elt)))
1304 (car elt)))
1305
1306(defun dbus-introspect-get-argument-names (bus service path interface name)
1307 "Return a list of all argument names as list of strings.
1308NAME must be a \"method\" or \"signal\" object.
1309
1310Argument names are optional, the function can return `nil'
1311therefore, even if the method or signal has arguments."
1312 (let ((object
1313 (or (dbus-introspect-get-method bus service path interface name)
1314 (dbus-introspect-get-signal bus service path interface name)))
1315 result)
1316 (dolist (elt (xml-get-children object 'arg) result)
1317 (add-to-list
1318 'result (dbus-introspect-get-attribute elt "name") 'append))))
1319
1320(defun dbus-introspect-get-argument (bus service path interface name arg)
1321 "Return argument ARG as XML object.
35b148ee
JB
1322NAME must be a \"method\" or \"signal\" object. ARG must be a string,
1323element of the list returned by `dbus-introspect-get-argument-names'."
f636d3ca
MA
1324 (let ((elt (xml-get-children
1325 (or (dbus-introspect-get-method bus service path interface name)
1326 (dbus-introspect-get-signal bus service path interface name))
1327 'arg)))
1328 (while (and elt
1329 (not (string-equal
1330 arg (dbus-introspect-get-attribute (car elt) "name"))))
1331 (setq elt (cdr elt)))
1332 (car elt)))
1333
1334(defun dbus-introspect-get-signature
1335 (bus service path interface name &optional direction)
1336 "Return signature of a `method' or `signal', represented by NAME, as string.
1337If NAME is a `method', DIRECTION can be either \"in\" or \"out\".
1338If DIRECTION is `nil', \"in\" is assumed.
1339
1340If NAME is a `signal', and DIRECTION is non-`nil', DIRECTION must
1341be \"out\"."
1342 ;; For methods, we use "in" as default direction.
1343 (let ((object (or (dbus-introspect-get-method
1344 bus service path interface name)
1345 (dbus-introspect-get-signal
1346 bus service path interface name))))
1347 (when (and (string-equal
1348 "method" (dbus-introspect-get-attribute object "name"))
1349 (not (stringp direction)))
1350 (setq direction "in"))
1351 ;; In signals, no direction is given.
1352 (when (string-equal "signal" (dbus-introspect-get-attribute object "name"))
1353 (setq direction nil))
1354 ;; Collect the signatures.
1355 (mapconcat
4f91a816
SM
1356 (lambda (x)
1357 (let ((arg (dbus-introspect-get-argument
1358 bus service path interface name x)))
1359 (if (or (not (stringp direction))
1360 (string-equal
1361 direction
1362 (dbus-introspect-get-attribute arg "direction")))
1363 (dbus-introspect-get-attribute arg "type")
1364 "")))
f636d3ca
MA
1365 (dbus-introspect-get-argument-names bus service path interface name)
1366 "")))
3a993e3d 1367
f636d3ca
MA
1368\f
1369;;; D-Bus properties.
3a993e3d 1370
f636d3ca
MA
1371(defun dbus-get-property (bus service path interface property)
1372 "Return the value of PROPERTY of INTERFACE.
1373It will be checked at BUS, SERVICE, PATH. The result can be any
1374valid D-Bus value, or `nil' if there is no PROPERTY."
246a286b 1375 (dbus-ignore-errors
dcbf5805
MA
1376 ;; "Get" returns a variant, so we must use the `car'.
1377 (car
1378 (dbus-call-method
1379 bus service path dbus-interface-properties
1380 "Get" :timeout 500 interface property))))
f636d3ca
MA
1381
1382(defun dbus-set-property (bus service path interface property value)
1383 "Set value of PROPERTY of INTERFACE to VALUE.
1384It will be checked at BUS, SERVICE, PATH. When the value has
1385been set successful, the result is VALUE. Otherwise, `nil' is
1386returned."
1387 (dbus-ignore-errors
dcbf5805
MA
1388 ;; "Set" requires a variant.
1389 (dbus-call-method
1390 bus service path dbus-interface-properties
1391 "Set" :timeout 500 interface property (list :variant value))
1392 ;; Return VALUE.
1393 (dbus-get-property bus service path interface property)))
f636d3ca
MA
1394
1395(defun dbus-get-all-properties (bus service path interface)
1396 "Return all properties of INTERFACE at BUS, SERVICE, PATH.
1397The result is a list of entries. Every entry is a cons of the
1398name of the property, and its value. If there are no properties,
1399`nil' is returned."
f636d3ca 1400 (dbus-ignore-errors
b172ed20 1401 ;; "GetAll" returns "a{sv}".
f636d3ca 1402 (let (result)
b172ed20 1403 (dolist (dict
dcbf5805 1404 (dbus-call-method
b172ed20
MA
1405 bus service path dbus-interface-properties
1406 "GetAll" :timeout 500 interface)
f636d3ca 1407 result)
f58e0fd5 1408 (add-to-list 'result (cons (car dict) (cl-caadr dict)) 'append)))))
b172ed20
MA
1409
1410(defun dbus-register-property
6388924a
MA
1411 (bus service path interface property access value
1412 &optional emits-signal dont-register-service)
b172ed20
MA
1413 "Register property PROPERTY on the D-Bus BUS.
1414
e73f184c
MA
1415BUS is either a Lisp symbol, `:system' or `:session', or a string
1416denoting the bus address.
b172ed20
MA
1417
1418SERVICE is the D-Bus service name of the D-Bus. It must be a
6388924a
MA
1419known name (See discussion of DONT-REGISTER-SERVICE below).
1420
1421PATH is the D-Bus object path SERVICE is registered (See
1422discussion of DONT-REGISTER-SERVICE below). INTERFACE is the
1423name of the interface used at PATH, PROPERTY is the name of the
1424property of INTERFACE. ACCESS indicates, whether the property
1425can be changed by other services via D-Bus. It must be either
1426the symbol `:read' or `:readwrite'. VALUE is the initial value
1427of the property, it can be of any valid type (see
b172ed20
MA
1428`dbus-call-method' for details).
1429
1430If PROPERTY already exists on PATH, it will be overwritten. For
1431properties with access type `:read' this is the only way to
1432change their values. Properties with access type `:readwrite'
1433can be changed by `dbus-set-property'.
1434
1435The interface \"org.freedesktop.DBus.Properties\" is added to
1436PATH, including a default handler for the \"Get\", \"GetAll\" and
b1ce08da
MA
1437\"Set\" methods of this interface. When EMITS-SIGNAL is non-nil,
1438the signal \"PropertiesChanged\" is sent when the property is
6388924a
MA
1439changed by `dbus-set-property'.
1440
1441When DONT-REGISTER-SERVICE is non-nil, the known name SERVICE is
1442not registered. This means that other D-Bus clients have no way
1443of noticing the newly registered property. When interfaces are
1444constructed incrementally by adding single methods or properties
1445at a time, DONT-REGISTER-SERVICE can be used to prevent other
1446clients from discovering the still incomplete interface."
b172ed20 1447 (unless (member access '(:read :readwrite))
dcbf5805 1448 (signal 'wrong-type-argument (list "Access type invalid" access)))
b172ed20 1449
0a203b61 1450 ;; Add handlers for the three property-related methods.
b172ed20 1451 (dbus-register-method
0a203b61 1452 bus service path dbus-interface-properties "Get"
1a27c64e 1453 'dbus-property-handler 'dont-register)
b172ed20 1454 (dbus-register-method
1a27c64e
MA
1455 bus service path dbus-interface-properties "GetAll"
1456 'dbus-property-handler 'dont-register)
b172ed20 1457 (dbus-register-method
1a27c64e
MA
1458 bus service path dbus-interface-properties "Set"
1459 'dbus-property-handler 'dont-register)
0a203b61 1460
dcbf5805
MA
1461 ;; Register SERVICE.
1462 (unless (or dont-register-service (member service (dbus-list-names bus)))
0a203b61 1463 (dbus-register-service bus service))
b172ed20 1464
b1ce08da
MA
1465 ;; Send the PropertiesChanged signal.
1466 (when emits-signal
1467 (dbus-send-signal
1468 bus service path dbus-interface-properties "PropertiesChanged"
dcbf5805 1469 `((:dict-entry ,property (:variant ,value)))
b1ce08da
MA
1470 '(:array)))
1471
b172ed20
MA
1472 ;; Create a hash table entry. We use nil for the unique name,
1473 ;; because the property might be accessed from anybody.
dcbf5805 1474 (let ((key (list :property bus interface property))
b1ce08da
MA
1475 (val
1476 (list
1477 (list
1478 nil service path
1479 (cons
1480 (if emits-signal (list access :emits-signal) (list access))
1481 value)))))
b172ed20
MA
1482 (puthash key val dbus-registered-objects-table)
1483
1484 ;; Return the object.
1485 (list key (list service path))))
1486
1487(defun dbus-property-handler (&rest args)
35b148ee 1488 "Default handler for the \"org.freedesktop.DBus.Properties\" interface.
dcbf5805 1489It will be registered for all objects created by `dbus-register-property'."
b172ed20 1490 (let ((bus (dbus-event-bus-name last-input-event))
b1ce08da 1491 (service (dbus-event-service-name last-input-event))
b172ed20
MA
1492 (path (dbus-event-path-name last-input-event))
1493 (method (dbus-event-member-name last-input-event))
1494 (interface (car args))
1495 (property (cadr args)))
1496 (cond
1497 ;; "Get" returns a variant.
1498 ((string-equal method "Get")
dcbf5805 1499 (let ((entry (gethash (list :property bus interface property)
b1ce08da
MA
1500 dbus-registered-objects-table)))
1501 (when (string-equal path (nth 2 (car entry)))
dcbf5805 1502 `((:variant ,(cdar (last (car entry))))))))
b172ed20
MA
1503
1504 ;; "Set" expects a variant.
1505 ((string-equal method "Set")
b1ce08da 1506 (let* ((value (caar (cddr args)))
dcbf5805 1507 (entry (gethash (list :property bus interface property)
b1ce08da
MA
1508 dbus-registered-objects-table))
1509 ;; The value of the hash table is a list; in case of
1510 ;; properties it contains just one element (UNAME SERVICE
1511 ;; PATH OBJECT). OBJECT is a cons cell of a list, which
1512 ;; contains a list of annotations (like :read,
1513 ;; :read-write, :emits-signal), and the value of the
1514 ;; property.
1515 (object (car (last (car entry)))))
1516 (unless (consp object)
b172ed20
MA
1517 (signal 'dbus-error
1518 (list "Property not registered at path" property path)))
b1ce08da 1519 (unless (member :readwrite (car object))
b172ed20
MA
1520 (signal 'dbus-error
1521 (list "Property not writable at path" property path)))
dcbf5805 1522 (puthash (list :property bus interface property)
b1ce08da
MA
1523 (list (append (butlast (car entry))
1524 (list (cons (car object) value))))
b172ed20 1525 dbus-registered-objects-table)
b1ce08da
MA
1526 ;; Send the "PropertiesChanged" signal.
1527 (when (member :emits-signal (car object))
1528 (dbus-send-signal
1529 bus service path dbus-interface-properties "PropertiesChanged"
dcbf5805 1530 `((:dict-entry ,property (:variant ,value)))
b1ce08da
MA
1531 '(:array)))
1532 ;; Return empty reply.
b172ed20
MA
1533 :ignore))
1534
1535 ;; "GetAll" returns "a{sv}".
1536 ((string-equal method "GetAll")
1537 (let (result)
1538 (maphash
1539 (lambda (key val)
dcbf5805 1540 (when (and (equal (butlast key) (list :property bus interface))
b172ed20 1541 (string-equal path (nth 2 (car val)))
31bb373f 1542 (not (functionp (car (last (car val))))))
b172ed20
MA
1543 (add-to-list
1544 'result
1545 (list :dict-entry
1546 (car (last key))
1547 (list :variant (cdar (last (car val))))))))
1548 dbus-registered-objects-table)
052e28ac
MA
1549 ;; Return the result, or an empty array.
1550 (list :array (or result '(:signature "{sv}"))))))))
b172ed20 1551
dcbf5805
MA
1552\f
1553;;; D-Bus object manager.
1554
1555(defun dbus-get-all-managed-objects (bus service path)
1556 "Return all objects at BUS, SERVICE, PATH, and the children of PATH.
1557The result is a list of objects. Every object is a cons of an
1558existing path name, and the list of available interface objects.
1559An interface object is another cons, which car is the interface
1560name, and the cdr is the list of properties as returned by
205a7391 1561`dbus-get-all-properties' for that path and interface. Example:
dcbf5805
MA
1562
1563\(dbus-get-all-managed-objects :session \"org.gnome.SettingsDaemon\" \"/\")
1564
1565 => \(\(\"/org/gnome/SettingsDaemon/MediaKeys\"
1566 \(\"org.gnome.SettingsDaemon.MediaKeys\")
1567 \(\"org.freedesktop.DBus.Peer\")
1568 \(\"org.freedesktop.DBus.Introspectable\")
1569 \(\"org.freedesktop.DBus.Properties\")
1570 \(\"org.freedesktop.DBus.ObjectManager\"))
1571 \(\"/org/gnome/SettingsDaemon/Power\"
1572 \(\"org.gnome.SettingsDaemon.Power.Keyboard\")
1573 \(\"org.gnome.SettingsDaemon.Power.Screen\")
1574 \(\"org.gnome.SettingsDaemon.Power\"
1575 \(\"Icon\" . \". GThemedIcon battery-full-charged-symbolic \")
1576 \(\"Tooltip\" . \"Laptop battery is charged\"))
1577 \(\"org.freedesktop.DBus.Peer\")
1578 \(\"org.freedesktop.DBus.Introspectable\")
1579 \(\"org.freedesktop.DBus.Properties\")
1580 \(\"org.freedesktop.DBus.ObjectManager\"))
1581 ...)
1582
1583If possible, \"org.freedesktop.DBus.ObjectManager.GetManagedObjects\"
1584is used for retrieving the information. Otherwise, the information
1585is collected via \"org.freedesktop.DBus.Introspectable.Introspect\"
1586and \"org.freedesktop.DBus.Properties.GetAll\", which is slow."
1587 (let ((result
1588 ;; Direct call. Fails, if the target does not support the
1589 ;; object manager interface.
1590 (dbus-ignore-errors
1591 (dbus-call-method
1592 bus service path dbus-interface-objectmanager
1593 "GetManagedObjects" :timeout 1000))))
1594
1595 (if result
1596 ;; Massage the returned structure.
1597 (dolist (entry result result)
1598 ;; "a{oa{sa{sv}}}".
1599 (dolist (entry1 (cdr entry))
1600 ;; "a{sa{sv}}".
1601 (dolist (entry2 entry1)
1602 ;; "a{sv}".
1603 (if (cadr entry2)
1604 ;; "sv".
1605 (dolist (entry3 (cadr entry2))
f58e0fd5 1606 (setcdr entry3 (cl-caadr entry3)))
dcbf5805
MA
1607 (setcdr entry2 nil)))))
1608
1609 ;; Fallback: collect the information. Slooow!
1610 (dolist (object
1611 (dbus-introspect-get-all-nodes bus service path)
1612 result)
1613 (let (result1)
1614 (dolist
1615 (interface
1616 (dbus-introspect-get-interface-names bus service object)
1617 result1)
1618 (add-to-list
1619 'result1
1620 (cons interface
1621 (dbus-get-all-properties bus service object interface))))
1622 (when result1
1623 (add-to-list 'result (cons object result1))))))))
1624
1625(defun dbus-managed-objects-handler ()
1626 "Default handler for the \"org.freedesktop.DBus.ObjectManager\" interface.
1627It will be registered for all objects created by `dbus-register-method'."
1628 (let* ((last-input-event last-input-event)
1629 (bus (dbus-event-bus-name last-input-event))
dcbf5805
MA
1630 (path (dbus-event-path-name last-input-event)))
1631 ;; "GetManagedObjects" returns "a{oa{sa{sv}}}".
1632 (let (interfaces result)
1633
1634 ;; Check for object path wildcard interfaces.
1635 (maphash
1636 (lambda (key val)
1637 (when (and (equal (butlast key 2) (list :method bus))
1638 (null (nth 2 (car-safe val))))
1639 (add-to-list 'interfaces (nth 2 key))))
1640 dbus-registered-objects-table)
1641
1642 ;; Check all registered object paths.
1643 (maphash
1644 (lambda (key val)
6c42fc3e 1645 (let ((object (or (nth 2 (car-safe val)) "")))
dcbf5805
MA
1646 (when (and (equal (butlast key 2) (list :method bus))
1647 (string-prefix-p path object))
1648 (dolist (interface (cons (nth 2 key) interfaces))
1649 (unless (assoc object result)
1650 (add-to-list 'result (list object)))
1651 (unless (assoc interface (cdr (assoc object result)))
1652 (setcdr
1653 (assoc object result)
1654 (append
1655 (list (cons
1656 interface
1657 ;; We simulate "org.freedesktop.DBus.Properties.GetAll"
1658 ;; by using an appropriate D-Bus event.
1659 (let ((last-input-event
1660 (append
1661 (butlast last-input-event 4)
1662 (list object dbus-interface-properties
1663 "GetAll" 'dbus-property-handler))))
1664 (dbus-property-handler interface))))
1665 (cdr (assoc object result)))))))))
1666 dbus-registered-objects-table)
1667
1668 ;; Return the result, or an empty array.
1669 (list
1670 :array
1671 (or
1672 (mapcar
1673 (lambda (x)
1674 (list
1675 :dict-entry :object-path (car x)
1676 (cons :array (mapcar (lambda (y) (cons :dict-entry y)) (cdr x)))))
1677 result)
1678 '(:signature "{oa{sa{sv}}}"))))))
1679
b172ed20 1680 \f
dcbf5805 1681;; Initialize `:system' and `:session' buses. This adds their file
720c7cd6
MA
1682;; descriptors to input_wait_mask, in order to detect incoming
1683;; messages immediately.
9e846523
MA
1684(when (featurep 'dbusbind)
1685 (dbus-ignore-errors
dcbf5805
MA
1686 (dbus-init-bus :system))
1687 (dbus-ignore-errors
9e846523 1688 (dbus-init-bus :session)))
720c7cd6 1689
3a993e3d
MA
1690(provide 'dbus)
1691
dcbf5805
MA
1692;;; TODO:
1693
1694;; * Implement org.freedesktop.DBus.ObjectManager.InterfacesAdded and
1695;; org.freedesktop.DBus.ObjectManager.InterfacesRemoved.
1696
3a993e3d 1697;;; dbus.el ends here