Spelling fixes.
[emacs.git] / doc / misc / dbus.texi
blob88b068ccd5bc7b876ab7ad4ac8a1938197117c7f
1 \input texinfo   @c -*-texinfo-*-
2 @setfilename ../../info/dbus
3 @c %**start of header
4 @settitle Using of D-Bus
5 @c @setchapternewpage odd
6 @c %**end of header
8 @syncodeindex vr cp
9 @syncodeindex fn cp
11 @copying
12 Copyright @copyright{} 2007-2011 Free Software Foundation, Inc.
14 @quotation
15 Permission is granted to copy, distribute and/or modify this document
16 under the terms of the GNU Free Documentation License, Version 1.3 or
17 any later version published by the Free Software Foundation; with no
18 Invariant Sections, with the Front-Cover texts being ``A GNU Manual'',
19 and with the Back-Cover Texts as in (a) below.  A copy of the license
20 is included in the section entitled ``GNU Free Documentation License''.
22 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
23 modify this GNU manual.  Buying copies from the FSF supports it in
24 developing GNU and promoting software freedom.''
25 @end quotation
26 @end copying
28 @dircategory Emacs lisp libraries
29 @direntry
30 * D-Bus: (dbus).                Using D-Bus in Emacs.
31 @end direntry
33 @contents
36 @node Top, Overview, (dir), (dir)
37 @top D-Bus integration in Emacs
39 This manual documents an API for usage of D-Bus in Emacs.  D-Bus is a
40 message bus system, a simple way for applications to talk to one
41 another.  An overview of D-Bus can be found at
42 @uref{http://dbus.freedesktop.org/}.
44 @ifnottex
45 @insertcopying
46 @end ifnottex
48 @menu
49 * Overview::                    An overview of D-Bus.
50 * Inspection::                  Inspection of D-Bus services.
51 * Type Conversion::             Mapping Lisp types and D-Bus types.
52 * Synchronous Methods::         Calling methods in a blocking way.
53 * Asynchronous Methods::        Calling methods non-blocking.
54 * Receiving Method Calls::      Offering own methods.
55 * Signals::                     Sending and receiving signals.
56 * Alternative Buses::           Alternative buses.
57 * Errors and Events::           Errors and events.
58 * Index::                       Index including concepts, functions, variables.
60 * GNU Free Documentation License:: The license for this documentation.
61 @end menu
64 @node Overview
65 @chapter An overview of D-Bus
66 @cindex overview
68 D-Bus is an inter-process communication mechanism for applications
69 residing on the same host.  The communication is based on
70 @dfn{messages}.  Data in the messages is carried in a structured way,
71 it is not just a byte stream.
73 The communication is connection oriented to two kinds of message
74 buses: a so called @dfn{system bus}, and a @dfn{session bus}.  On a
75 given machine, there is always one single system bus for miscellaneous
76 system-wide communication, like changing of hardware configuration.
77 On the other hand, the session bus is always related to a single
78 user's session.
80 Every client application, which is connected to a bus, registers under
81 a @dfn{unique name} at the bus.  This name is used for identifying the
82 client application.  Such a unique name starts always with a colon,
83 and looks like @samp{:1.42}.
85 Additionally, a client application can register itself to a so called
86 @dfn{known name}, which is a series of identifiers separated by dots,
87 as in @samp{org.gnu.Emacs}.  If several applications register to the
88 same known name, these registrations are queued, and only the first
89 application which has registered for the known name is reachable via
90 this name.  If this application disconnects from the bus, the next
91 queued unique name becomes the owner of this known name.
93 An application can install one or several objects under its name.
94 Such objects are identified by an @dfn{object path}, which looks
95 similar to paths in a filesystem.  An example of such an object path
96 could be @samp{/org/gnu/Emacs/}.
98 Applications might send a request to an object, that means sending a
99 message with some data as input parameters, and receiving a message
100 from that object with the result of this message, the output
101 parameters.  Such a request is called @dfn{method} in D-Bus.
103 The other form of communication are @dfn{signals}.  The underlying
104 message is emitted from an object and will be received by all other
105 applications which have registered for such a signal.
107 All methods and signals an object supports are called @dfn{interface}
108 of the object.  Interfaces are specified under a hierarchical name in
109 D-Bus; an object can support several interfaces.  Such an interface
110 name could be @samp{org.gnu.Emacs.TextEditor} or
111 @samp{org.gnu.Emacs.FileManager}.
114 @node Inspection
115 @chapter Inspection of D-Bus services.
116 @cindex inspection
118 @menu
119 * Bus names::                   Discovering D-Bus names.
120 * Introspection::               Knowing the details of D-Bus services.
121 * Nodes and Interfaces::        Detecting object paths and interfaces.
122 * Methods and Signal::          Applying the functionality.
123 * Properties and Annotations::  What else to know about interfaces.
124 * Arguments and Signatures::    The final details.
125 @end menu
128 @node Bus names
129 @section Bus names.
131 There are several basic functions which inspect the buses for
132 registered names.  Internally they use the basic interface
133 @samp{org.freedesktop.DBus}, which is supported by all objects of a bus.
135 @defun dbus-list-activatable-names &optional bus
136 This function returns the D-Bus service names, which can be activated
137 for @var{bus}.  It must be either the symbol @code{:system} (the
138 default) or the symbol @code{:session}.  An activatable service is
139 described in a service registration file.  Under GNU/Linux, such files
140 are located at @file{/usr/share/dbus-1/system-services/} (for the
141 @code{:system} bus) or @file{/usr/share/dbus-1/services/}.  An
142 activatable service is not necessarily registered at @var{bus} at already.
144 The result is a list of strings, which is @code{nil} when there are no
145 activatable service names at all.  Example:
147 @lisp
148 ;; Check, whether the document viewer can be accessed via D-Bus.
149 (member "org.gnome.evince.Daemon"
150         (dbus-list-activatable-names :session))
151 @end lisp
153 @end defun
155 @defun dbus-list-names bus
156 All service names, which are registered at D-Bus @var{bus}, are
157 returned.  The result is a list of strings, which is @code{nil} when
158 there are no registered service names at all.  Well known names are
159 strings like @samp{org.freedesktop.DBus}.  Names starting with
160 @samp{:} are unique names for services.
162 @var{bus} must be either the symbol @code{:system} or the symbol
163 @code{:session}.
164 @end defun
166 @defun dbus-list-known-names bus
167 Retrieves all registered services which correspond to a known name in @var{bus}.
168 A service has a known name if it doesn't start with @samp{:}.  The
169 result is a list of strings, which is @code{nil} when there are no
170 known names at all.
172 @var{bus} must be either the symbol @code{:system} or the symbol
173 @code{:session}.
174 @end defun
176 @defun dbus-list-queued-owners bus service
177 For a given service, registered at D-Bus @var{bus} under the name
178 @var{service}, all queued unique names are returned.  The result is a
179 list of strings, or @code{nil} when there are no queued names for
180 @var{service} at all.
182 @var{bus} must be either the symbol @code{:system} or the symbol
183 @code{:session}.  @var{service} must be a known service name as
184 string.
185 @end defun
187 @defun dbus-get-name-owner bus service
188 For a given service, registered at D-Bus @var{bus} under the name
189 @var{service}, the unique name of the name owner is returned.  The
190 result is a string, or @code{nil} when there exist no name owner of
191 @var{service}.
193 @var{bus} must be either the symbol @code{:system} or the symbol
194 @code{:session}.  @var{service} must be a known service name as
195 string.
196 @end defun
198 @defun dbus-ping bus service &optional timeout
199 Check whether the service name @var{service} is registered at D-Bus
200 @var{bus}.  @var{service} might not have been started yet, it is
201 autostarted if possible.  The result is either @code{t} or @code{nil}.
203 @var{bus} must be either the symbol @code{:system} or the symbol
204 @code{:session}.  @var{service} must be a string.  @var{timeout}, a
205 nonnegative integer, specifies the maximum number of milliseconds
206 @code{dbus-ping} must return.  The default value is 25,000.  Example:
208 @lisp
209 (message
210    "%s screensaver on board."
211    (cond
212      ((dbus-ping :session "org.gnome.ScreenSaver" 100) "Gnome")
213      ((dbus-ping :session "org.freedesktop.ScreenSaver" 100) "KDE")
214      (t "No")))
215 @end lisp
217 If it shall be checked whether @var{service} is already running
218 without autostarting it, one shall apply
220 @lisp
221 (member service (dbus-list-known-names bus))
222 @end lisp
223 @end defun
225 @defun dbus-get-unique-name bus
226 The unique name, under which Emacs is registered at D-Bus @var{bus},
227 is returned as string.
229 @var{bus} must be either the symbol @code{:system} or the symbol
230 @code{:session}.
231 @end defun
234 @node Introspection
235 @section Knowing the details of D-Bus services.
237 D-Bus services publish their interfaces.  This can be retrieved and
238 analyzed during runtime, in order to understand the used
239 implementation.
241 The resulting introspection data are in XML format.  The root
242 introspection element is always a @code{node} element.  It might have
243 a @code{name} attribute, which denotes the (absolute) object path an
244 interface is introspected.
246 The root @code{node} element may have @code{node} and @code{interface}
247 children.  A child @code{node} element must have a @code{name}
248 attribute, this case it is the relative object path to the root
249 @code{node} element.
251 An @code{interface} element has just one attribute, @code{name}, which
252 is the full name of that interface.  The default interface
253 @samp{org.freedesktop.DBus.Introspectable} is always present.  Example:
255 @example
256 <node name="/org/bluez">
257   <interface name="org.freedesktop.DBus.Introspectable">
258     @dots{}
259   </interface>
260   <interface name="org.bluez.Manager">
261     @dots{}
262   </interface>
263   <interface name="org.bluez.Database">
264     @dots{}
265   </interface>
266   <interface name="org.bluez.Security">
267     @dots{}
268   </interface>
269   <node name="service_audio"/>
270   <node name="service_input"/>
271   <node name="service_network"/>
272   <node name="service_serial"/>
273 </node>
274 @end example
276 Children of an @code{interface} element can be @code{method},
277 @code{signal} and @code{property} elements.  A @code{method} element
278 stands for a D-Bus method of the surrounding interface.  The element
279 itself has a @code{name} attribute, showing the method name.  Children
280 elements @code{arg} stand for the arguments of a method.  Example:
282 @example
283 <method name="ResolveHostName">
284   <arg name="interface" type="i" direction="in"/>
285   <arg name="protocol" type="i" direction="in"/>
286   <arg name="name" type="s" direction="in"/>
287   <arg name="aprotocol" type="i" direction="in"/>
288   <arg name="flags" type="u" direction="in"/>
289   <arg name="interface" type="i" direction="out"/>
290   <arg name="protocol" type="i" direction="out"/>
291   <arg name="name" type="s" direction="out"/>
292   <arg name="aprotocol" type="i" direction="out"/>
293   <arg name="address" type="s" direction="out"/>
294   <arg name="flags" type="u" direction="out"/>
295 </method>
296 @end example
298 @code{arg} elements can have the attributes @code{name}, @code{type}
299 and @code{direction}.  The @code{name} attribute is optional.  The
300 @code{type} attribute stands for the @dfn{signature} of the argument
301 in D-Bus.  For a discussion of D-Bus types and their Lisp
302 representation see @ref{Type Conversion}.@footnote{D-Bus signatures
303 are explained in the D-Bus specification
304 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures}.}
305 The @code{direction} attribute of an @code{arg} element can be only
306 @samp{in} or @samp{out}; in case it is omitted, it defaults to
307 @samp{in}.
309 A @code{signal} element of an @code{interface} has a similar
310 structure.  The @code{direction} attribute of an @code{arg} child
311 element can be only @samp{out} here; which is also the default value.
312 Example:
314 @example
315 <signal name="StateChanged">
316   <arg name="state" type="i"/>
317   <arg name="error" type="s"/>
318 </signal>
319 @end example
321 A @code{property} element has no @code{arg} child
322 element.  It just has the attributes @code{name}, @code{type} and
323 @code{access}, which are all mandatory.  The @code{access} attribute
324 allows the values @samp{readwrite}, @samp{read}, and @samp{write}.
325 Example:
327 @example
328 <property name="Status" type="u" direction="read"/>
329 @end example
331 @code{annotation} elements can be children of @code{interface},
332 @code{method}, @code{signal}, and @code{property} elements.  Unlike
333 properties, which can change their values during lifetime of a D-Bus
334 object, annotations are static.  Often they are used for code
335 generators of D-Bus language bindings.  Example:
337 @example
338 <annotation name="de.berlios.Pinot.GetStatistics" value="pinotDBus"/>
339 @end example
341 Annotations have just @code{name} and @code{value} attributes, both
342 must be strings.
344 @defun dbus-introspect bus service path
345 This function returns all interfaces and sub-nodes of @var{service},
346 registered at object path @var{path} at bus @var{bus}.
348 @var{bus} must be either the symbol @code{:system} or the symbol
349 @code{:session}.  @var{service} must be a known service name, and
350 @var{path} must be a valid object path.  The last two parameters are
351 strings.  The result, the introspection data, is a string in XML
352 format.  Example:
354 @lisp
355 (dbus-introspect
356   :system "org.freedesktop.Hal"
357   "/org/freedesktop/Hal/devices/computer")
359 @result{} "<!DOCTYPE node PUBLIC
360     "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
361     "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
362     <node>
363       <interface name="org.freedesktop.Hal.Device">
364         <method name="GetAllProperties">
365           <arg name="properties" direction="out" type="a@{sv@}"/>
366         </method>
367         @dots{}
368         <signal name="PropertyModified">
369           <arg name="num_updates" type="i"/>
370           <arg name="updates" type="a(sbb)"/>
371         </signal>
372       </interface>
373       @dots{}
374     </node>"
375 @end lisp
377 This example informs us, that the service @samp{org.freedesktop.Hal}
378 at object path @samp{/org/freedesktop/Hal/devices/computer} offers the
379 interface @samp{org.freedesktop.Hal.Device} (and 2 other interfaces
380 not documented here).  This interface contains the method
381 @samp{GetAllProperties}, which needs no input parameters, but returns
382 as output parameter an array of dictionary entries (key-value pairs).
383 Every dictionary entry has a string as key, and a variant as value.
385 The interface offers also a signal, which returns 2 parameters: an
386 integer, and an array consisting of elements which are a struct of a
387 string and 2 boolean values.@footnote{ The interfaces of the service
388 @samp{org.freedesktop.Hal} are described at
389 @uref{http://people.freedesktop.org/~david/hal-spec/hal-spec.html#interfaces}.}
390 @end defun
392 @defun dbus-introspect-xml bus service path
393 This function has the same intention as function
394 @code{dbus-introspect}.  The returned value is a parsed XML tree,
395 which can be used for further analysis.  Example:
397 @lisp
398 (dbus-introspect-xml
399   :session "org.freedesktop.xesam.searcher"
400   "/org/freedesktop/xesam/searcher/main")
402 @result{} (node ((name . "/org/freedesktop/xesam/searcher/main"))
403      (interface ((name . "org.freedesktop.xesam.Search"))
404        (method ((name . "GetHitData"))
405          (arg ((name . "search") (type . "s") (direction . "in")))
406          (arg ((name . "hit_ids") (type . "au") (direction . "in")))
407          (arg ((name . "fields") (type . "as") (direction . "in")))
408          (arg ((name . "hit_data") (type . "aav") (direction . "out")))
409        )
410        @dots{}
411        (signal ((name . "HitsAdded"))
412          (arg ((name . "search") (type . "s")))
413          (arg ((name . "count") (type . "u")))
414        )
415      )
416      @dots{}
417    )
418 @end lisp
419 @end defun
421 @defun dbus-introspect-get-attribute object attribute
422 It returns the @var{attribute} value of a D-Bus introspection
423 @var{object}.  @var{object} can be every subtree of a parsed XML tree
424 as retrieved with @code{dbus-introspect-xml}.  @var{attribute} must be
425 a string according to the attribute names in the D-Bus specification.
426 Example:
428 @lisp
429 (dbus-introspect-get-attribute
430   (dbus-introspect-xml :system "org.freedesktop.SystemToolsBackends"
431     "/org/freedesktop/SystemToolsBackends/UsersConfig")
432   "name")
434 @result{} "/org/freedesktop/SystemToolsBackends/UsersConfig"
435 @end lisp
437 If @var{object} has no @var{attribute}, the function returns
438 @code{nil}.
439 @end defun
442 @node Nodes and Interfaces
443 @section Detecting object paths and interfaces.
445 The first elements, to be introspected for a D-Bus object, are further
446 object paths and interfaces.
448 @defun dbus-introspect-get-node-names bus service path
449 All node names of @var{service} in D-Bus @var{bus} at object path
450 @var{path} are returned as list of strings.  Example:
452 @lisp
453 (dbus-introspect-get-node-names
454   :session "org.gnome.seahorse" "/org/gnome/seahorse")
456 @result{} ("crypto" "keys")
457 @end lisp
459 The node names stand for further object paths of the D-Bus
460 @var{service}, relative to @var{path}.  In the example,
461 @samp{/org/gnome/seahorse/crypto} and @samp{/org/gnome/seahorse/keys}
462 are also object paths of the D-Bus service @samp{org.gnome.seahorse}.
463 @end defun
465 @defun dbus-introspect-get-all-nodes bus service path
466 This function returns all node names of @var{service} in D-Bus
467 @var{bus} at object path @var{path}.  It returns a list of strings
468 with all object paths of @var{service}, starting at @var{path}.
469 Example:
471 @lisp
472 (dbus-introspect-get-all-nodes :session "org.gnome.seahorse" "/")
474 @result{} ("/" "/org" "/org/gnome" "/org/gnome/seahorse"
475     "/org/gnome/seahorse/crypto"
476     "/org/gnome/seahorse/keys"
477     "/org/gnome/seahorse/keys/openpgp"
478     "/org/gnome/seahorse/keys/openpgp/local"
479     "/org/gnome/seahorse/keys/openssh"
480     "/org/gnome/seahorse/keys/openssh/local")
481 @end lisp
482 @end defun
484 @defun dbus-introspect-get-interface-names bus service path
485 There will be returned a list strings of all interface names of
486 @var{service} in D-Bus @var{bus} at object path @var{path}.  This list
487 will contain the default interface @samp{org.freedesktop.DBus.Introspectable}.
489 Another default interface is @samp{org.freedesktop.DBus.Properties}.
490 If present, @code{interface} elements can also have @code{property}
491 children.  Example:
493 @lisp
494 (dbus-introspect-get-interface-names
495   :system "org.freedesktop.Hal"
496   "/org/freedesktop/Hal/devices/computer")
498 @result{} ("org.freedesktop.DBus.Introspectable"
499     "org.freedesktop.Hal.Device"
500     "org.freedesktop.Hal.Device.SystemPowerManagement"
501     "org.freedesktop.Hal.Device.CPUFreq")
502 @end lisp
503 @end defun
505 @defun dbus-introspect-get-interface bus service path interface
506 Return @var{interface} of @var{service} in D-Bus @var{bus} at object
507 path @var{path}.  The return value is an XML element.  @var{interface}
508 must be a string, element of the list returned by
509 @code{dbus-introspect-get-interface-names}.  Example:
511 @lisp
512 (dbus-introspect-get-interface
513   :session "org.freedesktop.xesam.searcher"
514   "/org/freedesktop/xesam/searcher/main"
515   "org.freedesktop.xesam.Search")
517 @result{} (interface ((name . "org.freedesktop.xesam.Search"))
518      (method ((name . "GetHitData"))
519        (arg ((name . "search") (type . "s") (direction . "in")))
520        (arg ((name . "hit_ids") (type . "au") (direction . "in")))
521        (arg ((name . "fields") (type . "as") (direction . "in")))
522        (arg ((name . "hit_data") (type . "aav") (direction . "out")))
523      )
524      @dots{}
525      (signal ((name . "HitsAdded"))
526        (arg ((name . "search") (type . "s")))
527        (arg ((name . "count") (type . "u")))
528      )
529    )
530 @end lisp
531 @end defun
533 @noindent
534 With these functions, it is possible to retrieve all introspection
535 data from a running system:
537 @lisp
538 (with-current-buffer (switch-to-buffer "*introspect*")
539   (erase-buffer)
540   (dolist (service (dbus-list-known-names :session))
541     (dolist (path (dbus-introspect-get-all-nodes :session service "/"))
542       ;; We want to introspect only elements, which have more than
543       ;; the default interface "org.freedesktop.DBus.Introspectable".
544       (when (delete
545              "org.freedesktop.DBus.Introspectable"
546              (dbus-introspect-get-interface-names :session service path))
547         (insert (message "\nservice: \"%s\" path: \"%s\"\n" service path)
548                 (dbus-introspect :session service path))
549         (redisplay t)))))
550 @end lisp
553 @node Methods and Signal
554 @section Applying the functionality.
556 Methods and signals are the communication means to D-Bus.  The
557 following functions return their specifications.
559 @defun dbus-introspect-get-method-names bus service path interface
560 Return a list of strings of all method names of @var{interface} of
561 @var{service} in D-Bus @var{bus} at object path @var{path}.  Example:
563 @lisp
564 (dbus-introspect-get-method-names
565   :session "org.freedesktop.xesam.searcher"
566   "/org/freedesktop/xesam/searcher/main"
567   "org.freedesktop.xesam.Search")
569 @result{} ("GetState" "StartSearch" "GetHitCount" "GetHits" "NewSession"
570     "CloseSession" "GetHitData" "SetProperty" "NewSearch"
571     "GetProperty" "CloseSearch")
572 @end lisp
573 @end defun
575 @defun dbus-introspect-get-method bus service path interface method
576 This function returns @var{method} of @var{interface} as XML element.
577 It must be located at @var{service} in D-Bus @var{bus} at object path
578 @var{path}.  @var{method} must be a string, element of the list
579 returned by @code{dbus-introspect-get-method-names}.  Example:
581 @lisp
582 (dbus-introspect-get-method
583   :session "org.freedesktop.xesam.searcher"
584   "/org/freedesktop/xesam/searcher/main"
585   "org.freedesktop.xesam.Search" "GetHitData")
587 @result{} (method ((name . "GetHitData"))
588      (arg ((name . "search") (type . "s") (direction . "in")))
589      (arg ((name . "hit_ids") (type . "au") (direction . "in")))
590      (arg ((name . "fields") (type . "as") (direction . "in")))
591      (arg ((name . "hit_data") (type . "aav") (direction . "out")))
592    )
593 @end lisp
594 @end defun
596 @defun dbus-introspect-get-signal-names bus service path interface
597 Return a list of strings of all signal names of @var{interface} of
598 @var{service} in D-Bus @var{bus} at object path @var{path}.  Example:
600 @lisp
601 (dbus-introspect-get-signal-names
602   :session "org.freedesktop.xesam.searcher"
603   "/org/freedesktop/xesam/searcher/main"
604   "org.freedesktop.xesam.Search")
606 @result{} ("StateChanged" "SearchDone" "HitsModified"
607     "HitsRemoved" "HitsAdded")
608 @end lisp
609 @end defun
611 @defun dbus-introspect-get-signal bus service path interface signal
612 This function returns @var{signal} of @var{interface} as XML element.
613 It must be located at @var{service} in D-Bus @var{bus} at object path
614 @var{path}.  @var{signal} must be a string, element of the list
615 returned by @code{dbus-introspect-get-signal-names}.  Example:
617 @lisp
618 (dbus-introspect-get-signal
619   :session "org.freedesktop.xesam.searcher"
620   "/org/freedesktop/xesam/searcher/main"
621   "org.freedesktop.xesam.Search" "HitsAdded")
623 @result{} (signal ((name . "HitsAdded"))
624      (arg ((name . "search") (type . "s")))
625      (arg ((name . "count") (type . "u")))
626    )
627 @end lisp
628 @end defun
631 @node Properties and Annotations
632 @section What else to know about interfaces.
634 Interfaces can have properties.  These can be exposed via the
635 @samp{org.freedesktop.DBus.Properties} interface@footnote{See
636 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties}}.
637 That is, properties can be retrieved and changed during lifetime of an
638 element.
640 Annotations, on the other hand, are static values for an element.
641 Often, they are used to instruct generators, how to generate code from
642 the interface for a given language binding.
644 @defun dbus-introspect-get-property-names bus service path interface
645 Return a list of strings with all property names of @var{interface} of
646 @var{service} in D-Bus @var{bus} at object path @var{path}.  Example:
648 @lisp
649 (dbus-introspect-get-property-names
650   :session "org.kde.kded" "/modules/networkstatus"
651   "org.kde.Solid.Networking.Client")
653 @result{} ("Status")
654 @end lisp
656 If an interface declares properties, the corresponding element supports
657 also the @samp{org.freedesktop.DBus.Properties} interface.
658 @end defun
660 @defun dbus-introspect-get-property bus service path interface property
661 This function returns @var{property} of @var{interface} as XML element.
662 It must be located at @var{service} in D-Bus @var{bus} at object path
663 @var{path}.  @var{property} must be a string, element of the list
664 returned by @code{dbus-introspect-get-property-names}.
666 A @var{property} value can be retrieved by the function
667 @code{dbus-introspect-get-attribute}.  Example:
669 @lisp
670 (dbus-introspect-get-property
671   :session "org.kde.kded" "/modules/networkstatus"
672   "org.kde.Solid.Networking.Client" "Status")
674 @result{} (property ((access . "read") (type . "u") (name . "Status")))
676 (dbus-introspect-get-attribute
677   (dbus-introspect-get-property
678     :session "org.kde.kded" "/modules/networkstatus"
679     "org.kde.Solid.Networking.Client" "Status")
680   "access")
682 @result{} "read"
683 @end lisp
684 @end defun
686 @defun dbus-get-property bus service path interface property
687 This function returns the value of @var{property} of @var{interface}.
688 It will be checked at @var{bus}, @var{service}, @var{path}.  The
689 result can be any valid D-Bus value, or @code{nil} if there is no
690 @var{property}.  Example:
692 @lisp
693 (dbus-get-property
694   :session "org.kde.kded" "/modules/networkstatus"
695   "org.kde.Solid.Networking.Client" "Status")
697 @result{} 4
698 @end lisp
699 @end defun
701 @defun dbus-set-property bus service path interface property value
702 Set value of @var{property} of @var{interface} to @var{value}.  It
703 will be checked at @var{bus}, @var{service}, @var{path}.  When the
704 value has been set successful, the result is @var{value}.  Otherwise,
705 @code{nil} is returned.  Example:
707 @lisp
708 (dbus-set-property
709   :session "org.kde.kaccess" "/MainApplication"
710   "com.trolltech.Qt.QApplication" "doubleClickInterval" 500)
712 @result{} 500
713 @end lisp
714 @end defun
716 @defun dbus-get-all-properties bus service path interface
717 This function returns all properties of @var{interface}.  It will be
718 checked at @var{bus}, @var{service}, @var{path}.  The result is a list
719 of cons.  Every cons contains the name of the property, and its value.
720 If there are no properties, @code{nil} is returned.  Example:
722 @lisp
723 (dbus-get-all-properties
724   :session "org.kde.kaccess" "/MainApplication"
725   "com.trolltech.Qt.QApplication")
727 @result{} (("cursorFlashTime" . 1000) ("doubleClickInterval" . 500)
728     ("keyboardInputInterval" . 400) ("wheelScrollLines" . 3)
729     ("globalStrut" 0 0) ("startDragTime" . 500)
730     ("startDragDistance" . 4) ("quitOnLastWindowClosed" . t)
731     ("styleSheet" . ""))
732 @end lisp
733 @end defun
735 @defun dbus-introspect-get-annotation-names bus service path interface &optional name
736 Return a list of all annotation names as list of strings.  If
737 @var{name} is @code{nil}, the annotations are children of
738 @var{interface}, otherwise @var{name} must be a @code{method},
739 @code{signal}, or @code{property} XML element, where the annotations
740 belong to.  Example:
742 @lisp
743 (dbus-introspect-get-annotation-names
744   :session "de.berlios.Pinot" "/de/berlios/Pinot"
745   "de.berlios.Pinot" "GetStatistics")
747 @result{} ("de.berlios.Pinot.GetStatistics")
748 @end lisp
750 Default annotation names@footnote{See
751 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format}}
754 @table @samp
755 @item org.freedesktop.DBus.Deprecated
756 Whether or not the entity is deprecated; defaults to @code{nil}
758 @item org.freedesktop.DBus.GLib.CSymbol
759 The C symbol; may be used for @code{methods} and @code{interfaces}
761 @item org.freedesktop.DBus.Method.NoReply
762 If set, don't expect a reply to the @code{method} call; defaults to @code{nil}
763 @end table
764 @end defun
766 @defun dbus-introspect-get-annotation bus service path interface name annotation
767 Return annotation @var{ANNOTATION} as XML object.  If @var{name} is
768 @code{nil}, @var{ANNOTATION} is a child of @var{interface}, otherwise
769 @var{name} must be the name of a @code{method}, @code{signal}, or
770 @code{property} XML element, where the @var{ANNOTATION} belongs to.
772 An attribute value can be retrieved by
773 @code{dbus-introspect-get-attribute}.  Example:
775 @lisp
776 (dbus-introspect-get-annotation
777   :session "de.berlios.Pinot" "/de/berlios/Pinot"
778   "de.berlios.Pinot" "GetStatistics"
779   "de.berlios.Pinot.GetStatistics")
781 @result{} (annotation ((name . "de.berlios.Pinot.GetStatistics")
782                 (value . "pinotDBus")))
784 (dbus-introspect-get-attribute
785   (dbus-introspect-get-annotation
786     :session "de.berlios.Pinot" "/de/berlios/Pinot"
787     "de.berlios.Pinot" "GetStatistics"
788     "de.berlios.Pinot.GetStatistics")
789   "value")
791 @result{} "pinotDBus"
792 @end lisp
793 @end defun
796 @node Arguments and Signatures
797 @section The final details.
799 Methods and signals have arguments.  They are described in the
800 @code{arg} XML elements.
802 @defun dbus-introspect-get-argument-names bus service path interface name
803 Return a list of all argument names as list of strings.  @var{name}
804 must be a @code{method} or @code{signal} XML element.  Example:
806 @lisp
807 (dbus-introspect-get-argument-names
808   :session "org.freedesktop.xesam.searcher"
809   "/org/freedesktop/xesam/searcher/main"
810   "org.freedesktop.xesam.Search" "GetHitData")
812 @result{} ("search" "hit_ids" "fields" "hit_data")
813 @end lisp
815 Argument names are optional; the function can return @code{nil}
816 therefore, even if the method or signal has arguments.
817 @end defun
819 @defun dbus-introspect-get-argument bus service path interface name arg
820 Return argument @var{ARG} as XML object.  @var{name}
821 must be a @code{method} or @code{signal} XML element.  Example:
823 @lisp
824 (dbus-introspect-get-argument
825   :session "org.freedesktop.xesam.searcher"
826   "/org/freedesktop/xesam/searcher/main"
827   "org.freedesktop.xesam.Search" "GetHitData" "search")
829 @result{} (arg ((name . "search") (type . "s") (direction . "in")))
830 @end lisp
831 @end defun
833 @defun dbus-introspect-get-signature bus service path interface name &optional direction
834 Return signature of a @code{method} or @code{signal}, represented by
835 @var{name}, as string.
837 If @var{name} is a @code{method}, @var{direction} can be either
838 @samp{in} or @samp{out}.  If @var{direction} is @code{nil}, @samp{in}
839 is assumed.
841 If @var{name} is a @code{signal}, and @var{direction} is
842 non-@code{nil}, @var{direction} must be @samp{out}.  Example:
844 @lisp
845 (dbus-introspect-get-signature
846   :session "org.freedesktop.xesam.searcher"
847   "/org/freedesktop/xesam/searcher/main"
848   "org.freedesktop.xesam.Search" "GetHitData" "in")
850 @result{} "sauas"
852 (dbus-introspect-get-signature
853   :session "org.freedesktop.xesam.searcher"
854   "/org/freedesktop/xesam/searcher/main"
855   "org.freedesktop.xesam.Search" "HitsAdded")
857 @result{} "su"
858 @end lisp
859 @end defun
862 @node Type Conversion
863 @chapter Mapping Lisp types and D-Bus types.
864 @cindex type conversion
866 D-Bus method calls and signals accept usually several arguments as
867 parameters, either as input parameter, or as output parameter.  Every
868 argument belongs to a D-Bus type.
870 Such arguments must be mapped between the value encoded as a D-Bus
871 type, and the corresponding type of Lisp objects.  The mapping is
872 applied Lisp object @expansion{} D-Bus type for input parameters, and
873 D-Bus type @expansion{} Lisp object for output parameters.
876 @section Input parameters.
878 Input parameters for D-Bus methods and signals occur as arguments of a
879 Lisp function call.  The following mapping to D-Bus types is
880 applied, when the corresponding D-Bus message is created:
882 @example
883 @multitable {negative integer} {@expansion{}} {DBUS_TYPE_BOOLEAN}
884 @item Lisp type               @tab              @tab D-Bus type
885 @item
886 @item @code{t} and @code{nil} @tab @expansion{} @tab DBUS_TYPE_BOOLEAN
887 @item natural number          @tab @expansion{} @tab DBUS_TYPE_UINT32
888 @item negative integer        @tab @expansion{} @tab DBUS_TYPE_INT32
889 @item float                   @tab @expansion{} @tab DBUS_TYPE_DOUBLE
890 @item string                  @tab @expansion{} @tab DBUS_TYPE_STRING
891 @item list                    @tab @expansion{} @tab DBUS_TYPE_ARRAY
892 @end multitable
893 @end example
895 Other Lisp objects, like symbols or hash tables, are not accepted as
896 input parameters.
898 If it is necessary to use another D-Bus type, a corresponding type
899 symbol can be prepended to the corresponding Lisp object.  Basic D-Bus
900 types are represented by the type symbols @code{:byte},
901 @code{:boolean}, @code{:int16}, @code{:uint16}, @code{:int32},
902 @code{:uint32}, @code{:int64}, @code{:uint64}, @code{:double},
903 @code{:string}, @code{:object-path}, @code{:signature} and
904 @code{:unix-fd}.
906 @noindent
907 Example:
909 @lisp
910 (dbus-call-method @dots{} @var{NAT-NUMBER} @var{STRING})
911 @end lisp
913 is equivalent to
915 @lisp
916 (dbus-call-method @dots{} :uint32 @var{NAT-NUMBER} :string @var{STRING})
917 @end lisp
919 but different to
921 @lisp
922 (dbus-call-method @dots{} :int32 @var{NAT-NUMBER} :signature @var{STRING})
923 @end lisp
925 The value for a byte D-Bus type can be any integer in the range 0
926 through 255.  If a character is used as argument, modifiers
927 represented outside this range are stripped of.  For example,
928 @code{:byte ?x} is equal to @code{:byte ?\M-x}, but it is not equal to
929 @code{:byte ?\C-x} or @code{:byte ?\M-\C-x}.
931 A D-Bus compound type is always represented as a list.  The @sc{car}
932 of this list can be the type symbol @code{:array}, @code{:variant},
933 @code{:struct} or @code{:dict-entry}, which would result in a
934 corresponding D-Bus container.  @code{:array} is optional, because
935 this is the default compound D-Bus type for a list.
937 The objects being elements of the list are checked according to the
938 D-Bus compound type rules.
940 @itemize
941 @item An array must contain only elements of the same D-Bus type.  It
942 can be empty.
944 @item A variant must contain only one single element.
946 @item A dictionary entry must be element of an array, and it must
947 contain only a key-value pair of two elements, with a basic D-Bus type
948 key.
950 @item There is no restriction for structs.
951 @end itemize
953 If an empty array needs an element D-Bus type other than string, it
954 can contain exactly one element of D-Bus type @code{:signature}.  The
955 value of this element (a string) is used as the signature of the
956 elements of this array.  Example:
958 @lisp
959 (dbus-call-method
960   :session "org.freedesktop.Notifications"
961   "/org/freedesktop/Notifications"
962   "org.freedesktop.Notifications" "Notify"
963   "GNU Emacs"                 ;; Application name.
964   0                           ;; No replacement of other notifications.
965   ""                          ;; No icon.
966   "Notification summary"      ;; Summary.
967   (format                     ;; Body.
968     "This is a test notification, raised from %s" (emacs-version))
969   '(:array)                   ;; No actions (empty array of strings).
970   '(:array :signature "@{sv@}") ;; No hints
971                               ;; (empty array of dictionary entries).
972   :int32 -1)                 ;; Default timeout.
974 @result{} 3
975 @end lisp
977 @defun dbus-string-to-byte-array string
978 Sometimes, D-Bus methods require as input parameter an array of bytes,
979 instead of a string.  If it is guaranteed, that @var{string} is an
980 UTF8 string, this function performs the conversion.  Example:
982 @lisp
983 (dbus-string-to-byte-array "/etc/hosts")
985 @result{} (:array :byte 47 :byte 101 :byte 116 :byte 99 :byte 47
986            :byte 104 :byte 111 :byte 115 :byte 116 :byte 115)
987 @end lisp
988 @end defun
990 @defun dbus-escape-as-identifier string
991 Escape an arbitrary @var{string} so it follows the rules for a C
992 identifier.  The escaped string can be used as object path component,
993 interface element component, bus name component or member name in
994 D-Bus.
996 The escaping consists of replacing all non-alphanumerics, and the
997 first character if it's a digit, with an underscore and two
998 lower-case hex digits.  As a special case, "" is escaped to
999 "_".  Example:
1001 @lisp
1002 (dbus-escape-as-identifier "0123abc_xyz\x01\xff")
1004 @result{} "_30123abc_5fxyz_01_ff"
1005 @end lisp
1006 @end defun
1009 @section Output parameters.
1011 Output parameters of D-Bus methods and signals are mapped to Lisp
1012 objects.
1014 @example
1015 @multitable {DBUS_TYPE_OBJECT_PATH} {@expansion{}} {natural number or float}
1016 @item D-Bus type            @tab              @tab Lisp type
1017 @item
1018 @item DBUS_TYPE_BOOLEAN     @tab @expansion{} @tab @code{t} or @code{nil}
1019 @item DBUS_TYPE_BYTE        @tab @expansion{} @tab natural number
1020 @item DBUS_TYPE_UINT16      @tab @expansion{} @tab natural number
1021 @item DBUS_TYPE_INT16       @tab @expansion{} @tab integer
1022 @item DBUS_TYPE_UINT32      @tab @expansion{} @tab natural number or float
1023 @item DBUS_TYPE_UNIX_FD     @tab @expansion{} @tab natural number or float
1024 @item DBUS_TYPE_INT32       @tab @expansion{} @tab integer or float
1025 @item DBUS_TYPE_UINT64      @tab @expansion{} @tab natural number or float
1026 @item DBUS_TYPE_INT64       @tab @expansion{} @tab integer or float
1027 @item DBUS_TYPE_DOUBLE      @tab @expansion{} @tab float
1028 @item DBUS_TYPE_STRING      @tab @expansion{} @tab string
1029 @item DBUS_TYPE_OBJECT_PATH @tab @expansion{} @tab string
1030 @item DBUS_TYPE_SIGNATURE   @tab @expansion{} @tab string
1031 @item DBUS_TYPE_ARRAY       @tab @expansion{} @tab list
1032 @item DBUS_TYPE_VARIANT     @tab @expansion{} @tab list
1033 @item DBUS_TYPE_STRUCT      @tab @expansion{} @tab list
1034 @item DBUS_TYPE_DICT_ENTRY  @tab @expansion{} @tab list
1035 @end multitable
1036 @end example
1038 A float object in case of @code{DBUS_TYPE_UINT32},
1039 @code{DBUS_TYPE_INT32}, @code{DBUS_TYPE_UINT64},
1040 @code{DBUS_TYPE_INT64} and @code{DBUS_TYPE_UNIX_FD} is returned, when
1041 the C value exceeds the Emacs number size range.
1043 The resulting list of the last 4 D-Bus compound types contains as
1044 elements the elements of the D-Bus container, mapped according to the
1045 same rules.
1047 The signal @code{PropertyModified}, discussed as example in
1048 @ref{Inspection}, would offer as Lisp data the following object
1049 (@var{BOOL} stands here for either @code{nil} or @code{t}):
1051 @lisp
1052 (@var{INTEGER} ((@var{STRING} @var{BOOL} @var{BOOL}) (@var{STRING} @var{BOOL} @var{BOOL}) @dots{}))
1053 @end lisp
1055 @defun dbus-byte-array-to-string byte-array
1056 If a D-Bus method or signal returns an array of bytes, which are known
1057 to represent an UTF8 string, this function converts @var{byte-array}
1058 to the corresponding string.  Example:
1060 @lisp
1061 (dbus-byte-array-to-string '(47 101 116 99 47 104 111 115 116 115))
1063 @result{} "/etc/hosts"
1064 @end lisp
1065 @end defun
1067 @defun dbus-unescape-from-identifier string
1068 Retrieve the original string from the encoded @var{string}.
1069 @var{string} must have been coded with
1070 @code{dbus-escape-as-identifier}.  Example:
1072 @lisp
1073 (dbus-unescape-from-identifier "_30123abc_5fxyz_01_ff")
1075 @ifinfo
1076 @result{} "0123abc_xyz^Aÿ"
1077 @end ifinfo
1078 @ifnotinfo
1079 @result{} "0123abc_xyz^A@"y"
1080 @end ifnotinfo
1081 @end lisp
1082 @end defun
1085 @node Synchronous Methods
1086 @chapter Calling methods in a blocking way.
1087 @cindex method calls, synchronous
1088 @cindex synchronous method calls
1090 Methods can be called synchronously (@dfn{blocking}) or asynchronously
1091 (@dfn{non-blocking}).
1093 At D-Bus level, a method call consist of two messages: one message
1094 which carries the input parameters to the object owning the method to
1095 be called, and a reply message returning the resulting output
1096 parameters from the object.
1098 @defun dbus-call-method bus service path interface method &optional :timeout timeout &rest args
1099 This function calls @var{method} on the D-Bus @var{bus}.  @var{bus} is
1100 either the symbol @code{:system} or the symbol @code{:session}.
1102 @var{service} is the D-Bus service name to be used.  @var{path} is the
1103 D-Bus object path, @var{service} is registered at.  @var{interface} is
1104 an interface offered by @var{service}.  It must provide @var{method}.
1106 If the parameter @code{:timeout} is given, the following integer
1107 @var{timeout} specifies the maximum number of milliseconds the method
1108 call must return.  The default value is 25,000.  If the method call
1109 doesn't return in time, a D-Bus error is raised (@pxref{Errors and
1110 Events}).
1112 All other arguments args are passed to @var{method} as arguments.
1113 They are converted into D-Bus types as described in @ref{Type
1114 Conversion}.
1116 The function returns the resulting values of @var{method} as a list of
1117 Lisp objects, according to the type conversion rules described in
1118 @ref{Type Conversion}.  Example:
1120 @lisp
1121 (dbus-call-method
1122   :session "org.gnome.seahorse" "/org/gnome/seahorse/keys/openpgp"
1123   "org.gnome.seahorse.Keys" "GetKeyField"
1124   "openpgp:657984B8C7A966DD" "simple-name")
1126 @result{} (t ("Philip R. Zimmermann"))
1127 @end lisp
1129 If the result of the method call is just one value, the converted Lisp
1130 object is returned instead of a list containing this single Lisp
1131 object.  Example:
1133 @lisp
1134 (dbus-call-method
1135   :system "org.freedesktop.Hal"
1136   "/org/freedesktop/Hal/devices/computer"
1137   "org.freedesktop.Hal.Device" "GetPropertyString"
1138   "system.kernel.machine")
1140 @result{} "i686"
1141 @end lisp
1143 With the @code{dbus-introspect} function it is possible to explore the
1144 interfaces of @samp{org.freedesktop.Hal} service. It offers the
1145 interfaces @samp{org.freedesktop.Hal.Manager} for the object at the
1146 path @samp{/org/freedesktop/Hal/Manager} as well as the interface
1147 @samp{org.freedesktop.Hal.Device} for all objects prefixed with the
1148 path @samp{/org/freedesktop/Hal/devices}.  With the methods
1149 @samp{GetAllDevices} and @samp{GetAllProperties}, it is simple to
1150 emulate the @code{lshal} command on GNU/Linux systems:
1152 @lisp
1153 (dolist (device
1154           (dbus-call-method
1155             :system "org.freedesktop.Hal"
1156             "/org/freedesktop/Hal/Manager"
1157             "org.freedesktop.Hal.Manager" "GetAllDevices"))
1158   (message "\nudi = %s" device)
1159   (dolist (properties
1160             (dbus-call-method
1161               :system "org.freedesktop.Hal" device
1162               "org.freedesktop.Hal.Device" "GetAllProperties"))
1163     (message "  %s = %S"
1164              (car properties) (or (caar (cdr properties)) ""))))
1166 @print{} "udi = /org/freedesktop/Hal/devices/computer
1167       info.addons = (\"hald-addon-acpi\")
1168       info.bus = \"unknown\"
1169       info.product = \"Computer\"
1170       info.subsystem = \"unknown\"
1171       info.udi = \"/org/freedesktop/Hal/devices/computer\"
1172       linux.sysfs_path_device = \"(none)\"
1173       power_management.acpi.linux.version = \"20051216\"
1174       power_management.can_suspend_to_disk = t
1175       power_management.can_suspend_to_ram = \"\"
1176       power_management.type = \"acpi\"
1177       smbios.bios.release_date = \"11/07/2001\"
1178       system.chassis.manufacturer = \"COMPAL\"
1179       system.chassis.type = \"Notebook\"
1180       system.firmware.release_date = \"03/19/2005\"
1181       @dots{}"
1182 @end lisp
1183 @end defun
1185 @defun dbus-call-method-non-blocking bus service path interface method &optional :timeout timeout &rest args
1186 Call @var{method} on the D-Bus @var{bus}, but don't block the event queue.
1187 This is necessary for communicating to registered D-Bus methods,
1188 which are running in the same Emacs process.
1190 The arguments are the same as in @code{dbus-call-method}.  Example:
1192 @lisp
1193 (dbus-call-method-non-blocking
1194   :system "org.freedesktop.Hal"
1195   "/org/freedesktop/Hal/devices/computer"
1196   "org.freedesktop.Hal.Device" "GetPropertyString"
1197   "system.kernel.machine")
1199 @result{} "i686"
1200 @end lisp
1201 @end defun
1204 @node Asynchronous Methods
1205 @chapter Calling methods non-blocking.
1206 @cindex method calls, asynchronous
1207 @cindex asynchronous method calls
1209 @defun dbus-call-method-asynchronously bus service path interface method handler &optional :timeout timeout &rest args
1210 This function calls @var{method} on the D-Bus @var{bus}
1211 asynchronously.  @var{bus} is either the symbol @code{:system} or the
1212 symbol @code{:session}.
1214 @var{service} is the D-Bus service name to be used.  @var{path} is the
1215 D-Bus object path, @var{service} is registered at.  @var{interface} is
1216 an interface offered by @var{service}.  It must provide @var{method}.
1218 @var{handler} is a Lisp function, which is called when the
1219 corresponding return message has arrived.  If @var{handler} is
1220 @code{nil}, no return message will be expected.
1222 If the parameter @code{:timeout} is given, the following integer
1223 @var{timeout} specifies the maximum number of milliseconds a reply
1224 message must arrive.  The default value is 25,000.  If there is no
1225 reply message in time, a D-Bus error is raised (@pxref{Errors and
1226 Events}).
1228 All other arguments args are passed to @var{method} as arguments.
1229 They are converted into D-Bus types as described in @ref{Type
1230 Conversion}.
1232 Unless @var{handler} is @code{nil}, the function returns a key into
1233 the hash table @code{dbus-registered-objects-table}.  The
1234 corresponding entry in the hash table is removed, when the return
1235 message has been arrived, and @var{handler} is called.  Example:
1237 @lisp
1238 (dbus-call-method-asynchronously
1239   :system "org.freedesktop.Hal"
1240   "/org/freedesktop/Hal/devices/computer"
1241   "org.freedesktop.Hal.Device" "GetPropertyString" 'message
1242   "system.kernel.machine")
1244 @result{} (:system 2)
1246 @print{} i686
1247 @end lisp
1248 @end defun
1251 @node Receiving Method Calls
1252 @chapter Offering own methods.
1253 @cindex method calls, returning
1254 @cindex returning method calls
1256 In order to register methods on the D-Bus, Emacs has to request a well
1257 known name on the D-Bus under which it will be available for other
1258 clients.  Names on the D-Bus can be registered and unregistered using
1259 the following functions:
1261 @defun dbus-register-service bus service &rest flags
1262 Register the known name @var{service} on D-Bus @var{bus}.
1264 @var{bus} is either the symbol @code{:system} or the symbol
1265 @code{:session}.
1267 @var{service} is the service name to be registered on the D-Bus.  It
1268 must be a known name.
1270 @var{flags} is a subset of the following keywords:
1272 @itemize
1273 @item @code{:allow-replacement}: Allow another service to become the primary
1274 owner if requested.
1276 @item @code{:replace-existing}: Request to replace the current primary owner.
1278 @item @code{:do-not-queue}: If we can not become the primary owner do not
1279 place us in the queue.
1280 @end itemize
1282 One of the following keywords is returned:
1284 @itemize
1286 @item @code{:primary-owner}: We have become the primary owner of the name
1287 @var{service}.
1289 @item @code{:in-queue}: We could not become the primary owner and
1290 have been placed in the queue.
1292 @item @code{:exists}: We already are in the queue.
1294 @item @code{:already-owner}: We already are the primary
1295 owner.
1296 @end itemize
1297 @end defun
1299 @defun dbus-unregister-service bus service
1300 Unregister all objects from D-Bus @var{bus}, registered by Emacs for
1301 @var{service}.
1303 @var{bus} is either the symbol @code{:system} or the symbol
1304 @code{:session}.
1306 @var{service} is the D-Bus service name of the D-Bus.  It must be a
1307 known name.  Emacs releases its association to @var{service} from
1308 D-Bus.
1310 One of the following keywords is returned:
1312 @itemize
1313 @item @code{:released}: We successfully released the name @var{service}.
1314 @item @code{:non-existent}: The name @var{service} does not exist on the bus.
1315 @item @code{:not-owner}: We are not an owner of the name @var{service}.
1316 @end itemize
1317 @end defun
1319 When a name has been chosen, Emacs can offer own methods, which can be
1320 called by other applications.  These methods could be an
1321 implementation of an interface of a well known service, like
1322 @samp{org.freedesktop.TextEditor}.
1324 It could be also an implementation of an own interface.  In this case,
1325 the service name must be @samp{org.gnu.Emacs}.  The object path shall
1326 begin with @samp{/org/gnu/Emacs/@strong{Application}/}, and the
1327 interface name shall be @code{org.gnu.Emacs.@strong{Application}}.
1328 @samp{@strong{Application}} is the name of the application which
1329 provides the interface.
1331 @deffn Constant dbus-service-emacs
1332 The well known service name of Emacs.
1333 @end deffn
1335 @deffn Constant dbus-path-emacs
1336 The object path head "/org/gnu/Emacs" used by Emacs.  All object
1337 paths, used by offered methods or signals, shall start with this
1338 string.
1339 @end deffn
1341 @defun dbus-register-method bus service path interface method handler dont-register-service
1342 With this function, an application registers @var{method} on the D-Bus
1343 @var{bus}.
1345 @var{bus} is either the symbol @code{:system} or the symbol
1346 @code{:session}.
1348 @var{service} is the D-Bus service name of the D-Bus object
1349 @var{method} is registered for.  It must be a known name (See
1350 discussion of @var{dont-register-service} below).
1352 @var{path} is the D-Bus object path @var{service} is registered (See
1353 discussion of @var{dont-register-service} below).
1355 @var{interface} is the interface offered by @var{service}.  It must
1356 provide @var{method}.
1358 @var{handler} is a Lisp function to be called when a @var{method} call
1359 is received.  It must accept as arguments the input arguments of
1360 @var{method}.  @var{handler} should return a list, whose elements are
1361 to be used as arguments for the reply message of @var{method}.  This
1362 list can be composed like the input parameters in @ref{Type
1363 Conversion}.
1365 If @var{handler} wants to return just one Lisp object and it is not a
1366 cons cell, @var{handler} can return this object directly, instead of
1367 returning a list containing the object.
1369 In case @var{handler} shall return a reply message with an empty
1370 argument list, @var{handler} must return the symbol @code{:ignore}.
1372 When @var{dont-register-service} is non-@code{nil}, the known name
1373 @var{service} is not registered.  This means that other D-Bus clients
1374 have no way of noticing the newly registered method.  When interfaces
1375 are constructed incrementally by adding single methods or properties
1376 at a time, @var{dont-register-service} can be used to prevent other
1377 clients from discovering the still incomplete interface.
1379 The default D-Bus timeout when waiting for a message reply is 25
1380 seconds.  This value could be even smaller, depending on the calling
1381 client.  Therefore, @var{handler} shall not last longer than
1382 absolutely necessary.
1384 @code{dbus-register-method} returns a Lisp object, which can be used
1385 as argument in @code{dbus-unregister-object} for removing the
1386 registration for @var{method}.  Example:
1388 @lisp
1389 (defun my-dbus-method-handler (filename)
1390   (let (result)
1391     (if (find-file filename)
1392         (setq result '(:boolean t))
1393       (setq result '(:boolean nil)))
1394     result))
1396 @result{} my-dbus-method-handler
1398 (dbus-register-method
1399   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1400   "org.freedesktop.TextEditor" "OpenFile"
1401   'my-dbus-method-handler)
1403 @result{} ((:session "org.freedesktop.TextEditor" "OpenFile")
1404     ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1405      my-dbus-method-handler))
1406 @end lisp
1408 If you invoke the method @samp{org.freedesktop.TextEditor.OpenFile}
1409 from another D-Bus application with a filename as parameter, the file
1410 is opened in Emacs, and the method returns either @var{true} or
1411 @var{false}, indicating the success of the method.  As test tool one
1412 could use the command line tool @code{dbus-send} in a shell:
1414 @example
1415 # dbus-send --session --print-reply \
1416     --dest="org.freedesktop.TextEditor" \
1417     "/org/freedesktop/TextEditor" \
1418     "org.freedesktop.TextEditor.OpenFile" string:"/etc/hosts"
1420 @print{} method return sender=:1.22 -> dest=:1.23 reply_serial=2
1421       boolean true
1422 @end example
1424 You can indicate an error by raising the Emacs signal
1425 @code{dbus-error}.  The handler above could be changed like this:
1427 @lisp
1428 (defun my-dbus-method-handler (&rest args)
1429   (unless (and (= (length args) 1) (stringp (car args)))
1430     (signal 'dbus-error (list (format "Wrong argument list: %S" args))))
1431   (condition-case err
1432       (find-file (car args))
1433     (error (signal 'dbus-error (cdr err))))
1434   t)
1436 @result{} my-dbus-method-handler
1437 @end lisp
1439 The test runs then
1441 @example
1442 # dbus-send --session --print-reply \
1443     --dest="org.freedesktop.TextEditor" \
1444     "/org/freedesktop/TextEditor" \
1445     "org.freedesktop.TextEditor.OpenFile" \
1446     string:"/etc/hosts" string:"/etc/passwd"
1448 @print{} Error org.freedesktop.DBus.Error.Failed:
1449    Wrong argument list: ("/etc/hosts" "/etc/passwd")
1450 @end example
1451 @end defun
1453 @defun dbus-register-property bus service path interface property access value &optional emits-signal dont-register-service
1454 With this function, an application declares a @var{property} on the D-Bus
1455 @var{bus}.
1457 @var{bus} is either the symbol @code{:system} or the symbol
1458 @code{:session}.
1460 @var{service} is the D-Bus service name of the D-Bus.  It must be a
1461 known name.
1463 @var{path} is the D-Bus object path @var{service} is registered (See
1464 discussion of @var{dont-register-service} below).
1466 @var{interface} is the name of the interface used at @var{path},
1467 @var{property} is the name of the property of @var{interface}.
1469 @var{access} indicates, whether the property can be changed by other
1470 services via D-Bus.  It must be either the symbol @code{:read} or
1471 @code{:readwrite}.  @var{value} is the initial value of the property,
1472 it can be of any valid type (see @code{dbus-call-method} for details).
1474 If @var{property} already exists on @var{path}, it will be
1475 overwritten.  For properties with access type @code{:read} this is the
1476 only way to change their values.  Properties with access type
1477 @code{:readwrite} can be changed by @code{dbus-set-property}.
1479 The interface @samp{org.freedesktop.DBus.Properties} is added to
1480 @var{path}, including a default handler for the @samp{Get},
1481 @samp{GetAll} and @samp{Set} methods of this interface.  When
1482 @var{emits-signal} is non-@code{nil}, the signal
1483 @samp{PropertiesChanged} is sent when the property is changed by
1484 @code{dbus-set-property}.
1486 When @var{dont-register-service} is non-@code{nil}, the known name
1487 @var{service} is not registered.  This means that other D-Bus clients
1488 have no way of noticing the newly registered method.  When interfaces
1489 are constructed incrementally by adding single methods or properties
1490 at a time, @var{dont-register-service} can be used to prevent other
1491 clients from discovering the still incomplete interface.
1493 @noindent Example:
1495 @lisp
1496 (dbus-register-property
1497   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1498   "org.freedesktop.TextEditor" "name" :read "GNU Emacs")
1500 @result{} ((:session "org.freedesktop.TextEditor" "name")
1501     ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
1503 (dbus-register-property
1504   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1505   "org.freedesktop.TextEditor" "version" :readwrite emacs-version t)
1507 @result{} ((:session "org.freedesktop.TextEditor" "version")
1508     ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
1509 @end lisp
1511 Other D-Bus applications can read the property via the default methods
1512 @samp{org.freedesktop.DBus.Properties.Get} and
1513 @samp{org.freedesktop.DBus.Properties.GetAll}.  Testing is also
1514 possible via the command line tool @code{dbus-send} in a shell:
1516 @example
1517 # dbus-send --session --print-reply \
1518     --dest="org.freedesktop.TextEditor" \
1519     "/org/freedesktop/TextEditor" \
1520     "org.freedesktop.DBus.Properties.GetAll" \
1521     string:"org.freedesktop.TextEditor"
1523 @print{} method return sender=:1.22 -> dest=:1.23 reply_serial=3
1524       array [
1525          dict entry(
1526             string "name"
1527             variant             string "GNU Emacs"
1528          )
1529          dict entry(
1530             string "version"
1531             variant             string "23.1.50.5"
1532          )
1533       ]
1534 @end example
1536 It is also possible, to apply the @code{dbus-get-property},
1537 @code{dbus-get-all-properties} and @code{dbus-set-property} functions
1538 (@pxref{Properties and Annotations}).
1540 @lisp
1541 (dbus-set-property
1542   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1543   "org.freedesktop.TextEditor" "version" "23.1.50")
1545 @result{} "23.1.50"
1547 (dbus-get-property
1548   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1549   "org.freedesktop.TextEditor" "version")
1551 @result{} "23.1.50"
1552 @end lisp
1553 @end defun
1555 @defun dbus-unregister-object object
1556 Unregister @var{object} from the D-Bus.  @var{object} must be the
1557 result of a preceding @code{dbus-register-method},
1558 @code{dbus-register-property} or @code{dbus-register-signal} call
1559 (@pxref{Signals}).  It returns @code{t} if @var{object} has been
1560 unregistered, @code{nil} otherwise.
1562 When @var{object} identifies the last method or property, which is
1563 registered for the respective service, Emacs releases its association
1564 to the service from D-Bus.
1565 @end defun
1568 @node Signals
1569 @chapter Sending and receiving signals.
1570 @cindex signals
1572 Signals are broadcast messages.  They carry input parameters, which
1573 are received by all objects which have registered for such a signal.
1575 @defun dbus-send-signal bus service path interface signal &rest args
1576 This function is similar to @code{dbus-call-method}.  The difference
1577 is, that there are no returning output parameters.
1579 The function emits @var{signal} on the D-Bus @var{bus}.  @var{bus} is
1580 either the symbol @code{:system} or the symbol @code{:session}.  It
1581 doesn't matter whether another object has registered for @var{signal}.
1583 @var{service} is the D-Bus service name of the object the signal is
1584 emitted from.  @var{path} is the corresponding D-Bus object path,
1585 @var{service} is registered at.  @var{interface} is an interface
1586 offered by @var{service}.  It must provide @var{signal}.
1588 All other arguments args are passed to @var{signal} as arguments.
1589 They are converted into D-Bus types as described in @ref{Type
1590 Conversion}.  Example:
1592 @lisp
1593 (dbus-send-signal
1594   :session dbus-service-emacs dbus-path-emacs
1595   (concat dbus-service-emacs ".FileManager") "FileModified"
1596   "/home/albinus/.emacs")
1597 @end lisp
1598 @end defun
1600 @defun dbus-register-signal bus service path interface signal handler &rest args
1601 With this function, an application registers for @var{signal} on the
1602 D-Bus @var{bus}.
1604 @var{bus} is either the symbol @code{:system} or the symbol
1605 @code{:session}.
1607 @var{service} is the D-Bus service name used by the sending D-Bus
1608 object.  It can be either a known name or the unique name of the D-Bus
1609 object sending the signal.  In case of a unique name, signals won't be
1610 received any longer once the object owning this unique name has
1611 disappeared, and a new queued object has replaced it.
1613 When @var{service} is @code{nil}, related signals from all D-Bus
1614 objects shall be accepted.
1616 @var{path} is the corresponding D-Bus object path, @var{service} is
1617 registered at.  It can also be @code{nil} if the path name of incoming
1618 signals shall not be checked.
1620 @var{interface} is an interface offered by @var{service}.  It must
1621 provide @var{signal}.
1623 @var{handler} is a Lisp function to be called when the @var{signal} is
1624 received.  It must accept as arguments the output parameters
1625 @var{signal} is sending.
1627 All other arguments @var{args}, if specified, must be strings.  They
1628 stand for the respective arguments of @var{signal} in their order, and
1629 are used for filtering as well.  A @code{nil} argument might be used
1630 to preserve the order.
1632 @code{dbus-register-signal} returns a Lisp object, which can be used
1633 as argument in @code{dbus-unregister-object} for removing the
1634 registration for @var{signal}.  Example:
1636 @lisp
1637 (defun my-dbus-signal-handler (device)
1638   (message "Device %s added" device))
1640 @result{} my-dbus-signal-handler
1642 (dbus-register-signal
1643   :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1644   "org.freedesktop.Hal.Manager" "DeviceAdded"
1645   'my-dbus-signal-handler)
1647 @result{} ((:system "org.freedesktop.Hal.Manager" "DeviceAdded")
1648     ("org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1649      my-signal-handler))
1650 @end lisp
1652 As we know from the introspection data of interface
1653 @samp{org.freedesktop.Hal.Manager}, the signal @samp{DeviceAdded}
1654 provides one single parameter, which is mapped into a Lisp string.
1655 The callback function @code{my-dbus-signal-handler} must define one
1656 single string argument therefore.  Plugging an USB device to your
1657 machine, when registered for signal @samp{DeviceAdded}, will show you
1658 which objects the GNU/Linux @code{hal} daemon adds.
1659 @end defun
1662 @node Alternative Buses
1663 @chapter Alternative buses.
1664 @cindex bus names
1665 @cindex UNIX domain socket
1667 Until now, we have spoken about the system and the session buses,
1668 which are the default buses to be connected to.  However, it is
1669 possible to connect to any bus, from which the address is known.  This
1670 is a UNIX domain socket.  Everywhere, where a @var{bus} is mentioned
1671 as argument of a function (the symbol @code{:system} or the symbol
1672 @code{:session}), this address can be used instead.  The connection to
1673 this bus must be initialized first.
1675 @defun dbus-init-bus bus
1676 Establish the connection to D-Bus @var{bus}.
1678 @var{bus} can be either the symbol @code{:system} or the symbol
1679 @code{:session}, or it can be a string denoting the address of the
1680 corresponding bus.  For the system and session busses, this function
1681 is called when loading @file{dbus.el}, there is no need to call it
1682 again.
1684 Example: You open another session bus in a terminal window on your host:
1686 @example
1687 # eval `dbus-launch --auto-syntax`
1688 # echo $DBUS_SESSION_BUS_ADDRESS
1690 @print{} unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e
1691 @end example
1693 In Emacs, you can access to this bus via its address:
1695 @lisp
1696 (setq my-bus
1697       "unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e")
1699 @result{} "unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e"
1701 (dbus-init-bus my-bus)
1703 @result{} nil
1705 (dbus-get-unique-name my-bus)
1707 @result{} ":1.0"
1708 @end lisp
1709 @end defun
1712 @node Errors and Events
1713 @chapter Errors and events.
1714 @cindex debugging
1715 @cindex errors
1716 @cindex events
1718 The internal actions can be traced by running in a debug mode.
1720 @defvar dbus-debug
1721 If this variable is non-@code{nil}, D-Bus specific debug messages are raised.
1722 @end defvar
1724 Input parameters of @code{dbus-call-method},
1725 @code{dbus-call-method-non-blocking},
1726 @code{dbus-call-method-asynchronously}, and
1727 @code{dbus-register-signal} are checked for correct D-Bus types. If
1728 there is a type mismatch, the Lisp error @code{wrong-type-argument}
1729 @code{D-Bus ARG} is raised.
1731 All errors raised by D-Bus are signaled with the error symbol
1732 @code{dbus-error}.  If possible, error messages from D-Bus are
1733 appended to the @code{dbus-error}.
1735 @defspec dbus-ignore-errors forms@dots{}
1736 This executes @var{forms} exactly like a @code{progn}, except that
1737 @code{dbus-error} errors are ignored during the @var{forms}.  These
1738 errors can be made visible when @code{dbus-debug} is set to @code{t}.
1739 @end defspec
1741 Incoming D-Bus messages are handled as Emacs events, see @pxref{Misc
1742 Events, , , elisp}.  They are retrieved only, when Emacs runs in
1743 interactive mode.  The generated event has this form:
1745 @lisp
1746 (dbus-event @var{bus} @var{type} @var{serial} @var{service} @var{path} @var{interface} @var{member} @var{handler}
1747         &rest @var{args})
1748 @end lisp
1750 @var{bus} identifies the D-Bus the message is coming from.  It is
1751 either the symbol @code{:system} or the symbol @code{:session}.
1753 @var{type} is the D-Bus message type which has caused the event.  It
1754 can be @code{dbus-message-type-invalid},
1755 @code{dbus-message-type-method-call},
1756 @code{dbus-message-type-method-return},
1757 @code{dbus-message-type-error}, or @code{dbus-message-type-signal}.
1758 @var{serial} is the serial number of the received D-Bus message.
1760 @var{service} and @var{path} are the unique name and the object path
1761 of the D-Bus object emitting the message.  @var{interface} and
1762 @var{member} denote the message which has been sent.
1764 @var{handler} is the callback function which has been registered for
1765 this message (see @pxref{Signals}).  When a @code{dbus-event} event
1766 arrives, @var{handler} is called with @var{args} as arguments.
1768 In order to inspect the @code{dbus-event} data, you could extend the
1769 definition of the callback function in @ref{Signals}:
1771 @lisp
1772 (defun my-dbus-signal-handler (&rest args)
1773   (message "my-dbus-signal-handler: %S" last-input-event))
1774 @end lisp
1776 There exist convenience functions which could be called inside a
1777 callback function in order to retrieve the information from the event.
1779 @defun dbus-event-bus-name event
1780 Returns the bus name @var{event} is coming from.
1781 The result is either the symbol @code{:system} or the symbol @code{:session}.
1782 @end defun
1784 @defun dbus-event-message-type event
1785 Returns the message type of the corresponding D-Bus message.  The
1786 result is a natural number.
1787 @end defun
1789 @defun dbus-event-serial-number event
1790 Returns the serial number of the corresponding D-Bus message.
1791 The result is a natural number.
1792 @end defun
1794 @defun dbus-event-service-name event
1795 Returns the unique name of the D-Bus object @var{event} is coming from.
1796 @end defun
1798 @defun dbus-event-path-name event
1799 Returns the object path of the D-Bus object @var{event} is coming from.
1800 @end defun
1802 @defun dbus-event-interface-name event
1803 Returns the interface name of the D-Bus object @var{event} is coming from.
1804 @end defun
1806 @defun dbus-event-member-name event
1807 Returns the member name of the D-Bus object @var{event} is coming
1808 from.  It is either a signal name or a method name.
1809 @end defun
1811 D-Bus errors are not propagated during event handling, because it is
1812 usually not desired.  D-Bus errors in events can be made visible by
1813 setting the variable @code{dbus-debug} to @code{t}.  They can also be
1814 handled by a hook function.
1816 @defvar dbus-event-error-hooks
1817 This hook variable keeps a list of functions, which are called when a
1818 D-Bus error happens in the event handler.  Every function must accept
1819 two arguments, the event and the error variable catched in
1820 @code{condition-case} by @code{dbus-error}.
1822 Such functions can be used the adapt the error signal to be raised.
1823 Example:
1825 @lisp
1826 (defun my-dbus-event-error-handler (event error)
1827   (when (string-equal (concat dbus-service-emacs ".FileManager")
1828                       (dbus-event-interface-name event))
1829     (message "my-dbus-event-error-handler: %S %S" event error)
1830     (signal 'file-error (cdr error))))
1832 (add-hook 'dbus-event-error-hooks 'my-dbus-event-error-handler)
1833 @end lisp
1834 @end defvar
1836 Hook functions shall take into account, that there might be other
1837 D-Bus applications running.  Therefore, they shall check carefully,
1838 whether a given D-Bus error is related to them.
1841 @node Index
1842 @unnumbered Index
1844 @printindex cp
1847 @node GNU Free Documentation License
1848 @appendix GNU Free Documentation License
1849 @include doclicense.texi
1851 @bye