Screen Locking and Clipboard Clearing

I recently setup a new, minimal install of Debian on my personal laptop (separate blog post to follow?) and am therefore needing to configure things (like my screen locker) in a much more manual, hands-on way. It’s a great experience - I’m learning more about how my computer works, and how programs interact with each other and form a (relatively) cohesive whole.

Lock Screen

I installed i3lock as my screen locker. It’ll lock my screen, show a wallpaper of my choice, and require a password to unlock again. But it doesn’t do so automatically. For that I needed xautolock . It runs a program (like a screen locker) after a certain amount of inactivity. So the following command (run automatically via .xprofile) works quite nicely:

xautolock -secure -time 5 -locker "i3lock -i /PATH/TO/WALLPAPER.png" &

Clearing The Clipboard

Separately to all this, I started thinking about how best to automatically clear my clipboard (for privacy/security). Password managers do this by starting a timer when you copy a password, then clearing the clipboard when the timer ends. I wanted a similar but generic solution - basically a program to monitor when the clipboard was updated, then clear it after a certain time.

Unfortunately I couldn’t find anything like this, but found suggestions online that you could just have an automation (via cron I believe) that cleared the clipboard every few minutes. The obvious problem with that is if I were to copy something right before the automation ran, meaning the clipboard would be cleared before I got to paste it somewhere.

Then, when I was reading up on xautolock, I discovered that it isn’t specific to screen lockers - it’ll let you specify any program to run after a certain time. I figured I could use it to clear my clipboard after a certain period of inactivity.

Apparently you can’t run multiple instances of xautolock though, so since I was already using it to lock my screen, I needed to write a simple bash script that would clear the clipboard and lock my screen, then call this script via xautolock after 5 minutes.

My .xprofile command became:

xautolock -secure -time 5 -locker "~/.local/bin/lock-screen.sh" &

and lock-screen.sh was as follows:

#! /bin/bash

# Clear all clipboards.
xclip -selection primary /dev/null
xclip -selection secondary /dev/null
xclip -selection clipboard /dev/null

# Lock screen.
i3lock -i /PATH/TO/WALLPAPER.png

Now after 5 minutes, my clipboards are cleared and then my screen is locked!

Watching Videos

This was all working great, until I was in the middle of watching a video when the screen locked. So now I needed to find a way to prevent the screen from locking while I’m watching videos…

Initial searches online suggested checking if the current application is in fullscreen mode (and not locking the screen if so). However I often watch videos non-fullscreen, so that wouldn’t work for me. Then I saw the suggestion to check if any audio is playing (since most videos you watch have audio too), however I often have music playing in the background while I work (via Spotify) and I don’t want my screen to stay unlocked just because there’s music playing.

I considered my specific requirements and worked out that I want to prevent the screen from locking when any audio other than Spotify music is playing. I then had the idea that if I were to pause Spotify after 5 mins of inactivity, then the check for audio playing would work as expected (any other audio still playing would prevent the screen locking, and no other audio would mean it can lock).

I found some code to pause Spotify, a check to see if there’s audio playing, and added them to my lock script. I also needed to add sleep 5 as the audio playing check takes a few seconds to update after Spotify stops. Now everything works nicely with no interuptions to my video watching!

My updated lock-screen.sh is:

#! /bin/bash

# Clear all clipboards.
xclip -selection primary /dev/null
xclip -selection secondary /dev/null
xclip -selection clipboard /dev/null

# Pause Spotify.
dbus-send --type=method_call --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause
sleep 5

# Lock screen.
if [ $(grep -r "RUNNING" /proc/asound | wc -l) -eq 0 ]; then
  i3lock -i /PATH/TO/WALLPAPER.png
fi