]> code.delx.au - gnu-emacs/blob - doc/misc/dbus.texi
* dbus.texi (Errors and Events): Fix typos. Describe second parameter
[gnu-emacs] / 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 @copying
9 Copyright @copyright{} 2007, 2008, 2009 Free Software Foundation, Inc.
10
11 @quotation
12 Permission is granted to copy, distribute and/or modify this document
13 under the terms of the GNU Free Documentation License, Version 1.3 or
14 any later version published by the Free Software Foundation; with no
15 Invariant Sections, with the Front-Cover texts being ``A GNU Manual'',
16 and with the Back-Cover Texts as in (a) below. A copy of the license
17 is included in the section entitled ``GNU Free Documentation License''.
18
19 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
20 modify this GNU manual. Buying copies from the FSF supports it in
21 developing GNU and promoting software freedom.''
22 @end quotation
23 @end copying
24
25 @dircategory Emacs
26 @direntry
27 * D-Bus: (dbus). Using D-Bus in Emacs.
28 @end direntry
29
30
31 @node Top, Overview, (dir), (dir)
32 @top D-Bus integration in Emacs
33
34 This manual documents an API for usage of D-Bus in
35 Emacs.@footnote{D-Bus is not enabled by default. You must run
36 @command{./configure --with-dbus} in Emacs' top level directory,
37 before you compile Emacs.} D-Bus is a message bus system, a simple
38 way for applications to talk to one another. An overview of D-Bus can
39 be found at @uref{http://dbus.freedesktop.org/}.
40
41 @insertcopying
42
43 @menu
44 * Overview:: An overview of D-Bus.
45 * Inspection:: Inspection of D-Bus services.
46 * Type Conversion:: Mapping Lisp types and D-Bus types.
47 * Synchronous Methods:: Calling methods in a blocking way.
48 * Asynchronous Methods:: Calling methods non-blocking.
49 * Receiving Method Calls:: Offering own methods.
50 * Signals:: Sending and receiving signals.
51 * Errors and Events:: Errors and events.
52 * GNU Free Documentation License:: The license for this documentation.
53 @end menu
54
55
56 @node Overview
57 @chapter An overview of D-Bus
58 @cindex overview
59
60 D-Bus is an inter-process communication mechanism for applications
61 residing on the same host. The communication is based on
62 @dfn{messages}. Data in the messages is carried in a structured way,
63 it is not just a byte stream.
64
65 The communication is connection oriented to two kinds of message
66 buses: a so called @dfn{system bus}, and a @dfn{session bus}. On a
67 given machine, there is always one single system bus for miscellaneous
68 system-wide communication, like changing of hardware configuration.
69 On the other hand, the session bus is always related to a single
70 user's session.
71
72 Every client application, which is connected to a bus, registers under
73 a @dfn{unique name} at the bus. This name is used for identifying the
74 client application. Such a unique name starts always with a colon,
75 and looks like @samp{:1.42}.
76
77 Additionally, a client application can register itself to a so called
78 @dfn{known name}, which is a series of identifiers separated by dots,
79 as in @samp{org.gnu.Emacs}. If several applications register to the
80 same known name, these registrations are queued, and only the first
81 application which has registered for the known name is reachable via
82 this name. If this application disconnects from the bus, the next
83 queued unique name becomes the owner of this known name.
84
85 An application can install one or several objects under its name.
86 Such objects are identified by an @dfn{object path}, which looks
87 similar to paths in a filesystem. An example of such an object path
88 could be @samp{/org/gnu/Emacs/}.
89
90 Applications might send a request to an object, that means sending a
91 message with some data as input parameters, and receiving a message
92 from that object with the result of this message, the output
93 parameters. Such a request is called @dfn{method} in D-Bus.
94
95 The other form of communication are @dfn{signals}. The underlying
96 message is emitted from an object and will be received by all other
97 applications which have registered for such a signal.
98
99 All methods and signals an object supports are called @dfn{interface}
100 of the object. Interfaces are specified under a hierarchical name in
101 D-Bus; an object can support several interfaces. Such an interface
102 name could be @samp{org.gnu.Emacs.TextEditor} or
103 @samp{org.gnu.Emacs.FileManager}.
104
105
106 @node Inspection
107 @chapter Inspection of D-Bus services.
108 @cindex inspection
109
110 @menu
111 * Bus names:: Discovering D-Bus names.
112 * Introspection:: Knowing the details of D-Bus services.
113 * Nodes and Interfaces:: Detecting object paths and interfaces.
114 * Methods and Signal:: Applying the functionality.
115 * Properties and Annotations:: What else to know about interfaces.
116 * Arguments and Signatures:: The final details.
117 @end menu
118
119
120 @node Bus names
121 @section Bus names.
122
123 There are several basic functions which inspect the buses for
124 registered names. Internally they use the basic interface
125 @samp{org.freedesktop.DBus}, which is supported by all objects of a bus.
126
127 @defun dbus-list-activatable-names
128 This function returns the D-Bus service names, which can be activated.
129 An activatable service is described in a service registration file.
130 Under GNU/Linux, such files are located at
131 @file{/usr/share/dbus-1/services/}.
132
133 The result is a list of strings, which is @code{nil} when there are no
134 activatable service names at all.
135 @end defun
136
137 @defun dbus-list-names bus
138 All service names, which are registered at D-Bus @var{bus}, are
139 returned. The result is a list of strings, which is @code{nil} when
140 there are no registered service names at all. Well known names are
141 strings like @samp{org.freedesktop.DBus}. Names starting with
142 @samp{:} are unique names for services.
143
144 @var{bus} must be either the symbol @code{:system} or the symbol
145 @code{:session}.
146 @end defun
147
148 @defun dbus-list-known-names bus
149 Retrieves all services which correspond to a known name in @var{bus}.
150 A service has a known name if it doesn't start with @samp{:}. The
151 result is a list of strings, which is @code{nil} when there are no
152 known names at all.
153
154 @var{bus} must be either the symbol @code{:system} or the symbol
155 @code{:session}.
156 @end defun
157
158 @defun dbus-list-queued-owners bus service
159 For a given service, registered at D-Bus @var{bus} under the name
160 @var{service}, all queued unique names are returned. The result is a
161 list of strings, or @code{nil} when there are no queued names for
162 @var{service} at all.
163
164 @var{bus} must be either the symbol @code{:system} or the symbol
165 @code{:session}. @var{service} must be a known service name as
166 string.
167 @end defun
168
169 @defun dbus-get-name-owner bus service
170 For a given service, registered at D-Bus @var{bus} under the name
171 @var{service}, the unique name of the name owner is returned. The
172 result is a string, or @code{nil} when there exist no name owner of
173 @var{service}.
174
175 @var{bus} must be either the symbol @code{:system} or the symbol
176 @code{:session}. @var{service} must be a known service name as
177 string.
178 @end defun
179
180 @defun dbus-ping bus service
181 Check whether the service name @var{service} is registered at D-Bus
182 @var{bus}. @var{service} might not have been started yet. The result
183 is either @code{t} or @code{nil}.
184
185 @var{bus} must be either the symbol @code{:system} or the symbol
186 @code{:session}. @var{service} must be a string. Example:
187
188 @lisp
189 (message
190 "%s screensaver on board."
191 (cond
192 ((dbus-ping :session "org.gnome.ScreenSaver") "Gnome")
193 ((dbus-ping :session "org.freedesktop.ScreenSaver") "KDE")
194 (t "No")))
195 @end lisp
196 @end defun
197
198 @defun dbus-get-unique-name bus
199 The unique name, under which Emacs is registered at D-Bus @var{bus},
200 is returned as string.
201
202 @var{bus} must be either the symbol @code{:system} or the symbol
203 @code{:session}.
204 @end defun
205
206
207 @node Introspection
208 @section Knowing the details of D-Bus services.
209
210 D-Bus services publish their interfaces. This can be retrieved and
211 analyzed during runtime, in order to understand the used
212 implementation.
213
214 The resulting introspection data are in XML format. The root
215 introspection element is always a @code{node} element. It might have
216 a @code{name} attribute, which denotes the (absolute) object path an
217 interface is introspected.
218
219 The root @code{node} element may have @code{node} and @code{interface}
220 children. A child @code{node} element must have a @code{name}
221 attribute, this case it is the relative object path to the root
222 @code{node} element.
223
224 An @code{interface} element has just one attribute, @code{name}, which
225 is the full name of that interface. The default interface
226 @samp{org.freedesktop.DBus.Introspectable} is always present. Example:
227
228 @example
229 <node name="/org/bluez">
230 <interface name="org.freedesktop.DBus.Introspectable">
231 @dots{}
232 </interface>
233 <interface name="org.bluez.Manager">
234 @dots{}
235 </interface>
236 <interface name="org.bluez.Database">
237 @dots{}
238 </interface>
239 <interface name="org.bluez.Security">
240 @dots{}
241 </interface>
242 <node name="service_audio"/>
243 <node name="service_input"/>
244 <node name="service_network"/>
245 <node name="service_serial"/>
246 </node>
247 @end example
248
249 Children of an @code{interface} element can be @code{method},
250 @code{signal} and @code{property} elements. A @code{method} element
251 stands for a D-Bus method of the surrounding interface. The element
252 itself has a @code{name} attribute, showing the method name. Children
253 elements @code{arg} stand for the arguments of a method. Example:
254
255 @example
256 <method name="ResolveHostName">
257 <arg name="interface" type="i" direction="in"/>
258 <arg name="protocol" type="i" direction="in"/>
259 <arg name="name" type="s" direction="in"/>
260 <arg name="aprotocol" type="i" direction="in"/>
261 <arg name="flags" type="u" direction="in"/>
262 <arg name="interface" type="i" direction="out"/>
263 <arg name="protocol" type="i" direction="out"/>
264 <arg name="name" type="s" direction="out"/>
265 <arg name="aprotocol" type="i" direction="out"/>
266 <arg name="address" type="s" direction="out"/>
267 <arg name="flags" type="u" direction="out"/>
268 </method>
269 @end example
270
271 @code{arg} elements can have the attributes @code{name}, @code{type}
272 and @code{direction}. The @code{name} attribute is optional. The
273 @code{type} attribute stands for the @dfn{signature} of the argument
274 in D-Bus. For a discussion of D-Bus types and their Lisp
275 representation see @ref{Type Conversion}.@footnote{D-Bus signatures
276 are explained in the D-Bus specification
277 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures}.}
278 The @code{direction} attribute of an @code{arg} element can be only
279 @samp{in} or @samp{out}; in case it is omitted, it defaults to
280 @samp{in}.
281
282 A @code{signal} element of an @code{interface} has a similar
283 structure. The @code{direction} attribute of an @code{arg} child
284 element can be only @samp{out} here; which is also the default value.
285 Example:
286
287 @example
288 <signal name="StateChanged">
289 <arg name="state" type="i"/>
290 <arg name="error" type="s"/>
291 </signal>
292 @end example
293
294 A @code{property} element has no @code{arg} child
295 element. It just has the attributes @code{name}, @code{type} and
296 @code{access}, which are all mandatory. The @code{access} attribute
297 allows the values @samp{readwrite}, @samp{read}, and @samp{write}.
298 Example:
299
300 @example
301 <property name="Status" type="u" direction="read"/>
302 @end example
303
304 @code{annotation} elements can be children of @code{interface},
305 @code{method}, @code{signal}, and @code{property} elements. Unlike
306 properties, which can change their values during lifetime of a D-Bus
307 object, annotations are static. Often they are used for code
308 generators of D-Bus langugae bindings. Example:
309
310 @example
311 <annotation name="de.berlios.Pinot.GetStatistics" value="pinotDBus"/>
312 @end example
313
314 Annotations have just @code{name} and @code{value} attributes, both
315 must be strings.
316
317 @defun dbus-introspect bus service path
318 This function returns all interfaces and sub-nodes of @var{service},
319 registered at object path @var{path} at bus @var{bus}.
320
321 @var{bus} must be either the symbol @code{:system} or the symbol
322 @code{:session}. @var{service} must be a known service name, and
323 @var{path} must be a valid object path. The last two parameters are
324 strings. The result, the introspection data, is a string in XML
325 format. Example:
326
327 @lisp
328 (dbus-introspect
329 :system "org.freedesktop.Hal"
330 "/org/freedesktop/Hal/devices/computer")
331
332 @result{} "<!DOCTYPE node PUBLIC
333 "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
334 "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
335 <node>
336 <interface name="org.freedesktop.Hal.Device">
337 <method name="GetAllProperties">
338 <arg name="properties" direction="out" type="a@{sv@}"/>
339 </method>
340 @dots{}
341 <signal name="PropertyModified">
342 <arg name="num_updates" type="i"/>
343 <arg name="updates" type="a(sbb)"/>
344 </signal>
345 </interface>
346 @dots{}
347 </node>"
348 @end lisp
349
350 This example informs us, that the service @samp{org.freedesktop.Hal}
351 at object path @samp{/org/freedesktop/Hal/devices/computer} offers the
352 interface @samp{org.freedesktop.Hal.Device} (and 2 other interfaces
353 not documented here). This interface contains the method
354 @samp{GetAllProperties}, which needs no input parameters, but returns
355 as output parameter an array of dictionary entries (key-value pairs).
356 Every dictionary entry has a string as key, and a variant as value.
357
358 The interface offers also a signal, which returns 2 parameters: an
359 integer, and an array consisting of elements which are a struct of a
360 string and 2 boolean values.@footnote{ The interfaces of the service
361 @samp{org.freedesktop.Hal} are described at
362 @uref{http://people.freedesktop.org/~david/hal-spec/hal-spec.html#interfaces}.}
363 @end defun
364
365 @defun dbus-introspect-xml bus service path
366 This function has the same intention as function
367 @code{dbus-introspect}. The returned value is a parsed XML tree,
368 which can be used for further analysis. Example:
369
370 @lisp
371 (dbus-introspect-xml
372 :session "org.freedesktop.xesam.searcher"
373 "/org/freedesktop/xesam/searcher/main")
374
375 @result{} (node ((name . "/org/freedesktop/xesam/searcher/main"))
376 (interface ((name . "org.freedesktop.xesam.Search"))
377 (method ((name . "GetHitData"))
378 (arg ((name . "search") (type . "s") (direction . "in")))
379 (arg ((name . "hit_ids") (type . "au") (direction . "in")))
380 (arg ((name . "fields") (type . "as") (direction . "in")))
381 (arg ((name . "hit_data") (type . "aav") (direction . "out")))
382 )
383 @dots{}
384 (signal ((name . "HitsAdded"))
385 (arg ((name . "search") (type . "s")))
386 (arg ((name . "count") (type . "u")))
387 )
388 )
389 @dots{}
390 )
391 @end lisp
392 @end defun
393
394 @defun dbus-introspect-get-attribute object attribute
395 It returns the @var{attribute} value of a D-Bus introspection
396 @var{object}. @var{object} can be every subtree of a parsed XML tree
397 as retrieved with @code{dbus-introspect-xml}. @var{attribute} must be
398 a string according to the attribute names in the D-Bus specification.
399 Example:
400
401 @lisp
402 (dbus-introspect-get-attribute
403 (dbus-introspect-xml :system "org.freedesktop.SystemToolsBackends"
404 "/org/freedesktop/SystemToolsBackends/UsersConfig")
405 "name")
406
407 @result{} "/org/freedesktop/SystemToolsBackends/UsersConfig"
408 @end lisp
409
410 If @var{object} has no @var{attribute}, the function returns nil.
411 @end defun
412
413
414 @node Nodes and Interfaces
415 @section Detecting object paths and interfaces.
416
417 The first elements, to be introspected for a D-Bus object, are further
418 object paths and interfaces.
419
420 @defun dbus-introspect-get-node-names bus service path
421 All node names of @var{service} in D-Bus @var{bus} at object path
422 @var{path} are returned as list of strings. Example:
423
424 @lisp
425 (dbus-introspect-get-node-names
426 :session "org.gnome.seahorse" "/org/gnome/seahorse")
427
428 @result{} ("crypto" "keys")
429 @end lisp
430
431 The node names stand for further object paths of the D-Bus
432 @var{service}, relative to @var{path}. In the example,
433 @samp{/org/gnome/seahorse/crypto} and @samp{/org/gnome/seahorse/keys}
434 are also object paths of the D-Bus service @samp{org.gnome.seahorse}.
435 @end defun
436
437 @defun dbus-introspect-get-all-nodes bus service path
438 This function returns all node names of @var{service} in D-Bus
439 @var{bus} at object path @var{path}. It returns a list of strings
440 with all object paths of @var{service}, starting at @var{path}.
441 Example:
442
443 @lisp
444 (dbus-introspect-get-all-nodes :session "org.gnome.seahorse" "/")
445
446 @result{} ("/" "/org" "/org/gnome" "/org/gnome/seahorse"
447 "/org/gnome/seahorse/crypto"
448 "/org/gnome/seahorse/keys"
449 "/org/gnome/seahorse/keys/openpgp"
450 "/org/gnome/seahorse/keys/openpgp/local"
451 "/org/gnome/seahorse/keys/openssh"
452 "/org/gnome/seahorse/keys/openssh/local")
453 @end lisp
454 @end defun
455
456 @defun dbus-introspect-get-interface-names bus service path
457 There will be returned a list strings of all interface names of
458 @var{service} in D-Bus @var{bus} at object path @var{path}. This list
459 will contain the default interface @samp{org.freedesktop.DBus.Introspectable}.
460
461 Another default interface is @samp{org.freedesktop.DBus.Properties}.
462 If present, @code{interface} elements can also have @code{property}
463 children. Example:
464
465 @lisp
466 (dbus-introspect-get-interface-names
467 :system "org.freedesktop.Hal"
468 "/org/freedesktop/Hal/devices/computer")
469
470 @result{} ("org.freedesktop.DBus.Introspectable"
471 "org.freedesktop.Hal.Device"
472 "org.freedesktop.Hal.Device.SystemPowerManagement"
473 "org.freedesktop.Hal.Device.CPUFreq")
474 @end lisp
475 @end defun
476
477 @defun dbus-introspect-get-interface bus service path interface
478 Return @var{interface} of @var{service} in D-Bus @var{bus} at object
479 path @var{path}. The return value is an XML element. @var{interface}
480 must be a string, element of the list returned by
481 @code{dbus-introspect-get-interface-names}. Example:
482
483 @lisp
484 (dbus-introspect-get-interface
485 :session "org.freedesktop.xesam.searcher"
486 "/org/freedesktop/xesam/searcher/main"
487 "org.freedesktop.xesam.Search")
488
489 @result{} (interface ((name . "org.freedesktop.xesam.Search"))
490 (method ((name . "GetHitData"))
491 (arg ((name . "search") (type . "s") (direction . "in")))
492 (arg ((name . "hit_ids") (type . "au") (direction . "in")))
493 (arg ((name . "fields") (type . "as") (direction . "in")))
494 (arg ((name . "hit_data") (type . "aav") (direction . "out")))
495 )
496 @dots{}
497 (signal ((name . "HitsAdded"))
498 (arg ((name . "search") (type . "s")))
499 (arg ((name . "count") (type . "u")))
500 )
501 )
502 @end lisp
503 @end defun
504
505 @noindent
506 With these functions, it is possible to retrieve all introspection
507 data from a running system:
508
509 @lisp
510 (with-current-buffer (switch-to-buffer "*introspect*")
511 (erase-buffer)
512 (dolist (service (dbus-list-known-names :session))
513 (dolist (path (dbus-introspect-get-all-nodes :session service "/"))
514 ;; We want to introspect only elements, which have more than
515 ;; the default interface "org.freedesktop.DBus.Introspectable".
516 (when (delete
517 "org.freedesktop.DBus.Introspectable"
518 (dbus-introspect-get-interface-names :session service path))
519 (insert (message "\nservice: \"%s\" path: \"%s\"\n" service path)
520 (dbus-introspect :session service path))
521 (redisplay t)))))
522 @end lisp
523
524
525 @node Methods and Signal
526 @section Applying the functionality.
527
528 Methods and signals are the communicatione means to D-Bus. The
529 following functions return their specifications.
530
531 @defun dbus-introspect-get-method-names bus service path interface
532 Return a list of strings of all method names of @var{interface} of
533 @var{service} in D-Bus @var{bus} at object path @var{path}. Example:
534
535 @lisp
536 (dbus-introspect-get-method-names
537 :session "org.freedesktop.xesam.searcher"
538 "/org/freedesktop/xesam/searcher/main"
539 "org.freedesktop.xesam.Search")
540
541 @result{} ("GetState" "StartSearch" "GetHitCount" "GetHits" "NewSession"
542 "CloseSession" "GetHitData" "SetProperty" "NewSearch"
543 "GetProperty" "CloseSearch")
544 @end lisp
545 @end defun
546
547 @defun dbus-introspect-get-method bus service path interface method
548 This function returns @var{method} of @var{interface} as XML element.
549 It must be located at @var{service} in D-Bus @var{bus} at object path
550 @var{path}. @var{method} must be a string, element of the list
551 returned by @code{dbus-introspect-get-method-names}. Example:
552
553 @lisp
554 (dbus-introspect-get-method
555 :session "org.freedesktop.xesam.searcher"
556 "/org/freedesktop/xesam/searcher/main"
557 "org.freedesktop.xesam.Search" "GetHitData")
558
559 @result{} (method ((name . "GetHitData"))
560 (arg ((name . "search") (type . "s") (direction . "in")))
561 (arg ((name . "hit_ids") (type . "au") (direction . "in")))
562 (arg ((name . "fields") (type . "as") (direction . "in")))
563 (arg ((name . "hit_data") (type . "aav") (direction . "out")))
564 )
565 @end lisp
566 @end defun
567
568 @defun dbus-introspect-get-signal-names bus service path interface
569 Return a list of strings of all signal names of @var{interface} of
570 @var{service} in D-Bus @var{bus} at object path @var{path}. Example:
571
572 @lisp
573 (dbus-introspect-get-signal-names
574 :session "org.freedesktop.xesam.searcher"
575 "/org/freedesktop/xesam/searcher/main"
576 "org.freedesktop.xesam.Search")
577
578 @result{} ("StateChanged" "SearchDone" "HitsModified"
579 "HitsRemoved" "HitsAdded")
580 @end lisp
581 @end defun
582
583 @defun dbus-introspect-get-signal bus service path interface signal
584 This function returns @var{signal} of @var{interface} as XML element.
585 It must be located at @var{service} in D-Bus @var{bus} at object path
586 @var{path}. @var{signal} must be a string, element of the list
587 returned by @code{dbus-introspect-get-signal-names}. Example:
588
589 @lisp
590 (dbus-introspect-get-signal
591 :session "org.freedesktop.xesam.searcher"
592 "/org/freedesktop/xesam/searcher/main"
593 "org.freedesktop.xesam.Search" "HitsAdded")
594
595 @result{} (signal ((name . "HitsAdded"))
596 (arg ((name . "search") (type . "s")))
597 (arg ((name . "count") (type . "u")))
598 )
599 @end lisp
600 @end defun
601
602
603 @node Properties and Annotations
604 @section What else to know about interfaces.
605
606 Interfaces can have properties. These can be exposed via the
607 @samp{org.freedesktop.DBus.Properties} interface@footnote{See
608 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties}}.
609 That is, properties can be retrieved and changed during lifetime of an
610 element.
611
612 Annotations, on the other hand, are static values for an element.
613 Often, they are used to instruct generators, how to generate code from
614 the interface for a given language binding.
615
616 @defun dbus-introspect-get-property-names bus service path interface
617 Return a list of strings with all property names of @var{interface} of
618 @var{service} in D-Bus @var{bus} at object path @var{path}. Example:
619
620 @lisp
621 (dbus-introspect-get-property-names
622 :session "org.kde.kded" "/modules/networkstatus"
623 "org.kde.Solid.Networking.Client")
624
625 @result{} ("Status")
626 @end lisp
627
628 If an interface declares properties, the corresponding element supports
629 also the @samp{org.freedesktop.DBus.Properties} interface.
630 @end defun
631
632 @defun dbus-introspect-get-property bus service path interface property
633 This function returns @var{property} 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{property} must be a string, element of the list
636 returned by @code{dbus-introspect-get-property-names}.
637
638 A @var{property} value can be retrieved by the function
639 @code{dbus-introspect-get-attribute}. Example:
640
641 @lisp
642 (dbus-introspect-get-property
643 :session "org.kde.kded" "/modules/networkstatus"
644 "org.kde.Solid.Networking.Client" "Status")
645
646 @result{} (property ((access . "read") (type . "u") (name . "Status")))
647
648 (dbus-introspect-get-attribute
649 (dbus-introspect-get-property
650 :session "org.kde.kded" "/modules/networkstatus"
651 "org.kde.Solid.Networking.Client" "Status")
652 "access")
653
654 @result{} "read"
655 @end lisp
656 @end defun
657
658 @defun dbus-get-property bus service path interface property
659 This function returns the value of @var{property} of @var{interface}.
660 It will be checked at @var{bus}, @var{service}, @var{path}. The
661 result can be any valid D-Bus value, or nil if there is no
662 @var{property}. Example:
663
664 @lisp
665 (dbus-get-property
666 :session "org.kde.kded" "/modules/networkstatus"
667 "org.kde.Solid.Networking.Client" "Status")
668
669 @result{} 4
670 @end lisp
671 @end defun
672
673 @defun dbus-set-property bus service path interface property value
674 Set value of @var{property} of @var{interface} to @var{value}. It
675 will be checked at @var{bus}, @var{service}, @var{path}. When the
676 value has been set successful, the result is @var{value}. Otherwise,
677 @code{nil} is returned. Example:
678
679 @lisp
680 (dbus-set-property
681 :session "org.kde.kaccess" "/MainApplication"
682 "com.trolltech.Qt.QApplication" "doubleClickInterval" 500)
683
684 @result{} 500
685 @end lisp
686 @end defun
687
688 @defun dbus-get-all-properties bus service path interface
689 This function returns all properties of @var{interface}. It will be
690 checked at @var{bus}, @var{service}, @var{path}. The result is a list
691 of cons. Every cons contains the name of the property, and its value.
692 If there are no properties, @code{nil} is returned. Example:
693
694 @lisp
695 (dbus-get-all-properties
696 :session "org.kde.kaccess" "/MainApplication"
697 "com.trolltech.Qt.QApplication")
698
699 @result{} (("cursorFlashTime" . 1000) ("doubleClickInterval" . 500)
700 ("keyboardInputInterval" . 400) ("wheelScrollLines" . 3)
701 ("globalStrut" 0 0) ("startDragTime" . 500)
702 ("startDragDistance" . 4) ("quitOnLastWindowClosed" . t)
703 ("styleSheet" . ""))
704 @end lisp
705 @end defun
706
707 @defun dbus-introspect-get-annotation-names bus service path interface &optional name
708 Return a list of all annotation names as list of strings. If
709 @var{name} is @code{nil}, the annotations are children of
710 @var{interface}, otherwise @var{name} must be a @code{method},
711 @code{signal}, or @code{property} XML element, where the annotations
712 belong to. Example:
713
714 @lisp
715 (dbus-introspect-get-annotation-names
716 :session "de.berlios.Pinot" "/de/berlios/Pinot"
717 "de.berlios.Pinot" "GetStatistics")
718
719 @result{} ("de.berlios.Pinot.GetStatistics")
720 @end lisp
721
722 Default annotation names@footnote{See
723 @uref{http://dbus.freedesktop.org/doc/dbus-specification.html#introspection-format}}
724 are
725
726 @table @samp
727 @item org.freedesktop.DBus.Deprecated
728 Whether or not the entity is deprecated; defaults to @code{nil}
729
730 @item org.freedesktop.DBus.GLib.CSymbol
731 The C symbol; may be used for @code{methods} and @code{interfaces}
732
733 @item org.freedesktop.DBus.Method.NoReply
734 If set, don't expect a reply to the @code{method} call; defaults to @code{nil}
735 @end table
736 @end defun
737
738 @defun dbus-introspect-get-annotation bus service path interface name annotation
739 Return annotation @var{ANNOTATION} as XML object. If @var{name} is
740 @code{nil}, @var{ANNOTATION} is a child of @var{interface}, otherwise
741 @var{name} must be the name of a @code{method}, @code{signal}, or
742 @code{property} XML element, where the @var{ANNOTATION} belongs to.
743
744 An attribute value can be retrieved by
745 @code{dbus-introspect-get-attribute}. Example:
746
747 @lisp
748 (dbus-introspect-get-annotation
749 :session "de.berlios.Pinot" "/de/berlios/Pinot"
750 "de.berlios.Pinot" "GetStatistics"
751 "de.berlios.Pinot.GetStatistics")
752
753 @result{} (annotation ((name . "de.berlios.Pinot.GetStatistics")
754 (value . "pinotDBus")))
755
756 (dbus-introspect-get-attribute
757 (dbus-introspect-get-annotation
758 :session "de.berlios.Pinot" "/de/berlios/Pinot"
759 "de.berlios.Pinot" "GetStatistics"
760 "de.berlios.Pinot.GetStatistics")
761 "value")
762
763 @result{} "pinotDBus"
764 @end lisp
765 @end defun
766
767
768 @node Arguments and Signatures
769 @section The final details.
770
771 Methods and signals have arguments. They are described in the
772 @code{arg} XML elements.
773
774 @defun dbus-introspect-get-argument-names bus service path interface name
775 Return a list of all argument names as list of strings. @var{name}
776 must be a @code{method} or @code{signal} XML element. Example:
777
778 @lisp
779 (dbus-introspect-get-argument-names
780 :session "org.freedesktop.xesam.searcher"
781 "/org/freedesktop/xesam/searcher/main"
782 "org.freedesktop.xesam.Search" "GetHitData")
783
784 @result{} ("search" "hit_ids" "fields" "hit_data")
785 @end lisp
786
787 Argument names are optional; the function can return @code{nil}
788 therefore, even if the method or signal has arguments.
789 @end defun
790
791 @defun dbus-introspect-get-argument bus service path interface name arg
792 Return argument @var{ARG} as XML object. @var{name}
793 must be a @code{method} or @code{signal} XML element. Example:
794
795 @lisp
796 (dbus-introspect-get-argument
797 :session "org.freedesktop.xesam.searcher"
798 "/org/freedesktop/xesam/searcher/main"
799 "org.freedesktop.xesam.Search" "GetHitData" "search")
800
801 @result{} (arg ((name . "search") (type . "s") (direction . "in")))
802 @end lisp
803 @end defun
804
805 @defun dbus-introspect-get-signature bus service path interface name &optional direction
806 Return signature of a @code{method} or @code{signal}, represented by
807 @var{name}, as string.
808
809 If @var{name} is a @code{method}, @var{direction} can be either
810 @samp{in} or @samp{out}. If @var{direction} is @code{nil}, @samp{in}
811 is assumed.
812
813 If @var{name} is a @code{signal}, and @var{direction} is
814 non-@code{nil}, @var{direction} must be @samp{out}. Example:
815
816 @lisp
817 (dbus-introspect-get-signature
818 :session "org.freedesktop.xesam.searcher"
819 "/org/freedesktop/xesam/searcher/main"
820 "org.freedesktop.xesam.Search" "GetHitData" "in")
821
822 @result{} "sauas"
823
824 (dbus-introspect-get-signature
825 :session "org.freedesktop.xesam.searcher"
826 "/org/freedesktop/xesam/searcher/main"
827 "org.freedesktop.xesam.Search" "HitsAdded")
828
829 @result{} "su"
830 @end lisp
831 @end defun
832
833
834 @node Type Conversion
835 @chapter Mapping Lisp types and D-Bus types.
836 @cindex type conversion
837
838 D-Bus method calls and signals accept usually several arguments as
839 parameters, either as input parameter, or as output parameter. Every
840 argument belongs to a D-Bus type.
841
842 Such arguments must be mapped between the value encoded as a D-Bus
843 type, and the corresponding type of Lisp objects. The mapping is
844 applied Lisp object @expansion{} D-Bus type for input parameters, and
845 D-Bus type @expansion{} Lisp object for output parameters.
846
847
848 @section Input parameters.
849
850 Input parameters for D-Bus methods and signals occur as arguments of a
851 Lisp function call. The following mapping to D-Bus types is
852 applied, when the corresponding D-Bus message is created:
853
854 @example
855 @multitable {@code{t} and @code{nil}} {@expansion{}} {DBUS_TYPE_BOOLEAN}
856 @item Lisp type @tab @tab D-Bus type
857 @item
858 @item @code{t} and @code{nil} @tab @expansion{} @tab DBUS_TYPE_BOOLEAN
859 @item number @tab @expansion{} @tab DBUS_TYPE_UINT32
860 @item integer @tab @expansion{} @tab DBUS_TYPE_INT32
861 @item float @tab @expansion{} @tab DBUS_TYPE_DOUBLE
862 @item string @tab @expansion{} @tab DBUS_TYPE_STRING
863 @item list @tab @expansion{} @tab DBUS_TYPE_ARRAY
864 @end multitable
865 @end example
866
867 Other Lisp objects, like symbols or hash tables, are not accepted as
868 input parameter.
869
870 If it is necessary to use another D-Bus type, a corresponding type
871 symbol can be preceeded to the corresponding Lisp object. Basic D-Bus
872 types are represented by the type symbols @code{:byte},
873 @code{:boolean}, @code{:int16}, @code{:uint16}, @code{:int32},
874 @code{:uint32}, @code{:int64}, @code{:uint64}, @code{:double},
875 @code{:string}, @code{:object-path} and @code{:signature}.
876
877 @noindent
878 Example:
879
880 @lisp
881 (dbus-call-method @dots{} @var{NUMBER} @var{STRING})
882 @end lisp
883
884 is equivalent to
885
886 @lisp
887 (dbus-call-method @dots{} :uint32 @var{NUMBER} :string @var{STRING})
888 @end lisp
889
890 but different to
891
892 @lisp
893 (dbus-call-method @dots{} :int32 @var{NUMBER} :signature @var{STRING})
894 @end lisp
895
896 The value for a byte D-Bus type can be any integer in the range 0
897 through 255. If a character is used as argument, modifiers
898 represented outside this range are stripped of. For example,
899 @code{:byte ?x} is equal to @code{:byte ?\M-x}, but it is not equal to
900 @code{:byte ?\C-x} or @code{:byte ?\M-\C-x}.
901
902 A D-Bus compound type is always represented as a list. The @sc{car}
903 of this list can be the type symbol @code{:array}, @code{:variant},
904 @code{:struct} or @code{:dict-entry}, which would result in a
905 corresponding D-Bus container. @code{:array} is optional, because
906 this is the default compound D-Bus type for a list.
907
908 The objects being elements of the list are checked according to the
909 D-Bus compound type rules.
910
911 @itemize
912 @item An array must contain only elements of the same D-Bus type. It
913 can be empty.
914
915 @item A variant must contain only one single element.
916
917 @item A dictionary entry must be element of an array, and it must
918 contain only a key-value pair of two elements, with a basic D-Bus type
919 key.
920
921 @item There is no restriction for structs.
922 @end itemize
923
924 If an empty array needs an element D-Bus type other than string, it
925 can contain exactly one element of D-Bus type @code{:signature}. The
926 value of this element (a string) is used as the signature of the
927 elements of this array. Example:
928
929 @lisp
930 (dbus-call-method
931 :session "org.freedesktop.Notifications"
932 "/org/freedesktop/Notifications"
933 "org.freedesktop.Notifications" "Notify"
934 "GNU Emacs" ;; Application name.
935 0 ;; No replacement of other notifications.
936 "" ;; No icon.
937 "Notification summary" ;; Summary.
938 (format ;; Body.
939 "This is a test notification, raised from %s" (emacs-version))
940 '(:array) ;; No actions (empty array of strings).
941 '(:array :signature "@{sv@}") ;; No hints
942 ;; (empty array of dictionary entries).
943 ':int32 -1) ;; Default timeout.
944
945 @result{} 3
946 @end lisp
947
948 @defun dbus-string-to-byte-array string
949 Sometimes, D-Bus methods require as input parameter an array of bytes,
950 instead of a string. If it is guaranteed, that @var{string} is an
951 UTF8 string, this function performs the conversion. Example:
952
953 @lisp
954 (dbus-string-to-byte-array "/etc/hosts")
955
956 @result{} (:array :byte 47 :byte 101 :byte 116 :byte 99 :byte 47
957 :byte 104 :byte 111 :byte 115 :byte 116 :byte 115)
958 @end lisp
959 @end defun
960
961 @defun dbus-escape-as-identifier string
962 Escape an arbitrary @var{string} so it follows the rules for a C
963 identifier. The escaped string can be used as object path component,
964 interface element component, bus name component or member name in
965 D-Bus.
966
967 The escaping consists of replacing all non-alphanumerics, and the
968 first character if it's a digit, with an underscore and two
969 lower-case hex digits. As a special case, "" is escaped to
970 "_". Example:
971
972 @lisp
973 (dbus-escape-as-identifier "0123abc_xyz\x01\xff")
974
975 @result{} "_30123abc_5fxyz_01_ff"
976 @end lisp
977 @end defun
978
979
980 @section Output parameters.
981
982 Output parameters of D-Bus methods and signals are mapped to Lisp
983 objects.
984
985 @example
986 @multitable {DBUS_TYPE_OBJECT_PATH} {@expansion{}} {@code{t} or @code{nil}}
987 @item D-Bus type @tab @tab Lisp type
988 @item
989 @item DBUS_TYPE_BOOLEAN @tab @expansion{} @tab @code{t} or @code{nil}
990 @item DBUS_TYPE_BYTE @tab @expansion{} @tab number
991 @item DBUS_TYPE_UINT16 @tab @expansion{} @tab number
992 @item DBUS_TYPE_INT16 @tab @expansion{} @tab number
993 @item DBUS_TYPE_UINT32 @tab @expansion{} @tab number or float
994 @item DBUS_TYPE_INT32 @tab @expansion{} @tab number or float
995 @item DBUS_TYPE_UINT64 @tab @expansion{} @tab number or float
996 @item DBUS_TYPE_INT64 @tab @expansion{} @tab number or float
997 @item DBUS_TYPE_DOUBLE @tab @expansion{} @tab float
998 @item DBUS_TYPE_STRING @tab @expansion{} @tab string
999 @item DBUS_TYPE_OBJECT_PATH @tab @expansion{} @tab string
1000 @item DBUS_TYPE_SIGNATURE @tab @expansion{} @tab string
1001 @item DBUS_TYPE_ARRAY @tab @expansion{} @tab list
1002 @item DBUS_TYPE_VARIANT @tab @expansion{} @tab list
1003 @item DBUS_TYPE_STRUCT @tab @expansion{} @tab list
1004 @item DBUS_TYPE_DICT_ENTRY @tab @expansion{} @tab list
1005 @end multitable
1006 @end example
1007
1008 A float object in case of @code{DBUS_TYPE_UINT32},
1009 @code{DBUS_TYPE_INT32}, @code{DBUS_TYPE_UINT64} and
1010 @code{DBUS_TYPE_INT6432} is returned, when the C value exceeds the
1011 Emacs number size range.
1012
1013 The resulting list of the last 4 D-Bus compound types contains as
1014 elements the elements of the D-Bus container, mapped according to the
1015 same rules.
1016
1017 The signal @code{PropertyModified}, discussed as example in
1018 @ref{Inspection}, would offer as Lisp data the following object
1019 (@var{BOOL} stands here for either @code{nil} or @code{t}):
1020
1021 @lisp
1022 (@var{NUMBER} ((@var{STRING} @var{BOOL} @var{BOOL}) (@var{STRING} @var{BOOL} @var{BOOL}) @dots{}))
1023 @end lisp
1024
1025 @defun dbus-byte-array-to-string byte-array
1026 If a D-Bus method or signal returns an array of bytes, which are known
1027 to represent an UTF8 string, this function converts @var{byte-array}
1028 to the corresponding string. Example:
1029
1030 @lisp
1031 (dbus-byte-array-to-string '(47 101 116 99 47 104 111 115 116 115))
1032
1033 @result{} "/etc/hosts"
1034 @end lisp
1035 @end defun
1036
1037 @defun dbus-unescape-from-identifier string
1038 Retrieve the original string from the encoded @var{string}.
1039 @var{string} must have been coded with
1040 @code{dbus-escape-as-identifier}. Example:
1041
1042 @lisp
1043 (dbus-unescape-from-identifier "_30123abc_5fxyz_01_ff")
1044
1045 @result{} "0123abc_xyz\ 1ÿ"
1046 @end lisp
1047 @end defun
1048
1049
1050 @node Synchronous Methods
1051 @chapter Calling methods in a blocking way.
1052 @cindex method calls, synchronous
1053 @cindex synchronous method calls
1054
1055 Methods can be called synchronously (@dfn{blocking}) or asynchronously
1056 (@dfn{non-blocking}).
1057
1058 At D-Bus level, a method call consist of two messages: one message
1059 which carries the input parameters to the object owning the method to
1060 be called, and a reply message returning the resulting output
1061 parameters from the object.
1062
1063 @defun dbus-call-method bus service path interface method &optional :timeout timeout &rest args
1064 This function calls @var{method} on the D-Bus @var{bus}. @var{bus} is
1065 either the symbol @code{:system} or the symbol @code{:session}.
1066
1067 @var{service} is the D-Bus service name to be used. @var{path} is the
1068 D-Bus object path, @var{service} is registered at. @var{interface} is
1069 an interface offered by @var{service}. It must provide @var{method}.
1070
1071 If the parameter @code{:timeout} is given, the following integer
1072 @var{timeout} specifies the maximum number of milliseconds the method
1073 call must return. The default value is 25.000. If the method call
1074 doesn't return in time, a D-Bus error is raised (@pxref{Errors and
1075 Events}).
1076
1077 All other arguments args are passed to @var{method} as arguments.
1078 They are converted into D-Bus types as described in @ref{Type
1079 Conversion}.
1080
1081 The function returns the resulting values of @var{method} as a list of
1082 Lisp objects, according to the type conversion rules described in
1083 @ref{Type Conversion}. Example:
1084
1085 @lisp
1086 (dbus-call-method
1087 :session "org.gnome.seahorse" "/org/gnome/seahorse/keys/openpgp"
1088 "org.gnome.seahorse.Keys" "GetKeyField"
1089 "openpgp:657984B8C7A966DD" "simple-name")
1090
1091 @result{} (t ("Philip R. Zimmermann"))
1092 @end lisp
1093
1094 If the result of the method call is just one value, the converted Lisp
1095 object is returned instead of a list containing this single Lisp
1096 object. Example:
1097
1098 @lisp
1099 (dbus-call-method
1100 :system "org.freedesktop.Hal"
1101 "/org/freedesktop/Hal/devices/computer"
1102 "org.freedesktop.Hal.Device" "GetPropertyString"
1103 "system.kernel.machine")
1104
1105 @result{} "i686"
1106 @end lisp
1107
1108 With the @code{dbus-introspect} function it is possible to explore the
1109 interfaces of @samp{org.freedesktop.Hal} service. It offers the
1110 interfaces @samp{org.freedesktop.Hal.Manager} for the object at the
1111 path @samp{/org/freedesktop/Hal/Manager} as well as the interface
1112 @samp{org.freedesktop.Hal.Device} for all objects prefixed with the
1113 path @samp{/org/freedesktop/Hal/devices}. With the methods
1114 @samp{GetAllDevices} and @samp{GetAllProperties}, it is simple to
1115 emulate the @code{lshal} command on GNU/Linux systems:
1116
1117 @lisp
1118 (dolist (device
1119 (dbus-call-method
1120 :system "org.freedesktop.Hal"
1121 "/org/freedesktop/Hal/Manager"
1122 "org.freedesktop.Hal.Manager" "GetAllDevices"))
1123 (message "\nudi = %s" device)
1124 (dolist (properties
1125 (dbus-call-method
1126 :system "org.freedesktop.Hal" device
1127 "org.freedesktop.Hal.Device" "GetAllProperties"))
1128 (message " %s = %S"
1129 (car properties) (or (caar (cdr properties)) ""))))
1130
1131 @print{} "udi = /org/freedesktop/Hal/devices/computer
1132 info.addons = (\"hald-addon-acpi\")
1133 info.bus = \"unknown\"
1134 info.product = \"Computer\"
1135 info.subsystem = \"unknown\"
1136 info.udi = \"/org/freedesktop/Hal/devices/computer\"
1137 linux.sysfs_path_device = \"(none)\"
1138 power_management.acpi.linux.version = \"20051216\"
1139 power_management.can_suspend_to_disk = t
1140 power_management.can_suspend_to_ram = \"\"
1141 power_management.type = \"acpi\"
1142 smbios.bios.release_date = \"11/07/2001\"
1143 system.chassis.manufacturer = \"COMPAL\"
1144 system.chassis.type = \"Notebook\"
1145 system.firmware.release_date = \"03/19/2005\"
1146 @dots{}"
1147 @end lisp
1148 @end defun
1149
1150 @defun dbus-call-method-non-blocking bus service path interface method &optional :timeout timeout &rest args
1151 Call @var{method} on the D-Bus @var{bus}, but don't block the event queue.
1152 This is necessary for communicating to registered D-Bus methods,
1153 which are running in the same Emacs process.
1154
1155 The arguments are the same as in @code{dbus-call-method}. Example:
1156
1157 @lisp
1158 (dbus-call-method-non-blocking
1159 :system "org.freedesktop.Hal"
1160 "/org/freedesktop/Hal/devices/computer"
1161 "org.freedesktop.Hal.Device" "GetPropertyString"
1162 "system.kernel.machine")
1163
1164 @result{} "i686"
1165 @end lisp
1166 @end defun
1167
1168
1169 @node Asynchronous Methods
1170 @chapter Calling methods non-blocking.
1171 @cindex method calls, asynchronous
1172 @cindex asynchronous method calls
1173
1174 @defun dbus-call-method-asynchronously bus service path interface method handler &optional :timeout timeout &rest args
1175 This function calls @var{method} on the D-Bus @var{bus}
1176 asynchronously. @var{bus} is either the symbol @code{:system} or the
1177 symbol @code{:session}.
1178
1179 @var{service} is the D-Bus service name to be used. @var{path} is the
1180 D-Bus object path, @var{service} is registered at. @var{interface} is
1181 an interface offered by @var{service}. It must provide @var{method}.
1182
1183 @var{handler} is a Lisp function, which is called when the
1184 corresponding return message has arrived.
1185
1186 If the parameter @code{:timeout} is given, the following integer
1187 @var{timeout} specifies the maximum number of milliseconds a reply
1188 message must arrive. The default value is 25.000. If there is no
1189 reply message in time, a D-Bus error is raised (@pxref{Errors and
1190 Events}).
1191
1192 All other arguments args are passed to @var{method} as arguments.
1193 They are converted into D-Bus types as described in @ref{Type
1194 Conversion}.
1195
1196 The function returns a key into the hash table
1197 @code{dbus-registered-functions-table}. The corresponding entry in
1198 the hash table is removed, when the return message has been arrived,
1199 and @var{handler} is called. Example:
1200
1201 @lisp
1202 (dbus-call-method-asynchronously
1203 :system "org.freedesktop.Hal"
1204 "/org/freedesktop/Hal/devices/computer"
1205 "org.freedesktop.Hal.Device" "GetPropertyString" 'message
1206 "system.kernel.machine")
1207
1208 @result{} (:system 2)
1209
1210 @print{} i686
1211 @end lisp
1212 @end defun
1213
1214
1215 @node Receiving Method Calls
1216 @chapter Offering own methods.
1217 @cindex method calls, returning
1218 @cindex returning method calls
1219
1220 Emacs can also offer own methods, which can be called by other
1221 applications. These methods could be an implementation of an
1222 interface of a well known service, like @samp{org.freedesktop.TextEditor}.
1223
1224 It could be also an implementation of an own interface. In this case,
1225 the service name must be @samp{org.gnu.Emacs}. The object path shall
1226 begin with @samp{/org/gnu/Emacs/@strong{Application}/}, and the
1227 interface name shall be @code{org.gnu.Emacs.@strong{Application}}.
1228 @samp{@strong{Application}} is the name of the application which
1229 provides the interface.
1230
1231 @deffn Constant dbus-service-emacs
1232 The well known service name of Emacs.
1233 @end deffn
1234
1235 @deffn Constant dbus-path-emacs
1236 The object path head "/org/gnu/Emacs" used by Emacs. All object
1237 paths, used by offered methods or signals, shall start with this
1238 string.
1239 @end deffn
1240
1241 @defun dbus-register-method bus service path interface method handler
1242 With this function, an application registers @var{method} on the D-Bus
1243 @var{bus}.
1244
1245 @var{bus} is either the symbol @code{:system} or the symbol
1246 @code{:session}.
1247
1248 @var{service} is the D-Bus service name of the D-Bus object
1249 @var{method} is registered for. It must be a known name.
1250
1251 @var{path} is the D-Bus object path @var{service} is
1252 registered.
1253
1254 @var{interface} is the interface offered by @var{service}. It must
1255 provide @var{method}.
1256
1257 @var{handler} is a Lisp function to be called when a @var{method} call
1258 is received. It must accept as arguments the input arguments of
1259 @var{method}. @var{handler} should return a list, whose elements are
1260 to be used as arguments for the reply message of @var{method}. This
1261 list can be composed like the input parameters in @ref{Type
1262 Conversion}.
1263
1264 If @var{handler} wants to return just one Lisp object and it is not a
1265 cons cell, @var{handler} can return this object directly, instead of
1266 returning a list containing the object.
1267
1268 The default D-Bus timeout when waiting for a message reply is 25
1269 seconds. This value could be even smaller, depending on the calling
1270 client. Therefore, @var{handler} shall not last longer than
1271 absolutely necessary.
1272
1273 @code{dbus-register-method} returns a Lisp object, which can be used
1274 as argument in @code{dbus-unregister-object} for removing the
1275 registration for @var{method}. Example:
1276
1277 @lisp
1278 (defun my-dbus-method-handler (filename)
1279 (let (result)
1280 (if (find-file filename)
1281 (setq result '(:boolean t))
1282 (setq result '(:boolean nil)))
1283 result))
1284
1285 @result{} my-dbus-method-handler
1286
1287 (dbus-register-method
1288 :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1289 "org.freedesktop.TextEditor" "OpenFile"
1290 'my-dbus-method-handler)
1291
1292 @result{} ((:session "org.freedesktop.TextEditor" "OpenFile")
1293 ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
1294 my-dbus-method-handler))
1295 @end lisp
1296
1297 If you invoke the method @samp{org.freedesktop.TextEditor.OpenFile}
1298 from another D-Bus application with a filename as parameter, the file
1299 is opened in Emacs, and the method returns either @var{true} or
1300 @var{false}, indicating the success if the method. As test tool one
1301 could use the command line tool @code{dbus-send} in a shell:
1302
1303 @example
1304 # dbus-send --session --print-reply \
1305 --dest="org.freedesktop.TextEditor" \
1306 "/org/freedesktop/TextEditor" \
1307 "org.freedesktop.TextEditor.OpenFile" string:"/etc/hosts"
1308
1309 @print{} method return sender=:1.22 -> dest=:1.23 reply_serial=2
1310 boolean true
1311 @end example
1312
1313 You can indicate an error by raising the Emacs signal
1314 @code{dbus-error}. The handler above could be changed like this:
1315
1316 @lisp
1317 (defun my-dbus-method-handler (&rest args)
1318 (unless (and (= (length args) 1) (stringp (car args)))
1319 (signal 'dbus-error (list (format "Wrong argument list: %S" args))))
1320 (condition-case err
1321 (find-file (car args))
1322 (error (signal 'dbus-error (cdr err))))
1323 t)
1324
1325 @result{} my-dbus-method-handler
1326 @end lisp
1327
1328 The test runs then
1329
1330 @example
1331 # dbus-send --session --print-reply \
1332 --dest="org.freedesktop.TextEditor" \
1333 "/org/freedesktop/TextEditor" \
1334 "org.freedesktop.TextEditor.OpenFile" \
1335 string:"/etc/hosts" string:"/etc/passwd"
1336
1337 @print{} Error org.freedesktop.DBus.Error.Failed:
1338 Wrong argument list: ("/etc/hosts" "/etc/passwd")
1339 @end example
1340 @end defun
1341
1342
1343 @node Signals
1344 @chapter Sending and receiving signals.
1345 @cindex signals
1346
1347 Signals are broadcast messages. They carry input parameters, which
1348 are received by all objects which have registered for such a signal.
1349
1350 @defun dbus-send-signal bus service path interface signal &rest args
1351 This function is similar to @code{dbus-call-method}. The difference
1352 is, that there are no returning output parameters.
1353
1354 The function emits @var{signal} on the D-Bus @var{bus}. @var{bus} is
1355 either the symbol @code{:system} or the symbol @code{:session}. It
1356 doesn't matter whether another object has registered for @var{signal}.
1357
1358 @var{service} is the D-Bus service name of the object the signal is
1359 emitted from. @var{path} is the corresponding D-Bus object path,
1360 @var{service} is registered at. @var{interface} is an interface
1361 offered by @var{service}. It must provide @var{signal}.
1362
1363 All other arguments args are passed to @var{signal} as arguments.
1364 They are converted into D-Bus types as described in @ref{Type
1365 Conversion}. Example:
1366
1367 @lisp
1368 (dbus-send-signal
1369 :session dbus-service-emacs dbus-path-emacs
1370 (concat dbus-service-emacs ".FileManager") "FileModified"
1371 "/home/albinus/.emacs")
1372 @end lisp
1373 @end defun
1374
1375 @defun dbus-register-signal bus service path interface signal handler &rest args
1376 With this function, an application registers for @var{signal} on the
1377 D-Bus @var{bus}.
1378
1379 @var{bus} is either the symbol @code{:system} or the symbol
1380 @code{:session}.
1381
1382 @var{service} is the D-Bus service name used by the sending D-Bus
1383 object. It can be either a known name or the unique name of the D-Bus
1384 object sending the signal. In case of a unique name, signals won't be
1385 received any longer once the object owning this unique name has
1386 disappeared, and a new queued object has replaced it.
1387
1388 When @var{service} is @code{nil}, related signals from all D-Bus
1389 objects shall be accepted.
1390
1391 @var{path} is the corresponding D-Bus object path, @var{service} is
1392 registered at. It can also be @code{nil} if the path name of incoming
1393 signals shall not be checked.
1394
1395 @var{interface} is an interface offered by @var{service}. It must
1396 provide @var{signal}.
1397
1398 @var{handler} is a Lisp function to be called when the @var{signal} is
1399 received. It must accept as arguments the output parameters
1400 @var{signal} is sending.
1401
1402 All other arguments @var{args}, if specified, must be strings. They
1403 stand for the respective arguments of @var{signal} in their order, and
1404 are used for filtering as well. A @code{nil} argument might be used
1405 to preserve the order.
1406
1407 @code{dbus-register-signal} returns a Lisp object, which can be used
1408 as argument in @code{dbus-unregister-object} for removing the
1409 registration for @var{signal}. Example:
1410
1411 @lisp
1412 (defun my-dbus-signal-handler (device)
1413 (message "Device %s added" device))
1414
1415 @result{} my-dbus-signal-handler
1416
1417 (dbus-register-signal
1418 :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1419 "org.freedesktop.Hal.Manager" "DeviceAdded"
1420 'my-dbus-signal-handler)
1421
1422 @result{} ((:system "org.freedesktop.Hal.Manager" "DeviceAdded")
1423 ("org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
1424 my-signal-handler))
1425 @end lisp
1426
1427 As we know from the introspection data of interface
1428 @samp{org.freedesktop.Hal.Manager}, the signal @samp{DeviceAdded}
1429 provides one single parameter, which is mapped into a Lisp string.
1430 The callback function @code{my-dbus-signal-handler} must define one
1431 single string argument therefore. Plugging an USB device to your
1432 machine, when registered for signal @samp{DeviceAdded}, will show you
1433 which objects the GNU/Linux @code{hal} daemon adds.
1434 @end defun
1435
1436 @defun dbus-unregister-object object
1437 Unregister @var{object} from the the D-Bus. @var{object} must be the
1438 result of a preceding @code{dbus-register-signal} or
1439 @code{dbus-register-method} call. It returns @code{t} if @var{object}
1440 has been unregistered, @code{nil} otherwise.
1441 @end defun
1442
1443
1444 @node Errors and Events
1445 @chapter Errors and events.
1446 @cindex errors
1447 @cindex events
1448
1449 Input parameters of @code{dbus-call-method},
1450 @code{dbus-call-method-non-blocking},
1451 @code{dbus-call-method-asynchronously}, and
1452 @code{dbus-register-signal} are checked for correct D-Bus types. If
1453 there is a type mismatch, the Lisp error @code{wrong-type-argument}
1454 @code{D-Bus ARG} is raised.
1455
1456 All errors raised by D-Bus are signaled with the error symbol
1457 @code{dbus-error}. If possible, error messages from D-Bus are
1458 appended to the @code{dbus-error}.
1459
1460 @defspec dbus-ignore-errors forms@dots{}
1461 This executes @var{forms} exactly like a @code{progn}, except that
1462 @code{dbus-error} errors are ignored during the @var{forms}. These
1463 errors can be made visible when variable @code{dbus-debug} is set to
1464 @code{t}.
1465 @end defspec
1466
1467 Incoming D-Bus messages are handled as Emacs events (see @pxref{Misc
1468 Events, , , elisp}). The generated event has this form:
1469
1470 @lisp
1471 (dbus-event @var{bus} @var{type} @var{serial} @var{service} @var{path} @var{interface} @var{member} @var{handler}
1472 &rest @var{args})
1473 @end lisp
1474
1475 @var{bus} identifies the D-Bus the message is coming from. It is
1476 either the symbol @code{:system} or the symbol @code{:session}.
1477
1478 @var{type} is the D-Bus message type which has caused the event. It
1479 can be @code{dbus-message-type-invalid},
1480 @code{dbus-message-type-method-call},
1481 @code{dbus-message-type-method-return},
1482 @code{dbus-message-type-error}, or @code{dbus-message-type-signal}.
1483 @var{serial} is the serial number of the received D-Bus message.
1484
1485 @var{service} and @var{path} are the unique name and the object path
1486 of the D-Bus object emitting the message. @var{interface} and
1487 @var{member} denote the message which has been sent.
1488
1489 @var{handler} is the callback function which has been registered for
1490 this message (see @pxref{Signals}). When a @code{dbus-event} event
1491 arrives, @var{handler} is called with @var{args} as arguments.
1492
1493 In order to inspect the @code{dbus-event} data, you could extend the
1494 definition of the callback function in @ref{Signals}:
1495
1496 @lisp
1497 (defun my-dbus-signal-handler (&rest args)
1498 (message "my-dbus-signal-handler: %S" last-input-event))
1499 @end lisp
1500
1501 There exist convenience functions which could be called inside a
1502 callback function in order to retrieve the information from the event.
1503
1504 @defun dbus-event-bus-name event
1505 Returns the bus name @var{event} is coming from.
1506 The result is either the symbol @code{:system} or the symbol @code{:session}.
1507 @end defun
1508
1509 @defun dbus-event-message-type event
1510 Returns the message type of the corresponding D-Bus message. The
1511 result is a number.
1512 @end defun
1513
1514 @defun dbus-event-serial-number event
1515 Returns the serial number of the corresponding D-Bus message.
1516 The result is a number.
1517 @end defun
1518
1519 @defun dbus-event-service-name event
1520 Returns the unique name of the D-Bus object @var{event} is coming from.
1521 @end defun
1522
1523 @defun dbus-event-path-name event
1524 Returns the object path of the D-Bus object @var{event} is coming from.
1525 @end defun
1526
1527 @defun dbus-event-interface-name event
1528 Returns the interface name of the D-Bus object @var{event} is coming from.
1529 @end defun
1530
1531 @defun dbus-event-member-name event
1532 Returns the member name of the D-Bus object @var{event} is coming
1533 from. It is either a signal name or a method name.
1534 @end defun
1535
1536 D-Bus errors are not propagated during event handling, because it is
1537 usually not desired. D-Bus errors in events can be made visible by
1538 setting the variable @code{dbus-debug} to @code{t}. They can also be
1539 handled by a hook function.
1540
1541 @defvar dbus-event-error-hooks
1542 This hook variable keeps a list of functions, which are called when a
1543 D-Bus error happens in the event handler. Every function must accept
1544 two arguments, the event and the error variable catched in
1545 @code{condition-case} by @code{dbus-error}.
1546
1547 Such functions can be used the adapt the error signal to be raised.
1548 Example:
1549
1550 @lisp
1551 (defun my-dbus-event-error-handler (event error)
1552 (when (string-equal (concat dbus-service-emacs ".FileManager")
1553 (dbus-event-interface-name event))
1554 (message "my-dbus-event-error-handler: %S %S" event error)
1555 (signal 'file-error (cdr error))))
1556
1557 (add-hook 'dbus-event-error-hooks 'my-dbus-event-error-handler)
1558 @end lisp
1559 @end defvar
1560
1561 Hook functions shall take into account, that there might be other
1562 D-Bus applications running. Therefore, they shall check carefully,
1563 whether a given D-Bus error is related to them.
1564
1565
1566 @node GNU Free Documentation License
1567 @appendix GNU Free Documentation License
1568 @include doclicense.texi
1569
1570 @contents
1571 @c End of dbus.texi
1572 @bye
1573
1574 @ignore
1575 arch-tag: 2eeec19d-0caf-44e0-a193-329d7f9951d8
1576 @end ignore