Any simple DBus examples?
Posted
#1
(In Topic #698)
Guru

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
Posted
Enthusiast

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
Posted
Guru

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)
- 'check if gbTE is already open
- 'yes it is, so pass-off current file list
- DBus[TEXTEDITOR_DBUS_NAME][TEXTEDITOR_PATH, TEXTEDITOR_DBUS_INTERFACE].Open($file.Name, $file.ReadOnly, $file.LineNumber, $file.ColumnNumber)
- Return 'exit, replace with Me.Close if in startup form
- 'set up to receive pass-off commands
- DBus.Name = TEXTEDITOR_DBUS_NAME
- hDbusObj = New DbusObj
- 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
- DBus.Unregister(hDbusObj)
The remaining code goes into a class file named dbusobj.
Code (gambas)
- ' Gambas class file
- Inherits DBusObject
- fEditor.Show()
- fEditor.AddEditor(sFilePath, bReadOnly, nLine, nColumn)
Posted
Enthusiast

Posted
Guru

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)
.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.
Posted
Enthusiast

Posted
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
Posted
Enthusiast

BruceSteers said
A solution to what?
I thought it was about preventing the application from starting multiple times …
sorry if I missed something
Posted
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)
1 guest and 0 members have just viewed this.



