1 ;;; -*- no-byte-compile: t; -*-
2 ;;; dbus.el --- Elisp bindings for D-Bus.
4 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Michael Albinus <michael.albinus@gmx.de>
7 ;; Keywords: comm, hardware
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, see
23 ;; <http://www.gnu.org/licenses/>.
27 ;; This package provides language bindings for the D-Bus API. D-Bus
28 ;; is a message bus system, a simple way for applications to talk to
29 ;; one another. See <http://dbus.freedesktop.org/> for details.
31 ;; Low-level language bindings are implemented in src/dbusbind.c.
35 ;; D-Bus support in the Emacs core can be disabled with configuration
36 ;; option "--without-dbus". Check it.
37 (assert (featurep 'dbusbind) nil "D-Bus is not supported")
41 (defconst dbus-service-dbus "org.freedesktop.DBus"
42 "The bus name used to talk to the bus itself.")
44 (defconst dbus-path-dbus "/org/freedesktop/DBus"
45 "The object path used to talk to the bus itself.")
47 (defconst dbus-interface-dbus "org.freedesktop.DBus"
48 "The interface exported by the object with `dbus-service-dbus' and `dbus-path-dbus'.")
50 (defconst dbus-interface-introspectable "org.freedesktop.DBus.Introspectable"
51 "The interface supported by introspectable objects.")
53 (defmacro dbus-ignore-errors (&rest body)
54 "Execute BODY; signal D-Bus error when `dbus-debug' is non-nil.
55 Otherwise, return result of last form in BODY, or all other errors."
58 (dbus-error (when dbus-debug (signal (car err) (cdr err))))))
60 (put 'dbus-ignore-errors 'lisp-indent-function 0)
61 (put 'dbus-ignore-errors 'edebug-form-spec '(form symbolp body))
62 (font-lock-add-keywords 'emacs-lisp-mode '("\\<dbus-ignore-errors\\>"))
65 ;;; Hash table of registered functions.
67 ;; We create it here. So we have a simple test in dbusbind.c, whether
68 ;; the Lisp code has been loaded.
69 (setq dbus-registered-functions-table (make-hash-table :test 'equal))
71 (defun dbus-list-hash-table ()
72 "Returns all registered member registrations to D-Bus.
73 The return value is a list, with elements of kind (KEY . VALUE).
74 See `dbus-registered-functions-table' for a description of the
78 '(lambda (key value) (add-to-list 'result (cons key value) 'append))
79 dbus-registered-functions-table)
82 (defun dbus-unregister-object (object)
83 "Unregister OBJECT from D-Bus.
84 OBJECT must be the result of a preceding `dbus-register-method'
85 or `dbus-register-signal' call. It returns t if OBJECT has been
86 unregistered, nil otherwise."
88 (unless (and (consp object) (not (null (car object))) (consp (cdr object)))
89 (signal 'wrong-type-argument (list 'D-Bus object)))
91 ;; Find the corresponding entry in the hash table.
92 (let* ((key (car object))
93 (value (gethash key dbus-registered-functions-table)))
94 ;; Loop over the registered functions.
96 ;; (car value) has the structure (UNAME SERVICE PATH HANDLER).
97 ;; (cdr object) has the structure ((SERVICE PATH HANDLER) ...).
98 (if (not (equal (cdr (car value)) (car (cdr object))))
99 (setq value (cdr value))
100 ;; Compute new hash value. If it is empty, remove it from
105 (delete (car value) (gethash key dbus-registered-functions-table))
106 dbus-registered-functions-table)
107 (remhash key dbus-registered-functions-table))
111 (defun dbus-name-owner-changed-handler (&rest args)
112 "Reapplies all member registrations to D-Bus.
113 This handler is applied when a \"NameOwnerChanged\" signal has
114 arrived. SERVICE is the object name for which the name owner has
115 been changed. OLD-OWNER is the previous owner of SERVICE, or the
116 empty string if SERVICE was not owned yet. NEW-OWNER is the new
117 owner of SERVICE, or the empty string if SERVICE looses any name owner.
119 usage: (dbus-name-owner-changed-handler service old-owner new-owner)"
121 ;; Check the arguments. We should silently ignore it when they
123 (if (and (= (length args) 3)
125 (stringp (cadr args))
126 (stringp (caddr args)))
127 (let ((service (car args))
128 (old-owner (cadr args))
129 (new-owner (caddr args)))
130 ;; Check whether SERVICE is a known name.
131 (when (not (string-match "^:" service))
135 ;; key has the structure (BUS INTERFACE MEMBER).
136 ;; elt has the structure (UNAME SERVICE PATH HANDLER).
137 (when (string-equal old-owner (car elt))
138 ;; Remove old key, and add new entry with changed name.
139 (dbus-unregister-object (list key (cdr elt)))
140 ;; Maybe we could arrange the lists a little bit better
141 ;; that we don't need to extract every single element?
142 (dbus-register-signal
144 (nth 0 key) (nth 1 elt) (nth 2 elt)
145 ;; INTERFACE MEMBER HANDLER
146 (nth 1 key) (nth 2 key) (nth 3 elt)))))
147 (copy-hash-table dbus-registered-functions-table))))
148 ;; The error is reported only in debug mode.
153 (format "Wrong arguments of %s.NameOwnerChanged" dbus-interface-dbus)
156 ;; Register the handler.
158 (dbus-register-signal
159 :system dbus-service-dbus dbus-path-dbus dbus-interface-dbus
160 "NameOwnerChanged" 'dbus-name-owner-changed-handler)
161 (dbus-register-signal
162 :session dbus-service-dbus dbus-path-dbus dbus-interface-dbus
163 "NameOwnerChanged" 'dbus-name-owner-changed-handler))
168 (defun dbus-check-event (event)
169 "Checks whether EVENT is a well formed D-Bus event.
170 EVENT is a list which starts with symbol `dbus-event':
172 (dbus-event BUS SERIAL SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS)
174 BUS identifies the D-Bus the message is coming from. It is
175 either the symbol `:system' or the symbol `:session'. SERIAL is
176 the serial number of the received D-Bus message if it is a method
177 call, or nil. SERVICE and PATH are the unique name and the
178 object path of the D-Bus object emitting the message. INTERFACE
179 and MEMBER denote the message which has been sent. HANDLER is
180 the function which has been registered for this message. ARGS
181 are the arguments passed to HANDLER, when it is called during
182 event handling in `dbus-handle-event'.
184 This function raises a `dbus-error' signal in case the event is
186 (when dbus-debug (message "DBus-Event %s" event))
187 (unless (and (listp event)
188 (eq (car event) 'dbus-event)
190 (symbolp (nth 1 event))
192 (or (natnump (nth 2 event)) (null (nth 2 event)))
194 (stringp (nth 3 event))
196 (stringp (nth 4 event))
198 (stringp (nth 5 event))
200 (stringp (nth 6 event))
202 (functionp (nth 7 event)))
203 (signal 'dbus-error (list "Not a valid D-Bus event" event))))
206 (defun dbus-handle-event (event)
207 "Handle events from the D-Bus.
208 EVENT is a D-Bus event, see `dbus-check-event'. HANDLER, being
209 part of the event, is called with arguments ARGS."
211 ;; We don't want to raise an error, because this function is called
212 ;; in the event handling loop.
215 (dbus-check-event event)
216 (setq result (apply (nth 7 event) (nthcdr 8 event)))
217 (unless (consp result) (setq result (cons result nil)))
218 ;; Return a message when serial is not nil.
219 (when (not (null (nth 2 event)))
220 (apply 'dbus-method-return-internal
221 (nth 1 event) (nth 2 event) (nth 3 event) result)))))
223 (defun dbus-event-bus-name (event)
224 "Return the bus name the event is coming from.
225 The result is either the symbol `:system' or the symbol `:session'.
226 EVENT is a D-Bus event, see `dbus-check-event'. This function
227 raises a `dbus-error' signal in case the event is not well
229 (dbus-check-event event)
232 (defun dbus-event-serial-number (event)
233 "Return the serial number of the corresponding D-Bus message.
234 The result is a number in case the D-Bus message is a method
235 call, or nil for all other mesage types. The serial number is
236 needed for generating a reply message. EVENT is a D-Bus event,
237 see `dbus-check-event'. This function raises a `dbus-error'
238 signal in case the event is not well formed."
239 (dbus-check-event event)
242 (defun dbus-event-service-name (event)
243 "Return the name of the D-Bus object the event is coming from.
244 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
245 This function raises a `dbus-error' signal in case the event is
247 (dbus-check-event event)
250 (defun dbus-event-path-name (event)
251 "Return the object path of the D-Bus object the event is coming from.
252 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
253 This function raises a `dbus-error' signal in case the event is
255 (dbus-check-event event)
258 (defun dbus-event-interface-name (event)
259 "Return the interface name of the D-Bus object the event is coming from.
260 The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
261 This function raises a `dbus-error' signal in case the event is
263 (dbus-check-event event)
266 (defun dbus-event-member-name (event)
267 "Return the member name the event is coming from.
268 It is either a signal name or a method name. The result is is a
269 string. EVENT is a D-Bus event, see `dbus-check-event'. This
270 function raises a `dbus-error' signal in case the event is not
272 (dbus-check-event event)
276 ;;; D-Bus registered names.
278 (defun dbus-list-activatable-names ()
279 "Return the D-Bus service names which can be activated as list.
280 The result is a list of strings, which is nil when there are no
281 activatable service names at all."
284 :system dbus-service-dbus
285 dbus-path-dbus dbus-interface-dbus "ListActivatableNames")))
287 (defun dbus-list-names (bus)
288 "Return the service names registered at D-Bus BUS.
289 The result is a list of strings, which is nil when there are no
290 registered service names at all. Well known names are strings like
291 \"org.freedesktop.DBus\". Names starting with \":\" are unique names
295 bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus "ListNames")))
297 (defun dbus-list-known-names (bus)
298 "Retrieve all services which correspond to a known name in BUS.
299 A service has a known name if it doesn't start with \":\"."
301 (dolist (name (dbus-list-names bus) result)
302 (unless (string-equal ":" (substring name 0 1))
303 (add-to-list 'result name 'append)))))
305 (defun dbus-list-queued-owners (bus service)
306 "Return the unique names registered at D-Bus BUS and queued for SERVICE.
307 The result is a list of strings, or nil when there are no queued name
308 owners service names at all."
311 bus dbus-service-dbus dbus-path-dbus
312 dbus-interface-dbus "ListQueuedOwners" service)))
314 (defun dbus-get-name-owner (bus service)
315 "Return the name owner of SERVICE registered at D-Bus BUS.
316 The result is either a string, or nil if there is no name owner."
319 bus dbus-service-dbus dbus-path-dbus
320 dbus-interface-dbus "GetNameOwner" service)))
322 (defun dbus-introspect (bus service path)
323 "Return the introspection data of SERVICE in D-Bus BUS at object path PATH.
324 The data are in XML format.
329 :system \"org.freedesktop.Hal\"
330 \"/org/freedesktop/Hal/devices/computer\")"
333 bus service path dbus-interface-introspectable "Introspect")))
335 (if nil ;; Must be reworked. Shall we offer D-Bus signatures at all?
336 (defun dbus-get-signatures (bus interface signal)
337 "Retrieve SIGNAL's type signatures from D-Bus.
338 The result is a list of SIGNAL's type signatures. Example:
340 \(\"s\" \"b\" \"ai\"\)
342 This list represents 3 parameters of SIGNAL. The first parameter
343 is of type string, the second parameter is of type boolean, and
344 the third parameter is of type array of integer.
346 If INTERFACE or SIGNAL do not exist, or if they do not support
347 the D-Bus method org.freedesktop.DBus.Introspectable.Introspect,
348 the function returns nil."
350 (let ((introspect-xml
352 (insert (dbus-introspect bus interface))
353 (xml-parse-region (point-min) (point-max))))
354 node interfaces signals args result)
355 ;; Get the root node.
356 (setq node (xml-node-name introspect-xml))
357 ;; Get all interfaces.
358 (setq interfaces (xml-get-children node 'interface))
360 (when (string-equal (xml-get-attribute (car interfaces) 'name)
362 ;; That's the requested interface. Check for signals.
363 (setq signals (xml-get-children (car interfaces) 'signal))
365 (when (string-equal (xml-get-attribute (car signals) 'name) signal)
366 ;; The signal we are looking for.
367 (setq args (xml-get-children (car signals) 'arg))
369 (unless (xml-get-attribute (car args) 'type)
370 ;; This shouldn't happen, let's escape.
371 (signal 'dbus-error nil))
372 ;; We append the signature.
374 result (append result
375 (list (xml-get-attribute (car args) 'type))))
376 (setq args (cdr args)))
378 (setq signals (cdr signals)))
379 (setq interfaces nil))
380 (setq interfaces (cdr interfaces)))
386 ;; arch-tag: a47caf84-9162-4811-90cc-5d388e37b9bd
387 ;;; dbus.el ends here