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