I am trying to make a bash script to turn down the brightness to lowest when idle for a certain amount of time but reverts back to its previous brightness when it detects user interaction whether it be from the mouse or the keyboard. I know how to do the first part using xautolock but do not know how to execute the second part. Thanks

2 Answers 2


Presumably, you know about xset dpms which provides power management of the display, and brightd which does progressive backlight changes, but if these don't suffice you can detect movement once you are in idle mode by using xinput as follows.

List the input devices with simply xinput and find the ids of the mouse and keyboard you want to listen to. In my case it was 11 and 9.

Logitech USB Optical Mouse               id=11   [slave  pointer  (2)]
HID 05f3:0007                            id=9    [slave  keyboard (3)]

You can then wait for any output from xinput --test on each id:

xinput --test 9 | read stuff &  pid1=$!
xinput --test 11 | read stuff & pid2=$!
wait -n
kill -hup $pid1 $pid2 2>/dev/null
echo 'ready'

You can try using dbus. Specifically, use dbus-send to invoke the GetSessionIdleTime method on org.freedesktop.ScreenSaver:

dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call --print-reply=literal /ScreenSaver org.freedesktop.ScreenSaver.GetSessionIdleTime

This will print out 4 spaces, then "uint32", then the number of milliseconds since the last time the user pressed a key or moved the mouse. You can extract the number using the cut command:

echo $result | cut -d ' ' -f 5

This requires polling dbus at regular intervals, so there will be a delay between user input and your script's response, depending on how often you poll.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .