How to know display uptime

I wondered why this solution does not already exist, when I took the dive at it, I see it wasn't feasible, at least as far as my research, skills and knowledge goes.

On linux, running with x11 (maybe not), I want to know the display uptime for a user session.

Let me elaborate, display uptime, I mean when the screen is flashing light minus screensaver or locked screen. The display time when the user can launch an application. Say am building a program that monitors how long an application is running, ideally I would need to kmow the total uptime the display was on to say "this application was used 20% today".

I know I said a lot, but I hope I buttressed the point very well. So far the solution I've;

  1. polling xset q on x11 for the string "Monitor is On" and recording the intervals

  2. Another parochial solution, Gnome has it desktop background has a window, Gjs, that I have noticed is always on the LIST_STACKING x11 API calls that corresponds to me closing my laptop lid a destroy event on that window and a map whenever it opens again. This could be monitored ideally for when the display was up, I think so, but the solution is too narrow. It could even be only to the Gnome version am using.

  3. Journalctl, obvious but I could not navigate "how"

Thank you for any response to this. It would be appreciated.

I don't know an actionable answer to your question @tobytobias.

But, assuming a contemporary Linux desktop, there is a good chance that dbus, or something like it, is running. In the past I wrote a program that subscribed to / looked for dbus events related to the screen saver. My use case was to change an availability / busy / away status light when I locked my system stepped away. Maybe something like that could work for you.

I no longer have access to the code as it was at an old job.

1 Like

Thank you very much... I've actually looked at Dbus, my doubts is how compatible the solution would be for other DE... the program is aimed to be packaged and distributed for all distro (or as much as we can try).