I want to automatically dim keyboards light when its been inactive for some time. How can I detect inactivity time? I use archlinux, btw.
3 Answers
On Debian-based systems, there's a xprintidle
command in the xprintidle
package (from https://github.com/g0hl1n/xprintidle/) that prints the idle time in milliseconds on stdout.
$ sleep 2; xprintidle
1925
If you ltrace
it, you'll see it uses XScreenSaverQueryExtension()
, XScreenSaverQueryInfo()
API like in other answers though also adds some workarounds to specific issues in corner cases with some servers.
Compiling and running this gets idle time in miliseconds
#include <X11/extensions/scrnsaver.h>
#include <stdio.h>
int main(void) {
Display *dpy = XOpenDisplay(NULL);
if (!dpy) {
return(1);
}
XScreenSaverInfo *info = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
printf("%u\n", info->idle);
return(0);
}
This is also implemented in C (with Makefile) at https://github.com/mjml/xidle.
It will provide hours, minutes, seconds, and milliseconds of idle time for shell scripts, with units/denominations selected by optional switches.