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