@c @setchapternewpage odd
@c %**end of header
+@syncodeindex vr cp
+@syncodeindex fn cp
+
@copying
-Copyright @copyright{} 2007, 2008 Free Software Foundation, Inc.
+Copyright @copyright{} 2007-2011 Free Software Foundation, Inc.
@quotation
Permission is granted to copy, distribute and/or modify this document
-under the terms of the GNU Free Documentation License, Version 1.2 or
+under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover texts being ``A GNU Manual'',
and with the Back-Cover Texts as in (a) below. A copy of the license
@end quotation
@end copying
-@dircategory Emacs
+@dircategory Emacs lisp libraries
@direntry
* D-Bus: (dbus). Using D-Bus in Emacs.
@end direntry
+@contents
+
@node Top, Overview, (dir), (dir)
@top D-Bus integration in Emacs
-This manual documents an API for usage of D-Bus in
-Emacs.@footnote{D-Bus is not enabled by default. You must run
-@command{./configure --with-dbus} in Emacs' top level directory,
-before you compile Emacs.} D-Bus is a message bus system, a simple
-way for applications to talk to one another. An overview of D-Bus can
-be found at @uref{http://dbus.freedesktop.org/}.
+This manual documents an API for usage of D-Bus in Emacs. D-Bus is a
+message bus system, a simple way for applications to talk to one
+another. An overview of D-Bus can be found at
+@uref{http://dbus.freedesktop.org/}.
+@ifnottex
@insertcopying
+@end ifnottex
@menu
* Overview:: An overview of D-Bus.
* Inspection:: Inspection of D-Bus services.
* Type Conversion:: Mapping Lisp types and D-Bus types.
* Synchronous Methods:: Calling methods in a blocking way.
+* Asynchronous Methods:: Calling methods non-blocking.
* Receiving Method Calls:: Offering own methods.
* Signals:: Sending and receiving signals.
+* Alternative Buses:: Alternative buses.
* Errors and Events:: Errors and events.
+* Index:: Index including concepts, functions, variables.
+
* GNU Free Documentation License:: The license for this documentation.
@end menu
registered names. Internally they use the basic interface
@samp{org.freedesktop.DBus}, which is supported by all objects of a bus.
-@defun dbus-list-activatable-names
-This function returns the D-Bus service names, which can be activated.
-An activatable service is described in a service registration file.
-Under GNU/Linux, such files are located at
-@file{/usr/share/dbus-1/services/}.
+@defun dbus-list-activatable-names &optional bus
+This function returns the D-Bus service names, which can be activated
+for @var{bus}. It must be either the symbol @code{:system} (the
+default) or the symbol @code{:session}. An activatable service is
+described in a service registration file. Under GNU/Linux, such files
+are located at @file{/usr/share/dbus-1/system-services/} (for the
+@code{:system} bus) or @file{/usr/share/dbus-1/services/}. An
+activatable service is not necessarily registered at @var{bus} at already.
The result is a list of strings, which is @code{nil} when there are no
-activatable service names at all.
+activatable service names at all. Example:
+
+@lisp
+;; Check, whether the document viewer can be accessed via D-Bus.
+(member "org.gnome.evince.Daemon"
+ (dbus-list-activatable-names :session))
+@end lisp
+
@end defun
@defun dbus-list-names bus
@end defun
@defun dbus-list-known-names bus
-Retrieves all services which correspond to a known name in @var{bus}.
+Retrieves all registered services which correspond to a known name in @var{bus}.
A service has a known name if it doesn't start with @samp{:}. The
result is a list of strings, which is @code{nil} when there are no
known names at all.
string.
@end defun
-@defun dbus-ping bus service
+@defun dbus-ping bus service &optional timeout
Check whether the service name @var{service} is registered at D-Bus
-@var{bus}. @var{service} might not have been started yet. The result
-is either @code{t} or @code{nil}.
+@var{bus}. @var{service} might not have been started yet, it is
+autostarted if possible. The result is either @code{t} or @code{nil}.
@var{bus} must be either the symbol @code{:system} or the symbol
-@code{:session}. @var{service} must be a string. Example:
+@code{:session}. @var{service} must be a string. @var{timeout}, a
+nonnegative integer, specifies the maximum number of milliseconds
+@code{dbus-ping} must return. The default value is 25,000. Example:
@lisp
(message
"%s screensaver on board."
(cond
- ((dbus-ping :session "org.gnome.ScreenSaver") "Gnome")
- ((dbus-ping :session "org.freedesktop.ScreenSaver") "KDE")
+ ((dbus-ping :session "org.gnome.ScreenSaver" 100) "Gnome")
+ ((dbus-ping :session "org.freedesktop.ScreenSaver" 100) "KDE")
(t "No")))
@end lisp
+
+If it shall be checked whether @var{service} is already running
+without autostarting it, one shall apply
+
+@lisp
+(member service (dbus-list-known-names bus))
+@end lisp
@end defun
@defun dbus-get-unique-name bus
@result{} "/org/freedesktop/SystemToolsBackends/UsersConfig"
@end lisp
-If @var{object} has no @var{attribute}, the function returns nil.
+If @var{object} has no @var{attribute}, the function returns
+@code{nil}.
@end defun
@defun dbus-get-property bus service path interface property
This function returns the value of @var{property} of @var{interface}.
It will be checked at @var{bus}, @var{service}, @var{path}. The
-result can be any valid D-Bus value, or nil if there is no
+result can be any valid D-Bus value, or @code{nil} if there is no
@var{property}. Example:
@lisp
"/org/freedesktop/xesam/searcher/main"
"org.freedesktop.xesam.Search" "HitsAdded")
-@result{} \"su\""
+@result{} "su"
@end lisp
@end defun
applied, when the corresponding D-Bus message is created:
@example
-@multitable {@code{t} and @code{nil}} {@expansion{}} {DBUS_TYPE_BOOLEAN}
+@multitable {negative integer} {@expansion{}} {DBUS_TYPE_BOOLEAN}
@item Lisp type @tab @tab D-Bus type
@item
@item @code{t} and @code{nil} @tab @expansion{} @tab DBUS_TYPE_BOOLEAN
-@item number @tab @expansion{} @tab DBUS_TYPE_UINT32
-@item integer @tab @expansion{} @tab DBUS_TYPE_INT32
+@item natural number @tab @expansion{} @tab DBUS_TYPE_UINT32
+@item negative integer @tab @expansion{} @tab DBUS_TYPE_INT32
@item float @tab @expansion{} @tab DBUS_TYPE_DOUBLE
@item string @tab @expansion{} @tab DBUS_TYPE_STRING
@item list @tab @expansion{} @tab DBUS_TYPE_ARRAY
@end example
Other Lisp objects, like symbols or hash tables, are not accepted as
-input parameter.
+input parameters.
If it is necessary to use another D-Bus type, a corresponding type
-symbol can be preceeded to the corresponding Lisp object. Basic D-Bus
+symbol can be prepended to the corresponding Lisp object. Basic D-Bus
types are represented by the type symbols @code{:byte},
@code{:boolean}, @code{:int16}, @code{:uint16}, @code{:int32},
@code{:uint32}, @code{:int64}, @code{:uint64}, @code{:double},
-@code{:string}, @code{:object-path} and @code{:signature}.
+@code{:string}, @code{:object-path}, @code{:signature} and
+@code{:unix-fd}.
@noindent
Example:
@lisp
-(dbus-call-method @dots{} @var{NUMBER} @var{STRING})
+(dbus-call-method @dots{} @var{NAT-NUMBER} @var{STRING})
@end lisp
is equivalent to
@lisp
-(dbus-call-method @dots{} :uint32 @var{NUMBER} :string @var{STRING})
+(dbus-call-method @dots{} :uint32 @var{NAT-NUMBER} :string @var{STRING})
@end lisp
but different to
@lisp
-(dbus-call-method @dots{} :int32 @var{NUMBER} :signature @var{STRING})
+(dbus-call-method @dots{} :int32 @var{NAT-NUMBER} :signature @var{STRING})
@end lisp
The value for a byte D-Bus type can be any integer in the range 0
'(:array) ;; No actions (empty array of strings).
'(:array :signature "@{sv@}") ;; No hints
;; (empty array of dictionary entries).
- ':int32 -1) ;; Default timeout.
+ :int32 -1) ;; Default timeout.
@result{} 3
@end lisp
+@defun dbus-string-to-byte-array string
+Sometimes, D-Bus methods require as input parameter an array of bytes,
+instead of a string. If it is guaranteed, that @var{string} is an
+UTF8 string, this function performs the conversion. Example:
+
+@lisp
+(dbus-string-to-byte-array "/etc/hosts")
+
+@result{} (:array :byte 47 :byte 101 :byte 116 :byte 99 :byte 47
+ :byte 104 :byte 111 :byte 115 :byte 116 :byte 115)
+@end lisp
+@end defun
+
+@defun dbus-escape-as-identifier string
+Escape an arbitrary @var{string} so it follows the rules for a C
+identifier. The escaped string can be used as object path component,
+interface element component, bus name component or member name in
+D-Bus.
+
+The escaping consists of replacing all non-alphanumerics, and the
+first character if it's a digit, with an underscore and two
+lower-case hex digits. As a special case, "" is escaped to
+"_". Example:
+
+@lisp
+(dbus-escape-as-identifier "0123abc_xyz\x01\xff")
+
+@result{} "_30123abc_5fxyz_01_ff"
+@end lisp
+@end defun
+
@section Output parameters.
objects.
@example
-@multitable {DBUS_TYPE_OBJECT_PATH} {@expansion{}} {@code{t} or @code{nil}}
+@multitable {DBUS_TYPE_OBJECT_PATH} {@expansion{}} {natural number or float}
@item D-Bus type @tab @tab Lisp type
@item
@item DBUS_TYPE_BOOLEAN @tab @expansion{} @tab @code{t} or @code{nil}
-@item DBUS_TYPE_BYTE @tab @expansion{} @tab number
-@item DBUS_TYPE_UINT16 @tab @expansion{} @tab number
-@item DBUS_TYPE_INT16 @tab @expansion{} @tab number
-@item DBUS_TYPE_UINT32 @tab @expansion{} @tab number or float
-@item DBUS_TYPE_INT32 @tab @expansion{} @tab number or float
-@item DBUS_TYPE_UINT64 @tab @expansion{} @tab number or float
-@item DBUS_TYPE_INT64 @tab @expansion{} @tab number or float
+@item DBUS_TYPE_BYTE @tab @expansion{} @tab natural number
+@item DBUS_TYPE_UINT16 @tab @expansion{} @tab natural number
+@item DBUS_TYPE_INT16 @tab @expansion{} @tab integer
+@item DBUS_TYPE_UINT32 @tab @expansion{} @tab natural number or float
+@item DBUS_TYPE_UNIX_FD @tab @expansion{} @tab natural number or float
+@item DBUS_TYPE_INT32 @tab @expansion{} @tab integer or float
+@item DBUS_TYPE_UINT64 @tab @expansion{} @tab natural number or float
+@item DBUS_TYPE_INT64 @tab @expansion{} @tab integer or float
@item DBUS_TYPE_DOUBLE @tab @expansion{} @tab float
@item DBUS_TYPE_STRING @tab @expansion{} @tab string
@item DBUS_TYPE_OBJECT_PATH @tab @expansion{} @tab string
@end example
A float object in case of @code{DBUS_TYPE_UINT32},
-@code{DBUS_TYPE_INT32}, @code{DBUS_TYPE_UINT64} and
-@code{DBUS_TYPE_INT6432} is returned, when the C value exceeds the
-Emacs number size range.
+@code{DBUS_TYPE_INT32}, @code{DBUS_TYPE_UINT64},
+@code{DBUS_TYPE_INT64} and @code{DBUS_TYPE_UNIX_FD} is returned, when
+the C value exceeds the Emacs number size range.
The resulting list of the last 4 D-Bus compound types contains as
elements the elements of the D-Bus container, mapped according to the
(@var{BOOL} stands here for either @code{nil} or @code{t}):
@lisp
-(@var{NUMBER} ((@var{STRING} @var{BOOL} @var{BOOL}) (@var{STRING} @var{BOOL} @var{BOOL}) @dots{}))
+(@var{INTEGER} ((@var{STRING} @var{BOOL} @var{BOOL}) (@var{STRING} @var{BOOL} @var{BOOL}) @dots{}))
+@end lisp
+
+@defun dbus-byte-array-to-string byte-array
+If a D-Bus method or signal returns an array of bytes, which are known
+to represent an UTF8 string, this function converts @var{byte-array}
+to the corresponding string. Example:
+
+@lisp
+(dbus-byte-array-to-string '(47 101 116 99 47 104 111 115 116 115))
+
+@result{} "/etc/hosts"
@end lisp
+@end defun
+
+@defun dbus-unescape-from-identifier string
+Retrieve the original string from the encoded @var{string}.
+@var{string} must have been coded with
+@code{dbus-escape-as-identifier}. Example:
+
+@lisp
+(dbus-unescape-from-identifier "_30123abc_5fxyz_01_ff")
+
+@ifinfo
+@result{} "0123abc_xyz^Aÿ"
+@end ifinfo
+@ifnotinfo
+@result{} "0123abc_xyz^A@"y"
+@end ifnotinfo
+@end lisp
+@end defun
@node Synchronous Methods
@cindex synchronous method calls
Methods can be called synchronously (@dfn{blocking}) or asynchronously
-(@dfn{non-blocking}). Currently, just synchronous methods are
-implemented.
+(@dfn{non-blocking}).
At D-Bus level, a method call consist of two messages: one message
which carries the input parameters to the object owning the method to
an interface offered by @var{service}. It must provide @var{method}.
If the parameter @code{:timeout} is given, the following integer
-@var{timeout} specifies the maximun number of milliseconds the method
-call must return. The default value is 25.000. If the method call
+@var{timeout} specifies the maximum number of milliseconds the method
+call must return. The default value is 25,000. If the method call
doesn't return in time, a D-Bus error is raised (@pxref{Errors and
Events}).
@end lisp
@end defun
+@defun dbus-call-method-non-blocking bus service path interface method &optional :timeout timeout &rest args
+Call @var{method} on the D-Bus @var{bus}, but don't block the event queue.
+This is necessary for communicating to registered D-Bus methods,
+which are running in the same Emacs process.
+
+The arguments are the same as in @code{dbus-call-method}. Example:
+
+@lisp
+(dbus-call-method-non-blocking
+ :system "org.freedesktop.Hal"
+ "/org/freedesktop/Hal/devices/computer"
+ "org.freedesktop.Hal.Device" "GetPropertyString"
+ "system.kernel.machine")
+
+@result{} "i686"
+@end lisp
+@end defun
+
+
+@node Asynchronous Methods
+@chapter Calling methods non-blocking.
+@cindex method calls, asynchronous
+@cindex asynchronous method calls
+
+@defun dbus-call-method-asynchronously bus service path interface method handler &optional :timeout timeout &rest args
+This function calls @var{method} on the D-Bus @var{bus}
+asynchronously. @var{bus} is either the symbol @code{:system} or the
+symbol @code{:session}.
+
+@var{service} is the D-Bus service name to be used. @var{path} is the
+D-Bus object path, @var{service} is registered at. @var{interface} is
+an interface offered by @var{service}. It must provide @var{method}.
+
+@var{handler} is a Lisp function, which is called when the
+corresponding return message has arrived. If @var{handler} is
+@code{nil}, no return message will be expected.
+
+If the parameter @code{:timeout} is given, the following integer
+@var{timeout} specifies the maximum number of milliseconds a reply
+message must arrive. The default value is 25,000. If there is no
+reply message in time, a D-Bus error is raised (@pxref{Errors and
+Events}).
+
+All other arguments args are passed to @var{method} as arguments.
+They are converted into D-Bus types as described in @ref{Type
+Conversion}.
+
+Unless @var{handler} is @code{nil}, the function returns a key into
+the hash table @code{dbus-registered-objects-table}. The
+corresponding entry in the hash table is removed, when the return
+message has been arrived, and @var{handler} is called. Example:
+
+@lisp
+(dbus-call-method-asynchronously
+ :system "org.freedesktop.Hal"
+ "/org/freedesktop/Hal/devices/computer"
+ "org.freedesktop.Hal.Device" "GetPropertyString" 'message
+ "system.kernel.machine")
+
+@result{} (:system 2)
+
+@print{} i686
+@end lisp
+@end defun
+
@node Receiving Method Calls
@chapter Offering own methods.
@cindex method calls, returning
@cindex returning method calls
-Emacs can also offer own methods, which can be called by other
-applications. These methods could be an implementation of an
-interface of a well known service, like @samp{org.freedesktop.TextEditor}.
+In order to register methods on the D-Bus, Emacs has to request a well
+known name on the D-Bus under which it will be available for other
+clients. Names on the D-Bus can be registered and unregistered using
+the following functions:
+
+@defun dbus-register-service bus service &rest flags
+Register the known name @var{service} on D-Bus @var{bus}.
+
+@var{bus} is either the symbol @code{:system} or the symbol
+@code{:session}.
+
+@var{service} is the service name to be registered on the D-Bus. It
+must be a known name.
+
+@var{flags} is a subset of the following keywords:
+
+@itemize
+@item @code{:allow-replacement}: Allow another service to become the primary
+owner if requested.
+
+@item @code{:replace-existing}: Request to replace the current primary owner.
+
+@item @code{:do-not-queue}: If we can not become the primary owner do not
+place us in the queue.
+@end itemize
+
+One of the following keywords is returned:
+
+@itemize
+
+@item @code{:primary-owner}: We have become the primary owner of the name
+@var{service}.
+
+@item @code{:in-queue}: We could not become the primary owner and
+have been placed in the queue.
+
+@item @code{:exists}: We already are in the queue.
+
+@item @code{:already-owner}: We already are the primary
+owner.
+@end itemize
+@end defun
+
+@defun dbus-unregister-service bus service
+Unregister all objects from D-Bus @var{bus}, registered by Emacs for
+@var{service}.
+
+@var{bus} is either the symbol @code{:system} or the symbol
+@code{:session}.
+
+@var{service} is the D-Bus service name of the D-Bus. It must be a
+known name. Emacs releases its association to @var{service} from
+D-Bus.
+
+One of the following keywords is returned:
+
+@itemize
+@item @code{:released}: We successfully released the name @var{service}.
+@item @code{:non-existent}: The name @var{service} does not exist on the bus.
+@item @code{:not-owner}: We are not an owner of the name @var{service}.
+@end itemize
+@end defun
+
+When a name has been chosen, Emacs can offer own methods, which can be
+called by other applications. These methods could be an
+implementation of an interface of a well known service, like
+@samp{org.freedesktop.TextEditor}.
It could be also an implementation of an own interface. In this case,
the service name must be @samp{org.gnu.Emacs}. The object path shall
@samp{@strong{Application}} is the name of the application which
provides the interface.
-@defun dbus-register-method bus service path interface method handler
+@deffn Constant dbus-service-emacs
+The well known service name of Emacs.
+@end deffn
+
+@deffn Constant dbus-path-emacs
+The object path head "/org/gnu/Emacs" used by Emacs. All object
+paths, used by offered methods or signals, shall start with this
+string.
+@end deffn
+
+@defun dbus-register-method bus service path interface method handler dont-register-service
With this function, an application registers @var{method} on the D-Bus
@var{bus}.
@code{:session}.
@var{service} is the D-Bus service name of the D-Bus object
-@var{method} is registered for. It must be a known name.
+@var{method} is registered for. It must be a known name (See
+discussion of @var{dont-register-service} below).
-@var{path} is the D-Bus object path @var{service} is
-registered.
+@var{path} is the D-Bus object path @var{service} is registered (See
+discussion of @var{dont-register-service} below).
@var{interface} is the interface offered by @var{service}. It must
provide @var{method}.
-@var{handler} is a Lisp function to be called when when a @var{method}
-call is is received. It must accept as arguments the input arguments
-of @var{method}. @var{handler} must return a list, which elements are
-used as arguments for the reply message of @var{method}. This list
-can be composed like the input parameters in @ref{Type Conversion}.
+@var{handler} is a Lisp function to be called when a @var{method} call
+is received. It must accept as arguments the input arguments of
+@var{method}. @var{handler} should return a list, whose elements are
+to be used as arguments for the reply message of @var{method}. This
+list can be composed like the input parameters in @ref{Type
+Conversion}.
+
+If @var{handler} wants to return just one Lisp object and it is not a
+cons cell, @var{handler} can return this object directly, instead of
+returning a list containing the object.
+
+In case @var{handler} shall return a reply message with an empty
+argument list, @var{handler} must return the symbol @code{:ignore}.
+
+When @var{dont-register-service} is non-@code{nil}, the known name
+@var{service} is not registered. This means that other D-Bus clients
+have no way of noticing the newly registered method. When interfaces
+are constructed incrementally by adding single methods or properties
+at a time, @var{dont-register-service} can be used to prevent other
+clients from discovering the still incomplete interface.
The default D-Bus timeout when waiting for a message reply is 25
seconds. This value could be even smaller, depending on the calling
"org.freedesktop.TextEditor" "OpenFile"
'my-dbus-method-handler)
-@result{} ((:system "org.freedesktop.TextEditor" "OpenFile")
+@result{} ((:session "org.freedesktop.TextEditor" "OpenFile")
("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
- my-method-handler))
+ my-dbus-method-handler))
@end lisp
If you invoke the method @samp{org.freedesktop.TextEditor.OpenFile}
from another D-Bus application with a filename as parameter, the file
is opened in Emacs, and the method returns either @var{true} or
-@var{false}, indicating the success if the method. As test tool one
+@var{false}, indicating the success of the method. As test tool one
could use the command line tool @code{dbus-send} in a shell:
@example
"org.freedesktop.TextEditor.OpenFile" string:"/etc/hosts"
@print{} method return sender=:1.22 -> dest=:1.23 reply_serial=2
- boolean true
+ boolean true
+@end example
+
+You can indicate an error by raising the Emacs signal
+@code{dbus-error}. The handler above could be changed like this:
+
+@lisp
+(defun my-dbus-method-handler (&rest args)
+ (unless (and (= (length args) 1) (stringp (car args)))
+ (signal 'dbus-error (list (format "Wrong argument list: %S" args))))
+ (condition-case err
+ (find-file (car args))
+ (error (signal 'dbus-error (cdr err))))
+ t)
+
+@result{} my-dbus-method-handler
+@end lisp
+
+The test runs then
+
+@example
+# dbus-send --session --print-reply \
+ --dest="org.freedesktop.TextEditor" \
+ "/org/freedesktop/TextEditor" \
+ "org.freedesktop.TextEditor.OpenFile" \
+ string:"/etc/hosts" string:"/etc/passwd"
+
+@print{} Error org.freedesktop.DBus.Error.Failed:
+ Wrong argument list: ("/etc/hosts" "/etc/passwd")
@end example
@end defun
+@defun dbus-register-property bus service path interface property access value &optional emits-signal dont-register-service
+With this function, an application declares a @var{property} on the D-Bus
+@var{bus}.
+
+@var{bus} is either the symbol @code{:system} or the symbol
+@code{:session}.
+
+@var{service} is the D-Bus service name of the D-Bus. It must be a
+known name.
+
+@var{path} is the D-Bus object path @var{service} is registered (See
+discussion of @var{dont-register-service} below).
+
+@var{interface} is the name of the interface used at @var{path},
+@var{property} is the name of the property of @var{interface}.
+
+@var{access} indicates, whether the property can be changed by other
+services via D-Bus. It must be either the symbol @code{:read} or
+@code{:readwrite}. @var{value} is the initial value of the property,
+it can be of any valid type (see @code{dbus-call-method} for details).
+
+If @var{property} already exists on @var{path}, it will be
+overwritten. For properties with access type @code{:read} this is the
+only way to change their values. Properties with access type
+@code{:readwrite} can be changed by @code{dbus-set-property}.
+
+The interface @samp{org.freedesktop.DBus.Properties} is added to
+@var{path}, including a default handler for the @samp{Get},
+@samp{GetAll} and @samp{Set} methods of this interface. When
+@var{emits-signal} is non-@code{nil}, the signal
+@samp{PropertiesChanged} is sent when the property is changed by
+@code{dbus-set-property}.
+
+When @var{dont-register-service} is non-@code{nil}, the known name
+@var{service} is not registered. This means that other D-Bus clients
+have no way of noticing the newly registered method. When interfaces
+are constructed incrementally by adding single methods or properties
+at a time, @var{dont-register-service} can be used to prevent other
+clients from discovering the still incomplete interface.
+
+@noindent Example:
+
+@lisp
+(dbus-register-property
+ :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
+ "org.freedesktop.TextEditor" "name" :read "GNU Emacs")
+
+@result{} ((:session "org.freedesktop.TextEditor" "name")
+ ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
+
+(dbus-register-property
+ :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
+ "org.freedesktop.TextEditor" "version" :readwrite emacs-version t)
+
+@result{} ((:session "org.freedesktop.TextEditor" "version")
+ ("org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"))
+@end lisp
+
+Other D-Bus applications can read the property via the default methods
+@samp{org.freedesktop.DBus.Properties.Get} and
+@samp{org.freedesktop.DBus.Properties.GetAll}. Testing is also
+possible via the command line tool @code{dbus-send} in a shell:
+
+@example
+# dbus-send --session --print-reply \
+ --dest="org.freedesktop.TextEditor" \
+ "/org/freedesktop/TextEditor" \
+ "org.freedesktop.DBus.Properties.GetAll" \
+ string:"org.freedesktop.TextEditor"
+
+@print{} method return sender=:1.22 -> dest=:1.23 reply_serial=3
+ array [
+ dict entry(
+ string "name"
+ variant string "GNU Emacs"
+ )
+ dict entry(
+ string "version"
+ variant string "23.1.50.5"
+ )
+ ]
+@end example
+
+It is also possible, to apply the @code{dbus-get-property},
+@code{dbus-get-all-properties} and @code{dbus-set-property} functions
+(@pxref{Properties and Annotations}).
+
+@lisp
+(dbus-set-property
+ :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
+ "org.freedesktop.TextEditor" "version" "23.1.50")
+
+@result{} "23.1.50"
+
+(dbus-get-property
+ :session "org.freedesktop.TextEditor" "/org/freedesktop/TextEditor"
+ "org.freedesktop.TextEditor" "version")
+
+@result{} "23.1.50"
+@end lisp
+@end defun
+
+@defun dbus-unregister-object object
+Unregister @var{object} from the D-Bus. @var{object} must be the
+result of a preceding @code{dbus-register-method},
+@code{dbus-register-property} or @code{dbus-register-signal} call
+(@pxref{Signals}). It returns @code{t} if @var{object} has been
+unregistered, @code{nil} otherwise.
+
+When @var{object} identifies the last method or property, which is
+registered for the respective service, Emacs releases its association
+to the service from D-Bus.
+@end defun
+
@node Signals
@chapter Sending and receiving signals.
@lisp
(dbus-send-signal
- :session "org.gnu.Emacs" "/org/gnu/Emacs"
- "org.gnu.Emacs.FileManager" "FileModified" "/home/albinus/.emacs")
+ :session dbus-service-emacs dbus-path-emacs
+ (concat dbus-service-emacs ".FileManager") "FileModified"
+ "/home/albinus/.emacs")
@end lisp
@end defun
which objects the GNU/Linux @code{hal} daemon adds.
@end defun
-@defun dbus-unregister-object object
-Unregister @var{object} from the the D-Bus. @var{object} must be the
-result of a preceding @code{dbus-register-signal} or
-@code{dbus-register-method} call. It returns @code{t} if @var{object}
-has been unregistered, @code{nil} otherwise.
+
+@node Alternative Buses
+@chapter Alternative buses.
+@cindex bus names
+@cindex UNIX domain socket
+
+Until now, we have spoken about the system and the session buses,
+which are the default buses to be connected to. However, it is
+possible to connect to any bus, from which the address is known. This
+is a UNIX domain socket. Everywhere, where a @var{bus} is mentioned
+as argument of a function (the symbol @code{:system} or the symbol
+@code{:session}), this address can be used instead. The connection to
+this bus must be initialized first.
+
+@defun dbus-init-bus bus
+Establish the connection to D-Bus @var{bus}.
+
+@var{bus} can be either the symbol @code{:system} or the symbol
+@code{:session}, or it can be a string denoting the address of the
+corresponding bus. For the system and session busses, this function
+is called when loading @file{dbus.el}, there is no need to call it
+again.
+
+Example: You open another session bus in a terminal window on your host:
+
+@example
+# eval `dbus-launch --auto-syntax`
+# echo $DBUS_SESSION_BUS_ADDRESS
+
+@print{} unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e
+@end example
+
+In Emacs, you can access to this bus via its address:
+
+@lisp
+(setq my-bus
+ "unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e")
+
+@result{} "unix:abstract=/tmp/dbus-JoFtAVG92w,guid=2f320a1ebe50b7ef58e"
+
+(dbus-init-bus my-bus)
+
+@result{} nil
+
+(dbus-get-unique-name my-bus)
+
+@result{} ":1.0"
+@end lisp
@end defun
@node Errors and Events
@chapter Errors and events.
+@cindex debugging
@cindex errors
@cindex events
-Input parameters of @code{dbus-call-method} and
+The internal actions can be traced by running in a debug mode.
+
+@defvar dbus-debug
+If this variable is non-@code{nil}, D-Bus specific debug messages are raised.
+@end defvar
+
+Input parameters of @code{dbus-call-method},
+@code{dbus-call-method-non-blocking},
+@code{dbus-call-method-asynchronously}, and
@code{dbus-register-signal} are checked for correct D-Bus types. If
there is a type mismatch, the Lisp error @code{wrong-type-argument}
@code{D-Bus ARG} is raised.
@defspec dbus-ignore-errors forms@dots{}
This executes @var{forms} exactly like a @code{progn}, except that
@code{dbus-error} errors are ignored during the @var{forms}. These
-errors can be made visible when variable @code{dbus-debug} is set to
-@code{t}.
+errors can be made visible when @code{dbus-debug} is set to @code{t}.
@end defspec
-Incoming D-Bus messages are handled as Emacs events (see @pxref{Misc
-Events, , , elisp}). The generated event has this form:
+Incoming D-Bus messages are handled as Emacs events, see @pxref{Misc
+Events, , , elisp}. They are retrieved only, when Emacs runs in
+interactive mode. The generated event has this form:
@lisp
-(dbus-event @var{bus} @var{serial} @var{service} @var{path} @var{interface} @var{member} @var{handler} &rest @var{args})
+(dbus-event @var{bus} @var{type} @var{serial} @var{service} @var{path} @var{interface} @var{member} @var{handler}
+ &rest @var{args})
@end lisp
-@var{bus} identifies the D-Bus the signal is coming from. It is
+@var{bus} identifies the D-Bus the message is coming from. It is
either the symbol @code{:system} or the symbol @code{:session}.
-@var{serial} is the serial number of the received D-Bus message if it
-is a method call, or @code{nil}.
+@var{type} is the D-Bus message type which has caused the event. It
+can be @code{dbus-message-type-invalid},
+@code{dbus-message-type-method-call},
+@code{dbus-message-type-method-return},
+@code{dbus-message-type-error}, or @code{dbus-message-type-signal}.
+@var{serial} is the serial number of the received D-Bus message.
@var{service} and @var{path} are the unique name and the object path
of the D-Bus object emitting the message. @var{interface} and
The result is either the symbol @code{:system} or the symbol @code{:session}.
@end defun
+@defun dbus-event-message-type event
+Returns the message type of the corresponding D-Bus message. The
+result is a natural number.
+@end defun
+
@defun dbus-event-serial-number event
Returns the serial number of the corresponding D-Bus message.
-The result is a number in case the D-Bus message is a method
-call, or @code{nil} for all other mesage types.
+The result is a natural number.
@end defun
@defun dbus-event-service-name event
@end defun
@defun dbus-event-interface-name event
-Returns the interface name of of the D-Bus object @var{event} is coming from.
+Returns the interface name of the D-Bus object @var{event} is coming from.
@end defun
@defun dbus-event-member-name event
-Returns the member name of of the D-Bus object @var{event} is coming
+Returns the member name of the D-Bus object @var{event} is coming
from. It is either a signal name or a method name.
@end defun
D-Bus errors are not propagated during event handling, because it is
usually not desired. D-Bus errors in events can be made visible by
-setting the variable @code{dbus-debug} to @code{t}.
+setting the variable @code{dbus-debug} to @code{t}. They can also be
+handled by a hook function.
+
+@defvar dbus-event-error-hooks
+This hook variable keeps a list of functions, which are called when a
+D-Bus error happens in the event handler. Every function must accept
+two arguments, the event and the error variable catched in
+@code{condition-case} by @code{dbus-error}.
+
+Such functions can be used the adapt the error signal to be raised.
+Example:
+
+@lisp
+(defun my-dbus-event-error-handler (event error)
+ (when (string-equal (concat dbus-service-emacs ".FileManager")
+ (dbus-event-interface-name event))
+ (message "my-dbus-event-error-handler: %S %S" event error)
+ (signal 'file-error (cdr error))))
+
+(add-hook 'dbus-event-error-hooks 'my-dbus-event-error-handler)
+@end lisp
+@end defvar
+
+Hook functions shall take into account, that there might be other
+D-Bus applications running. Therefore, they shall check carefully,
+whether a given D-Bus error is related to them.
+
+
+@node Index
+@unnumbered Index
+
+@printindex cp
@node GNU Free Documentation License
@appendix GNU Free Documentation License
@include doclicense.texi
-@contents
-@c End of dbus.texi
@bye
-
-@ignore
- arch-tag: 2eeec19d-0caf-44e0-a193-329d7f9951d8
-@end ignore