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