i3lock with xautolock corners
Tags: i3, tutorial, devjournal, tooling
Published: 2022-08-10 12:14:00

Here is my first post on dev.to, so please be kind and indulgent.

## A bit more about the context:
I was struggling lately with my screensaver set-up. I was often facing screen saver while being in web conf.

It happens when you simply listen or talk, because you are not using the mouse or keyboard to keep the session "active".

## Initial setup

Here was my setup, a very basic set-up, as per documented on [Arch Linux wiki](https://wiki.archlinux.org/title/I3#Screensaver_and_power_management
).

```
xautolock -time 10 -locker "i3lock -i background_image.png"
```

so in the i3 conf, it looks like this

```
exec_always --no-startup-id xautolock -time 5 -locker "~/bin/lockscreen.sh" -detectsleep
```

where `~/bin/lockscreen.sh` is a simple script that locks the screen by calling `i3lock`.

```shell
#!/bin/sh

set -e

# enable the lock screen
i3lock -c 272d2d -i ~/Documents/wallpaper-lockscreen.png
```

Why a script ? because my configuration is way more complicated than that, and I simplified the code for sharing it with you.

OK so we have `xautolock` that calls `i3lock`.

The `--detectsleep` parameter is for telling i3lock to do not activate when you are closing the lid.

```
       -detectsleep    Instructs  xautolock  to detect that computer has been put to sleep.  This is done by detecting that time has jumped by more than 3 seconds.  When this occurs, the lock timer is reset
                       and locker program is not launched even if primary timeout has been reached. This  option is typically used to prevent the locker program from being launched  when  awaking  a  laptop
                       computer.
```

## How I solve my issue

Well, no surprise: RTFM technique

I read the man page ... and found `xautolock` has corners feature. Here is an extract of the documentation:

```
 You can tell xautolock to take special actions when you move the mouse into one of the corners of the display and leave it there, by using the -corners, -cornerdelay, -cornerredelay  and  -cornersize
       options. This works as follows:

       The xxxx argument to the -corners option must consist of exactly 4 characters from the following set: '0', '+', '-'. Each one of these specifies what xautolock should do when the mouse enters a small
       square area located in each of the corners of the screen. The corners are considered in the following order: top left, top right, bottom left, bottom right.  A '0' indicates that xautolock should ig‐
       nore  the  corner. A '+' indicates that xautolock should start the locker after secs or altsecs seconds (see below for the difference between both), unless the mouse is moved or keyboard input is re‐
       ceived. A '-' indicates that xautolock should not start the locker at all. The pixels argument specifies the size in pixels of the corner areas.

       Most users of the -corners option want the locker to activate within a very short time interval after they move the mouse into a '+' corner.  This can be achieved by specifying a small value for  the
       -cornerdelay option. However, if the mouse is subsequently left where it is, xautolock will almost immediately start a new locker right after the user quits the current one. To prevent this from hap‐
       pening, the -cornerredelay option can be used to specify the time-out interval to be used if and only if the mouse is sitting in a `+' corner and has not been moved since the previous locker exited
```

So you simply add something like --corners '++--' to make `xautolock` to behave in a way that suited my needs

```shell
xautolock -time 5 -locker "~/bin/lockscreen.sh" -detectsleep -corners '++--' -cornerdelay 5
```

Tips: please make sure to kill `xautolock` before reloading i3 config, otherwise it will change nothing.

So here, the locker will be called if I put my mouse in one of the two top corners for at least 5 seconds.

The `--` part of the `++--` tells `xautolock` to do not lock the screen when I put the mouse in these corner, which is useful when you are watching a video in full screen or attending a web meeting.

## Further changes

OK, so it's good, but it was not good enough, because I was not able to know the screen is about to lock.

So, here I simply used the `--notify` parameter

Here again, take a look at the man page

```

       -notify         Warn the user margin seconds before locking. The default is to not warn the user. If used in conjunction with -cornerdelay or -cornerredelay, the notification margin iused is the min‐
                       imum of margin, secs and/or altsecs.

       -notifier       Specifies  the  notifier to be used. The default is none. This option is only useful in conjunction with -notify. Notice that if notifier contains multiple words, it must be specified
                       between quotes.
```

So I created a script `~/bin/lockscreen-notify.sh` and added it to my `xautolock`

More information on the page dedicated to the [lockscreen notify script](https://dev.to/ccoveille/lock-screen-notifier-4o1h)