Add new VC methods: vc-log-incoming and vc-log-outgoing.
[emacs.git] / lisp / net / dbus.el
blob5635640cd4c92abccd1552a46d17800273fa9f46
1 ;;; dbus.el --- Elisp bindings for D-Bus.
3 ;; Copyright (C) 2007, 2008, 2009, 2010 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/>.
23 ;;; Commentary:
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.
31 ;;; Code:
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 (defvar dbus-debug)
42 (defvar dbus-registered-objects-table)
44 ;; Pacify byte compiler.
45 (eval-when-compile
46 (require 'cl))
48 (require 'xml)
50 (defconst dbus-service-dbus "org.freedesktop.DBus"
51 "The bus name used to talk to the bus itself.")
53 (defconst dbus-path-dbus "/org/freedesktop/DBus"
54 "The object path used to talk to the bus itself.")
56 (defconst dbus-interface-dbus "org.freedesktop.DBus"
57 "The interface exported by the object with `dbus-service-dbus' and `dbus-path-dbus'.")
59 (defconst dbus-interface-peer (concat dbus-interface-dbus ".Peer")
60 "The interface for peer objects.")
62 (defconst dbus-interface-introspectable
63 (concat dbus-interface-dbus ".Introspectable")
64 "The interface supported by introspectable objects.")
66 (defconst dbus-interface-properties (concat dbus-interface-dbus ".Properties")
67 "The interface for property objects.")
69 (defconst dbus-service-emacs "org.gnu.Emacs"
70 "The well known service name of Emacs.")
72 (defconst dbus-path-emacs "/org/gnu/Emacs"
73 "The object path head used by Emacs.")
75 (defconst dbus-message-type-invalid 0
76 "This value is never a valid message type.")
78 (defconst dbus-message-type-method-call 1
79 "Message type of a method call message.")
81 (defconst dbus-message-type-method-return 2
82 "Message type of a method return message.")
84 (defconst dbus-message-type-error 3
85 "Message type of an error reply message.")
87 (defconst dbus-message-type-signal 4
88 "Message type of a signal message.")
90 (defmacro dbus-ignore-errors (&rest body)
91 "Execute BODY; signal D-Bus error when `dbus-debug' is non-nil.
92 Otherwise, return result of last form in BODY, or all other errors."
93 `(condition-case err
94 (progn ,@body)
95 (dbus-error (when dbus-debug (signal (car err) (cdr err))))))
97 (put 'dbus-ignore-errors 'lisp-indent-function 0)
98 (put 'dbus-ignore-errors 'edebug-form-spec '(form body))
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 ;; We create it here. So we have a simple test in dbusbind.c, whether
110 ;; the Lisp code has been loaded.
111 (setq dbus-registered-objects-table (make-hash-table :test 'equal))
113 (defvar dbus-return-values-table (make-hash-table :test 'equal)
114 "Hash table for temporary storing arguments of reply messages.
115 A key in this hash table is a list (BUS SERIAL). BUS is either the
116 symbol `:system' or the symbol `:session'. SERIAL is the serial number
117 of the reply message. See `dbus-call-method-non-blocking-handler' and
118 `dbus-call-method-non-blocking'.")
120 (defun dbus-list-hash-table ()
121 "Returns all registered member registrations to D-Bus.
122 The return value is a list, with elements of kind (KEY . VALUE).
123 See `dbus-registered-objects-table' for a description of the
124 hash table."
125 (let (result)
126 (maphash
127 '(lambda (key value) (add-to-list 'result (cons key value) 'append))
128 dbus-registered-objects-table)
129 result))
131 (defun dbus-unregister-object (object)
132 "Unregister OBJECT from D-Bus.
133 OBJECT must be the result of a preceding `dbus-register-method',
134 `dbus-register-property' or `dbus-register-signal' call. It
135 returns `t' if OBJECT has been unregistered, `nil' otherwise.
137 When OBJECT identifies the last method or property, which is
138 registered for the respective service, Emacs releases its
139 association to the service from D-Bus."
140 ;; Check parameter.
141 (unless (and (consp object) (not (null (car object))) (consp (cdr object)))
142 (signal 'wrong-type-argument (list 'D-Bus object)))
144 ;; Find the corresponding entry in the hash table.
145 (let* ((key (car object))
146 (value (cdr object))
147 (entry (gethash key dbus-registered-objects-table))
148 ret)
149 ;; entry has the structure ((UNAME SERVICE PATH MEMBER) ...).
150 ;; value has the structure ((SERVICE PATH [HANDLER]) ...).
151 ;; MEMBER is either a string (the handler), or a cons cell (a
152 ;; property value). UNAME and property values are not taken into
153 ;; account for comparision.
155 ;; Loop over the registered functions.
156 (dolist (elt entry)
157 (when (equal
158 (car value)
159 (butlast (cdr elt) (- (length (cdr elt)) (length (car value)))))
160 ;; Compute new hash value. If it is empty, remove it from the
161 ;; hash table.
162 (unless (puthash key (delete elt entry) dbus-registered-objects-table)
163 (remhash key dbus-registered-objects-table))
164 (setq ret t)))
165 ;; Check, whether there is still a registered function or property
166 ;; for the given service. If not, unregister the service from the
167 ;; bus.
168 (dolist (elt entry)
169 (let ((service (cadr elt))
170 (bus (car key))
171 found)
172 (maphash
173 (lambda (k v)
174 (dolist (e v)
175 (ignore-errors
176 (when (and (equal bus (car k)) (string-equal service (cadr e)))
177 (setq found t)))))
178 dbus-registered-objects-table)
179 (unless found
180 (dbus-call-method
181 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
182 "ReleaseName" service))))
183 ;; Return.
184 ret))
186 (defun dbus-unregister-service (bus service)
187 "Unregister all objects related to SERVICE from D-Bus BUS.
188 BUS must be either the symbol `:system' or the symbol `:session'.
189 SERVICE must be a known service name."
190 (maphash
191 (lambda (key value)
192 (dolist (elt value)
193 (ignore-errors
194 (when (and (equal bus (car key)) (string-equal service (cadr elt)))
195 (unless
196 (puthash key (delete elt value) dbus-registered-objects-table)
197 (remhash key dbus-registered-objects-table))))))
198 dbus-registered-objects-table)
199 (dbus-call-method
200 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
201 "ReleaseName" service))
203 (defun dbus-call-method-non-blocking-handler (&rest args)
204 "Handler for reply messages of asynchronous D-Bus message calls.
205 It calls the function stored in `dbus-registered-objects-table'.
206 The result will be made available in `dbus-return-values-table'."
207 (puthash (list (dbus-event-bus-name last-input-event)
208 (dbus-event-serial-number last-input-event))
209 (if (= (length args) 1) (car args) args)
210 dbus-return-values-table))
212 (defun dbus-call-method-non-blocking
213 (bus service path interface method &rest args)
214 "Call METHOD on the D-Bus BUS, but don't block the event queue.
215 This is necessary for communicating to registered D-Bus methods,
216 which are running in the same Emacs process.
218 The arguments are the same as in `dbus-call-method'.
220 usage: (dbus-call-method-non-blocking
221 BUS SERVICE PATH INTERFACE METHOD
222 &optional :timeout TIMEOUT &rest ARGS)"
224 (let ((key
225 (apply
226 'dbus-call-method-asynchronously
227 bus service path interface method
228 'dbus-call-method-non-blocking-handler args)))
229 ;; Wait until `dbus-call-method-non-blocking-handler' has put the
230 ;; result into `dbus-return-values-table'.
231 (while (eq (gethash key dbus-return-values-table :ignore) :ignore)
232 (read-event nil nil 0.1))
234 ;; Cleanup `dbus-return-values-table'. Return the result.
235 (prog1
236 (gethash key dbus-return-values-table nil)
237 (remhash key dbus-return-values-table))))
239 (defun dbus-name-owner-changed-handler (&rest args)
240 "Reapplies all member registrations to D-Bus.
241 This handler is applied when a \"NameOwnerChanged\" signal has
242 arrived. SERVICE is the object name for which the name owner has
243 been changed. OLD-OWNER is the previous owner of SERVICE, or the
244 empty string if SERVICE was not owned yet. NEW-OWNER is the new
245 owner of SERVICE, or the empty string if SERVICE looses any name owner.
247 usage: (dbus-name-owner-changed-handler service old-owner new-owner)"
248 (save-match-data
249 ;; Check the arguments. We should silently ignore it when they
250 ;; are wrong.
251 (if (and (= (length args) 3)
252 (stringp (car args))
253 (stringp (cadr args))
254 (stringp (caddr args)))
255 (let ((service (car args))
256 (old-owner (cadr args))
257 (new-owner (caddr args)))
258 ;; Check whether SERVICE is a known name.
259 (when (not (string-match "^:" service))
260 (maphash
261 '(lambda (key value)
262 (dolist (elt value)
263 ;; key has the structure (BUS INTERFACE MEMBER).
264 ;; elt has the structure (UNAME SERVICE PATH HANDLER).
265 (when (string-equal old-owner (car elt))
266 ;; Remove old key, and add new entry with changed name.
267 (dbus-unregister-object (list key (cdr elt)))
268 ;; Maybe we could arrange the lists a little bit better
269 ;; that we don't need to extract every single element?
270 (dbus-register-signal
271 ;; BUS SERVICE PATH
272 (nth 0 key) (nth 1 elt) (nth 2 elt)
273 ;; INTERFACE MEMBER HANDLER
274 (nth 1 key) (nth 2 key) (nth 3 elt)))))
275 (copy-hash-table dbus-registered-objects-table))))
276 ;; The error is reported only in debug mode.
277 (when dbus-debug
278 (signal
279 'dbus-error
280 (cons
281 (format "Wrong arguments of %s.NameOwnerChanged" dbus-interface-dbus)
282 args))))))
284 ;; Register the handler.
285 (when nil ;ignore-errors
286 (dbus-register-signal
287 :system dbus-service-dbus dbus-path-dbus dbus-interface-dbus
288 "NameOwnerChanged" 'dbus-name-owner-changed-handler)
289 (dbus-register-signal
290 :session dbus-service-dbus dbus-path-dbus dbus-interface-dbus
291 "NameOwnerChanged" 'dbus-name-owner-changed-handler))
294 ;;; D-Bus type conversion.
296 (defun dbus-string-to-byte-array (string)
297 "Transforms STRING to list (:array :byte c1 :byte c2 ...).
298 STRING shall be UTF8 coded."
299 (if (zerop (length string))
300 '(:array :signature "y")
301 (let (result)
302 (dolist (elt (string-to-list string) (append '(:array) result))
303 (setq result (append result (list :byte elt)))))))
305 (defun dbus-byte-array-to-string (byte-array)
306 "Transforms BYTE-ARRAY into UTF8 coded string.
307 BYTE-ARRAY must be a list of structure (c1 c2 ...)."
308 (apply 'string byte-array))
310 (defun dbus-escape-as-identifier (string)
311 "Escape an arbitrary STRING so it follows the rules for a C identifier.
312 The escaped string can be used as object path component, interface element
313 component, bus name component or member name in D-Bus.
315 The escaping consists of replacing all non-alphanumerics, and the
316 first character if it's a digit, with an underscore and two
317 lower-case hex digits:
319 \"0123abc_xyz\\x01\\xff\" -> \"_30123abc_5fxyz_01_ff\"
321 i.e. similar to URI encoding, but with \"_\" taking the role of \"%\",
322 and a smaller allowed set. As a special case, \"\" is escaped to
323 \"_\".
325 Returns the escaped string. Algorithm taken from
326 telepathy-glib's `tp-escape-as-identifier'."
327 (if (zerop (length string))
329 (replace-regexp-in-string
330 "^[0-9]\\|[^A-Za-z0-9]"
331 (lambda (x) (format "_%2x" (aref x 0)))
332 string)))
334 (defun dbus-unescape-from-identifier (string)
335 "Retrieve the original string from the encoded STRING.
336 STRING must have been coded with `dbus-escape-as-identifier'"
337 (if (string-equal string "_")
339 (replace-regexp-in-string
340 "_.."
341 (lambda (x) (format "%c" (string-to-number (substring x 1) 16)))
342 string)))
345 ;;; D-Bus events.
347 (defun dbus-check-event (event)
348 "Checks whether EVENT is a well formed D-Bus event.
349 EVENT is a list which starts with symbol `dbus-event':
351 (dbus-event BUS TYPE SERIAL SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS)
353 BUS identifies the D-Bus the message is coming from. It is
354 either the symbol `:system' or the symbol `:session'. TYPE is
355 the D-Bus message type which has caused the event, SERIAL is the
356 serial number of the received D-Bus message. SERVICE and PATH
357 are the unique name and the object path of the D-Bus object
358 emitting the message. INTERFACE and MEMBER denote the message
359 which has been sent. HANDLER is the function which has been
360 registered for this message. ARGS are the arguments passed to
361 HANDLER, when it is called during event handling in
362 `dbus-handle-event'.
364 This function raises a `dbus-error' signal in case the event is
365 not well formed."
366 (when dbus-debug (message "DBus-Event %s" event))
367 (unless (and (listp event)
368 (eq (car event) 'dbus-event)
369 ;; Bus symbol.
370 (symbolp (nth 1 event))
371 ;; Type.
372 (and (natnump (nth 2 event))
373 (< dbus-message-type-invalid (nth 2 event)))
374 ;; Serial.
375 (natnump (nth 3 event))
376 ;; Service.
377 (or (= dbus-message-type-method-return (nth 2 event))
378 (= dbus-message-type-error (nth 2 event))
379 (stringp (nth 4 event)))
380 ;; Object path.
381 (or (= dbus-message-type-method-return (nth 2 event))
382 (= dbus-message-type-error (nth 2 event))
383 (stringp (nth 5 event)))
384 ;; Interface.
385 (or (= dbus-message-type-method-return (nth 2 event))
386 (= dbus-message-type-error (nth 2 event))
387 (stringp (nth 6 event)))
388 ;; Member.
389 (or (= dbus-message-type-method-return (nth 2 event))
390 (= dbus-message-type-error (nth 2 event))
391 (stringp (nth 7 event)))
392 ;; Handler.
393 (functionp (nth 8 event)))
394 (signal 'dbus-error (list "Not a valid D-Bus event" event))))
396 ;;;###autoload
397 (defun dbus-handle-event (event)
398 "Handle events from the D-Bus.
399 EVENT is a D-Bus event, see `dbus-check-event'. HANDLER, being
400 part of the event, is called with arguments ARGS.
401 If the HANDLER returns an `dbus-error', it is propagated as return message."
402 (interactive "e")
403 (condition-case err
404 (let (result)
405 ;; We ignore not well-formed events.
406 (dbus-check-event event)
407 ;; Error messages must be propagated.
408 (when (= dbus-message-type-error (nth 2 event))
409 (signal 'dbus-error (nthcdr 9 event)))
410 ;; Apply the handler.
411 (setq result (apply (nth 8 event) (nthcdr 9 event)))
412 ;; Return a message when it is a message call.
413 (when (= dbus-message-type-method-call (nth 2 event))
414 (dbus-ignore-errors
415 (if (eq result :ignore)
416 (dbus-method-return-internal
417 (nth 1 event) (nth 3 event) (nth 4 event))
418 (apply 'dbus-method-return-internal
419 (nth 1 event) (nth 3 event) (nth 4 event)
420 (if (consp result) result (list result)))))))
421 ;; Error handling.
422 (dbus-error
423 ;; Return an error message when it is a message call.
424 (when (= dbus-message-type-method-call (nth 2 event))
425 (dbus-ignore-errors
426 (dbus-method-error-internal
427 (nth 1 event) (nth 3 event) (nth 4 event) (cadr err))))
428 ;; Propagate D-Bus error messages.
429 (run-hook-with-args 'dbus-event-error-hooks event err)
430 (when (or dbus-debug (= dbus-message-type-error (nth 2 event)))
431 (signal (car err) (cdr err))))))
433 (defun dbus-event-bus-name (event)
434 "Return the bus name the event is coming from.
435 The result is either the symbol `:system' or the symbol `:session'.
436 EVENT is a D-Bus event, see `dbus-check-event'. This function
437 raises a `dbus-error' signal in case the event is not well
438 formed."
439 (dbus-check-event event)
440 (nth 1 event))
442 (defun dbus-event-message-type (event)
443 "Return the message type of the corresponding D-Bus message.
444 The result is a number. EVENT is a D-Bus event, see
445 `dbus-check-event'. This function raises a `dbus-error' signal
446 in case the event is not well formed."
447 (dbus-check-event event)
448 (nth 2 event))
450 (defun dbus-event-serial-number (event)
451 "Return the serial number of the corresponding D-Bus message.
452 The result is a number. The serial number is needed for
453 generating a reply message. EVENT is a D-Bus event, see
454 `dbus-check-event'. This function raises a `dbus-error' signal
455 in case the event is not well formed."
456 (dbus-check-event event)
457 (nth 3 event))
459 (defun dbus-event-service-name (event)
460 "Return the name of the D-Bus object the event is coming from.
461 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
462 This function raises a `dbus-error' signal in case the event is
463 not well formed."
464 (dbus-check-event event)
465 (nth 4 event))
467 (defun dbus-event-path-name (event)
468 "Return the object path of the D-Bus object the event is coming from.
469 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
470 This function raises a `dbus-error' signal in case the event is
471 not well formed."
472 (dbus-check-event event)
473 (nth 5 event))
475 (defun dbus-event-interface-name (event)
476 "Return the interface name of the D-Bus object the event is coming from.
477 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
478 This function raises a `dbus-error' signal in case the event is
479 not well formed."
480 (dbus-check-event event)
481 (nth 6 event))
483 (defun dbus-event-member-name (event)
484 "Return the member name the event is coming from.
485 It is either a signal name or a method name. The result is is a
486 string. EVENT is a D-Bus event, see `dbus-check-event'. This
487 function raises a `dbus-error' signal in case the event is not
488 well formed."
489 (dbus-check-event event)
490 (nth 7 event))
493 ;;; D-Bus registered names.
495 (defun dbus-list-activatable-names ()
496 "Return the D-Bus service names which can be activated as list.
497 The result is a list of strings, which is `nil' when there are no
498 activatable service names at all."
499 (dbus-ignore-errors
500 (dbus-call-method
501 :system dbus-service-dbus
502 dbus-path-dbus dbus-interface-dbus "ListActivatableNames")))
504 (defun dbus-list-names (bus)
505 "Return the service names registered at D-Bus BUS.
506 The result is a list of strings, which is `nil' when there are no
507 registered service names at all. Well known names are strings
508 like \"org.freedesktop.DBus\". Names starting with \":\" are
509 unique names for services."
510 (dbus-ignore-errors
511 (dbus-call-method
512 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus "ListNames")))
514 (defun dbus-list-known-names (bus)
515 "Retrieve all services which correspond to a known name in BUS.
516 A service has a known name if it doesn't start with \":\"."
517 (let (result)
518 (dolist (name (dbus-list-names bus) result)
519 (unless (string-equal ":" (substring name 0 1))
520 (add-to-list 'result name 'append)))))
522 (defun dbus-list-queued-owners (bus service)
523 "Return the unique names registered at D-Bus BUS and queued for SERVICE.
524 The result is a list of strings, or `nil' when there are no
525 queued name owners service names at all."
526 (dbus-ignore-errors
527 (dbus-call-method
528 bus dbus-service-dbus dbus-path-dbus
529 dbus-interface-dbus "ListQueuedOwners" service)))
531 (defun dbus-get-name-owner (bus service)
532 "Return the name owner of SERVICE registered at D-Bus BUS.
533 The result is either a string, or `nil' if there is no name owner."
534 (dbus-ignore-errors
535 (dbus-call-method
536 bus dbus-service-dbus dbus-path-dbus
537 dbus-interface-dbus "GetNameOwner" service)))
539 (defun dbus-ping (bus service &optional timeout)
540 "Check whether SERVICE is registered for D-Bus BUS.
541 TIMEOUT, a nonnegative integer, specifies the maximum number of
542 milliseconds `dbus-ping' must return. The default value is 25,000.
544 Note, that this autoloads SERVICE if it is not running yet. If
545 it shall be checked whether SERVICE is already running, one shall
546 apply
548 \(member service \(dbus-list-known-names bus))"
549 ;; "Ping" raises a D-Bus error if SERVICE does not exist.
550 ;; Otherwise, it returns silently with `nil'.
551 (condition-case nil
552 (not
553 (if (natnump timeout)
554 (dbus-call-method
555 bus service dbus-path-dbus dbus-interface-peer
556 "Ping" :timeout timeout)
557 (dbus-call-method
558 bus service dbus-path-dbus dbus-interface-peer "Ping")))
559 (dbus-error nil)))
562 ;;; D-Bus introspection.
564 (defun dbus-introspect (bus service path)
565 "This function returns all interfaces and sub-nodes of SERVICE,
566 registered at object path PATH at bus BUS.
568 BUS must be either the symbol `:system' or the symbol `:session'.
569 SERVICE must be a known service name, and PATH must be a valid
570 object path. The last two parameters are strings. The result,
571 the introspection data, is a string in XML format."
572 ;; We don't want to raise errors. `dbus-call-method-non-blocking'
573 ;; is used, because the handler can be registered in our Emacs
574 ;; instance; caller an callee would block each other.
575 (dbus-ignore-errors
576 (funcall
577 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
578 bus service path dbus-interface-introspectable "Introspect")))
580 (defun dbus-introspect-xml (bus service path)
581 "Return the introspection data of SERVICE in D-Bus BUS at object path PATH.
582 The data are a parsed list. The root object is a \"node\",
583 representing the object path PATH. The root object can contain
584 \"interface\" and further \"node\" objects."
585 ;; We don't want to raise errors.
586 (xml-node-name
587 (ignore-errors
588 (with-temp-buffer
589 (insert (dbus-introspect bus service path))
590 (xml-parse-region (point-min) (point-max))))))
592 (defun dbus-introspect-get-attribute (object attribute)
593 "Return the ATTRIBUTE value of D-Bus introspection OBJECT.
594 ATTRIBUTE must be a string according to the attribute names in
595 the D-Bus specification."
596 (xml-get-attribute-or-nil object (intern attribute)))
598 (defun dbus-introspect-get-node-names (bus service path)
599 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
600 It returns a list of strings. The node names stand for further
601 object paths of the D-Bus service."
602 (let ((object (dbus-introspect-xml bus service path))
603 result)
604 (dolist (elt (xml-get-children object 'node) result)
605 (add-to-list
606 'result (dbus-introspect-get-attribute elt "name") 'append))))
608 (defun dbus-introspect-get-all-nodes (bus service path)
609 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
610 It returns a list of strings, which are further object paths of SERVICE."
611 (let ((result (list path)))
612 (dolist (elt
613 (dbus-introspect-get-node-names bus service path)
614 result)
615 (setq elt (expand-file-name elt path))
616 (setq result
617 (append result (dbus-introspect-get-all-nodes bus service elt))))))
619 (defun dbus-introspect-get-interface-names (bus service path)
620 "Return all interface names of SERVICE in D-Bus BUS at object path PATH.
621 It returns a list of strings.
623 There will be always the default interface
624 \"org.freedesktop.DBus.Introspectable\". Another default
625 interface is \"org.freedesktop.DBus.Properties\". If present,
626 \"interface\" objects can also have \"property\" objects as
627 children, beside \"method\" and \"signal\" objects."
628 (let ((object (dbus-introspect-xml bus service path))
629 result)
630 (dolist (elt (xml-get-children object 'interface) result)
631 (add-to-list
632 'result (dbus-introspect-get-attribute elt "name") 'append))))
634 (defun dbus-introspect-get-interface (bus service path interface)
635 "Return the INTERFACE of SERVICE in D-Bus BUS at object path PATH.
636 The return value is an XML object. INTERFACE must be a string,
637 element of the list returned by
638 `dbus-introspect-get-interface-names'. The resulting
639 \"interface\" object can contain \"method\", \"signal\",
640 \"property\" and \"annotation\" children."
641 (let ((elt (xml-get-children
642 (dbus-introspect-xml bus service path) 'interface)))
643 (while (and elt
644 (not (string-equal
645 interface
646 (dbus-introspect-get-attribute (car elt) "name"))))
647 (setq elt (cdr elt)))
648 (car elt)))
650 (defun dbus-introspect-get-method-names (bus service path interface)
651 "Return a list of strings of all method names of INTERFACE.
652 SERVICE is a service of D-Bus BUS at object path PATH."
653 (let ((object (dbus-introspect-get-interface bus service path interface))
654 result)
655 (dolist (elt (xml-get-children object 'method) result)
656 (add-to-list
657 'result (dbus-introspect-get-attribute elt "name") 'append))))
659 (defun dbus-introspect-get-method (bus service path interface method)
660 "Return method METHOD of interface INTERFACE as XML object.
661 It must be located at SERVICE in D-Bus BUS at object path PATH.
662 METHOD must be a string, element of the list returned by
663 `dbus-introspect-get-method-names'. The resulting \"method\"
664 object can contain \"arg\" and \"annotation\" children."
665 (let ((elt (xml-get-children
666 (dbus-introspect-get-interface bus service path interface)
667 'method)))
668 (while (and elt
669 (not (string-equal
670 method (dbus-introspect-get-attribute (car elt) "name"))))
671 (setq elt (cdr elt)))
672 (car elt)))
674 (defun dbus-introspect-get-signal-names (bus service path interface)
675 "Return a list of strings of all signal names of INTERFACE.
676 SERVICE is a service of D-Bus BUS at object path PATH."
677 (let ((object (dbus-introspect-get-interface bus service path interface))
678 result)
679 (dolist (elt (xml-get-children object 'signal) result)
680 (add-to-list
681 'result (dbus-introspect-get-attribute elt "name") 'append))))
683 (defun dbus-introspect-get-signal (bus service path interface signal)
684 "Return signal SIGNAL of interface INTERFACE as XML object.
685 It must be located at SERVICE in D-Bus BUS at object path PATH.
686 SIGNAL must be a string, element of the list returned by
687 `dbus-introspect-get-signal-names'. The resulting \"signal\"
688 object can contain \"arg\" and \"annotation\" children."
689 (let ((elt (xml-get-children
690 (dbus-introspect-get-interface bus service path interface)
691 'signal)))
692 (while (and elt
693 (not (string-equal
694 signal (dbus-introspect-get-attribute (car elt) "name"))))
695 (setq elt (cdr elt)))
696 (car elt)))
698 (defun dbus-introspect-get-property-names (bus service path interface)
699 "Return a list of strings of all property names of INTERFACE.
700 SERVICE is a service of D-Bus BUS at object path PATH."
701 (let ((object (dbus-introspect-get-interface bus service path interface))
702 result)
703 (dolist (elt (xml-get-children object 'property) result)
704 (add-to-list
705 'result (dbus-introspect-get-attribute elt "name") 'append))))
707 (defun dbus-introspect-get-property (bus service path interface property)
708 "This function returns PROPERTY of INTERFACE as XML object.
709 It must be located at SERVICE in D-Bus BUS at object path PATH.
710 PROPERTY must be a string, element of the list returned by
711 `dbus-introspect-get-property-names'. The resulting PROPERTY
712 object can contain \"annotation\" children."
713 (let ((elt (xml-get-children
714 (dbus-introspect-get-interface bus service path interface)
715 'property)))
716 (while (and elt
717 (not (string-equal
718 property
719 (dbus-introspect-get-attribute (car elt) "name"))))
720 (setq elt (cdr elt)))
721 (car elt)))
723 (defun dbus-introspect-get-annotation-names
724 (bus service path interface &optional name)
725 "Return all annotation names as list of strings.
726 If NAME is `nil', the annotations are children of INTERFACE,
727 otherwise NAME must be a \"method\", \"signal\", or \"property\"
728 object, where the annotations belong to."
729 (let ((object
730 (if name
731 (or (dbus-introspect-get-method bus service path interface name)
732 (dbus-introspect-get-signal bus service path interface name)
733 (dbus-introspect-get-property bus service path interface name))
734 (dbus-introspect-get-interface bus service path interface)))
735 result)
736 (dolist (elt (xml-get-children object 'annotation) result)
737 (add-to-list
738 'result (dbus-introspect-get-attribute elt "name") 'append))))
740 (defun dbus-introspect-get-annotation
741 (bus service path interface name annotation)
742 "Return ANNOTATION as XML object.
743 If NAME is `nil', ANNOTATION is a child of INTERFACE, otherwise
744 NAME must be the name of a \"method\", \"signal\", or
745 \"property\" object, where the ANNOTATION belongs to."
746 (let ((elt (xml-get-children
747 (if name
748 (or (dbus-introspect-get-method
749 bus service path interface name)
750 (dbus-introspect-get-signal
751 bus service path interface name)
752 (dbus-introspect-get-property
753 bus service path interface name))
754 (dbus-introspect-get-interface bus service path interface))
755 'annotation)))
756 (while (and elt
757 (not (string-equal
758 annotation
759 (dbus-introspect-get-attribute (car elt) "name"))))
760 (setq elt (cdr elt)))
761 (car elt)))
763 (defun dbus-introspect-get-argument-names (bus service path interface name)
764 "Return a list of all argument names as list of strings.
765 NAME must be a \"method\" or \"signal\" object.
767 Argument names are optional, the function can return `nil'
768 therefore, even if the method or signal has arguments."
769 (let ((object
770 (or (dbus-introspect-get-method bus service path interface name)
771 (dbus-introspect-get-signal bus service path interface name)))
772 result)
773 (dolist (elt (xml-get-children object 'arg) result)
774 (add-to-list
775 'result (dbus-introspect-get-attribute elt "name") 'append))))
777 (defun dbus-introspect-get-argument (bus service path interface name arg)
778 "Return argument ARG as XML object.
779 NAME must be a \"method\" or \"signal\" object. ARG must be a
780 string, element of the list returned by `dbus-introspect-get-argument-names'."
781 (let ((elt (xml-get-children
782 (or (dbus-introspect-get-method bus service path interface name)
783 (dbus-introspect-get-signal bus service path interface name))
784 'arg)))
785 (while (and elt
786 (not (string-equal
787 arg (dbus-introspect-get-attribute (car elt) "name"))))
788 (setq elt (cdr elt)))
789 (car elt)))
791 (defun dbus-introspect-get-signature
792 (bus service path interface name &optional direction)
793 "Return signature of a `method' or `signal', represented by NAME, as string.
794 If NAME is a `method', DIRECTION can be either \"in\" or \"out\".
795 If DIRECTION is `nil', \"in\" is assumed.
797 If NAME is a `signal', and DIRECTION is non-`nil', DIRECTION must
798 be \"out\"."
799 ;; For methods, we use "in" as default direction.
800 (let ((object (or (dbus-introspect-get-method
801 bus service path interface name)
802 (dbus-introspect-get-signal
803 bus service path interface name))))
804 (when (and (string-equal
805 "method" (dbus-introspect-get-attribute object "name"))
806 (not (stringp direction)))
807 (setq direction "in"))
808 ;; In signals, no direction is given.
809 (when (string-equal "signal" (dbus-introspect-get-attribute object "name"))
810 (setq direction nil))
811 ;; Collect the signatures.
812 (mapconcat
813 '(lambda (x)
814 (let ((arg (dbus-introspect-get-argument
815 bus service path interface name x)))
816 (if (or (not (stringp direction))
817 (string-equal
818 direction
819 (dbus-introspect-get-attribute arg "direction")))
820 (dbus-introspect-get-attribute arg "type")
821 "")))
822 (dbus-introspect-get-argument-names bus service path interface name)
823 "")))
826 ;;; D-Bus properties.
828 (defun dbus-get-property (bus service path interface property)
829 "Return the value of PROPERTY of INTERFACE.
830 It will be checked at BUS, SERVICE, PATH. The result can be any
831 valid D-Bus value, or `nil' if there is no PROPERTY."
832 (dbus-ignore-errors
833 ;; "Get" returns a variant, so we must use the `car'.
834 (car
835 (funcall
836 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
837 bus service path dbus-interface-properties
838 "Get" :timeout 500 interface property))))
840 (defun dbus-set-property (bus service path interface property value)
841 "Set value of PROPERTY of INTERFACE to VALUE.
842 It will be checked at BUS, SERVICE, PATH. When the value has
843 been set successful, the result is VALUE. Otherwise, `nil' is
844 returned."
845 (dbus-ignore-errors
846 ;; "Set" requires a variant.
847 (funcall
848 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
849 bus service path dbus-interface-properties
850 "Set" :timeout 500 interface property (list :variant value))
851 ;; Return VALUE.
852 (dbus-get-property bus service path interface property)))
854 (defun dbus-get-all-properties (bus service path interface)
855 "Return all properties of INTERFACE at BUS, SERVICE, PATH.
856 The result is a list of entries. Every entry is a cons of the
857 name of the property, and its value. If there are no properties,
858 `nil' is returned."
859 (dbus-ignore-errors
860 ;; "GetAll" returns "a{sv}".
861 (let (result)
862 (dolist (dict
863 (funcall
864 (if noninteractive
865 'dbus-call-method
866 'dbus-call-method-non-blocking)
867 bus service path dbus-interface-properties
868 "GetAll" :timeout 500 interface)
869 result)
870 (add-to-list 'result (cons (car dict) (caadr dict)) 'append)))))
872 (defun dbus-register-property
873 (bus service path interface property access value)
874 "Register property PROPERTY on the D-Bus BUS.
876 BUS is either the symbol `:system' or the symbol `:session'.
878 SERVICE is the D-Bus service name of the D-Bus. It must be a
879 known name.
881 PATH is the D-Bus object path SERVICE is registered. INTERFACE
882 is the name of the interface used at PATH, PROPERTY is the name
883 of the property of INTERFACE. ACCESS indicates, whether the
884 property can be changed by other services via D-Bus. It must be
885 either the symbol `:read' or `:readwrite'. VALUE is the initial
886 value of the property, it can be of any valid type (see
887 `dbus-call-method' for details).
889 If PROPERTY already exists on PATH, it will be overwritten. For
890 properties with access type `:read' this is the only way to
891 change their values. Properties with access type `:readwrite'
892 can be changed by `dbus-set-property'.
894 The interface \"org.freedesktop.DBus.Properties\" is added to
895 PATH, including a default handler for the \"Get\", \"GetAll\" and
896 \"Set\" methods of this interface."
897 (unless (member access '(:read :readwrite))
898 (signal 'dbus-error (list "Access type invalid" access)))
900 ;; Register SERVICE.
901 (unless (member service (dbus-list-names bus))
902 (dbus-call-method
903 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
904 "RequestName" service 0))
906 ;; Add the handler. We use `dbus-service-emacs' as service name, in
907 ;; order to let unregister SERVICE despite of this default handler.
908 (dbus-register-method
909 bus service path dbus-interface-properties "Get" 'dbus-property-handler)
910 (dbus-register-method
911 bus service path dbus-interface-properties "GetAll" 'dbus-property-handler)
912 (dbus-register-method
913 bus service path dbus-interface-properties "Set" 'dbus-property-handler)
915 ;; Create a hash table entry. We use nil for the unique name,
916 ;; because the property might be accessed from anybody.
917 (let ((key (list bus interface property))
918 (val (list (list nil service path (cons access value)))))
919 (puthash key val dbus-registered-objects-table)
921 ;; Return the object.
922 (list key (list service path))))
924 (defun dbus-property-handler (&rest args)
925 "Default Handler for the \"org.freedesktop.DBus.Properties\" interface.
926 It will be registered for all objects created by `dbus-register-object'."
927 (let ((bus (dbus-event-bus-name last-input-event))
928 (path (dbus-event-path-name last-input-event))
929 (method (dbus-event-member-name last-input-event))
930 (interface (car args))
931 (property (cadr args)))
932 (cond
933 ;; "Get" returns a variant.
934 ((string-equal method "Get")
935 (let ((val (gethash (list bus interface property)
936 dbus-registered-objects-table)))
937 (when (string-equal path (nth 2 (car val)))
938 (list (list :variant (cdar (last (car val))))))))
940 ;; "Set" expects a variant.
941 ((string-equal method "Set")
942 (let ((val (gethash (list bus interface property)
943 dbus-registered-objects-table)))
944 (unless (consp (car (last (car val))))
945 (signal 'dbus-error
946 (list "Property not registered at path" property path)))
947 (unless (equal (caar (last (car val))) :readwrite)
948 (signal 'dbus-error
949 (list "Property not writable at path" property path)))
950 (puthash (list bus interface property)
951 (list (append (butlast (car val))
952 (list (cons :readwrite (caar (cddr args))))))
953 dbus-registered-objects-table)
954 :ignore))
956 ;; "GetAll" returns "a{sv}".
957 ((string-equal method "GetAll")
958 (let (result)
959 (maphash
960 (lambda (key val)
961 (when (and (equal (butlast key) (list bus interface))
962 (string-equal path (nth 2 (car val)))
963 (not (functionp (car (last (car val))))))
964 (add-to-list
965 'result
966 (list :dict-entry
967 (car (last key))
968 (list :variant (cdar (last (car val))))))))
969 dbus-registered-objects-table)
970 (list result))))))
973 ;; Initialize :system and :session buses. This adds their file
974 ;; descriptors to input_wait_mask, in order to detect incoming
975 ;; messages immediately.
976 (when (featurep 'dbusbind)
977 (dbus-ignore-errors
978 (dbus-init-bus :system)
979 (dbus-init-bus :session)))
981 (provide 'dbus)
983 ;; arch-tag: a47caf84-9162-4811-90cc-5d388e37b9bd
984 ;;; dbus.el ends here