Recent

Author Topic: [SOLVED] How to prevent Linux to enter Standby while my program is running?  (Read 12764 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 742
While my program runs, I want to prevent that the computer enters Standby mode after 20 minutes, as usual. It does not matter, if the monitor goes to sleep or not.

I found https://forum.lazarus.freepascal.org/index.php/topic,53143.0.html and walked through it, but found no "solution" in there.

Because every mp3- or video-player needs this function, I assume that Linux must provide a way for that. Either via some kind of "System Call" or a "command line tool".

Does someone of the Linux experts know such a "System Call" or command line tool?

I need a solution for Ubuntu 18.04 (with KDE Desktop, if that matters). Thanks in advance.
« Last Edit: October 12, 2021, 06:58:28 pm by Hartmut »

Wallaby

  • Jr. Member
  • **
  • Posts: 78
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #1 on: October 10, 2021, 02:24:47 pm »

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #2 on: October 10, 2021, 03:02:01 pm »
Hello.

In console (or use a TProcess) do this:

Code: Bash  [Select][+][-]
  1. xset -dpms
  2. xset s off
« Last Edit: October 10, 2021, 03:22:49 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #3 on: October 10, 2021, 06:05:09 pm »
It's called Inhibit lock: https://www.freedesktop.org/wiki/Software/systemd/inhibit/
If you want a command line tool, then systemd-inhibit: https://www.freedesktop.org/software/systemd/man/systemd-inhibit.html

Thank you very much Wallaby for that 2 links. The first seems to be what I need, but I'm a bit helpless how and where to start. The documentation is very large (and has a lot of links with other links) but this is hard stuff and I did not understand very much how to access that thing from Free Pascal. Is there a class or a unit which I can use? I tried units Baseunix, Linux and Unix but didn't find procedure Inhibit() there. Can you please give me more infos how to access that thing? I'm a bloody beginner to that kind of stuff...

If I understood your second link correctly, it is not possible to call this solution from a program, because the program is called by command "systemd-inhibit". My program has only 1 possible function, which needs to prevent Standby, but not for the whole program. So I need something, which I can call at the beginning of this special function.

In console (or use a TProcess) do this:
Code: Bash  [Select][+][-]
  1. xset -dpms
  2. xset s off

Thanks Fred vS for that idea. I tried it from a console, but unfortunately it didn't work. It made the monitor not going to sleep after 15 minutes, but Standby was entered 5 minutes later as usual. I'm only looking for preventing Standby, not the monitor.

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #4 on: October 10, 2021, 06:24:46 pm »
Thanks Fred vS for that idea. I tried it from a console, but unfortunately it didn't work. It made the monitor not going to sleep after 15 minutes, but Standby was entered 5 minutes later as usual. I'm only looking for preventing Standby, not the monitor.

Ha ok, then why dont you use simply the Settings-Power Management applet and disable "Suspend" ?
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #5 on: October 10, 2021, 06:40:58 pm »
Thanks Fred vS for that idea. I tried it from a console, but unfortunately it didn't work. It made the monitor not going to sleep after 15 minutes, but Standby was entered 5 minutes later as usual. I'm only looking for preventing Standby, not the monitor.

Ha ok, then why dont you use simply the Settings-Power Management applet and disable "Suspend" ?

If you prefer to do it from command line, this is working here on Debian 11 (Ubuntu is a fork of Debian):

To disable hibernate and suspend use the following command:

Code: Bash  [Select][+][-]
  1. sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

To re-enable hibernate and suspend use the following command:

Code: Bash  [Select][+][-]
  1. sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #6 on: October 10, 2021, 07:20:53 pm »
Ha ok, then why dont you use simply the Settings-Power Management applet and disable "Suspend" ?

This would need every time:
 - half a dozen mouse-clicks to disable Standby
 - half a dozen mouse-clicks to re-enable Standby after my program finished
 - and I would forget at least one of both every 5th time...
This would be extremely uncomfortable and unsure. If I would want something like this, I would not have asked in this forum ;-)

If you prefer to do it from command line, this is working here on Debian 11 (Ubuntu is a fork of Debian):

To disable hibernate and suspend use the following command:
Code: Bash  [Select][+][-]
  1. sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

To re-enable hibernate and suspend use the following command:
Code: Bash  [Select][+][-]
  1. sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target

Thanks a lot for that interesting suggestion, but unfortunately it needs sudo. It would be very uncomfortable, if I had to start my program always with password with a chance of only 5% to 10% that to prevent from Standby is really needed. Every mp3- and video-player does this job without needing sudo, so there must be a better way...

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #7 on: October 10, 2021, 07:27:12 pm »
If you want a command line tool, then systemd-inhibit: https://www.freedesktop.org/software/systemd/man/systemd-inhibit.html

I tried it in a console:
   systemd-inhibit <my_program>
which from the Example in your link should "inhibit system sleeping, shutdown and idle". But it did not work, the computer entered Standby as usual :-((

Does anybody have an idea, why this does not work on my Ubuntu 18.04 (with KDE Desktop, if that matters)?

Does it make sense, to follow this suggestion:
It's called Inhibit lock: https://www.freedesktop.org/wiki/Software/systemd/inhibit/
if console command "systemd-inhibit" does *not* work on my system?

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #8 on: October 10, 2021, 07:47:17 pm »
Does anybody have an idea, why this does not work on my Ubuntu 18.04 (with KDE Desktop, if that matters)?

Does it make sense, to follow this suggestion:
It's called Inhibit lock: https://www.freedesktop.org/wiki/Software/systemd/inhibit/
if console command "systemd-inhibit" does *not* work on my system?

Did you try with root privilege?
Code: Pascal  [Select][+][-]
  1. sudo systemd-inhibit <my_program>
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #9 on: October 10, 2021, 07:59:56 pm »
Hello.

Sorry but I dont see console solution without sudo.
[EDIT] Maybe this one: https://superuser.com/questions/1413107/how-to-use-systemd-inhibit-without-sudo

Maybe you could think about a workaround to reset the counter of suspend?
Something like creating the same dummy file with filecreate() every 3 minutes ?
[EDIT] Not sure that creating file reset idle but maybe moving the mouse with something like this:

Code: Pascal  [Select][+][-]
  1.     var
  2.       MousePos: TPoint;
  3.     ...
  4.       MousePos := Mouse.CursorPos;
  5.  
  6.       MousePos.X := MousePos.X + 10;
  7.       MousePos.Y := MousePos.Y + 10 ;
  8.       Mouse.CursorPos := MousePos;
  9.        sleep(10);
  10.       MousePos.X := MousePos.X - 10;
  11.       MousePos.Y := MousePos.Y - 10 ;
  12.       Mouse.CursorPos := MousePos;
  13.  

this with a timer duration 10 minutes?
And reset this timer each time the user do something in your program.

Not the most academical way but it should work.
« Last Edit: October 10, 2021, 08:51:02 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1113
  • Professional amateur ;-P
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #10 on: October 11, 2021, 01:42:24 am »
Hey Hartmut,

I need a solution for Ubuntu 18.04 (with KDE Desktop, if that matters). Thanks in advance.

Did a very quick search on Google and found this answer on Stack Overflow: [Answer] How to programmatically prevent linux computer from sleeping or turning on screensaver?

The question is a bit misleading when you're thinking about media players, but the answer actually mentions that he looked at mplayer and SDL when he searched for a solution.

One of the things he mentions is using DBus to access the Inhibit thingamaboob.

Around 5 years ago I created this repo on GitHub with some code to access DBus: fpdbus-tools.
All of the code I've posted there is just a copy of the code that Michaël Van Canneyt created along side his article on DBus.
In those tools you have a DBus explorer and a tool to create the stubs(pascal code) to access the thing you're after.

I had a bit of a go at using it but didn't quite got past the whole magic of it all, so I'm completely useless if you want help on it, very sorry.

Hope this helps, or at least gets you unstuck :)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Gustavo 'Gus' Carreno

  • Hero Member
  • *****
  • Posts: 1113
  • Professional amateur ;-P
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #11 on: October 11, 2021, 01:56:47 am »
Hey Hartmut,

If you decide to take the Dbus route, I've found a bit more info while using the title of the first question in Google:
Prevent system from going to sleep/suspend - how Xviewer/VLC do it.

The answer that is marked as the solution details a lot of the DBus chatter that Firefox and VLC do when they have to disable the screensaver/suspend.

Didn't read it all, but I found it long enough, that I'm guessing that it contains enough information to get you started if you would want to opt for the DBus solution.

On a side note, this would be the way I would implement it. DBus was created for such Desktop Environments agnostic usage, and, while it's a bit complicated to wrap you're head around, seems to function quite well. But don't heed me any attention, cuz this is just my 2 bits opinion on it ;)

Cheers,
Gus
Lazarus 3.99(main) FPC 3.3.1(main) Ubuntu 23.10 64b Dark Theme
Lazarus 3.0.0(stable) FPC 3.2.2(stable) Ubuntu 23.10 64b Dark Theme
http://github.com/gcarreno

Thaddy

  • Hero Member
  • *****
  • Posts: 14205
  • Probably until I exterminate Putin.
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #12 on: October 11, 2021, 09:51:43 am »
Every recent Debian or Debian derived distro (like Ubuntu) contains molly-guard:
sudo apt-get install molly-guard.

http://manpages.ubuntu.com/manpages/impish/en/man8/molly-guard.8.html

Note that there are plenty of other options (editting system.d for one) , but this one is very user friendly. In essence: it does that for you... (Bonus: Molly is very proficient in Pascal too
  8-) :-X )
« Last Edit: October 11, 2021, 09:56:53 am by Thaddy »
Specialize a type, not a var.

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #13 on: October 11, 2021, 12:38:28 pm »
I knew the "problem" sounded familiar
https://forum.lazarus.freepascal.org/index.php/topic,56307.msg418401.html#msg418401

Nota Bene: That's for Windows.
Not sure if there is a cross-platform way

On a sidenote: Never ever touch Power-Management settings from within your Program
« Last Edit: October 11, 2021, 12:45:50 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Hartmut

  • Hero Member
  • *****
  • Posts: 742
Re: How to prevent Linux to enter Standby while my program is running?
« Reply #14 on: October 11, 2021, 05:07:31 pm »
Thanks a lot to all for your posts. You are quicker in writing that I can read the (long) links and test them. I'm slow because my English is not the best and this all is completely new and hard stuff for me. I will try and report your suggestions piece by piece...

If you prefer to do it from command line, this is working here on Debian 11 (Ubuntu is a fork of Debian):

To disable hibernate and suspend use the following command:
Code: Bash  [Select][+][-]
  1. sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

To re-enable hibernate and suspend use the following command:
Code: Bash  [Select][+][-]
  1. sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target

I tried this suggestion and after 20 minutes the system did not enter Standby, but my console session got lost (disappeared) and the Login screen was displayed, where I had to enter my password, although this is configured not to be neccessary. After login the system needed 45 seconds to draw the desktop. Tested 2 times => not usable on my system.



Did you try with root privilege?
Code: Pascal  [Select][+][-]
  1. sudo systemd-inhibit <my_program>

Now I tried with sudo: after 20 minutes the system did not enter Standby, but a dialog appeared (see screenshot) to enter sudo password => not usable for me.



... but maybe moving the mouse with something like this:

Code: Pascal  [Select][+][-]
  1.     var
  2.       MousePos: TPoint;
  3.     ...
  4.       MousePos := Mouse.CursorPos;
  5.  
  6.       MousePos.X := MousePos.X + 10;
  7.       MousePos.Y := MousePos.Y + 10 ;
  8.       Mouse.CursorPos := MousePos;
  9.        sleep(10);
  10.       MousePos.X := MousePos.X - 10;
  11.       MousePos.Y := MousePos.Y - 10 ;
  12.       Mouse.CursorPos := MousePos;
  13.  

I made a litte demo and it worked!
Code: Pascal  [Select][+][-]
  1. const stop: boolean = false;
  2.  
  3. procedure TForm1.Button_StartClick(Sender: TObject);
  4.    var mp: TPoint;
  5.        d: integer;
  6.    begin
  7.    d:=0; {'0' works on Ubuntu 18.04 with KDE desktop}
  8.    repeat
  9.       mp:=Mouse.CursorPos;
  10.       inc(mp.x,d);
  11.       inc(mp.y,d);
  12.       Mouse.CursorPos:=mp;
  13.       Application.ProcessMessages;
  14.       sleep(2000);
  15.       d:=-d;
  16.    until stop;
  17.    Close;
  18.    end;
  19.  
  20. procedure TForm1.Button_CloseClick(Sender: TObject);
  21.    begin
  22.    stop:=true;
  23.    end;

I reduced var 'd' until zero pixels and it still worked! So only command "Mouse.CursorPos:=mp;" does prevent the system from entering Standby. Thank you very much Fred vS for this idea! A benefit is, that no system settings have to be changed (and later restored!). And no sudo is needed :-) A side effect is, that the monitor does not go to sleep any longer (no problem for me).

It does *not* work on Windows 10 (not even with 'd:=10'), but I'm now only interested on Linux.

But a problem is, that I didn't manage to adapt this mouse solution into a console program. Here is my attempt:
Code: Pascal  [Select][+][-]
  1. uses sysutils, crt, Mouse;
  2.  
  3. procedure Test_Maus;
  4.    var d,x,y: integer;
  5.    begin
  6.    InitMouse;
  7.    write(DetectMouse, ' Buttons '); {returns "2 Buttons"}
  8.    ShowMouse;
  9.    d:=10;
  10.  
  11.    repeat
  12.       x:=GetMouseX;
  13.       y:=GetMouseY;
  14.       write(x, '/', y, '':2); {is ALWAYS 2x zero!!}
  15.       inc(x,d); inc(y,d);
  16.       SetMouseXY(x,y); {seems to do nothing}
  17.       sleep(2000);
  18.       d:=-d;
  19.    until keypressed;
  20.    DoneMouse;
  21.    end;

The problem is: GetMouseX() and GetMouseY() both ALWAYS return '0' => when 'd' is negative, an exception occurs. What do I make wrong?
Is there another (better) way to access the mouse in a console program?


Meanwhile I'll have a look at the other suggestions...
« Last Edit: October 11, 2021, 05:10:38 pm by Hartmut »

 

TinyPortal © 2005-2018