doc/Reference.md in ruby-dbus-0.11.0 vs doc/Reference.md in ruby-dbus-0.11.1
- old
+ new
@@ -12,13 +12,15 @@
### Basic Concepts
#### Setting Up
-The following code is assumed as a prolog to all following ones
+Note that although the gem is named "ruby-dbus", the required name
+is simply "dbus"
-{include:file:doc/ex-setup.rb}
+ #! /usr/bin/env ruby
+ require "dbus"
#### Calling Methods
1. {DBus.session_bus Connect to the session bus};
{DBus::Connection#[] get the screensaver service}
@@ -27,20 +29,27 @@
to define the interfaces and methods
on the {DBus::ProxyObject object proxy}
([I#28](https://github.com/mvidner/ruby-dbus/issues/28)).
3. Call one of its methods in a loop, solving [xkcd#196](http://xkcd.com/196).
-{include:file:doc/ex-calling-methods.body.rb}
+
+ mybus = DBus.session_bus
+ service = mybus['org.freedesktop.ScreenSaver']
+ object = service.object '/ScreenSaver'
+ object.introspect
+ loop do
+ object.SimulateUserActivity
+ sleep 5 * 60
+ end
##### Retrieving Return Values
A method proxy always returns an array of values. This is to
accomodate the rare cases of a DBus method specifying more than one
*out* parameter. For nearly all methods you should use `Method[0]` or
`Method.first`
([I#30](https://github.com/mvidner/ruby-dbus/issues/30)).
-
# wrong
if upower_i.SuspendAllowed # [false] is true!
upower_i.Suspend
end
@@ -55,12 +64,20 @@
To access properties, think of the {DBus::ProxyObjectInterface interface} as a
{DBus::ProxyObjectInterface#[] hash} keyed by strings,
or use {DBus::ProxyObjectInterface#all_properties} to get
an actual Hash of them.
-{include:file:doc/ex-properties.body.rb}
+ sysbus = DBus.system_bus
+ upower_s = sysbus['org.freedesktop.UPower']
+ upower_o = upower_s.object '/org/freedesktop/UPower'
+ upower_o.introspect
+ upower_i = upower_o['org.freedesktop.UPower']
+ on_battery = upower_i['OnBattery']
+
+ puts "Is the computer on battery now? #{on_battery}"
+
(TODO a writable property example)
Note that unlike for methods where the interface is inferred if unambiguous,
for properties the interface must be explicitly chosen.
That is because {DBus::ProxyObject} uses the {DBus::ProxyObject Hash#[]} API
@@ -87,10 +104,29 @@
To receive signals for a specific object and interface, use
{DBus::ProxyObjectInterface#on\_signal}(name, &block) or
{DBus::ProxyObject#on_signal}(name, &block), for the default interface.
-{include:file:doc/ex-signal.body.rb}
+ sysbus = DBus.system_bus
+ login_s = sysbus['org.freedesktop.login1'] # part of systemd
+ login_o = login_s.object '/org/freedesktop/login1'
+ login_o.introspect
+ login_o.default_iface = 'org.freedesktop.login1.Manager'
+
+ # to trigger this signal, login on the Linux console
+ login_o.on_signal("SessionNew") do |name, opath|
+ puts "New session: #{name}"
+
+ session_o = login_s.object(opath)
+ session_o.introspect
+ session_i = session_o['org.freedesktop.login1.Session']
+ uid, user_opath = session_i['User']
+ puts "Its UID: #{uid}"
+ end
+
+ main = DBus::Main.new
+ main << sysbus
+ main.run
### Intermediate Concepts
#### Names
#### Types and Values, D-Bus -> Ruby