Lock after Suspend

In dwm and other minimal window managers, handling idle behaviors like screen locking and system suspension requires explicit configuration. Unlike full desktop environments, which handle these tasks automatically, a window manager like dwm focuses solely on managing windows. This leaves it up to the user to implement additional functionality as needed. While this might seem cumbersome, it allows for complete control over your system’s behavior, free from unnecessary overhead or bloat.

The motivation for this setup stems from a specific concern: after suspending the computer, there’s always a chance it might unintentionally wake up, such as from a stray keypress or accidental mouse movement. If you’re away from your desk when this happens, your system could remain exposed without a screen lock in place. This risk can be mitigated by combining slock with tools that monitor idle time and handle system suspension, ensuring that the screen remains locked even after resuming from a suspended state.

For this setup, I use three main components: slock for locking the screen, xidlehook for monitoring idle time, and a systemd service to enforce the lock on resume. xidlehook is a lightweight utility that executes commands after specified periods of inactivity. In this case, it runs slock after five minutes of idle time and suspends the system after ten minutes. To ensure the screen is locked after resuming, a systemd service triggers slock again whenever the system wakes from suspension.

Documenting this is just a matter of practicality. It’s easy to forget the details of a setup like this after months of it running smoothly in the background. By writing it down, I save myself the trouble of figuring it out again later.

Below is the configuration I use to implement this:

xidlehook Script

# install xidlehook from aur
yay -S xidlehook

idlehook-setup.sh:

#!/bin/bash

xidlehook \
  --timer 300 \
    'slock' \
    '' \
  --timer 600 \
    'systemctl suspend' \
    ''

Lock Server Post-Suspend

Create a .service file

sudo nvim /etc/systemd/system/lock-on-resume.service
[Unit]
Description=Lock screen after resume from suspend
After=suspend.target

[Service]
User=<your_username>
Type=simple
Environment=DISPLAY=:0
ExecStart=/usr/bin/slock

[Install]
WantedBy=suspend.target

Enable the service:

sudo systemctl daemon-reload
sudo systemctl enable lock-on-resume.service

This configuration effectively addresses the risk of accidental wake-ups, keeping the system secure even if you’re away from the keyboard.