How are sleep events handled such that applications have time to capture current state?

Part of an application that I maintain plays audio lectures held in a database. A browser is used for the user interface and Tcl is used as a local server.

If all the user does is listen to the lectures without interacting with the interface, it appears that this is considered a period of inactivity; and, if the system settings for power management are set to sleep, for example, the audio stops playing.
(The interface updates the time and track information as the lecture segments play in turn but that doesn’t appear considered user interaction.)

I realize that the user should increase the time in power management; but, if they do not, is there any way either through Tcl or the browser to programmatically indicate to the OS that the application is active?

Or, is there some way to listen for a sleep or wake event in Tcl? If the OS sleeps in the middle of playing the audio, that would not be an issue except for the fact that the audio stops without changing the UI player controls and I want to capture the UI state. In other words, the player controls show pause instead of play at awake and, if the user were to shutdown immediately upon awake, I wouldn’t capture the exact point that the audio paused.

A listener in the browser for an audio pause event appears to still fire upon sleep such that I can update the player controls to reflect that; but will an OS sleep give applications time to capture the last state? For example, if at the pause event the code updates the UI player controls and sends a message over a web socket to the local Tcl server to store some state data (such as the current time of the audio track) will the OS reliably permit that to complete before sleep? [Adding that it is working such that, upon sleep, the UI updates and the web socket request to Tcl completes a write to a SQLite database capturing the state before sleeping. But is that just “lucky” or is it guaranteed?]

Where can I read about how Manjaro/arch handles sleep?

Thank you.

Hello @Gary1 :wink:

Nowadays, browsers use the systemd API internally to inhibit suspending the computer when in full screen, for example. No idea about audio only.

See:

Example:

systemd-inhibit --what='idle' --who='TCL' --why='Audio Streaming' --mode='block' <command>
1 Like

Maybe you can glean something useful from the way caffeine-ng handles temporary prevention of sleep events.

1 Like

As mentioned above, read Inhibitor Locks. You most likely want to add a “delay” mode inhibitor, and listen to the PrepareForShutdown() and PrepareForSleep() signals emitted by systemd, and save whatever state you want when those signals arrive.

The PrepareForShutdown() and PrepareForSleep() signals are emitted when a system suspend or shutdown has been requested and is about to be executed, as well as after the suspend/shutdown was completed (or failed).

The signals carry a boolean argument. If True the shutdown/sleep has been requested, and the preparation phase for it begins, if False the operation has finished completion (or failed).

If True, this should be used as indication for applications to quickly execute the operations they wanted to execute before suspend/shutdown and then release any delay lock taken. If False the suspend/shutdown operation is over, either successfully or unsuccessfully (of course, this signal will never be sent if a shutdown request was successful).