User Tools

Site Tools


Network and communication

k24:k24.9:k24.9.6:k24.9.6.2:start Projects - Process, send and receive D-Bus signal

In contrast to the first two projects, in which signals selected by a Gambas client from other d-bus-compatible applications were only intercepted on the D-Bus and some of the content in the variant array argument was also evaluated, two projects are presented here with which you can send a self-defined signal (server) or intercept and evaluate (client) on the one hand:

Figure Signal transmitter Project - Signal transmitter

The source code for a project with which D-Bus signals are also to be transmitted always includes the preparation of the signal to be transmitted in a special class file - whose name you can freely define - and the actual signal transmitter in the start class, for example in the FMain.class.

Signal definition

The signal to be transmitted is processed in the class file SGBVersion.class. The prefix S stands for signal definition and GBVersion for the signal name, which is also called member. You can define the signal name yourself:

[1] ' Gambas class file
[3] Inherits DBusObject
[4] Create Static
[6] ' (Pseudo-) signature of the list of arguments: "s" => String
[8] Event org_gambas_SignalTX_SGBVersion_GBVersion(Version As String)

The signal to be transmitted can be processed quickly. The class SGBVersion.class inherits in line 3 from the class DBusObject. Therefore, each instance of SGBVersion.class is of the data type DBusObject. The signal is defined in line 8. A D-Bus signal is defined as an event in Gambas. For the syntax of the signals to be sent, refer to chapter Sending Signals. Since the signature of the signal is “s” - only one string is sent - the list of arguments consists of only one element: Version with the data type String. The native Gambas data type'String' is automatically converted to the appropriate D-Bus data type during transmission.

Signal emitter

Before you can send a signal, you must export the DBus object in which the signal was defined (SGBVersion.class) with the method DBus.Raise(…) or DBus[AppName].Raise(…) to the Session D-Bus, so that d-bus-enabled programs can observe the defined signals.

Source text sender:

[1] ' Gambas class file
[3] Private $sDBusName As String
[4] Private $hDBusObject As SGBVersion
[5] Private $sDBusObjectPath As String
[7] Public Sub Form_Open()
[9]   FMain.Resizable = False
[10]   DBus.Unique = True
[11] ' For tests only: DBus.Debug = True
[13]   $hDBusObject = New SGBVersion
[14]   $sDBusObjectPath = "/SGBVersion"
[16]   DBus.Session.Register($hDBusObject, $sDBusObjectPath)
[18] End
[20] Public Sub btnSendSignal_Click()
[22]   lblCaption.Foreground = Color.Red
[23]   SendSignal()
[24]   Wait 0.1
[25]   lblCaption.Foreground = Color.Black
[27] End
[29] Private Sub SendSignal()
[31]   Dim aArguments As New Variant[]
[33]   aArguments.Add(System.FullVersion)
[35]   DBus.Raise($hDBusObject, "org.gambas.SignalTX.SGBVersion.GBVersion", aArguments)
[36] ' $sDBusName = DBus.Name
[37] ' DBus[$sDBusName].Raise($hDBusObject, "org.gambas.SignalTX.SGBVersion.GBVersion", aArguments)
[39] End
[41] Public Sub Form_Close()
[43]   If DBus.IsRegistered($hDBusObject) Then DBus.Session.Unregister($hDBusObject)
[44]   FMain.Close()
[46] End


  • In line 16 the D-Bus object with the D-Bus object path '/SGBVersion' is exported to the D-Bus.
  • In lines 22 and 25, the color in the display changes only briefly to indicate that the signal is being sent.
  • The content of the signal is a string that contains the current Gambas version with System.fullVersion (line 33).
  • The transmission of the signal is pushed in line 35 or alternatively in line 37.
  • Since a D-Bus object with the D-Bus object path '/SGBVersion' was exported to the D-Bus, it is logged off again in line 43 before the program is closed. Project - Client

The client SignalRX observes the signal with the name'GBVersion' and the D-Bus object path '/SGBVersion' of the application with the D-Bus name org.gambas.SignalTX on the Session-D-Bus. When the signal arrives, the content - the current Gambas version on the system - is read out and displayed in a text area together with the preceding date and the current time:


Figure Signal receiver

The complete source code for the client is specified and then commented:

[1] ' Gambas class file
[3] Private $hDBusSignal As DBusSignal
[4] Private $sDBusInterface As String
[5] Private $sDBusSignalName As String
[7] Public Sub Form_Open()
[9]   Dim sMessage As String
[11]   If Not DBus.Session.Applications.Exist("org.gambas.SignalTX") Then
[12]      sMessage = ("There is no suitable data server on the session bus!")
[13]      sMessage &= "<center><font color='red'>"
[14]      sMessage &= ("The program is terminated.")
[15]      sMessage &= "</font></center>"
[16]      Message.Warning(sMessage)
[17]      Quit
[18]   Endif
[20]   FMain.Resizable = False
[21]   lblCaption.Text = ("WATCHING SPECIAL SIGNAL")
[22] ' For tests only: DBus.Debug = True
[24]   $sDBusInterface = "org.gambas.SignalTX.SGBVersion"
[25]   $sDBusSignalName = "GBVersion" ' The signal name is called 'Member'
[27]   $hDBusSignal = New DBusSignal(DBus.Session, $sDBusInterface, True) As "ObservedSignal"
[29] End
[31] Public Sub ObservedSignal_Signal(Signal As String, Arguments As Variant[])
[33]   If Signal = $sDBusSignalName Then
[34]      TextArea1.Insert("Zeit: " & Format(Now(), " hh:nn:ss") & "    Signal-Name:  " & Signal & " -> " & Arguments[0] & gb.NewLine)
[35]      TextArea1.Pos = TextArea1.Length
[36]   Endif
[38] End
[40] Public Sub Form_Close()
[42]   If $hDBusSignal Then $hDBusSignal.Enabled = False
[43]   FMain.Close()
[45] End


  • Line 11 checks whether the (server) program exists. However, this type of check only works if the property DBus.Unique has been set to True in the source code of the server. In chapter you can read which alternative exists for the standard DBus.Unique = False.


Figure Wrong start…

  • A new signal observer with the event name 'ObservedSignal' is created in line 27.
  • How to react when the observed signal arrives and thus triggers the event'Signal' is defined in the event handling routine in lines 31 to 38.
  • The signal observer is switched off in line 42 before the client program is terminated.


The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.9/k24.9.6/k24.9.6.2/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools