DBus and Gambas
The DBus 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
gb.dbus 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.
Note that the gb.dbus component has a part written in C and a part written in
Gambas. The part written in C provide hidden classes (whose name begins with
"_") that the part written in Gambas uses to provided the full (and easy!)
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, do:
And to set the value of a property, do:
DBus[Application][ObjectPath].Property = Value
Application is the string that identifies the application on the bus.
It can begin with
"session://" 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 that way:
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 to DBus, 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 bus.
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.
As soon as at least one object is registered, your application will be visible on the bus.
The name of your application on the bus is defined by the DBus.Name property. By default, it is defined as
<xxx> is the value of Application.Name.
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
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
An object 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
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
DBus.Session.Register(MyMediaPlayer, "/org/mpris/MediaPlayer2", ["org.mpris.MediaPlayer2", "org.mrpris.MediaPlayer2.Player"])
You can raise D-Bus signals since Gambas 3.9 only.
To let an object raise signals, you must declare them first as Gambas events.
The name of the event must be the normalized name of the interface, followed by an underscore and the normalized name of the signal.
The signal is emitted to the D-Bus bus by using the DBusApplication.Raise method.
For example, in gb.dbus.trayicon component, the internal DBusStatusIcon class is a DBusObject implementing the
that can raise the "NewIcon" signal, among many others.
It is implemented that way:
The "NewIcon" signal must be raised when the status icon image changes. It is raised in the function that implements the
Private Sub Picture_Write(Value As Picture)