How to lock X before going to sleep?

To get a few things out of the way:
I'm running FreeBSD 13.2-p4 on a Lenovo ThinkPad T590.
I have sleep working wonderfully, zero issues, whatsoever.
I also use Xscreensaver as a locking utility for X.
How can I set it up so that the screen locks THEN goes to sleep?
I believe I heard somewhere that this may be an issue that can be resolved with dbus?

My current way of locking my laptop is, well, locking it then closing the lid. When I open the lid, I'm presented with usually a blank screen but after pressing any button on the keyboard, the Xscreensaver login prompt pops up and I can log back in.
 
In /etc/devd.conf you can see:
Code:
....
notify 10 {
        match "system"          "ACPI";
        match "subsystem"       "Suspend";
        action "/etc/rc.suspend acpi $notify";
};
notify 10 {
        match "system"          "ACPI";
        match "subsystem"       "Resume";
        action "/etc/rc.resume acpi $notify";
};
....
You can add another action to /etc/rc.suspend and/or /etc/rc.resume.
 
OP: my rereading of the first post, it sounds like the procedure on resume is fine, but you'd like to have:
When I close the lid, I want to have it automatically lock screen then sleep.

Is that a reasonable understanding?
 
I've looked at those suggestions before but they don't work as root doesn't have an X session, so it's incapable of locking my session.
Well, you just need to provide the $DISPLAY:
Code:
setenv DISPLAY :0
su -m -l <your_user_name> -c <command>

Also, keep in mind, that for security you want to disable switching to console by Ctrl-Alt-Fx by adding to /etc/X11/xorg.conf:
Code:
Section "ServerFlags"
    Option "DontVTSwitch" "on"
EndSection
 
OP: my rereading of the first post, it sounds like the procedure on resume is fine, but you'd like to have:
When I close the lid, I want to have it automatically lock screen then sleep.
Is that a reasonable understanding?
Exactly!

Does xscreensaver-command --lock work as intended? If so, then you could easily link that to a short-cut. Alternatively, it could also be possible to link this to the appropriate event that signals the closing of the lid (edit: the locking should be executed before the "sleep-process" is started); unfortunately I don't know how to do that.
 
  • Like
Reactions: mer
A bit hacky but perhaps write a script, started from your .xinitrc / .xsession that runs in the background and keeps checking the seconds from epoch.

If it jumps more than i.e 5 seconds (resume from suspend can be assumed), then get it to launch the screen saver program.

Since it runs as your user session, it should have all the Xauthority/DISPLAY, etc in place.

OpenBSD's Xenocara comes with xidle(1). You might be able to install / utilize this rather than rely entirely on a custom script.
 
Here's my rough notes on locking before sleep. I don't have auto-sleep on lid close... but sleep does lock first:

xfce4 settings -> session and startup -> enable "lock screen before sleep"
need to use the xfce4 menu suspend rather than doas zzz

Activate stop / suspend options​

Code:
polkit.addRule(function(action, subject) {
if((action.id == "org.freedesktop.consolekit.system.restart" || action.id == "org.freedesktop.consolekit.system.stop" || action.id == "org.freedesktop.consolekit.system.suspend") && subject.isInGroup("operator")) {
return polkit.Result.YES;
}
});
 
My laptop sleeps and resumes just fine, I want X to lock before going to sleep.
I've looked at those suggestions before but they don't work as root doesn't have an X session, so it's incapable of locking my session.
Well, you just need to provide the $DISPLAY:
Code:
setenv DISPLAY :0
su -m -l <your_user_name> -c <command>

-l and -m are mutually exclusive, so it needs (assuming running as root from /etc/rc.suspend, so in sh not csh) more like:

DISPLAY=":0" ; export DISPLAY
su -m <user> -c <command>

But best after checking that X is actually running, something like:

ps | grep /[u]sr/local/bin/X
if [ $? -eq 0 ] then
<the above su -c command>
sleep 1 # if needed?
fi

[note: square brackets in grep command avoid matching on the grep process itself]

Does xscreensaver-command --lock work as intended? If so, then you could easily link that to a short-cut. Alternatively, it could also be possible to link this to the appropriate event that signals the closing of the lid (edit: the locking should be executed before the "sleep-process" is started); unfortunately I don't know how to do that.

Suspend always goes through /etc/rc.suspend, and this needs to happen where the examples are listed, before the logger, sync & sleep commands.

This point is actually partway through the suspend process.

PS: I can't test this currently; I'm assuming the su with command will exit once that command finishes. If not, add " ; exit" to the invoked command.
 
Back
Top