Recent

Author Topic: Responding to or preventing energy/activity state events  (Read 1911 times)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Responding to or preventing energy/activity state events
« on: February 01, 2021, 01:35:17 pm »
I knew this but I can neither remember or find where I used it last :-[

I've an application that needs to be always active when run even if the system is otherwise inactive (i.e. no user attending it). It's not this1 but imagine, if you will, a computer used as a "media player" which has to keep playing whether there is someone "at the controls" or not so that, for example,  you can let it playing while you sit in your sofa and read some latest novel (or old classic ;)).

Problem is that when the system goes inactive (after, say, 15 min.) the screen shuts down, the session locks ... and your program stops running (and no music sounds)2.

How can I prevent this? I don't mind if the screen shuts down or what not, but I've to prevent the session lock and/or corresponding pause in the "playing".

I kind of recall that there was some system wide or dbus event(s) to which one could react but I can't remember what those where (if indeed I'm recalling right) or how to intercept them in Pascal code.

Any help, kind folks?


ETA: Forgot to tell! This has to work at least on GNOME, KDE, Xfce and LXDE (or close cousins) but I'll admit any partial solution ...


1. I' sorry but I can't even remotely hint at what this program does :(
2. Curiously enough, a TTimer the program has keeps aparently running, so when the system is restored to "ready" state the count it keeps is badly out of synch with the time it's suppossed to track. To keep on our example, let's say it reports a four minute song playing its 25th minute ... Thank the gods this isn't very important, but it's ugly as sin.
« Last Edit: February 01, 2021, 01:39:03 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Responding to or preventing energy/activity state events
« Reply #1 on: February 01, 2021, 01:55:41 pm »
This is at best a partial solution. I don't generally run systems set to hibernate etc. automatically, but I have in the past been caught by "semi-daemons"- i.e. programs which are both graphical but I want them to remain active- being nobbled by the screen saver. In the end I had the sensitive program run xdg-screensaver suspend every 30 seconds... I suspect that that is actually the maximally-portable way of doing it since AIUI distreaux are supposed to tailor the xdg-* programs for the window manager etc. which is actually installed and running and in fact...


$ file `which xdg-screensaver`
/usr/bin/xdg-screensaver: POSIX shell script, ASCII text executable


MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Responding to or preventing energy/activity state events
« Reply #2 on: February 01, 2021, 01:59:29 pm »
Not quite what I was looking for but thanks, Mark, and if it works I'm completely open to change my mind ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 4030
Re: Responding to or preventing energy/activity state events
« Reply #3 on: February 01, 2021, 02:37:36 pm »
I have no idea how to get that done on Linux. 

On Windows, I suggest you read the post https://stackoverflow.com/questions/629240/prevent-windows-from-going-into-sleep-when-my-program-is-running

For Linux, I would look at the source code for the VLC media player.  I presume it prevents sleeping on Linux just as it does under Windows.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Responding to or preventing energy/activity state events
« Reply #4 on: February 01, 2021, 02:38:45 pm »
I can't remember /why/ I hit it regularly... it might have been that there was something else on the system trying to turn the screensaver on, conflicts between the wm/desktop's screensaver and X11's are hardly unknown.

Mandatory XKCD https://xkcd.com/196/ :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Responding to or preventing energy/activity state events
« Reply #5 on: February 01, 2021, 03:15:17 pm »
On Windows, I suggest you read the post https://stackoverflow.com/questions/629240/prevent-windows-from-going-into-sleep-when-my-program-is-running

Thanks. I already knew how to do it on Windows (it's somewhere on my Delphi notes collection) but it's always nice to have another info source.

Quote
For Linux, I would look at the source code for the VLC media player.  I presume it prevents sleeping on Linux just as it does under Windows.

Ouch! That's close to 300 GiB MiB of (very complex) source to dive into! :o
I'll do it (or with almost any other media player: most of them can do it) if I have to but that's quite a project in itself ... I was hoping someone out there would spare me that task :-X

I can't remember /why/ I hit it regularly... it might have been that there was something else on the system trying to turn the screensaver on, conflicts between the wm/desktop's screensaver and X11's are hardly unknown.

It doesn't quite work here (Xubuntu 18.04): a debug log shows one of those arcane DBus messages and the screensaver keeps active.

There must be some other generally accepted way; after all, other programs (media players, mostly) do it. Even browsers do it when, say, you're watching videos in YouTube or the latest blockbuster in Amazon's Prime Video ... which now I think about it might not be the browser but the underlying media lib.

Quote
Mandatory XKCD https://xkcd.com/196/ :-)
Never fails :D :D :D
« Last Edit: February 01, 2021, 06:38:28 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Responding to or preventing energy/activity state events
« Reply #6 on: February 01, 2021, 04:00:19 pm »
> Doesn't quite work here :-(

You could possibly get somewhere by firing up a D-Bus monitor and then starting to play something using VLC. However I emphasise "possibly" there since there might also be notification events travelling around via the X11 socket and/or OpenGL etc.

xdg-screensaver looks as though it tries to use all possibilities. I think that looking at that might be a good start, and trying all methods individually and in combination in case what's in there works but is unable to deduce which method to use.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

varianus

  • New Member
  • *
  • Posts: 22
Re: Responding to or preventing energy/activity state events
« Reply #7 on: February 01, 2021, 05:47:52 pm »
Quote
Quote
For Linux, I would look at the source code for the VLC media player.  I presume it prevents sleeping on Linux just as it does under Windows.
Ouch! That's close to 300 GiB of (very complex) source to dive into! :o
I'll do it (or with almost any other media player: most of them can do it) if I have to but that's quite a project in itself ... I was hoping someone out there would spare me that task :-X

A few days ago I did a search in the VLC sources for the same problem. I haven't written any code yet, but I saved this link which highlights the points where it refers to screensaver inhibition  for various platforms, maybe it can be useful:  https://code.videolan.org/search?utf8=%E2%9C%93&snippets=false&scope=&repository_ref=master&search=vlc_inhibit_t&group_id=9&project_id=435

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Responding to or preventing energy/activity state events
« Reply #8 on: February 01, 2021, 06:36:56 pm »
A few days ago I did a search in the VLC sources for the same problem. I haven't written any code yet, but I saved this link which highlights the points where it refers to screensaver inhibition  for various platforms, maybe it can be useful:  https://code.videolan.org/search?utf8=%E2%9C%93&snippets=false&scope=&repository_ref=master&search=vlc_inhibit_t&group_id=9&project_id=435

Yes! It is quite usefull, thanks. I'm a bit "rusty" (to say it kindly) in C(++) code interpretation but it looks like iit could be relatively easy to translate to Pascal. I'll give it a whirl as soon as I can; this project has me swamped (lots of work, more than anything) in quite a lot of areas and this one is a relatively minor issue.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6686
Re: Responding to or preventing energy/activity state events
« Reply #9 on: February 01, 2021, 06:43:44 pm »
Hmm... well done. Using that same search system for "screensaver" is also interesting in that among other things it pulled up


 * On X11 platforms, a _working_ version of xdg-screensaver is typically
   required to inhibit the screensaver during video playback.


The emphasis is theirs.

MarkMLl

MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018