How to auto lock screen in i3wm

最近修改于 2021-08-01

Desktop environments (Gnome, KDE, …) have an elaborate set of utilities covering almost every aspect of usages. When I started using i3wm, I missed the convenience of power-manager, locking screen, etc. But it is possible to implement them manually. This article is written about how auto lock-screen working in i3wm.

Desktop environments like Gnome and KDE come with a plethora of utilities that cater to almost every conceivable usage scenario. However, when I switched to i3wm, I found myself missing the convenience of certain features such as power management and screen locking. But fear not, for it is possible to manually implement these features in i3wm. In this article, I will be sharing my insights on how to set up an automatic lock-screen in i3wm, so you can enjoy the best of both worlds - the minimalism and efficiency of i3wm coupled with the convenience of a fully functional desktop environment.

Following introduction may be not concise and clear. If you want to see details about the configuration, my dotfiles is here.

There are two parts when configuring autolock. One is lock, the other is auto.

How to lock screen?

i3lock is a simple screen locker that have synergy with i3wm. If you are using Arch linux, install i3 package group will install i3lock automatically. And there are several alternatives. Such as i3lock-color, i3lock-fancy. They are all wrappers of primitive i3lock to add functions that can change lockscreen pictures and blur background easily.

And what I am using currently is XSecureLock. XSecurelock provides a simple ascii user interface when lock screen. Ascii art YES!

\|/          (__)    
     `\------(oo)
       ||    (__)
       ||w--||     \|/
   \|/

All the lockscreen programs can be invoked in the shell manually. Add some keybindings in i3wm will make life easily. Furthermore, Arch wiki provides a useful snippets to shutdown, reboot or lock screen in various keystrokes. Adapt it to your demand.

set $Locker i3lock && sleep 1

set $mode_system System (l) lock, (e) logout, (s) suspend, (h) hibernate, (r) reboot, (Shift+s) shutdown
mode "$mode_system" {
    bindsym l exec --no-startup-id $Locker, mode "default"
    bindsym e exec --no-startup-id i3-msg exit, mode "default"
    bindsym s exec --no-startup-id $Locker && systemctl suspend, mode "default"
    bindsym h exec --no-startup-id $Locker && systemctl hibernate, mode "default"
    bindsym r exec --no-startup-id systemctl reboot, mode "default"
    bindsym Shift+s exec --no-startup-id systemctl poweroff -i, mode "default"  

    # back to normal: Enter or Escape
    bindsym Return mode "default"
    bindsym Escape mode "default"
}

bindsym $mod+Pause mode "$mode_system"

How to auto lock screen?

Xautolock can be used to invoke preceding locking programs when the computer has been idle for a specified amount of time. And xidlehook is a general-purpose replacement for xautolock rewritten in Rust.

I have a script to use xidlehook and i3lock(i3lock-color, i3lock-fancy…) to auto lock screen.

Click to see the script

#!/bin/bash

# Only exported variables can be used within the timer's command.
export PRIMARY_DISPLAY="$(xrandr | awk '/ primary/{print $1}')"

# Run xidlehook
xidlehook \
  `# Don't lock when there's a fullscreen application` \
  --not-when-fullscreen \
  `# Don't lock when there's audio playing` \
  --not-when-audio \
  `# Dim the screen after 300 seconds, undim if user becomes active` \
  --timer 300 \
    'xrandr --output "$PRIMARY_DISPLAY" --brightness .1' \
    'xrandr --output "$PRIMARY_DISPLAY" --brightness 1' \
  `# Undim & lock after 10 more seconds` \
  --timer 10 \
    'xrandr --output "$PRIMARY_DISPLAY" --brightness 1; i3lock' \
	#                                                     │
    #                                                     └───  Replace this command with you lockscreen program or script
    '' \
  `# Finally, suspend an hour after it locks` \
  --timer 1200 \
    'systemctl suspend' \
    ''

Or, execute xset to specify autolock interval. For example, I am using xset cooperate with xsecurelock, the config of i3 is like this:

exec xset s 300 5
exec xss-lock -n /usr/lib/xsecurelock/dimmer -l -- xsecurelock

This combo is the simplest method to auto lock screen in i3wm, and is also the method I am using currently. The only thing you should to do is installing xsecurelock, and put previous two line configurations in you i3 config file.

Some deeper manipulation, such as the options of XSecurelock, or system suspend and hibernate, is not contained in this article.

再读《三国演义》

Git checkout 的两个替代命令