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