xsettingsd: optimize a bit the shell script

This commit is contained in:
Vincent Bernat 2021-07-15 18:13:14 +02:00
parent 550ad9deac
commit 11044c6f3e

View file

@ -1,28 +1,27 @@
#!/bin/sh #!/bin/sh
DISPLAY=${DISPLAY%.0}
# Compute DPI of each screens # Compute DPI of each screens
dpis="" dpi=$(xrandr --current \
for info in $(xrandr --current \ | sed -En 's/^([^ ]+)* connected.* ([0-9]+)x.* ([0-9]+)mm x .*/\1 \2 \3/p' \
| sed -n 's/^\([^ ]\{1,\}\)* connected.* \([0-9]\{1,\}\)x.* \([0-9]\{1,\}\)mm x .*/\1,\2,\3/p'); do | while read output pixels mm; do
output=${info%%,*}
pixels=${info#*,} # Compute DPI
pixels=${pixels%%,*} dpi=$((pixels * 254 / 10 / mm))
mm=${info##*,}
dpi=$(($pixels * 254 / 10 / $mm))
# For laptop screens, we need to apply a correction factor # For laptop screens, we need to apply a correction factor
case $output in case $output in
eDP-1|eDP1) corrected=$(($dpi * 96/144)) ;; eDP-1|eDP1) corrected=$(($dpi * 96/144)) ;;
*) corrected=$dpi ;; *) corrected=$dpi ;;
esac esac
# Authorized factors: 1, 1.5, 2, 3, 4, ... # Authorized factors: 1, 1.5, 2, 3, 4, ...
rounded=$(((corrected + 24) / 48 * 48)) rounded=$(((corrected + 24) / 48 * 48))
[ $rounded -gt 192 ] && rounded=$(((corrected + 48) / 96 * 96)) [ $rounded -gt 192 ] && rounded=$(((corrected + 48) / 96 * 96))
[ $rounded -lt 96 ] && rounded=96 [ $rounded -lt 96 ] && rounded=96
dpis="$dpis$rounded "
echo "$output: ${dpi}dpi (corrected to ${corrected}dpi, rounded to ${rounded}dpi)" >&2 echo "$output: ${dpi}dpi (corrected to ${corrected}dpi, rounded to ${rounded}dpi)" >&2
done echo "$rounded"
done)
# Use first screen DPI # Use first screen DPI
dpi=${dpis%% *} dpi=${dpis%% *}