If you use Xorg, you can use the utility xss-lock to lock the screen of your session. xss-lock is triggered by DPMS which since Xorg 1.8 is auto-detected and enabled if ACPI is also enabled at kernel runtime.
To use xss-lock, you can simple execute it and put it into the background before you start your window manager from e.g. your ~/.xsession:
xss-lock -- slock & exec stumpwm
In this example, xss-lock uses
slock to do the actual locking of the screen when
it determines it’s appropriate, like when you suspend your device.
For slock to be allowed to be a screen locker for the graphical session, it needs to be made setuid-root so it can authenticate users, and it needs a PAM service. This can be achieved by adding the following service to your config.scm:
If you manually lock your screen, e.g. by directly calling slock when you want to lock
your screen but not suspend it, it’s a good idea to notify xss-lock about this so no
confusion occurs. This can be done by executing
xset s activate immediately
before you execute slock.