3

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.

2
+50

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.

  • I'm not entirely sure how much of this works with my system (for example, I don't have xfce4-session installed and thus I don't have xflock4). Nevertheless, I think this answer shows me the right path and indeed is a (C|c)anonical solution, so I'll award the bounty even if I don't now have the time to test everything. – JiK Mar 14 '16 at 10:22
  • Thanks for it, but I don't feel I totally deserve this if I didn't solve your problem. If you know which screen lock you have installed, replace xflock4 with the command needed to invoke it and try that. Do you have XScreenSaver? – Jeffrey Lebowski Mar 14 '16 at 16:57
  • Finally I tried this, and it seems to work. (With some adjustments: I'm using gnome-screensaver so the lock command is gnome-screensaver-command --lock, the lid status file name is /proc/acpi/button/lid/LID0/state (so an extra 0 there), and I'm using XMonad's startupHook to run the script at startup.) – JiK Oct 24 '16 at 22:32
0

I'm not on my laptop at the moment, but check to be sure that Power Manager --> Battery (and AC) --> Monitor --> "Put display to sleep when computer is inactive for" and "Switch off display when computer is inactive for" are not set to "Never". The number doesn't matter, but "Never" will override the lid closed option.

Also, make sure that any applications you have running don't disable the screen's powersaver. Media players and games often have options to prevent the screen from shutting off while they're running.

  • The options you mention are not set to "Never". I don't think I have any applications running that would influence this. The screen indeed gets switched off when I close the lid if I have selected "Switch off display" so there's no application that would prevent that, and the screensaver starts if I have selected "Lock Screen" so there's no application that would prevent that one either. – JiK Mar 7 '16 at 13:46
0

xflock4 may invoke different screensavers depending on which ones are installed on your system: see /usr/bin/xflock4.

Here, I have xscreensaver, which is the preferred option in xflock4. It can be configured by xscreensaver-command -prefs, where you can set the screen to be switched off after a delay of your choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.