Modify the recovery method and optimize the code structure

This commit is contained in:
xzhih 2020-11-16 08:50:08 +08:00
parent 84e35ead60
commit fa0e36f68a
3 changed files with 248 additions and 228 deletions

View file

@ -38,34 +38,42 @@ bash -c "$(curl -fsSL https://raw.githubusercontent.com/xzhih/one-key-hidpi/dev/
### 恢复模式
如果使用此脚本后,开机无法进入系统,请到 macos 恢复模式中或使用 clover `-x` 安全模式进入系统,打开终端
如果使用此脚本后,开机无法进入系统,请到 macos 恢复模式,打开终端
这里有两种方式进行关闭,建议选第一种
1. 快捷恢复
```bash
ls /Volumes/
cd /Volumes/你的系统盘/System/Library/Displays/Contents/Resources/Overrides/HIDPI
```
./disable
你会看到你的系统盘
```bash
cd /Volumes/你的系统盘/Users/
ls
```
你可以看到所有用户的家目录
```bash
cd 你的用户名
./.hidpi-disable
```
2. 手动恢复
使用终端删除 `/System/Library/Displays/Contents/Resources/Overrides` 下删除显示器 VendorID 对应的文件夹,并把 `HIDPI/backup` 文件夹中的备份复制出来。
请使用单个显示器执行以下命令,笔记本关闭外接显示器的 HIDPI 时请关闭内置显示器
使用终端删除 `Library/Displays/Contents/Resources/Overrides` 下删除所有通过外部注入的显示器配置文件夹
具体命令如下:
```bash
ls /Volumes/
cd /Volumes/你的系统盘/System/Library/Displays/Contents/Resources/Overrides
EDID=($(ioreg -lw0 | grep -i "IODisplayEDID" | sed -e "/[^<]*</s///" -e "s/\>//"))
Vid=($(echo $EDID | cut -c18-20))
rm -rf ./DisplayVendorID-$Vid
cp -r ./HIDPI/backup/* ./
rm -rf /Volumes/你的系统盘/Library/Displays/Contents/Resources/Overrides
```
## 从以下得到启发

View file

@ -44,26 +44,31 @@ There are two ways to close it. It is recommended to choose the first one
```bash
ls /Volumes/
cd /Volumes/"Your System Disk Part"/System/Library/Displays/Contents/Resources/Overrides/HIDPI
```
./disable
you can see all Disk.
```bash
cd /Volumes/"Your System Disk Part"/Users/
ls
```
you can see user home directory.
```bash
cd "user name"
./.hidpi-disable
```
2.
Remove your display's DisplayVendorID folder under `/System/Library/Displays/Contents/Resources/Overrides` , and move backup files
Please use the single display to execute the following commands. If it is a laptop, turn off the internal monitor when turning off the HIDPI of the external monitor.
In Terminal:
Remove all injected display's DisplayVendorID folder under `Library/Displays/Contents/Resources/Overrides`
```bash
ls /Volumes/
cd /Volumes/"Your System Disk Part"/System/Library/Displays/Contents/Resources/Overrides
EDID=($(ioreg -lw0 | grep -i "IODisplayEDID" | sed -e "/[^<]*</s///" -e "s/\>//"))
Vid=($(echo $EDID | cut -c18-20))
rm -rf ./DisplayVendorID-$Vid
cp -r ./HIDPI/backup/* ./
rm -rf /Volumes/"Your System Disk Part"/Library/Displays/Contents/Resources/Overrides
```
## Inspired

185
hidpi.sh
View file

@ -11,6 +11,7 @@ cat << EEF
============================================
EEF
currentDir="$(cd $(dirname -- $0) && pwd)"
systemLanguage=($(locale | grep LANG | sed s/'LANG='// | tr -d '"' | cut -d "." -f 1))
langDisplay="Display"
@ -81,8 +82,7 @@ if [[ "${systemLanguage}" == "zh_CN" ]]; then
langChooseResOpCustom="(6) 手动输入分辨率"
fi
function get_edid()
{
function get_edid() {
local index=0
local selection=0
@ -98,8 +98,7 @@ function get_edid()
echo "--------------------------------------------------------"
# Show monitors.
for display in "${gDisplayInf[@]}"
do
for display in "${gDisplayInf[@]}"; do
let index++
MonitorName=("$(echo ${display:190:24} | xxd -p -r)")
VendorID=${display:16:4}
@ -125,7 +124,7 @@ function get_edid()
[[:digit:]]*)
# Lower selection (arrays start at zero).
if ((selection < 1 || selection > index)); then
echo "${langEnterError}";
echo "${langEnterError}"
exit 1
fi
let selection-=1
@ -133,7 +132,7 @@ function get_edid()
;;
*)
echo "${langEnterError}";
echo "${langEnterError}"
exit 1
;;
esac
@ -152,17 +151,25 @@ function get_edid()
}
# init
function init()
{
get_edid
function init() {
rm -rf ${currentDir}/tmp/
mkdir -p ${currentDir}/tmp/
currentDir="$(cd `dirname -- $0` && pwd)"
libDisplaysDir="/Library/Displays"
targetDir="${libDisplaysDir}/Contents/Resources/Overrides"
sysDisplayDir="/System${targetDir}"
Overrides="\/Library\/Displays\/Contents\/Resources\/Overrides"
sysOverrides="\/System${Overrides}"
if [[ ! -d "${targetDir}" ]]; then
sudo mkdir -p "${targetDir}"
fi
downloadHost="https://raw.githubusercontent.com/xzhih/one-key-hidpi/master"
if [ -d "${currentDir}/displayIcons" ]; then
downloadHost="file://${currentDir}"
fi
DICON="com\.apple\.cinema-display"
imacicon=${sysOverrides}"\/DisplayVendorID\-610\/DisplayProductID\-a032\.tiff"
mbpicon=${sysOverrides}"\/DisplayVendorID\-610\/DisplayProductID\-a030\-e1e1df\.tiff"
@ -170,29 +177,16 @@ function init()
lgicon=${sysOverrides}"\/DisplayVendorID\-1e6d\/DisplayProductID\-5b11\.tiff"
proxdricon=${Overrides}"\/DisplayVendorID\-610\/DisplayProductID\-ae2f\_Landscape\.tiff"
if [[ ! -d ${targetDir}/HIDPI ]]; then
sudo mkdir -p ${targetDir}/HIDPI
fi
downloadHost="https://raw.githubusercontent.com/xzhih/one-key-hidpi/master"
if [ -d "${currentDir}/displayIcons" ];then
downloadHost="file://${currentDir}"
fi
get_edid
generate_restore_cmd
}
#
function generate_restore_cmd()
{
#
rm -rf ${currentDir}/tmp/
mkdir -p ${currentDir}/tmp/
cat > "${currentDir}/tmp/disable" <<-\CCC
#!/bin/sh
function generate_restore_cmd() {
function get_edid()
{
cat >"$(cd && pwd)/.hidpi-disable" <<-\CCC
#!/bin/sh
function get_edid() {
local index=0
local selection=0
gDisplayInf=($(ioreg -lw0 | grep -i "IODisplayEDID" | sed -e "/[^<]*</s///" -e "s/\>//"))
@ -201,8 +195,7 @@ function get_edid()
echo '------------------------------------'
echo ' Index | VendorID | ProductID '
echo '------------------------------------'
for display in "${gDisplayInf[@]}"
do
for display in "${gDisplayInf[@]}"; do
let index++
printf " %d | ${display:16:4} | ${gMonitor:22:2}${gMonitor:20:2}\n" $index
done
@ -211,14 +204,14 @@ function get_edid()
case $selection in
[[:digit:]]*)
if ((selection < 1 || selection > index)); then
echo "Enter error, bye";
echo "Enter error, bye"
exit 1
fi
let selection-=1
gMonitor=${gDisplayInf[$selection]}
;;
*)
echo "Enter error, bye";
echo "Enter error, bye"
exit 1
;;
esac
@ -235,7 +228,8 @@ function get_edid()
get_edid
rootPath="../../../../../.."
rootPath="../.."
restorePath="${rootPath}/Library/Displays/Contents/Resources/Overrides"
echo ""
echo "(1) Disable HIDPI on this monitor"
@ -244,13 +238,20 @@ echo ""
read -p "Enter your choice [1~2]: " input
case ${input} in
1) ${rootPath}/usr/libexec/plistbuddy -c "Delete :vendors:${Vid}:products:${Pid}" ../Icons.plist
1)
if [[ -f "${restorePath}/Icons.plist" ]]; then
${rootPath}/usr/libexec/plistbuddy -c "Delete :vendors:${Vid}:products:${Pid}" "${restorePath}/Icons.plist"
fi
if [[ -d "${restorePath}/DisplayVendorID-${Vid}" ]]; then
rm -rf "${restorePath}/DisplayVendorID-${Vid}"
fi
;;
2) rm -rf ${rootPath}/Library/Displays
2)
rm -rf "${restorePath}"
;;
*)
echo "Enter error, bye";
echo "Enter error, bye"
exit 1
;;
esac
@ -258,15 +259,13 @@ esac
echo "HIDPI Disabled"
CCC
sudo mv ${currentDir}/tmp/disable ${targetDir}/HIDPI/
sudo chmod +x ${targetDir}/HIDPI/disable
chmod +x "$(cd && pwd)/.hidpi-disable"
}
# choose_icon
function choose_icon()
{
#
function choose_icon() {
rm -rf ${currentDir}/tmp/
mkdir -p ${currentDir}/tmp/
mkdir -p ${currentDir}/tmp/DisplayVendorID-${Vid}
@ -284,26 +283,31 @@ function choose_icon()
echo "(5) Pro Display XDR"
echo "(6) ${langNotChange}"
echo ""
#
read -p "${langInputChoice} [1~6]: " logo
case ${logo} in
1) Picon=${imacicon}
1)
Picon=${imacicon}
RP=("33" "68" "160" "90")
curl -fsSL "${downloadHost}/displayIcons/iMac.icns" -o ${currentDir}/tmp/DisplayVendorID-${Vid}/DisplayProductID-${Pid}.icns
;;
2) Picon=${mbicon}
2)
Picon=${mbicon}
RP=("52" "66" "122" "76")
curl -fsSL "${downloadHost}/displayIcons/MacBook.icns" -o ${currentDir}/tmp/DisplayVendorID-${Vid}/DisplayProductID-${Pid}.icns
;;
3) Picon=${mbpicon}
3)
Picon=${mbpicon}
RP=("40" "62" "147" "92")
curl -fsSL "${downloadHost}/displayIcons/MacBookPro.icns" -o ${currentDir}/tmp/DisplayVendorID-${Vid}/DisplayProductID-${Pid}.icns
;;
4) Picon=${lgicon}
4)
Picon=${lgicon}
RP=("11" "47" "202" "114")
cp ${sysDisplayDir}/DisplayVendorID-1e6d/DisplayProductID-5b11.icns ${currentDir}/tmp/DisplayVendorID-${Vid}/DisplayProductID-${Pid}.icns
;;
5) Picon=${proxdricon}
5)
Picon=${proxdricon}
RP=("5" "45" "216" "121")
curl -fsSL "${downloadHost}/displayIcons/ProDisplayXDR.icns" -o ${currentDir}/tmp/DisplayVendorID-${Vid}/DisplayProductID-${Pid}.icns
if [[ ! -f ${targetDir}/DisplayVendorID-610/DisplayProductID-ae2f_Landscape.tiff ]]; then
@ -311,11 +315,12 @@ case ${logo} in
Picon=${Overrides}"\/DisplayVendorID\-${Vid}\/DisplayProductID\-${Pid}\.tiff"
fi
;;
6) rm -rf ${currentDir}/tmp/Icons.plist
6)
rm -rf ${currentDir}/tmp/Icons.plist
;;
*)
echo "${langEnterError}";
echo "${langEnterError}"
exit 1
;;
esac
@ -335,13 +340,11 @@ fi
}
# main
function main()
{
function main() {
sudo mkdir -p ${currentDir}/tmp/DisplayVendorID-${Vid}
dpiFile=${currentDir}/tmp/DisplayVendorID-${Vid}/DisplayProductID-${Pid}
sudo chmod -R 777 ${currentDir}/tmp/
#
cat >"${dpiFile}" <<-\CCC
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
@ -369,41 +372,46 @@ echo ${langChooseResOp5}
echo ${langChooseResOpCustom}
echo ""
#
read -p "${langInputChoice}: " res
case ${res} in
1 ) create_res_1 1680x945 1440x810 1280x720 1024x576
1)
create_res_1 1680x945 1440x810 1280x720 1024x576
create_res_2 1280x800 1280x720 960x600 960x540 640x360
create_res_3 840x472 800x450 720x405 640x360 576x324 512x288 420x234 400x225 320x180
create_res_4 1680x945 1440x810 1280x720 1024x576 960x540 840x472 800x450 640x360
;;
2 ) create_res_1 1680x945 1424x802 1280x720 1024x576
2)
create_res_1 1680x945 1424x802 1280x720 1024x576
create_res_2 1280x800 1280x720 960x600 960x540 640x360
create_res_3 840x472 800x450 720x405 640x360 576x324 512x288 420x234 400x225 320x180
create_res_4 1680x945 1440x810 1280x720 1024x576 960x540 840x472 800x450 640x360
;;
3 ) create_res_1 1680x1050 1440x900 1280x800 1024x640
3)
create_res_1 1680x1050 1440x900 1280x800 1024x640
create_res_2 1280x800 1280x720 960x600 960x540 640x360
create_res_3 840x472 800x450 720x405 640x360 576x324 512x288 420x234 400x225 320x180
create_res_4 1680x1050 1440x900 1280x800 1024x640 960x540 840x472 800x450 640x360
;;
4 ) create_res_1 2560x1440 2048x1152 1920x1080 1760x990 1680x945 1440x810 1360x765 1280x720
4)
create_res_1 2560x1440 2048x1152 1920x1080 1760x990 1680x945 1440x810 1360x765 1280x720
create_res_2 1360x765 1280x800 1280x720 1024x576 960x600 960x540 640x360
create_res_3 960x540 840x472 800x450 720x405 640x360 576x324 512x288 420x234 400x225 320x180
create_res_4 2048x1152 1920x1080 1680x945 1440x810 1280x720 1024x576 960x540 840x472 800x450 640x360
;;
5 ) create_res_1 3000x2000 2880x1920 2250x1500 1920x1280 1680x1050 1440x900 1280x800 1024x640
5)
create_res_1 3000x2000 2880x1920 2250x1500 1920x1280 1680x1050 1440x900 1280x800 1024x640
create_res_2 1280x800 1280x720 960x600 960x540 640x360
create_res_3 840x472 800x450 720x405 640x360 576x324 512x288 420x234 400x225 320x180
create_res_4 1920x1280 1680x1050 1440x900 1280x800 1024x640 960x540 840x472 800x450 640x360
;;
6 ) custom_res
6)
custom_res
create_res_2 1360x765 1280x800 1280x720 960x600 960x540 640x360
create_res_3 840x472 800x450 720x405 640x360 576x324 512x288 420x234 400x225 320x180
create_res_4 1680x945 1440x810 1280x720 1024x576 960x540 840x472 800x450 640x360
;;
*)
echo "${langEnterError}";
echo "${langEnterError}"
exit 1
;;
esac
@ -421,8 +429,7 @@ FFF
}
# end
function end()
{
function end() {
sudo chown -R root:wheel ${currentDir}/tmp/
sudo chmod -R 0755 ${currentDir}/tmp/
sudo chmod 0644 ${currentDir}/tmp/DisplayVendorID-${Vid}/*
@ -434,16 +441,14 @@ function end()
}
# custom resolution
function custom_res()
{
function custom_res() {
echo "${langCustomRes}"
read -p ":" res
create_res ${res}
}
# create resolution
function create_res()
{
function create_res() {
for res in $@; do
width=$(echo ${res} | cut -d x -f 1)
height=$(echo ${res} | cut -d x -f 2)
@ -456,8 +461,7 @@ OOO
done
}
function create_res_1()
{
function create_res_1() {
for res in $@; do
width=$(echo ${res} | cut -d x -f 1)
height=$(echo ${res} | cut -d x -f 2)
@ -469,8 +473,7 @@ OOO
done
}
function create_res_2()
{
function create_res_2() {
for res in $@; do
width=$(echo ${res} | cut -d x -f 1)
height=$(echo ${res} | cut -d x -f 2)
@ -482,8 +485,7 @@ OOO
done
}
function create_res_3()
{
function create_res_3() {
for res in $@; do
width=$(echo ${res} | cut -d x -f 1)
height=$(echo ${res} | cut -d x -f 2)
@ -495,8 +497,7 @@ OOO
done
}
function create_res_4()
{
function create_res_4() {
for res in $@; do
width=$(echo ${res} | cut -d x -f 1)
height=$(echo ${res} | cut -d x -f 2)
@ -509,8 +510,7 @@ done
}
# enable
function enable_hidpi()
{
function enable_hidpi() {
choose_icon
main
sed -i "" "/.*IODisplayEDID/d" ${dpiFile}
@ -519,8 +519,7 @@ function enable_hidpi()
}
# patch
function enable_hidpi_with_patch()
{
function enable_hidpi_with_patch() {
choose_icon
main
@ -536,8 +535,7 @@ function enable_hidpi_with_patch()
}
# disable
function disable()
{
function disable() {
echo ""
echo "${langDisableOpt1}"
echo "${langDisableOpt2}"
@ -545,13 +543,20 @@ function disable()
read -p "${langInputChoice} [1~2]: " input
case ${input} in
1) sudo /usr/libexec/plistbuddy -c "Delete :vendors:${Vid}:products:${Pid}" ${targetDir}/Icons.plist
1)
if [[ -f "${targetDir}/Icons.plist" ]]; then
sudo /usr/libexec/plistbuddy -c "Delete :vendors:${Vid}:products:${Pid}" "${targetDir}/Icons.plist"
fi
if [[ -d "${targetDir}/DisplayVendorID-${Vid}" ]]; then
sudo rm -rf "${targetDir}/DisplayVendorID-${Vid}"
fi
;;
2) sudo rm -rf ${libDisplaysDir}
2)
sudo rm -rf "${targetDir}"
;;
*)
echo "${langEnterError}";
echo "${langEnterError}"
exit 1
;;
esac
@ -560,8 +565,7 @@ function disable()
}
#
function start()
{
function start() {
init
echo ""
echo ${langEnableOp1}
@ -572,15 +576,18 @@ function start()
#
read -p "${langInputChoice} [1~3]: " input
case ${input} in
1) enable_hidpi
1)
enable_hidpi
;;
2) enable_hidpi_with_patch
2)
enable_hidpi_with_patch
;;
3) disable
3)
disable
;;
*)
echo "${langEnterError}";
echo "${langEnterError}"
exit 1
;;
esac