Skip to content

Instantly share code, notes, and snippets.

@johanwiden
Last active December 20, 2023 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanwiden/900723175c1717a72442f00b49b5060c to your computer and use it in GitHub Desktop.
Save johanwiden/900723175c1717a72442f00b49b5060c to your computer and use it in GitHub Desktop.
How to be warned before Sway locks the screen

Sway Screenlock Warning

When using the Sway window manager: How to be warned before inactivity timeout locks the screen.

I first describe a setup that works with the official swaylock. This displays a notification for some seconds before the screen is locked.

I then describe a setup that requires installation of a fork of swaylock. This warns by gradually dimming the screen before it is locked. This can of course be combined with the warning notification.

Warn by showing a notification

In the sway config file, enable locking of screen on inactivity timeout:

exec swayidle -w \
         timeout 1170 'notify-send --app-name=screenlockwarning "Screen will lock in 30 seconds"' \
         timeout 1200 'swaylock -f -c 000000' \
         timeout 1500 'swaymsg "output * dpms off"' resume 'swaymsg "output * dpms on"' \
         before-sleep 'swaylock -f -c 000000'

This will lock the screen after 1200 seconds of inactivity, and turn off the display after an additional 300 seconds of inactivity.

Thirty seconds before the screen is to be locked, the command notify-send is executed to display a warning.

The command notify-send is in the libnotify-bin package in Ubuntu. In archlinux the package is called libnotify.

I use the mako notification daemon to display my notifications. mako is available on github, and also has a package in e.g. Ubuntu. mako seems to be well maintained, and has so far been able to do everything I expect of a notification daemon.

Here is my current config file for mako (~/.config/mako/config):

sort=-time
layer=overlay
background-color=#2e3440
width=300
height=250
border-size=2
border-color=#88c0d0
#border-radius=15
icons=0
max-icon-size=64
#default-timeout=5000
#ignore-timeout=1
font=monospace 8

[urgency=low]
border-color=#cccccc

[urgency=normal]
border-color=#d08770

[urgency=high]
border-color=#bf616a
default-timeout=0

[category=mpd]
default-timeout=2000
group-by=category

[app-name=screenlockwarning]
anchor=center
font=monospace 16
default-timeout=30000

[app-name="NetworkManager Applet"]
default-timeout=5000

As regards the screenlock warning, this is configured in this section:

[app-name=screenlockwarning]
anchor=center
font=monospace 16
default-timeout=30000

The section overrides the default notification settings, for notifications with app-name set to screenlockwarning. The notification is displayed at the center of the screen, and uses a rather large font, to make it clearly visible. The default-timeout setting removes the notification after thirty seconds. One can also remove the notification by clicking on it.

Warn by dimming the screen

Now for the alternative version of swaylock, that warns by dimming the screen.

In the sway config file, enable locking of screen on inactivity timeout:

exec swayidle -w \
         timeout 1200 '/usr/local/bin/swaylock -f --grace 10 --fade-in 8' \
         timeout 1500 'swaymsg "output * dpms off"' resume 'swaymsg "output * dpms on"' \
         before-sleep 'swaylock -f -c 000000'

This requires installation of https://github.com/mortie/swaylock-effects. I built from source as there were no package for Ubuntu. There seems to be packages for a number of other deistros though.

Note: I also tried a different fork https://github.com/blastrock/swaylock-effects-second, at commit da4efb5, but the fade-in did not appear to work there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment