Linux idle time detection broken
Posted: Tue Apr 21, 2020 2:46 am
The idle detection algorithm seems to be broken in Linux, even though X Server provides this information in it's API see this
xidle is show below
This code upon testing produces segfaults when the info or display pointers are NULL and is therefore unsafe for use, it however does show what is needed at a minimum to get idle time in milliseconds.
xprintidle is a safer approach as it contains error checking and a workarounds for some systems. (See workaroundCreepyXServer in xprintidle link).
This will only work on systems with an xserver running, which will still leave headless systems with broken idle time detection. You could use the 'w' command and awk something useful out of it, or possibly grab the source here as a baseline to derive what's needed.
Code: Select all
https://github.com/gpolitis/xidle/blob/master/xidle.c or
https://github.com/g0hl1n/xprintidle/blob/master/xprintidle.c
Code: Select all
#include <stdio.h>
#include <X11/extensions/scrnsaver.h>
main() {
XScreenSaverInfo *info = XScreenSaverAllocInfo();
Display *display = XOpenDisplay(0);
XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
printf("%u ms\n", info->idle);
}
xprintidle is a safer approach as it contains error checking and a workarounds for some systems. (See workaroundCreepyXServer in xprintidle link).
This will only work on systems with an xserver running, which will still leave headless systems with broken idle time detection. You could use the 'w' command and awk something useful out of it, or possibly grab the source here
Code: Select all
https://gitlab.com/procps-ng/procps/-/blob/master/w.c