How can I delay (but not disable) the screen lock on suspend/lid close?

I would like my screen to lock automatically when I close the lid (or otherwise trigger a suspend), but only if I leave it closed for a certain amount of time, e.g. 5 minutes. That is, if I close the lid and then open it 1 minute later, I'd like the screen to not be locked, but if I open the lid 10 minutes later, it should be locked. Is there a way to set this up in Ubuntu (GNOME)?

I see several similar questions, but none that describe what I'm looking for. I still want the laptop to suspend immediately when the lid closes, I just don't want it to lock unless the lid stays closed for a certain amount of time. (Ideally it would obey the same "Automatic Screen Lock Delay" setting that is used while the lid is open.)

One way I could imagine implementing this is to run scripts at suspend and wake. The suspend script would just record a timestamp for the suspend, and the wake script would check to see how old the timestamp is and decide based on that whether to lock or unlock the screen. However, I assume I would have to set up these scripts to run as my user (i.e. not root) and they would need to be able to access my existing login session (presumably via some environment variables?), so I'm not if this is possible to implement.


Solution 1:

I figured out how to do this. I wrote a Python script that listens for the DBus signals that indicate suspending and locking the screen. When both signals occur within a short time of each other, the script assumes that this is a suspend event that triggered a screen lock event. It then waits for the signal indicating a resume. If the elapsed time since the suspend event is less than 5 minutes, it forces the screensaver to unlock without requiring a password. In addition, it only unlocks the screen if the lid was closed when the suspend signal was sent (thus presumably indicating a suspend that was triggered by the lid closing), so manually suspending from the menu will not trigger the script. This ensures that the screen will not unlock after being manually locked by the user.

You can find the script here: https://gist.github.com/DarwinAwardWinner/77e8acea2f14ed9ea66d7222d7ace500

I saved the script in ~/.local/bin/, made it executable, and set the script up to run in the background when I log in using the Startup Applications configuration program, as shown here:

Startup Application Entry for shortsleep-unlock.py

I also used run-one-constantly from the run-one package to automatically restart the script if it ever crashes for any reason. With this script running, I get exactly the behavior I want: if I close the lid and then open it a minute later, the screen unlocks without me having to type my password. But if I leave the lid closed for more than 5 minutes, or if I suspend the laptop without closing the lid, then it requires a password when I open the lid. You can configure both the time limit and the lid requirement by editing the script (look around line 100).

In its current form, the script only works with the GNOME screensaver. However, it should not be terribly difficult to adapt it to KDE or other screen locking programs, as long as they provide the appropriate DBus signals and methods.

Important security note: I've done my best to handle all edge cases I can think of, but there's always the possibility that I failed to anticipate some way of triggering the unlock even after the time limit has expired. Therefore, don't use this script if if you have concerns about security, unless you take the time to audit it yourself.