forked from syscl/Enable-HiDPI-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable-HiDPI.sh
executable file
·373 lines (313 loc) · 11 KB
/
enable-HiDPI.sh
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/sh
# enable-HiDPI.sh
#
#
# Created by syscl/lighting/Yating Zhou on 16/3/2.
#
#================================= GLOBAL VARS ==================================
#
# The script expects '0.5' but non-US localizations use '0,5' so we export
# LC_NUMERIC here (for the duration of the ssdtPRGen.sh) to prevent errors.
#
export LC_NUMERIC="en_US.UTF-8"
#
# Prevent non-printable/control characters.
#
unset GREP_OPTIONS
unset GREP_COLORS
unset GREP_COLOR
#
# Output styling.
#
BOLD="\033[1m"
RED="\033[1;31m"
GREEN="\033[1;32m"
BLUE="\033[1;34m"
OFF="\033[m"
STYLE_UNDERLINED="\e[4m"
#
# Repository location
#
REPO=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
#
# Define variables.
# Gvariables stands for getting datas from OS X.
#
gDisplayVendorID_RAW=""
gDisplayVendorID=""
gDisplayProductID_RAW=""
gDisplayProductID_sfix=""
gDisplayProductID_fix=""
gDisplayProductID=""
gConfig=""
gRes_RAW="F"
gRes_VAL=""
gRes_ENCODE=""
gHeight_HiDPI=""
gWide_HiDPI=""
gHeight_HiDPI_VAL=""
gWide_HiDPI_VAL=""
gRes_HiDPI_VAL=""
gRes_HiDPI_ENCODE=""
gDespath=""
gBak_Time=$(date +%Y-%m-%d-h%H_%M_%S)
gBak_Dir="${REPO}/Display-Backups/${gBak_Time}"
#
# Define gConfig
#
gConfig=""
#
#--------------------------------------------------------------------------------
#
function _PRINT_MSG()
{
local message=$1
case "$message" in
OK* ) local message=$(echo $message | sed -e 's/.*OK://')
echo "[ ${GREEN}OK${OFF} ] ${message}."
;;
FAILED*) local message=$(echo $message | sed -e 's/.*://')
echo "[${RED}FAILED${OFF}] ${message}."
;;
---* ) local message=$(echo $message | sed -e 's/.*--->://')
echo "[ ${GREEN}--->${OFF} ] ${message}"
;;
NOTE* ) local message=$(echo $message | sed -e 's/.*NOTE://')
echo "[ ${RED}Note${OFF} ] ${message}."
;;
esac
}
#
#--------------------------------------------------------------------------------
#
function _getEDID()
{
local index=0
local selection=0
gDisplayInf=($(ioreg -lw0 | grep -i "IODisplayEDID" | sed -e "/[^<]*</s///" -e "s/\>//"))
if [[ "${#gDisplayInf[@]}" -ge 2 ]];
then
#
# Multi monitors detected. Choose target monitor.
#
echo " Multi monitors detected, please choose target monitor: "
for display in "${gDisplayInf[@]}"
do
let index++
#
# Show monitors.
#
printf " [ %d ] ${display:16:4}::${display:20:4}" $index
echo ''
done
#
#
#
echo ''
#
# Let user make a selection.
#
printf "Please choose the desired display for enable Hi-DPI (${STYLE_UNDERLINED}E${OFF}xit/1-${index})"
read -p " ? " selection
case "$(_toLowerCase $selection)" in
e|exit ) echo "Abort script."
exit -0
;;
[[:digit:]]* ) #
# Lower selection (arrays start at zero).
#
let selection-=1
gMonitor=${gDisplayInf[$selection]}
;;
* ) echo "Invalid menu action, please type valid among 1, ..., ${index}"
;;
esac
else
gMonitor=${gDisplayInf}
fi
gDisplayVendorID_RAW=${gMonitor:16:4}
gDisplayVendorID=$((0x$gDisplayVendorID_RAW))
gDisplayProductID_RAW=${gMonitor:20:4}
#
# Exchange two bytes
#
gDisplayProduct_pr=${gDisplayProductID_RAW:2:2}
gDisplayProduct_st=${gDisplayProductID_RAW:0:2}
gDisplayProductID_sfix=$(echo $gDisplayProduct_pr$gDisplayProduct_st)
gDisplayProductID=$((0x$gDisplayProduct_pr$gDisplayProduct_st))
#
# Fix an issue that will cause wrong name of DisplayProductID
#
if [[ $gDisplayProduct_pr == "0"* ]];
then
gDisplayProductID_fix=${gDisplayProductID_sfix:1:3}
else
gDisplayProductID_fix=$(echo $gDisplayProductID_sfix)
fi
# echo $gDisplayVendorID_RAW
# echo $gDisplayVendorID
# echo $gDisplayProductID_RAW
# echo $gDisplayProductID
# echo $gDisplayProductID_fix
gConfig=${REPO}/DisplayVendorID-$gDisplayVendorID_RAW/DisplayProductID-$gDisplayProductID_fix
}
#
#--------------------------------------------------------------------------------
#
function _toLowerCase()
{
echo "`echo $1 | tr '[:upper:]' '[:lower:]'`"
}
#
#--------------------------------------------------------------------------------
#
function _printHeader()
{
echo '<?xml version="1.0" encoding="UTF-8"?>' > "$gConfig"
echo '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >> "$gConfig"
echo '<plist version="1.0">' >> "$gConfig"
echo '<dict>' >> "$gConfig"
echo ' <key>DisplayProductID</key>' >> "$gConfig"
echo " <integer>${gDisplayProductID}</integer>" >> "$gConfig"
echo ' <key>DisplayVendorID</key>' >> "$gConfig"
echo " <integer>${gDisplayVendorID}</integer>" >> "$gConfig"
echo ' <key>scale-resolutions</key>' >> "$gConfig"
echo ' <array>' >> "$gConfig"
echo ' </array>' >> "$gConfig"
echo '</dict>' >> "$gConfig"
echo '</plist>' >> "$gConfig"
}
#
#--------------------------------------------------------------------------------
#
function _create_dir()
{
if [ ! -d "$1" ];
then
mkdir -p "$1"
fi
}
#
#--------------------------------------------------------------------------------
#
function _buildconfig()
{
_create_dir ${gBak_Dir}
rm -R ${REPO}/DisplayVendorID-*
_create_dir ${REPO}/DisplayVendorID-$gDisplayVendorID_RAW
}
#
#--------------------------------------------------------------------------------
#
function _toLowerCase()
{
echo "`echo $1 | tr '[:upper:]' '[:lower:]'`"
}
#
#--------------------------------------------------------------------------------
#
function _calcsRes()
{
#
# Increment i stands for adding arrays.
#
i=0
while [ "$gRes_RAW" != 0 ];
do
read -p "Enter the Resolution you want to enable HiDPI(If you want to stop adding or say you just add enough number of HiDPI resolution you want, enter 0: " gRes_RAW
if [[ $gRes_RAW != 0 ]];
then
#
# Raw Datas
#
gHeightVAL=$(echo $gRes_RAW | cut -f 1 -d "x")
gWideVAL=$(echo $gRes_RAW | cut -f 2 -d "x")
#
# HiDPI (note that for enable HiDPI, all Height and Val must be twice
#
gHeight_HiDPI_VAL=$(echo $((gHeightVAL*2)))
gWide_HiDPI_VAL=$(echo $((gWideVAL*2)))
#echo $gHeight_HiDPI_VAL
#echo $gWide_HiDPI_VAL
#
# Convet Height and Wide(decimal) into hex
#
gHeight=$(echo "obase=16;$gHeightVAL" | bc)
gWide=$(echo "obase=16;$gWideVAL" | bc)
gHeight_HiDPI=$(echo "obase=16;$gHeight_HiDPI_VAL" | bc)
gWide_HiDPI=$(echo "obase=16;$gWide_HiDPI_VAL" | bc)
#
# Generate Resolution Values (Hex)
#
gRes_VAL=$(echo "00000$gHeight 00000$gWide 00000001 00200000")
gRes_HiDPI_VAL=$(echo "00000$gHeight_HiDPI 00000$gWide_HiDPI 00000001 00200000")
#
# Encode Resolution Values(Hex) into base64
#
gRes_ENCODE=$(echo $gRes_VAL | xxd -r -p | base64)
gRes_HiDPI_ENCODE=$(echo $gRes_HiDPI_VAL | xxd -r -p | base64)
#
# Inject HiDPI values.
#
/usr/libexec/plistbuddy -c "Add ':scale-resolutions:$i' string" $gConfig
/usr/libexec/plistbuddy -c "Set ':scale-resolutions:$i' $gRes_ENCODE" $gConfig
/usr/libexec/plistbuddy -c "Add ':scale-resolutions:$((i+1))' string" $gConfig
/usr/libexec/plistbuddy -c "Set ':scale-resolutions:$((i+1))' $gRes_HiDPI_ENCODE" $gConfig
perl -pi -e 's/string/data/g' $gConfig
gRes_RAW=""
i=$(($i+1))
fi
done
}
#
#--------------------------------------------------------------------------------
#
function _OSCheck()
{
#
# Extract minor version (eg. 10.9 vs. 10.10 vs. 10.11)
#
MINOR_VER=$([[ "$(sw_vers -productVersion)" =~ [0-9]+\.([0-9]+) ]] && echo ${BASH_REMATCH[1]})
if [[ $MINOR_VER -ge 11 ]];
then
gDespath=$(echo "/System/Library/Displays/Contents/Resources/Overrides/")
else
gDespath=$(echo "/System/Library/Displays/Overrides/")
fi
}
#
#--------------------------------------------------------------------------------
#
function _patch()
{
#
# Count number indicates patch system or not.
#
if [ $i != 0 ];
then
_PRINT_MSG "--->: Backuping origin Display Information..."
sudo cp -R "$gDespath" ${gBak_Dir}
sudo defaults write /Library/Preferences/com.apple.windowserver DisplayResolutionEnabled -bool YES
sudo defaults delete /Library/Preferences/com.apple.windowserver DisplayResolutionDisabled 2>&1 >/dev/null
sudo cp -R "${REPO}/DisplayVendorID-$gDisplayVendorID_RAW" "$gDespath"
_PRINT_MSG "OK: Done, Please Reboot to see the change! Pay attention to use Retina Menu Display to select the HiDPI resolution!"
else
_PRINT_MSG "NOTE: Since you stop the operation, don't worry all your files in system hasnt been touched."
fi
}
#
#--------------------------------------------------------------------------------
#
function main()
{
_getEDID
_buildconfig
_printHeader
_calcsRes
_OSCheck
_patch
}
#==================================== START =====================================
main
#================================================================================
exit 0