dbus.texi 71 KB

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