Skip to content

Commit 684bedc

Browse files
committed
doc: Basic Concepts: xref'd, added signals+async.
1 parent c846f6d commit 684bedc

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

doc/Reference.md

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ The following code is assumed as a prolog to all following ones
2020

2121
#### Calling Methods
2222

23-
1. Connect to the session bus; get the screensaver service and its
24-
screensaver object.
25-
2. Perform explicit introspection to define the interfaces and methods
26-
on the {DBus::ProxyObject object proxy}
27-
({https://github.com/mvidner/ruby-dbus/issues/28 I#28}).
28-
3. Get the screensaver interface
29-
({https://github.com/mvidner/ruby-dbus/issues/29 I#29}).
23+
1. {DBus.session_bus Connect to the session bus};
24+
{DBus::Connection#[] get the screensaver service}
25+
{DBus::Service#object and its screensaver object}.
26+
2. Perform {DBus::ProxyObject#introspect explicit introspection}
27+
to define the interfaces and methods
28+
on the {DBus::ProxyObject object proxy}
29+
[I#28](https://github.com/mvidner/ruby-dbus/issues/28).
30+
3. Get the screensaver {DBus::ProxyObject#[] interface}
31+
[I#29](https://github.com/mvidner/ruby-dbus/issues/29).
3032
4. Call one of its methods in a loop, solving [xkcd#196](http://xkcd.com/196).
3133

3234
{include:file:doc/ex-calling-methods.body.rb}
@@ -37,7 +39,7 @@ A method proxy always returns an array of values. This is to
3739
accomodate the rare cases of a DBus method specifying more than one
3840
*out* parameter. For nearly all methods you should use `Method[0]` or
3941
`Method.first`
40-
({https://github.com/mvidner/ruby-dbus/issues/30 I#30}).
42+
[I#30](https://github.com/mvidner/ruby-dbus/issues/30).
4143

4244

4345
# wrong
@@ -52,14 +54,41 @@ accomodate the rare cases of a DBus method specifying more than one
5254

5355
#### Accessing Properties
5456

55-
{include:file:doc/ex-properties.body.rb}
57+
To access properties, think of the {DBus::ProxyObjectInterface interface} as a
58+
{DBus::ProxyObjectInterface#[] hash} keyed by strings,
59+
or use {DBus::ProxyObjectInterface#all_properties} to get
60+
an actual Hash of them.
5661

57-
#### Receiving Signals
62+
{include:file:doc/ex-properties.body.rb}
5863

64+
(TODO a writable property example)
5965

6066
#### Asynchronous Operation
67+
68+
If a method call has a block attached, it is asynchronous and the block
69+
is invoked on receiving a method_return message or an error message
70+
6171
##### Main Loop
6272

73+
For asynchronous operation an event loop is necessary. Use {DBus::Main}:
74+
75+
# [set up signal handlers...]
76+
main = DBus::Main.new
77+
main << mybus
78+
main.run
79+
80+
Alternately, run the GLib main loop and add your DBus connections to it via
81+
{DBus::Connection#glibize}.
82+
83+
#### Receiving Signals
84+
85+
To receive signals for a specific object and interface, use
86+
{DBus::ProxyObjectInterface#on\_signal}(bus, name, &block) or
87+
{DBus::ProxyObject#on_signal}(name, &block), for the default interface.
88+
[I#31](https://github.com/mvidner/ruby-dbus/issues/31)
89+
90+
{include:file:doc/ex-signal.body.rb}
91+
6392
### Intermediate Concepts
6493
#### Names
6594
#### Types

doc/ex-signal.body.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sysbus = DBus.system_bus
2+
login_s = sysbus['org.freedesktop.login1'] # part of systemd
3+
login_o = login_s.object '/org/freedesktop/login1'
4+
login_o.introspect
5+
login_o.default_iface = 'org.freedesktop.login1.Manager'
6+
7+
# to trigger this signal, login on the Linux console
8+
login_o.on_signal("SessionNew") do |name, opath|
9+
puts "New session: #{name}"
10+
11+
session_o = login_s.object(opath)
12+
session_o.introspect
13+
session_i = session_o['org.freedesktop.login1.Session']
14+
uid, user_opath = session_i['User']
15+
puts "Its UID: #{uid}"
16+
end
17+
18+
main = DBus::Main.new
19+
main << sysbus
20+
main.run

doc/ex-signal.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#! /usr/bin/env ruby
2+
require 'example-helper.rb'
3+
example 'ex-signal.body.rb'

lib/dbus/bus.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,12 +797,14 @@ class SystemBus < ASystemBus
797797
include Singleton
798798
end
799799

800-
# Shortcut for the SystemBus instance
800+
# Shortcut for the {SystemBus} instance
801+
# @return [Connection]
801802
def DBus.system_bus
802803
SystemBus.instance
803804
end
804805

805-
# Shortcut for the SessionBus instance
806+
# Shortcut for the {SessionBus} instance
807+
# @return [Connection]
806808
def DBus.session_bus
807809
SessionBus.instance
808810
end

lib/dbus/introspect.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def []=(propname, value)
339339
end
340340

341341
# Read all properties at once, as a hash.
342+
# @return [Hash{String}]
342343
def all_properties
343344
self.object[PROPERTY_INTERFACE].GetAll(self.name)[0]
344345
end
@@ -377,12 +378,16 @@ def interfaces
377378
@interfaces.keys
378379
end
379380

380-
# Retrieves an interface of the proxy object (ProxyObjectInterface instance).
381+
# Retrieves an interface of the proxy object
382+
# @return [ProxyObjectInterface]
381383
def [](intfname)
382384
@interfaces[intfname]
383385
end
384386

385387
# Maps the given interface name _intfname_ to the given interface _intf.
388+
# @param [String] intfname
389+
# @param [ProxyObjectInterface] intf
390+
# @return [ProxyObjectInterface]
386391
def []=(intfname, intf)
387392
@interfaces[intfname] = intf
388393
end

0 commit comments

Comments
 (0)