Skip to content

Commit d9969e7

Browse files
committed
Improved screen detection
1 parent b59d9c4 commit d9969e7

1 file changed

Lines changed: 48 additions & 41 deletions

File tree

scripts/common/system-info.sh

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,49 @@ source "${REPO_DIR}/scripts/common/common.sh"
66

77
function get_screen_width() {
88
if [ "${DEVICE_MODEL}" = "Steam Deck" ]; then
9-
echo "1280"
9+
echo 1280
1010
return
1111
elif [ "${DEVICE_MODEL}" = "Xiaomi Redmi Note 4X" ]; then
12-
echo "1080"
12+
echo 1080
1313
return
1414
fi
1515

16-
if [ "${XDG_SESSION_TYPE}" = "wayland" ]; then
17-
if does_bin_exist "wayland-info"; then
18-
wayland-info | grep "refresh" | head -n 1 | sed 's/.*width: \([1-9][0-9]*\)\s*px.*/\1/g'
16+
if [ "${XDG_SESSION_TYPE}" = 'wayland' ]; then
17+
if does_bin_exist 'wayland-info'; then
18+
wayland-info | grep 'refresh' | head -n 1 | sed 's/.*width: \([1-9][0-9]*\)\s*px.*/\1/g'
1919
else
2020
echo 0
2121
fi
22-
else
23-
if does_bin_exist "xrandr"; then
22+
elif [ -n "${DISPLAY}" ]; then
23+
if does_bin_exist 'xrandr'; then
2424
xrandr | grep -w connected | grep primary | sed 's/^.*primary \([0-9][0-9]*\)x.*/\1/g'
25-
elif does_bin_exist "xdpyinfo"; then
26-
xdpyinfo | grep "dimensions" | sed 's/^[^0-9]*\([0-9]\+\)x[0-9]\+ pixels.*/\1/g'
25+
elif does_bin_exist 'xdpyinfo'; then
26+
xdpyinfo | grep 'dimensions' | sed 's/^[^0-9]*\([0-9]\+\)x[0-9]\+ pixels.*/\1/g'
2727
else
2828
echo 0
2929
fi
30+
else
31+
echo 0
3032
fi
3133
}
3234

3335
function get_screen_width_millimetres() {
34-
if [ "${XDG_SESSION_TYPE}" = "wayland" ]; then
35-
if does_bin_exist "wayland-info"; then
36-
wayland-info | grep "physical_" | head -n 1 | sed 's/.*physical_width: \([1-9][0-9]*\)\s*mm.*/\1/g'
36+
if [ "${XDG_SESSION_TYPE}" = 'wayland' ]; then
37+
if does_bin_exist 'wayland-info'; then
38+
wayland-info | grep 'physical_' | head -n 1 | sed 's/.*physical_width: \([1-9][0-9]*\)\s*mm.*/\1/g'
3739
else
3840
echo 0
3941
fi
40-
else
41-
if does_bin_exist "xrandr"; then
42+
elif [ -n "${DISPLAY}" ]; then
43+
if does_bin_exist 'xrandr'; then
4244
xrandr | grep -w connected | grep primary | sed 's/.* \([0-9]\+\)mm x [0-9]\+mm.*/\1/g'
43-
elif does_bin_exist "xdpyinfo"; then
44-
xdpyinfo | grep "dimensions" | sed 's/^.* pixels (\([0-9]\+\)x[0-9]\+ mil.*/\1/g'
45+
elif does_bin_exist 'xdpyinfo'; then
46+
xdpyinfo | grep 'dimensions' | sed 's/^.* pixels (\([0-9]\+\)x[0-9]\+ mil.*/\1/g'
4547
else
4648
echo 0
4749
fi
50+
else
51+
echo 0
4852
fi
4953
}
5054

@@ -54,66 +58,69 @@ function get_screen_width_inches() {
5458
}
5559

5660
function get_screen_height() {
57-
if [ "${DEVICE_MODEL}" = "Steam Deck" ]; then
58-
echo "800"
61+
if [ "${DEVICE_MODEL}" = 'Steam Deck' ]; then
62+
echo 800
5963
return
60-
elif [ "${DEVICE_MODEL}" = "Xiaomi Redmi Note 4X" ]; then
61-
echo "1920"
64+
elif [ "${DEVICE_MODEL}" = 'Xiaomi Redmi Note 4X' ]; then
65+
echo 1920
6266
return
6367
fi
6468

65-
if [ "${XDG_SESSION_TYPE}" = "wayland" ]; then
66-
if does_bin_exist "wayland-info"; then
67-
wayland-info | grep "refresh" | head -n 1 | sed 's/.*height: \([1-9][0-9]*\)\s*px.*/\1/g'
69+
if [ "${XDG_SESSION_TYPE}" = 'wayland' ]; then
70+
if does_bin_exist 'wayland-info'; then
71+
wayland-info | grep 'refresh' | head -n 1 | sed 's/.*height: \([1-9][0-9]*\)\s*px.*/\1/g'
6872
else
6973
echo 0
7074
fi
71-
else
72-
if does_bin_exist "xrandr"; then
75+
elif [ -n "${DISPLAY}" ]; then
76+
if does_bin_exist 'xrandr'; then
7377
xrandr | grep -w connected | grep primary | sed 's/^.*primary [0-9]\+x\([0-9]\+\).*/\1/g'
74-
elif does_bin_exist "xdpyinfo"; then
75-
xdpyinfo | grep "dimensions" | sed 's/^[^0-9]*[0-9]\+x\([0-9]\+\) pixels.*/\1/g'
78+
elif does_bin_exist 'xdpyinfo'; then
79+
xdpyinfo | grep 'dimensions' | sed 's/^[^0-9]*[0-9]\+x\([0-9]\+\) pixels.*/\1/g'
7680
else
7781
echo 0
7882
fi
83+
else
84+
echo 0
7985
fi
8086
}
8187

8288
function get_screen_height_millimetres() {
83-
if [ "${XDG_SESSION_TYPE}" = "wayland" ]; then
84-
if does_bin_exist "wayland-info"; then
85-
wayland-info | grep "physical_" | head -n 1 | sed 's/.*physical_height: \([1-9][0-9]*\)\s*mm.*/\1/g'
89+
if [ "${XDG_SESSION_TYPE}" = 'wayland' ]; then
90+
if does_bin_exist 'wayland-info'; then
91+
wayland-info | grep 'physical_' | head -n 1 | sed 's/.*physical_height: \([1-9][0-9]*\)\s*mm.*/\1/g'
8692
else
8793
echo 0
8894
fi
89-
else
90-
if does_bin_exist "xrandr"; then
95+
elif [ -n "${DISPLAY}" ]; then
96+
if does_bin_exist 'xrandr'; then
9197
xrandr | grep -w connected | grep primary | sed 's/.* [0-9]\+mm x \([0-9]\+\)mm.*/\1/g'
92-
elif does_bin_exist "xdpyinfo"; then
93-
xdpyinfo | grep "dimensions" | sed 's/^.* pixels ([0-9]\+x\([0-9]\+\) mil.*/\1/g'
98+
elif does_bin_exist 'xdpyinfo'; then
99+
xdpyinfo | grep 'dimensions' | sed 's/^.* pixels ([0-9]\+x\([0-9]\+\) mil.*/\1/g'
94100
else
95101
echo 0
96102
fi
103+
else
104+
echo 0
97105
fi
98106
}
99107

100108
function get_screen_dpi() {
101-
if [ "${DEVICE_MODEL}" = "Xiaomi Redmi Note 4X" ]; then
102-
echo "401"
109+
if [ "${DEVICE_MODEL}" = 'Xiaomi Redmi Note 4X' ]; then
110+
echo 401
103111
return
104112
fi
105113

106114
local RESOLUTION_H=$(get_screen_width)
107115

108-
if [ -z "${RESOLUTION_H}" ] \
109-
|| [ "${RESOLUTION_H}" -eq 0 ]; then
110-
if [ "${XDG_SESSION_TYPE}" = "wayland" ]; then
116+
if [ -z "${RESOLUTION_H}" ] || [ "${RESOLUTION_H}" -eq 0 ]; then
117+
if [ "${XDG_SESSION_TYPE}" = 'wayland' ]; then
111118
echo 0 # Wayland not yet supported
112119
return
113120
fi
114121

115-
if does_bin_exist "xdpyinfo"; then
116-
xdpyinfo | grep "resolution" | sed 's/^[^0-9]*\([0-9]*\)x[0-9]*.*/\1/g'
122+
if [ -n "${DISPLAY}" ] && does_bin_exist 'xdpyinfo'; then
123+
xdpyinfo | grep 'resolution' | sed 's/^[^0-9]*\([0-9]*\)x[0-9]*.*/\1/g'
117124
else
118125
echo 0
119126
fi

0 commit comments

Comments
 (0)