UNPKG

3.78 kBapplication/x-shView Raw
1#!/bin/bash
2
3export DISPLAY=:0
4
5tvservice=`which tvservice >/dev/null ; echo $?`
6vcgencmd=`which vcgencmd >/dev/null ; echo $?`
7
8case "$1" in
9 root-on)
10 if [ $tvservice = 0 ]; then
11 # must be run in sudo mode
12 /opt/vc/bin/tvservice --preferred || echo "no tvservice"
13 # Hack to enable virtual terminal nr 7 again:
14 chvt 6
15 chvt 7
16 sleep 1
17 fi
18 ;;
19 on)
20 # power on
21 if [ $vcgencmd = 0 ]; then
22 # https://raspberrypi.stackexchange.com/questions/52042/turning-tvservice-on-and-off-leaves-screen-blank
23 vcgencmd display_power 1 || echo "no vcgencmd"
24 else
25 for screen in `xrandr | awk '/ connected/{print $1}'`; do
26 xrandr --output "$screen" --auto
27 done
28 fi
29
30 # Force screen on
31 xset dpms force on
32
33 # CEC
34 echo "on 0" | cec-client -s > /dev/null
35 NOT_CEC_COMPATIBLE=$?
36 if [ $NOT_CEC_COMPATIBLE -ne 0 ]; then
37 ( sleep 5 ; echo "as" | cec-client -s > /dev/null ) &
38 fi
39 ;;
40
41 off)
42 # CEC
43 echo "standby 0" | cec-client -s > /dev/null
44
45 # force screen off
46 xset dpms force off
47
48 # power off
49 if [ $vcgencmd = 0 ]; then
50 vcgencmd display_power 0 || echo "no vcgencmd"
51 else
52 for screen in `xrandr | awk '/ connected/{print $1}'`; do
53 xrandr --output "$screen" --off
54 done
55 fi
56 ;;
57 state)
58 powerstate=`vcgencmd display_power 2>&1`
59 # if [[ $? != 0 ]] || [[ $tvstate == *"Failed to connect"* ]] ; then
60 if [[ $? != 0 ]] ; then
61 listmonitors=`xrandr --listmonitors`
62 if [[ $? != 0 ]]; then
63 echo 'unavailable'
64 elif [[ $listmonitors == "Monitors: 0" ]] ; then
65 echo 'off'
66 elif [[ $listmonitors == *"Monitors:"* ]] ; then
67 echo 'on'
68 else
69 echo 'unavailable'
70 fi
71 elif [[ $powerstate == "display_power=0" ]] ; then
72 echo 'off'
73 else
74 echo 'on'
75 fi
76 ;;
77 subscribe)
78 /opt/vc/bin/tvservice -M
79 ;;
80 screens)
81 xrandr | awk '/ connected/{print $1}'
82 ;;
83 screens-on)
84 for screen in ${@:2}; do
85 xrandr --output "$screen" --auto
86 done
87 ;;
88 screens-off)
89 for screen in ${@:2}; do
90 xrandr --output "$screen" --off
91 done
92 ;;
93 screen-off)
94 xrandr --output "$2" --off
95 ;;
96 screen-on)
97 xrandr --output "$2" --auto ${@:3}
98 ;;
99 reverse-screen)
100 xrandr --output "$2" --auto --rotate inverted
101 if [ -z "$3" ]; then
102 echo "No xinput device provided"
103 else
104 xinput set-prop "$3" "Coordinate Transformation Matrix" -1 0 1 0 -1 1 0 0 1
105 fi
106 ;;
107 displays)
108 ps -u $(id -u) -o pid= | \
109 while read pid; do
110 cat /proc/$pid/environ 2>/dev/null | tr '\0' '\n' | grep '^DISPLAY=:'
111 done | grep -o ':[0-9]*' | sort -u
112 ;;
113 screenshot)
114 if [ "$BOARD_ID" = 'rpi' ]; then
115 nice -19 raspi2png --stdout -h 100
116 else
117 nice -19 scrot -t 20 -q 50 /tmp/screenshot.jpg || exit 1
118 rm /tmp/screenshot.jpg
119 cat /tmp/screenshot-thumb.jpg
120 fi
121 ;;
122 waitForX)
123 MAX=120 # About 60 seconds
124 CT=0
125 while ! xdpyinfo >/dev/null 2>&1; do
126 sleep 0.50s
127 CT=$(( CT + 1 ))
128 if [ "$CT" -ge "$MAX" ]; then
129 echo "FATAL: $0: Gave up waiting for X server $DISPLAY"
130 exit 1
131 fi
132 done
133 ;;
134 *)
135 echo "Usage screen.sh {on|off|state|subscribe|screenshot|waitForX}"
136 exit 1
137 ;;
138esac
139
140exit 0