Update Back-Cover Text as per maintain.info.
[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
4db2806c 9Copyright @copyright{} 2007, 2008 Free Software Foundation, Inc.
22d8ac3a
MA
10
11@quotation
12Permission is granted to copy, distribute and/or modify this document
13under the terms of the GNU Free Documentation License, Version 1.2 or
14any later version published by the Free Software Foundation; with no
15Invariant Sections, with the Front-Cover texts being ``A GNU
16Manual'', and with the Back-Cover Texts as in (a) below. A copy of the
17license is included in the section entitled ``GNU Free Documentation
18License'' in the Emacs manual.
19
6f093307
GM
20(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
21modify this GNU manual. Buying copies from the FSF supports it in
22developing GNU and promoting software freedom.''
22d8ac3a
MA
23
24This document is part of a collection distributed under the GNU Free
25Documentation License. If you want to distribute this document
26separately from the collection, you can do so by adding a copy of the
27license to the document, as described in section 6 of the license.
28@end quotation
29@end copying
30
31@dircategory Emacs
32@direntry
33* D-Bus: (dbus). Using D-Bus in Emacs.
34@end direntry
35
36@node Top, Overview, (dir), (dir)
37@top D-Bus integration in Emacs
38
39This manual documents an API for usage of D-Bus in
40Emacs.@footnote{D-Bus is not enabled by default. You must run
41@command{./configure --with-dbus} in Emacs' top level directory,
42before you compile Emacs.} D-Bus is a message bus system, a simple
43way for applications to talk to one another. An overview of D-Bus can
44be found at @uref{http://dbus.freedesktop.org/}.
45
46@insertcopying
47
48@menu
49* Overview:: An overview of D-Bus.
50* Inspection:: Inspection of the bus names.
51* Type Conversion:: Mapping Lisp types and D-Bus types.
52* Synchronous Methods:: Calling methods in a blocking way.
addb7f2e 53* Receiving Method Calls:: Offering own methods.
22d8ac3a
MA
54* Signals:: Sending and receiving signals.
55* Errors and Events:: Errors and events.
56* GNU Free Documentation License:: The license for this documentation.
57@end menu
58
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
110@chapter Inspection of the bus names.
111@cindex inspection
112
113There are several basic functions which inspect the buses for
114registered names. Internally they use the basic interface
115@samp{org.freedesktop.DBus}, which is supported by all objects of a bus.
116
117@defun dbus-list-activatable-names
22d8ac3a
MA
118This function returns the D-Bus service names, which can be activated.
119An activatable service is described in a service registration file.
120Under GNU/Linux, such files are located at
121@file{/usr/share/dbus-1/services/}.
122
123The result is a list of strings, which is @code{nil} when there are no
124activatable service names at all.
125@end defun
126
127@defun dbus-list-names bus
22d8ac3a
MA
128All service names, which are registered at D-Bus @var{bus}, are
129returned. The result is a list of strings, which is @code{nil} when
130there are no registered service names at all. Well known names are
131strings like @samp{org.freedesktop.DBus}. Names starting with
132@samp{:} are unique names for services.
133
134@var{bus} must be either the symbol @code{:system} or the symbol
135@code{:session}.
136@end defun
137
138@defun dbus-list-known-names bus
22d8ac3a
MA
139Retrieves all services which correspond to a known name in @var{bus}.
140A service has a known name if it doesn't start with @samp{:}. The
141result is a list of strings, which is @code{nil} when there are no
142known names at all.
143
144@var{bus} must be either the symbol @code{:system} or the symbol
145@code{:session}.
146@end defun
147
148@defun dbus-list-queued-owners bus service
22d8ac3a
MA
149For a given service, registered at D-Bus @var{bus} under the name
150@var{service}, all queued unique names are returned. The result is a
151list of strings, or @code{nil} when there are no queued names for
152@var{service} at all.
153
154@var{bus} must be either the symbol @code{:system} or the symbol
155@code{:session}. @var{service} must be a known service name as
156string.
157@end defun
158
159@defun dbus-get-name-owner bus service
22d8ac3a 160For a given service, registered at D-Bus @var{bus} under the name
06c0751a
MA
161@var{service}, the unique name of the name owner is returned. The
162result is a string, or @code{nil} when there exist no name owner of
163@var{service}.
22d8ac3a
MA
164
165@var{bus} must be either the symbol @code{:system} or the symbol
166@code{:session}. @var{service} must be a known service name as
167string.
168@end defun
169
06c0751a
MA
170@defun dbus-ping bus service
171Check whether the service name @var{service} is registered at D-Bus
172@var{bus}. @var{service} might not have been started yet. The result
173is either @code{t} or @code{nil}.
174
175@var{bus} must be either the symbol @code{:system} or the symbol
176@code{:session}. @var{service} must be a string. Example:
177
178@lisp
179(message
180 "%s screensaver on board."
181 (cond
182 ((dbus-ping :session "org.gnome.ScreenSaver") "Gnome")
183 ((dbus-ping :session "org.freedesktop.ScreenSaver") "KDE")
184 (t "No")))
185@end lisp
186@end defun
187
22d8ac3a 188@defun dbus-get-unique-name bus
22d8ac3a
MA
189The unique name, under which Emacs is registered at D-Bus @var{bus},
190is returned as string.
191
192@var{bus} must be either the symbol @code{:system} or the symbol
193@code{:session}.
194@end defun
195
196@defun dbus-introspect bus service path
22d8ac3a
MA
197Objects can publish there interfaces to the D-Bus. This function
198returns all interfaces of @var{service}, registered at object path
199@var{path} at bus @var{bus}.
200
201@var{bus} must be either the symbol @code{:system} or the symbol
202@code{:session}. @var{service} must be a known service name, and
203@var{path} must be a valid object path. The last two parameters are
204strings. The result, the introspection data, is a string in XML
205format. Example:
206
06c0751a 207@lisp
22d8ac3a
MA
208(dbus-introspect
209 :system "org.freedesktop.Hal"
210 "/org/freedesktop/Hal/devices/computer")
211
d9e21158
MA
212@result{} "<!DOCTYPE node PUBLIC
213 \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"
214 \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">
215 <node>
216 <interface name=\"org.freedesktop.Hal.Device\">
217 <method name=\"GetAllProperties\">
218 <arg name=\"properties\" direction=\"out\" type=\"a@{sv@}\"/>
219 </method>
c9ecb5a7 220 @dots{}
d9e21158
MA
221 <signal name=\"PropertyModified\">
222 <arg name=\"num_updates\" type=\"i\"/>
223 <arg name=\"updates\" type=\"a(sbb)\"/>
224 </signal>
225 </interface>
c9ecb5a7 226 @dots{}
d9e21158 227 </node>"
06c0751a 228@end lisp
22d8ac3a
MA
229
230This example informs us, that the service @code{org.freedesktop.Hal}
231at object path @code{/org/freedesktop/Hal/devices/computer} offers the
232interface @code{org.freedesktop.Hal.Device} (and 2 other interfaces
233not documented here). This interface contains the method
234@code{GetAllProperties}, which needs no input parameters, but returns
235as output parameter an array of dictionary entries (key-value pairs).
236Every dictionary entry has a string as key, and a variant as value.
237
238The interface offers also a signal, which returns 2 parameters: an
239integer, and an array consisting of elements which are a struct of a
240string and 2 boolean values.
241
242Such type descriptions are called @dfn{signature} in D-Bus. For a
243discussion of D-Bus types and their Lisp representation see @ref{Type
244Conversion}.@footnote{D-Bus signatures are explained in the D-Bus
245specification
246@uref{http://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-signatures}.
247The interfaces of the service @code{org.freedesktop.Hal} are described
248at
249@uref{http://people.freedesktop.org/~david/hal-spec/hal-spec.html#interfaces}.}
250@end defun
251
252
253@node Type Conversion
254@chapter Mapping Lisp types and D-Bus types.
255@cindex type conversion
256
257D-Bus method calls and signals accept usually several arguments as
258parameters, either as input parameter, or as output parameter. Every
259argument belongs to a D-Bus type.
260
6a31c819 261Such arguments must be mapped between the value encoded as a D-Bus
22d8ac3a
MA
262type, and the corresponding type of Lisp objects. The mapping is
263applied Lisp object @expansion{} D-Bus type for input parameters, and
264D-Bus type @expansion{} Lisp object for output parameters.
265
266
267@section Input parameters.
268
269Input parameters for D-Bus methods and signals occur as arguments of a
6a31c819 270Lisp function call. The following mapping to D-Bus types is
22d8ac3a
MA
271applied, when the corresponding D-Bus message is created:
272
273@example
274@multitable {@code{t} and @code{nil}} {@expansion{}} {DBUS_TYPE_BOOLEAN}
275@item Lisp type @tab @tab D-Bus type
276@item
277@item @code{t} and @code{nil} @tab @expansion{} @tab DBUS_TYPE_BOOLEAN
278@item number @tab @expansion{} @tab DBUS_TYPE_UINT32
279@item integer @tab @expansion{} @tab DBUS_TYPE_INT32
280@item float @tab @expansion{} @tab DBUS_TYPE_DOUBLE
281@item string @tab @expansion{} @tab DBUS_TYPE_STRING
6a31c819 282@item list @tab @expansion{} @tab DBUS_TYPE_ARRAY
22d8ac3a
MA
283@end multitable
284@end example
285
6a31c819
MA
286Other Lisp objects, like symbols or hash tables, are not accepted as
287input parameter.
288
289If it is necessary to use another D-Bus type, a corresponding type
290symbol can be preceeded to the corresponding Lisp object. Basic D-Bus
d4e67bc5
MA
291types are represented by the type symbols @code{:byte},
292@code{:boolean}, @code{:int16}, @code{:uint16}, @code{:int32},
293@code{:uint32}, @code{:int64}, @code{:uint64}, @code{:double},
294@code{:string}, @code{:object-path} and @code{:signature}.
6a31c819 295
22d8ac3a 296@noindent
6a31c819
MA
297Example:
298
299@lisp
c9ecb5a7 300(dbus-call-method @dots{} @var{NUMBER} @var{STRING})
6a31c819
MA
301@end lisp
302
303is equivalent to
304
305@lisp
c9ecb5a7 306(dbus-call-method @dots{} :uint32 @var{NUMBER} :string @var{STRING})
6a31c819
MA
307@end lisp
308
309but different to
310
311@lisp
c9ecb5a7 312(dbus-call-method @dots{} :int32 @var{NUMBER} :signature @var{STRING})
6a31c819
MA
313@end lisp
314
4db2806c
MA
315The value for a byte D-Bus type can be any integer in the range 0
316through 255. If a character is used as argument, modifiers
317represented outside this range are stripped of. For example,
318@code{:byte ?x} is equal to @code{:byte ?\M-x}, but it is not equal to
319@code{:byte ?\C-x} or @code{:byte ?\M-\C-x}.
c9ecb5a7 320
4db2806c
MA
321A D-Bus compound type is always represented as a list. The @sc{car}
322of this list can be the type symbol @code{:array}, @code{:variant},
d4e67bc5
MA
323@code{:struct} or @code{:dict-entry}, which would result in a
324corresponding D-Bus container. @code{:array} is optional, because
4db2806c 325this is the default compound D-Bus type for a list.
6a31c819
MA
326
327The objects being elements of the list are checked according to the
328D-Bus compound type rules.
329
330@itemize
4db2806c
MA
331@item An array must contain only elements of the same D-Bus type. It
332can be empty.
333
6a31c819 334@item A variant must contain only one single element.
4db2806c 335
6a31c819 336@item A dictionary entry must be element of an array, and it must
4db2806c
MA
337contain only a key-value pair of two elements, with a basic D-Bus type
338key.
339
6a31c819
MA
340@item There is no restriction for structs.
341@end itemize
342
4db2806c
MA
343If an empty array needs an element D-Bus type other than string, it
344can contain exactly one element of D-Bus type @code{:signature}. The
345value of this element (a string) is used as the signature of the
346elements of this array. Example:
6a31c819
MA
347
348@lisp
4db2806c
MA
349(dbus-call-method
350 :session "org.freedesktop.Notifications"
351 "/org/freedesktop/Notifications"
352 "org.freedesktop.Notifications" "Notify"
353 "GNU Emacs" ;; Application name.
354 0 ;; No replacement of other notifications.
355 "" ;; No icon.
356 "Notification summary" ;; Summary.
357 (format ;; Body.
358 "This is a test notification, raised from %s" (emacs-version))
359 '(:array) ;; No actions (empty array of strings).
360 '(:array :signature "@{sv@}") ;; No hints
361 ;; (empty array of dictionary entries).
362 ':int32 -1) ;; Default timeout.
363
364@result{} 3
6a31c819 365@end lisp
22d8ac3a
MA
366
367
368@section Output parameters.
369
370Output parameters of D-Bus methods and signals are mapped to Lisp
6a31c819 371objects.
22d8ac3a
MA
372
373@example
374@multitable {DBUS_TYPE_OBJECT_PATH} {@expansion{}} {@code{t} or @code{nil}}
375@item D-Bus type @tab @tab Lisp type
376@item
377@item DBUS_TYPE_BOOLEAN @tab @expansion{} @tab @code{t} or @code{nil}
d4e67bc5 378@item DBUS_TYPE_BYTE @tab @expansion{} @tab number
6a31c819 379@item DBUS_TYPE_UINT16 @tab @expansion{} @tab number
d4e67bc5
MA
380@item DBUS_TYPE_INT16 @tab @expansion{} @tab number
381@item DBUS_TYPE_UINT32 @tab @expansion{} @tab number or float
382@item DBUS_TYPE_INT32 @tab @expansion{} @tab number or float
383@item DBUS_TYPE_UINT64 @tab @expansion{} @tab number or float
384@item DBUS_TYPE_INT64 @tab @expansion{} @tab number or float
22d8ac3a
MA
385@item DBUS_TYPE_DOUBLE @tab @expansion{} @tab float
386@item DBUS_TYPE_STRING @tab @expansion{} @tab string
387@item DBUS_TYPE_OBJECT_PATH @tab @expansion{} @tab string
6a31c819 388@item DBUS_TYPE_SIGNATURE @tab @expansion{} @tab string
22d8ac3a
MA
389@item DBUS_TYPE_ARRAY @tab @expansion{} @tab list
390@item DBUS_TYPE_VARIANT @tab @expansion{} @tab list
391@item DBUS_TYPE_STRUCT @tab @expansion{} @tab list
392@item DBUS_TYPE_DICT_ENTRY @tab @expansion{} @tab list
393@end multitable
394@end example
395
d4e67bc5
MA
396A float object in case of @code{DBUS_TYPE_UINT32},
397@code{DBUS_TYPE_INT32}, @code{DBUS_TYPE_UINT64} and
398@code{DBUS_TYPE_INT6432} is returned, when the C value exceeds the
399Emacs number size range.
400
22d8ac3a
MA
401The resulting list of the last 4 D-Bus compound types contains as
402elements the elements of the D-Bus container, mapped according to the
403same rules.
404
405The signal @code{PropertyModified}, discussed as example in
406@ref{Inspection}, would offer as Lisp data the following object
407(@var{BOOL} stands here for either @code{nil} or @code{t}):
408
409@lisp
c9ecb5a7 410(@var{NUMBER} ((@var{STRING} @var{BOOL} @var{BOOL}) (@var{STRING} @var{BOOL} @var{BOOL}) @dots{}))
22d8ac3a
MA
411@end lisp
412
413
414@node Synchronous Methods
415@chapter Calling methods in a blocking way.
416@cindex method calls, synchronous
417@cindex synchronous method calls
418
419Methods can be called synchronously (@dfn{blocking}) or asynchronously
420(@dfn{non-blocking}). Currently, just synchronous methods are
421implemented.
422
423At D-Bus level, a method call consist of two messages: one message
424which carries the input parameters to the object owning the method to
425be called, and a reply message returning the resulting output
426parameters from the object.
427
134ce16c 428@defun dbus-call-method bus service path interface method &optional :timeout timeout &rest args
22d8ac3a
MA
429This function calls @var{method} on the D-Bus @var{bus}. @var{bus} is
430either the symbol @code{:system} or the symbol @code{:session}.
431
432@var{service} is the D-Bus service name to be used. @var{path} is the
433D-Bus object path, @var{service} is registered at. @var{interface} is
434an interface offered by @var{service}. It must provide @var{method}.
435
134ce16c
MA
436If the parameter @code{:timeout} is given, the following integer
437@var{timeout} specifies the maximun number of milliseconds the method
438call must return. The default value is 25.000. If the method call
439doesn't return in time, a D-Bus error is raised (@pxref{Errors and
440Events}).
441
22d8ac3a
MA
442All other arguments args are passed to @var{method} as arguments.
443They are converted into D-Bus types as described in @ref{Type
444Conversion}.
445
446The function returns the resulting values of @var{method} as a list of
447Lisp objects, according to the type conversion rules described in
448@ref{Type Conversion}. Example:
449
06c0751a 450@lisp
22d8ac3a 451(dbus-call-method
0ce574ef
MA
452 :session "org.gnome.seahorse" "/org/gnome/seahorse/keys/openpgp"
453 "org.gnome.seahorse.Keys" "GetKeyField"
22d8ac3a
MA
454 "openpgp:657984B8C7A966DD" "simple-name")
455
456@result{} (t ("Philip R. Zimmermann"))
06c0751a 457@end lisp
22d8ac3a
MA
458
459If the result of the method call is just one value, the converted Lisp
460object is returned instead of a list containing this single Lisp
461object. Example:
462
06c0751a 463@lisp
22d8ac3a 464(dbus-call-method
0ce574ef
MA
465 :system "org.freedesktop.Hal"
466 "/org/freedesktop/Hal/devices/computer"
467 "org.freedesktop.Hal.Device" "GetPropertyString"
22d8ac3a
MA
468 "system.kernel.machine")
469
470@result{} "i686"
06c0751a 471@end lisp
22d8ac3a
MA
472
473With the @code{dbus-introspect} function it is possible to explore the
474interfaces of @samp{org.freedesktop.Hal} service. It offers the
475interfaces @samp{org.freedesktop.Hal.Manager} for the object at the
476path @samp{/org/freedesktop/Hal/Manager} as well as the interface
477@samp{org.freedesktop.Hal.Device} for all objects prefixed with the
478path @samp{/org/freedesktop/Hal/devices}. With the methods
479@samp{GetAllDevices} and @samp{GetAllProperties}, it is simple to
480emulate the @code{lshal} command on GNU/Linux systems:
481
06c0751a 482@lisp
22d8ac3a
MA
483(dolist (device
484 (dbus-call-method
0ce574ef 485 :system "org.freedesktop.Hal"
22d8ac3a 486 "/org/freedesktop/Hal/Manager"
0ce574ef 487 "org.freedesktop.Hal.Manager" "GetAllDevices"))
22d8ac3a
MA
488 (message "\nudi = %s" device)
489 (dolist (properties
490 (dbus-call-method
0ce574ef
MA
491 :system "org.freedesktop.Hal" device
492 "org.freedesktop.Hal.Device" "GetAllProperties"))
22d8ac3a
MA
493 (message " %s = %S"
494 (car properties) (or (caar (cdr properties)) ""))))
495
7b13a0f2 496@print{} "udi = /org/freedesktop/Hal/devices/computer
d9e21158
MA
497 info.addons = (\"hald-addon-acpi\")
498 info.bus = \"unknown\"
499 info.product = \"Computer\"
500 info.subsystem = \"unknown\"
501 info.udi = \"/org/freedesktop/Hal/devices/computer\"
502 linux.sysfs_path_device = \"(none)\"
503 power_management.acpi.linux.version = \"20051216\"
504 power_management.can_suspend_to_disk = t
505 power_management.can_suspend_to_ram = \"\"
506 power_management.type = \"acpi\"
507 smbios.bios.release_date = \"11/07/2001\"
508 system.chassis.manufacturer = \"COMPAL\"
509 system.chassis.type = \"Notebook\"
510 system.firmware.release_date = \"03/19/2005\"
c9ecb5a7 511 @dots{}"
06c0751a 512@end lisp
22d8ac3a
MA
513@end defun
514
515
addb7f2e
MA
516@node Receiving Method Calls
517@chapter Offering own methods.
518@cindex method calls, returning
519@cindex returning method calls
520
521Emacs can also offer own methods, which can be called by other
522applications. These methods could be an implementation of an
523interface of a well known service, like @code{org.freedesktop.TextEditor}.
524
525It could be also an implementation of an own interface. In this case,
526the service name must be @code{org.gnu.Emacs}. The object path shall
527begin with @code{/org/gnu/Emacs/@strong{Application}/}, and the
528interface name shall be @code{org.gnu.Emacs.@strong{Application}}.
529@code{@strong{Application}} is the name of the application which
530provides the interface.
531
532@defun dbus-register-method bus service path interface method handler
533With this function, an application registers @var{method} on the D-Bus
534@var{bus}.
535
536@var{bus} is either the symbol @code{:system} or the symbol
537@code{:session}.
538
539@var{service} is the D-Bus service name of the D-Bus object
540@var{method} is registered for. It must be a known name.
541
542@var{path} is the D-Bus object path @var{service} is
543registered.
544
545@var{interface} is the interface offered by @var{service}. It must
546provide @var{method}.
547
548@var{handler} is a Lisp function to be called when when a @var{method}
549call is is received. It must accept as arguments the input arguments
550of @var{method}. @var{handler} must return a list, which elements are
551used as arguments for the reply message of @var{method}. This list
552can be composed like the input parameters in @ref{Type Conversion}.
553
4a7c4c40 554The default D-Bus timeout when waiting for a message reply is 25
134ce16c
MA
555seconds. This value could be even smaller, depending on the calling
556client. Therefore, @var{handler} shall not last longer than
4a7c4c40
MA
557absolutely necessary.
558
addb7f2e
MA
559@code{dbus-register-method} returns a Lisp symbol, which can be used
560as argument in @code{dbus-unregister-object} for removing the
561registration for @var{method}. Example:
562
06c0751a 563@lisp
addb7f2e
MA
564(defun my-dbus-method-handler (filename)
565 (let (result)
566 (if (find-file filename)
567 (setq result '(:boolean t))
568 (setq result '(:boolean nil)))
569 result))
570
571@result{} my-dbus-method-handler
572
573(dbus-register-method
574 :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
575 "org.freedesktop.TextEditor" "OpenFile"
576 'my-dbus-method-handler)
577
578@result{} ((:system "org.freedesktop.TextEditor" "OpenFile")
579 ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
580 my-method-handler))
06c0751a 581@end lisp
addb7f2e
MA
582
583If you invoke the method @code{org.freedesktop.TextEditor.OpenFile}
584from another D-Bus application with a filename as parameter, the file
585is opened in Emacs, and the method returns either @var{true} or
586@var{false}, indicating the success if the method. As test tool one
587could use the command line tool @code{dbus-send} in a shell:
588
589@example
590# dbus-send --session --print-reply \
591 --dest="org.freedesktop.TextEditor" \
592 "/org/freedesktop/TextEditor" \
593 "org.freedesktop.TextEditor.OpenFile" string:"/etc/hosts"
594
595@print{} method return sender=:1.22 -> dest=:1.23 reply_serial=2
596 boolean true
597@end example
598@end defun
599
600
22d8ac3a
MA
601@node Signals
602@chapter Sending and receiving signals.
603@cindex signals
604
605Signals are broadcast messages. They carry input parameters, which
606are received by all objects which have registered for such a signal.
607
0ce574ef 608@defun dbus-send-signal bus service path interface signal &rest args
22d8ac3a
MA
609This function is similar to @code{dbus-call-method}. The difference
610is, that there are no returning output parameters.
611
612The function emits @var{signal} on the D-Bus @var{bus}. @var{bus} is
613either the symbol @code{:system} or the symbol @code{:session}. It
614doesn't matter whether another object has registered for @var{signal}.
615
616@var{service} is the D-Bus service name of the object the signal is
617emitted from. @var{path} is the corresponding D-Bus object path,
618@var{service} is registered at. @var{interface} is an interface
619offered by @var{service}. It must provide @var{signal}.
620
621All other arguments args are passed to @var{signal} as arguments.
622They are converted into D-Bus types as described in @ref{Type
623Conversion}. Example:
624
06c0751a 625@lisp
22d8ac3a 626(dbus-send-signal
0ce574ef
MA
627 :session "org.gnu.Emacs" "/org/gnu/Emacs"
628 "org.gnu.Emacs.FileManager" "FileModified" "/home/albinus/.emacs")
06c0751a 629@end lisp
22d8ac3a
MA
630@end defun
631
0ce574ef 632@defun dbus-register-signal bus service path interface signal handler
22d8ac3a
MA
633With this function, an application registers for @var{signal} on the
634D-Bus @var{bus}.
635
636@var{bus} is either the symbol @code{:system} or the symbol
637@code{:session}.
638
a4397af9
MA
639@var{service} is the D-Bus service name used by the sending D-Bus
640object. It can be either a known name or the unique name of the D-Bus
641object sending the signal. In case of a unique name, signals won't be
642received any longer once the object owning this unique name has
643disappeared, and a new queued object has replaced it.
644
645When @var{service} is @code{nil}, related signals from all D-Bus
646objects shall be accepted.
647
648@var{path} is the corresponding D-Bus object path, @var{service} is
649registered at. It can also be @code{nil} if the path name of incoming
650signals shall not be checked.
651
652@var{interface} is an interface offered by @var{service}. It must
653provide @var{signal}.
22d8ac3a
MA
654
655@var{handler} is a Lisp function to be called when the @var{signal} is
656received. It must accept as arguments the output parameters
657@var{signal} is sending. Example:
658
06c0751a 659@lisp
22d8ac3a
MA
660(defun my-dbus-signal-handler (device)
661 (message "Device %s added" device))
662
0ce574ef
MA
663@result{} my-dbus-signal-handler
664
22d8ac3a 665(dbus-register-signal
0ce574ef
MA
666 :system "org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
667 "org.freedesktop.Hal.Manager" "DeviceAdded"
22d8ac3a
MA
668 'my-dbus-signal-handler)
669
cd71c3ef
MA
670@result{} ((:system "org.freedesktop.Hal.Manager" "DeviceAdded")
671 ("org.freedesktop.Hal" "/org/freedesktop/Hal/Manager"
672 my-signal-handler))
06c0751a 673@end lisp
22d8ac3a
MA
674
675As we know from the inspection data of interface
676@code{org.freedesktop.Hal.Manager}, the signal @code{DeviceAdded}
677provides one single parameter, which is mapped into a Lisp string.
678The callback function @code{my-dbus-signal-handler} must define one
679single string argument therefore. Plugging an USB device to your
680machine, when registered for signal @code{DeviceAdded}, will show you
681which objects the GNU/Linux @code{hal} daemon adds.
682
683@code{dbus-register-signal} returns a Lisp symbol, which can be used
c9ecb5a7 684as argument in @code{dbus-unregister-object} for removing the
22d8ac3a
MA
685registration for @var{signal}.
686@end defun
687
c9ecb5a7 688@defun dbus-unregister-object object
22d8ac3a 689Unregister @var{object} from the the D-Bus. @var{object} must be the
c9ecb5a7
MA
690result of a preceding @code{dbus-register-signal} or
691@code{dbus-register-method} call. It returns @code{t} if @var{object}
692has been unregistered, @code{nil} otherwise.
22d8ac3a
MA
693@end defun
694
695
696@node Errors and Events
697@chapter Errors and events.
698@cindex errors
699@cindex events
700
6a31c819
MA
701Input parameters of @code{dbus-call-method} and
702@code{dbus-register-signal} are checked for correct D-Bus types. If
703there is a type mismatch, the Lisp error @code{wrong-type-argument}
704@code{D-Bus ARG} is raised.
705
22d8ac3a 706All errors raised by D-Bus are signaled with the error symbol
d2e4a6c9
MA
707@code{dbus-error}. If possible, error messages from D-Bus are
708appended to the @code{dbus-error}.
709
710@defspec dbus-ignore-errors forms@dots{}
711This executes @var{forms} exactly like a @code{progn}, except that
712@code{dbus-error} errors are ignored during the @var{forms}. These
713errors can be made visible when variable @code{dbus-debug} is set to
714@code{t}.
715@end defspec
22d8ac3a 716
a4397af9 717Incoming D-Bus messages are handled as Emacs events (see @pxref{Misc
22d8ac3a
MA
718Events, , , elisp}). The generated event has this form:
719
06c0751a 720@lisp
addb7f2e 721(dbus-event @var{bus} @var{serial} @var{service} @var{path} @var{interface} @var{member} @var{handler} &rest @var{args})
06c0751a 722@end lisp
22d8ac3a 723
a4397af9
MA
724@var{bus} identifies the D-Bus the signal is coming from. It is
725either the symbol @code{:system} or the symbol @code{:session}.
22d8ac3a 726
addb7f2e
MA
727@var{serial} is the serial number of the received D-Bus message if it
728is a method call, or @code{nil}.
729
22d8ac3a 730@var{service} and @var{path} are the unique name and the object path
addb7f2e
MA
731of the D-Bus object emitting the message. @var{interface} and
732@var{member} denote the message which has been sent.
22d8ac3a 733
0ce574ef 734@var{handler} is the callback function which has been registered for
addb7f2e 735this message (see @pxref{Signals}). When a @code{dbus-event} event
0ce574ef
MA
736arrives, @var{handler} is called with @var{args} as arguments.
737
22d8ac3a
MA
738In order to inspect the @code{dbus-event} data, you could extend the
739definition of the callback function in @ref{Signals}:
740
06c0751a 741@lisp
22d8ac3a
MA
742(defun my-dbus-signal-handler (&rest args)
743 (message "my-dbus-signal-handler: %S" last-input-event))
06c0751a 744@end lisp
22d8ac3a
MA
745
746There exist convenience functions which could be called inside a
747callback function in order to retrieve the information from the event.
748
749@defun dbus-event-bus-name event
22d8ac3a
MA
750Returns the bus name @var{event} is coming from.
751The result is either the symbol @code{:system} or the symbol @code{:session}.
752@end defun
753
addb7f2e
MA
754@defun dbus-event-serial-number event
755Returns the serial number of the corresponding D-Bus message.
756The result is a number in case the D-Bus message is a method
757call, or @code{nil} for all other mesage types.
758@end defun
759
22d8ac3a 760@defun dbus-event-service-name event
22d8ac3a
MA
761Returns the unique name of the D-Bus object @var{event} is coming from.
762@end defun
763
764@defun dbus-event-path-name event
22d8ac3a
MA
765Returns the object path of the D-Bus object @var{event} is coming from.
766@end defun
767
768@defun dbus-event-interface-name event
22d8ac3a
MA
769Returns the interface name of of the D-Bus object @var{event} is coming from.
770@end defun
771
772@defun dbus-event-member-name event
22d8ac3a
MA
773Returns the member name of of the D-Bus object @var{event} is coming
774from. It is either a signal name or a method name.
775@end defun
776
c9ecb5a7
MA
777D-Bus errors are not propagated during event handling, because it is
778usually not desired. D-Bus errors in events can be made visible by
779setting the variable @code{dbus-debug} to @code{t}.
780
22d8ac3a
MA
781
782@node GNU Free Documentation License
783@appendix GNU Free Documentation License
784@include doclicense.texi
785
786@contents
787@c End of dbus.texi
788@bye
79f10da0
MB
789
790@ignore
791 arch-tag: 2eeec19d-0caf-44e0-a193-329d7f9951d8
792@end ignore