Enabling slock on lid close even without suspend

Enabling slock screensaver on lid close event without suspend

Context

TL;DR: You can avoid suspending the system and then use then acpid handler to trigger slock on the "lid closed" event.

I was looking for a way to keep my wifi connection alive when i'd close the lid of my laptop running Archlinux. I have slock1installed on my system, and a shortcut in AwesomeWM to trigger it. I have a couple of issues with that setup, one of which is that I almost always forget to use it, because most of the time I just close the lid. The other obviously being that it falls in a suspend state, and therefore looses the wifi connection. It's not that big of a deal, especially since it does connect back to the wifi automatically. But i like things to be super snappy, and it was another reason for me to procrastinate doing archlinux config…

Let's get into it

First of all, if you're looking for informations about power management on Linux, i can more than recommend the archlinux article about it. The /etc/systemd/logind.conf is the file containing the actions related to ACPI events. We just have to uncomment and modify the HandleLidSwitch line2 to:

HandleListSwitch=ignore

There might be a way to let systemd-logind to handle the whole "launch slock on this event", but I haven't find it. Anyway, since we need another way to do it, we'll use acpid. Once installed, you can configure it in /etc/acpid/handler.sh which is just a big switch-case, and find the "button/lid)" case and change it to:

button/lid)
 case "$3" in
     close)
     logger 'LID closed'
     sudo -u <username> DISPLAY=:0.0 slock
     ;;
     open)
     logger 'LID opened'
     ;;
     *)
     logger "ACPI action undefined: $3"
     ;;

Addendum

Hopefully you'll find this useful, and do feel free to email me if you find a better way to do this or to point out any mistake I made.

Footnotes

1 See also the Archlinux documentation

2 You have an array of different events depending on your use case, be it HandleLidSwitchExternalPower or HandleLidSwitchDocked.

links

social