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