Locking Laptops

2026-01-21

I’ve been running OpenBSD as my primary OS for some time now. Since I use a laptop, I like to have my system automatically lock on suspend, or after a set period of inactivity. Here’s a quick guide to help set things up.

I’ve added the following line to my ~/.xsession script to run xidle in the background when my user starts a graphical session:

...
xidle -program "/usr/X11R6/bin/xlock" -timeout 300 -no &

exec bspwm

After five minutes of inactivity, xidle will call xlock and lock the screen. Now all that’s left is to trigger xidle when the laptop suspends.

Reading xidle(1):

DESCRIPTION
     xidle uses the XScreenSaver(3) extension to receive inactivity events
     when a timeout is specified, running a specific program after the elapsed
     time.  xidle will also run the program if it is sent a SIGUSR1 signal, or
     if the pointer sits in a corner of the screen for an amount of time.
     Signal and corner locking are always enabled, whether -timeout is
     specified or not.

and apmd(8):

FILES
     ...
     /etc/apm/hibernate
     /etc/apm/powerdown
     /etc/apm/powerup
     /etc/apm/resume
     /etc/apm/standby
     /etc/apm/suspend
     /etc/apm/warnlow      Action hook files which, if present, must be
                           executable.  Each file must be an executable binary
                           or shell script.  A single program or script can be
                           used to control all transitions by examining the
                           name by which it was called.

reveals the solution is to create a script for /etc/apm/suspend that will send a SIGUSR1 signal to xidle when the laptop suspends:

#!/bin/sh
pkill -USR1 xidle