-
Notifications
You must be signed in to change notification settings - Fork 8
/
screen-toggle
executable file
·60 lines (51 loc) · 1.89 KB
/
screen-toggle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/sh
usage(){
fmt <<EOF
DESCRIPTION
Toggle the display on or off.
USAGE
screen-toggle
EOF
exit
}
die(){ printf "Error: %s\n" "${1}" 1>&2; exit 1; }
require(){ command -v "${1}" > /dev/null 2>&1 || { suggestion=""; if [ ! -z "$2" ]; then suggestion=" $2"; fi; die "$1 is not installed.${suggestion}"; } }
if [ $# -eq 1 ]; then if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then usage; fi fi
# End boilerplate
sessionType=$(loginctl show-session "$(loginctl |grep "$(whoami)" |awk '{print $1}')" -p Type | sed "s/Type=//")
if [ "${sessionType}" = "wayland" ]; then
screenState=$(busctl --user get-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode)
if [ "${screenState}" = "i 0" ]; then
# Turn screen off
busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 1
echo 1 | sudo tee /sys/class/leds/tpacpi::power/brightness > /dev/null
else
# Turn screen on
busctl --user set-property org.gnome.Mutter.DisplayConfig /org/gnome/Mutter/DisplayConfig org.gnome.Mutter.DisplayConfig PowerSaveMode i 0
echo 0 | sudo tee /sys/class/leds/tpacpi::power/brightness > /dev/null
fi
else
screenOffLockFile="/tmp/screen-toggle-lock"
if [ ! -f "${screenOffLockFile}" ]; then
# Turn the screen off. Because keyboard/mouse input will turn the screen on again,
# do this in a loop that sets the screen off every .5 seconds.
touch "${screenOffLockFile}"
echo 1 | sudo tee /sys/class/leds/tpacpi::power/brightness > /dev/null
while :
do
if ! xset q | grep --quiet "Monitor is Off"; then
if [ -f ${screenOffLockFile} ]; then
xset dpms force off
else
xset dpms force on
exit
fi
fi
sleep .5
done
else
echo 0 | sudo tee /sys/class/leds/tpacpi::power/brightness > /dev/null
rm "${screenOffLockFile}"
xset dpms force on
fi
fi