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