This repository has been archived by the owner on Dec 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
set-overscan
executable file
·237 lines (200 loc) · 8.53 KB
/
set-overscan
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/bin/bash
#########################################################################
# set_overscan.sh v0.11
# Modify overscan on the fly.
# By Russell "ukscone" Davis using code from Broadcom, Dom Cobley & Alex Bradbury
# 2013-03-10, 2014-07-23, 2015-05-01, 2017-08-01
#
# There is very little, ok no error/sanity checking. I've left that as an exercise
# for the reader :D This is a very simplistic script but it works and i'm sure someone
# will take it and make it all flashy and cool looking.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions, and the following disclaimer,
# without modification.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. The names of the above-listed copyright holders may not be used
# to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
CONFIG=/boot/config.txt
#########################################################################
# Function #
#########################################################################
get_kc ()
{
od -t o1 | awk '{ for (i=2; i<=NF; i++)
printf("%s%s", i==2 ? "" : " ", $i)
exit }'
}
set_config_var() {
lua - "$1" "$2" "$3" <<EOF > "$3.bak"
local key=assert(arg[1])
local value=assert(arg[2])
local fn=assert(arg[3])
local file=assert(io.open(fn))
local made_change=false
for line in file:lines() do
if line:match("^#?%s*"..key.."=.*$") then
line=key.."="..value
made_change=true
end
print(line)
end
if not made_change then
print(key.."="..value)
end
EOF
mv "$3.bak" "$3"
}
#########################################################################
# Check we are root or using sudo otherwise why bother? #
#########################################################################
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
echo "This script must be run by root or using sudo" 1>&2
exit 1
fi
#########################################################################
# Is overscan enabled? If not the fix it & reboot #
#########################################################################
if [ "$(vcgencmd get_config disable_overscan | awk -F '=' '{print $2}')" -eq "1" ]; then
set_config_var disable_overscan 0 $CONFIG
whiptail --msgbox "disable_overscan=0 added to config.txt, overscan will be enabled on next reboot. reboot & then rerun this script." 10 45
exit 1
fi
#########################################################################
# Variables & Constants & create some files #
#########################################################################
# Grab terminal capabilities
tty_cuu1=$(tput cuu1 2>&1 | get_kc) # up arrow
tty_kcuu1=$(tput kcuu1 2>&1 | get_kc)
tty_cud1=$(tput cud1 2>&1 | get_kc) # down arrow
tty_kcud1=$(tput kcud1 2>&1 | get_kc)
tty_cub1=$(tput cub1 2>&1 | get_kc) # left arrow
tty_kcub1=$(tput kcud1 2>&1 | get_kc)
tty_cuf1=$(tput cuf1 2>&1 | get_kc) # right arrow
tty_kcuf1=$(tput kcud1 2>&1 | get_kc)
# Some terminals (e.g. PuTTY) send the wrong code for certain arrow keys
if [ "$tty_cuu1" = "033 133 101" -o "$tty_kcuu1" = "033 133 101" ]; then
tty_cudx="033 133 102"
tty_cufx="033 133 103"
tty_cubx="033 133 104"
fi
# Check for mailbox & if not existing create it.
created_mailbox=0
if [ ! -c /dev/vcio ]; then
mknod /dev/vcio c 100 0
created_mailbox=1
fi
# Get current overscan values from GPU
TEMP=$(./overscan)
GPU_OVERSCAN_TOP=$(echo "$TEMP" | awk -F ' ' '{print $1}')
GPU_OVERSCAN_BOTTOM=$(echo "$TEMP" | awk -F ' ' '{print $2}')
GPU_OVERSCAN_LEFT=$(echo "$TEMP" | awk -F ' ' '{print $3}')
GPU_OVERSCAN_RIGHT=$(echo "$TEMP" | awk -F ' ' '{print $4}')
# How big is the screen?
TEMP=$(fbset | grep 'mode "' | awk -F ' ' '{print $2}' | tr \" \ )
FXRES=$(echo "$TEMP" |awk -F 'x' '{print $1}')
FYRES=$(echo "$TEMP" |awk -F 'x' '{print $2}')
BYTES=$(expr $FXRES \* $FYRES \* 4)
TXRES=$(stty size | awk -F ' ' '{print $2}')
TYRES=$(stty size | awk -F ' ' '{print $1}')
XMIDPOINT=$((TXRES/2))
YMIDPOINT=$((TYRES/2))
# Create some random data & zero'ed data
head -c $BYTES < /dev/urandom > rand
head -c $BYTES </dev/zero > cleared
########################################################################
# Main()
########################################################################
tty_save=$(stty -g)
stty cs8 -icanon -echo min 3 time 1
stty intr '' susp ''
trap 'stty $tty_save; tput cnorm ; exit' INT HUP TERM
# Going to modify top-left overscan
whiptail --title "Instructions" --msgbox "We are going to dump some random data to the screen. Once the screen is full of random coloured dots use the arrow keys to increase or decrease the top-left corner's overscan & press the q key when finished." 12 50
clear
# We don't need no cursor messing up my pretty screen
tput civis
# Dump some random data to /dev/fb0
cat rand >/dev/fb0
# Set overscan top-left corner
LOOP=1
while [ $LOOP -eq 1 ]; do
TEXT=" TOP=$GPU_OVERSCAN_TOP, LEFT=$GPU_OVERSCAN_LEFT, BOTTOM=$GPU_OVERSCAN_BOTTOM, RIGHT=$GPU_OVERSCAN_RIGHT "
LEN=$((${#TEXT}/2))
XPOS=$((XMIDPOINT-LEN))
echo -ne "\033[${YMIDPOINT};${XPOS}f$TEXT"
keypress=$(dd bs=3 count=1 2> /dev/null | get_kc)
case "$keypress" in
"$tty_cuu1"|"$tty_kcuu1") ((GPU_OVERSCAN_TOP--));;
"$tty_cud1"|"$tty_kcud1"|"$tty_cudx") ((GPU_OVERSCAN_TOP++));;
"$tty_cub1"|"$tty_kcub1"|"$tty_cubx") ((GPU_OVERSCAN_LEFT--));;
"$tty_cuf1"|"$tty_kcuf1"|"$tty_cufx") ((GPU_OVERSCAN_LEFT++));;
"161") LOOP=0;;
esac
./overscan $GPU_OVERSCAN_TOP $GPU_OVERSCAN_BOTTOM $GPU_OVERSCAN_LEFT $GPU_OVERSCAN_RIGHT
done
# Clear the screen
cat cleared >/dev/fb0
clear
# Going to modify bottom-right overscan
whiptail --title "Instructions" --msgbox "We are going to dump some random data to the screen. Once the screen is full of random coloured dots use the arrow keys to increase or decrease the bottom-right corner's overscan & press the q key when finished." 12 50
clear
# No cursor messing up my pretty screen
tput civis
# Dump some random data to /dev/fb0
cat rand >/dev/fb0
# Set overscan bottom-right corner
LOOP=1
while [ $LOOP -eq 1 ]; do
TEXT=" TOP=$GPU_OVERSCAN_TOP, LEFT=$GPU_OVERSCAN_LEFT, BOTTOM=$GPU_OVERSCAN_BOTTOM, RIGHT=$GPU_OVERSCAN_RIGHT "
LEN=$((${#TEXT}/2))
XPOS=$((XMIDPOINT-LEN))
echo -ne "\033[${YMIDPOINT};${XPOS}f$TEXT"
keypress=$(dd bs=3 count=1 2> /dev/null | get_kc)
case "$keypress" in
"$tty_cuu1"|"$tty_kcuu1") ((GPU_OVERSCAN_BOTTOM++));;
"$tty_cud1"|"$tty_kcud1"|"$tty_cudx") ((GPU_OVERSCAN_BOTTOM--));;
"$tty_cub1"|"$tty_kcub1"|"$tty_cubx") ((GPU_OVERSCAN_RIGHT++)) ;;
"$tty_cuf1"|"$tty_kcuf1"|"$tty_cufx") ((GPU_OVERSCAN_RIGHT--));;
"161") LOOP=0;;
esac
./overscan $GPU_OVERSCAN_TOP $GPU_OVERSCAN_BOTTOM $GPU_OVERSCAN_LEFT $GPU_OVERSCAN_RIGHT
done
# Clear the screen
cat cleared > /dev/fb0
clear
# Finished so update $CONFIG
set_config_var disable_overscan 1 $CONFIG
set_config_var overscan_top $GPU_OVERSCAN_TOP $CONFIG
set_config_var overscan_bottom $GPU_OVERSCAN_BOTTOM $CONFIG
set_config_var overscan_left $GPU_OVERSCAN_LEFT $CONFIG
set_config_var overscan_right $GPU_OVERSCAN_RIGHT $CONFIG
# Clean up
if [ $created_mailbox -eq 1 ]; then
rm -f /dev/vcio
fi
rm rand
rm cleared
# Restore stty to old value
stty $tty_save
clear
tput cnorm