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