* term/common-win.el (x-colors): Add "dark green" and "dark turquoise" (Bug#6332).
[bpt/emacs.git] / lisp / net / dbus.el
CommitLineData
3a993e3d
MA
1;;; dbus.el --- Elisp bindings for D-Bus.
2
114f9c96 3;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3a993e3d
MA
4
5;; Author: Michael Albinus <michael.albinus@gmx.de>
6;; Keywords: comm, hardware
7
8;; This file is part of GNU Emacs.
9
874a927a 10;; GNU Emacs is free software: you can redistribute it and/or modify
3a993e3d 11;; it under the terms of the GNU General Public License as published by
874a927a
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
3a993e3d
MA
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
874a927a 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
3a993e3d
MA
22
23;;; Commentary:
24
25;; This package provides language bindings for the D-Bus API. D-Bus
26;; is a message bus system, a simple way for applications to talk to
27;; one another. See <http://dbus.freedesktop.org/> for details.
28
29;; Low-level language bindings are implemented in src/dbusbind.c.
30
31;;; Code:
32
7bb7efbd 33;; D-Bus support in the Emacs core can be disabled with configuration
6981d00a
MA
34;; option "--without-dbus". Declare used subroutines and variables.
35(declare-function dbus-call-method "dbusbind.c")
52a39a64 36(declare-function dbus-call-method-asynchronously "dbusbind.c")
ba6f7d86 37(declare-function dbus-init-bus "dbusbind.c")
5e895c06
MA
38(declare-function dbus-method-return-internal "dbusbind.c")
39(declare-function dbus-method-error-internal "dbusbind.c")
6981d00a
MA
40(declare-function dbus-register-signal "dbusbind.c")
41(defvar dbus-debug)
b172ed20 42(defvar dbus-registered-objects-table)
6981d00a
MA
43
44;; Pacify byte compiler.
45(eval-when-compile
46 (require 'cl))
7bb7efbd 47
3a993e3d
MA
48(require 'xml)
49
50(defconst dbus-service-dbus "org.freedesktop.DBus"
51 "The bus name used to talk to the bus itself.")
52
53(defconst dbus-path-dbus "/org/freedesktop/DBus"
54 "The object path used to talk to the bus itself.")
55
56(defconst dbus-interface-dbus "org.freedesktop.DBus"
57 "The interface exported by the object with `dbus-service-dbus' and `dbus-path-dbus'.")
58
4ba11bcb
MA
59(defconst dbus-interface-peer (concat dbus-interface-dbus ".Peer")
60 "The interface for peer objects.")
61
62(defconst dbus-interface-introspectable
63 (concat dbus-interface-dbus ".Introspectable")
3a993e3d
MA
64 "The interface supported by introspectable objects.")
65
f636d3ca
MA
66(defconst dbus-interface-properties (concat dbus-interface-dbus ".Properties")
67 "The interface for property objects.")
68
65b7cb2c
MA
69(defconst dbus-service-emacs "org.gnu.Emacs"
70 "The well known service name of Emacs.")
71
72(defconst dbus-path-emacs "/org/gnu/Emacs"
73 "The object path head used by Emacs.")
74
98c38bfc
MA
75(defconst dbus-message-type-invalid 0
76 "This value is never a valid message type.")
77
78(defconst dbus-message-type-method-call 1
79 "Message type of a method call message.")
80
81(defconst dbus-message-type-method-return 2
82 "Message type of a method return message.")
83
84(defconst dbus-message-type-error 3
85 "Message type of an error reply message.")
86
87(defconst dbus-message-type-signal 4
88 "Message type of a signal message.")
89
246a286b
MA
90(defmacro dbus-ignore-errors (&rest body)
91 "Execute BODY; signal D-Bus error when `dbus-debug' is non-nil.
92Otherwise, 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))))))
96
97(put 'dbus-ignore-errors 'lisp-indent-function 0)
98c38bfc 98(put 'dbus-ignore-errors 'edebug-form-spec '(form body))
246a286b
MA
99(font-lock-add-keywords 'emacs-lisp-mode '("\\<dbus-ignore-errors\\>"))
100
e12c189f
MA
101(defvar dbus-event-error-hooks nil
102 "Functions to be called when a D-Bus error happens in the event handler.
f213fc09 103Every function must accept two arguments, the event and the error variable
e12c189f
MA
104catched in `condition-case' by `dbus-error'.")
105
5363d8ea
MA
106\f
107;;; Hash table of registered functions.
108
79945ac1
MA
109;; We create it here. So we have a simple test in dbusbind.c, whether
110;; the Lisp code has been loaded.
b172ed20 111(setq dbus-registered-objects-table (make-hash-table :test 'equal))
5363d8ea 112
98c38bfc
MA
113(defvar dbus-return-values-table (make-hash-table :test 'equal)
114 "Hash table for temporary storing arguments of reply messages.
115A key in this hash table is a list (BUS SERIAL). BUS is either the
116symbol `:system' or the symbol `:session'. SERIAL is the serial number
117of the reply message. See `dbus-call-method-non-blocking-handler' and
118`dbus-call-method-non-blocking'.")
119
ef6ce14c 120(defun dbus-list-hash-table ()
e49d337b 121 "Returns all registered member registrations to D-Bus.
ef6ce14c 122The return value is a list, with elements of kind (KEY . VALUE).
b172ed20 123See `dbus-registered-objects-table' for a description of the
ef6ce14c
MA
124hash table."
125 (let (result)
126 (maphash
127 '(lambda (key value) (add-to-list 'result (cons key value) 'append))
b172ed20 128 dbus-registered-objects-table)
ef6ce14c
MA
129 result))
130
246a286b
MA
131(defun dbus-unregister-object (object)
132 "Unregister OBJECT from D-Bus.
b172ed20
MA
133OBJECT must be the result of a preceding `dbus-register-method',
134`dbus-register-property' or `dbus-register-signal' call. It
135returns `t' if OBJECT has been unregistered, `nil' otherwise.
136
137When OBJECT identifies the last method or property, which is
138registered for the respective service, Emacs releases its
139association to the service from D-Bus."
246a286b
MA
140 ;; Check parameter.
141 (unless (and (consp object) (not (null (car object))) (consp (cdr object)))
142 (signal 'wrong-type-argument (list 'D-Bus object)))
143
144 ;; Find the corresponding entry in the hash table.
145 (let* ((key (car object))
b172ed20
MA
146 (value (cdr object))
147 (entry (gethash key dbus-registered-objects-table))
8fb1629f 148 ret)
b172ed20
MA
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.
154
246a286b 155 ;; Loop over the registered functions.
b172ed20
MA
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
246a286b 161 ;; hash table.
b172ed20
MA
162 (unless (puthash key (delete elt entry) dbus-registered-objects-table)
163 (remhash key dbus-registered-objects-table))
8fb1629f 164 (setq ret t)))
b172ed20
MA
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))
8fb1629f
MA
171 found)
172 (maphash
173 (lambda (k v)
b172ed20 174 (dolist (e v)
8fb1629f 175 (ignore-errors
b172ed20 176 (when (and (equal bus (car k)) (string-equal service (cadr e)))
3f324173 177 (setq found t)))))
b172ed20 178 dbus-registered-objects-table)
8fb1629f
MA
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))
246a286b 185
c0a39702
MA
186(defun dbus-unregister-service (bus service)
187 "Unregister all objects related to SERVICE from D-Bus BUS.
188BUS must be either the symbol `:system' or the symbol `:session'.
189SERVICE 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))
202
98c38bfc
MA
203(defun dbus-call-method-non-blocking-handler (&rest args)
204 "Handler for reply messages of asynchronous D-Bus message calls.
b172ed20 205It calls the function stored in `dbus-registered-objects-table'.
98c38bfc
MA
206The 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))
211
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.
215This is necessary for communicating to registered D-Bus methods,
216which are running in the same Emacs process.
217
218The arguments are the same as in `dbus-call-method'.
219
220usage: (dbus-call-method-non-blocking
221 BUS SERVICE PATH INTERFACE METHOD
222 &optional :timeout TIMEOUT &rest ARGS)"
223
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'.
3dec5c36 231 (while (eq (gethash key dbus-return-values-table :ignore) :ignore)
98c38bfc
MA
232 (read-event nil nil 0.1))
233
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))))
238
0e0c4247 239(defun dbus-name-owner-changed-handler (&rest args)
e49d337b 240 "Reapplies all member registrations to D-Bus.
ef6ce14c
MA
241This handler is applied when a \"NameOwnerChanged\" signal has
242arrived. SERVICE is the object name for which the name owner has
243been changed. OLD-OWNER is the previous owner of SERVICE, or the
244empty string if SERVICE was not owned yet. NEW-OWNER is the new
0e0c4247
MA
245owner of SERVICE, or the empty string if SERVICE looses any name owner.
246
247usage: (dbus-name-owner-changed-handler service old-owner new-owner)"
ef6ce14c 248 (save-match-data
0e0c4247
MA
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)
e49d337b 263 ;; key has the structure (BUS INTERFACE MEMBER).
0e0c4247
MA
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.
7d1112ae 267 (dbus-unregister-object (list key (cdr elt)))
0e0c4247
MA
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)
e49d337b 273 ;; INTERFACE MEMBER HANDLER
0e0c4247 274 (nth 1 key) (nth 2 key) (nth 3 elt)))))
b172ed20 275 (copy-hash-table dbus-registered-objects-table))))
0e0c4247
MA
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))))))
ef6ce14c
MA
283
284;; Register the handler.
98c38bfc 285(when nil ;ignore-errors
246a286b
MA
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))
ef6ce14c 292
5363d8ea 293\f
82697a45
MA
294;;; D-Bus type conversion.
295
296(defun dbus-string-to-byte-array (string)
297 "Transforms STRING to list (:array :byte c1 :byte c2 ...).
298STRING shall be UTF8 coded."
d665fff0
MA
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)))))))
82697a45
MA
304
305(defun dbus-byte-array-to-string (byte-array)
306 "Transforms BYTE-ARRAY into UTF8 coded string.
307BYTE-ARRAY must be a list of structure (c1 c2 ...)."
308 (apply 'string byte-array))
309
310(defun dbus-escape-as-identifier (string)
311 "Escape an arbitrary STRING so it follows the rules for a C identifier.
312The escaped string can be used as object path component, interface element
313component, bus name component or member name in D-Bus.
314
315The escaping consists of replacing all non-alphanumerics, and the
316first character if it's a digit, with an underscore and two
317lower-case hex digits:
318
319 \"0123abc_xyz\\x01\\xff\" -> \"_30123abc_5fxyz_01_ff\"
320
321i.e. similar to URI encoding, but with \"_\" taking the role of \"%\",
322and a smaller allowed set. As a special case, \"\" is escaped to
323\"_\".
324
325Returns the escaped string. Algorithm taken from
326telepathy-glib's `tp-escape-as-identifier'."
327 (if (zerop (length string))
328 "_"
329 (replace-regexp-in-string
330 "^[0-9]\\|[^A-Za-z0-9]"
331 (lambda (x) (format "_%2x" (aref x 0)))
332 string)))
333
334(defun dbus-unescape-from-identifier (string)
335 "Retrieve the original string from the encoded STRING.
336STRING must have been coded with `dbus-escape-as-identifier'"
337 (if (string-equal string "_")
338 ""
339 (replace-regexp-in-string
340 "_.."
341 (lambda (x) (format "%c" (string-to-number (substring x 1) 16)))
342 string)))
343
344\f
5363d8ea
MA
345;;; D-Bus events.
346
3a993e3d
MA
347(defun dbus-check-event (event)
348 "Checks whether EVENT is a well formed D-Bus event.
349EVENT is a list which starts with symbol `dbus-event':
350
98c38bfc 351 (dbus-event BUS TYPE SERIAL SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS)
3a993e3d 352
e49d337b 353BUS identifies the D-Bus the message is coming from. It is
98c38bfc
MA
354either the symbol `:system' or the symbol `:session'. TYPE is
355the D-Bus message type which has caused the event, SERIAL is the
356serial number of the received D-Bus message. SERVICE and PATH
357are the unique name and the object path of the D-Bus object
358emitting the message. INTERFACE and MEMBER denote the message
359which has been sent. HANDLER is the function which has been
360registered for this message. ARGS are the arguments passed to
361HANDLER, when it is called during event handling in
362`dbus-handle-event'.
3a993e3d
MA
363
364This function raises a `dbus-error' signal in case the event is
365not well formed."
366 (when dbus-debug (message "DBus-Event %s" event))
367 (unless (and (listp event)
368 (eq (car event) 'dbus-event)
5363d8ea 369 ;; Bus symbol.
ef6ce14c 370 (symbolp (nth 1 event))
98c38bfc
MA
371 ;; Type.
372 (and (natnump (nth 2 event))
373 (< dbus-message-type-invalid (nth 2 event)))
e49d337b 374 ;; Serial.
98c38bfc 375 (natnump (nth 3 event))
5363d8ea 376 ;; Service.
98c38bfc 377 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 378 (= dbus-message-type-error (nth 2 event))
98c38bfc 379 (stringp (nth 4 event)))
e49d337b 380 ;; Object path.
98c38bfc 381 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 382 (= dbus-message-type-error (nth 2 event))
98c38bfc 383 (stringp (nth 5 event)))
e49d337b 384 ;; Interface.
98c38bfc 385 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 386 (= dbus-message-type-error (nth 2 event))
98c38bfc 387 (stringp (nth 6 event)))
e49d337b 388 ;; Member.
98c38bfc 389 (or (= dbus-message-type-method-return (nth 2 event))
ba0b66b0 390 (= dbus-message-type-error (nth 2 event))
98c38bfc 391 (stringp (nth 7 event)))
ef6ce14c 392 ;; Handler.
98c38bfc 393 (functionp (nth 8 event)))
3a993e3d
MA
394 (signal 'dbus-error (list "Not a valid D-Bus event" event))))
395
396;;;###autoload
397(defun dbus-handle-event (event)
398 "Handle events from the D-Bus.
5363d8ea 399EVENT is a D-Bus event, see `dbus-check-event'. HANDLER, being
98c38bfc
MA
400part of the event, is called with arguments ARGS.
401If the HANDLER returns an `dbus-error', it is propagated as return message."
3a993e3d 402 (interactive "e")
98c38bfc
MA
403 (condition-case err
404 (let (result)
ba0b66b0 405 ;; We ignore not well-formed events.
98c38bfc 406 (dbus-check-event event)
ba0b66b0
MA
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.
98c38bfc
MA
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
3dec5c36
MA
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)))))))
98c38bfc
MA
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))))
ba0b66b0 428 ;; Propagate D-Bus error messages.
f213fc09 429 (run-hook-with-args 'dbus-event-error-hooks event err)
ba0b66b0
MA
430 (when (or dbus-debug (= dbus-message-type-error (nth 2 event)))
431 (signal (car err) (cdr err))))))
3a993e3d
MA
432
433(defun dbus-event-bus-name (event)
434 "Return the bus name the event is coming from.
435The result is either the symbol `:system' or the symbol `:session'.
436EVENT is a D-Bus event, see `dbus-check-event'. This function
437raises a `dbus-error' signal in case the event is not well
438formed."
439 (dbus-check-event event)
ef6ce14c 440 (nth 1 event))
3a993e3d 441
98c38bfc
MA
442(defun dbus-event-message-type (event)
443 "Return the message type of the corresponding D-Bus message.
444The result is a number. EVENT is a D-Bus event, see
445`dbus-check-event'. This function raises a `dbus-error' signal
446in case the event is not well formed."
447 (dbus-check-event event)
448 (nth 2 event))
449
e49d337b
MA
450(defun dbus-event-serial-number (event)
451 "Return the serial number of the corresponding D-Bus message.
98c38bfc
MA
452The result is a number. The serial number is needed for
453generating a reply message. EVENT is a D-Bus event, see
454`dbus-check-event'. This function raises a `dbus-error' signal
455in case the event is not well formed."
e49d337b 456 (dbus-check-event event)
98c38bfc 457 (nth 3 event))
e49d337b 458
3a993e3d 459(defun dbus-event-service-name (event)
5363d8ea 460 "Return the name of the D-Bus object the event is coming from.
3a993e3d
MA
461The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
462This function raises a `dbus-error' signal in case the event is
463not well formed."
464 (dbus-check-event event)
98c38bfc 465 (nth 4 event))
3a993e3d
MA
466
467(defun dbus-event-path-name (event)
468 "Return the object path of the D-Bus object the event is coming from.
469The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
470This function raises a `dbus-error' signal in case the event is
471not well formed."
472 (dbus-check-event event)
98c38bfc 473 (nth 5 event))
3a993e3d
MA
474
475(defun dbus-event-interface-name (event)
476 "Return the interface name of the D-Bus object the event is coming from.
477The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
478This function raises a `dbus-error' signal in case the event is
479not well formed."
480 (dbus-check-event event)
98c38bfc 481 (nth 6 event))
3a993e3d
MA
482
483(defun dbus-event-member-name (event)
484 "Return the member name the event is coming from.
485It is either a signal name or a method name. The result is is a
486string. EVENT is a D-Bus event, see `dbus-check-event'. This
487function raises a `dbus-error' signal in case the event is not
488well formed."
489 (dbus-check-event event)
98c38bfc 490 (nth 7 event))
5363d8ea
MA
491
492\f
493;;; D-Bus registered names.
3a993e3d
MA
494
495(defun dbus-list-activatable-names ()
496 "Return the D-Bus service names which can be activated as list.
f636d3ca 497The result is a list of strings, which is `nil' when there are no
3a993e3d 498activatable service names at all."
246a286b
MA
499 (dbus-ignore-errors
500 (dbus-call-method
501 :system dbus-service-dbus
502 dbus-path-dbus dbus-interface-dbus "ListActivatableNames")))
3a993e3d
MA
503
504(defun dbus-list-names (bus)
505 "Return the service names registered at D-Bus BUS.
f636d3ca
MA
506The result is a list of strings, which is `nil' when there are no
507registered service names at all. Well known names are strings
508like \"org.freedesktop.DBus\". Names starting with \":\" are
509unique names for services."
246a286b
MA
510 (dbus-ignore-errors
511 (dbus-call-method
512 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus "ListNames")))
3a993e3d
MA
513
514(defun dbus-list-known-names (bus)
515 "Retrieve all services which correspond to a known name in BUS.
516A 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)))))
521
522(defun dbus-list-queued-owners (bus service)
f636d3ca
MA
523 "Return the unique names registered at D-Bus BUS and queued for SERVICE.
524The result is a list of strings, or `nil' when there are no
525queued name owners service names at all."
246a286b
MA
526 (dbus-ignore-errors
527 (dbus-call-method
528 bus dbus-service-dbus dbus-path-dbus
529 dbus-interface-dbus "ListQueuedOwners" service)))
3a993e3d
MA
530
531(defun dbus-get-name-owner (bus service)
532 "Return the name owner of SERVICE registered at D-Bus BUS.
f636d3ca 533The result is either a string, or `nil' if there is no name owner."
246a286b
MA
534 (dbus-ignore-errors
535 (dbus-call-method
536 bus dbus-service-dbus dbus-path-dbus
537 dbus-interface-dbus "GetNameOwner" service)))
3a993e3d 538
93fb0645
MA
539(defun dbus-ping (bus service &optional timeout)
540 "Check whether SERVICE is registered for D-Bus BUS.
541TIMEOUT, a nonnegative integer, specifies the maximum number of
542milliseconds `dbus-ping' must return. The default value is 25,000.
543
544Note, that this autoloads SERVICE if it is not running yet. If
545it shall be checked whether SERVICE is already running, one shall
546apply
547
548 \(member service \(dbus-list-known-names bus))"
4ba11bcb
MA
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
93fb0645
MA
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")))
4ba11bcb
MA
559 (dbus-error nil)))
560
f636d3ca
MA
561\f
562;;; D-Bus introspection.
3a993e3d 563
f636d3ca
MA
564(defun dbus-introspect (bus service path)
565 "This function returns all interfaces and sub-nodes of SERVICE,
566registered at object path PATH at bus BUS.
567
568BUS must be either the symbol `:system' or the symbol `:session'.
569SERVICE must be a known service name, and PATH must be a valid
570object path. The last two parameters are strings. The result,
571the introspection data, is a string in XML format."
736215fd
MA
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.
246a286b 575 (dbus-ignore-errors
d4b06783
MA
576 (funcall
577 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
246a286b 578 bus service path dbus-interface-introspectable "Introspect")))
3a993e3d 579
f636d3ca
MA
580(defun dbus-introspect-xml (bus service path)
581 "Return the introspection data of SERVICE in D-Bus BUS at object path PATH.
582The data are a parsed list. The root object is a \"node\",
583representing 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))))))
591
592(defun dbus-introspect-get-attribute (object attribute)
593 "Return the ATTRIBUTE value of D-Bus introspection OBJECT.
594ATTRIBUTE must be a string according to the attribute names in
595the D-Bus specification."
596 (xml-get-attribute-or-nil object (intern attribute)))
597
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.
600It returns a list of strings. The node names stand for further
601object 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))))
607
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.
610It 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))))))
618
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.
621It returns a list of strings.
622
623There will be always the default interface
624\"org.freedesktop.DBus.Introspectable\". Another default
625interface is \"org.freedesktop.DBus.Properties\". If present,
626\"interface\" objects can also have \"property\" objects as
627children, 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))))
633
634(defun dbus-introspect-get-interface (bus service path interface)
635 "Return the INTERFACE of SERVICE in D-Bus BUS at object path PATH.
636The return value is an XML object. INTERFACE must be a string,
637element 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)))
649
650(defun dbus-introspect-get-method-names (bus service path interface)
651 "Return a list of strings of all method names of INTERFACE.
652SERVICE 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))))
658
659(defun dbus-introspect-get-method (bus service path interface method)
660 "Return method METHOD of interface INTERFACE as XML object.
661It must be located at SERVICE in D-Bus BUS at object path PATH.
662METHOD must be a string, element of the list returned by
663`dbus-introspect-get-method-names'. The resulting \"method\"
664object 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)))
673
674(defun dbus-introspect-get-signal-names (bus service path interface)
675 "Return a list of strings of all signal names of INTERFACE.
676SERVICE 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))))
682
683(defun dbus-introspect-get-signal (bus service path interface signal)
684 "Return signal SIGNAL of interface INTERFACE as XML object.
685It must be located at SERVICE in D-Bus BUS at object path PATH.
686SIGNAL must be a string, element of the list returned by
687`dbus-introspect-get-signal-names'. The resulting \"signal\"
688object 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)))
697
698(defun dbus-introspect-get-property-names (bus service path interface)
699 "Return a list of strings of all property names of INTERFACE.
700SERVICE 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))))
706
707(defun dbus-introspect-get-property (bus service path interface property)
708 "This function returns PROPERTY of INTERFACE as XML object.
709It must be located at SERVICE in D-Bus BUS at object path PATH.
710PROPERTY must be a string, element of the list returned by
711`dbus-introspect-get-property-names'. The resulting PROPERTY
712object 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)))
722
723(defun dbus-introspect-get-annotation-names
724 (bus service path interface &optional name)
725 "Return all annotation names as list of strings.
726If NAME is `nil', the annotations are children of INTERFACE,
727otherwise NAME must be a \"method\", \"signal\", or \"property\"
728object, 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))))
739
740(defun dbus-introspect-get-annotation
741 (bus service path interface name annotation)
742 "Return ANNOTATION as XML object.
743If NAME is `nil', ANNOTATION is a child of INTERFACE, otherwise
744NAME 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)))
762
763(defun dbus-introspect-get-argument-names (bus service path interface name)
764 "Return a list of all argument names as list of strings.
765NAME must be a \"method\" or \"signal\" object.
766
767Argument names are optional, the function can return `nil'
768therefore, 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))))
776
777(defun dbus-introspect-get-argument (bus service path interface name arg)
778 "Return argument ARG as XML object.
779NAME must be a \"method\" or \"signal\" object. ARG must be a
780string, 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)))
790
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.
794If NAME is a `method', DIRECTION can be either \"in\" or \"out\".
795If DIRECTION is `nil', \"in\" is assumed.
796
797If NAME is a `signal', and DIRECTION is non-`nil', DIRECTION must
798be \"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 "")))
3a993e3d 824
f636d3ca
MA
825\f
826;;; D-Bus properties.
3a993e3d 827
f636d3ca
MA
828(defun dbus-get-property (bus service path interface property)
829 "Return the value of PROPERTY of INTERFACE.
830It will be checked at BUS, SERVICE, PATH. The result can be any
831valid D-Bus value, or `nil' if there is no PROPERTY."
246a286b 832 (dbus-ignore-errors
b172ed20
MA
833 ;; "Get" returns a variant, so we must use the `car'.
834 (car
d4b06783
MA
835 (funcall
836 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
b172ed20
MA
837 bus service path dbus-interface-properties
838 "Get" :timeout 500 interface property))))
f636d3ca
MA
839
840(defun dbus-set-property (bus service path interface property value)
841 "Set value of PROPERTY of INTERFACE to VALUE.
842It will be checked at BUS, SERVICE, PATH. When the value has
843been set successful, the result is VALUE. Otherwise, `nil' is
844returned."
845 (dbus-ignore-errors
b172ed20 846 ;; "Set" requires a variant.
d4b06783
MA
847 (funcall
848 (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
b172ed20
MA
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)))
f636d3ca
MA
853
854(defun dbus-get-all-properties (bus service path interface)
855 "Return all properties of INTERFACE at BUS, SERVICE, PATH.
856The result is a list of entries. Every entry is a cons of the
857name of the property, and its value. If there are no properties,
858`nil' is returned."
f636d3ca 859 (dbus-ignore-errors
b172ed20 860 ;; "GetAll" returns "a{sv}".
f636d3ca 861 (let (result)
b172ed20 862 (dolist (dict
d4b06783
MA
863 (funcall
864 (if noninteractive
865 'dbus-call-method
866 'dbus-call-method-non-blocking)
b172ed20
MA
867 bus service path dbus-interface-properties
868 "GetAll" :timeout 500 interface)
f636d3ca 869 result)
b172ed20
MA
870 (add-to-list 'result (cons (car dict) (caadr dict)) 'append)))))
871
872(defun dbus-register-property
873 (bus service path interface property access value)
874 "Register property PROPERTY on the D-Bus BUS.
875
876BUS is either the symbol `:system' or the symbol `:session'.
877
878SERVICE is the D-Bus service name of the D-Bus. It must be a
879known name.
880
881PATH is the D-Bus object path SERVICE is registered. INTERFACE
882is the name of the interface used at PATH, PROPERTY is the name
883of the property of INTERFACE. ACCESS indicates, whether the
884property can be changed by other services via D-Bus. It must be
885either the symbol `:read' or `:readwrite'. VALUE is the initial
886value of the property, it can be of any valid type (see
887`dbus-call-method' for details).
888
889If PROPERTY already exists on PATH, it will be overwritten. For
890properties with access type `:read' this is the only way to
891change their values. Properties with access type `:readwrite'
892can be changed by `dbus-set-property'.
893
894The interface \"org.freedesktop.DBus.Properties\" is added to
895PATH, 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)))
899
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))
905
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
c0a39702 909 bus service path dbus-interface-properties "Get" 'dbus-property-handler)
b172ed20 910 (dbus-register-method
c0a39702 911 bus service path dbus-interface-properties "GetAll" 'dbus-property-handler)
b172ed20 912 (dbus-register-method
c0a39702 913 bus service path dbus-interface-properties "Set" 'dbus-property-handler)
b172ed20
MA
914
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)
920
921 ;; Return the object.
922 (list key (list service path))))
923
924(defun dbus-property-handler (&rest args)
c0a39702
MA
925 "Default Handler for the \"org.freedesktop.DBus.Properties\" interface.
926It will be registered for all objects created by `dbus-register-object'."
b172ed20
MA
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))))))))
939
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))
955
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)))
31bb373f 963 (not (functionp (car (last (car val))))))
b172ed20
MA
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))))))
971
972 \f
720c7cd6
MA
973;; Initialize :system and :session buses. This adds their file
974;; descriptors to input_wait_mask, in order to detect incoming
975;; messages immediately.
9e846523
MA
976(when (featurep 'dbusbind)
977 (dbus-ignore-errors
978 (dbus-init-bus :system)
979 (dbus-init-bus :session)))
720c7cd6 980
3a993e3d
MA
981(provide 'dbus)
982
79f10da0 983;; arch-tag: a47caf84-9162-4811-90cc-5d388e37b9bd
3a993e3d 984;;; dbus.el ends here