DBus and Gambas
system is (almost) fully supported in Gambas
3, by the gb.dbus
What is DBus ? It is a "message bus system", i.e. a little program that can
pass different kinds of messages between applications. It's like the old KDE 3
DCOP system. For more information, see http://dbus.freedesktop.org
There are normally two kind of bus running on your machine: the "system bus",
that is global and unique, and the "session bus", that is launched for each
allows you to:
- Call any method or property exported by any application that is connected with the bus.
- Catch any signal raised by any interface.
- Export any Gambas object to DBus.
Using objects exported by other applications
Calling methods or properties
To call a method
, use the following syntax:
To get the value of a property
And to set the value of a property, do:
DBus[Application][ObjectPath].Property = Value
is the string that identifies the application on the bus.
It can begin with "system://"
to define which bus
you want to connect with.
By default you connect with the session bus.
DBus["org.kde.kmail"]["/kmail/kmail_mainwindow_1"].geometry = [0, 24, 1024, 768]
Catching signals relies on two classes: DBusObserver
, and DBusSignal
DBusObserver is a low-level class
that you should not use unless you have very specific needs.
Instead, use the DBusSignal class that is built upon the DBusObserver class.
To watch a signal, just create a new DBusSignal object
hSignal = New DBusSignal(Bus, Interface, Every) As "MySignal"
- Bus is the bus you are connecting with.
- Interface is the name of the interface that raises the signal.
- Every is an optional boolean flag that allows you to catch signals sent to all applications, not just yours.
MySignal = New DBusSignal(DBus.System, "org.freedesktop.Hal.Manager") As "DBusSignal"
Public Sub DBusSignal_Signal(Signal As String, Arguments As Variant)
If Signal = "DeviceAdded" Then
Exporting Gambas object to other applications
Registering the object
To export a Gambas object
, this object must inherits the DBusObject class
Then you have to register it to the bus with the DBus.Session.Register
or DBus.System.Register method
Use the first method to register your object on the Session
bus, the second one to register it on the System
Dim hObject As MyClass ' MyClass inherits DBusObject
The Gambas object is attached to an "object path", because all objects exposed
to the DBus by your application is like a hierarchical file system.
What is exported?
- Public methods whose name has no underscore inside, and whose arguments and return type can be converted to a DBus datatype.
- Public properties whose name has no underscore inside, and whose type can be converted to a DBus datatype.
As soon as you register at least one object
, your application appears on the
bus with the name "org.gambas.<application name>"
All these methods and properties are exported with an interface name named
"org.gambas.<application name>.<class name>"
You can check that with the DBus explorer example, or the qdbusviewer Qt4
Implementing a specific interface
can implement specific DBus
interfaces by using the third optional argument of the Register method
That third argument is a string array of interface names.
If this argument is not specified, then, as before, all public methods
and properties are put under the org.gambas..
But if this argument is specified, then all public methods and properties
whose name begins with the interface name are put under this interface.
The interface name is normalized in the method name, by replacing points by underscores,
and by adding another underscore between the interface name and the method name.
' "Open" method of the org.mpris.MediaPlayer2 interface
Public Sub org_mpris_MediaPlayer2_Open()
' "Play" method of the org.mpris.MediaPlayer2.Player interface
Public Sub org_mpris_MediaPlayer2_Player_Play()
' "Enabled" property of the org.mpris.MediaPlayer2 interface
Property org_mpris_MediaPlayer2_Enabled As Boolean