Nuke arch-tags.
[emacs.git] / doc / misc / dbus.texi
blobdcf1b521860b754cbc209bc9fad79f3a917aad22
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, 2008, 2009, 2010,
13 2011 Free Software Foundation, Inc.
15 @quotation
16 Permission is granted to copy, distribute and/or modify this document
17 under the terms of the GNU Free Documentation License, Version 1.3 or
18 any later version published by the Free Software Foundation; with no
19 Invariant Sections, with the Front-Cover texts being ``A GNU Manual'',
20 and with the Back-Cover Texts as in (a) below.  A copy of the license
21 is included in the section entitled ``GNU Free Documentation License''.
23 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
24 modify this GNU manual.  Buying copies from the FSF supports it in
25 developing GNU and promoting software freedom.''
26 @end quotation
27 @end copying
29 @dircategory Emacs
30 @direntry
31 * D-Bus: (dbus).                Using D-Bus in Emacs.
32 @end direntry
34 @contents
37 @node Top, Overview, (dir), (dir)
38 @top D-Bus integration in Emacs
40 This manual documents an API for usage of D-Bus in Emacs.  D-Bus is a
41 message bus system, a simple way for applications to talk to one
42 another.  An overview of D-Bus can be found at
43 @uref{http://dbus.freedesktop.org/}.
45 @ifnottex
46 @insertcopying
47 @end ifnottex
49 @menu
50 * Overview::                    An overview of D-Bus.
51 * Inspection::                  Inspection of D-Bus services.
52 * Type Conversion::             Mapping Lisp types and D-Bus types.
53 * Synchronous Methods::         Calling methods in a blocking way.
54 * Asynchronous Methods::        Calling methods non-blocking.
55 * Receiving Method Calls::      Offering own methods.
56 * Signals::                     Sending and receiving signals.
57 * Alternative Buses::           Alternative buses.
58 * Errors and Events::           Errors and events.
59 * Index::                       Index including concepts, functions, variables.
61 * GNU Free Documentation License:: The license for this documentation.
62 @end menu
65 @node Overview
66 @chapter An overview of D-Bus
67 @cindex overview
69 D-Bus is an inter-process communication mechanism for applications
70 residing on the same host.  The communication is based on
71 @dfn{messages}.  Data in the messages is carried in a structured way,
72 it is not just a byte stream.
74 The communication is connection oriented to two kinds of message
75 buses: a so called @dfn{system bus}, and a @dfn{session bus}.  On a
76 given machine, there is always one single system bus for miscellaneous
77 system-wide communication, like changing of hardware configuration.
78 On the other hand, the session bus is always related to a single
79 user's session.
81 Every client application, which is connected to a bus, registers under
82 a @dfn{unique name} at the bus.  This name is used for identifying the
83 client application.  Such a unique name starts always with a colon,
84 and looks like @samp{:1.42}.
86 Additionally, a client application can register itself to a so called
87 @dfn{known name}, which is a series of identifiers separated by dots,
88 as in @samp{org.gnu.Emacs}.  If several applications register to the
89 same known name, these registrations are queued, and only the first
90 application which has registered for the known name is reachable via
91 this name.  If this application disconnects from the bus, the next
92 queued unique name becomes the owner of this known name.
94 An application can install one or several objects under its name.
95 Such objects are identified by an @dfn{object path}, which looks
96 similar to paths in a filesystem.  An example of such an object path
97 could be @samp{/org/gnu/Emacs/}.
99 Applications might send a request to an object, that means sending a
100 message with some data as input parameters, and receiving a message
101 from that object with the result of this message, the output
102 parameters.  Such a request is called @dfn{method} in D-Bus.
104 The other form of communication are @dfn{signals}.  The underlying
105 message is emitted from an object and will be received by all other
106 applications which have registered for such a signal.
108 All methods and signals an object supports are called @dfn{interface}
109 of the object.  Interfaces are specified under a hierarchical name in
110 D-Bus; an object can support several interfaces.  Such an interface
111 name could be @samp{org.gnu.Emacs.TextEditor} or
112 @samp{org.gnu.Emacs.FileManager}.
115 @node Inspection
116 @chapter Inspection of D-Bus services.
117 @cindex inspection
119 @menu
120 * Bus names::                   Discovering D-Bus names.
121 * Introspection::               Knowing the details of D-Bus services.
122 * Nodes and Interfaces::        Detecting object paths and interfaces.
123 * Methods and Signal::          Applying the functionality.
124 * Properties and Annotations::  What else to know about interfaces.
125 * Arguments and Signatures::    The final details.
126 @end menu
129 @node Bus names
130 @section Bus names.
132 There are several basic functions which inspect the buses for
133 registered names.  Internally they use the basic interface
134 @samp{org.freedesktop.DBus}, which is supported by all objects of a bus.
136 @defun dbus-list-activatable-names
137 This function returns the D-Bus service names, which can be activated.
138 An activatable service is described in a service registration file.
139 Under GNU/Linux, such files are located at
140 @file{/usr/share/dbus-1/services/}.
142 The result is a list of strings, which is @code{nil} when there are no
143 activatable service names at all.
144 @end defun
146 @defun dbus-list-names bus
147 All service names, which are registered at D-Bus @var{bus}, are
148 returned.  The result is a list of strings, which is @code{nil} when
149 there are no registered service names at all.  Well known names are
150 strings like @samp{org.freedesktop.DBus}.  Names starting with
151 @samp{:} are unique names for services.
153 @var{bus} must be either the symbol @code{:system} or the symbol
154 @code{:session}.
155 @end defun
157 @defun dbus-list-known-names bus
158 Retrieves all services which correspond to a known name in @var{bus}.
159 A service has a known name if it doesn't start with @samp{:}.  The
160 result is a list of strings, which is @code{nil} when there are no
161 known names at all.
163 @var{bus} must be either the symbol @code{:system} or the symbol
164 @code{:session}.
165 @end defun
167 @defun dbus-list-queued-owners bus service
168 For a given service, registered at D-Bus @var{bus} under the name
169 @var{service}, all queued unique names are returned.  The result is a
170 list of strings, or @code{nil} when there are no queued names for
171 @var{service} at all.
173 @var{bus} must be either the symbol @code{:system} or the symbol
174 @code{:session}.  @var{service} must be a known service name as
175 string.
176 @end defun
178 @defun dbus-get-name-owner bus service
179 For a given service, registered at D-Bus @var{bus} under the name
180 @var{service}, the unique name of the name owner is returned.  The
181 result is a string, or @code{nil} when there exist no name owner of
182 @var{service}.
184 @var{bus} must be either the symbol @code{:system} or the symbol
185 @code{:session}.  @var{service} must be a known service name as
186 string.
187 @end defun
189 @defun dbus-ping bus service &optional timeout
190 Check whether the service name @var{service} is registered at D-Bus
191 @var{bus}.  @var{service} might not have been started yet, it is
192 autostarted if possible.  The result is either @code{t} or @code{nil}.
194 @var{bus} must be either the symbol @code{:system} or the symbol
195 @code{:session}.  @var{service} must be a string.  @var{timeout}, a
196 nonnegative integer, specifies the maximum number of milliseconds
197 @code{dbus-ping} must return.  The default value is 25,000.  Example:
199 @lisp
200 (message
201    "%s screensaver on board."
202    (cond
203      ((dbus-ping :session "org.gnome.ScreenSaver" 100) "Gnome")
204      ((dbus-ping :session "org.freedesktop.ScreenSaver" 100) "KDE")
205      (t "No")))
206 @end lisp
208 If it shall be checked whether @var{service} is already running
209 without autostarting it, one shall apply
211 @lisp
212 (member service (dbus-list-known-names bus))
213 @end lisp
214 @end defun
216 @defun dbus-get-unique-name bus
217 The unique name, under which Emacs is registered at D-Bus @var{bus},
218 is returned as string.
220 @var{bus} must be either the symbol @code{:system} or the symbol
221 @code{:session}.
222 @end defun
225 @node Introspection
226 @section Knowing the details of D-Bus services.
228 D-Bus services publish their interfaces.  This can be retrieved and
229 analyzed during runtime, in order to understand the used
230 implementation.
232 The resulting introspection data are in XML format.  The root
233 introspection element is always a @code{node} element.  It might have
234 a @code{name} attribute, which denotes the (absolute) object path an
235 interface is introspected.
237 The root @code{node} element may have @code{node} and @code{interface}
238 children.  A child @code{node} element must have a @code{name}
239 attribute, this case it is the relative object path to the root
240 @code{node} element.
242 An @code{interface} element has just one attribute, @code{name}, which
243 is the full name of that interface.  The default interface
244 @samp{org.freedesktop.DBus.Introspectable} is always present.  Example:
246 @example
247 <node name="/org/bluez">
248   <interface name="org.freedesktop.DBus.Introspectable">
249     @dots{}
250   </interface>
251   <interface name="org.bluez.Manager">
252     @dots{}
253   </interface>
254   <interface name="org.bluez.Database">
255     @dots{}
256   </interface>
257   <interface name="org.bluez.Security">
258     @dots{}
259   </interface>
260   <node name="service_audio"/>
261   <node name="service_input"/>
262   <node name="service_network"/>
263   <node name="service_serial"/>
264 </node>
265 @end example
267 Children of an @code{interface} element can be @code{method},
268 @code{signal} and @code{property} elements.  A @code{method} element
269 stands for a D-Bus method of the surrounding interface.  The element
270 itself has a @code{name} attribute, showing the method name.  Children
271 elements @code{arg} stand for the arguments of a method.  Example:
273 @example
274 <method name="ResolveHostName">
275   <arg name="interface" type="i" direction="in"/>
276   <arg name="protocol" type="i" direction="in"/>
277   <arg name="name" type="s" direction="in"/>
278   <arg name="aprotocol" type="i" direction="in"/>
279   <arg name="flags" type="u" direction="in"/>
280   <arg name="interface" type="i" direction="out"/>
281   <arg name="protocol" type="i" direction="out"/>
282   <arg name="name" type="s" direction="out"/>
283   <arg name="aprotocol" type="i" direction="out"/>
284   <arg name="address" type="s" direction="out"/>
285   <arg name="flags" type="u" direction="out"/>
286 </method>
287 @end example
289 @code{arg} elements can have the attributes @code{name}, @code{type}
290 and @code{direction}.  The @code{name} attribute is optional.  The
291 @code{type} attribute stands for the @dfn{signature} of the argument
292 in D-Bus.  For a discussion of D-Bus types and their Lisp
293 representation see @ref{Type Conversion}.@footnote{D-Bus signatures
294 are explained in the D-Bus specification
295 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures}.}
296 The @code{direction} attribute of an @code{arg} element can be only
297 @samp{in} or @samp{out}; in case it is omitted, it defaults to
298 @samp{in}.
300 A @code{signal} element of an @code{interface} has a similar
301 structure.  The @code{direction} attribute of an @code{arg} child
302 element can be only @samp{out} here; which is also the default value.
303 Example:
305 @example
306 <signal name="StateChanged">
307   <arg name="state" type="i"/>
308   <arg name="error" type="s"/>
309 </signal>
310 @end example
312 A @code{property} element has no @code{arg} child
313 element.  It just has the attributes @code{name}, @code{type} and
314 @code{access}, which are all mandatory.  The @code{access} attribute
315 allows the values @samp{readwrite}, @samp{read}, and @samp{write}.
316 Example:
318 @example
319 <property name="Status" type="u" direction="read"/>
320 @end example
322 @code{annotation} elements can be children of @code{interface},
323 @code{method}, @code{signal}, and @code{property} elements.  Unlike
324 properties, which can change their values during lifetime of a D-Bus
325 object, annotations are static.  Often they are used for code
326 generators of D-Bus langugae bindings.  Example:
328 @example
329 <annotation name="de.berlios.Pinot.GetStatistics" value="pinotDBus"/>
330 @end example
332 Annotations have just @code{name} and @code{value} attributes, both
333 must be strings.
335 @defun dbus-introspect bus service path
336 This function returns all interfaces and sub-nodes of @var{service},
337 registered at object path @var{path} at bus @var{bus}.
339 @var{bus} must be either the symbol @code{:system} or the symbol
340 @code{:session}.  @var{service} must be a known service name, and
341 @var{path} must be a valid object path.  The last two parameters are
342 strings.  The result, the introspection data, is a string in XML
343 format.  Example:
345 @lisp
346 (dbus-introspect
347   :system "org.freedesktop.Hal"
348   "/org/freedesktop/Hal/devices/computer")
350 @result{} "<!DOCTYPE node PUBLIC
351     "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
352     "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
353     <node>
354       <interface name="org.freedesktop.Hal.Device">
355         <method name="GetAllProperties">
356           <arg name="properties" direction="out" type="a@{sv@}"/>
357         </method>
358         @dots{}
359         <signal name="PropertyModified">
360           <arg name="num_updates" type="i"/>
361           <arg name="updates" type="a(sbb)"/>
362         </signal>
363       </interface>
364       @dots{}
365     </node>"
366 @end lisp
368 This example informs us, that the service @samp{org.freedesktop.Hal}
369 at object path @samp{/org/freedesktop/Hal/devices/computer} offers the
370 interface @samp{org.freedesktop.Hal.Device} (and 2 other interfaces
371 not documented here).  This interface contains the method
372 @samp{GetAllProperties}, which needs no input parameters, but returns
373 as output parameter an array of dictionary entries (key-value pairs).
374 Every dictionary entry has a string as key, and a variant as value.
376 The interface offers also a signal, which returns 2 parameters: an
377 integer, and an array consisting of elements which are a struct of a
378 string and 2 boolean values.@footnote{ The interfaces of the service
379 @samp{org.freedesktop.Hal} are described at
380 @uref{http://people.freedesktop.org/~david/hal-spec/hal-spec.html#interfaces}.}
381 @end defun
383 @defun dbus-introspect-xml bus service path
384 This function has the same intention as function
385 @code{dbus-introspect}.  The returned value is a parsed XML tree,
386 which can be used for further analysis.  Example:
388 @lisp
389 (dbus-introspect-xml
390   :session "org.freedesktop.xesam.searcher"
391   "/org/freedesktop/xesam/searcher/main")
393 @result{} (node ((name . "/org/freedesktop/xesam/searcher/main"))
394      (interface ((name . "org.freedesktop.xesam.Search"))
395        (method ((name . "GetHitData"))
396          (arg ((name . "search") (type . "s") (direction . "in")))
397          (arg ((name . "hit_ids") (type . "au") (direction . "in")))
398          (arg ((name . "fields") (type . "as") (direction . "in")))
399          (arg ((name . "hit_data") (type . "aav") (direction . "out")))
400        )
401        @dots{}
402        (signal ((name . "HitsAdded"))
403          (arg ((name . "search") (type . "s")))
404          (arg ((name . "count") (type . "u")))
405        )
406      )
407      @dots{}
408    )
409 @end lisp
410 @end defun
412 @defun dbus-introspect-get-attribute object attribute
413 It returns the @var{attribute} value of a D-Bus introspection
414 @var{object}.  @var{object} can be every subtree of a parsed XML tree
415 as retrieved with @code{dbus-introspect-xml}.  @var{attribute} must be
416 a string according to the attribute names in the D-Bus specification.
417 Example:
419 @lisp
420 (dbus-introspect-get-attribute
421   (dbus-introspect-xml :system "org.freedesktop.SystemToolsBackends"
422     "/org/freedesktop/SystemToolsBackends/UsersConfig")
423   "name")
425 @result{} "/org/freedesktop/SystemToolsBackends/UsersConfig"
426 @end lisp
428 If @var{object} has no @var{attribute}, the function returns
429 @code{nil}.
430 @end defun
433 @node Nodes and Interfaces
434 @section Detecting object paths and interfaces.
436 The first elements, to be introspected for a D-Bus object, are further
437 object paths and interfaces.
439 @defun dbus-introspect-get-node-names bus service path
440 All node names of @var{service} in D-Bus @var{bus} at object path
441 @var{path} are returned as list of strings.  Example:
443 @lisp
444 (dbus-introspect-get-node-names
445   :session "org.gnome.seahorse" "/org/gnome/seahorse")
447 @result{} ("crypto" "keys")
448 @end lisp
450 The node names stand for further object paths of the D-Bus
451 @var{service}, relative to @var{path}.  In the example,
452 @samp{/org/gnome/seahorse/crypto} and @samp{/org/gnome/seahorse/keys}
453 are also object paths of the D-Bus service @samp{org.gnome.seahorse}.
454 @end defun
456 @defun dbus-introspect-get-all-nodes bus service path
457 This function returns all node names of @var{service} in D-Bus
458 @var{bus} at object path @var{path}.  It returns a list of strings
459 with all object paths of @var{service}, starting at @var{path}.
460 Example:
462 @lisp
463 (dbus-introspect-get-all-nodes :session "org.gnome.seahorse" "/")
465 @result{} ("/" "/org" "/org/gnome" "/org/gnome/seahorse"
466     "/org/gnome/seahorse/crypto"
467     "/org/gnome/seahorse/keys"
468     "/org/gnome/seahorse/keys/openpgp"
469     "/org/gnome/seahorse/keys/openpgp/local"
470     "/org/gnome/seahorse/keys/openssh"
471     "/org/gnome/seahorse/keys/openssh/local")
472 @end lisp
473 @end defun
475 @defun dbus-introspect-get-interface-names bus service path
476 There will be returned a list strings of all interface names of
477 @var{service} in D-Bus @var{bus} at object path @var{path}.  This list
478 will contain the default interface @samp{org.freedesktop.DBus.Introspectable}.
480 Another default interface is @samp{org.freedesktop.DBus.Properties}.
481 If present, @code{interface} elements can also have @code{property}
482 children.  Example:
484 @lisp
485 (dbus-introspect-get-interface-names
486   :system "org.freedesktop.Hal"
487   "/org/freedesktop/Hal/devices/computer")
489 @result{} ("org.freedesktop.DBus.Introspectable"
490     "org.freedesktop.Hal.Device"
491     "org.freedesktop.Hal.Device.SystemPowerManagement"
492     "org.freedesktop.Hal.Device.CPUFreq")
493 @end lisp
494 @end defun
496 @defun dbus-introspect-get-interface bus service path interface
497 Return @var{interface} of @var{service} in D-Bus @var{bus} at object
498 path @var{path}.  The return value is an XML element.  @var{interface}
499 must be a string, element of the list returned by
500 @code{dbus-introspect-get-interface-names}.  Example:
502 @lisp
503 (dbus-introspect-get-interface
504   :session "org.freedesktop.xesam.searcher"
505   "/org/freedesktop/xesam/searcher/main"
506   "org.freedesktop.xesam.Search")
508 @result{} (interface ((name . "org.freedesktop.xesam.Search"))
509      (method ((name . "GetHitData"))
510        (arg ((name . "search") (type . "s") (direction . "in")))
511        (arg ((name . "hit_ids") (type . "au") (direction . "in")))
512        (arg ((name . "fields") (type . "as") (direction . "in")))
513        (arg ((name . "hit_data") (type . "aav") (direction . "out")))
514      )
515      @dots{}
516      (signal ((name . "HitsAdded"))
517        (arg ((name . "search") (type . "s")))
518        (arg ((name . "count") (type . "u")))
519      )
520    )
521 @end lisp
522 @end defun
524 @noindent
525 With these functions, it is possible to retrieve all introspection
526 data from a running system:
528 @lisp
529 (with-current-buffer (switch-to-buffer "*introspect*")
530   (erase-buffer)
531   (dolist (service (dbus-list-known-names :session))
532     (dolist (path (dbus-introspect-get-all-nodes :session service "/"))
533       ;; We want to introspect only elements, which have more than
534       ;; the default interface "org.freedesktop.DBus.Introspectable".
535       (when (delete
536              "org.freedesktop.DBus.Introspectable"
537              (dbus-introspect-get-interface-names :session service path))
538         (insert (message "\nservice: \"%s\" path: \"%s\"\n" service path)
539                 (dbus-introspect :session service path))
540         (redisplay t)))))
541 @end lisp
544 @node Methods and Signal
545 @section Applying the functionality.
547 Methods and signals are the communicatione means to D-Bus.  The
548 following functions return their specifications.
550 @defun dbus-introspect-get-method-names bus service path interface
551 Return a list of strings of all method names of @var{interface} of
552 @var{service} in D-Bus @var{bus} at object path @var{path}.  Example:
554 @lisp
555 (dbus-introspect-get-method-names
556   :session "org.freedesktop.xesam.searcher"
557   "/org/freedesktop/xesam/searcher/main"
558   "org.freedesktop.xesam.Search")
560 @result{} ("GetState" "StartSearch" "GetHitCount" "GetHits" "NewSession"
561     "CloseSession" "GetHitData" "SetProperty" "NewSearch"
562     "GetProperty" "CloseSearch")
563 @end lisp
564 @end defun
566 @defun dbus-introspect-get-method bus service path interface method
567 This function returns @var{method} of @var{interface} as XML element.
568 It must be located at @var{service} in D-Bus @var{bus} at object path
569 @var{path}.  @var{method} must be a string, element of the list
570 returned by @code{dbus-introspect-get-method-names}.  Example:
572 @lisp
573 (dbus-introspect-get-method
574   :session "org.freedesktop.xesam.searcher"
575   "/org/freedesktop/xesam/searcher/main"
576   "org.freedesktop.xesam.Search" "GetHitData")
578 @result{} (method ((name . "GetHitData"))
579      (arg ((name . "search") (type . "s") (direction . "in")))
580      (arg ((name . "hit_ids") (type . "au") (direction . "in")))
581      (arg ((name . "fields") (type . "as") (direction . "in")))
582      (arg ((name . "hit_data") (type . "aav") (direction . "out")))
583    )
584 @end lisp
585 @end defun
587 @defun dbus-introspect-get-signal-names bus service path interface
588 Return a list of strings of all signal names of @var{interface} of
589 @var{service} in D-Bus @var{bus} at object path @var{path}.  Example:
591 @lisp
592 (dbus-introspect-get-signal-names
593   :session "org.freedesktop.xesam.searcher"
594   "/org/freedesktop/xesam/searcher/main"
595   "org.freedesktop.xesam.Search")
597 @result{} ("StateChanged" "SearchDone" "HitsModified"
598     "HitsRemoved" "HitsAdded")
599 @end lisp
600 @end defun
602 @defun dbus-introspect-get-signal bus service path interface signal
603 This function returns @var{signal} of @var{interface} as XML element.
604 It must be located at @var{service} in D-Bus @var{bus} at object path
605 @var{path}.  @var{signal} must be a string, element of the list
606 returned by @code{dbus-introspect-get-signal-names}.  Example:
608 @lisp
609 (dbus-introspect-get-signal
610   :session "org.freedesktop.xesam.searcher"
611   "/org/freedesktop/xesam/searcher/main"
612   "org.freedesktop.xesam.Search" "HitsAdded")
614 @result{} (signal ((name . "HitsAdded"))
615      (arg ((name . "search") (type . "s")))
616      (arg ((name . "count") (type . "u")))
617    )
618 @end lisp
619 @end defun
622 @node Properties and Annotations
623 @section What else to know about interfaces.
625 Interfaces can have properties.  These can be exposed via the
626 @samp{org.freedesktop.DBus.Properties} interface@footnote{See
627 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties}}.
628 That is, properties can be retrieved and changed during lifetime of an
629 element.
631 Annotations, on the other hand, are static values for an element.
632 Often, they are used to instruct generators, how to generate code from
633 the interface for a given language binding.
635 @defun dbus-introspect-get-property-names bus service path interface
636 Return a list of strings with all property names of @var{interface} of
637 @var{service} in D-Bus @var{bus} at object path @var{path}.  Example:
639 @lisp
640 (dbus-introspect-get-property-names
641   :session "org.kde.kded" "/modules/networkstatus"
642   "org.kde.Solid.Networking.Client")
644 @result{} ("Status")
645 @end lisp
647 If an interface declares properties, the corresponding element supports
648 also the @samp{org.freedesktop.DBus.Properties} interface.
649 @end defun
651 @defun dbus-introspect-get-property bus service path interface property
652 This function returns @var{property} of @var{interface} as XML element.
653 It must be located at @var{service} in D-Bus @var{bus} at object path
654 @var{path}.  @var{property} must be a string, element of the list
655 returned by @code{dbus-introspect-get-property-names}.
657 A @var{property} value can be retrieved by the function
658 @code{dbus-introspect-get-attribute}.  Example:
660 @lisp
661 (dbus-introspect-get-property
662   :session "org.kde.kded" "/modules/networkstatus"
663   "org.kde.Solid.Networking.Client" "Status")
665 @result{} (property ((access . "read") (type . "u") (name . "Status")))
667 (dbus-introspect-get-attribute
668   (dbus-introspect-get-property
669     :session "org.kde.kded" "/modules/networkstatus"
670     "org.kde.Solid.Networking.Client" "Status")
671   "access")
673 @result{} "read"
674 @end lisp
675 @end defun
677 @defun dbus-get-property bus service path interface property
678 This function returns the value of @var{property} of @var{interface}.
679 It will be checked at @var{bus}, @var{service}, @var{path}.  The
680 result can be any valid D-Bus value, or @code{nil} if there is no
681 @var{property}.  Example:
683 @lisp
684 (dbus-get-property
685   :session "org.kde.kded" "/modules/networkstatus"
686   "org.kde.Solid.Networking.Client" "Status")
688 @result{} 4
689 @end lisp
690 @end defun
692 @defun dbus-set-property bus service path interface property value
693 Set value of @var{property} of @var{interface} to @var{value}.  It
694 will be checked at @var{bus}, @var{service}, @var{path}.  When the
695 value has been set successful, the result is @var{value}.  Otherwise,
696 @code{nil} is returned.  Example:
698 @lisp
699 (dbus-set-property
700   :session "org.kde.kaccess" "/MainApplication"
701   "com.trolltech.Qt.QApplication" "doubleClickInterval" 500)
703 @result{} 500
704 @end lisp
705 @end defun
707 @defun dbus-get-all-properties bus service path interface
708 This function returns all properties of @var{interface}.  It will be
709 checked at @var{bus}, @var{service}, @var{path}.  The result is a list
710 of cons.  Every cons contains the name of the property, and its value.
711 If there are no properties, @code{nil} is returned.  Example:
713 @lisp
714 (dbus-get-all-properties
715   :session "org.kde.kaccess" "/MainApplication"
716   "com.trolltech.Qt.QApplication")
718 @result{} (("cursorFlashTime" . 1000) ("doubleClickInterval" . 500)
719     ("keyboardInputInterval" . 400) ("wheelScrollLines" . 3)
720     ("globalStrut" 0 0) ("startDragTime" . 500)
721     ("startDragDistance" . 4) ("quitOnLastWindowClosed" . t)
722     ("styleSheet" . ""))
723 @end lisp
724 @end defun
726 @defun dbus-introspect-get-annotation-names bus service path interface &optional name
727 Return a list of all annotation names as list of strings.  If
728 @var{name} is @code{nil}, the annotations are children of
729 @var{interface}, otherwise @var{name} must be a @code{method},
730 @code{signal}, or @code{property} XML element, where the annotations
731 belong to.  Example:
733 @lisp
734 (dbus-introspect-get-annotation-names
735   :session "de.berlios.Pinot" "/de/berlios/Pinot"
736   "de.berlios.Pinot" "GetStatistics")
738 @result{} ("de.berlios.Pinot.GetStatistics")
739 @end lisp
741 Default annotation names@footnote{See
742 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format}}
745 @table @samp
746 @item org.freedesktop.DBus.Deprecated
747 Whether or not the entity is deprecated; defaults to @code{nil}
749 @item org.freedesktop.DBus.GLib.CSymbol
750 The C symbol; may be used for @code{methods} and @code{interfaces}
752 @item org.freedesktop.DBus.Method.NoReply
753 If set, don't expect a reply to the @code{method} call; defaults to @code{nil}
754 @end table
755 @end defun
757 @defun dbus-introspect-get-annotation bus service path interface name annotation
758 Return annotation @var{ANNOTATION} as XML object.  If @var{name} is
759 @code{nil}, @var{ANNOTATION} is a child of @var{interface}, otherwise
760 @var{name} must be the name of a @code{method}, @code{signal}, or
761 @code{property} XML element, where the @var{ANNOTATION} belongs to.
763 An attribute value can be retrieved by
764 @code{dbus-introspect-get-attribute}.  Example:
766 @lisp
767 (dbus-introspect-get-annotation
768   :session "de.berlios.Pinot" "/de/berlios/Pinot"
769   "de.berlios.Pinot" "GetStatistics"
770   "de.berlios.Pinot.GetStatistics")
772 @result{} (annotation ((name . "de.berlios.Pinot.GetStatistics")
773                 (value . "pinotDBus")))
775 (dbus-introspect-get-attribute
776   (dbus-introspect-get-annotation
777     :session "de.berlios.Pinot" "/de/berlios/Pinot"
778     "de.berlios.Pinot" "GetStatistics"
779     "de.berlios.Pinot.GetStatistics")
780   "value")
782 @result{} "pinotDBus"
783 @end lisp
784 @end defun
787 @node Arguments and Signatures
788 @section The final details.
790 Methods and signals have arguments.  They are described in the
791 @code{arg} XML elements.
793 @defun dbus-introspect-get-argument-names bus service path interface name
794 Return a list of all argument names as list of strings.  @var{name}
795 must be a @code{method} or @code{signal} XML element.  Example:
797 @lisp
798 (dbus-introspect-get-argument-names
799   :session "org.freedesktop.xesam.searcher"
800   "/org/freedesktop/xesam/searcher/main"
801   "org.freedesktop.xesam.Search" "GetHitData")
803 @result{} ("search" "hit_ids" "fields" "hit_data")
804 @end lisp
806 Argument names are optional; the function can return @code{nil}
807 therefore, even if the method or signal has arguments.
808 @end defun
810 @defun dbus-introspect-get-argument bus service path interface name arg
811 Return argument @var{ARG} as XML object.  @var{name}
812 must be a @code{method} or @code{signal} XML element.  Example:
814 @lisp
815 (dbus-introspect-get-argument
816   :session "org.freedesktop.xesam.searcher"
817   "/org/freedesktop/xesam/searcher/main"
818   "org.freedesktop.xesam.Search" "GetHitData" "search")
820 @result{} (arg ((name . "search") (type . "s") (direction . "in")))
821 @end lisp
822 @end defun
824 @defun dbus-introspect-get-signature bus service path interface name &optional direction
825 Return signature of a @code{method} or @code{signal}, represented by
826 @var{name}, as string.
828 If @var{name} is a @code{method}, @var{direction} can be either
829 @samp{in} or @samp{out}.  If @var{direction} is @code{nil}, @samp{in}
830 is assumed.
832 If @var{name} is a @code{signal}, and @var{direction} is
833 non-@code{nil}, @var{direction} must be @samp{out}.  Example:
835 @lisp
836 (dbus-introspect-get-signature
837   :session "org.freedesktop.xesam.searcher"
838   "/org/freedesktop/xesam/searcher/main"
839   "org.freedesktop.xesam.Search" "GetHitData" "in")
841 @result{} "sauas"
843 (dbus-introspect-get-signature
844   :session "org.freedesktop.xesam.searcher"
845   "/org/freedesktop/xesam/searcher/main"
846   "org.freedesktop.xesam.Search" "HitsAdded")
848 @result{} "su"
849 @end lisp
850 @end defun
853 @node Type Conversion
854 @chapter Mapping Lisp types and D-Bus types.
855 @cindex type conversion
857 D-Bus method calls and signals accept usually several arguments as
858 parameters, either as input parameter, or as output parameter.  Every
859 argument belongs to a D-Bus type.
861 Such arguments must be mapped between the value encoded as a D-Bus
862 type, and the corresponding type of Lisp objects.  The mapping is
863 applied Lisp object @expansion{} D-Bus type for input parameters, and
864 D-Bus type @expansion{} Lisp object for output parameters.
867 @section Input parameters.
869 Input parameters for D-Bus methods and signals occur as arguments of a
870 Lisp function call.  The following mapping to D-Bus types is
871 applied, when the corresponding D-Bus message is created:
873 @example
874 @multitable {negative integer} {@expansion{}} {DBUS_TYPE_BOOLEAN}
875 @item Lisp type               @tab              @tab D-Bus type
876 @item
877 @item @code{t} and @code{nil} @tab @expansion{} @tab DBUS_TYPE_BOOLEAN
878 @item natural number          @tab @expansion{} @tab DBUS_TYPE_UINT32
879 @item negative integer        @tab @expansion{} @tab DBUS_TYPE_INT32
880 @item float                   @tab @expansion{} @tab DBUS_TYPE_DOUBLE
881 @item string                  @tab @expansion{} @tab DBUS_TYPE_STRING
882 @item list                    @tab @expansion{} @tab DBUS_TYPE_ARRAY
883 @end multitable
884 @end example
886 Other Lisp objects, like symbols or hash tables, are not accepted as
887 input parameter.
889 If it is necessary to use another D-Bus type, a corresponding type
890 symbol can be preceeded to the corresponding Lisp object. Basic D-Bus
891 types are represented by the type symbols @code{:byte},
892 @code{:boolean}, @code{:int16}, @code{:uint16}, @code{:int32},
893 @code{:uint32}, @code{:int64}, @code{:uint64}, @code{:double},
894 @code{:string}, @code{:object-path}, @code{:signature} and
895 @code{:unix-fd}.
897 @noindent
898 Example:
900 @lisp
901 (dbus-call-method @dots{} @var{NAT-NUMBER} @var{STRING})
902 @end lisp
904 is equivalent to
906 @lisp
907 (dbus-call-method @dots{} :uint32 @var{NAT-NUMBER} :string @var{STRING})
908 @end lisp
910 but different to
912 @lisp
913 (dbus-call-method @dots{} :int32 @var{NAT-NUMBER} :signature @var{STRING})
914 @end lisp
916 The value for a byte D-Bus type can be any integer in the range 0
917 through 255.  If a character is used as argument, modifiers
918 represented outside this range are stripped of.  For example,
919 @code{:byte ?x} is equal to @code{:byte ?\M-x}, but it is not equal to
920 @code{:byte ?\C-x} or @code{:byte ?\M-\C-x}.
922 A D-Bus compound type is always represented as a list.  The @sc{car}
923 of this list can be the type symbol @code{:array}, @code{:variant},
924 @code{:struct} or @code{:dict-entry}, which would result in a
925 corresponding D-Bus container.  @code{:array} is optional, because
926 this is the default compound D-Bus type for a list.
928 The objects being elements of the list are checked according to the
929 D-Bus compound type rules.
931 @itemize
932 @item An array must contain only elements of the same D-Bus type.  It
933 can be empty.
935 @item A variant must contain only one single element.
937 @item A dictionary entry must be element of an array, and it must
938 contain only a key-value pair of two elements, with a basic D-Bus type
939 key.
941 @item There is no restriction for structs.
942 @end itemize
944 If an empty array needs an element D-Bus type other than string, it
945 can contain exactly one element of D-Bus type @code{:signature}.  The
946 value of this element (a string) is used as the signature of the
947 elements of this array.  Example:
949 @lisp
950 (dbus-call-method
951   :session "org.freedesktop.Notifications"
952   "/org/freedesktop/Notifications"
953   "org.freedesktop.Notifications" "Notify"
954   "GNU Emacs"                 ;; Application name.
955   0                           ;; No replacement of other notifications.
956   ""                          ;; No icon.
957   "Notification summary"      ;; Summary.
958   (format                     ;; Body.
959     "This is a test notification, raised from %s" (emacs-version))
960   '(:array)                   ;; No actions (empty array of strings).
961   '(:array :signature "@{sv@}") ;; No hints
962                               ;; (empty array of dictionary entries).
963   :int32 -1)                 ;; Default timeout.
965 @result{} 3
966 @end lisp
968 @defun dbus-string-to-byte-array string
969 Sometimes, D-Bus methods require as input parameter an array of bytes,
970 instead of a string.  If it is guaranteed, that @var{string} is an
971 UTF8 string, this function performs the conversion.  Example:
973 @lisp
974 (dbus-string-to-byte-array "/etc/hosts")
976 @result{} (:array :byte 47 :byte 101 :byte 116 :byte 99 :byte 47
977            :byte 104 :byte 111 :byte 115 :byte 116 :byte 115)
978 @end lisp
979 @end defun
981 @defun dbus-escape-as-identifier string
982 Escape an arbitrary @var{string} so it follows the rules for a C
983 identifier.  The escaped string can be used as object path component,
984 interface element component, bus name component or member name in
985 D-Bus.
987 The escaping consists of replacing all non-alphanumerics, and the
988 first character if it's a digit, with an underscore and two
989 lower-case hex digits.  As a special case, "" is escaped to
990 "_".  Example:
992 @lisp
993 (dbus-escape-as-identifier "0123abc_xyz\x01\xff")
995 @result{} "_30123abc_5fxyz_01_ff"
996 @end lisp
997 @end defun
1000 @section Output parameters.
1002 Output parameters of D-Bus methods and signals are mapped to Lisp
1003 objects.
1005 @example
1006 @multitable {DBUS_TYPE_OBJECT_PATH} {@expansion{}} {natural number or float}
1007 @item D-Bus type            @tab              @tab Lisp type
1008 @item
1009 @item DBUS_TYPE_BOOLEAN     @tab @expansion{} @tab @code{t} or @code{nil}
1010 @item DBUS_TYPE_BYTE        @tab @expansion{} @tab natural number
1011 @item DBUS_TYPE_UINT16      @tab @expansion{} @tab natural number
1012 @item DBUS_TYPE_INT16       @tab @expansion{} @tab integer
1013 @item DBUS_TYPE_UINT32      @tab @expansion{} @tab natural number or float
1014 @item DBUS_TYPE_UNIX_FD     @tab @expansion{} @tab natural number or float
1015 @item DBUS_TYPE_INT32       @tab @expansion{} @tab integer or float
1016 @item DBUS_TYPE_UINT64      @tab @expansion{} @tab natural number or float
1017 @item DBUS_TYPE_INT64       @tab @expansion{} @tab integer or float
1018 @item DBUS_TYPE_DOUBLE      @tab @expansion{} @tab float
1019 @item DBUS_TYPE_STRING      @tab @expansion{} @tab string
1020 @item DBUS_TYPE_OBJECT_PATH @tab @expansion{} @tab string
1021 @item DBUS_TYPE_SIGNATURE   @tab @expansion{} @tab string
1022 @item DBUS_TYPE_ARRAY       @tab @expansion{} @tab list
1023 @item DBUS_TYPE_VARIANT     @tab @expansion{} @tab list
1024 @item DBUS_TYPE_STRUCT      @tab @expansion{} @tab list
1025 @item DBUS_TYPE_DICT_ENTRY  @tab @expansion{} @tab list
1026 @end multitable
1027 @end example
1029 A float object in case of @code{DBUS_TYPE_UINT32},
1030 @code{DBUS_TYPE_INT32}, @code{DBUS_TYPE_UINT64},
1031 @code{DBUS_TYPE_INT64} and @code{DBUS_TYPE_UNIX_FD} is returned, when
1032 the C value exceeds the Emacs number size range.
1034 The resulting list of the last 4 D-Bus compound types contains as
1035 elements the elements of the D-Bus container, mapped according to the
1036 same rules.
1038 The signal @code{PropertyModified}, discussed as example in
1039 @ref{Inspection}, would offer as Lisp data the following object
1040 (@var{BOOL} stands here for either @code{nil} or @code{t}):
1042 @lisp
1043 (@var{INTEGER} ((@var{STRING} @var{BOOL} @var{BOOL}) (@var{STRING} @var{BOOL} @var{BOOL}) @dots{}))
1044 @end lisp
1046 @defun dbus-byte-array-to-string byte-array
1047 If a D-Bus method or signal returns an array of bytes, which are known
1048 to represent an UTF8 string, this function converts @var{byte-array}
1049 to the corresponding string.  Example:
1051 @lisp
1052 (dbus-byte-array-to-string '(47 101 116 99 47 104 111 115 116 115))
1054 @result{} "/etc/hosts"
1055 @end lisp
1056 @end defun
1058 @defun dbus-unescape-from-identifier string
1059 Retrieve the original string from the encoded @var{string}.
1060 @var{string} must have been coded with
1061 @code{dbus-escape-as-identifier}.  Example:
1063 @lisp
1064 (dbus-unescape-from-identifier "_30123abc_5fxyz_01_ff")
1066 @ifinfo
1067 @result{} "0123abc_xyz^Aÿ"
1068 @end ifinfo
1069 @ifnotinfo
1070 @result{} "0123abc_xyz^A@"y"
1071 @end ifnotinfo
1072 @end lisp
1073 @end defun
1076 @node Synchronous Methods
1077 @chapter Calling methods in a blocking way.
1078 @cindex method calls, synchronous
1079 @cindex synchronous method calls
1081 Methods can be called synchronously (@dfn{blocking}) or asynchronously
1082 (@dfn{non-blocking}).
1084 At D-Bus level, a method call consist of two messages: one message
1085 which carries the input parameters to the object owning the method to
1086 be called, and a reply message returning the resulting output
1087 parameters from the object.
1089 @defun dbus-call-method bus service path interface method &optional :timeout timeout &rest args
1090 This function calls @var{method} on the D-Bus @var{bus}.  @var{bus} is
1091 either the symbol @code{:system} or the symbol @code{:session}.
1093 @var{service} is the D-Bus service name to be used.  @var{path} is the
1094 D-Bus object path, @var{service} is registered at.  @var{interface} is
1095 an interface offered by @var{service}.  It must provide @var{method}.
1097 If the parameter @code{:timeout} is given, the following integer
1098 @var{timeout} specifies the maximum number of milliseconds the method
1099 call must return.  The default value is 25,000.  If the method call
1100 doesn't return in time, a D-Bus error is raised (@pxref{Errors and
1101 Events}).
1103 All other arguments args are passed to @var{method} as arguments.
1104 They are converted into D-Bus types as described in @ref{Type
1105 Conversion}.
1107 The function returns the resulting values of @var{method} as a list of
1108 Lisp objects, according to the type conversion rules described in
1109 @ref{Type Conversion}.  Example:
1111 @lisp
1112 (dbus-call-method
1113   :session "org.gnome.seahorse" "/org/gnome/seahorse/keys/openpgp"
1114   "org.gnome.seahorse.Keys" "GetKeyField"
1115   "openpgp:657984B8C7A966DD" "simple-name")
1117 @result{} (t ("Philip R. Zimmermann"))
1118 @end lisp
1120 If the result of the method call is just one value, the converted Lisp
1121 object is returned instead of a list containing this single Lisp
1122 object.  Example:
1124 @lisp
1125 (dbus-call-method
1126   :system "org.freedesktop.Hal"
1127   "/org/freedesktop/Hal/devices/computer"
1128   "org.freedesktop.Hal.Device" "GetPropertyString"
1129   "system.kernel.machine")
1131 @result{} "i686"
1132 @end lisp
1134 With the @code{dbus-introspect} function it is possible to explore the
1135 interfaces of @samp{org.freedesktop.Hal} service. It offers the
1136 interfaces @samp{org.freedesktop.Hal.Manager} for the object at the
1137 path @samp{/org/freedesktop/Hal/Manager} as well as the interface
1138 @samp{org.freedesktop.Hal.Device} for all objects prefixed with the
1139 path @samp{/org/freedesktop/Hal/devices}.  With the methods
1140 @samp{GetAllDevices} and @samp{GetAllProperties}, it is simple to
1141 emulate the @code{lshal} command on GNU/Linux systems:
1143 @lisp
1144 (dolist (device
1145           (dbus-call-method
1146             :system "org.freedesktop.Hal"
1147             "/org/freedesktop/Hal/Manager"
1148             "org.freedesktop.Hal.Manager" "GetAllDevices"))
1149   (message "\nudi = %s" device)
1150   (dolist (properties
1151             (dbus-call-method
1152               :system "org.freedesktop.Hal" device
1153               "org.freedesktop.Hal.Device" "GetAllProperties"))
1154     (message "  %s = %S"
1155              (car properties) (or (caar (cdr properties)) ""))))
1157 @print{} "udi = /org/freedesktop/Hal/devices/computer
1158       info.addons = (\"hald-addon-acpi\")
1159       info.bus = \"unknown\"
1160       info.product = \"Computer\"
1161       info.subsystem = \"unknown\"
1162       info.udi = \"/org/freedesktop/Hal/devices/computer\"
1163       linux.sysfs_path_device = \"(none)\"
1164       power_management.acpi.linux.version = \"20051216\"
1165       power_management.can_suspend_to_disk = t
1166       power_management.can_suspend_to_ram = \"\"
1167       power_management.type = \"acpi\"
1168       smbios.bios.release_date = \"11/07/2001\"
1169       system.chassis.manufacturer = \"COMPAL\"
1170       system.chassis.type = \"Notebook\"
1171       system.firmware.release_date = \"03/19/2005\"
1172       @dots{}"
1173 @end lisp
1174 @end defun
1176 @defun dbus-call-method-non-blocking bus service path interface method &optional :timeout timeout &rest args
1177 Call @var{method} on the D-Bus @var{bus}, but don't block the event queue.
1178 This is necessary for communicating to registered D-Bus methods,
1179 which are running in the same Emacs process.
1181 The arguments are the same as in @code{dbus-call-method}.  Example:
1183 @lisp
1184 (dbus-call-method-non-blocking
1185   :system "org.freedesktop.Hal"
1186   "/org/freedesktop/Hal/devices/computer"
1187   "org.freedesktop.Hal.Device" "GetPropertyString"
1188   "system.kernel.machine")
1190 @result{} "i686"
1191 @end lisp
1192 @end defun
1195 @node Asynchronous Methods
1196 @chapter Calling methods non-blocking.
1197 @cindex method calls, asynchronous
1198 @cindex asynchronous method calls
1200 @defun dbus-call-method-asynchronously bus service path interface method handler &optional :timeout timeout &rest args
1201 This function calls @var{method} on the D-Bus @var{bus}
1202 asynchronously.  @var{bus} is either the symbol @code{:system} or the
1203 symbol @code{:session}.
1205 @var{service} is the D-Bus service name to be used.  @var{path} is the
1206 D-Bus object path, @var{service} is registered at.  @var{interface} is
1207 an interface offered by @var{service}.  It must provide @var{method}.
1209 @var{handler} is a Lisp function, which is called when the
1210 corresponding return message has arrived.  If @var{handler} is
1211 @code{nil}, no return message will be expected.
1213 If the parameter @code{:timeout} is given, the following integer
1214 @var{timeout} specifies the maximum number of milliseconds a reply
1215 message must arrive.  The default value is 25,000.  If there is no
1216 reply message in time, a D-Bus error is raised (@pxref{Errors and
1217 Events}).
1219 All other arguments args are passed to @var{method} as arguments.
1220 They are converted into D-Bus types as described in @ref{Type
1221 Conversion}.
1223 Unless @var{handler} is @code{nil}, the function returns a key into
1224 the hash table @code{dbus-registered-objects-table}.  The
1225 corresponding entry in the hash table is removed, when the return
1226 message has been arrived, and @var{handler} is called.  Example:
1228 @lisp
1229 (dbus-call-method-asynchronously
1230   :system "org.freedesktop.Hal"
1231   "/org/freedesktop/Hal/devices/computer"
1232   "org.freedesktop.Hal.Device" "GetPropertyString" 'message
1233   "system.kernel.machine")
1235 @result{} (:system 2)
1237 @print{} i686
1238 @end lisp
1239 @end defun
1242 @node Receiving Method Calls
1243 @chapter Offering own methods.
1244 @cindex method calls, returning
1245 @cindex returning method calls
1247 In order to register methods on the D-Bus, Emacs has to request a well
1248 known name on the D-Bus under which it will be available for other
1249 clients.  Names on the D-Bus can be registered and unregistered using
1250 the following functions:
1252 @defun dbus-register-service bus service &rest flags
1253 Register the known name @var{service} on D-Bus @var{bus}.
1255 @var{bus} is either the symbol @code{:system} or the symbol
1256 @code{:session}.
1258 @var{service} is the service name to be registered on the D-Bus.  It
1259 must be a known name.
1261 @var{flags} is a subset of the following keywords:
1263 @itemize
1264 @item @code{:allow-replacement}: Allow another service to become the primary
1265 owner if requested.
1267 @item @code{:replace-existing}: Request to replace the current primary owner.
1269 @item @code{:do-not-queue}: If we can not become the primary owner do not
1270 place us in the queue.
1271 @end itemize
1273 One of the following keywords is returned:
1275 @itemize
1277 @item @code{:primary-owner}: We have become the primary owner of the name
1278 @var{service}.
1280 @item @code{:in-queue}: We could not become the primary owner and
1281 have been placed in the queue.
1283 @item @code{:exists}: We already are in the queue.
1285 @item @code{:already-owner}: We already are the primary
1286 owner.
1287 @end itemize
1288 @end defun
1290 @defun dbus-unregister-service bus service
1291 Unregister all objects from D-Bus @var{bus}, registered by Emacs for
1292 @var{service}.
1294 @var{bus} is either the symbol @code{:system} or the symbol
1295 @code{:session}.
1297 @var{service} is the D-Bus service name of the D-Bus.  It must be a
1298 known name.  Emacs releases its association to @var{service} from
1299 D-Bus.
1301 One of the following keywords is returned:
1303 @itemize
1304 @item @code{:released}: We successfully released the name @var{service}.
1305 @item @code{:non-existent}: The name @var{service} does not exist on the bus.
1306 @item @code{:not-owner}: We are not an owner of the name @var{service}.
1307 @end itemize
1308 @end defun
1310 When a name has been chosen, Emacs can offer own methods, which can be
1311 called by other applications.  These methods could be an
1312 implementation of an interface of a well known service, like
1313 @samp{org.freedesktop.TextEditor}.
1315 It could be also an implementation of an own interface.  In this case,
1316 the service name must be @samp{org.gnu.Emacs}.  The object path shall
1317 begin with @samp{/org/gnu/Emacs/@strong{Application}/}, and the
1318 interface name shall be @code{org.gnu.Emacs.@strong{Application}}.
1319 @samp{@strong{Application}} is the name of the application which
1320 provides the interface.
1322 @deffn Constant dbus-service-emacs
1323 The well known service name of Emacs.
1324 @end deffn
1326 @deffn Constant dbus-path-emacs
1327 The object path head "/org/gnu/Emacs" used by Emacs.  All object
1328 paths, used by offered methods or signals, shall start with this
1329 string.
1330 @end deffn
1332 @defun dbus-register-method bus service path interface method handler dont-register-service
1333 With this function, an application registers @var{method} on the D-Bus
1334 @var{bus}.
1336 @var{bus} is either the symbol @code{:system} or the symbol
1337 @code{:session}.
1339 @var{service} is the D-Bus service name of the D-Bus object
1340 @var{method} is registered for.  It must be a known name (See
1341 discussion of @var{dont-register-service} below).
1343 @var{path} is the D-Bus object path @var{service} is registered (See
1344 discussion of @var{dont-register-service} below).
1346 @var{interface} is the interface offered by @var{service}.  It must
1347 provide @var{method}.
1349 @var{handler} is a Lisp function to be called when a @var{method} call
1350 is received.  It must accept as arguments the input arguments of
1351 @var{method}.  @var{handler} should return a list, whose elements are
1352 to be used as arguments for the reply message of @var{method}.  This
1353 list can be composed like the input parameters in @ref{Type
1354 Conversion}.
1356 If @var{handler} wants to return just one Lisp object and it is not a
1357 cons cell, @var{handler} can return this object directly, instead of
1358 returning a list containing the object.
1360 In case @var{handler} shall return a reply message with an empty
1361 argument list, @var{handler} must return the symbol @code{:ignore}.
1363 When @var{dont-register-service} is non-@code{nil}, the known name
1364 @var{service} is not registered.  This means that other D-Bus clients
1365 have no way of noticing the newly registered method.  When interfaces
1366 are constructed incrementally by adding single methods or properties
1367 at a time, @var{dont-register-service} can be used to prevent other
1368 clients from discovering the still incomplete interface.
1370 The default D-Bus timeout when waiting for a message reply is 25
1371 seconds.  This value could be even smaller, depending on the calling
1372 client.  Therefore, @var{handler} shall not last longer than
1373 absolutely necessary.
1375 @code{dbus-register-method} returns a Lisp object, which can be used
1376 as argument in @code{dbus-unregister-object} for removing the
1377 registration for @var{method}.  Example:
1379 @lisp
1380 (defun my-dbus-method-handler (filename)
1381   (let (result)
1382     (if (find-file filename)
1383         (setq result '(:boolean t))
1384       (setq result '(:boolean nil)))
1385     result))
1387 @result{} my-dbus-method-handler
1389 (dbus-register-method
1390   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1391   "org.freedesktop.TextEditor" "OpenFile"
1392   'my-dbus-method-handler)
1394 @result{} ((:session "org.freedesktop.TextEditor" "OpenFile")
1395     ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1396      my-dbus-method-handler))
1397 @end lisp
1399 If you invoke the method @samp{org.freedesktop.TextEditor.OpenFile}
1400 from another D-Bus application with a filename as parameter, the file
1401 is opened in Emacs, and the method returns either @var{true} or
1402 @var{false}, indicating the success of the method.  As test tool one
1403 could use the command line tool @code{dbus-send} in a shell:
1405 @example
1406 # dbus-send --session --print-reply \
1407     --dest="org.freedesktop.TextEditor" \
1408     "/org/freedesktop/TextEditor" \
1409     "org.freedesktop.TextEditor.OpenFile" string:"/etc/hosts"
1411 @print{} method return sender=:1.22 -> dest=:1.23 reply_serial=2
1412       boolean true
1413 @end example
1415 You can indicate an error by raising the Emacs signal
1416 @code{dbus-error}.  The handler above could be changed like this:
1418 @lisp
1419 (defun my-dbus-method-handler (&rest args)
1420   (unless (and (= (length args) 1) (stringp (car args)))
1421     (signal 'dbus-error (list (format "Wrong argument list: %S" args))))
1422   (condition-case err
1423       (find-file (car args))
1424     (error (signal 'dbus-error (cdr err))))
1425   t)
1427 @result{} my-dbus-method-handler
1428 @end lisp
1430 The test runs then
1432 @example
1433 # dbus-send --session --print-reply \
1434     --dest="org.freedesktop.TextEditor" \
1435     "/org/freedesktop/TextEditor" \
1436     "org.freedesktop.TextEditor.OpenFile" \
1437     string:"/etc/hosts" string:"/etc/passwd"
1439 @print{} Error org.freedesktop.DBus.Error.Failed:
1440    Wrong argument list: ("/etc/hosts" "/etc/passwd")
1441 @end example
1442 @end defun
1444 @defun dbus-register-property bus service path interface property access value &optional emits-signal dont-register-service
1445 With this function, an application declares a @var{property} on the D-Bus
1446 @var{bus}.
1448 @var{bus} is either the symbol @code{:system} or the symbol
1449 @code{:session}.
1451 @var{service} is the D-Bus service name of the D-Bus.  It must be a
1452 known name.
1454 @var{path} is the D-Bus object path @var{service} is registered (See
1455 discussion of @var{dont-register-service} below).
1457 @var{interface} is the name of the interface used at @var{path},
1458 @var{property} is the name of the property of @var{interface}.
1460 @var{access} indicates, whether the property can be changed by other
1461 services via D-Bus.  It must be either the symbol @code{:read} or
1462 @code{:readwrite}.  @var{value} is the initial value of the property,
1463 it can be of any valid type (see @code{dbus-call-method} for details).
1465 If @var{property} already exists on @var{path}, it will be
1466 overwritten.  For properties with access type @code{:read} this is the
1467 only way to change their values.  Properties with access type
1468 @code{:readwrite} can be changed by @code{dbus-set-property}.
1470 The interface @samp{org.freedesktop.DBus.Properties} is added to
1471 @var{path}, including a default handler for the @samp{Get},
1472 @samp{GetAll} and @samp{Set} methods of this interface.  When
1473 @var{emits-signal} is non-@code{nil}, the signal
1474 @samp{PropertiesChanged} is sent when the property is changed by
1475 @code{dbus-set-property}.
1477 When @var{dont-register-service} is non-@code{nil}, the known name
1478 @var{service} is not registered.  This means that other D-Bus clients
1479 have no way of noticing the newly registered method.  When interfaces
1480 are constructed incrementally by adding single methods or properties
1481 at a time, @var{dont-register-service} can be used to prevent other
1482 clients from discovering the still incomplete interface.
1484 @noindent Example:
1486 @lisp
1487 (dbus-register-property
1488   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1489   "org.freedesktop.TextEditor" "name" :read "GNU Emacs")
1491 @result{} ((:session "org.freedesktop.TextEditor" "name")
1492     ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
1494 (dbus-register-property
1495   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1496   "org.freedesktop.TextEditor" "version" :readwrite emacs-version t)
1498 @result{} ((:session "org.freedesktop.TextEditor" "version")
1499     ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
1500 @end lisp
1502 Other D-Bus applications can read the property via the default methods
1503 @samp{org.freedesktop.DBus.Properties.Get} and
1504 @samp{org.freedesktop.DBus.Properties.GetAll}.  Testing is also
1505 possible via the command line tool @code{dbus-send} in a shell:
1507 @example
1508 # dbus-send --session --print-reply \
1509     --dest="org.freedesktop.TextEditor" \
1510     "/org/freedesktop/TextEditor" \
1511     "org.freedesktop.DBus.Properties.GetAll" \
1512     string:"org.freedesktop.TextEditor"
1514 @print{} method return sender=:1.22 -> dest=:1.23 reply_serial=3
1515       array [
1516          dict entry(
1517             string "name"
1518             variant             string "GNU Emacs"
1519          )
1520          dict entry(
1521             string "version"
1522             variant             string "23.1.50.5"
1523          )
1524       ]
1525 @end example
1527 It is also possible, to apply the @code{dbus-get-property},
1528 @code{dbus-get-all-properties} and @code{dbus-set-property} functions
1529 (@pxref{Properties and Annotations}).
1531 @lisp
1532 (dbus-set-property
1533   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1534   "org.freedesktop.TextEditor" "version" "23.1.50")
1536 @result{} "23.1.50"
1538 (dbus-get-property
1539   :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1540   "org.freedesktop.TextEditor" "version")
1542 @result{} "23.1.50"
1543 @end lisp
1544 @end defun
1546 @defun dbus-unregister-object object
1547 Unregister @var{object} from the D-Bus.  @var{object} must be the
1548 result of a preceding @code{dbus-register-method},
1549 @code{dbus-register-property} or @code{dbus-register-signal} call
1550 (@pxref{Signals}).  It returns @code{t} if @var{object} has been
1551 unregistered, @code{nil} otherwise.
1553 When @var{object} identifies the last method or property, which is
1554 registered for the respective service, Emacs releases its association
1555 to the service from D-Bus.
1556 @end defun
1559 @node Signals
1560 @chapter Sending and receiving signals.
1561 @cindex signals
1563 Signals are broadcast messages.  They carry input parameters, which
1564 are received by all objects which have registered for such a signal.
1566 @defun dbus-send-signal bus service path interface signal &rest args
1567 This function is similar to @code{dbus-call-method}.  The difference
1568 is, that there are no returning output parameters.
1570 The function emits @var{signal} on the D-Bus @var{bus}.  @var{bus} is
1571 either the symbol @code{:system} or the symbol @code{:session}.  It
1572 doesn't matter whether another object has registered for @var{signal}.
1574 @var{service} is the D-Bus service name of the object the signal is
1575 emitted from.  @var{path} is the corresponding D-Bus object path,
1576 @var{service} is registered at.  @var{interface} is an interface
1577 offered by @var{service}.  It must provide @var{signal}.
1579 All other arguments args are passed to @var{signal} as arguments.
1580 They are converted into D-Bus types as described in @ref{Type
1581 Conversion}.  Example:
1583 @lisp
1584 (dbus-send-signal
1585   :session dbus-service-emacs dbus-path-emacs
1586   (concat dbus-service-emacs ".FileManager") "FileModified"
1587   "/home/albinus/.emacs")
1588 @end lisp
1589 @end defun
1591 @defun dbus-register-signal bus service path interface signal handler &rest args
1592 With this function, an application registers for @var{signal} on the
1593 D-Bus @var{bus}.
1595 @var{bus} is either the symbol @code{:system} or the symbol
1596 @code{:session}.
1598 @var{service} is the D-Bus service name used by the sending D-Bus
1599 object.  It can be either a known name or the unique name of the D-Bus
1600 object sending the signal.  In case of a unique name, signals won't be
1601 received any longer once the object owning this unique name has
1602 disappeared, and a new queued object has replaced it.
1604 When @var{service} is @code{nil}, related signals from all D-Bus
1605 objects shall be accepted.
1607 @var{path} is the corresponding D-Bus object path, @var{service} is
1608 registered at.  It can also be @code{nil} if the path name of incoming
1609 signals shall not be checked.
1611 @var{interface} is an interface offered by @var{service}.  It must
1612 provide @var{signal}.
1614 @var{handler} is a Lisp function to be called when the @var{signal} is
1615 received.  It must accept as arguments the output parameters
1616 @var{signal} is sending.
1618 All other arguments @var{args}, if specified, must be strings.  They
1619 stand for the respective arguments of @var{signal} in their order, and
1620 are used for filtering as well.  A @code{nil} argument might be used
1621 to preserve the order.
1623 @code{dbus-register-signal} returns a Lisp object, which can be used
1624 as argument in @code{dbus-unregister-object} for removing the
1625 registration for @var{signal}.  Example:
1627 @lisp
1628 (defun my-dbus-signal-handler (device)
1629   (message "Device %s added" device))
1631 @result{} my-dbus-signal-handler
1633 (dbus-register-signal
1634   :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1635   "org.freedesktop.Hal.Manager" "DeviceAdded"
1636   'my-dbus-signal-handler)
1638 @result{} ((:system "org.freedesktop.Hal.Manager" "DeviceAdded")
1639     ("org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1640      my-signal-handler))
1641 @end lisp
1643 As we know from the introspection data of interface
1644 @samp{org.freedesktop.Hal.Manager}, the signal @samp{DeviceAdded}
1645 provides one single parameter, which is mapped into a Lisp string.
1646 The callback function @code{my-dbus-signal-handler} must define one
1647 single string argument therefore.  Plugging an USB device to your
1648 machine, when registered for signal @samp{DeviceAdded}, will show you
1649 which objects the GNU/Linux @code{hal} daemon adds.
1650 @end defun
1653 @node Alternative Buses
1654 @chapter Alternative buses.
1655 @cindex bus names
1656 @cindex UNIX domain socket
1658 Until now, we have spoken about the system and the session buses,
1659 which are the default buses to be connected to.  However, it is
1660 possible to connect to any bus, from which the address is known.  This
1661 is a UNIX domain socket.  Everywhere, where a @var{bus} is mentioned
1662 as argument of a function (the symbol @code{:system} or the symbol
1663 @code{:session}), this address can be used instead.  The connection to
1664 this bus must be initialized first.
1666 @defun dbus-init-bus bus
1667 Establish the connection to D-Bus @var{bus}.
1669 @var{bus} can be either the symbol @code{:system} or the symbol
1670 @code{:session}, or it can be a string denoting the address of the
1671 corresponding bus.  For the system and session busses, this function
1672 is called when loading @file{dbus.el}, there is no need to call it
1673 again.
1675 Example: You open another session bus in a terminal window on your host:
1677 @example
1678 # eval `dbus-launch --auto-syntax`
1679 # echo $DBUS_SESSION_BUS_ADDRESS
1681 @print{} unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e
1682 @end example
1684 In Emacs, you can access to this bus via its address:
1686 @lisp
1687 (setq my-bus
1688       "unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e")
1690 @result{} "unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e"
1692 (dbus-init-bus my-bus)
1694 @result{} nil
1696 (dbus-get-unique-name my-bus)
1698 @result{} ":1.0"
1699 @end lisp
1700 @end defun
1703 @node Errors and Events
1704 @chapter Errors and events.
1705 @cindex debugging
1706 @cindex errors
1707 @cindex events
1709 The internal actions can be traced by running in a debug mode.
1711 @defvar dbus-debug
1712 If this variable is non-@code{nil}, D-Bus specific debug messages are raised.
1713 @end defvar
1715 Input parameters of @code{dbus-call-method},
1716 @code{dbus-call-method-non-blocking},
1717 @code{dbus-call-method-asynchronously}, and
1718 @code{dbus-register-signal} are checked for correct D-Bus types. If
1719 there is a type mismatch, the Lisp error @code{wrong-type-argument}
1720 @code{D-Bus ARG} is raised.
1722 All errors raised by D-Bus are signaled with the error symbol
1723 @code{dbus-error}.  If possible, error messages from D-Bus are
1724 appended to the @code{dbus-error}.
1726 @defspec dbus-ignore-errors forms@dots{}
1727 This executes @var{forms} exactly like a @code{progn}, except that
1728 @code{dbus-error} errors are ignored during the @var{forms}.  These
1729 errors can be made visible when @code{dbus-debug} is set to @code{t}.
1730 @end defspec
1732 Incoming D-Bus messages are handled as Emacs events, see @pxref{Misc
1733 Events, , , elisp}.  They are retrieved only, when Emacs runs in
1734 interactive mode.  The generated event has this form:
1736 @lisp
1737 (dbus-event @var{bus} @var{type} @var{serial} @var{service} @var{path} @var{interface} @var{member} @var{handler}
1738         &rest @var{args})
1739 @end lisp
1741 @var{bus} identifies the D-Bus the message is coming from.  It is
1742 either the symbol @code{:system} or the symbol @code{:session}.
1744 @var{type} is the D-Bus message type which has caused the event.  It
1745 can be @code{dbus-message-type-invalid},
1746 @code{dbus-message-type-method-call},
1747 @code{dbus-message-type-method-return},
1748 @code{dbus-message-type-error}, or @code{dbus-message-type-signal}.
1749 @var{serial} is the serial number of the received D-Bus message.
1751 @var{service} and @var{path} are the unique name and the object path
1752 of the D-Bus object emitting the message.  @var{interface} and
1753 @var{member} denote the message which has been sent.
1755 @var{handler} is the callback function which has been registered for
1756 this message (see @pxref{Signals}).  When a @code{dbus-event} event
1757 arrives, @var{handler} is called with @var{args} as arguments.
1759 In order to inspect the @code{dbus-event} data, you could extend the
1760 definition of the callback function in @ref{Signals}:
1762 @lisp
1763 (defun my-dbus-signal-handler (&rest args)
1764   (message "my-dbus-signal-handler: %S" last-input-event))
1765 @end lisp
1767 There exist convenience functions which could be called inside a
1768 callback function in order to retrieve the information from the event.
1770 @defun dbus-event-bus-name event
1771 Returns the bus name @var{event} is coming from.
1772 The result is either the symbol @code{:system} or the symbol @code{:session}.
1773 @end defun
1775 @defun dbus-event-message-type event
1776 Returns the message type of the corresponding D-Bus message.  The
1777 result is a natural number.
1778 @end defun
1780 @defun dbus-event-serial-number event
1781 Returns the serial number of the corresponding D-Bus message.
1782 The result is a natural number.
1783 @end defun
1785 @defun dbus-event-service-name event
1786 Returns the unique name of the D-Bus object @var{event} is coming from.
1787 @end defun
1789 @defun dbus-event-path-name event
1790 Returns the object path of the D-Bus object @var{event} is coming from.
1791 @end defun
1793 @defun dbus-event-interface-name event
1794 Returns the interface name of the D-Bus object @var{event} is coming from.
1795 @end defun
1797 @defun dbus-event-member-name event
1798 Returns the member name of the D-Bus object @var{event} is coming
1799 from.  It is either a signal name or a method name.
1800 @end defun
1802 D-Bus errors are not propagated during event handling, because it is
1803 usually not desired.  D-Bus errors in events can be made visible by
1804 setting the variable @code{dbus-debug} to @code{t}.  They can also be
1805 handled by a hook function.
1807 @defvar dbus-event-error-hooks
1808 This hook variable keeps a list of functions, which are called when a
1809 D-Bus error happens in the event handler.  Every function must accept
1810 two arguments, the event and the error variable catched in
1811 @code{condition-case} by @code{dbus-error}.
1813 Such functions can be used the adapt the error signal to be raised.
1814 Example:
1816 @lisp
1817 (defun my-dbus-event-error-handler (event error)
1818   (when (string-equal (concat dbus-service-emacs ".FileManager")
1819                       (dbus-event-interface-name event))
1820     (message "my-dbus-event-error-handler: %S %S" event error)
1821     (signal 'file-error (cdr error))))
1823 (add-hook 'dbus-event-error-hooks 'my-dbus-event-error-handler)
1824 @end lisp
1825 @end defvar
1827 Hook functions shall take into account, that there might be other
1828 D-Bus applications running.  Therefore, they shall check carefully,
1829 whether a given D-Bus error is related to them.
1832 @node Index
1833 @unnumbered Index
1835 @printindex cp
1838 @node GNU Free Documentation License
1839 @appendix GNU Free Documentation License
1840 @include doclicense.texi
1842 @bye