Fix compilation warnings on MS-DOS due to constifying of pointers in xmenu.c.
[emacs.git] / lisp / net / dbus.el
blob05c7af2a8c37cb299c5ae4b44e614fed6ba96448
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/>.
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 (declare-function dbus-register-method "dbusbind.c")
42 (declare-function dbus-send-signal "dbusbind.c")
43 (defvar dbus-debug)
44 (defvar dbus-registered-objects-table)
46 ;; Pacify byte compiler.
47 (eval-when-compile
48 (require 'cl))
50 (require 'xml)
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))
96 `(condition-case err
97 (progn ,@body)
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
121 hash table."
122 (let (result)
123 (maphash
124 '(lambda (key value) (add-to-list 'result (cons key value) 'append))
125 dbus-registered-objects-table)
126 result))
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."
137 ;; Check parameter.
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))
143 (value (cdr object))
144 (entry (gethash key dbus-registered-objects-table))
145 ret)
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.
153 (dolist (elt entry)
154 (when (equal
155 (car value)
156 (butlast (cdr elt) (- (length (cdr elt)) (length (car value)))))
157 ;; Compute new hash value. If it is empty, remove it from the
158 ;; hash table.
159 (unless (puthash key (delete elt entry) dbus-registered-objects-table)
160 (remhash key dbus-registered-objects-table))
161 (setq ret t)))
162 ;; Check, whether there is still a registered function or property
163 ;; for the given service. If not, unregister the service from the
164 ;; bus.
165 (dolist (elt entry)
166 (let ((service (cadr elt))
167 (bus (car key))
168 found)
169 (maphash
170 (lambda (k v)
171 (dolist (e v)
172 (ignore-errors
173 (when (and (equal bus (car k)) (string-equal service (cadr e)))
174 (setq found t)))))
175 dbus-registered-objects-table)
176 (unless found
177 (dbus-call-method
178 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
179 "ReleaseName" service))))
180 ;; Return.
181 ret))
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."
198 (maphash
199 (lambda (key value)
200 (dolist (elt value)
201 (ignore-errors
202 (when (and (equal bus (car key)) (string-equal service (cadr elt)))
203 (unless
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)))
210 (case reply
211 (1 :released)
212 (2 :non-existent)
213 (3 :not-owner)
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)"
237 (let ((key
238 (apply
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.
248 (prog1
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)"
261 (save-match-data
262 ;; Check the arguments. We should silently ignore it when they
263 ;; are wrong.
264 (if (and (= (length args) 3)
265 (stringp (car args))
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))
273 (maphash
274 '(lambda (key value)
275 (dolist (elt value)
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
284 ;; BUS SERVICE PATH
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.
290 (when dbus-debug
291 (signal
292 'dbus-error
293 (cons
294 (format "Wrong arguments of %s.NameOwnerChanged" dbus-interface-dbus)
295 args))))))
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")
314 (let (result)
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
336 \"_\".
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)))
345 string)))
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
353 "_.."
354 (lambda (x) (format "%c" (string-to-number (substring x 1) 16)))
355 string)))
358 ;;; D-Bus events.
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
378 not well formed."
379 (when dbus-debug (message "DBus-Event %s" event))
380 (unless (and (listp event)
381 (eq (car event) 'dbus-event)
382 ;; Bus symbol.
383 (or (symbolp (nth 1 event))
384 (stringp (nth 1 event)))
385 ;; Type.
386 (and (natnump (nth 2 event))
387 (< dbus-message-type-invalid (nth 2 event)))
388 ;; Serial.
389 (natnump (nth 3 event))
390 ;; Service.
391 (or (= dbus-message-type-method-return (nth 2 event))
392 (= dbus-message-type-error (nth 2 event))
393 (stringp (nth 4 event)))
394 ;; Object path.
395 (or (= dbus-message-type-method-return (nth 2 event))
396 (= dbus-message-type-error (nth 2 event))
397 (stringp (nth 5 event)))
398 ;; Interface.
399 (or (= dbus-message-type-method-return (nth 2 event))
400 (= dbus-message-type-error (nth 2 event))
401 (stringp (nth 6 event)))
402 ;; Member.
403 (or (= dbus-message-type-method-return (nth 2 event))
404 (= dbus-message-type-error (nth 2 event))
405 (stringp (nth 7 event)))
406 ;; Handler.
407 (functionp (nth 8 event)))
408 (signal 'dbus-error (list "Not a valid D-Bus event" event))))
410 ;;;###autoload
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."
416 (interactive "e")
417 (condition-case err
418 (let (result)
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))
428 (dbus-ignore-errors
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)))))))
435 ;; Error handling.
436 (dbus-error
437 ;; Return an error message when it is a message call.
438 (when (= dbus-message-type-method-call (nth 2 event))
439 (dbus-ignore-errors
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)
454 (nth 1 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)
462 (nth 2 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)
471 (nth 3 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
477 not well formed."
478 (dbus-check-event event)
479 (nth 4 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
485 not well formed."
486 (dbus-check-event event)
487 (nth 5 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
493 not well formed."
494 (dbus-check-event event)
495 (nth 6 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
502 well formed."
503 (dbus-check-event event)
504 (nth 7 event))
507 ;;; D-Bus registered names.
509 (defun dbus-list-activatable-names (&optional bus)
510 "Return the D-Bus service names which can be activated as list.
511 If BUS is left nil, `:system' is assumed. The result is a list
512 of strings, which is `nil' when there are no activatable service
513 names at all."
514 (dbus-ignore-errors
515 (dbus-call-method
516 (or bus :system) dbus-service-dbus
517 dbus-path-dbus dbus-interface-dbus "ListActivatableNames")))
519 (defun dbus-list-names (bus)
520 "Return the service names registered at D-Bus BUS.
521 The result is a list of strings, which is `nil' when there are no
522 registered service names at all. Well known names are strings
523 like \"org.freedesktop.DBus\". Names starting with \":\" are
524 unique names for services."
525 (dbus-ignore-errors
526 (dbus-call-method
527 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus "ListNames")))
529 (defun dbus-list-known-names (bus)
530 "Retrieve all services which correspond to a known name in BUS.
531 A service has a known name if it doesn't start with \":\"."
532 (let (result)
533 (dolist (name (dbus-list-names bus) result)
534 (unless (string-equal ":" (substring name 0 1))
535 (add-to-list 'result name 'append)))))
537 (defun dbus-list-queued-owners (bus service)
538 "Return the unique names registered at D-Bus BUS and queued for SERVICE.
539 The result is a list of strings, or `nil' when there are no
540 queued name owners service names at all."
541 (dbus-ignore-errors
542 (dbus-call-method
543 bus dbus-service-dbus dbus-path-dbus
544 dbus-interface-dbus "ListQueuedOwners" service)))
546 (defun dbus-get-name-owner (bus service)
547 "Return the name owner of SERVICE registered at D-Bus BUS.
548 The result is either a string, or `nil' if there is no name owner."
549 (dbus-ignore-errors
550 (dbus-call-method
551 bus dbus-service-dbus dbus-path-dbus
552 dbus-interface-dbus "GetNameOwner" service)))
554 (defun dbus-ping (bus service &optional timeout)
555 "Check whether SERVICE is registered for D-Bus BUS.
556 TIMEOUT, a nonnegative integer, specifies the maximum number of
557 milliseconds `dbus-ping' must return. The default value is 25,000.
559 Note, that this autoloads SERVICE if it is not running yet. If
560 it shall be checked whether SERVICE is already running, one shall
561 apply
563 \(member service \(dbus-list-known-names bus))"
564 ;; "Ping" raises a D-Bus error if SERVICE does not exist.
565 ;; Otherwise, it returns silently with `nil'.
566 (condition-case nil
567 (not
568 (if (natnump timeout)
569 (dbus-call-method
570 bus service dbus-path-dbus dbus-interface-peer
571 "Ping" :timeout timeout)
572 (dbus-call-method
573 bus service dbus-path-dbus dbus-interface-peer "Ping")))
574 (dbus-error nil)))
577 ;;; D-Bus introspection.
579 (defun dbus-introspect (bus service path)
580 "Return all interfaces and sub-nodes of SERVICE,
581 registered at object path PATH at bus BUS.
583 BUS is either a Lisp symbol, `:system' or `:session', or a string
584 denoting the bus address. SERVICE must be a known service name,
585 and PATH must be a valid object path. The last two parameters
586 are strings. The result, the introspection data, is a string in
587 XML format."
588 ;; We don't want to raise errors. `dbus-call-method-non-blocking'
589 ;; is used, because the handler can be registered in our Emacs
590 ;; instance; caller an callee would block each other.
591 (dbus-ignore-errors
592 (funcall
593 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
594 bus service path dbus-interface-introspectable "Introspect")))
596 (defun dbus-introspect-xml (bus service path)
597 "Return the introspection data of SERVICE in D-Bus BUS at object path PATH.
598 The data are a parsed list. The root object is a \"node\",
599 representing the object path PATH. The root object can contain
600 \"interface\" and further \"node\" objects."
601 ;; We don't want to raise errors.
602 (xml-node-name
603 (ignore-errors
604 (with-temp-buffer
605 (insert (dbus-introspect bus service path))
606 (xml-parse-region (point-min) (point-max))))))
608 (defun dbus-introspect-get-attribute (object attribute)
609 "Return the ATTRIBUTE value of D-Bus introspection OBJECT.
610 ATTRIBUTE must be a string according to the attribute names in
611 the D-Bus specification."
612 (xml-get-attribute-or-nil object (intern attribute)))
614 (defun dbus-introspect-get-node-names (bus service path)
615 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
616 It returns a list of strings. The node names stand for further
617 object paths of the D-Bus service."
618 (let ((object (dbus-introspect-xml bus service path))
619 result)
620 (dolist (elt (xml-get-children object 'node) result)
621 (add-to-list
622 'result (dbus-introspect-get-attribute elt "name") 'append))))
624 (defun dbus-introspect-get-all-nodes (bus service path)
625 "Return all node names of SERVICE in D-Bus BUS at object path PATH.
626 It returns a list of strings, which are further object paths of SERVICE."
627 (let ((result (list path)))
628 (dolist (elt
629 (dbus-introspect-get-node-names bus service path)
630 result)
631 (setq elt (expand-file-name elt path))
632 (setq result
633 (append result (dbus-introspect-get-all-nodes bus service elt))))))
635 (defun dbus-introspect-get-interface-names (bus service path)
636 "Return all interface names of SERVICE in D-Bus BUS at object path PATH.
637 It returns a list of strings.
639 There will be always the default interface
640 \"org.freedesktop.DBus.Introspectable\". Another default
641 interface is \"org.freedesktop.DBus.Properties\". If present,
642 \"interface\" objects can also have \"property\" objects as
643 children, beside \"method\" and \"signal\" objects."
644 (let ((object (dbus-introspect-xml bus service path))
645 result)
646 (dolist (elt (xml-get-children object 'interface) result)
647 (add-to-list
648 'result (dbus-introspect-get-attribute elt "name") 'append))))
650 (defun dbus-introspect-get-interface (bus service path interface)
651 "Return the INTERFACE of SERVICE in D-Bus BUS at object path PATH.
652 The return value is an XML object. INTERFACE must be a string,
653 element of the list returned by `dbus-introspect-get-interface-names'.
654 The resulting \"interface\" object can contain \"method\", \"signal\",
655 \"property\" and \"annotation\" children."
656 (let ((elt (xml-get-children
657 (dbus-introspect-xml bus service path) 'interface)))
658 (while (and elt
659 (not (string-equal
660 interface
661 (dbus-introspect-get-attribute (car elt) "name"))))
662 (setq elt (cdr elt)))
663 (car elt)))
665 (defun dbus-introspect-get-method-names (bus service path interface)
666 "Return a list of strings of all method names of INTERFACE.
667 SERVICE is a service of D-Bus BUS at object path PATH."
668 (let ((object (dbus-introspect-get-interface bus service path interface))
669 result)
670 (dolist (elt (xml-get-children object 'method) result)
671 (add-to-list
672 'result (dbus-introspect-get-attribute elt "name") 'append))))
674 (defun dbus-introspect-get-method (bus service path interface method)
675 "Return method METHOD of interface INTERFACE as XML object.
676 It must be located at SERVICE in D-Bus BUS at object path PATH.
677 METHOD must be a string, element of the list returned by
678 `dbus-introspect-get-method-names'. The resulting \"method\"
679 object can contain \"arg\" and \"annotation\" children."
680 (let ((elt (xml-get-children
681 (dbus-introspect-get-interface bus service path interface)
682 'method)))
683 (while (and elt
684 (not (string-equal
685 method (dbus-introspect-get-attribute (car elt) "name"))))
686 (setq elt (cdr elt)))
687 (car elt)))
689 (defun dbus-introspect-get-signal-names (bus service path interface)
690 "Return a list of strings of all signal names of INTERFACE.
691 SERVICE is a service of D-Bus BUS at object path PATH."
692 (let ((object (dbus-introspect-get-interface bus service path interface))
693 result)
694 (dolist (elt (xml-get-children object 'signal) result)
695 (add-to-list
696 'result (dbus-introspect-get-attribute elt "name") 'append))))
698 (defun dbus-introspect-get-signal (bus service path interface signal)
699 "Return signal SIGNAL of interface INTERFACE as XML object.
700 It must be located at SERVICE in D-Bus BUS at object path PATH.
701 SIGNAL must be a string, element of the list returned by
702 `dbus-introspect-get-signal-names'. The resulting \"signal\"
703 object can contain \"arg\" and \"annotation\" children."
704 (let ((elt (xml-get-children
705 (dbus-introspect-get-interface bus service path interface)
706 'signal)))
707 (while (and elt
708 (not (string-equal
709 signal (dbus-introspect-get-attribute (car elt) "name"))))
710 (setq elt (cdr elt)))
711 (car elt)))
713 (defun dbus-introspect-get-property-names (bus service path interface)
714 "Return a list of strings of all property names of INTERFACE.
715 SERVICE is a service of D-Bus BUS at object path PATH."
716 (let ((object (dbus-introspect-get-interface bus service path interface))
717 result)
718 (dolist (elt (xml-get-children object 'property) result)
719 (add-to-list
720 'result (dbus-introspect-get-attribute elt "name") 'append))))
722 (defun dbus-introspect-get-property (bus service path interface property)
723 "This function returns PROPERTY of INTERFACE as XML object.
724 It must be located at SERVICE in D-Bus BUS at object path PATH.
725 PROPERTY must be a string, element of the list returned by
726 `dbus-introspect-get-property-names'. The resulting PROPERTY
727 object can contain \"annotation\" children."
728 (let ((elt (xml-get-children
729 (dbus-introspect-get-interface bus service path interface)
730 'property)))
731 (while (and elt
732 (not (string-equal
733 property
734 (dbus-introspect-get-attribute (car elt) "name"))))
735 (setq elt (cdr elt)))
736 (car elt)))
738 (defun dbus-introspect-get-annotation-names
739 (bus service path interface &optional name)
740 "Return all annotation names as list of strings.
741 If NAME is `nil', the annotations are children of INTERFACE,
742 otherwise NAME must be a \"method\", \"signal\", or \"property\"
743 object, where the annotations belong to."
744 (let ((object
745 (if name
746 (or (dbus-introspect-get-method bus service path interface name)
747 (dbus-introspect-get-signal bus service path interface name)
748 (dbus-introspect-get-property bus service path interface name))
749 (dbus-introspect-get-interface bus service path interface)))
750 result)
751 (dolist (elt (xml-get-children object 'annotation) result)
752 (add-to-list
753 'result (dbus-introspect-get-attribute elt "name") 'append))))
755 (defun dbus-introspect-get-annotation
756 (bus service path interface name annotation)
757 "Return ANNOTATION as XML object.
758 If NAME is `nil', ANNOTATION is a child of INTERFACE, otherwise
759 NAME must be the name of a \"method\", \"signal\", or
760 \"property\" object, where the ANNOTATION belongs to."
761 (let ((elt (xml-get-children
762 (if name
763 (or (dbus-introspect-get-method
764 bus service path interface name)
765 (dbus-introspect-get-signal
766 bus service path interface name)
767 (dbus-introspect-get-property
768 bus service path interface name))
769 (dbus-introspect-get-interface bus service path interface))
770 'annotation)))
771 (while (and elt
772 (not (string-equal
773 annotation
774 (dbus-introspect-get-attribute (car elt) "name"))))
775 (setq elt (cdr elt)))
776 (car elt)))
778 (defun dbus-introspect-get-argument-names (bus service path interface name)
779 "Return a list of all argument names as list of strings.
780 NAME must be a \"method\" or \"signal\" object.
782 Argument names are optional, the function can return `nil'
783 therefore, even if the method or signal has arguments."
784 (let ((object
785 (or (dbus-introspect-get-method bus service path interface name)
786 (dbus-introspect-get-signal bus service path interface name)))
787 result)
788 (dolist (elt (xml-get-children object 'arg) result)
789 (add-to-list
790 'result (dbus-introspect-get-attribute elt "name") 'append))))
792 (defun dbus-introspect-get-argument (bus service path interface name arg)
793 "Return argument ARG as XML object.
794 NAME must be a \"method\" or \"signal\" object. ARG must be a string,
795 element of the list returned by `dbus-introspect-get-argument-names'."
796 (let ((elt (xml-get-children
797 (or (dbus-introspect-get-method bus service path interface name)
798 (dbus-introspect-get-signal bus service path interface name))
799 'arg)))
800 (while (and elt
801 (not (string-equal
802 arg (dbus-introspect-get-attribute (car elt) "name"))))
803 (setq elt (cdr elt)))
804 (car elt)))
806 (defun dbus-introspect-get-signature
807 (bus service path interface name &optional direction)
808 "Return signature of a `method' or `signal', represented by NAME, as string.
809 If NAME is a `method', DIRECTION can be either \"in\" or \"out\".
810 If DIRECTION is `nil', \"in\" is assumed.
812 If NAME is a `signal', and DIRECTION is non-`nil', DIRECTION must
813 be \"out\"."
814 ;; For methods, we use "in" as default direction.
815 (let ((object (or (dbus-introspect-get-method
816 bus service path interface name)
817 (dbus-introspect-get-signal
818 bus service path interface name))))
819 (when (and (string-equal
820 "method" (dbus-introspect-get-attribute object "name"))
821 (not (stringp direction)))
822 (setq direction "in"))
823 ;; In signals, no direction is given.
824 (when (string-equal "signal" (dbus-introspect-get-attribute object "name"))
825 (setq direction nil))
826 ;; Collect the signatures.
827 (mapconcat
828 '(lambda (x)
829 (let ((arg (dbus-introspect-get-argument
830 bus service path interface name x)))
831 (if (or (not (stringp direction))
832 (string-equal
833 direction
834 (dbus-introspect-get-attribute arg "direction")))
835 (dbus-introspect-get-attribute arg "type")
836 "")))
837 (dbus-introspect-get-argument-names bus service path interface name)
838 "")))
841 ;;; D-Bus properties.
843 (defun dbus-get-property (bus service path interface property)
844 "Return the value of PROPERTY of INTERFACE.
845 It will be checked at BUS, SERVICE, PATH. The result can be any
846 valid D-Bus value, or `nil' if there is no PROPERTY."
847 (dbus-ignore-errors
848 ;; "Get" returns a variant, so we must use the `car'.
849 (car
850 (funcall
851 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
852 bus service path dbus-interface-properties
853 "Get" :timeout 500 interface property))))
855 (defun dbus-set-property (bus service path interface property value)
856 "Set value of PROPERTY of INTERFACE to VALUE.
857 It will be checked at BUS, SERVICE, PATH. When the value has
858 been set successful, the result is VALUE. Otherwise, `nil' is
859 returned."
860 (dbus-ignore-errors
861 ;; "Set" requires a variant.
862 (funcall
863 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
864 bus service path dbus-interface-properties
865 "Set" :timeout 500 interface property (list :variant value))
866 ;; Return VALUE.
867 (dbus-get-property bus service path interface property)))
869 (defun dbus-get-all-properties (bus service path interface)
870 "Return all properties of INTERFACE at BUS, SERVICE, PATH.
871 The result is a list of entries. Every entry is a cons of the
872 name of the property, and its value. If there are no properties,
873 `nil' is returned."
874 (dbus-ignore-errors
875 ;; "GetAll" returns "a{sv}".
876 (let (result)
877 (dolist (dict
878 (funcall
879 (if noninteractive
880 'dbus-call-method
881 'dbus-call-method-non-blocking)
882 bus service path dbus-interface-properties
883 "GetAll" :timeout 500 interface)
884 result)
885 (add-to-list 'result (cons (car dict) (caadr dict)) 'append)))))
887 (defun dbus-register-property
888 (bus service path interface property access value
889 &optional emits-signal dont-register-service)
890 "Register property PROPERTY on the D-Bus BUS.
892 BUS is either a Lisp symbol, `:system' or `:session', or a string
893 denoting the bus address.
895 SERVICE is the D-Bus service name of the D-Bus. It must be a
896 known name (See discussion of DONT-REGISTER-SERVICE below).
898 PATH is the D-Bus object path SERVICE is registered (See
899 discussion of DONT-REGISTER-SERVICE below). INTERFACE is the
900 name of the interface used at PATH, PROPERTY is the name of the
901 property of INTERFACE. ACCESS indicates, whether the property
902 can be changed by other services via D-Bus. It must be either
903 the symbol `:read' or `:readwrite'. VALUE is the initial value
904 of the property, it can be of any valid type (see
905 `dbus-call-method' for details).
907 If PROPERTY already exists on PATH, it will be overwritten. For
908 properties with access type `:read' this is the only way to
909 change their values. Properties with access type `:readwrite'
910 can be changed by `dbus-set-property'.
912 The interface \"org.freedesktop.DBus.Properties\" is added to
913 PATH, including a default handler for the \"Get\", \"GetAll\" and
914 \"Set\" methods of this interface. When EMITS-SIGNAL is non-nil,
915 the signal \"PropertiesChanged\" is sent when the property is
916 changed by `dbus-set-property'.
918 When DONT-REGISTER-SERVICE is non-nil, the known name SERVICE is
919 not registered. This means that other D-Bus clients have no way
920 of noticing the newly registered property. When interfaces are
921 constructed incrementally by adding single methods or properties
922 at a time, DONT-REGISTER-SERVICE can be used to prevent other
923 clients from discovering the still incomplete interface."
924 (unless (member access '(:read :readwrite))
925 (signal 'dbus-error (list "Access type invalid" access)))
927 ;; Register SERVICE.
928 (unless (or dont-register-service
929 (member service (dbus-list-names bus)))
930 (dbus-call-method
931 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
932 "RequestName" service 0))
934 ;; Add handlers for the three property-related methods.
935 (dbus-register-method
936 bus service path dbus-interface-properties "Get"
937 'dbus-property-handler 'dont-register)
938 (dbus-register-method
939 bus service path dbus-interface-properties "GetAll"
940 'dbus-property-handler 'dont-register)
941 (dbus-register-method
942 bus service path dbus-interface-properties "Set"
943 'dbus-property-handler 'dont-register)
945 ;; Register the name SERVICE with BUS.
946 (unless dont-register-service
947 (dbus-register-service bus service))
949 ;; Send the PropertiesChanged signal.
950 (when emits-signal
951 (dbus-send-signal
952 bus service path dbus-interface-properties "PropertiesChanged"
953 (list (list :dict-entry property (list :variant value)))
954 '(:array)))
956 ;; Create a hash table entry. We use nil for the unique name,
957 ;; because the property might be accessed from anybody.
958 (let ((key (list bus interface property))
959 (val
960 (list
961 (list
962 nil service path
963 (cons
964 (if emits-signal (list access :emits-signal) (list access))
965 value)))))
966 (puthash key val dbus-registered-objects-table)
968 ;; Return the object.
969 (list key (list service path))))
971 (defun dbus-property-handler (&rest args)
972 "Default handler for the \"org.freedesktop.DBus.Properties\" interface.
973 It will be registered for all objects created by `dbus-register-object'."
974 (let ((bus (dbus-event-bus-name last-input-event))
975 (service (dbus-event-service-name last-input-event))
976 (path (dbus-event-path-name last-input-event))
977 (method (dbus-event-member-name last-input-event))
978 (interface (car args))
979 (property (cadr args)))
980 (cond
981 ;; "Get" returns a variant.
982 ((string-equal method "Get")
983 (let ((entry (gethash (list bus interface property)
984 dbus-registered-objects-table)))
985 (when (string-equal path (nth 2 (car entry)))
986 (list (list :variant (cdar (last (car entry))))))))
988 ;; "Set" expects a variant.
989 ((string-equal method "Set")
990 (let* ((value (caar (cddr args)))
991 (entry (gethash (list bus interface property)
992 dbus-registered-objects-table))
993 ;; The value of the hash table is a list; in case of
994 ;; properties it contains just one element (UNAME SERVICE
995 ;; PATH OBJECT). OBJECT is a cons cell of a list, which
996 ;; contains a list of annotations (like :read,
997 ;; :read-write, :emits-signal), and the value of the
998 ;; property.
999 (object (car (last (car entry)))))
1000 (unless (consp object)
1001 (signal 'dbus-error
1002 (list "Property not registered at path" property path)))
1003 (unless (member :readwrite (car object))
1004 (signal 'dbus-error
1005 (list "Property not writable at path" property path)))
1006 (puthash (list bus interface property)
1007 (list (append (butlast (car entry))
1008 (list (cons (car object) value))))
1009 dbus-registered-objects-table)
1010 ;; Send the "PropertiesChanged" signal.
1011 (when (member :emits-signal (car object))
1012 (dbus-send-signal
1013 bus service path dbus-interface-properties "PropertiesChanged"
1014 (list (list :dict-entry property (list :variant value)))
1015 '(:array)))
1016 ;; Return empty reply.
1017 :ignore))
1019 ;; "GetAll" returns "a{sv}".
1020 ((string-equal method "GetAll")
1021 (let (result)
1022 (maphash
1023 (lambda (key val)
1024 (when (and (equal (butlast key) (list bus interface))
1025 (string-equal path (nth 2 (car val)))
1026 (not (functionp (car (last (car val))))))
1027 (add-to-list
1028 'result
1029 (list :dict-entry
1030 (car (last key))
1031 (list :variant (cdar (last (car val))))))))
1032 dbus-registered-objects-table)
1033 (list result))))))
1036 ;; Initialize :system and :session buses. This adds their file
1037 ;; descriptors to input_wait_mask, in order to detect incoming
1038 ;; messages immediately.
1039 (when (featurep 'dbusbind)
1040 (dbus-ignore-errors
1041 (dbus-init-bus :system)
1042 (dbus-init-bus :session)))
1044 (provide 'dbus)
1046 ;;; dbus.el ends here