5

xset dpms force off doesn't work, outputting:

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  147 (DPMS)
  Minor opcode of failed request:  6 (DPMSForceLevel)
  Serial number of failed request:  12
  Current serial number in output stream:  14

I have two monitors that have power buttons on them, with default power management settings. However, pressing one keyboard shortcut using a command would be handy.

I'm pretty sure I'm using Gnome and read that xset doesn't work with Gnome.

As an alternative solution I'll just change the screen timeout to 1 minute.

Related:

James Ray
  • 393

5 Answers5

4

This script works on my Ubuntu 17.10

#!/bin/bash

busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 1
read -n 1 -s -r -p "Press any key continue"
busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 0
Gonki
  • 163
  • I guess that this would work with something like autohotkey, but how would it be enabled in keyboard shortcuts?

    I tried entering #!/bin/bash;busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 1;read -n 1 -s -r -p "Press any key continue";busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 0 in as a command with a shortcut but it doesn't work.

    – James Ray Dec 27 '17 at 03:25
  • Could you create a .sh file with the above commands and access that file with a shortcut? – Gonki Dec 27 '17 at 18:40
  • Thanks, I worked it out. I just copied and pasted the commands into the file turnthescreenoff.sh which I saved in my home directory, then entered bash /home/james/turnthescreenoff.sh as a keyboard shortcut. – James Ray Dec 29 '17 at 04:06
  • Watch out: if anything pops up that covers the terminal, you need to go back to it with a dark screen – Pietro Battiston Feb 08 '20 at 12:15
1

Ubuntu 17.10 runs a Wayland session by default, click the gear icon at login and choose a Xorg session instead if you need commands like xset. A common rule of thumb is that commands starting with x only run in Xorg. As far as I know Wayland doesn't provide a way to turn off the screen yet. This may very well change in the future though.

dessert
  • 39,982
1

Update 23 Mar 2018: alternatively you can set the power button to suspend rather than turn off. 24 Mar: however if you suspend it then the execution of all processes will be halted, which may not be desirable.

I entered the following commands made by @Gonki into the file turnthescreenoff.sh which I saved in my home directory, then entered bash /home/james/turnthescreenoff.sh as a keyboard shortcut.

#!/bin/bash

busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 1
read -n 1 -s -r -p "Press any key continue"
busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 0
James Ray
  • 393
  • It didn't work for me : when I try to set a shortcut, it turns off the screen and immediately after, turns it on again. I guess it is due to the fact that the shortcut both runs the script and is seen by the script as the signal to turn on again the screen... So I even tried to add sleep 1s but it did not work. – Johannes Lemonde Jan 03 '18 at 19:24
  • OK, yes that happens to me too, I guess that I didn't wait long enough. – James Ray Jan 04 '18 at 03:08
  • It's not meant as a complete solution. I will wait for next Ubuntu where maybe it will be worked out. – Gonki Jan 14 '18 at 00:58
  • Fair enough, we will have to wait and use the power buttons or let power saver do the job in the meantime. – James Ray Jan 15 '18 at 23:53
  • Watch out: if anything pops up that covers the terminal, you need to go back to it with a dark screen – Pietro Battiston Feb 08 '20 at 12:16
0

I took inspiration from Gonki and James Ray's answers and wrote a (python) version which runs without a shell:

#! /usr/bin/python3
import subprocess

template = ('busctl --user {what}-property org.gnome.Mutter.DisplayConfig '
            '/org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig '
            'PowerSaveMode')

current = subprocess.check_output(template.format(what='get').split()).strip()
try:
    if current == b'i 0':
        subprocess.call(template.format(what='set').split() + ['i', '1'])
    else:
        subprocess.call(template.format(what='set').split() + ['i', '0'])
except:
    # If anything goes wrong, turn on the light:
    subprocess.call(template.format(what='set').split() + ['i', '0'])

It should solve the problem that Johannes Lemonde reported (that the screen would immediately turn on again), and hence will work with any hotkey: just press the same hotkey again to turn the screen on.

0

Here's a script in bash similar to Pietro Battiston's answer ( I would rather have added a comment but I can't ) :

#!/bin/bash
p="org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode"

[ $(busctl --user get-property $p | cut -d ' ' -f 2) -eq 1 ] && s=0 || s=1

busctl --user set-property $p i $s

Each call of the script toogle the screen on or off.