Recommended

2013-10-30

How to query the X11 screen saver state

This blog post explains how to query the X11 screen saver state and other information such as idle time. This is useful on Unix systems if your program wants to know e.g how long the user has been idle.

If you need only the idle time, and you have TCL/Tk version 8.5 or later installed, run the following command (without the leading $). It prints the number of seconds the user has been idle.

$ echo 'puts [tk inactive]; exit' | wish

If you need more information about the screen saver state or you need better accuracy (milliseconds), then compile and run the following C program:

Please note that it's possible to affect the screen saver with the xset s ... command, and it's possible to affect the monitor power saving (sleep) with the xset dpms ... command. There are no such widely available commands for querying the screen saver state. The xset ... commands work differently for different display drivers. You may want to do a sleep .1 && xset ... to put your monitor to sleep right away.