Don't let the mouse wake up displays from standby

I like to put my displays to powersave/standby mode when I leave the computer for a while. It would be ok if it weren't for oversensitive mouse. Sometimes the driver reads in some movement that's not visible to the naked eye (the cursor, that is) and it breaks the power save. It would wait for another 10 minutes before going back to its standby.

My workaround is the following script bound to C-S-q:

xlock -startCmd 'xset dpms 2 2 2' -endCmd 'xset dpms 600 1200 1300' -mode blank -echokeys -timeelapsed +usefirst

By using xset I set the values to 2 seconds each before going to standby. It's not nice, anyway. Sometimes there are cool fortunes that I want to read before typing in the password. I could keep the cursor moving but it's cludgy. (By the way, xlock's option mousemotion doesn't help -- it just hides the cursor but the displays fire up nevertheless.)

So the question: is there a way to make displays go standby and stay there until a keyboard key is pressed? I'm running gentoo and recent Xorg, but I hope the answer doesn't have to be distro-specific.

Basically the answer can be as simple as how to enable/disable mouse within command line? It think that would do the job if DPMS doesn't know the idea.


Solution 1:

There's a great post by @pbm that covers this, over on the Unix SE site.

In short, first use xinput list to get the device ID for your mouse, and then use

xinput --set-prop [ID#] "Device Enabled" "0"

and

xinput --set-prop [ID#] "Device Enabled" "1"

to disable and enable the mouse.

I've just tested these here by disabling the mouse and then calling xset dpms force standby. Waving the mouse around for several seconds did nothing to disturb the screen, but pressing a single key worked fine. Using the second command then returned the mouse back to normal, including the "constant deceleration" setting that my default startup scripts set for me.

You should be able to pass these two commands to the -startCmd and -endCmd options to xlock to get what you're after.

Solution 2:

I do it this way in Ubuntu:

#!/bin/bash

# allow only one instance
r=$(pidof -x -o $$ ssmonoff.sh)
set -- $r
if [ "${#@}" -ge 1 ]; then
    echo "Script already running. Exit..."
    exit
fi

dbus-monitor --session "type='signal',interface='org.gnome.ScreenSaver'" | ( while read line; do
    if echo $line | grep "boolean true" &> /dev/null; then
            xinput --set-prop "Dell Premium USB Optical Mouse" "Device Enabled" "0"
            xset dpms force off
    else
            xinput --set-prop "Dell Premium USB Optical Mouse" "Device Enabled" "1"
    fi
done )

Some notes:

  • I'm also forcing monitor off.
  • Better use the full name instead of ID.

I hope that this helps.

Solution 3:

Something similar to MiLo answer but without dbus:

MOUSE="Logitech Wireless Mouse"

# Disable mouse
xinput --set-prop "${MOUSE}" "Device Enabled" "0"

# Turn off screen
xset dpms force off

# Wait to screen to be on again to enable mouse
while true; do
  status=$(xset q | grep -o "^\s*Monitor is .*" | grep -o -e On -e Off)
  if [[ $status == "On" ]]; then
    xinput --set-prop "${MOUSE}" "Device Enabled" "1"
    exit 0
  fi
  sleep 1
done