Any simple DBus examples?

Ask about the individual Gambas components here.
Post Reply
User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Any simple DBus examples?

Post by BruceSteers » Wednesday 07th July 2021 9:35am

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
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
PJBlack
Posts: 159
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Any simple DBus examples?

Post by PJBlack » Wednesday 07th July 2021 11:07am

https://gambas-buch.de/dwen/doku.php?id=k24:k24.9:start


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

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: Any simple DBus examples?

Post by BruceSteers » Wednesday 07th July 2021 2:20pm

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.

Private hDbusObj As DbusObj

Const TEXTEDITOR_DBUS_NAME As String = "org.ajmconsulting.TextEditor"
Const TEXTEDITOR_PATH As String = "/org/ajmconsulting/TextEditor"
Const TEXTEDITOR_DBUS_INTERFACE As String = "org.ajmconsulting.TextEditor"

Public Sub Main()    'Form_Open if startup form
    'check if gbTE is already open
    If DBus.Session.Applications.Exist(TEXTEDITOR_DBUS_NAME) Then
      'yes it is, so pass-off current file list
      If Files.Count > 0 Then
        For Each $file In Files
          DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE].Open($file.Name, $file.ReadOnly, $file.LineNumber, $file.ColumnNumber)
        Next
      Endif
      Try DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE].Show()
      Return    'exit, replace with Me.Close if in startup form
    Endif
    'set up to receive pass-off commands
    DBus.Name = TEXTEDITOR_DBUS_NAME
    hDbusObj = New DbusObj
    DBus.Session.Register(hDbusObj, TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE])
  Endif

  fEditor.ShowModal()  'wait until editing is all done, skip if in startup form

  'clean-up and exit, place remaining code in Form_Close if in startup form
  If Not IsNull(hDbusObj) And If DBus.IsRegistered(hDbusObj) Then
    DBus.Unregister(hDbusObj)
  Endif

End
The remaining code goes into a class file named dbusobj.
' Gambas class file

Inherits DBusObject

Public Sub org_ajmconsulting_TextEditor_Show()

  fEditor.Show()

End

Public Sub org_ajmconsulting_TextEditor_Open(sFilePath As String, bReadOnly As Boolean, nLine As Integer, nColumn As Integer)

  fEditor.AddEditor(sFilePath, bReadOnly, nLine, nColumn)
  fEditor.Raise()

End


Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
PJBlack
Posts: 159
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Any simple DBus examples?

Post by PJBlack » Wednesday 07th July 2021 2:42pm

;)

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: Any simple DBus examples?

Post by BruceSteers » Thursday 08th July 2021 9:15am

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)
Public Sub Form_Open()

  hSing = New SingleInstance As "Sing"
  If hSing.Initialise() Then
    Me.Close
    Return
  Endif

  Wait 0.5  ' we do not want to be finished before other args are sent so wait half a second.

  While hSing.ArgList.Count
    ListBox1.Add(hSing.PullArg())
  Wend

End
.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.
Attachments
SingleBus.zip
(24.96 KiB) Downloaded 27 times
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
PJBlack
Posts: 159
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Any simple DBus examples?

Post by PJBlack » Thursday 08th July 2021 10:38am

would using a PID file be a possible solution?

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: Any simple DBus examples?

Post by BruceSteers » Thursday 08th July 2021 11:55am

PJBlack wrote:
Thursday 08th July 2021 10:38am
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
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
PJBlack
Posts: 159
Joined: Tuesday 09th June 2020 10:26pm
Location: Florstadt, Hessen, Germany

Re: Any simple DBus examples?

Post by PJBlack » Thursday 08th July 2021 1:17pm

BruceSteers wrote:
Thursday 08th July 2021 11:55am
A solution to what?
I thought it was about preventing the application from starting multiple times ...

sorry if I missed something

User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

Re: Any simple DBus examples?

Post by BruceSteers » Thursday 08th July 2021 5:00pm

PJBlack wrote:
Thursday 08th July 2021 1:17pm
BruceSteers wrote:
Thursday 08th July 2021 11:55am
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..
https://forum.gambas.one/viewtopic.php?f=13&t=943

using pgrep is better than PID files but Dbus is even better as it handles the messages too (no need for pipe or socket)
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply