Xfce Power Manager – Switch Off Display and Lock Screen When Laptop Lid is Closed

power-managementscreensaver

I have a Dell XPS 13 (2015 model), and I'm using Ubuntu 15.10 with Xmonad. I have xfce4-power-manager running and gnome-screensaver is used to lock the screen. In the settings dialog of xfce4-power-manager, there is a setting "When laptop lid is closed", which has three options (separately for when on battery and when plugged in): Switch off display, Suspend, and Lock Screen.

If I choose "Switch off display", the screen doesn't get locked when closing the lid. I'd like to have the screen locked when I close the lid. However, when I select "Lock Screen", the display doesn't seem to be switched off. To save battery, I'd like to switch the display off, too. What would be a good (easy and/or somewhat canonical) way to get both?

The question
Lock screen after blanking with gnome-screensaver and XFCE
may be related, but it doesn't have answers.

Best Answer

I don't think it is possible to do that in XFCE power manager. However, you can make a script to both lock screen and turn it off, and make it so that it is launched as soon as you close your lid, overriding power manager's settings.

How to launch a custom script is reported in the official Ubuntu help page. I'll write the passages sequentially, to understand the reason of each step read the original post. Maybe it's not the easiest process around (it is pretty easy anyway, as you just have to input a series of specified commands and fill files with precise content), but I think it is the only way to go. Also, is pretty canonical, both with the lower-case and capital C, as the solution is in Ubuntu's official help.

First, create a script to make environment variables available for root, so that it can launch commands in your normal user's environment. (I put mousepad in the commands as it's the default text editor for XFCE. Replace it with GEdit, SciTE, Emacs, nano, vim or whatever editor you use)

$ mousepad ~/export_x_info

With the following content

# Export the dbus session address on startup so it can be used by any other environment
sleep 5
touch $HOME/.Xdbus
chmod 600 $HOME/.Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.Xdbus

# Export XAUTHORITY value on startup so it can be used by cron
env | grep XAUTHORITY >> $HOME/.Xdbus
echo 'export XAUTHORITY' >> $HOME/.Xdbus

And make it executable

$ chmod 700 ~/export_x_info

Add it in SettingsSession and StartupApplication autostart to Set it to run on startup.

Create /etc/acpi/events/lm_lid

# mousepad /etc/acpi/events/lm_lid

With this content

event=button/lid.*
action=/etc/acpi/lid.sh

This will execute /etc/api/lid.sh when lid button is triggered. Let's create it

# mousepad /etc/api/lid.sh

#!/bin/bash

/home/%user/lid_event

(obviously replace %user with your username)

This means the script will execute /home/%user/lid_event. Let's create it too

$ mousepad ~/lid_event

grep -q closed /proc/acpi/button/lid/LID/state
if [ $? = 0 ]
then
    /home/%user/close;
else
    /home/%user/open;
fi

This will execute ~/open if the lid is opened and ~/close if it's closed. Again, let's create them:

$ mousepad ~/close

Since to turn screen off you need xset dpms force off and to lock it you need xflock4, will write this inside close:

#This runs so that root can run the following command under the user's environment
source /home/%user/.Xdbus
#Lock and turn off screen
DISPLAY=:0.0 su %user -c "xflock4 && xset dpms force off"

Inside open, as suggested by Ubuntu's help, you can play a sound of your choice (I made a test with Barney Gumble's burp)

#This runs so that root can run the following command under the user's environment
source /home/%user/.Xdbus
#play a open sound
DISPLAY=:0.0 su %user -c "aplay %path/to/a/sound/of/your/choice"

Let's make them all executable

$ chmod +x ~/lid_event
$ chmod +x ~/open
$ chmod +x ~/close

Restart acpid

# /etc/init.d/acpid restart

Launch ~/export_x_info (or restart your laptop so that it is autolaunched on startup) and close/open your lid to see if it works.

Note: in my laptop, locking screen would also turn it off, so I couldn't 100% test the double action, but it worked in locking it. Also, the help page puts, as an example, an action to change Pidgin's status as away when you close the lid and as "I'm here" when you open it, very useful.

TL;DR (if this issue should ever occur to a lazy person or a newbie): input the commands in the gray background (# means you need elevated root privileges, or simply type sudo before the command, while $ means you should input the command as regular user). When the command invokes a text editor (i.e., mousepad) copy and paste the part in the yellow background in your editor, then save and close.

Related Question