DBus and Gambas
Introduction
The
DBus system is (almost) fully supported in
Gambas 3, by the
gb.dbus
componente.
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
connected user.
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 objeto 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!)
component interface.
|
Using objects exported by other applications
Calling methods or properties
To call a
method, use the following syntax:
DBus[Application][ObjectPath].Method(Arguments)
To get the value of a
property, do:
DBus[Application][ObjectPath].Property
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
"system://" or
"session://" to define which bus
you want to connect with.
By default you connect with the session bus.
Exemplo
DBus["org.kde.kmail"]["/kmail/kmail_mainwindow_1"].geometry = [0, 24, 1024, 768]
Catching signals
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
objeto 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.
Exemplo
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
...
EndIf
End
Exporting Gambas object to other applications
Registering the object
To export a
Gambas objeto 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.
Exemplo
Dim hObject As MyClass ' MyClass inherits DBusObject
DBus.Session.Register(hObject, "/object/path")
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 org.gambas.
where 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
objeto, 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
program.
Implementing a specific interface
An
objeto 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..
interface.
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.
Exemplo
' "Open" method of the org.mpris.MediaPlayer2 interface
Public Sub org_mpris_MediaPlayer2_Open()
...
End
' "Play" method of the org.mpris.MediaPlayer2.Player interface
Public Sub org_mpris_MediaPlayer2_Player_Play()
...
End
' "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"])
Raising signals
 |
You cannot raise DBus signals. This is not yet implemented!
|