Any simple DBus examples?

Post

Posted
Rating:
#1 (In Topic #698)
Guru
BruceSteers is in the usergroup ‘Guru’
 Has anyone done any simple DBus stuff?

All I want is to see how to make my program set itself up for reading a command sent though dbus.

I'm basically rewriting my SingleIntsance routine that handles an app being launched multiple times.

So if the app is launched twice the first instance will register itself on the DBus and the second instance will send it's args to the first instance.  Currently i'm using a pipe but DBus i think would be better.

I could only find one DBus app on the farm but all it does is list all the commands on the bus.

TIA
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
k24:k24.9:start [GAMBAS BOOK 3.19.5]


    24.9.0 D-Bus
        24.9.0.1 D-Bus and Gambas
        24.9.0.2 D-Bus-Introspection
        24.9.0.3 D-Bus-Signatur
        24.9.1 DBusObject
        24.9.2 DBus
            24.9.2.1 Project
        24.9.3 DBusApplication
        24.9.4 DBusConnection
        24.9.5 DBusObserver
            24.5.2.1 Project
        24.9.6 DBusSignal
            24.9.6.1 Project 1
            24.9.6.2 Project 2
            24.9.6.3 Project 3
        24.9.7 DBusProxy
        24.9.8 DBusVariant
            24.9.8.1 Project 1
            24.9.8.2 Project 2
        24.9.9 D-Bus-Exkurs
Online now: No Back to the top

Post

Posted
Rating:
#3
Guru
BruceSteers is in the usergroup ‘Guru’
Cheers Michael :)
I was given this example so i'm on track again, got working communication happening now :)

————————fwd————
I have the following code in a startup module.  It could go in a startup form as well.

Code (gambas)

  1.  
  2. Private hDbusObj As DbusObj
  3.  
  4. Const TEXTEDITOR_DBUS_NAME As String = "org.ajmconsulting.TextEditor"
  5. Const TEXTEDITOR_PATH As String = "/org/ajmconsulting/TextEditor"
  6. Const TEXTEDITOR_DBUS_INTERFACE As String = "org.ajmconsulting.TextEditor"
  7.  
  8. Public Sub Main()    'Form_Open if startup form
  9.     'check if gbTE is already open
  10.     If DBus.Session.Applications.Exist(TEXTEDITOR_DBUS_NAME) Then
  11.       'yes it is, so pass-off current file list
  12.       If Files.Count > 0 Then
  13.         For Each $file In Files
  14.           DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE].Open($file.Name, $file.ReadOnly, $file.LineNumber, $file.ColumnNumber)
  15.         Next
  16.       Endif
  17.       Try DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE].Show()
  18.       Return    'exit, replace with Me.Close if in startup form
  19.     Endif
  20.     'set up to receive pass-off commands
  21.     DBus.Name = TEXTEDITOR_DBUS_NAME
  22.     hDbusObj = New DbusObj
  23.     DBus.Session.Register(hDbusObj, TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE])
  24.  
  25.   fEditor.ShowModal()  'wait until editing is all done, skip if in startup form
  26.  
  27.   'clean-up and exit, place remaining code in Form_Close if in startup form
  28.   If Not IsNull(hDbusObj) And If DBus.IsRegistered(hDbusObj) Then
  29.     DBus.Unregister(hDbusObj)
  30.  
  31.  

The remaining code goes into a class file named dbusobj.

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Inherits DBusObject
  4.  
  5. Public Sub org_ajmconsulting_TextEditor_Show()
  6.  
  7.   fEditor.Show()
  8.  
  9.  
  10. Public Sub org_ajmconsulting_TextEditor_Open(sFilePath As String, bReadOnly As Boolean, nLine As Integer, nColumn As Integer)
  11.  
  12.   fEditor.AddEditor(sFilePath, bReadOnly, nLine, nColumn)
  13.   fEditor.Raise()
  14.  
  15.  
  16.  
  17.  
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
;)
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
So this is what i managed to make..

In the source folder of the attached app is a SingleInstance folder containing the SingleInstance class.
(uses gb.dbus)

A desktop launcher can be made for this app and then multiple files can be dropped onto it and the app will only launch one instance with the other instances just adding their args to the first.

how it works…
it attempts to create a unique DBus interface. if successful it prepares to receive any messages.
If one already exists it sends the interface the args so only one application opens with all args.

here is how to use the SingleInstance.class (from FMain.class)

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.   hSing = New SingleInstance As "Sing"
  4.   If hSing.Initialise() Then
  5.     Me.Close
  6.     Return
  7.  
  8.   Wait 0.5  ' we do not want to be finished before other args are sent so wait half a second.
  9.  
  10.   While hSing.ArgList.Count
  11.     ListBox1.Add(hSing.PullArg())
  12.   Wend
  13.  
  14.  

.Initialise returns true if app is not 1st instance and so quits.
then we use hSing.Pull() to get the first arg and remove it from the list until the list has gone.
While application is open any other files dropped on the launcher trigger the ArgAdded event and get added to the list.

with this app you can drop five files on a launcher and rather than the application opening 5 times (as is usual behaviour) only one app opens with all 5 paths as arguments

(ps. the app does not do anything it's just a test app for the class)
Maybe someone else may find this useful.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’
would using a PID file be a possible solution?
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’

PJBlack said

would using a PID file be a possible solution?

A solution to what?

I've previously used pgrep and shell commands and pipes/sockets

I think DBus is a better way
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’

BruceSteers said

A solution to what?

I thought it was about preventing the application from starting multiple times …

sorry if I missed something
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’

PJBlack said

BruceSteers said

A solution to what?

I thought it was about preventing the application from starting multiple times …

sorry if I missed something

aah i see , no i've already been there..
Gambas One - Gambas ONE

using pgrep is better than PID files but Dbus is even better as it handles the messages too (no need for pipe or socket)
Online now: No Back to the top
1 guest and 0 members have just viewed this.