dbus.el 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. ;;; dbus.el --- Elisp bindings for D-Bus.
  2. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Michael Albinus <michael.albinus@gmx.de>
  4. ;; Keywords: comm, hardware
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This package provides language bindings for the D-Bus API. D-Bus
  18. ;; is a message bus system, a simple way for applications to talk to
  19. ;; one another. See <http://dbus.freedesktop.org/> for details.
  20. ;; Low-level language bindings are implemented in src/dbusbind.c.
  21. ;;; Code:
  22. ;; D-Bus support in the Emacs core can be disabled with configuration
  23. ;; option "--without-dbus". Declare used subroutines and variables.
  24. (declare-function dbus-call-method "dbusbind.c")
  25. (declare-function dbus-call-method-asynchronously "dbusbind.c")
  26. (declare-function dbus-init-bus "dbusbind.c")
  27. (declare-function dbus-method-return-internal "dbusbind.c")
  28. (declare-function dbus-method-error-internal "dbusbind.c")
  29. (declare-function dbus-register-signal "dbusbind.c")
  30. (declare-function dbus-register-method "dbusbind.c")
  31. (declare-function dbus-send-signal "dbusbind.c")
  32. (defvar dbus-debug)
  33. (defvar dbus-registered-objects-table)
  34. ;; Pacify byte compiler.
  35. (eval-when-compile
  36. (require 'cl))
  37. (require 'xml)
  38. (defconst dbus-service-dbus "org.freedesktop.DBus"
  39. "The bus name used to talk to the bus itself.")
  40. (defconst dbus-path-dbus "/org/freedesktop/DBus"
  41. "The object path used to talk to the bus itself.")
  42. (defconst dbus-interface-dbus "org.freedesktop.DBus"
  43. "The interface exported by the object with `dbus-service-dbus' and `dbus-path-dbus'.")
  44. (defconst dbus-interface-peer (concat dbus-interface-dbus ".Peer")
  45. "The interface for peer objects.")
  46. (defconst dbus-interface-introspectable
  47. (concat dbus-interface-dbus ".Introspectable")
  48. "The interface supported by introspectable objects.")
  49. (defconst dbus-interface-properties (concat dbus-interface-dbus ".Properties")
  50. "The interface for property objects.")
  51. (defconst dbus-service-emacs "org.gnu.Emacs"
  52. "The well known service name of Emacs.")
  53. (defconst dbus-path-emacs "/org/gnu/Emacs"
  54. "The object path head used by Emacs.")
  55. (defconst dbus-message-type-invalid 0
  56. "This value is never a valid message type.")
  57. (defconst dbus-message-type-method-call 1
  58. "Message type of a method call message.")
  59. (defconst dbus-message-type-method-return 2
  60. "Message type of a method return message.")
  61. (defconst dbus-message-type-error 3
  62. "Message type of an error reply message.")
  63. (defconst dbus-message-type-signal 4
  64. "Message type of a signal message.")
  65. (defmacro dbus-ignore-errors (&rest body)
  66. "Execute BODY; signal D-Bus error when `dbus-debug' is non-nil.
  67. Otherwise, return result of last form in BODY, or all other errors."
  68. (declare (indent 0) (debug t))
  69. `(condition-case err
  70. (progn ,@body)
  71. (dbus-error (when dbus-debug (signal (car err) (cdr err))))))
  72. (font-lock-add-keywords 'emacs-lisp-mode '("\\<dbus-ignore-errors\\>"))
  73. (defvar dbus-event-error-hooks nil
  74. "Functions to be called when a D-Bus error happens in the event handler.
  75. Every function must accept two arguments, the event and the error variable
  76. caught in `condition-case' by `dbus-error'.")
  77. ;;; Hash table of registered functions.
  78. (defvar dbus-return-values-table (make-hash-table :test 'equal)
  79. "Hash table for temporary storing arguments of reply messages.
  80. A key in this hash table is a list (BUS SERIAL). BUS is either a
  81. Lisp symbol, `:system' or `:session', or a string denoting the
  82. bus address. SERIAL is the serial number of the reply message.
  83. See `dbus-call-method-non-blocking-handler' and
  84. `dbus-call-method-non-blocking'.")
  85. (defun dbus-list-hash-table ()
  86. "Returns all registered member registrations to D-Bus.
  87. The return value is a list, with elements of kind (KEY . VALUE).
  88. See `dbus-registered-objects-table' for a description of the
  89. hash table."
  90. (let (result)
  91. (maphash
  92. (lambda (key value) (add-to-list 'result (cons key value) 'append))
  93. dbus-registered-objects-table)
  94. result))
  95. (defun dbus-unregister-object (object)
  96. "Unregister OBJECT from D-Bus.
  97. OBJECT must be the result of a preceding `dbus-register-method',
  98. `dbus-register-property' or `dbus-register-signal' call. It
  99. returns `t' if OBJECT has been unregistered, `nil' otherwise.
  100. When OBJECT identifies the last method or property, which is
  101. registered for the respective service, Emacs releases its
  102. association to the service from D-Bus."
  103. ;; Check parameter.
  104. (unless (and (consp object) (not (null (car object))) (consp (cdr object)))
  105. (signal 'wrong-type-argument (list 'D-Bus object)))
  106. ;; Find the corresponding entry in the hash table.
  107. (let* ((key (car object))
  108. (value (cadr object))
  109. (bus (car key))
  110. (service (car value))
  111. (entry (gethash key dbus-registered-objects-table))
  112. ret)
  113. ;; key has the structure (BUS INTERFACE MEMBER).
  114. ;; value has the structure (SERVICE PATH [HANDLER]).
  115. ;; entry has the structure ((UNAME SERVICE PATH MEMBER [RULE]) ...).
  116. ;; MEMBER is either a string (the handler), or a cons cell (a
  117. ;; property value). UNAME and property values are not taken into
  118. ;; account for comparison.
  119. ;; Loop over the registered functions.
  120. (dolist (elt entry)
  121. (when (equal
  122. value
  123. (butlast (cdr elt) (- (length (cdr elt)) (length value))))
  124. (setq ret t)
  125. ;; Compute new hash value. If it is empty, remove it from the
  126. ;; hash table.
  127. (unless (puthash key (delete elt entry) dbus-registered-objects-table)
  128. (remhash key dbus-registered-objects-table))
  129. ;; Remove match rule of signals.
  130. (let ((rule (nth 4 elt)))
  131. (when (stringp rule)
  132. (setq service nil) ; We do not need to unregister the service.
  133. (dbus-call-method
  134. bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
  135. "RemoveMatch" rule)))))
  136. ;; Check, whether there is still a registered function or property
  137. ;; for the given service. If not, unregister the service from the
  138. ;; bus.
  139. (when service
  140. (dolist (elt entry)
  141. (let (found)
  142. (maphash
  143. (lambda (k v)
  144. (dolist (e v)
  145. (ignore-errors
  146. (when (and (equal bus (car k)) (string-equal service (cadr e)))
  147. (setq found t)))))
  148. dbus-registered-objects-table)
  149. (unless found
  150. (dbus-call-method
  151. bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
  152. "ReleaseName" service)))))
  153. ;; Return.
  154. ret))
  155. (defun dbus-unregister-service (bus service)
  156. "Unregister all objects related to SERVICE from D-Bus BUS.
  157. BUS is either a Lisp symbol, `:system' or `:session', or a string
  158. denoting the bus address. SERVICE must be a known service name.
  159. The function returns a keyword, indicating the result of the
  160. operation. One of the following keywords is returned:
  161. `:released': Service has become the primary owner of the name.
  162. `:non-existent': Service name does not exist on this bus.
  163. `:not-owner': We are neither the primary owner nor waiting in the
  164. queue of this service."
  165. (maphash
  166. (lambda (key value)
  167. (dolist (elt value)
  168. (ignore-errors
  169. (when (and (equal bus (car key)) (string-equal service (cadr elt)))
  170. (unless
  171. (puthash key (delete elt value) dbus-registered-objects-table)
  172. (remhash key dbus-registered-objects-table))))))
  173. dbus-registered-objects-table)
  174. (let ((reply (dbus-call-method
  175. bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
  176. "ReleaseName" service)))
  177. (case reply
  178. (1 :released)
  179. (2 :non-existent)
  180. (3 :not-owner)
  181. (t (signal 'dbus-error (list "Could not unregister service" service))))))
  182. (defun dbus-call-method-non-blocking-handler (&rest args)
  183. "Handler for reply messages of asynchronous D-Bus message calls.
  184. It calls the function stored in `dbus-registered-objects-table'.
  185. The result will be made available in `dbus-return-values-table'."
  186. (puthash (list (dbus-event-bus-name last-input-event)
  187. (dbus-event-serial-number last-input-event))
  188. (if (= (length args) 1) (car args) args)
  189. dbus-return-values-table))
  190. (defun dbus-call-method-non-blocking
  191. (bus service path interface method &rest args)
  192. "Call METHOD on the D-Bus BUS, but don't block the event queue.
  193. This is necessary for communicating to registered D-Bus methods,
  194. which are running in the same Emacs process.
  195. The arguments are the same as in `dbus-call-method'.
  196. usage: (dbus-call-method-non-blocking
  197. BUS SERVICE PATH INTERFACE METHOD
  198. &optional :timeout TIMEOUT &rest ARGS)"
  199. (let ((key
  200. (apply
  201. 'dbus-call-method-asynchronously
  202. bus service path interface method
  203. 'dbus-call-method-non-blocking-handler args)))
  204. ;; Wait until `dbus-call-method-non-blocking-handler' has put the
  205. ;; result into `dbus-return-values-table'.
  206. (while (eq (gethash key dbus-return-values-table :ignore) :ignore)
  207. (read-event nil nil 0.1))
  208. ;; Cleanup `dbus-return-values-table'. Return the result.
  209. (prog1
  210. (gethash key dbus-return-values-table nil)
  211. (remhash key dbus-return-values-table))))
  212. (defun dbus-name-owner-changed-handler (&rest args)
  213. "Reapplies all member registrations to D-Bus.
  214. This handler is applied when a \"NameOwnerChanged\" signal has
  215. arrived. SERVICE is the object name for which the name owner has
  216. been changed. OLD-OWNER is the previous owner of SERVICE, or the
  217. empty string if SERVICE was not owned yet. NEW-OWNER is the new
  218. owner of SERVICE, or the empty string if SERVICE loses any name owner.
  219. usage: (dbus-name-owner-changed-handler service old-owner new-owner)"
  220. (save-match-data
  221. ;; Check the arguments. We should silently ignore it when they
  222. ;; are wrong.
  223. (if (and (= (length args) 3)
  224. (stringp (car args))
  225. (stringp (cadr args))
  226. (stringp (caddr args)))
  227. (let ((service (car args))
  228. (old-owner (cadr args))
  229. (new-owner (caddr args)))
  230. ;; Check whether SERVICE is a known name.
  231. (when (not (string-match "^:" service))
  232. (maphash
  233. (lambda (key value)
  234. (dolist (elt value)
  235. ;; key has the structure (BUS INTERFACE MEMBER).
  236. ;; elt has the structure (UNAME SERVICE PATH HANDLER).
  237. (when (string-equal old-owner (car elt))
  238. ;; Remove old key, and add new entry with changed name.
  239. (dbus-unregister-object (list key (cdr elt)))
  240. ;; Maybe we could arrange the lists a little bit better
  241. ;; that we don't need to extract every single element?
  242. (dbus-register-signal
  243. ;; BUS SERVICE PATH
  244. (nth 0 key) (nth 1 elt) (nth 2 elt)
  245. ;; INTERFACE MEMBER HANDLER
  246. (nth 1 key) (nth 2 key) (nth 3 elt)))))
  247. (copy-hash-table dbus-registered-objects-table))))
  248. ;; The error is reported only in debug mode.
  249. (when dbus-debug
  250. (signal
  251. 'dbus-error
  252. (cons
  253. (format "Wrong arguments of %s.NameOwnerChanged" dbus-interface-dbus)
  254. args))))))
  255. ;; Register the handler.
  256. (when nil ;ignore-errors
  257. (dbus-register-signal
  258. :system dbus-service-dbus dbus-path-dbus dbus-interface-dbus
  259. "NameOwnerChanged" 'dbus-name-owner-changed-handler)
  260. (dbus-register-signal
  261. :session dbus-service-dbus dbus-path-dbus dbus-interface-dbus
  262. "NameOwnerChanged" 'dbus-name-owner-changed-handler))
  263. ;;; D-Bus type conversion.
  264. (defun dbus-string-to-byte-array (string)
  265. "Transforms STRING to list (:array :byte c1 :byte c2 ...).
  266. STRING shall be UTF8 coded."
  267. (if (zerop (length string))
  268. '(:array :signature "y")
  269. (let (result)
  270. (dolist (elt (string-to-list string) (append '(:array) result))
  271. (setq result (append result (list :byte elt)))))))
  272. (defun dbus-byte-array-to-string (byte-array)
  273. "Transforms BYTE-ARRAY into UTF8 coded string.
  274. BYTE-ARRAY must be a list of structure (c1 c2 ...)."
  275. (apply 'string byte-array))
  276. (defun dbus-escape-as-identifier (string)
  277. "Escape an arbitrary STRING so it follows the rules for a C identifier.
  278. The escaped string can be used as object path component, interface element
  279. component, bus name component or member name in D-Bus.
  280. The escaping consists of replacing all non-alphanumerics, and the
  281. first character if it's a digit, with an underscore and two
  282. lower-case hex digits:
  283. \"0123abc_xyz\\x01\\xff\" -> \"_30123abc_5fxyz_01_ff\"
  284. i.e. similar to URI encoding, but with \"_\" taking the role of \"%\",
  285. and a smaller allowed set. As a special case, \"\" is escaped to
  286. \"_\".
  287. Returns the escaped string. Algorithm taken from
  288. telepathy-glib's `tp-escape-as-identifier'."
  289. (if (zerop (length string))
  290. "_"
  291. (replace-regexp-in-string
  292. "^[0-9]\\|[^A-Za-z0-9]"
  293. (lambda (x) (format "_%2x" (aref x 0)))
  294. string)))
  295. (defun dbus-unescape-from-identifier (string)
  296. "Retrieve the original string from the encoded STRING.
  297. STRING must have been coded with `dbus-escape-as-identifier'"
  298. (if (string-equal string "_")
  299. ""
  300. (replace-regexp-in-string
  301. "_.."
  302. (lambda (x) (format "%c" (string-to-number (substring x 1) 16)))
  303. string)))
  304. ;;; D-Bus events.
  305. (defun dbus-check-event (event)
  306. "Checks whether EVENT is a well formed D-Bus event.
  307. EVENT is a list which starts with symbol `dbus-event':
  308. (dbus-event BUS TYPE SERIAL SERVICE PATH INTERFACE MEMBER HANDLER &rest ARGS)
  309. BUS identifies the D-Bus the message is coming from. It is
  310. either a Lisp symbol, `:system' or `:session', or a string
  311. denoting the bus address. TYPE is the D-Bus message type which
  312. has caused the event, SERIAL is the serial number of the received
  313. D-Bus message. SERVICE and PATH are the unique name and the
  314. object path of the D-Bus object emitting the message. INTERFACE
  315. and MEMBER denote the message which has been sent. HANDLER is
  316. the function which has been registered for this message. ARGS
  317. are the arguments passed to HANDLER, when it is called during
  318. event handling in `dbus-handle-event'.
  319. This function raises a `dbus-error' signal in case the event is
  320. not well formed."
  321. (when dbus-debug (message "DBus-Event %s" event))
  322. (unless (and (listp event)
  323. (eq (car event) 'dbus-event)
  324. ;; Bus symbol.
  325. (or (symbolp (nth 1 event))
  326. (stringp (nth 1 event)))
  327. ;; Type.
  328. (and (natnump (nth 2 event))
  329. (< dbus-message-type-invalid (nth 2 event)))
  330. ;; Serial.
  331. (natnump (nth 3 event))
  332. ;; Service.
  333. (or (= dbus-message-type-method-return (nth 2 event))
  334. (= dbus-message-type-error (nth 2 event))
  335. (stringp (nth 4 event)))
  336. ;; Object path.
  337. (or (= dbus-message-type-method-return (nth 2 event))
  338. (= dbus-message-type-error (nth 2 event))
  339. (stringp (nth 5 event)))
  340. ;; Interface.
  341. (or (= dbus-message-type-method-return (nth 2 event))
  342. (= dbus-message-type-error (nth 2 event))
  343. (stringp (nth 6 event)))
  344. ;; Member.
  345. (or (= dbus-message-type-method-return (nth 2 event))
  346. (= dbus-message-type-error (nth 2 event))
  347. (stringp (nth 7 event)))
  348. ;; Handler.
  349. (functionp (nth 8 event)))
  350. (signal 'dbus-error (list "Not a valid D-Bus event" event))))
  351. ;;;###autoload
  352. (defun dbus-handle-event (event)
  353. "Handle events from the D-Bus.
  354. EVENT is a D-Bus event, see `dbus-check-event'. HANDLER, being
  355. part of the event, is called with arguments ARGS.
  356. If the HANDLER returns a `dbus-error', it is propagated as return message."
  357. (interactive "e")
  358. (condition-case err
  359. (let (result)
  360. ;; We ignore not well-formed events.
  361. (dbus-check-event event)
  362. ;; Error messages must be propagated.
  363. (when (= dbus-message-type-error (nth 2 event))
  364. (signal 'dbus-error (nthcdr 9 event)))
  365. ;; Apply the handler.
  366. (setq result (apply (nth 8 event) (nthcdr 9 event)))
  367. ;; Return a message when it is a message call.
  368. (when (= dbus-message-type-method-call (nth 2 event))
  369. (dbus-ignore-errors
  370. (if (eq result :ignore)
  371. (dbus-method-return-internal
  372. (nth 1 event) (nth 3 event) (nth 4 event))
  373. (apply 'dbus-method-return-internal
  374. (nth 1 event) (nth 3 event) (nth 4 event)
  375. (if (consp result) result (list result)))))))
  376. ;; Error handling.
  377. (dbus-error
  378. ;; Return an error message when it is a message call.
  379. (when (= dbus-message-type-method-call (nth 2 event))
  380. (dbus-ignore-errors
  381. (dbus-method-error-internal
  382. (nth 1 event) (nth 3 event) (nth 4 event) (cadr err))))
  383. ;; Propagate D-Bus error messages.
  384. (run-hook-with-args 'dbus-event-error-hooks event err)
  385. (when (or dbus-debug (= dbus-message-type-error (nth 2 event)))
  386. (signal (car err) (cdr err))))))
  387. (defun dbus-event-bus-name (event)
  388. "Return the bus name the event is coming from.
  389. The result is either a Lisp symbol, `:system' or `:session', or a
  390. string denoting the bus address. EVENT is a D-Bus event, see
  391. `dbus-check-event'. This function raises a `dbus-error' signal
  392. in case the event is not well formed."
  393. (dbus-check-event event)
  394. (nth 1 event))
  395. (defun dbus-event-message-type (event)
  396. "Return the message type of the corresponding D-Bus message.
  397. The result is a number. EVENT is a D-Bus event, see
  398. `dbus-check-event'. This function raises a `dbus-error' signal
  399. in case the event is not well formed."
  400. (dbus-check-event event)
  401. (nth 2 event))
  402. (defun dbus-event-serial-number (event)
  403. "Return the serial number of the corresponding D-Bus message.
  404. The result is a number. The serial number is needed for
  405. generating a reply message. EVENT is a D-Bus event, see
  406. `dbus-check-event'. This function raises a `dbus-error' signal
  407. in case the event is not well formed."
  408. (dbus-check-event event)
  409. (nth 3 event))
  410. (defun dbus-event-service-name (event)
  411. "Return the name of the D-Bus object the event is coming from.
  412. The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
  413. This function raises a `dbus-error' signal in case the event is
  414. not well formed."
  415. (dbus-check-event event)
  416. (nth 4 event))
  417. (defun dbus-event-path-name (event)
  418. "Return the object path of the D-Bus object the event is coming from.
  419. The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
  420. This function raises a `dbus-error' signal in case the event is
  421. not well formed."
  422. (dbus-check-event event)
  423. (nth 5 event))
  424. (defun dbus-event-interface-name (event)
  425. "Return the interface name of the D-Bus object the event is coming from.
  426. The result is a string. EVENT is a D-Bus event, see `dbus-check-event'.
  427. This function raises a `dbus-error' signal in case the event is
  428. not well formed."
  429. (dbus-check-event event)
  430. (nth 6 event))
  431. (defun dbus-event-member-name (event)
  432. "Return the member name the event is coming from.
  433. It is either a signal name or a method name. The result is a
  434. string. EVENT is a D-Bus event, see `dbus-check-event'. This
  435. function raises a `dbus-error' signal in case the event is not
  436. well formed."
  437. (dbus-check-event event)
  438. (nth 7 event))
  439. ;;; D-Bus registered names.
  440. (defun dbus-list-activatable-names (&optional bus)
  441. "Return the D-Bus service names which can be activated as list.
  442. If BUS is left nil, `:system' is assumed. The result is a list
  443. of strings, which is `nil' when there are no activatable service
  444. names at all."
  445. (dbus-ignore-errors
  446. (dbus-call-method
  447. (or bus :system) dbus-service-dbus
  448. dbus-path-dbus dbus-interface-dbus "ListActivatableNames")))
  449. (defun dbus-list-names (bus)
  450. "Return the service names registered at D-Bus BUS.
  451. The result is a list of strings, which is `nil' when there are no
  452. registered service names at all. Well known names are strings
  453. like \"org.freedesktop.DBus\". Names starting with \":\" are
  454. unique names for services."
  455. (dbus-ignore-errors
  456. (dbus-call-method
  457. bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus "ListNames")))
  458. (defun dbus-list-known-names (bus)
  459. "Retrieve all services which correspond to a known name in BUS.
  460. A service has a known name if it doesn't start with \":\"."
  461. (let (result)
  462. (dolist (name (dbus-list-names bus) result)
  463. (unless (string-equal ":" (substring name 0 1))
  464. (add-to-list 'result name 'append)))))
  465. (defun dbus-list-queued-owners (bus service)
  466. "Return the unique names registered at D-Bus BUS and queued for SERVICE.
  467. The result is a list of strings, or `nil' when there are no
  468. queued name owners service names at all."
  469. (dbus-ignore-errors
  470. (dbus-call-method
  471. bus dbus-service-dbus dbus-path-dbus
  472. dbus-interface-dbus "ListQueuedOwners" service)))
  473. (defun dbus-get-name-owner (bus service)
  474. "Return the name owner of SERVICE registered at D-Bus BUS.
  475. The result is either a string, or `nil' if there is no name owner."
  476. (dbus-ignore-errors
  477. (dbus-call-method
  478. bus dbus-service-dbus dbus-path-dbus
  479. dbus-interface-dbus "GetNameOwner" service)))
  480. (defun dbus-ping (bus service &optional timeout)
  481. "Check whether SERVICE is registered for D-Bus BUS.
  482. TIMEOUT, a nonnegative integer, specifies the maximum number of
  483. milliseconds `dbus-ping' must return. The default value is 25,000.
  484. Note, that this autoloads SERVICE if it is not running yet. If
  485. it shall be checked whether SERVICE is already running, one shall
  486. apply
  487. \(member service \(dbus-list-known-names bus))"
  488. ;; "Ping" raises a D-Bus error if SERVICE does not exist.
  489. ;; Otherwise, it returns silently with `nil'.
  490. (condition-case nil
  491. (not
  492. (if (natnump timeout)
  493. (dbus-call-method
  494. bus service dbus-path-dbus dbus-interface-peer
  495. "Ping" :timeout timeout)
  496. (dbus-call-method
  497. bus service dbus-path-dbus dbus-interface-peer "Ping")))
  498. (dbus-error nil)))
  499. ;;; D-Bus introspection.
  500. (defun dbus-introspect (bus service path)
  501. "Return all interfaces and sub-nodes of SERVICE,
  502. registered at object path PATH at bus BUS.
  503. BUS is either a Lisp symbol, `:system' or `:session', or a string
  504. denoting the bus address. SERVICE must be a known service name,
  505. and PATH must be a valid object path. The last two parameters
  506. are strings. The result, the introspection data, is a string in
  507. XML format."
  508. ;; We don't want to raise errors. `dbus-call-method-non-blocking'
  509. ;; is used, because the handler can be registered in our Emacs
  510. ;; instance; caller an callee would block each other.
  511. (dbus-ignore-errors
  512. (funcall
  513. (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
  514. bus service path dbus-interface-introspectable "Introspect")))
  515. (defun dbus-introspect-xml (bus service path)
  516. "Return the introspection data of SERVICE in D-Bus BUS at object path PATH.
  517. The data are a parsed list. The root object is a \"node\",
  518. representing the object path PATH. The root object can contain
  519. \"interface\" and further \"node\" objects."
  520. ;; We don't want to raise errors.
  521. (xml-node-name
  522. (ignore-errors
  523. (with-temp-buffer
  524. (insert (dbus-introspect bus service path))
  525. (xml-parse-region (point-min) (point-max))))))
  526. (defun dbus-introspect-get-attribute (object attribute)
  527. "Return the ATTRIBUTE value of D-Bus introspection OBJECT.
  528. ATTRIBUTE must be a string according to the attribute names in
  529. the D-Bus specification."
  530. (xml-get-attribute-or-nil object (intern attribute)))
  531. (defun dbus-introspect-get-node-names (bus service path)
  532. "Return all node names of SERVICE in D-Bus BUS at object path PATH.
  533. It returns a list of strings. The node names stand for further
  534. object paths of the D-Bus service."
  535. (let ((object (dbus-introspect-xml bus service path))
  536. result)
  537. (dolist (elt (xml-get-children object 'node) result)
  538. (add-to-list
  539. 'result (dbus-introspect-get-attribute elt "name") 'append))))
  540. (defun dbus-introspect-get-all-nodes (bus service path)
  541. "Return all node names of SERVICE in D-Bus BUS at object path PATH.
  542. It returns a list of strings, which are further object paths of SERVICE."
  543. (let ((result (list path)))
  544. (dolist (elt
  545. (dbus-introspect-get-node-names bus service path)
  546. result)
  547. (setq elt (expand-file-name elt path))
  548. (setq result
  549. (append result (dbus-introspect-get-all-nodes bus service elt))))))
  550. (defun dbus-introspect-get-interface-names (bus service path)
  551. "Return all interface names of SERVICE in D-Bus BUS at object path PATH.
  552. It returns a list of strings.
  553. There will be always the default interface
  554. \"org.freedesktop.DBus.Introspectable\". Another default
  555. interface is \"org.freedesktop.DBus.Properties\". If present,
  556. \"interface\" objects can also have \"property\" objects as
  557. children, beside \"method\" and \"signal\" objects."
  558. (let ((object (dbus-introspect-xml bus service path))
  559. result)
  560. (dolist (elt (xml-get-children object 'interface) result)
  561. (add-to-list
  562. 'result (dbus-introspect-get-attribute elt "name") 'append))))
  563. (defun dbus-introspect-get-interface (bus service path interface)
  564. "Return the INTERFACE of SERVICE in D-Bus BUS at object path PATH.
  565. The return value is an XML object. INTERFACE must be a string,
  566. element of the list returned by `dbus-introspect-get-interface-names'.
  567. The resulting \"interface\" object can contain \"method\", \"signal\",
  568. \"property\" and \"annotation\" children."
  569. (let ((elt (xml-get-children
  570. (dbus-introspect-xml bus service path) 'interface)))
  571. (while (and elt
  572. (not (string-equal
  573. interface
  574. (dbus-introspect-get-attribute (car elt) "name"))))
  575. (setq elt (cdr elt)))
  576. (car elt)))
  577. (defun dbus-introspect-get-method-names (bus service path interface)
  578. "Return a list of strings of all method names of INTERFACE.
  579. SERVICE is a service of D-Bus BUS at object path PATH."
  580. (let ((object (dbus-introspect-get-interface bus service path interface))
  581. result)
  582. (dolist (elt (xml-get-children object 'method) result)
  583. (add-to-list
  584. 'result (dbus-introspect-get-attribute elt "name") 'append))))
  585. (defun dbus-introspect-get-method (bus service path interface method)
  586. "Return method METHOD of interface INTERFACE as XML object.
  587. It must be located at SERVICE in D-Bus BUS at object path PATH.
  588. METHOD must be a string, element of the list returned by
  589. `dbus-introspect-get-method-names'. The resulting \"method\"
  590. object can contain \"arg\" and \"annotation\" children."
  591. (let ((elt (xml-get-children
  592. (dbus-introspect-get-interface bus service path interface)
  593. 'method)))
  594. (while (and elt
  595. (not (string-equal
  596. method (dbus-introspect-get-attribute (car elt) "name"))))
  597. (setq elt (cdr elt)))
  598. (car elt)))
  599. (defun dbus-introspect-get-signal-names (bus service path interface)
  600. "Return a list of strings of all signal names of INTERFACE.
  601. SERVICE is a service of D-Bus BUS at object path PATH."
  602. (let ((object (dbus-introspect-get-interface bus service path interface))
  603. result)
  604. (dolist (elt (xml-get-children object 'signal) result)
  605. (add-to-list
  606. 'result (dbus-introspect-get-attribute elt "name") 'append))))
  607. (defun dbus-introspect-get-signal (bus service path interface signal)
  608. "Return signal SIGNAL of interface INTERFACE as XML object.
  609. It must be located at SERVICE in D-Bus BUS at object path PATH.
  610. SIGNAL must be a string, element of the list returned by
  611. `dbus-introspect-get-signal-names'. The resulting \"signal\"
  612. object can contain \"arg\" and \"annotation\" children."
  613. (let ((elt (xml-get-children
  614. (dbus-introspect-get-interface bus service path interface)
  615. 'signal)))
  616. (while (and elt
  617. (not (string-equal
  618. signal (dbus-introspect-get-attribute (car elt) "name"))))
  619. (setq elt (cdr elt)))
  620. (car elt)))
  621. (defun dbus-introspect-get-property-names (bus service path interface)
  622. "Return a list of strings of all property names of INTERFACE.
  623. SERVICE is a service of D-Bus BUS at object path PATH."
  624. (let ((object (dbus-introspect-get-interface bus service path interface))
  625. result)
  626. (dolist (elt (xml-get-children object 'property) result)
  627. (add-to-list
  628. 'result (dbus-introspect-get-attribute elt "name") 'append))))
  629. (defun dbus-introspect-get-property (bus service path interface property)
  630. "This function returns PROPERTY of INTERFACE as XML object.
  631. It must be located at SERVICE in D-Bus BUS at object path PATH.
  632. PROPERTY must be a string, element of the list returned by
  633. `dbus-introspect-get-property-names'. The resulting PROPERTY
  634. object can contain \"annotation\" children."
  635. (let ((elt (xml-get-children
  636. (dbus-introspect-get-interface bus service path interface)
  637. 'property)))
  638. (while (and elt
  639. (not (string-equal
  640. property
  641. (dbus-introspect-get-attribute (car elt) "name"))))
  642. (setq elt (cdr elt)))
  643. (car elt)))
  644. (defun dbus-introspect-get-annotation-names
  645. (bus service path interface &optional name)
  646. "Return all annotation names as list of strings.
  647. If NAME is `nil', the annotations are children of INTERFACE,
  648. otherwise NAME must be a \"method\", \"signal\", or \"property\"
  649. object, where the annotations belong to."
  650. (let ((object
  651. (if name
  652. (or (dbus-introspect-get-method bus service path interface name)
  653. (dbus-introspect-get-signal bus service path interface name)
  654. (dbus-introspect-get-property bus service path interface name))
  655. (dbus-introspect-get-interface bus service path interface)))
  656. result)
  657. (dolist (elt (xml-get-children object 'annotation) result)
  658. (add-to-list
  659. 'result (dbus-introspect-get-attribute elt "name") 'append))))
  660. (defun dbus-introspect-get-annotation
  661. (bus service path interface name annotation)
  662. "Return ANNOTATION as XML object.
  663. If NAME is `nil', ANNOTATION is a child of INTERFACE, otherwise
  664. NAME must be the name of a \"method\", \"signal\", or
  665. \"property\" object, where the ANNOTATION belongs to."
  666. (let ((elt (xml-get-children
  667. (if name
  668. (or (dbus-introspect-get-method
  669. bus service path interface name)
  670. (dbus-introspect-get-signal
  671. bus service path interface name)
  672. (dbus-introspect-get-property
  673. bus service path interface name))
  674. (dbus-introspect-get-interface bus service path interface))
  675. 'annotation)))
  676. (while (and elt
  677. (not (string-equal
  678. annotation
  679. (dbus-introspect-get-attribute (car elt) "name"))))
  680. (setq elt (cdr elt)))
  681. (car elt)))
  682. (defun dbus-introspect-get-argument-names (bus service path interface name)
  683. "Return a list of all argument names as list of strings.
  684. NAME must be a \"method\" or \"signal\" object.
  685. Argument names are optional, the function can return `nil'
  686. therefore, even if the method or signal has arguments."
  687. (let ((object
  688. (or (dbus-introspect-get-method bus service path interface name)
  689. (dbus-introspect-get-signal bus service path interface name)))
  690. result)
  691. (dolist (elt (xml-get-children object 'arg) result)
  692. (add-to-list
  693. 'result (dbus-introspect-get-attribute elt "name") 'append))))
  694. (defun dbus-introspect-get-argument (bus service path interface name arg)
  695. "Return argument ARG as XML object.
  696. NAME must be a \"method\" or \"signal\" object. ARG must be a string,
  697. element of the list returned by `dbus-introspect-get-argument-names'."
  698. (let ((elt (xml-get-children
  699. (or (dbus-introspect-get-method bus service path interface name)
  700. (dbus-introspect-get-signal bus service path interface name))
  701. 'arg)))
  702. (while (and elt
  703. (not (string-equal
  704. arg (dbus-introspect-get-attribute (car elt) "name"))))
  705. (setq elt (cdr elt)))
  706. (car elt)))
  707. (defun dbus-introspect-get-signature
  708. (bus service path interface name &optional direction)
  709. "Return signature of a `method' or `signal', represented by NAME, as string.
  710. If NAME is a `method', DIRECTION can be either \"in\" or \"out\".
  711. If DIRECTION is `nil', \"in\" is assumed.
  712. If NAME is a `signal', and DIRECTION is non-`nil', DIRECTION must
  713. be \"out\"."
  714. ;; For methods, we use "in" as default direction.
  715. (let ((object (or (dbus-introspect-get-method
  716. bus service path interface name)
  717. (dbus-introspect-get-signal
  718. bus service path interface name))))
  719. (when (and (string-equal
  720. "method" (dbus-introspect-get-attribute object "name"))
  721. (not (stringp direction)))
  722. (setq direction "in"))
  723. ;; In signals, no direction is given.
  724. (when (string-equal "signal" (dbus-introspect-get-attribute object "name"))
  725. (setq direction nil))
  726. ;; Collect the signatures.
  727. (mapconcat
  728. (lambda (x)
  729. (let ((arg (dbus-introspect-get-argument
  730. bus service path interface name x)))
  731. (if (or (not (stringp direction))
  732. (string-equal
  733. direction
  734. (dbus-introspect-get-attribute arg "direction")))
  735. (dbus-introspect-get-attribute arg "type")
  736. "")))
  737. (dbus-introspect-get-argument-names bus service path interface name)
  738. "")))
  739. ;;; D-Bus properties.
  740. (defun dbus-get-property (bus service path interface property)
  741. "Return the value of PROPERTY of INTERFACE.
  742. It will be checked at BUS, SERVICE, PATH. The result can be any
  743. valid D-Bus value, or `nil' if there is no PROPERTY."
  744. (dbus-ignore-errors
  745. ;; "Get" returns a variant, so we must use the `car'.
  746. (car
  747. (funcall
  748. (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
  749. bus service path dbus-interface-properties
  750. "Get" :timeout 500 interface property))))
  751. (defun dbus-set-property (bus service path interface property value)
  752. "Set value of PROPERTY of INTERFACE to VALUE.
  753. It will be checked at BUS, SERVICE, PATH. When the value has
  754. been set successful, the result is VALUE. Otherwise, `nil' is
  755. returned."
  756. (dbus-ignore-errors
  757. ;; "Set" requires a variant.
  758. (funcall
  759. (if noninteractive 'dbus-call-method 'dbus-call-method-non-blocking)
  760. bus service path dbus-interface-properties
  761. "Set" :timeout 500 interface property (list :variant value))
  762. ;; Return VALUE.
  763. (dbus-get-property bus service path interface property)))
  764. (defun dbus-get-all-properties (bus service path interface)
  765. "Return all properties of INTERFACE at BUS, SERVICE, PATH.
  766. The result is a list of entries. Every entry is a cons of the
  767. name of the property, and its value. If there are no properties,
  768. `nil' is returned."
  769. (dbus-ignore-errors
  770. ;; "GetAll" returns "a{sv}".
  771. (let (result)
  772. (dolist (dict
  773. (funcall
  774. (if noninteractive
  775. 'dbus-call-method
  776. 'dbus-call-method-non-blocking)
  777. bus service path dbus-interface-properties
  778. "GetAll" :timeout 500 interface)
  779. result)
  780. (add-to-list 'result (cons (car dict) (caadr dict)) 'append)))))
  781. (defun dbus-register-property
  782. (bus service path interface property access value
  783. &optional emits-signal dont-register-service)
  784. "Register property PROPERTY on the D-Bus BUS.
  785. BUS is either a Lisp symbol, `:system' or `:session', or a string
  786. denoting the bus address.
  787. SERVICE is the D-Bus service name of the D-Bus. It must be a
  788. known name (See discussion of DONT-REGISTER-SERVICE below).
  789. PATH is the D-Bus object path SERVICE is registered (See
  790. discussion of DONT-REGISTER-SERVICE below). INTERFACE is the
  791. name of the interface used at PATH, PROPERTY is the name of the
  792. property of INTERFACE. ACCESS indicates, whether the property
  793. can be changed by other services via D-Bus. It must be either
  794. the symbol `:read' or `:readwrite'. VALUE is the initial value
  795. of the property, it can be of any valid type (see
  796. `dbus-call-method' for details).
  797. If PROPERTY already exists on PATH, it will be overwritten. For
  798. properties with access type `:read' this is the only way to
  799. change their values. Properties with access type `:readwrite'
  800. can be changed by `dbus-set-property'.
  801. The interface \"org.freedesktop.DBus.Properties\" is added to
  802. PATH, including a default handler for the \"Get\", \"GetAll\" and
  803. \"Set\" methods of this interface. When EMITS-SIGNAL is non-nil,
  804. the signal \"PropertiesChanged\" is sent when the property is
  805. changed by `dbus-set-property'.
  806. When DONT-REGISTER-SERVICE is non-nil, the known name SERVICE is
  807. not registered. This means that other D-Bus clients have no way
  808. of noticing the newly registered property. When interfaces are
  809. constructed incrementally by adding single methods or properties
  810. at a time, DONT-REGISTER-SERVICE can be used to prevent other
  811. clients from discovering the still incomplete interface."
  812. (unless (member access '(:read :readwrite))
  813. (signal 'dbus-error (list "Access type invalid" access)))
  814. ;; Register SERVICE.
  815. (unless (or dont-register-service
  816. (member service (dbus-list-names bus)))
  817. (dbus-call-method
  818. bus dbus-service-dbus dbus-path-dbus dbus-interface-dbus
  819. "RequestName" service 0))
  820. ;; Add handlers for the three property-related methods.
  821. (dbus-register-method
  822. bus service path dbus-interface-properties "Get"
  823. 'dbus-property-handler 'dont-register)
  824. (dbus-register-method
  825. bus service path dbus-interface-properties "GetAll"
  826. 'dbus-property-handler 'dont-register)
  827. (dbus-register-method
  828. bus service path dbus-interface-properties "Set"
  829. 'dbus-property-handler 'dont-register)
  830. ;; Register the name SERVICE with BUS.
  831. (unless dont-register-service
  832. (dbus-register-service bus service))
  833. ;; Send the PropertiesChanged signal.
  834. (when emits-signal
  835. (dbus-send-signal
  836. bus service path dbus-interface-properties "PropertiesChanged"
  837. (list (list :dict-entry property (list :variant value)))
  838. '(:array)))
  839. ;; Create a hash table entry. We use nil for the unique name,
  840. ;; because the property might be accessed from anybody.
  841. (let ((key (list bus interface property))
  842. (val
  843. (list
  844. (list
  845. nil service path
  846. (cons
  847. (if emits-signal (list access :emits-signal) (list access))
  848. value)))))
  849. (puthash key val dbus-registered-objects-table)
  850. ;; Return the object.
  851. (list key (list service path))))
  852. (defun dbus-property-handler (&rest args)
  853. "Default handler for the \"org.freedesktop.DBus.Properties\" interface.
  854. It will be registered for all objects created by `dbus-register-object'."
  855. (let ((bus (dbus-event-bus-name last-input-event))
  856. (service (dbus-event-service-name last-input-event))
  857. (path (dbus-event-path-name last-input-event))
  858. (method (dbus-event-member-name last-input-event))
  859. (interface (car args))
  860. (property (cadr args)))
  861. (cond
  862. ;; "Get" returns a variant.
  863. ((string-equal method "Get")
  864. (let ((entry (gethash (list bus interface property)
  865. dbus-registered-objects-table)))
  866. (when (string-equal path (nth 2 (car entry)))
  867. (list (list :variant (cdar (last (car entry))))))))
  868. ;; "Set" expects a variant.
  869. ((string-equal method "Set")
  870. (let* ((value (caar (cddr args)))
  871. (entry (gethash (list bus interface property)
  872. dbus-registered-objects-table))
  873. ;; The value of the hash table is a list; in case of
  874. ;; properties it contains just one element (UNAME SERVICE
  875. ;; PATH OBJECT). OBJECT is a cons cell of a list, which
  876. ;; contains a list of annotations (like :read,
  877. ;; :read-write, :emits-signal), and the value of the
  878. ;; property.
  879. (object (car (last (car entry)))))
  880. (unless (consp object)
  881. (signal 'dbus-error
  882. (list "Property not registered at path" property path)))
  883. (unless (member :readwrite (car object))
  884. (signal 'dbus-error
  885. (list "Property not writable at path" property path)))
  886. (puthash (list bus interface property)
  887. (list (append (butlast (car entry))
  888. (list (cons (car object) value))))
  889. dbus-registered-objects-table)
  890. ;; Send the "PropertiesChanged" signal.
  891. (when (member :emits-signal (car object))
  892. (dbus-send-signal
  893. bus service path dbus-interface-properties "PropertiesChanged"
  894. (list (list :dict-entry property (list :variant value)))
  895. '(:array)))
  896. ;; Return empty reply.
  897. :ignore))
  898. ;; "GetAll" returns "a{sv}".
  899. ((string-equal method "GetAll")
  900. (let (result)
  901. (maphash
  902. (lambda (key val)
  903. (when (and (equal (butlast key) (list bus interface))
  904. (string-equal path (nth 2 (car val)))
  905. (not (functionp (car (last (car val))))))
  906. (add-to-list
  907. 'result
  908. (list :dict-entry
  909. (car (last key))
  910. (list :variant (cdar (last (car val))))))))
  911. dbus-registered-objects-table)
  912. ;; Return the result, or an empty array.
  913. (list :array (or result '(:signature "{sv}"))))))))
  914. ;; Initialize :system and :session buses. This adds their file
  915. ;; descriptors to input_wait_mask, in order to detect incoming
  916. ;; messages immediately.
  917. (when (featurep 'dbusbind)
  918. (dbus-ignore-errors
  919. (dbus-init-bus :system)
  920. (dbus-init-bus :session)))
  921. (provide 'dbus)
  922. ;;; dbus.el ends here