1 ;;; dbus.el --- Elisp bindings for D-Bus.
3 ;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, hardware
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
29 ;; Low-level language bindings are implemented in src/dbusbind.c.
33 ;; D-Bus support in the Emacs core can be disabled with configuration
34 ;; option "--without-dbus". Declare used subroutines and variables.
35 (declare-function dbus-call-method
"dbusbind.c")
36 (declare-function dbus-call-method-asynchronously
"dbusbind.c")
37 (declare-function dbus-init-bus
"dbusbind.c")
38 (declare-function dbus-method-return-internal
"dbusbind.c")
39 (declare-function dbus-method-error-internal
"dbusbind.c")
40 (declare-function dbus-register-signal
"dbusbind.c")
41 (declare-function dbus-register-method
"dbusbind.c")
42 (declare-function dbus-send-signal
"dbusbind.c")
44 (defvar dbus-registered-objects-table
)
46 ;; Pacify byte compiler.
52 (defconst dbus-service-dbus
"org.freedesktop.DBus"
53 "The bus name used to talk to the bus itself.")
55 (defconst dbus-path-dbus
"/org/freedesktop/DBus"
56 "The object path used to talk to the bus itself.")
58 (defconst dbus-interface-dbus
"org.freedesktop.DBus"
59 "The interface exported by the object with `dbus-service-dbus' and `dbus-path-dbus'.")
61 (defconst dbus-interface-peer
(concat dbus-interface-dbus
".Peer")
62 "The interface for peer objects.")
64 (defconst dbus-interface-introspectable
65 (concat dbus-interface-dbus
".Introspectable")
66 "The interface supported by introspectable objects.")
68 (defconst dbus-interface-properties
(concat dbus-interface-dbus
".Properties")
69 "The interface for property objects.")
71 (defconst dbus-service-emacs
"org.gnu.Emacs"
72 "The well known service name of Emacs.")
74 (defconst dbus-path-emacs
"/org/gnu/Emacs"
75 "The object path head used by Emacs.")
77 (defconst dbus-message-type-invalid
0
78 "This value is never a valid message type.")
80 (defconst dbus-message-type-method-call
1
81 "Message type of a method call message.")
83 (defconst dbus-message-type-method-return
2
84 "Message type of a method return message.")
86 (defconst dbus-message-type-error
3
87 "Message type of an error reply message.")
89 (defconst dbus-message-type-signal
4
90 "Message type of a signal message.")
92 (defmacro dbus-ignore-errors
(&rest body
)
93 "Execute BODY; signal D-Bus error when `dbus-debug' is non-nil.
94 Otherwise, return result of last form in BODY, or all other errors."
95 (declare (indent 0) (debug t
))
98 (dbus-error (when dbus-debug
(signal (car err
) (cdr err
))))))
99 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<dbus-ignore-errors\\>"))
101 (defvar dbus-event-error-hooks nil
102 "Functions to be called when a D-Bus error happens in the event handler.
103 Every function must accept two arguments, the event and the error variable
104 catched in `condition-case' by `dbus-error'.")
107 ;;; Hash table of registered functions.
109 (defvar dbus-return-values-table
(make-hash-table :test
'equal
)
110 "Hash table for temporary storing arguments of reply messages.
111 A key in this hash table is a list (BUS SERIAL). BUS is either a
112 Lisp symbol, `:system' or `:session', or a string denoting the
113 bus address. SERIAL is the serial number of the reply message.
114 See `dbus-call-method-non-blocking-handler' and
115 `dbus-call-method-non-blocking'.")
117 (defun dbus-list-hash-table ()
118 "Returns all registered member registrations to D-Bus.
119 The return value is a list, with elements of kind (KEY . VALUE).
120 See `dbus-registered-objects-table' for a description of the
124 '(lambda (key value
) (add-to-list 'result
(cons key value
) 'append
))
125 dbus-registered-objects-table
)
128 (defun dbus-unregister-object (object)
129 "Unregister OBJECT from D-Bus.
130 OBJECT must be the result of a preceding `dbus-register-method',
131 `dbus-register-property' or `dbus-register-signal' call. It
132 returns `t' if OBJECT has been unregistered, `nil' otherwise.
134 When OBJECT identifies the last method or property, which is
135 registered for the respective service, Emacs releases its
136 association to the service from D-Bus."
138 (unless (and (consp object
) (not (null (car object
))) (consp (cdr object
)))
139 (signal 'wrong-type-argument
(list 'D-Bus object
)))
141 ;; Find the corresponding entry in the hash table.
142 (let* ((key (car object
))
144 (entry (gethash key dbus-registered-objects-table
))
146 ;; entry has the structure ((UNAME SERVICE PATH MEMBER) ...).
147 ;; value has the structure ((SERVICE PATH [HANDLER]) ...).
148 ;; MEMBER is either a string (the handler), or a cons cell (a
149 ;; property value). UNAME and property values are not taken into
150 ;; account for comparision.
152 ;; Loop over the registered functions.
156 (butlast (cdr elt
) (- (length (cdr elt
)) (length (car value
)))))
157 ;; Compute new hash value. If it is empty, remove it from the
159 (unless (puthash key
(delete elt entry
) dbus-registered-objects-table
)
160 (remhash key dbus-registered-objects-table
))
162 ;; Check, whether there is still a registered function or property
163 ;; for the given service. If not, unregister the service from the
166 (let ((service (cadr elt
))
173 (when (and (equal bus
(car k
)) (string-equal service
(cadr e
)))
175 dbus-registered-objects-table
)
178 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
179 "ReleaseName" service
))))
183 (defun dbus-unregister-service (bus service
)
184 "Unregister all objects related to SERVICE from D-Bus BUS.
185 BUS is either a Lisp symbol, `:system' or `:session', or a string
186 denoting the bus address. SERVICE must be a known service name.
188 The function returns a keyword, indicating the result of the
189 operation. One of the following keywords is returned:
191 `:released': Service has become the primary owner of the name.
193 `:non-existent': Service name does not exist on this bus.
195 `:not-owner': We are neither the primary owner nor waiting in the
196 queue of this service."
202 (when (and (equal bus
(car key
)) (string-equal service
(cadr elt
)))
204 (puthash key
(delete elt value
) dbus-registered-objects-table
)
205 (remhash key dbus-registered-objects-table
))))))
206 dbus-registered-objects-table
)
207 (let ((reply (dbus-call-method
208 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
209 "ReleaseName" service
)))
214 (t (signal 'dbus-error
(list "Could not unregister service" service
))))))
216 (defun dbus-call-method-non-blocking-handler (&rest args
)
217 "Handler for reply messages of asynchronous D-Bus message calls.
218 It calls the function stored in `dbus-registered-objects-table'.
219 The result will be made available in `dbus-return-values-table'."
220 (puthash (list (dbus-event-bus-name last-input-event
)
221 (dbus-event-serial-number last-input-event
))
222 (if (= (length args
) 1) (car args
) args
)
223 dbus-return-values-table
))
225 (defun dbus-call-method-non-blocking
226 (bus service path interface method
&rest args
)
227 "Call METHOD on the D-Bus BUS, but don't block the event queue.
228 This is necessary for communicating to registered D-Bus methods,
229 which are running in the same Emacs process.
231 The arguments are the same as in `dbus-call-method'.
233 usage: (dbus-call-method-non-blocking
234 BUS SERVICE PATH INTERFACE METHOD
235 &optional :timeout TIMEOUT &rest ARGS)"
239 'dbus-call-method-asynchronously
240 bus service path interface method
241 'dbus-call-method-non-blocking-handler args
)))
242 ;; Wait until `dbus-call-method-non-blocking-handler' has put the
243 ;; result into `dbus-return-values-table'.
244 (while (eq (gethash key dbus-return-values-table
:ignore
) :ignore
)
245 (read-event nil nil
0.1))
247 ;; Cleanup `dbus-return-values-table'. Return the result.
249 (gethash key dbus-return-values-table nil
)
250 (remhash key dbus-return-values-table
))))
252 (defun dbus-name-owner-changed-handler (&rest args
)
253 "Reapplies all member registrations to D-Bus.
254 This handler is applied when a \"NameOwnerChanged\" signal has
255 arrived. SERVICE is the object name for which the name owner has
256 been changed. OLD-OWNER is the previous owner of SERVICE, or the
257 empty string if SERVICE was not owned yet. NEW-OWNER is the new
258 owner of SERVICE, or the empty string if SERVICE loses any name owner.
260 usage: (dbus-name-owner-changed-handler service old-owner new-owner)"
262 ;; Check the arguments. We should silently ignore it when they
264 (if (and (= (length args
) 3)
266 (stringp (cadr args
))
267 (stringp (caddr args
)))
268 (let ((service (car args
))
269 (old-owner (cadr args
))
270 (new-owner (caddr args
)))
271 ;; Check whether SERVICE is a known name.
272 (when (not (string-match "^:" service
))
276 ;; key has the structure (BUS INTERFACE MEMBER).
277 ;; elt has the structure (UNAME SERVICE PATH HANDLER).
278 (when (string-equal old-owner
(car elt
))
279 ;; Remove old key, and add new entry with changed name.
280 (dbus-unregister-object (list key
(cdr elt
)))
281 ;; Maybe we could arrange the lists a little bit better
282 ;; that we don't need to extract every single element?
283 (dbus-register-signal
285 (nth 0 key
) (nth 1 elt
) (nth 2 elt
)
286 ;; INTERFACE MEMBER HANDLER
287 (nth 1 key
) (nth 2 key
) (nth 3 elt
)))))
288 (copy-hash-table dbus-registered-objects-table
))))
289 ;; The error is reported only in debug mode.
294 (format "Wrong arguments of %s.NameOwnerChanged" dbus-interface-dbus
)
297 ;; Register the handler.
298 (when nil
;ignore-errors
299 (dbus-register-signal
300 :system dbus-service-dbus dbus-path-dbus dbus-interface-dbus
301 "NameOwnerChanged" 'dbus-name-owner-changed-handler
)
302 (dbus-register-signal
303 :session dbus-service-dbus dbus-path-dbus dbus-interface-dbus
304 "NameOwnerChanged" 'dbus-name-owner-changed-handler
))
307 ;;; D-Bus type conversion.
309 (defun dbus-string-to-byte-array (string)
310 "Transforms STRING to list (:array :byte c1 :byte c2 ...).
311 STRING shall be UTF8 coded."
312 (if (zerop (length string
))
313 '(:array
:signature
"y")
315 (dolist (elt (string-to-list string
) (append '(:array
) result
))
316 (setq result
(append result
(list :byte elt
)))))))
318 (defun dbus-byte-array-to-string (byte-array)
319 "Transforms BYTE-ARRAY into UTF8 coded string.
320 BYTE-ARRAY must be a list of structure (c1 c2 ...)."
321 (apply 'string byte-array
))
323 (defun dbus-escape-as-identifier (string)
324 "Escape an arbitrary STRING so it follows the rules for a C identifier.
325 The escaped string can be used as object path component, interface element
326 component, bus name component or member name in D-Bus.
328 The escaping consists of replacing all non-alphanumerics, and the
329 first character if it's a digit, with an underscore and two
330 lower-case hex digits:
332 \"0123abc_xyz\\x01\\xff\" -> \"_30123abc_5fxyz_01_ff\"
334 i.e. similar to URI encoding, but with \"_\" taking the role of \"%\",
335 and a smaller allowed set. As a special case, \"\" is escaped to
338 Returns the escaped string. Algorithm taken from
339 telepathy-glib's `tp-escape-as-identifier'."
340 (if (zerop (length string
))
342 (replace-regexp-in-string
343 "^[0-9]\\|[^A-Za-z0-9]"
344 (lambda (x) (format "_%2x" (aref x
0)))
347 (defun dbus-unescape-from-identifier (string)
348 "Retrieve the original string from the encoded STRING.
349 STRING must have been coded with `dbus-escape-as-identifier'"
350 (if (string-equal string
"_")
352 (replace-regexp-in-string
354 (lambda (x) (format "%c" (string-to-number (substring x
1) 16)))
360 (defun dbus-check-event (event)
361 "Checks whether EVENT is a well formed D-Bus event.
362 EVENT is a list which starts with symbol `dbus-event':
364 (dbus-event BUS TYPE SERIAL SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS)
366 BUS identifies the D-Bus the message is coming from. It is
367 either a Lisp symbol, `:system' or `:session', or a string
368 denoting the bus address. TYPE is the D-Bus message type which
369 has caused the event, SERIAL is the serial number of the received
370 D-Bus message. SERVICE and PATH are the unique name and the
371 object path of the D-Bus object emitting the message. INTERFACE
372 and MEMBER denote the message which has been sent. HANDLER is
373 the function which has been registered for this message. ARGS
374 are the arguments passed to HANDLER, when it is called during
375 event handling in `dbus-handle-event'.
377 This function raises a `dbus-error' signal in case the event is
379 (when dbus-debug
(message "DBus-Event %s" event
))
380 (unless (and (listp event
)
381 (eq (car event
) 'dbus-event
)
383 (or (symbolp (nth 1 event
))
384 (stringp (nth 1 event
)))
386 (and (natnump (nth 2 event
))
387 (< dbus-message-type-invalid
(nth 2 event
)))
389 (natnump (nth 3 event
))
391 (or (= dbus-message-type-method-return
(nth 2 event
))
392 (= dbus-message-type-error
(nth 2 event
))
393 (stringp (nth 4 event
)))
395 (or (= dbus-message-type-method-return
(nth 2 event
))
396 (= dbus-message-type-error
(nth 2 event
))
397 (stringp (nth 5 event
)))
399 (or (= dbus-message-type-method-return
(nth 2 event
))
400 (= dbus-message-type-error
(nth 2 event
))
401 (stringp (nth 6 event
)))
403 (or (= dbus-message-type-method-return
(nth 2 event
))
404 (= dbus-message-type-error
(nth 2 event
))
405 (stringp (nth 7 event
)))
407 (functionp (nth 8 event
)))
408 (signal 'dbus-error
(list "Not a valid D-Bus event" event
))))
411 (defun dbus-handle-event (event)
412 "Handle events from the D-Bus.
413 EVENT is a D-Bus event, see `dbus-check-event'. HANDLER, being
414 part of the event, is called with arguments ARGS.
415 If the HANDLER returns a `dbus-error', it is propagated as return message."
419 ;; We ignore not well-formed events.
420 (dbus-check-event event
)
421 ;; Error messages must be propagated.
422 (when (= dbus-message-type-error
(nth 2 event
))
423 (signal 'dbus-error
(nthcdr 9 event
)))
424 ;; Apply the handler.
425 (setq result
(apply (nth 8 event
) (nthcdr 9 event
)))
426 ;; Return a message when it is a message call.
427 (when (= dbus-message-type-method-call
(nth 2 event
))
429 (if (eq result
:ignore
)
430 (dbus-method-return-internal
431 (nth 1 event
) (nth 3 event
) (nth 4 event
))
432 (apply 'dbus-method-return-internal
433 (nth 1 event
) (nth 3 event
) (nth 4 event
)
434 (if (consp result
) result
(list result
)))))))
437 ;; Return an error message when it is a message call.
438 (when (= dbus-message-type-method-call
(nth 2 event
))
440 (dbus-method-error-internal
441 (nth 1 event
) (nth 3 event
) (nth 4 event
) (cadr err
))))
442 ;; Propagate D-Bus error messages.
443 (run-hook-with-args 'dbus-event-error-hooks event err
)
444 (when (or dbus-debug
(= dbus-message-type-error
(nth 2 event
)))
445 (signal (car err
) (cdr err
))))))
447 (defun dbus-event-bus-name (event)
448 "Return the bus name the event is coming from.
449 The result is either a Lisp symbol, `:system' or `:session', or a
450 string denoting the bus address. EVENT is a D-Bus event, see
451 `dbus-check-event'. This function raises a `dbus-error' signal
452 in case the event is not well formed."
453 (dbus-check-event event
)
456 (defun dbus-event-message-type (event)
457 "Return the message type of the corresponding D-Bus message.
458 The result is a number. EVENT is a D-Bus event, see
459 `dbus-check-event'. This function raises a `dbus-error' signal
460 in case the event is not well formed."
461 (dbus-check-event event
)
464 (defun dbus-event-serial-number (event)
465 "Return the serial number of the corresponding D-Bus message.
466 The result is a number. The serial number is needed for
467 generating a reply message. EVENT is a D-Bus event, see
468 `dbus-check-event'. This function raises a `dbus-error' signal
469 in case the event is not well formed."
470 (dbus-check-event event
)
473 (defun dbus-event-service-name (event)
474 "Return the name of the D-Bus object the event is coming from.
475 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
476 This function raises a `dbus-error' signal in case the event is
478 (dbus-check-event event
)
481 (defun dbus-event-path-name (event)
482 "Return the object path of the D-Bus object the event is coming from.
483 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
484 This function raises a `dbus-error' signal in case the event is
486 (dbus-check-event event
)
489 (defun dbus-event-interface-name (event)
490 "Return the interface name of the D-Bus object the event is coming from.
491 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
492 This function raises a `dbus-error' signal in case the event is
494 (dbus-check-event event
)
497 (defun dbus-event-member-name (event)
498 "Return the member name the event is coming from.
499 It is either a signal name or a method name. The result is is a
500 string. EVENT is a D-Bus event, see `dbus-check-event'. This
501 function raises a `dbus-error' signal in case the event is not
503 (dbus-check-event event
)
507 ;;; D-Bus registered names.
509 (defun dbus-list-activatable-names ()
510 "Return the D-Bus service names which can be activated as list.
511 The result is a list of strings, which is `nil' when there are no
512 activatable service names at all."
515 :system dbus-service-dbus
516 dbus-path-dbus dbus-interface-dbus
"ListActivatableNames")))
518 (defun dbus-list-names (bus)
519 "Return the service names registered at D-Bus BUS.
520 The result is a list of strings, which is `nil' when there are no
521 registered service names at all. Well known names are strings
522 like \"org.freedesktop.DBus\". Names starting with \":\" are
523 unique names for services."
526 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
"ListNames")))
528 (defun dbus-list-known-names (bus)
529 "Retrieve all services which correspond to a known name in BUS.
530 A service has a known name if it doesn't start with \":\"."
532 (dolist (name (dbus-list-names bus
) result
)
533 (unless (string-equal ":" (substring name
0 1))
534 (add-to-list 'result name
'append
)))))
536 (defun dbus-list-queued-owners (bus service
)
537 "Return the unique names registered at D-Bus BUS and queued for SERVICE.
538 The result is a list of strings, or `nil' when there are no
539 queued name owners service names at all."
542 bus dbus-service-dbus dbus-path-dbus
543 dbus-interface-dbus
"ListQueuedOwners" service
)))
545 (defun dbus-get-name-owner (bus service
)
546 "Return the name owner of SERVICE registered at D-Bus BUS.
547 The result is either a string, or `nil' if there is no name owner."
550 bus dbus-service-dbus dbus-path-dbus
551 dbus-interface-dbus
"GetNameOwner" service
)))
553 (defun dbus-ping (bus service
&optional timeout
)
554 "Check whether SERVICE is registered for D-Bus BUS.
555 TIMEOUT, a nonnegative integer, specifies the maximum number of
556 milliseconds `dbus-ping' must return. The default value is 25,000.
558 Note, that this autoloads SERVICE if it is not running yet. If
559 it shall be checked whether SERVICE is already running, one shall
562 \(member service \(dbus-list-known-names bus))"
563 ;; "Ping" raises a D-Bus error if SERVICE does not exist.
564 ;; Otherwise, it returns silently with `nil'.
567 (if (natnump timeout
)
569 bus service dbus-path-dbus dbus-interface-peer
570 "Ping" :timeout timeout
)
572 bus service dbus-path-dbus dbus-interface-peer
"Ping")))
576 ;;; D-Bus introspection.
578 (defun dbus-introspect (bus service path
)
579 "Return all interfaces and sub-nodes of SERVICE,
580 registered at object path PATH at bus BUS.
582 BUS is either a Lisp symbol, `:system' or `:session', or a string
583 denoting the bus address. SERVICE must be a known service name,
584 and PATH must be a valid object path. The last two parameters
585 are strings. The result, the introspection data, is a string in
587 ;; We don't want to raise errors. `dbus-call-method-non-blocking'
588 ;; is used, because the handler can be registered in our Emacs
589 ;; instance; caller an callee would block each other.
592 (if noninteractive
'dbus-call-method
'dbus-call-method-non-blocking
)
593 bus service path dbus-interface-introspectable
"Introspect")))
595 (defun dbus-introspect-xml (bus service path
)
596 "Return the introspection data of SERVICE in D-Bus BUS at object path PATH.
597 The data are a parsed list. The root object is a \"node\",
598 representing the object path PATH. The root object can contain
599 \"interface\" and further \"node\" objects."
600 ;; We don't want to raise errors.
604 (insert (dbus-introspect bus service path
))
605 (xml-parse-region (point-min) (point-max))))))
607 (defun dbus-introspect-get-attribute (object attribute
)
608 "Return the ATTRIBUTE value of D-Bus introspection OBJECT.
609 ATTRIBUTE must be a string according to the attribute names in
610 the D-Bus specification."
611 (xml-get-attribute-or-nil object
(intern attribute
)))
613 (defun dbus-introspect-get-node-names (bus service path
)
614 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
615 It returns a list of strings. The node names stand for further
616 object paths of the D-Bus service."
617 (let ((object (dbus-introspect-xml bus service path
))
619 (dolist (elt (xml-get-children object
'node
) result
)
621 'result
(dbus-introspect-get-attribute elt
"name") 'append
))))
623 (defun dbus-introspect-get-all-nodes (bus service path
)
624 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
625 It returns a list of strings, which are further object paths of SERVICE."
626 (let ((result (list path
)))
628 (dbus-introspect-get-node-names bus service path
)
630 (setq elt
(expand-file-name elt path
))
632 (append result
(dbus-introspect-get-all-nodes bus service elt
))))))
634 (defun dbus-introspect-get-interface-names (bus service path
)
635 "Return all interface names of SERVICE in D-Bus BUS at object path PATH.
636 It returns a list of strings.
638 There will be always the default interface
639 \"org.freedesktop.DBus.Introspectable\". Another default
640 interface is \"org.freedesktop.DBus.Properties\". If present,
641 \"interface\" objects can also have \"property\" objects as
642 children, beside \"method\" and \"signal\" objects."
643 (let ((object (dbus-introspect-xml bus service path
))
645 (dolist (elt (xml-get-children object
'interface
) result
)
647 'result
(dbus-introspect-get-attribute elt
"name") 'append
))))
649 (defun dbus-introspect-get-interface (bus service path interface
)
650 "Return the INTERFACE of SERVICE in D-Bus BUS at object path PATH.
651 The return value is an XML object. INTERFACE must be a string,
652 element of the list returned by `dbus-introspect-get-interface-names'.
653 The resulting \"interface\" object can contain \"method\", \"signal\",
654 \"property\" and \"annotation\" children."
655 (let ((elt (xml-get-children
656 (dbus-introspect-xml bus service path
) 'interface
)))
660 (dbus-introspect-get-attribute (car elt
) "name"))))
661 (setq elt
(cdr elt
)))
664 (defun dbus-introspect-get-method-names (bus service path interface
)
665 "Return a list of strings of all method names of INTERFACE.
666 SERVICE is a service of D-Bus BUS at object path PATH."
667 (let ((object (dbus-introspect-get-interface bus service path interface
))
669 (dolist (elt (xml-get-children object
'method
) result
)
671 'result
(dbus-introspect-get-attribute elt
"name") 'append
))))
673 (defun dbus-introspect-get-method (bus service path interface method
)
674 "Return method METHOD of interface INTERFACE as XML object.
675 It must be located at SERVICE in D-Bus BUS at object path PATH.
676 METHOD must be a string, element of the list returned by
677 `dbus-introspect-get-method-names'. The resulting \"method\"
678 object can contain \"arg\" and \"annotation\" children."
679 (let ((elt (xml-get-children
680 (dbus-introspect-get-interface bus service path interface
)
684 method
(dbus-introspect-get-attribute (car elt
) "name"))))
685 (setq elt
(cdr elt
)))
688 (defun dbus-introspect-get-signal-names (bus service path interface
)
689 "Return a list of strings of all signal names of INTERFACE.
690 SERVICE is a service of D-Bus BUS at object path PATH."
691 (let ((object (dbus-introspect-get-interface bus service path interface
))
693 (dolist (elt (xml-get-children object
'signal
) result
)
695 'result
(dbus-introspect-get-attribute elt
"name") 'append
))))
697 (defun dbus-introspect-get-signal (bus service path interface signal
)
698 "Return signal SIGNAL of interface INTERFACE as XML object.
699 It must be located at SERVICE in D-Bus BUS at object path PATH.
700 SIGNAL must be a string, element of the list returned by
701 `dbus-introspect-get-signal-names'. The resulting \"signal\"
702 object can contain \"arg\" and \"annotation\" children."
703 (let ((elt (xml-get-children
704 (dbus-introspect-get-interface bus service path interface
)
708 signal
(dbus-introspect-get-attribute (car elt
) "name"))))
709 (setq elt
(cdr elt
)))
712 (defun dbus-introspect-get-property-names (bus service path interface
)
713 "Return a list of strings of all property names of INTERFACE.
714 SERVICE is a service of D-Bus BUS at object path PATH."
715 (let ((object (dbus-introspect-get-interface bus service path interface
))
717 (dolist (elt (xml-get-children object
'property
) result
)
719 'result
(dbus-introspect-get-attribute elt
"name") 'append
))))
721 (defun dbus-introspect-get-property (bus service path interface property
)
722 "This function returns PROPERTY of INTERFACE as XML object.
723 It must be located at SERVICE in D-Bus BUS at object path PATH.
724 PROPERTY must be a string, element of the list returned by
725 `dbus-introspect-get-property-names'. The resulting PROPERTY
726 object can contain \"annotation\" children."
727 (let ((elt (xml-get-children
728 (dbus-introspect-get-interface bus service path interface
)
733 (dbus-introspect-get-attribute (car elt
) "name"))))
734 (setq elt
(cdr elt
)))
737 (defun dbus-introspect-get-annotation-names
738 (bus service path interface
&optional name
)
739 "Return all annotation names as list of strings.
740 If NAME is `nil', the annotations are children of INTERFACE,
741 otherwise NAME must be a \"method\", \"signal\", or \"property\"
742 object, where the annotations belong to."
745 (or (dbus-introspect-get-method bus service path interface name
)
746 (dbus-introspect-get-signal bus service path interface name
)
747 (dbus-introspect-get-property bus service path interface name
))
748 (dbus-introspect-get-interface bus service path interface
)))
750 (dolist (elt (xml-get-children object
'annotation
) result
)
752 'result
(dbus-introspect-get-attribute elt
"name") 'append
))))
754 (defun dbus-introspect-get-annotation
755 (bus service path interface name annotation
)
756 "Return ANNOTATION as XML object.
757 If NAME is `nil', ANNOTATION is a child of INTERFACE, otherwise
758 NAME must be the name of a \"method\", \"signal\", or
759 \"property\" object, where the ANNOTATION belongs to."
760 (let ((elt (xml-get-children
762 (or (dbus-introspect-get-method
763 bus service path interface name
)
764 (dbus-introspect-get-signal
765 bus service path interface name
)
766 (dbus-introspect-get-property
767 bus service path interface name
))
768 (dbus-introspect-get-interface bus service path interface
))
773 (dbus-introspect-get-attribute (car elt
) "name"))))
774 (setq elt
(cdr elt
)))
777 (defun dbus-introspect-get-argument-names (bus service path interface name
)
778 "Return a list of all argument names as list of strings.
779 NAME must be a \"method\" or \"signal\" object.
781 Argument names are optional, the function can return `nil'
782 therefore, even if the method or signal has arguments."
784 (or (dbus-introspect-get-method bus service path interface name
)
785 (dbus-introspect-get-signal bus service path interface name
)))
787 (dolist (elt (xml-get-children object
'arg
) result
)
789 'result
(dbus-introspect-get-attribute elt
"name") 'append
))))
791 (defun dbus-introspect-get-argument (bus service path interface name arg
)
792 "Return argument ARG as XML object.
793 NAME must be a \"method\" or \"signal\" object. ARG must be a string,
794 element of the list returned by `dbus-introspect-get-argument-names'."
795 (let ((elt (xml-get-children
796 (or (dbus-introspect-get-method bus service path interface name
)
797 (dbus-introspect-get-signal bus service path interface name
))
801 arg
(dbus-introspect-get-attribute (car elt
) "name"))))
802 (setq elt
(cdr elt
)))
805 (defun dbus-introspect-get-signature
806 (bus service path interface name
&optional direction
)
807 "Return signature of a `method' or `signal', represented by NAME, as string.
808 If NAME is a `method', DIRECTION can be either \"in\" or \"out\".
809 If DIRECTION is `nil', \"in\" is assumed.
811 If NAME is a `signal', and DIRECTION is non-`nil', DIRECTION must
813 ;; For methods, we use "in" as default direction.
814 (let ((object (or (dbus-introspect-get-method
815 bus service path interface name
)
816 (dbus-introspect-get-signal
817 bus service path interface name
))))
818 (when (and (string-equal
819 "method" (dbus-introspect-get-attribute object
"name"))
820 (not (stringp direction
)))
821 (setq direction
"in"))
822 ;; In signals, no direction is given.
823 (when (string-equal "signal" (dbus-introspect-get-attribute object
"name"))
824 (setq direction nil
))
825 ;; Collect the signatures.
828 (let ((arg (dbus-introspect-get-argument
829 bus service path interface name x
)))
830 (if (or (not (stringp direction
))
833 (dbus-introspect-get-attribute arg
"direction")))
834 (dbus-introspect-get-attribute arg
"type")
836 (dbus-introspect-get-argument-names bus service path interface name
)
840 ;;; D-Bus properties.
842 (defun dbus-get-property (bus service path interface property
)
843 "Return the value of PROPERTY of INTERFACE.
844 It will be checked at BUS, SERVICE, PATH. The result can be any
845 valid D-Bus value, or `nil' if there is no PROPERTY."
847 ;; "Get" returns a variant, so we must use the `car'.
850 (if noninteractive
'dbus-call-method
'dbus-call-method-non-blocking
)
851 bus service path dbus-interface-properties
852 "Get" :timeout
500 interface property
))))
854 (defun dbus-set-property (bus service path interface property value
)
855 "Set value of PROPERTY of INTERFACE to VALUE.
856 It will be checked at BUS, SERVICE, PATH. When the value has
857 been set successful, the result is VALUE. Otherwise, `nil' is
860 ;; "Set" requires a variant.
862 (if noninteractive
'dbus-call-method
'dbus-call-method-non-blocking
)
863 bus service path dbus-interface-properties
864 "Set" :timeout
500 interface property
(list :variant value
))
866 (dbus-get-property bus service path interface property
)))
868 (defun dbus-get-all-properties (bus service path interface
)
869 "Return all properties of INTERFACE at BUS, SERVICE, PATH.
870 The result is a list of entries. Every entry is a cons of the
871 name of the property, and its value. If there are no properties,
874 ;; "GetAll" returns "a{sv}".
880 'dbus-call-method-non-blocking
)
881 bus service path dbus-interface-properties
882 "GetAll" :timeout
500 interface
)
884 (add-to-list 'result
(cons (car dict
) (caadr dict
)) 'append
)))))
886 (defun dbus-register-property
887 (bus service path interface property access value
888 &optional emits-signal dont-register-service
)
889 "Register property PROPERTY on the D-Bus BUS.
891 BUS is either a Lisp symbol, `:system' or `:session', or a string
892 denoting the bus address.
894 SERVICE is the D-Bus service name of the D-Bus. It must be a
895 known name (See discussion of DONT-REGISTER-SERVICE below).
897 PATH is the D-Bus object path SERVICE is registered (See
898 discussion of DONT-REGISTER-SERVICE below). INTERFACE is the
899 name of the interface used at PATH, PROPERTY is the name of the
900 property of INTERFACE. ACCESS indicates, whether the property
901 can be changed by other services via D-Bus. It must be either
902 the symbol `:read' or `:readwrite'. VALUE is the initial value
903 of the property, it can be of any valid type (see
904 `dbus-call-method' for details).
906 If PROPERTY already exists on PATH, it will be overwritten. For
907 properties with access type `:read' this is the only way to
908 change their values. Properties with access type `:readwrite'
909 can be changed by `dbus-set-property'.
911 The interface \"org.freedesktop.DBus.Properties\" is added to
912 PATH, including a default handler for the \"Get\", \"GetAll\" and
913 \"Set\" methods of this interface. When EMITS-SIGNAL is non-nil,
914 the signal \"PropertiesChanged\" is sent when the property is
915 changed by `dbus-set-property'.
917 When DONT-REGISTER-SERVICE is non-nil, the known name SERVICE is
918 not registered. This means that other D-Bus clients have no way
919 of noticing the newly registered property. When interfaces are
920 constructed incrementally by adding single methods or properties
921 at a time, DONT-REGISTER-SERVICE can be used to prevent other
922 clients from discovering the still incomplete interface."
923 (unless (member access
'(:read
:readwrite
))
924 (signal 'dbus-error
(list "Access type invalid" access
)))
927 (unless (or dont-register-service
928 (member service
(dbus-list-names bus
)))
930 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
931 "RequestName" service
0))
933 ;; Add handlers for the three property-related methods.
934 (dbus-register-method
935 bus service path dbus-interface-properties
"Get"
936 'dbus-property-handler
'dont-register
)
937 (dbus-register-method
938 bus service path dbus-interface-properties
"GetAll"
939 'dbus-property-handler
'dont-register
)
940 (dbus-register-method
941 bus service path dbus-interface-properties
"Set"
942 'dbus-property-handler
'dont-register
)
944 ;; Register the name SERVICE with BUS.
945 (unless dont-register-service
946 (dbus-register-service bus service
))
948 ;; Send the PropertiesChanged signal.
951 bus service path dbus-interface-properties
"PropertiesChanged"
952 (list (list :dict-entry property
(list :variant value
)))
955 ;; Create a hash table entry. We use nil for the unique name,
956 ;; because the property might be accessed from anybody.
957 (let ((key (list bus interface property
))
963 (if emits-signal
(list access
:emits-signal
) (list access
))
965 (puthash key val dbus-registered-objects-table
)
967 ;; Return the object.
968 (list key
(list service path
))))
970 (defun dbus-property-handler (&rest args
)
971 "Default handler for the \"org.freedesktop.DBus.Properties\" interface.
972 It will be registered for all objects created by `dbus-register-object'."
973 (let ((bus (dbus-event-bus-name last-input-event
))
974 (service (dbus-event-service-name last-input-event
))
975 (path (dbus-event-path-name last-input-event
))
976 (method (dbus-event-member-name last-input-event
))
977 (interface (car args
))
978 (property (cadr args
)))
980 ;; "Get" returns a variant.
981 ((string-equal method
"Get")
982 (let ((entry (gethash (list bus interface property
)
983 dbus-registered-objects-table
)))
984 (when (string-equal path
(nth 2 (car entry
)))
985 (list (list :variant
(cdar (last (car entry
))))))))
987 ;; "Set" expects a variant.
988 ((string-equal method
"Set")
989 (let* ((value (caar (cddr args
)))
990 (entry (gethash (list bus interface property
)
991 dbus-registered-objects-table
))
992 ;; The value of the hash table is a list; in case of
993 ;; properties it contains just one element (UNAME SERVICE
994 ;; PATH OBJECT). OBJECT is a cons cell of a list, which
995 ;; contains a list of annotations (like :read,
996 ;; :read-write, :emits-signal), and the value of the
998 (object (car (last (car entry
)))))
999 (unless (consp object
)
1001 (list "Property not registered at path" property path
)))
1002 (unless (member :readwrite
(car object
))
1004 (list "Property not writable at path" property path
)))
1005 (puthash (list bus interface property
)
1006 (list (append (butlast (car entry
))
1007 (list (cons (car object
) value
))))
1008 dbus-registered-objects-table
)
1009 ;; Send the "PropertiesChanged" signal.
1010 (when (member :emits-signal
(car object
))
1012 bus service path dbus-interface-properties
"PropertiesChanged"
1013 (list (list :dict-entry property
(list :variant value
)))
1015 ;; Return empty reply.
1018 ;; "GetAll" returns "a{sv}".
1019 ((string-equal method
"GetAll")
1023 (when (and (equal (butlast key
) (list bus interface
))
1024 (string-equal path
(nth 2 (car val
)))
1025 (not (functionp (car (last (car val
))))))
1030 (list :variant
(cdar (last (car val
))))))))
1031 dbus-registered-objects-table
)
1035 ;; Initialize :system and :session buses. This adds their file
1036 ;; descriptors to input_wait_mask, in order to detect incoming
1037 ;; messages immediately.
1038 (when (featurep 'dbusbind
)
1040 (dbus-init-bus :system
)
1041 (dbus-init-bus :session
)))
1045 ;;; dbus.el ends here