DBus logout/shutdown Inhibitor

Ask about the individual Gambas components here.
Post Reply
User avatar
BruceSteers
Posts: 1563
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

DBus logout/shutdown Inhibitor

Post by BruceSteers »

I've been trying to make Inhibit work on many systems.

What is Inhibit() ? you may ask.

Inhibit() is the terminology used to describe what your program does if the system wants to logout or shutdown or run a screensaver, etc.

For example most media players will use Inhibit() to stop your screensaver showing while watching a movie.
also cd writing programs and such will inhibit the system changing state while writing a cd/dvd.

How I have used it...
I have actually added it to my gambas IDE and it works great. :)
I have made it create Inhibitors if a file/gui has been edited and not saved in the IDE then the inhibitors are removed when the file saves.
Also it makes an inhibitor when saving .project data.

Now if i try to shut down with edited/unsaved documents i get a dialog showing the unsaved document names and logout is inhibited.
As i save each document they disappear from the dialog until the last one is gone then logout commences. (or i just press save project)

This to me is Brilliant! :)

But here's the problem..
org.gnome.SessionManager seems to really work well and do the required job but it seems only on gnome2 based environments (MATE, Cinnamon, gnome-fallback)

On gnome3 systems (gnome shell) and kde plasma and wayland it does not work.
it does not crash or cause problems it just doesn't work :'(

I have looked into many other ways.
org.freedesktop.portal.Desktop (doesn't seem to work)
System://org.freedesktop.login1 (just gives segfault 11)
a few others.

Cannot seem to get anything to work other than gnome.SessionManager

Anyone had any success using Inhibit on other systems (even if via terminal)

Attached is my current Inhibitor.class that I have added to my gambas IDE.

Usage of the inhibitor in the test app is very simple.

Here is the FMain code...
Private $hTask As Inhibitor

Public Sub Form_Close()

  If $hTask Then $hTask.Kill

End

Public Sub Button2_Click()
 
  If Last.Value Then
    $hTask = New Inhibitor(Application.Name, "Editing Doc", 15)
  Else
    $hTask.Kill
  Endif

End


If you run the test app you can press the "Toggle Inhibitor Enabled" toggle button to enable/disable the inhibitor.
Test it by enabling it, then trying to logout or suspend/sleep the system.
on a gnome2 based system it should pop up the warning and not logout until you either close the program, disable the inhibitor or pres "logout anyway" in the system dialog.

The Inhibitor is very simple to add to your own program, just pop the Inhibitor.class in your .src folder, that's it, it will auto-load gb.dbus so no need to add the component.

Create an inhibitor to inhibit and kill it to release the inhibit.
Shimples

Really want to get this working on other systems though.
Any help appreciated
BruceS
Attachments
Screenshot at 2023-02-21 19-30-11.png
Screenshot at 2023-02-21 19-30-11.png (196.3 KiB) Viewed 3393 times
dbus.inhibit-1.0.tar.gz
(12.67 KiB) Downloaded 182 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1125
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: DBus Inhibitor

Post by cogier »

Can I suggest you look at how others do this. I downloaded the source code for the text editor Gedit, did a search for 'inhibit' and came up with this.

Code: Select all

}

	if (can_close && (priv->inhibition_cookie != 0))
	{
		gtk_application_uninhibit (GTK_APPLICATION (g_application_get_default ()),
					   priv->inhibition_cookie);
		priv->inhibition_cookie = 0;
	}
	else if (!can_close && (priv->inhibition_cookie == 0))
	{
		priv->inhibition_cookie = gtk_application_inhibit (GTK_APPLICATION (g_application_get_default ()),
		                                                   GTK_WINDOW (window),
		                                                   GTK_APPLICATION_INHIBIT_LOGOUT,
		                                                   _("There are unsaved documents"));
	}
I admit that this looks very GTK specific, but maybe there is a program out there that would give you a clue. I tried the KDE text editor Kate but came up with nothing.
Reading this indicates that inhibiting closure is not guaranteed.
User avatar
BruceSteers
Posts: 1563
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: DBus Inhibitor

Post by BruceSteers »

hmm yeah but that's deep gtk code (like internal gb.gtk3 C functions)

i've asked Benoit if gb.gui could possibly implement Inhibit, (i think it would be awesome, especially if incorporated into the ide)
if there is a qt alternative then it probably won't take much code to add the inhibit methods to the gui components. (practically a copy-n-paste of another function) that side of gambas is a bit beyond me though.
If at first you don't succeed , try doing something differently.
BruceS
Post Reply