updated on: 2020-02-21

When you close the lid on your laptop (using Manjaro i3), the laptop goes to suspend mode. But the screen doesn’t lock.

logind.conf

Go to /etc/systemd/logind.conf. You’ll find a configuration option there:

#HandleLidSwitch=suspend

The commented out lines show you the default behavior. So, suspend on lid close should already work. If not, adjust to your liking.

See Power Management with systemd for more information.

i3 Configuration

Let’s see what’s inside the i3 configuration file. On my machine, I can find the file at ~/.i3/config.

Excerpt:

## Set shut down, restart and locking features
bindsym $mod+0 mode "$mode_system"
set $mode_system (l)ock, (e)xit, switch_(u)ser, (s)uspend, (h)ibernate, (r)eboot, (Shift+s)hutdown
mode "$mode_system" {
    bindsym l exec --no-startup-id i3exit lock, mode "default"
    bindsym s exec --no-startup-id i3exit suspend, mode "default"
    bindsym u exec --no-startup-id i3exit switch_user, mode "default"
    bindsym e exec --no-startup-id i3exit logout, mode "default"
    bindsym h exec --no-startup-id i3exit hibernate, mode "default"
    bindsym r exec --no-startup-id i3exit reboot, mode "default"
    bindsym Shift+s exec --no-startup-id i3exit shutdown, mode "default"

    ## exit system mode: "Enter" or "Escape"
    bindsym Return mode "default"
    bindsym Escape mode "default"
}

As you can see, i3 delegates to the i3exit script.

which i3exit
> /usr/bin/i3exit

i3exit

You can edit the file with sudo privileges:

#!/bin/sh
lock() {
    i3lock
}

case "$1" in
    lock)
        lock
        ;;
    logout)
        i3-msg exit
        ;;
    suspend)
        lock && systemctl suspend
        ;;
    hibernate)
        lock && systemctl hibernate
        ;;
    reboot)
        systemctl reboot
        ;;
    shutdown)
        systemctl poweroff
        ;;
    *)
        echo "Usage: $0 {lock|logout|suspend|hibernate|reboot|shutdown}"
        exit 2
esac

exit 0

If you want to use a different lockscreen utility (blurlock or betterlockscreen), you can change that in this file.

You also might want to change the order of lock and suspend.

In my case, the screen showed the lockscreen first. I had to enter my passcode in order to switch to suspend mode. That’s annoying.

Let’s see how we can adjust the script so use betterlockscreen.

betterlockscreen

betterlockscreen is a minimal, customizable lockscreen. On Arch, you can install it via the AUR (for example, with yay:

yay -S betterlockscreen

You don’t need a systemd sleep hock.

Instead, adjust your /usr/bin/i3exit script:

#!/bin/sh
## /usr/bin/i3exit

## with openrc use loginctl
[[ $(cat /proc/1/comm) == "systemd" ]] && logind=systemctl || logind=loginctl

lock() {
	betterlockscreen -l dimblur
}

case "$1" in
    lock)
        lock
        ;;
    logout)
        i3-msg exit
        ;;
    switch_user)
        dm-tool switch-to-greeter
        ;;
    suspend)
        $logind suspend && lock
        ;;
    hibernate)
        $logind hibernate && lock
        ;;
    reboot)
        $logind reboot
        ;;
    shutdown)
        $logind poweroff
        ;;
    *)
        echo "== ! i3exit: missing or invalid argument ! =="
        echo "Try again with: lock | logout | switch_user | suspend | hibernate | reboot | shutdown"
        exit 2
esac

exit 0

For Manjaro i3 community edition, you also have to change /usr/local/bin/xflock4.

Further Reading