Fix typos in doc files (bug#16089).
[bpt/emacs.git] / doc / misc / dbus.texi
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
7
8 @syncodeindex vr cp
9 @syncodeindex fn cp
10
11 @copying
12 Copyright @copyright{} 2007--2013 Free Software Foundation, Inc.
13
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''.
21
22 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
23 modify this GNU manual.''
24 @end quotation
25 @end copying
26
27 @dircategory Emacs lisp libraries
28 @direntry
29 * D-Bus: (dbus). Using D-Bus in Emacs.
30 @end direntry
31
32 @contents
33
34
35 @node Top, Overview, (dir), (dir)
36 @top D-Bus integration in Emacs
37
38 This manual documents an API for usage of D-Bus in Emacs. D-Bus is a
39 message bus system, a simple way for applications to talk to one
40 another. An overview of D-Bus can be found at
41 @uref{http://dbus.freedesktop.org/}.
42
43 @ifnottex
44 @insertcopying
45 @end ifnottex
46
47 @menu
48 * Overview:: An overview of D-Bus.
49 * Inspection:: Inspection of D-Bus services.
50 * Type Conversion:: Mapping Lisp types and D-Bus types.
51 * Synchronous Methods:: Calling methods in a blocking way.
52 * Asynchronous Methods:: Calling methods non-blocking.
53 * Receiving Method Calls:: Offering own methods.
54 * Signals:: Sending and receiving signals.
55 * Alternative Buses:: Alternative buses and environments.
56 * Errors and Events:: Errors and events.
57 * Index:: Index including concepts, functions, variables.
58
59 * GNU Free Documentation License:: The license for this documentation.
60 @end menu
61
62
63 @node Overview
64 @chapter An overview of D-Bus
65 @cindex overview
66
67 D-Bus is an inter-process communication mechanism for applications
68 residing on the same host. The communication is based on
69 @dfn{messages}. Data in the messages is carried in a structured way,
70 it is not just a byte stream.
71
72 The communication is connection oriented to two kinds of message
73 buses: a so called @dfn{system bus}, and a @dfn{session bus}. On a
74 given machine, there is always one single system bus for miscellaneous
75 system-wide communication, like changing of hardware configuration.
76 On the other hand, the session bus is always related to a single
77 user's session.
78
79 Every client application, which is connected to a bus, registers under
80 a @dfn{unique name} at the bus. This name is used for identifying the
81 client application. Such a unique name starts always with a colon,
82 and looks like @samp{:1.42}.
83
84 Additionally, a client application can register itself to a so called
85 @dfn{known name}, which is a series of identifiers separated by dots,
86 as in @samp{org.gnu.Emacs}. If several applications register to the
87 same known name, these registrations are queued, and only the first
88 application which has registered for the known name is reachable via
89 this name. If this application disconnects from the bus, the next
90 queued unique name becomes the owner of this known name.
91
92 An application can install one or several objects under its name.
93 Such objects are identified by an @dfn{object path}, which looks
94 similar to paths in a filesystem. An example of such an object path
95 could be @samp{/org/gnu/Emacs/}.
96
97 Applications might send a request to an object, that means sending a
98 message with some data as input parameters, and receiving a message
99 from that object with the result of this message, the output
100 parameters. Such a request is called @dfn{method} in D-Bus.
101
102 The other form of communication are @dfn{signals}. The underlying
103 message is emitted from an object and will be received by all other
104 applications which have registered for such a signal.
105
106 All methods and signals an object supports are called @dfn{interface}
107 of the object. Interfaces are specified under a hierarchical name in
108 D-Bus; an object can support several interfaces. Such an interface
109 name could be @samp{org.gnu.Emacs.TextEditor} or
110 @samp{org.gnu.Emacs.FileManager}.
111
112
113 @node Inspection
114 @chapter Inspection of D-Bus services.
115 @cindex inspection
116
117 @menu
118 * Version:: Determining the D-Bus version.
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
126
127
128 @node Version
129 @section D-Bus version.
130
131 D-Bus has evolved over the years. New features have been added with
132 new D-Bus versions. There are two variables, which allow to determine
133 the used D-Bus version.
134
135 @defvar dbus-compiled-version
136 This variable, a string, determines the version of D-Bus Emacs is
137 compiled against. If it cannot be determined the value is @code{nil}.
138 @end defvar
139
140 @defvar dbus-runtime-version
141 The other D-Bus version to be checked is the version of D-Bus Emacs
142 runs with. This string can be different from @code{dbus-compiled-version}.
143 It is also @code{nil}, if it cannot be determined at runtime.
144 @end defvar
145
146
147 @node Bus names
148 @section Bus names.
149
150 There are several basic functions which inspect the buses for
151 registered names. Internally they use the basic interface
152 @samp{org.freedesktop.DBus}, which is supported by all objects of a bus.
153
154 @defun dbus-list-activatable-names &optional bus
155 This function returns the D-Bus service names, which can be activated
156 for @var{bus}. It must be either the symbol @code{:system} (the
157 default) or the symbol @code{:session}. An activatable service is
158 described in a service registration file. Under GNU/Linux, such files
159 are located at @file{/usr/share/dbus-1/system-services/} (for the
160 @code{:system} bus) or @file{/usr/share/dbus-1/services/}. An
161 activatable service is not necessarily registered at @var{bus} at already.
162
163 The result is a list of strings, which is @code{nil} when there are no
164 activatable service names at all. Example:
165
166 @lisp
167 ;; Check, whether the document viewer can be accessed via D-Bus.
168 (member "org.gnome.evince.Daemon"
169 (dbus-list-activatable-names :session))
170 @end lisp
171 @end defun
172
173 @defun dbus-list-names bus
174 All service names, which are registered at D-Bus @var{bus}, are
175 returned. The result is a list of strings, which is @code{nil} when
176 there are no registered service names at all. Well known names are
177 strings like @samp{org.freedesktop.DBus}. Names starting with
178 @samp{:} are unique names for services.
179
180 @var{bus} must be either the symbol @code{:system} or the symbol
181 @code{:session}.
182 @end defun
183
184 @defun dbus-list-known-names bus
185 Retrieves all registered services which correspond to a known name in @var{bus}.
186 A service has a known name if it doesn't start with @samp{:}. The
187 result is a list of strings, which is @code{nil} when there are no
188 known names at all.
189
190 @var{bus} must be either the symbol @code{:system} or the symbol
191 @code{:session}.
192 @end defun
193
194 @defun dbus-list-queued-owners bus service
195 For a given service, registered at D-Bus @var{bus} under the name
196 @var{service}, all queued unique names are returned. The result is a
197 list of strings, or @code{nil} when there are no queued names for
198 @var{service} at all.
199
200 @var{bus} must be either the symbol @code{:system} or the symbol
201 @code{:session}. @var{service} must be a known service name as
202 string.
203 @end defun
204
205 @defun dbus-get-name-owner bus service
206 For a given service, registered at D-Bus @var{bus} under the name
207 @var{service}, the unique name of the name owner is returned. The
208 result is a string, or @code{nil} when there exist no name owner of
209 @var{service}.
210
211 @var{bus} must be either the symbol @code{:system} or the symbol
212 @code{:session}. @var{service} must be a known service name as
213 string.
214 @end defun
215
216 @defun dbus-ping bus service &optional timeout
217 Check whether the service name @var{service} is registered at D-Bus
218 @var{bus}. @var{service} might not have been started yet, it is
219 autostarted if possible. The result is either @code{t} or @code{nil}.
220
221 @var{bus} must be either the symbol @code{:system} or the symbol
222 @code{:session}. @var{service} must be a string. @var{timeout}, a
223 nonnegative integer, specifies the maximum number of milliseconds
224 @code{dbus-ping} must return. The default value is 25,000. Example:
225
226 @lisp
227 (message
228 "%s screensaver on board."
229 (cond
230 ((dbus-ping :session "org.gnome.ScreenSaver" 100) "Gnome")
231 ((dbus-ping :session "org.freedesktop.ScreenSaver" 100) "KDE")
232 (t "No")))
233 @end lisp
234
235 If it shall be checked whether @var{service} is already running
236 without autostarting it, one shall apply
237
238 @lisp
239 (member service (dbus-list-known-names bus))
240 @end lisp
241 @end defun
242
243 @defun dbus-get-unique-name bus
244 The unique name, under which Emacs is registered at D-Bus @var{bus},
245 is returned as string.
246
247 @var{bus} must be either the symbol @code{:system} or the symbol
248 @code{:session}.
249 @end defun
250
251
252 @node Introspection
253 @section Knowing the details of D-Bus services.
254
255 D-Bus services publish their interfaces. This can be retrieved and
256 analyzed during runtime, in order to understand the used
257 implementation.
258
259 The resulting introspection data are in XML format. The root
260 introspection element is always a @code{node} element. It might have
261 a @code{name} attribute, which denotes the (absolute) object path an
262 interface is introspected.
263
264 The root @code{node} element may have @code{node} and @code{interface}
265 children. A child @code{node} element must have a @code{name}
266 attribute, this case it is the relative object path to the root
267 @code{node} element.
268
269 An @code{interface} element has just one attribute, @code{name}, which
270 is the full name of that interface. The default interface
271 @samp{org.freedesktop.DBus.Introspectable} is always present. Example:
272
273 @example
274 <node name="/org/bluez">
275 <interface name="org.freedesktop.DBus.Introspectable">
276 @dots{}
277 </interface>
278 <interface name="org.bluez.Manager">
279 @dots{}
280 </interface>
281 <interface name="org.bluez.Database">
282 @dots{}
283 </interface>
284 <interface name="org.bluez.Security">
285 @dots{}
286 </interface>
287 <node name="service_audio"/>
288 <node name="service_input"/>
289 <node name="service_network"/>
290 <node name="service_serial"/>
291 </node>
292 @end example
293
294 Children of an @code{interface} element can be @code{method},
295 @code{signal} and @code{property} elements. A @code{method} element
296 stands for a D-Bus method of the surrounding interface. The element
297 itself has a @code{name} attribute, showing the method name. Children
298 elements @code{arg} stand for the arguments of a method. Example:
299
300 @example
301 <method name="ResolveHostName">
302 <arg name="interface" type="i" direction="in"/>
303 <arg name="protocol" type="i" direction="in"/>
304 <arg name="name" type="s" direction="in"/>
305 <arg name="aprotocol" type="i" direction="in"/>
306 <arg name="flags" type="u" direction="in"/>
307 <arg name="interface" type="i" direction="out"/>
308 <arg name="protocol" type="i" direction="out"/>
309 <arg name="name" type="s" direction="out"/>
310 <arg name="aprotocol" type="i" direction="out"/>
311 <arg name="address" type="s" direction="out"/>
312 <arg name="flags" type="u" direction="out"/>
313 </method>
314 @end example
315
316 @code{arg} elements can have the attributes @code{name}, @code{type}
317 and @code{direction}. The @code{name} attribute is optional. The
318 @code{type} attribute stands for the @dfn{signature} of the argument
319 in D-Bus. For a discussion of D-Bus types and their Lisp
320 representation see @ref{Type Conversion}.@footnote{D-Bus signatures
321 are explained in the D-Bus specification
322 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures}.}
323 The @code{direction} attribute of an @code{arg} element can be only
324 @samp{in} or @samp{out}; in case it is omitted, it defaults to
325 @samp{in}.
326
327 A @code{signal} element of an @code{interface} has a similar
328 structure. The @code{direction} attribute of an @code{arg} child
329 element can be only @samp{out} here; which is also the default value.
330 Example:
331
332 @example
333 <signal name="StateChanged">
334 <arg name="state" type="i"/>
335 <arg name="error" type="s"/>
336 </signal>
337 @end example
338
339 A @code{property} element has no @code{arg} child
340 element. It just has the attributes @code{name}, @code{type} and
341 @code{access}, which are all mandatory. The @code{access} attribute
342 allows the values @samp{readwrite}, @samp{read}, and @samp{write}.
343 Example:
344
345 @example
346 <property name="Status" type="u" direction="read"/>
347 @end example
348
349 @code{annotation} elements can be children of @code{interface},
350 @code{method}, @code{signal}, and @code{property} elements. Unlike
351 properties, which can change their values during lifetime of a D-Bus
352 object, annotations are static. Often they are used for code
353 generators of D-Bus language bindings. Example:
354
355 @example
356 <annotation name="de.berlios.Pinot.GetStatistics" value="pinotDBus"/>
357 @end example
358
359 Annotations have just @code{name} and @code{value} attributes, both
360 must be strings.
361
362 @defun dbus-introspect bus service path
363 This function returns all interfaces and sub-nodes of @var{service},
364 registered at object path @var{path} at bus @var{bus}.
365
366 @var{bus} must be either the symbol @code{:system} or the symbol
367 @code{:session}. @var{service} must be a known service name, and
368 @var{path} must be a valid object path. The last two parameters are
369 strings. The result, the introspection data, is a string in XML
370 format. Example:
371
372 @lisp
373 (dbus-introspect
374 :system "org.freedesktop.Hal"
375 "/org/freedesktop/Hal/devices/computer")
376
377 @result{} "<!DOCTYPE node PUBLIC
378 "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
379 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
380 <node>
381 <interface name="org.freedesktop.Hal.Device">
382 <method name="GetAllProperties">
383 <arg name="properties" direction="out" type="a@{sv@}"/>
384 </method>
385 @dots{}
386 <signal name="PropertyModified">
387 <arg name="num_updates" type="i"/>
388 <arg name="updates" type="a(sbb)"/>
389 </signal>
390 </interface>
391 @dots{}
392 </node>"
393 @end lisp
394
395 This example informs us, that the service @samp{org.freedesktop.Hal}
396 at object path @samp{/org/freedesktop/Hal/devices/computer} offers the
397 interface @samp{org.freedesktop.Hal.Device} (and 2 other interfaces
398 not documented here). This interface contains the method
399 @samp{GetAllProperties}, which needs no input parameters, but returns
400 as output parameter an array of dictionary entries (key-value pairs).
401 Every dictionary entry has a string as key, and a variant as value.
402
403 The interface offers also a signal, which returns 2 parameters: an
404 integer, and an array consisting of elements which are a struct of a
405 string and 2 boolean values.@footnote{ The interfaces of the service
406 @samp{org.freedesktop.Hal} are described in
407 @c Previous link is gone. Since HAL is now obsolete, this URL
408 @c (unchanged in ~ 4 years) feels like it might go too...
409 @uref{http://people.freedesktop.org/~dkukawka/hal-spec-git/hal-spec.html#interfaces,
410 the HAL specification}.}
411 @end defun
412
413 @defun dbus-introspect-xml bus service path
414 This function has the same intention as function
415 @code{dbus-introspect}. The returned value is a parsed XML tree,
416 which can be used for further analysis. Example:
417
418 @lisp
419 (dbus-introspect-xml
420 :session "org.freedesktop.xesam.searcher"
421 "/org/freedesktop/xesam/searcher/main")
422
423 @result{} (node ((name . "/org/freedesktop/xesam/searcher/main"))
424 (interface ((name . "org.freedesktop.xesam.Search"))
425 (method ((name . "GetHitData"))
426 (arg ((name . "search") (type . "s") (direction . "in")))
427 (arg ((name . "hit_ids") (type . "au") (direction . "in")))
428 (arg ((name . "fields") (type . "as") (direction . "in")))
429 (arg ((name . "hit_data") (type . "aav") (direction . "out")))
430 )
431 @dots{}
432 (signal ((name . "HitsAdded"))
433 (arg ((name . "search") (type . "s")))
434 (arg ((name . "count") (type . "u")))
435 )
436 )
437 @dots{}
438 )
439 @end lisp
440 @end defun
441
442 @defun dbus-introspect-get-attribute object attribute
443 It returns the @var{attribute} value of a D-Bus introspection
444 @var{object}. @var{object} can be every subtree of a parsed XML tree
445 as retrieved with @code{dbus-introspect-xml}. @var{attribute} must be
446 a string according to the attribute names in the D-Bus specification.
447 Example:
448
449 @lisp
450 (dbus-introspect-get-attribute
451 (dbus-introspect-xml :system "org.freedesktop.SystemToolsBackends"
452 "/org/freedesktop/SystemToolsBackends/UsersConfig")
453 "name")
454
455 @result{} "/org/freedesktop/SystemToolsBackends/UsersConfig"
456 @end lisp
457
458 If @var{object} has no @var{attribute}, the function returns
459 @code{nil}.
460 @end defun
461
462
463 @node Nodes and Interfaces
464 @section Detecting object paths and interfaces.
465
466 The first elements, to be introspected for a D-Bus object, are further
467 object paths and interfaces.
468
469 @defun dbus-introspect-get-node-names bus service path
470 All node names of @var{service} in D-Bus @var{bus} at object path
471 @var{path} are returned as list of strings. Example:
472
473 @lisp
474 (dbus-introspect-get-node-names
475 :session "org.gnome.seahorse" "/org/gnome/seahorse")
476
477 @result{} ("crypto" "keys")
478 @end lisp
479
480 The node names stand for further object paths of the D-Bus
481 @var{service}, relative to @var{path}. In the example,
482 @samp{/org/gnome/seahorse/crypto} and @samp{/org/gnome/seahorse/keys}
483 are also object paths of the D-Bus service @samp{org.gnome.seahorse}.
484 @end defun
485
486 @defun dbus-introspect-get-all-nodes bus service path
487 This function returns all node names of @var{service} in D-Bus
488 @var{bus} at object path @var{path}. It returns a list of strings
489 with all object paths of @var{service}, starting at @var{path}.
490 Example:
491
492 @lisp
493 (dbus-introspect-get-all-nodes :session "org.gnome.seahorse" "/")
494
495 @result{} ("/" "/org" "/org/gnome" "/org/gnome/seahorse"
496 "/org/gnome/seahorse/crypto"
497 "/org/gnome/seahorse/keys"
498 "/org/gnome/seahorse/keys/openpgp"
499 "/org/gnome/seahorse/keys/openpgp/local"
500 "/org/gnome/seahorse/keys/openssh"
501 "/org/gnome/seahorse/keys/openssh/local")
502 @end lisp
503 @end defun
504
505 @defun dbus-introspect-get-interface-names bus service path
506 There will be returned a list strings of all interface names of
507 @var{service} in D-Bus @var{bus} at object path @var{path}. This list
508 will contain the default interface @samp{org.freedesktop.DBus.Introspectable}.
509
510 Another default interface is @samp{org.freedesktop.DBus.Properties}.
511 If present, @code{interface} elements can also have @code{property}
512 children. Example:
513
514 @lisp
515 (dbus-introspect-get-interface-names
516 :system "org.freedesktop.Hal"
517 "/org/freedesktop/Hal/devices/computer")
518
519 @result{} ("org.freedesktop.DBus.Introspectable"
520 "org.freedesktop.Hal.Device"
521 "org.freedesktop.Hal.Device.SystemPowerManagement"
522 "org.freedesktop.Hal.Device.CPUFreq")
523 @end lisp
524 @end defun
525
526 @defun dbus-introspect-get-interface bus service path interface
527 Return @var{interface} of @var{service} in D-Bus @var{bus} at object
528 path @var{path}. The return value is an XML element. @var{interface}
529 must be a string, element of the list returned by
530 @code{dbus-introspect-get-interface-names}. Example:
531
532 @lisp
533 (dbus-introspect-get-interface
534 :session "org.freedesktop.xesam.searcher"
535 "/org/freedesktop/xesam/searcher/main"
536 "org.freedesktop.xesam.Search")
537
538 @result{} (interface ((name . "org.freedesktop.xesam.Search"))
539 (method ((name . "GetHitData"))
540 (arg ((name . "search") (type . "s") (direction . "in")))
541 (arg ((name . "hit_ids") (type . "au") (direction . "in")))
542 (arg ((name . "fields") (type . "as") (direction . "in")))
543 (arg ((name . "hit_data") (type . "aav") (direction . "out")))
544 )
545 @dots{}
546 (signal ((name . "HitsAdded"))
547 (arg ((name . "search") (type . "s")))
548 (arg ((name . "count") (type . "u")))
549 )
550 )
551 @end lisp
552 @end defun
553
554 @noindent
555 With these functions, it is possible to retrieve all introspection
556 data from a running system:
557
558 @lisp
559 (with-current-buffer (switch-to-buffer "*introspect*")
560 (erase-buffer)
561 (dolist (service (dbus-list-known-names :session))
562 (dolist (path (dbus-introspect-get-all-nodes :session service "/"))
563 ;; We want to introspect only elements, which have more than
564 ;; the default interface "org.freedesktop.DBus.Introspectable".
565 (when (delete
566 "org.freedesktop.DBus.Introspectable"
567 (dbus-introspect-get-interface-names :session service path))
568 (insert (message "\nservice: \"%s\" path: \"%s\"\n" service path)
569 (dbus-introspect :session service path))
570 (redisplay t)))))
571 @end lisp
572
573
574 @node Methods and Signal
575 @section Applying the functionality.
576
577 Methods and signals are the communication means to D-Bus. The
578 following functions return their specifications.
579
580 @defun dbus-introspect-get-method-names bus service path interface
581 Return a list of strings of all method names of @var{interface} of
582 @var{service} in D-Bus @var{bus} at object path @var{path}. Example:
583
584 @lisp
585 (dbus-introspect-get-method-names
586 :session "org.freedesktop.xesam.searcher"
587 "/org/freedesktop/xesam/searcher/main"
588 "org.freedesktop.xesam.Search")
589
590 @result{} ("GetState" "StartSearch" "GetHitCount" "GetHits" "NewSession"
591 "CloseSession" "GetHitData" "SetProperty" "NewSearch"
592 "GetProperty" "CloseSearch")
593 @end lisp
594 @end defun
595
596 @defun dbus-introspect-get-method bus service path interface method
597 This function returns @var{method} of @var{interface} as XML element.
598 It must be located at @var{service} in D-Bus @var{bus} at object path
599 @var{path}. @var{method} must be a string, element of the list
600 returned by @code{dbus-introspect-get-method-names}. Example:
601
602 @lisp
603 (dbus-introspect-get-method
604 :session "org.freedesktop.xesam.searcher"
605 "/org/freedesktop/xesam/searcher/main"
606 "org.freedesktop.xesam.Search" "GetHitData")
607
608 @result{} (method ((name . "GetHitData"))
609 (arg ((name . "search") (type . "s") (direction . "in")))
610 (arg ((name . "hit_ids") (type . "au") (direction . "in")))
611 (arg ((name . "fields") (type . "as") (direction . "in")))
612 (arg ((name . "hit_data") (type . "aav") (direction . "out")))
613 )
614 @end lisp
615 @end defun
616
617 @defun dbus-introspect-get-signal-names bus service path interface
618 Return a list of strings of all signal names of @var{interface} of
619 @var{service} in D-Bus @var{bus} at object path @var{path}. Example:
620
621 @lisp
622 (dbus-introspect-get-signal-names
623 :session "org.freedesktop.xesam.searcher"
624 "/org/freedesktop/xesam/searcher/main"
625 "org.freedesktop.xesam.Search")
626
627 @result{} ("StateChanged" "SearchDone" "HitsModified"
628 "HitsRemoved" "HitsAdded")
629 @end lisp
630 @end defun
631
632 @defun dbus-introspect-get-signal bus service path interface signal
633 This function returns @var{signal} of @var{interface} as XML element.
634 It must be located at @var{service} in D-Bus @var{bus} at object path
635 @var{path}. @var{signal} must be a string, element of the list
636 returned by @code{dbus-introspect-get-signal-names}. Example:
637
638 @lisp
639 (dbus-introspect-get-signal
640 :session "org.freedesktop.xesam.searcher"
641 "/org/freedesktop/xesam/searcher/main"
642 "org.freedesktop.xesam.Search" "HitsAdded")
643
644 @result{} (signal ((name . "HitsAdded"))
645 (arg ((name . "search") (type . "s")))
646 (arg ((name . "count") (type . "u")))
647 )
648 @end lisp
649 @end defun
650
651
652 @node Properties and Annotations
653 @section What else to know about interfaces.
654
655 Interfaces can have properties. These can be exposed via the
656 @samp{org.freedesktop.DBus.Properties} interface@footnote{See
657 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties}}.
658 That is, properties can be retrieved and changed during lifetime of an
659 element.
660
661 A generalized interface is
662 @samp{org.freedesktop.DBus.Objectmanager}@footnote{See
663 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-objectmanager}},
664 which returns objects, their interfaces and properties for a given
665 service in just one call.
666
667 Annotations, on the other hand, are static values for an element.
668 Often, they are used to instruct generators, how to generate code from
669 the interface for a given language binding.
670
671 @defun dbus-introspect-get-property-names bus service path interface
672 Return a list of strings with all property names of @var{interface} of
673 @var{service} in D-Bus @var{bus} at object path @var{path}. Example:
674
675 @lisp
676 (dbus-introspect-get-property-names
677 :session "org.kde.kded" "/modules/networkstatus"
678 "org.kde.Solid.Networking.Client")
679
680 @result{} ("Status")
681 @end lisp
682
683 If an interface declares properties, the corresponding element supports
684 also the @samp{org.freedesktop.DBus.Properties} interface.
685 @end defun
686
687 @defun dbus-introspect-get-property bus service path interface property
688 This function returns @var{property} of @var{interface} as XML element.
689 It must be located at @var{service} in D-Bus @var{bus} at object path
690 @var{path}. @var{property} must be a string, element of the list
691 returned by @code{dbus-introspect-get-property-names}.
692
693 A @var{property} value can be retrieved by the function
694 @code{dbus-introspect-get-attribute}. Example:
695
696 @lisp
697 (dbus-introspect-get-property
698 :session "org.kde.kded" "/modules/networkstatus"
699 "org.kde.Solid.Networking.Client" "Status")
700
701 @result{} (property ((access . "read") (type . "u") (name . "Status")))
702
703 (dbus-introspect-get-attribute
704 (dbus-introspect-get-property
705 :session "org.kde.kded" "/modules/networkstatus"
706 "org.kde.Solid.Networking.Client" "Status")
707 "access")
708
709 @result{} "read"
710 @end lisp
711 @end defun
712
713 @defun dbus-get-property bus service path interface property
714 This function returns the value of @var{property} of @var{interface}.
715 It will be checked at @var{bus}, @var{service}, @var{path}. The
716 result can be any valid D-Bus value, or @code{nil} if there is no
717 @var{property}. Example:
718
719 @lisp
720 (dbus-get-property
721 :session "org.kde.kded" "/modules/networkstatus"
722 "org.kde.Solid.Networking.Client" "Status")
723
724 @result{} 4
725 @end lisp
726 @end defun
727
728 @defun dbus-set-property bus service path interface property value
729 Set value of @var{property} of @var{interface} to @var{value}. It
730 will be checked at @var{bus}, @var{service}, @var{path}. When the
731 value has been set successful, the result is @var{value}. Otherwise,
732 @code{nil} is returned. Example:
733
734 @lisp
735 (dbus-set-property
736 :session "org.kde.kaccess" "/MainApplication"
737 "com.trolltech.Qt.QApplication" "doubleClickInterval" 500)
738
739 @result{} 500
740 @end lisp
741 @end defun
742
743 @defun dbus-get-all-properties bus service path interface
744 This function returns all properties of @var{interface}. It will be
745 checked at @var{bus}, @var{service}, @var{path}. The result is a list
746 of cons. Every cons contains the name of the property, and its value.
747 If there are no properties, @code{nil} is returned. Example:
748
749 @lisp
750 (dbus-get-all-properties
751 :session "org.kde.kaccess" "/MainApplication"
752 "com.trolltech.Qt.QApplication")
753
754 @result{} (("cursorFlashTime" . 1000) ("doubleClickInterval" . 500)
755 ("keyboardInputInterval" . 400) ("wheelScrollLines" . 3)
756 ("globalStrut" 0 0) ("startDragTime" . 500)
757 ("startDragDistance" . 4) ("quitOnLastWindowClosed" . t)
758 ("styleSheet" . ""))
759 @end lisp
760 @end defun
761
762 @defun dbus-get-all-managed-objects bus service path
763 This function returns all objects at @var{bus}, @var{service},
764 @var{path}, and the children of @var{path}. The result is a list of
765 objects. Every object is a cons of an existing path name, and the
766 list of available interface objects. An interface object is another
767 cons, which car is the interface name, and the cdr is the list of
768 properties as returned by @code{dbus-get-all-properties} for that path
769 and interface. Example:
770
771 @lisp
772 (dbus-get-all-managed-objects
773 :session "org.gnome.SettingsDaemon" "/")
774
775 @result{} (("/org/gnome/SettingsDaemon/MediaKeys"
776 ("org.gnome.SettingsDaemon.MediaKeys")
777 ("org.freedesktop.DBus.Peer")
778 ("org.freedesktop.DBus.Introspectable")
779 ("org.freedesktop.DBus.Properties")
780 ("org.freedesktop.DBus.ObjectManager"))
781 ("/org/gnome/SettingsDaemon/Power"
782 ("org.gnome.SettingsDaemon.Power.Keyboard")
783 ("org.gnome.SettingsDaemon.Power.Screen")
784 ("org.gnome.SettingsDaemon.Power"
785 ("Icon" . ". GThemedIcon battery-full-charged-symbolic ")
786 ("Tooltip" . "Laptop battery is charged"))
787 ("org.freedesktop.DBus.Peer")
788 ("org.freedesktop.DBus.Introspectable")
789 ("org.freedesktop.DBus.Properties")
790 ("org.freedesktop.DBus.ObjectManager"))
791 @dots{})
792 @end lisp
793
794 If possible, @samp{org.freedesktop.DBus.ObjectManager.GetManagedObjects}
795 is used for retrieving the information. Otherwise, the information
796 is collected via @samp{org.freedesktop.DBus.Introspectable.Introspect}
797 and @samp{org.freedesktop.DBus.Properties.GetAll}, which is slow.
798
799 An overview of all existing object paths, their interfaces and
800 properties could be retrieved by the following code:
801
802 @lisp
803 (with-current-buffer (switch-to-buffer "*objectmanager*")
804 (erase-buffer)
805 (let (result)
806 (dolist (service (dbus-list-known-names :session) result)
807 (message "%s" service)
808 (add-to-list
809 'result
810 (cons service
811 (dbus-get-all-managed-objects :session service "/"))))
812 (insert (message "%s" (pp result)))
813 (redisplay t)))
814 @end lisp
815 @end defun
816
817 @defun dbus-introspect-get-annotation-names bus service path interface &optional name
818 Return a list of all annotation names as list of strings. If
819 @var{name} is @code{nil}, the annotations are children of
820 @var{interface}, otherwise @var{name} must be a @code{method},
821 @code{signal}, or @code{property} XML element, where the annotations
822 belong to. Example:
823
824 @lisp
825 (dbus-introspect-get-annotation-names
826 :session "de.berlios.Pinot" "/de/berlios/Pinot"
827 "de.berlios.Pinot" "GetStatistics")
828
829 @result{} ("de.berlios.Pinot.GetStatistics")
830 @end lisp
831
832 Default annotation names@footnote{See
833 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format}}
834 are
835
836 @table @samp
837 @item org.freedesktop.DBus.Deprecated
838 Whether or not the entity is deprecated; defaults to @code{nil}
839
840 @item org.freedesktop.DBus.GLib.CSymbol
841 The C symbol; may be used for @code{methods} and @code{interfaces}
842
843 @item org.freedesktop.DBus.Method.NoReply
844 If set, don't expect a reply to the @code{method} call; defaults to @code{nil}
845 @end table
846 @end defun
847
848 @defun dbus-introspect-get-annotation bus service path interface name annotation
849 Return annotation @var{ANNOTATION} as XML object. If @var{name} is
850 @code{nil}, @var{ANNOTATION} is a child of @var{interface}, otherwise
851 @var{name} must be the name of a @code{method}, @code{signal}, or
852 @code{property} XML element, where the @var{ANNOTATION} belongs to.
853
854 An attribute value can be retrieved by
855 @code{dbus-introspect-get-attribute}. Example:
856
857 @lisp
858 (dbus-introspect-get-annotation
859 :session "de.berlios.Pinot" "/de/berlios/Pinot"
860 "de.berlios.Pinot" "GetStatistics"
861 "de.berlios.Pinot.GetStatistics")
862
863 @result{} (annotation ((name . "de.berlios.Pinot.GetStatistics")
864 (value . "pinotDBus")))
865
866 (dbus-introspect-get-attribute
867 (dbus-introspect-get-annotation
868 :session "de.berlios.Pinot" "/de/berlios/Pinot"
869 "de.berlios.Pinot" "GetStatistics"
870 "de.berlios.Pinot.GetStatistics")
871 "value")
872
873 @result{} "pinotDBus"
874 @end lisp
875 @end defun
876
877
878 @node Arguments and Signatures
879 @section The final details.
880
881 Methods and signals have arguments. They are described in the
882 @code{arg} XML elements.
883
884 @defun dbus-introspect-get-argument-names bus service path interface name
885 Return a list of all argument names as list of strings. @var{name}
886 must be a @code{method} or @code{signal} XML element. Example:
887
888 @lisp
889 (dbus-introspect-get-argument-names
890 :session "org.freedesktop.xesam.searcher"
891 "/org/freedesktop/xesam/searcher/main"
892 "org.freedesktop.xesam.Search" "GetHitData")
893
894 @result{} ("search" "hit_ids" "fields" "hit_data")
895 @end lisp
896
897 Argument names are optional; the function can return @code{nil}
898 therefore, even if the method or signal has arguments.
899 @end defun
900
901 @defun dbus-introspect-get-argument bus service path interface name arg
902 Return argument @var{ARG} as XML object. @var{name}
903 must be a @code{method} or @code{signal} XML element. Example:
904
905 @lisp
906 (dbus-introspect-get-argument
907 :session "org.freedesktop.xesam.searcher"
908 "/org/freedesktop/xesam/searcher/main"
909 "org.freedesktop.xesam.Search" "GetHitData" "search")
910
911 @result{} (arg ((name . "search") (type . "s") (direction . "in")))
912 @end lisp
913 @end defun
914
915 @defun dbus-introspect-get-signature bus service path interface name &optional direction
916 Return signature of a @code{method} or @code{signal}, represented by
917 @var{name}, as string.
918
919 If @var{name} is a @code{method}, @var{direction} can be either
920 @samp{in} or @samp{out}. If @var{direction} is @code{nil}, @samp{in}
921 is assumed.
922
923 If @var{name} is a @code{signal}, and @var{direction} is
924 non-@code{nil}, @var{direction} must be @samp{out}. Example:
925
926 @lisp
927 (dbus-introspect-get-signature
928 :session "org.freedesktop.xesam.searcher"
929 "/org/freedesktop/xesam/searcher/main"
930 "org.freedesktop.xesam.Search" "GetHitData" "in")
931
932 @result{} "sauas"
933
934 (dbus-introspect-get-signature
935 :session "org.freedesktop.xesam.searcher"
936 "/org/freedesktop/xesam/searcher/main"
937 "org.freedesktop.xesam.Search" "HitsAdded")
938
939 @result{} "su"
940 @end lisp
941 @end defun
942
943
944 @node Type Conversion
945 @chapter Mapping Lisp types and D-Bus types.
946 @cindex type conversion
947
948 D-Bus method calls and signals accept usually several arguments as
949 parameters, either as input parameter, or as output parameter. Every
950 argument belongs to a D-Bus type.
951
952 Such arguments must be mapped between the value encoded as a D-Bus
953 type, and the corresponding type of Lisp objects. The mapping is
954 applied Lisp object @expansion{} D-Bus type for input parameters, and
955 D-Bus type @expansion{} Lisp object for output parameters.
956
957
958 @section Input parameters.
959
960 Input parameters for D-Bus methods and signals occur as arguments of a
961 Lisp function call. The following mapping to D-Bus types is
962 applied, when the corresponding D-Bus message is created:
963
964 @example
965 @multitable {negative integer} {@expansion{}} {DBUS_TYPE_BOOLEAN}
966 @item Lisp type @tab @tab D-Bus type
967 @item
968 @item @code{t} and @code{nil} @tab @expansion{} @tab DBUS_TYPE_BOOLEAN
969 @item natural number @tab @expansion{} @tab DBUS_TYPE_UINT32
970 @item negative integer @tab @expansion{} @tab DBUS_TYPE_INT32
971 @item float @tab @expansion{} @tab DBUS_TYPE_DOUBLE
972 @item string @tab @expansion{} @tab DBUS_TYPE_STRING
973 @item list @tab @expansion{} @tab DBUS_TYPE_ARRAY
974 @end multitable
975 @end example
976
977 Other Lisp objects, like symbols or hash tables, are not accepted as
978 input parameters.
979
980 If it is necessary to use another D-Bus type, a corresponding type
981 symbol can be prepended to the corresponding Lisp object. Basic D-Bus
982 types are represented by the type symbols @code{:byte},
983 @code{:boolean}, @code{:int16}, @code{:uint16}, @code{:int32},
984 @code{:uint32}, @code{:int64}, @code{:uint64}, @code{:double},
985 @code{:string}, @code{:object-path}, @code{:signature} and
986 @code{:unix-fd}.
987
988 @noindent
989 Example:
990
991 @lisp
992 (dbus-call-method @dots{} @var{NAT-NUMBER} @var{STRING})
993 @end lisp
994
995 is equivalent to
996
997 @lisp
998 (dbus-call-method @dots{} :uint32 @var{NAT-NUMBER} :string @var{STRING})
999 @end lisp
1000
1001 but different to
1002
1003 @lisp
1004 (dbus-call-method @dots{} :int32 @var{NAT-NUMBER} :signature @var{STRING})
1005 @end lisp
1006
1007 The value for a byte D-Bus type can be any integer in the range 0
1008 through 255. If a character is used as argument, modifiers
1009 represented outside this range are stripped of. For example,
1010 @code{:byte ?x} is equal to @code{:byte ?\M-x}, but it is not equal to
1011 @code{:byte ?\C-x} or @code{:byte ?\M-\C-x}.
1012
1013 Signed and unsigned integer D-Bus types expect a corresponding integer
1014 value. If the value does not fit Emacs's integer range, it is also
1015 possible to use an equivalent floating point number.
1016
1017 A D-Bus compound type is always represented as a list. The @sc{car}
1018 of this list can be the type symbol @code{:array}, @code{:variant},
1019 @code{:struct} or @code{:dict-entry}, which would result in a
1020 corresponding D-Bus container. @code{:array} is optional, because
1021 this is the default compound D-Bus type for a list.
1022
1023 The objects being elements of the list are checked according to the
1024 D-Bus compound type rules.
1025
1026 @itemize
1027 @item An array must contain only elements of the same D-Bus type. It
1028 can be empty.
1029
1030 @item A variant must contain only one single element.
1031
1032 @item A dictionary entry must be element of an array, and it must
1033 contain only a key-value pair of two elements, with a basic D-Bus type
1034 key.
1035
1036 @item There is no restriction for structs.
1037 @end itemize
1038
1039 If an empty array needs an element D-Bus type other than string, it
1040 can contain exactly one element of D-Bus type @code{:signature}. The
1041 value of this element (a string) is used as the signature of the
1042 elements of this array. Example:
1043
1044 @lisp
1045 (dbus-call-method
1046 :session "org.freedesktop.Notifications"
1047 "/org/freedesktop/Notifications"
1048 "org.freedesktop.Notifications" "Notify"
1049 "GNU Emacs" ;; Application name.
1050 0 ;; No replacement of other notifications.
1051 "" ;; No icon.
1052 "Notification summary" ;; Summary.
1053 (format ;; Body.
1054 "This is a test notification, raised from %s" (emacs-version))
1055 '(:array) ;; No actions (empty array of strings).
1056 '(:array :signature "@{sv@}") ;; No hints
1057 ;; (empty array of dictionary entries).
1058 :int32 -1) ;; Default timeout.
1059
1060 @result{} 3
1061 @end lisp
1062
1063 @defun dbus-string-to-byte-array string
1064 Sometimes, D-Bus methods require as input parameter an array of bytes,
1065 instead of a string. If it is guaranteed, that @var{string} is an
1066 UTF8 string, this function performs the conversion. Example:
1067
1068 @lisp
1069 (dbus-string-to-byte-array "/etc/hosts")
1070
1071 @result{} (:array :byte 47 :byte 101 :byte 116 :byte 99 :byte 47
1072 :byte 104 :byte 111 :byte 115 :byte 116 :byte 115)
1073 @end lisp
1074 @end defun
1075
1076 @defun dbus-escape-as-identifier string
1077 Escape an arbitrary @var{string} so it follows the rules for a C
1078 identifier. The escaped string can be used as object path component,
1079 interface element component, bus name component or member name in
1080 D-Bus.
1081
1082 The escaping consists of replacing all non-alphanumerics, and the
1083 first character if it's a digit, with an underscore and two
1084 lower-case hex digits. As a special case, "" is escaped to
1085 "_". Example:
1086
1087 @lisp
1088 (dbus-escape-as-identifier "0123abc_xyz\x01\xff")
1089
1090 @result{} "_30123abc_5fxyz_01_ff"
1091 @end lisp
1092 @end defun
1093
1094
1095 @section Output parameters.
1096
1097 Output parameters of D-Bus methods and signals are mapped to Lisp
1098 objects.
1099
1100 @example
1101 @multitable {DBUS_TYPE_OBJECT_PATH} {@expansion{}} {natural number or float}
1102 @item D-Bus type @tab @tab Lisp type
1103 @item
1104 @item DBUS_TYPE_BOOLEAN @tab @expansion{} @tab @code{t} or @code{nil}
1105 @item DBUS_TYPE_BYTE @tab @expansion{} @tab natural number
1106 @item DBUS_TYPE_UINT16 @tab @expansion{} @tab natural number
1107 @item DBUS_TYPE_INT16 @tab @expansion{} @tab integer
1108 @item DBUS_TYPE_UINT32 @tab @expansion{} @tab natural number or float
1109 @item DBUS_TYPE_UNIX_FD @tab @expansion{} @tab natural number or float
1110 @item DBUS_TYPE_INT32 @tab @expansion{} @tab integer or float
1111 @item DBUS_TYPE_UINT64 @tab @expansion{} @tab natural number or float
1112 @item DBUS_TYPE_INT64 @tab @expansion{} @tab integer or float
1113 @item DBUS_TYPE_DOUBLE @tab @expansion{} @tab float
1114 @item DBUS_TYPE_STRING @tab @expansion{} @tab string
1115 @item DBUS_TYPE_OBJECT_PATH @tab @expansion{} @tab string
1116 @item DBUS_TYPE_SIGNATURE @tab @expansion{} @tab string
1117 @item DBUS_TYPE_ARRAY @tab @expansion{} @tab list
1118 @item DBUS_TYPE_VARIANT @tab @expansion{} @tab list
1119 @item DBUS_TYPE_STRUCT @tab @expansion{} @tab list
1120 @item DBUS_TYPE_DICT_ENTRY @tab @expansion{} @tab list
1121 @end multitable
1122 @end example
1123
1124 A float object in case of @code{DBUS_TYPE_UINT32},
1125 @code{DBUS_TYPE_INT32}, @code{DBUS_TYPE_UINT64},
1126 @code{DBUS_TYPE_INT64} and @code{DBUS_TYPE_UNIX_FD} is returned, when
1127 the C value exceeds the Emacs number size range.
1128
1129 The resulting list of the last 4 D-Bus compound types contains as
1130 elements the elements of the D-Bus container, mapped according to the
1131 same rules.
1132
1133 The signal @code{PropertyModified}, discussed as example in
1134 @ref{Inspection}, would offer as Lisp data the following object
1135 (@var{BOOL} stands here for either @code{nil} or @code{t}):
1136
1137 @lisp
1138 (@var{INTEGER} ((@var{STRING} @var{BOOL} @var{BOOL}) (@var{STRING} @var{BOOL} @var{BOOL}) @dots{}))
1139 @end lisp
1140
1141 @defun dbus-byte-array-to-string byte-array &optional multibyte
1142 If a D-Bus method or signal returns an array of bytes, which are known
1143 to represent an UTF8 string, this function converts @var{byte-array}
1144 to the corresponding string. The string is unibyte encoded, unless
1145 @var{multibyte} is non-@code{nil}. Example:
1146
1147 @lisp
1148 (dbus-byte-array-to-string '(47 101 116 99 47 104 111 115 116 115))
1149
1150 @result{} "/etc/hosts"
1151 @end lisp
1152 @end defun
1153
1154 @defun dbus-unescape-from-identifier string
1155 Retrieve the original string from the encoded @var{string} as unibyte
1156 string. @var{string} must have been encoded with
1157 @code{dbus-escape-as-identifier}. Example:
1158
1159 @lisp
1160 (dbus-unescape-from-identifier "_30123abc_5fxyz_01_ff")
1161
1162 @result{} "0123abc_xyz\x01\xff"
1163 @end lisp
1164
1165 If the original string used in @code{dbus-escape-as-identifier} is a
1166 multibyte string, it cannot be expected that this function returns
1167 that string:
1168
1169 @lisp
1170 (string-equal
1171 (dbus-unescape-from-identifier
1172 (dbus-escape-as-identifier "Grüß Göttin"))
1173 "Grüß Göttin")
1174
1175 @result{} nil
1176 @end lisp
1177
1178
1179 @end defun
1180
1181
1182 @node Synchronous Methods
1183 @chapter Calling methods in a blocking way.
1184 @cindex method calls, synchronous
1185 @cindex synchronous method calls
1186
1187 Methods can be called synchronously (@dfn{blocking}) or asynchronously
1188 (@dfn{non-blocking}).
1189
1190 At D-Bus level, a method call consist of two messages: one message
1191 which carries the input parameters to the object owning the method to
1192 be called, and a reply message returning the resulting output
1193 parameters from the object.
1194
1195 @defun dbus-call-method bus service path interface method &optional :timeout timeout &rest args
1196 This function calls @var{method} on the D-Bus @var{bus}. @var{bus} is
1197 either the symbol @code{:system} or the symbol @code{:session}.
1198
1199 @var{service} is the D-Bus service name to be used. @var{path} is the
1200 D-Bus object path, @var{service} is registered at. @var{interface} is
1201 an interface offered by @var{service}. It must provide @var{method}.
1202
1203 If the parameter @code{:timeout} is given, the following integer
1204 @var{timeout} specifies the maximum number of milliseconds the method
1205 call must return. The default value is 25,000. If the method call
1206 doesn't return in time, a D-Bus error is raised (@pxref{Errors and
1207 Events}).
1208
1209 All other arguments args are passed to @var{method} as arguments.
1210 They are converted into D-Bus types as described in @ref{Type
1211 Conversion}.
1212
1213 The function returns the resulting values of @var{method} as a list of
1214 Lisp objects, according to the type conversion rules described in
1215 @ref{Type Conversion}. Example:
1216
1217 @lisp
1218 (dbus-call-method
1219 :session "org.gnome.seahorse" "/org/gnome/seahorse/keys/openpgp"
1220 "org.gnome.seahorse.Keys" "GetKeyField"
1221 "openpgp:657984B8C7A966DD" "simple-name")
1222
1223 @result{} (t ("Philip R. Zimmermann"))
1224 @end lisp
1225
1226 If the result of the method call is just one value, the converted Lisp
1227 object is returned instead of a list containing this single Lisp
1228 object. Example:
1229
1230 @lisp
1231 (dbus-call-method
1232 :system "org.freedesktop.Hal"
1233 "/org/freedesktop/Hal/devices/computer"
1234 "org.freedesktop.Hal.Device" "GetPropertyString"
1235 "system.kernel.machine")
1236
1237 @result{} "i686"
1238 @end lisp
1239
1240 With the @code{dbus-introspect} function it is possible to explore the
1241 interfaces of @samp{org.freedesktop.Hal} service. It offers the
1242 interfaces @samp{org.freedesktop.Hal.Manager} for the object at the
1243 path @samp{/org/freedesktop/Hal/Manager} as well as the interface
1244 @samp{org.freedesktop.Hal.Device} for all objects prefixed with the
1245 path @samp{/org/freedesktop/Hal/devices}. With the methods
1246 @samp{GetAllDevices} and @samp{GetAllProperties}, it is simple to
1247 emulate the @code{lshal} command on GNU/Linux systems:
1248
1249 @lisp
1250 (dolist (device
1251 (dbus-call-method
1252 :system "org.freedesktop.Hal"
1253 "/org/freedesktop/Hal/Manager"
1254 "org.freedesktop.Hal.Manager" "GetAllDevices"))
1255 (message "\nudi = %s" device)
1256 (dolist (properties
1257 (dbus-call-method
1258 :system "org.freedesktop.Hal" device
1259 "org.freedesktop.Hal.Device" "GetAllProperties"))
1260 (message " %s = %S"
1261 (car properties) (or (caar (cdr properties)) ""))))
1262
1263 @print{} "udi = /org/freedesktop/Hal/devices/computer
1264 info.addons = (\"hald-addon-acpi\")
1265 info.bus = \"unknown\"
1266 info.product = \"Computer\"
1267 info.subsystem = \"unknown\"
1268 info.udi = \"/org/freedesktop/Hal/devices/computer\"
1269 linux.sysfs_path_device = \"(none)\"
1270 power_management.acpi.linux.version = \"20051216\"
1271 power_management.can_suspend_to_disk = t
1272 power_management.can_suspend_to_ram = \"\"
1273 power_management.type = \"acpi\"
1274 smbios.bios.release_date = \"11/07/2001\"
1275 system.chassis.manufacturer = \"COMPAL\"
1276 system.chassis.type = \"Notebook\"
1277 system.firmware.release_date = \"03/19/2005\"
1278 @dots{}"
1279 @end lisp
1280 @end defun
1281
1282
1283 @node Asynchronous Methods
1284 @chapter Calling methods non-blocking.
1285 @cindex method calls, asynchronous
1286 @cindex asynchronous method calls
1287
1288 @defun dbus-call-method-asynchronously bus service path interface method handler &optional :timeout timeout &rest args
1289 This function calls @var{method} on the D-Bus @var{bus}
1290 asynchronously. @var{bus} is either the symbol @code{:system} or the
1291 symbol @code{:session}.
1292
1293 @var{service} is the D-Bus service name to be used. @var{path} is the
1294 D-Bus object path, @var{service} is registered at. @var{interface} is
1295 an interface offered by @var{service}. It must provide @var{method}.
1296
1297 @var{handler} is a Lisp function, which is called when the
1298 corresponding return message has arrived. If @var{handler} is
1299 @code{nil}, no return message will be expected.
1300
1301 If the parameter @code{:timeout} is given, the following integer
1302 @var{timeout} specifies the maximum number of milliseconds a reply
1303 message must arrive. The default value is 25,000. If there is no
1304 reply message in time, a D-Bus error is raised (@pxref{Errors and
1305 Events}).
1306
1307 All other arguments args are passed to @var{method} as arguments.
1308 They are converted into D-Bus types as described in @ref{Type
1309 Conversion}.
1310
1311 If @var{handler} is a Lisp function, the function returns a key into
1312 the hash table @code{dbus-registered-objects-table}. The
1313 corresponding entry in the hash table is removed, when the return
1314 message has been arrived, and @var{handler} is called. Example:
1315
1316 @lisp
1317 (dbus-call-method-asynchronously
1318 :system "org.freedesktop.Hal"
1319 "/org/freedesktop/Hal/devices/computer"
1320 "org.freedesktop.Hal.Device" "GetPropertyString" 'message
1321 "system.kernel.machine")
1322
1323 @result{} (:serial :system 2)
1324
1325 @print{} i686
1326 @end lisp
1327 @end defun
1328
1329
1330 @node Receiving Method Calls
1331 @chapter Offering own methods.
1332 @cindex method calls, returning
1333 @cindex returning method calls
1334
1335 In order to register methods on the D-Bus, Emacs has to request a well
1336 known name on the D-Bus under which it will be available for other
1337 clients. Names on the D-Bus can be registered and unregistered using
1338 the following functions:
1339
1340 @defun dbus-register-service bus service &rest flags
1341 Register the known name @var{service} on D-Bus @var{bus}.
1342
1343 @var{bus} is either the symbol @code{:system} or the symbol
1344 @code{:session}.
1345
1346 @var{service} is the service name to be registered on the D-Bus. It
1347 must be a known name.
1348
1349 @var{flags} is a subset of the following keywords:
1350
1351 @itemize
1352 @item @code{:allow-replacement}: Allow another service to become the primary
1353 owner if requested.
1354
1355 @item @code{:replace-existing}: Request to replace the current primary owner.
1356
1357 @item @code{:do-not-queue}: If we can not become the primary owner do not
1358 place us in the queue.
1359 @end itemize
1360
1361 One of the following keywords is returned:
1362
1363 @itemize
1364
1365 @item @code{:primary-owner}: We have become the primary owner of the name
1366 @var{service}.
1367
1368 @item @code{:in-queue}: We could not become the primary owner and
1369 have been placed in the queue.
1370
1371 @item @code{:exists}: We already are in the queue.
1372
1373 @item @code{:already-owner}: We already are the primary
1374 owner.
1375 @end itemize
1376 @end defun
1377
1378 @defun dbus-unregister-service bus service
1379 Unregister all objects from D-Bus @var{bus}, registered by Emacs for
1380 @var{service}.
1381
1382 @var{bus} is either the symbol @code{:system} or the symbol
1383 @code{:session}.
1384
1385 @var{service} is the D-Bus service name of the D-Bus. It must be a
1386 known name. Emacs releases its association to @var{service} from
1387 D-Bus.
1388
1389 One of the following keywords is returned:
1390
1391 @itemize
1392 @item @code{:released}: We successfully released the name @var{service}.
1393 @item @code{:non-existent}: The name @var{service} does not exist on the bus.
1394 @item @code{:not-owner}: We are not an owner of the name @var{service}.
1395 @end itemize
1396 @end defun
1397
1398 When a name has been chosen, Emacs can offer own methods, which can be
1399 called by other applications. These methods could be an
1400 implementation of an interface of a well known service, like
1401 @samp{org.freedesktop.TextEditor}.
1402
1403 It could be also an implementation of an own interface. In this case,
1404 the service name must be @samp{org.gnu.Emacs}. The object path shall
1405 begin with @samp{/org/gnu/Emacs/@strong{Application}}, and the
1406 interface name shall be @code{org.gnu.Emacs.@strong{Application}}.
1407 @samp{@strong{Application}} is the name of the application which
1408 provides the interface.
1409
1410 @deffn Constant dbus-service-emacs
1411 The well known service name @samp{org.gnu.Emacs} of Emacs.
1412 @end deffn
1413
1414 @deffn Constant dbus-path-emacs
1415 The object path namespace @samp{/org/gnu/Emacs} used by Emacs.
1416 @end deffn
1417
1418 @deffn Constant dbus-interface-emacs
1419 The interface namespace @code{org.gnu.Emacs} used by Emacs.
1420 @end deffn
1421
1422 @defun dbus-register-method bus service path interface method handler dont-register-service
1423 With this function, an application registers @var{method} on the D-Bus
1424 @var{bus}.
1425
1426 @var{bus} is either the symbol @code{:system} or the symbol
1427 @code{:session}.
1428
1429 @var{service} is the D-Bus service name of the D-Bus object
1430 @var{method} is registered for. It must be a known name (See
1431 discussion of @var{dont-register-service} below).
1432
1433 @var{path} is the D-Bus object path @var{service} is registered (See
1434 discussion of @var{dont-register-service} below).
1435
1436 @var{interface} is the interface offered by @var{service}. It must
1437 provide @var{method}.
1438
1439 @var{handler} is a Lisp function to be called when a @var{method} call
1440 is received. It must accept as arguments the input arguments of
1441 @var{method}. @var{handler} should return a list, whose elements are
1442 to be used as arguments for the reply message of @var{method}. This
1443 list can be composed like the input parameters in @ref{Type
1444 Conversion}.
1445
1446 If @var{handler} wants to return just one Lisp object and it is not a
1447 cons cell, @var{handler} can return this object directly, instead of
1448 returning a list containing the object.
1449
1450 In case @var{handler} shall return a reply message with an empty
1451 argument list, @var{handler} must return the symbol @code{:ignore}.
1452
1453 When @var{dont-register-service} is non-@code{nil}, the known name
1454 @var{service} is not registered. This means that other D-Bus clients
1455 have no way of noticing the newly registered method. When interfaces
1456 are constructed incrementally by adding single methods or properties
1457 at a time, @var{dont-register-service} can be used to prevent other
1458 clients from discovering the still incomplete interface.
1459
1460 The default D-Bus timeout when waiting for a message reply is 25
1461 seconds. This value could be even smaller, depending on the calling
1462 client. Therefore, @var{handler} shall not last longer than
1463 absolutely necessary.
1464
1465 @code{dbus-register-method} returns a Lisp object, which can be used
1466 as argument in @code{dbus-unregister-object} for removing the
1467 registration for @var{method}. Example:
1468
1469 @lisp
1470 (defun my-dbus-method-handler (filename)
1471 (let (result)
1472 (if (find-file filename)
1473 (setq result '(:boolean t))
1474 (setq result '(:boolean nil)))
1475 result))
1476
1477 @result{} my-dbus-method-handler
1478
1479 (dbus-register-method
1480 :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1481 "org.freedesktop.TextEditor" "OpenFile"
1482 'my-dbus-method-handler)
1483
1484 @result{} ((:method :session "org.freedesktop.TextEditor" "OpenFile")
1485 ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1486 my-dbus-method-handler))
1487 @end lisp
1488
1489 If you invoke the method @samp{org.freedesktop.TextEditor.OpenFile}
1490 from another D-Bus application with a filename as parameter, the file
1491 is opened in Emacs, and the method returns either @var{true} or
1492 @var{false}, indicating the success of the method. As test tool one
1493 could use the command line tool @code{dbus-send} in a shell:
1494
1495 @example
1496 # dbus-send --session --print-reply \
1497 --dest="org.freedesktop.TextEditor" \
1498 "/org/freedesktop/TextEditor" \
1499 "org.freedesktop.TextEditor.OpenFile" string:"/etc/hosts"
1500
1501 @print{} method return sender=:1.22 -> dest=:1.23 reply_serial=2
1502 boolean true
1503 @end example
1504
1505 You can indicate an error by raising the Emacs signal
1506 @code{dbus-error}. The handler above could be changed like this:
1507
1508 @lisp
1509 (defun my-dbus-method-handler (&rest args)
1510 (unless (and (= (length args) 1) (stringp (car args)))
1511 (signal 'dbus-error (list (format "Wrong argument list: %S" args))))
1512 (condition-case err
1513 (find-file (car args))
1514 (error (signal 'dbus-error (cdr err))))
1515 t)
1516
1517 @result{} my-dbus-method-handler
1518 @end lisp
1519
1520 The test runs then
1521
1522 @example
1523 # dbus-send --session --print-reply \
1524 --dest="org.freedesktop.TextEditor" \
1525 "/org/freedesktop/TextEditor" \
1526 "org.freedesktop.TextEditor.OpenFile" \
1527 string:"/etc/hosts" string:"/etc/passwd"
1528
1529 @print{} Error org.freedesktop.DBus.Error.Failed:
1530 Wrong argument list: ("/etc/hosts" "/etc/passwd")
1531 @end example
1532 @end defun
1533
1534 @defun dbus-register-property bus service path interface property access value &optional emits-signal dont-register-service
1535 With this function, an application declares a @var{property} on the D-Bus
1536 @var{bus}.
1537
1538 @var{bus} is either the symbol @code{:system} or the symbol
1539 @code{:session}.
1540
1541 @var{service} is the D-Bus service name of the D-Bus. It must be a
1542 known name.
1543
1544 @var{path} is the D-Bus object path @var{service} is registered (See
1545 discussion of @var{dont-register-service} below).
1546
1547 @var{interface} is the name of the interface used at @var{path},
1548 @var{property} is the name of the property of @var{interface}.
1549
1550 @var{access} indicates, whether the property can be changed by other
1551 services via D-Bus. It must be either the symbol @code{:read} or
1552 @code{:readwrite}. @var{value} is the initial value of the property,
1553 it can be of any valid type (see @code{dbus-call-method} for details).
1554
1555 If @var{property} already exists on @var{path}, it will be
1556 overwritten. For properties with access type @code{:read} this is the
1557 only way to change their values. Properties with access type
1558 @code{:readwrite} can be changed by @code{dbus-set-property}.
1559
1560 The interface @samp{org.freedesktop.DBus.Properties} is added to
1561 @var{path}, including a default handler for the @samp{Get},
1562 @samp{GetAll} and @samp{Set} methods of this interface. When
1563 @var{emits-signal} is non-@code{nil}, the signal
1564 @samp{PropertiesChanged} is sent when the property is changed by
1565 @code{dbus-set-property}.
1566
1567 When @var{dont-register-service} is non-@code{nil}, the known name
1568 @var{service} is not registered. This means that other D-Bus clients
1569 have no way of noticing the newly registered method. When interfaces
1570 are constructed incrementally by adding single methods or properties
1571 at a time, @var{dont-register-service} can be used to prevent other
1572 clients from discovering the still incomplete interface.
1573
1574 @noindent Example:
1575
1576 @lisp
1577 (dbus-register-property
1578 :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1579 "org.freedesktop.TextEditor" "name" :read "GNU Emacs")
1580
1581 @result{} ((:property :session "org.freedesktop.TextEditor" "name")
1582 ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
1583
1584 (dbus-register-property
1585 :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1586 "org.freedesktop.TextEditor" "version" :readwrite emacs-version t)
1587
1588 @result{} ((:property :session "org.freedesktop.TextEditor" "version")
1589 ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
1590 @end lisp
1591
1592 Other D-Bus applications can read the property via the default methods
1593 @samp{org.freedesktop.DBus.Properties.Get} and
1594 @samp{org.freedesktop.DBus.Properties.GetAll}. Testing is also
1595 possible via the command line tool @code{dbus-send} in a shell:
1596
1597 @example
1598 # dbus-send --session --print-reply \
1599 --dest="org.freedesktop.TextEditor" \
1600 "/org/freedesktop/TextEditor" \
1601 "org.freedesktop.DBus.Properties.GetAll" \
1602 string:"org.freedesktop.TextEditor"
1603
1604 @print{} method return sender=:1.22 -> dest=:1.23 reply_serial=3
1605 array [
1606 dict entry(
1607 string "name"
1608 variant string "GNU Emacs"
1609 )
1610 dict entry(
1611 string "version"
1612 variant string "23.1.50.5"
1613 )
1614 ]
1615 @end example
1616
1617 It is also possible, to apply the @code{dbus-get-property},
1618 @code{dbus-get-all-properties} and @code{dbus-set-property} functions
1619 (@pxref{Properties and Annotations}).
1620
1621 @lisp
1622 (dbus-set-property
1623 :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1624 "org.freedesktop.TextEditor" "version" "23.1.50")
1625
1626 @result{} "23.1.50"
1627
1628 (dbus-get-property
1629 :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1630 "org.freedesktop.TextEditor" "version")
1631
1632 @result{} "23.1.50"
1633 @end lisp
1634 @end defun
1635
1636 @defun dbus-unregister-object object
1637 Unregister @var{object} from the D-Bus. @var{object} must be the
1638 result of a preceding @code{dbus-register-method},
1639 @code{dbus-register-property} or @code{dbus-register-signal} call
1640 (@pxref{Signals}). It returns @code{t} if @var{object} has been
1641 unregistered, @code{nil} otherwise.
1642
1643 When @var{object} identifies the last method or property, which is
1644 registered for the respective service, Emacs releases its association
1645 to the service from D-Bus.
1646 @end defun
1647
1648
1649 @node Signals
1650 @chapter Sending and receiving signals.
1651 @cindex signals
1652
1653 Signals are one way messages. They carry input parameters, which are
1654 received by all objects which have registered for such a signal.
1655
1656 @defun dbus-send-signal bus service path interface signal &rest args
1657 This function is similar to @code{dbus-call-method}. The difference
1658 is, that there are no returning output parameters.
1659
1660 The function emits @var{signal} on the D-Bus @var{bus}. @var{bus} is
1661 either the symbol @code{:system} or the symbol @code{:session}. It
1662 doesn't matter whether another object has registered for @var{signal}.
1663
1664 Signals can be unicast or broadcast messages. For broadcast messages,
1665 @var{service} must be @code{nil}. Otherwise, @var{service} is the
1666 D-Bus service name the signal is sent to as unicast
1667 message.@footnote{For backward compatibility, a broadcast message is
1668 also emitted if @var{service} is the known or unique name Emacs is
1669 registered at D-Bus @var{bus}.} @var{path} is the D-Bus object path
1670 @var{signal} is sent from. @var{interface} is an interface available
1671 at @var{path}. It must provide @var{signal}.
1672
1673 All other arguments args are passed to @var{signal} as arguments.
1674 They are converted into D-Bus types as described in @ref{Type
1675 Conversion}. Example:
1676
1677 @lisp
1678 (dbus-send-signal
1679 :session nil dbus-path-emacs
1680 (concat dbus-interface-emacs ".FileManager") "FileModified"
1681 "/home/albinus/.emacs")
1682 @end lisp
1683 @end defun
1684
1685 @defun dbus-register-signal bus service path interface signal handler &rest args
1686 With this function, an application registers for a signal on the D-Bus
1687 @var{bus}.
1688
1689 @var{bus} is either the symbol @code{:system} or the symbol
1690 @code{:session}.
1691
1692 @var{service} is the D-Bus service name used by the sending D-Bus
1693 object. It can be either a known name or the unique name of the D-Bus
1694 object sending the signal. A known name will be mapped onto the
1695 unique name of the object, owning @var{service} at registration time.
1696 When the corresponding D-Bus object disappears, signals won't be
1697 received any longer.
1698
1699 @var{path} is the corresponding D-Bus object path, @var{service} is
1700 registered at. @var{interface} is an interface offered by
1701 @var{service}. It must provide @var{signal}.
1702
1703 @var{service}, @var{path}, @var{interface} and @var{signal} can be
1704 @code{nil}. This is interpreted as a wildcard for the respective
1705 argument.
1706
1707 @var{handler} is a Lisp function to be called when the @var{signal} is
1708 received. It must accept as arguments the output parameters
1709 @var{signal} is sending.
1710
1711 The remaining arguments @var{args} can be keywords or keyword string
1712 pairs.@footnote{For backward compatibility, the arguments @var{args}
1713 can also be just strings. They stand for the respective arguments of
1714 @var{signal} in their order, and are used for filtering as well. A
1715 @code{nil} argument might be used to preserve the order.} The meaning
1716 is as follows:
1717
1718 @itemize
1719 @item @code{:argN} @var{string}:@*
1720 @code{:pathN} @var{string}:@*
1721 This stands for the Nth argument of the signal. @code{:pathN}
1722 arguments can be used for object path wildcard matches as specified by
1723 D-Bus, while an @code{:argN} argument requires an exact match.
1724
1725 @item @code{:arg-namespace} @var{string}:@*
1726 Register for the signals, which first argument defines the service or
1727 interface namespace @var{string}.
1728
1729 @item @code{:path-namespace} @var{string}:@*
1730 Register for the object path namespace @var{string}. All signals sent
1731 from an object path, which has @var{string} as the preceding string,
1732 are matched. This requires @var{path} to be @code{nil}.
1733
1734 @item @code{:eavesdrop}:@*
1735 Register for unicast signals which are not directed to the D-Bus
1736 object Emacs is registered at D-Bus BUS, if the security policy of BUS
1737 allows this. Otherwise, this argument is ignored.
1738 @end itemize
1739
1740 @code{dbus-register-signal} returns a Lisp object, which can be used
1741 as argument in @code{dbus-unregister-object} for removing the
1742 registration for @var{signal}. Example:
1743
1744 @lisp
1745 (defun my-dbus-signal-handler (device)
1746 (message "Device %s added" device))
1747
1748 @result{} my-dbus-signal-handler
1749
1750 (dbus-register-signal
1751 :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1752 "org.freedesktop.Hal.Manager" "DeviceAdded"
1753 'my-dbus-signal-handler)
1754
1755 @result{} ((:signal :system "org.freedesktop.Hal.Manager" "DeviceAdded")
1756 ("org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1757 my-signal-handler))
1758 @end lisp
1759
1760 As we know from the introspection data of interface
1761 @samp{org.freedesktop.Hal.Manager}, the signal @samp{DeviceAdded}
1762 provides one single parameter, which is mapped into a Lisp string.
1763 The callback function @code{my-dbus-signal-handler} must define one
1764 single string argument therefore. Plugging an USB device to your
1765 machine, when registered for signal @samp{DeviceAdded}, will show you
1766 which objects the GNU/Linux @code{hal} daemon adds.
1767
1768 Some of the match rules have been added to a later version of D-Bus.
1769 In order to test the availability of such features, you could register
1770 for a dummy signal, and check the result:
1771
1772 @lisp
1773 (dbus-ignore-errors
1774 (dbus-register-signal
1775 :system nil nil nil nil 'ignore :path-namespace "/invalid/path"))
1776
1777 @result{} nil
1778 @end lisp
1779 @end defun
1780
1781
1782 @node Alternative Buses
1783 @chapter Alternative buses and environments.
1784 @cindex bus names
1785 @cindex UNIX domain socket
1786 @cindex TCP/IP socket
1787
1788 Until now, we have spoken about the system and the session buses,
1789 which are the default buses to be connected to. However, it is
1790 possible to connect to any bus, from which the address is known. This
1791 is a UNIX domain or TCP/IP socket. Everywhere, where a @var{bus} is
1792 mentioned as argument of a function (the symbol @code{:system} or the
1793 symbol @code{:session}), this address can be used instead. The
1794 connection to this bus must be initialized first.
1795
1796 @defun dbus-init-bus bus &optional private
1797 Establish the connection to D-Bus @var{bus}.
1798
1799 @var{bus} can be either the symbol @code{:system} or the symbol
1800 @code{:session}, or it can be a string denoting the address of the
1801 corresponding bus. For the system and session buses, this function
1802 is called when loading @file{dbus.el}, there is no need to call it
1803 again.
1804
1805 The function returns a number, which counts the connections this Emacs
1806 session has established to the @var{bus} under the same unique name
1807 (see @code{dbus-get-unique-name}). It depends on the libraries Emacs
1808 is linked with, and on the environment Emacs is running. For example,
1809 if Emacs is linked with the gtk toolkit, and it runs in a GTK-aware
1810 environment like Gnome, another connection might already be
1811 established.
1812
1813 When @var{private} is non-@code{nil}, a new connection is established
1814 instead of reusing an existing one. It results in a new unique name
1815 at the bus. This can be used, if it is necessary to distinguish from
1816 another connection used in the same Emacs process, like the one
1817 established by GTK+. It should be used with care for at least the
1818 @code{:system} and @code{:session} buses, because other Emacs Lisp
1819 packages might already use this connection to those buses.
1820
1821 Example: You initialize a connection to the AT-SPI bus on your host:
1822
1823 @lisp
1824 (setq my-bus
1825 (dbus-call-method
1826 :session "org.a11y.Bus" "/org/a11y/bus"
1827 "org.a11y.Bus" "GetAddress"))
1828
1829 @result{} "unix:abstract=/tmp/dbus-2yzWHOCdSD,guid=a490dd26625870ca1298b6e10000fd7f"
1830
1831 ;; If Emacs is built with gtk support, and you run in a GTK enabled
1832 ;; environment (like a GNOME session), the initialization reuses the
1833 ;; connection established by GTK's atk bindings.
1834 (dbus-init-bus my-bus)
1835
1836 @result{} 2
1837
1838 (dbus-get-unique-name my-bus)
1839
1840 @result{} ":1.19"
1841
1842 ;; Open a new connection to the same bus. This obsoletes the
1843 ;; previous one.
1844 (dbus-init-bus my-bus 'private)
1845
1846 @result{} 1
1847
1848 (dbus-get-unique-name my-bus)
1849
1850 @result{} ":1.20"
1851 @end lisp
1852
1853 D-Bus addresses can specify different transport. A possible address
1854 could be based on TCP/IP sockets, see next example. However, it
1855 depends on the bus daemon configuration, which transport is supported.
1856 @end defun
1857
1858 @defun dbus-setenv bus variable value
1859 Set the value of the @var{bus} environment variable @var{variable} to
1860 @var{value}.
1861
1862 @var{bus} is either a Lisp symbol, @code{:system} or @code{:session},
1863 or a string denoting the bus address. Both @var{variable} and
1864 @var{value} should be strings.
1865
1866 Normally, services inherit the environment of the bus daemon. This
1867 function adds to or modifies that environment when activating services.
1868
1869 Some bus instances, such as @code{:system}, may disable setting the
1870 environment. In such cases, or if this feature is not available in
1871 older D-Bus versions, a @code{dbus-error} error is raised.
1872
1873 As an example, it might be desirable to start X11 enabled services on
1874 a remote host's bus on the same X11 server the local Emacs is
1875 running. This could be achieved by
1876
1877 @lisp
1878 (setq my-bus "unix:host=example.gnu.org,port=4711")
1879
1880 @result{} "unix:host=example.gnu.org,port=4711"
1881
1882 (dbus-init-bus my-bus)
1883
1884 @result{} 1
1885
1886 (dbus-setenv my-bus "DISPLAY" (getenv "DISPLAY"))
1887
1888 @result{} nil
1889 @end lisp
1890 @end defun
1891
1892
1893 @node Errors and Events
1894 @chapter Errors and events.
1895 @cindex debugging
1896 @cindex errors
1897 @cindex events
1898
1899 The internal actions can be traced by running in a debug mode.
1900
1901 @defvar dbus-debug
1902 If this variable is non-@code{nil}, D-Bus specific debug messages are raised.
1903 @end defvar
1904
1905 Input parameters of @code{dbus-call-method},
1906 @code{dbus-call-method-asynchronously}, @code{dbus-send-signal},
1907 @code{dbus-register-method}, @code{dbus-register-property} and
1908 @code{dbus-register-signal} are checked for correct D-Bus types. If
1909 there is a type mismatch, the Lisp error @code{wrong-type-argument}
1910 @code{D-Bus ARG} is raised.
1911
1912 All errors raised by D-Bus are signaled with the error symbol
1913 @code{dbus-error}. If possible, error messages from D-Bus are
1914 appended to the @code{dbus-error}.
1915
1916 @defspec dbus-ignore-errors forms@dots{}
1917 This executes @var{forms} exactly like a @code{progn}, except that
1918 @code{dbus-error} errors are ignored during the @var{forms}. These
1919 errors can be made visible when @code{dbus-debug} is set to @code{t}.
1920 @end defspec
1921
1922 Incoming D-Bus messages are handled as Emacs events, see @pxref{Misc
1923 Events, , , elisp}. They are retrieved only, when Emacs runs in
1924 interactive mode. The generated event has this form:
1925
1926 @lisp
1927 (dbus-event @var{bus} @var{type} @var{serial} @var{service} @var{path} @var{interface} @var{member} @var{handler}
1928 &rest @var{args})
1929 @end lisp
1930
1931 @var{bus} identifies the D-Bus the message is coming from. It is
1932 either the symbol @code{:system} or the symbol @code{:session}.
1933
1934 @var{type} is the D-Bus message type which has caused the event. It
1935 can be @code{dbus-message-type-invalid},
1936 @code{dbus-message-type-method-call},
1937 @code{dbus-message-type-method-return},
1938 @code{dbus-message-type-error}, or @code{dbus-message-type-signal}.
1939 @var{serial} is the serial number of the received D-Bus message.
1940
1941 @var{service} and @var{path} are the unique name and the object path
1942 of the D-Bus object emitting the message. @var{interface} and
1943 @var{member} denote the message which has been sent.
1944
1945 @var{handler} is the callback function which has been registered for
1946 this message (see @pxref{Signals}). When a @code{dbus-event} event
1947 arrives, @var{handler} is called with @var{args} as arguments.
1948
1949 In order to inspect the @code{dbus-event} data, you could extend the
1950 definition of the callback function in @ref{Signals}:
1951
1952 @lisp
1953 (defun my-dbus-signal-handler (&rest args)
1954 (message "my-dbus-signal-handler: %S" last-input-event))
1955 @end lisp
1956
1957 There exist convenience functions which could be called inside a
1958 callback function in order to retrieve the information from the event.
1959
1960 @defun dbus-event-bus-name event
1961 Returns the bus name @var{event} is coming from.
1962 The result is either the symbol @code{:system} or the symbol @code{:session}.
1963 @end defun
1964
1965 @defun dbus-event-message-type event
1966 Returns the message type of the corresponding D-Bus message. The
1967 result is a natural number.
1968 @end defun
1969
1970 @defun dbus-event-serial-number event
1971 Returns the serial number of the corresponding D-Bus message.
1972 The result is a natural number.
1973 @end defun
1974
1975 @defun dbus-event-service-name event
1976 Returns the unique name of the D-Bus object @var{event} is coming from.
1977 @end defun
1978
1979 @defun dbus-event-path-name event
1980 Returns the object path of the D-Bus object @var{event} is coming from.
1981 @end defun
1982
1983 @defun dbus-event-interface-name event
1984 Returns the interface name of the D-Bus object @var{event} is coming from.
1985 @end defun
1986
1987 @defun dbus-event-member-name event
1988 Returns the member name of the D-Bus object @var{event} is coming
1989 from. It is either a signal name or a method name.
1990 @end defun
1991
1992 D-Bus errors are not propagated during event handling, because it is
1993 usually not desired. D-Bus errors in events can be made visible by
1994 setting the variable @code{dbus-debug} to @code{t}. They can also be
1995 handled by a hook function.
1996
1997 @defvar dbus-event-error-functions
1998 This hook variable keeps a list of functions, which are called when a
1999 D-Bus error happens in the event handler. Every function must accept
2000 two arguments, the event and the error variable caught in
2001 @code{condition-case} by @code{dbus-error}.
2002
2003 Such functions can be used the adapt the error signal to be raised.
2004 Example:
2005
2006 @lisp
2007 (defun my-dbus-event-error-handler (event error)
2008 (when (string-equal (concat dbus-interface-emacs ".FileManager")
2009 (dbus-event-interface-name event))
2010 (message "my-dbus-event-error-handler: %S %S" event error)
2011 (signal 'file-error (cdr error))))
2012
2013 (add-hook 'dbus-event-error-functions 'my-dbus-event-error-handler)
2014 @end lisp
2015 @end defvar
2016
2017 Hook functions shall take into account, that there might be other
2018 D-Bus applications running. Therefore, they shall check carefully,
2019 whether a given D-Bus error is related to them.
2020
2021
2022 @node Index
2023 @unnumbered Index
2024
2025 @printindex cp
2026
2027
2028 @node GNU Free Documentation License
2029 @appendix GNU Free Documentation License
2030 @include doclicense.texi
2031
2032 @bye