Class: DBus::Bus

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus.rb

Overview

Represents a connection to a DBus daemon

Direct Known Subclasses

ActivationBus, SessionBus, SystemBus

Constant Summary collapse

TYPE_SESSION =
BUS_SESSION
TYPE_SYSTEM =
BUS_SYSTEM
TYPE_ACTIVATION =
BUS_ACTIVATION

Instance Method Summary collapse

Constructor Details

#initialize(bus_type = TYPE_SESSION, glib_mainloop = true) ⇒ Bus

Returns a new instance of Bus.


21
22
23
24
25
26
# File 'lib/dbus.rb', line 21

def initialize(bus_type=TYPE_SESSION, glib_mainloop=true)
  @connection = DBus::Binding::bus_get(bus_type)
  @connection.add_filter(method(:signal_dispatcher))
  @rules = {}
  @connection.setup_with_g_main if glib_mainloop
end

Instance Method Details

#activate_service(service_name, flags = 0) ⇒ Object

Activates the given service on this bus


91
92
93
# File 'lib/dbus.rb', line 91

def activate_service(service_name, flags=0)
  DBus::Binding::bus_activate_service(@connection, service_name, flags)
end

#add_signal_receiver(handler_proc, signal_name = nil, interface = nil, service = nil, path = nil) ⇒ Object

Add a handler handler_proc for the match rule resulting from passing the parameters to Bus#build_signal_rule. Multiple handlers can be installed for the same signal.


39
40
41
42
43
44
# File 'lib/dbus.rb', line 39

def add_signal_receiver(handler_proc, signal_name=nil, interface=nil, service=nil, path=nil)
  rule = build_signal_rule(signal_name, interface, service, path)
  @rules[rule] ||= []
  @rules[rule] << handler_proc unless @rules[rule].include?(handler_proc)
  DBus::Binding::bus_add_match(@connection, rule)
end

#build_signal_rule(signal_name, interface, service, path) ⇒ Object

Generate a match rule for a signal with the specified arguments. If any of the method arguments is nil, its processing (below) is skipped.

  • The rule “type” is set to “signal”

  • The rule “interface” is set to the value of the interface argument

  • The rule “sender” is set as follows: If the given service argument does not start with “:”, and is not “org.freedesktop.DBus”, the “sender” value is set to the owner of the supplied service argument (determined with a GetServiceOwner call), otherwise, the service value is used

  • The “path” parameter is set to the value of the path argument

  • The “member” parameter is set to the value of the signal_name argument


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dbus.rb', line 67

def build_signal_rule(signal_name, interface, service, path)
  rule = "type='signal'"
  if interface
    rule += ",interface='%s'" % interface
  end
  if service
    if service[0,1] != ":" && service != "org.freedesktop.DBus"
      bus_service = get_service("org.freedesktop.DBus")
      bus_object = bus_service.get_object("/org/freedesktop/DBus",
                                          "org.freedesktop.DBus")
      service = bus_object.GetServiceOwner(service)
    end
    rule += ",sender='%s'" % service
  end
  if path
    rule += ",path='%s'" % path
  end
  if signal_name
    rule += ",member='%s'" % signal_name
  end
  rule
end

#get_base_serviceObject

Returns the base service name of this bus


101
102
103
# File 'lib/dbus.rb', line 101

def get_base_service
  DBus::Binding::bus_get_base_service(@connection)
end

#get_connectionObject


28
29
30
# File 'lib/dbus.rb', line 28

def get_connection
  @connection
end

#get_service(name = "org.freedesktop.Broadcast") ⇒ Object


32
33
34
# File 'lib/dbus.rb', line 32

def get_service(name="org.freedesktop.Broadcast")
  RemoteService.new(self, name)
end

#get_unix_user(service_name) ⇒ Object

Returns the UNIX user ID for the given service


96
97
98
# File 'lib/dbus.rb', line 96

def get_unix_user(service_name)
  DBus::Binding::bus_get_unix_user(@connection, service_name)
end

#remove_signal_receiver(handler_proc, signal_name = nil, interface = nil, service = nil, path = nil) ⇒ Object

Remove the handler handler_proc for a match rule previously added using Bus#add_signal_receiver.


48
49
50
51
52
53
# File 'lib/dbus.rb', line 48

def remove_signal_receiver(handler_proc, signal_name=nil, interface=nil, service=nil, path=nil)
  rule = build_signal_rule(signal_name, interface, service, path)
  return nil unless @rules.has_key?(rule)
  @rules[rule].remove(handler_proc)
  DBus::Binding::bus_remove_match(@conn, rule)
end

#set_base_service(service_name) ⇒ Object

Sets the base service name of this bus


106
107
108
# File 'lib/dbus.rb', line 106

def set_base_service(service_name)
  DBus::Binding::bus_set_base_service(@connection, service_name)
end