If you’re concerned about the “sudo” part being inside of the script, setting up a listener script that listens to the power event is the only way. I have tried to make a non-sudo version of the script and launching it from wayfire or labwc won’t actually work properly for whatever reason.
I found I had to do this anyways, since I wanted to lock the screen (via swaylock
) in my case. The screen locker prevents the power key from being received by the compositor / window manager to wake back up, and systemd's
logind
isn’t flexible enough, by itself, to do this.
It wasn’t too hard, it turned out. I found that the libinputs
command, which is available via libinput-tools
or libinput-utils
depending on your distro, will watch for events on the devices you specify, and the output is simple to work with. I found that /dev/input/event0
shows up as a separate keyboard device, and is where the power button events come from on the uConsole.
So, I now have, on my device, a modified version of qsleep.sh
that looks like this:
function toggle {
if test "$(cat /sys/class/drm/card1-DSI-1/enabled)" = "enabled"
then
...
else
...
fi
}
function watch {
declare -a event
libinput debug-events --device /dev/input/event0 | while read -a event
do
case "${event[1]} ${event[3]} ${event[5]}" in
"KEYBOARD_KEY KEY_POWER released")
toggle
;;
esac
done
}
...
The reason for only responding to released
is because I still let logind
handle a long key press by shutting down the machine, so I can still power off cleanly. It’s nice, when I need to do this, that the screen is still powered on, so that I can see that the machine is actually powering off.
I run my script from within my session, so that wlr-randr
and swaylock
work without me having to set any specific variables, and I wrote a simple C program that is setuid root to set the governor (at least until I figure out a portable way to do all of this from the session manager).
The full code is here.