Files
anode/scripts/anode-apply-scale
T

170 lines
4.3 KiB
Bash
Executable File

#!/bin/sh
# ANODE fractional DPI scale & touch-mode manager (strict POSIX sh)
set -eu
: "${XDG_CONFIG_HOME:=$HOME/.config}"
CONF_DIR="$XDG_CONFIG_HOME/anode"
CONF_FILE="$CONF_DIR/scale.conf"
ENV_FILE="$CONF_DIR/scale.env"
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
usage() {
cat <<'EOF'
Usage: anode-apply-scale [SCALE|auto] [--touch=0|1] [--no-apply]
Options:
SCALE Fractional UI scale factor between 0.50 and 4.00 (e.g. 0.75, 1.00, 1.25, 1.50, 2.00)
auto Detect scale automatically from primary X11 output EDID dimensions (fallback 1.00)
--touch=0|1 Enable (1) or disable (0) touch-mode sizing & virtual keyboard defaults
--no-apply Only write scale.conf/scale.env without re-applying the active theme
EOF
}
SCALE=""
TOUCH=""
DO_APPLY=1
if [ -f "$CONF_FILE" ]; then
. "$CONF_FILE"
fi
: "${SCALE:=1.00}"
: "${TOUCH:=0}"
for arg in "$@"; do
case "$arg" in
-h|--help)
usage
exit 0
;;
--touch=0|--touch=false|--touch=off)
TOUCH=0
;;
--touch=1|--touch=true|--touch=on)
TOUCH=1
;;
--no-apply)
DO_APPLY=0
;;
auto)
SCALE="auto"
;;
*)
SCALE="$arg"
;;
esac
done
detect_edid_scale() {
_dpi="96"
if [ -n "${DISPLAY:-}" ] && command -v xrandr >/dev/null 2>&1; then
_xrandr_dpi="$(xrandr --query 2>/dev/null | awk '
/ connected/ {
if (match($0, /[0-9]+x[0-9]+/) && match($0, /[0-9]+mm x [0-9]+mm/)) {
res = substr($0, RSTART)
}
px = 0; wmm = 0
for (i = 1; i <= NF; i++) {
if ($i ~ /^[0-9]+x[0-9]+(\+[0-9]+\+[0-9]+)?$/ && px == 0) {
split($i, a, "x"); px = a[1] + 0
}
if ($i ~ /^[0-9]+mm$/ && $(i+1) == "x" && $(i+2) ~ /^[0-9]+mm$/) {
wmm = $i; gsub(/mm/, "", wmm); wmm += 0
}
}
if (px > 0 && wmm > 0) {
printf "%.2f\n", (px * 25.4) / wmm
exit
}
}
' || true)"
if [ -n "${_xrandr_dpi:-}" ]; then
_dpi="$_xrandr_dpi"
fi
fi
if [ "$_dpi" = "96" ] && [ -n "${DISPLAY:-}" ] && command -v xdpyinfo >/dev/null 2>&1; then
_xdpy_dpi="$(xdpyinfo 2>/dev/null | awk '/resolution:/ { split($2, a, "x"); if (a[1] > 0) { print a[1]; exit } }' || true)"
if [ -n "${_xdpy_dpi:-}" ]; then
_dpi="$_xdpy_dpi"
fi
fi
awk -v d="$_dpi" 'BEGIN {
s = (d + 0) / 96.0
s = int(s * 20.0 + 0.5) / 20.0
if (s < 0.50) s = 0.50
if (s > 4.00) s = 4.00
printf "%.2f\n", s
}'
}
if [ "$SCALE" = "auto" ]; then
SCALE="$(detect_edid_scale)"
fi
if ! printf '%s\n' "$SCALE" | awk '/^[0-9]+(\.[0-9]+)?$/ { exit 0 } { exit 1 }'; then
printf 'anode-apply-scale: invalid scale value: %s\n' "$SCALE" >&2
exit 1
fi
CALC="$(awk -v raw="$SCALE" -v touch="$TOUCH" 'BEGIN {
s = raw + 0.0
if (s < 0.50) s = 0.50
if (s > 4.00) s = 4.00
s = int(s * 100.0 + 0.5) / 100.0
xft_dpi = int(96.0 * s + 0.5)
gdk_scale = (s < 1.75) ? 1 : int(s + 0.5)
cur_sz = int(24.0 * s + 0.5)
if (cur_sz < 16) cur_sz = 16
if (cur_sz > 96) cur_sz = 96
t = (touch == "1" || touch == 1) ? 1 : 0
printf "%.2f %d %d %d %d\n", s, xft_dpi, gdk_scale, cur_sz, t
}')"
set -- $CALC
SCALE="$1"
XFT_DPI="$2"
GDK_SCALE="$3"
XCURSOR_SIZE="$4"
TOUCH="$5"
mkdir -p "$CONF_DIR"
TMP_CONF="$CONF_FILE.tmp.$$"
cat > "$TMP_CONF" <<EOF
# Managed by anode-apply-scale
SCALE=$SCALE
TOUCH=$TOUCH
XFT_DPI=$XFT_DPI
GDK_SCALE=$GDK_SCALE
XCURSOR_SIZE=$XCURSOR_SIZE
EOF
mv -f "$TMP_CONF" "$CONF_FILE"
TMP_ENV="$ENV_FILE.tmp.$$"
cat > "$TMP_ENV" <<EOF
# Managed by anode-apply-scale
export ANODE_SCALE="$SCALE"
export ANODE_TOUCH="$TOUCH"
export GDK_SCALE="$GDK_SCALE"
export GDK_DPI_SCALE="1.0"
export ADW_DISABLE_PORTAL="1"
export QT_AUTO_SCREEN_SCALE_FACTOR="0"
export QT_ENABLE_HIGHDPI_SCALING="1"
export QT_SCALE_FACTOR_ROUNDING_POLICY="PassThrough"
export QT_SCALE_FACTOR="$SCALE"
export QT_FONT_DPI="96"
export XCURSOR_SIZE="$XCURSOR_SIZE"
EOF
mv -f "$TMP_ENV" "$ENV_FILE"
if [ "$DO_APPLY" = "1" ]; then
if [ -x "$SCRIPT_DIR/anode-apply-theme" ]; then
"$SCRIPT_DIR/anode-apply-theme"
elif command -v anode-apply-theme >/dev/null 2>&1; then
anode-apply-theme
fi
fi
printf 'ANODE scale: %s (Xft.dpi=%s, GDK_SCALE=%s, QT_SCALE_FACTOR=%s, Xcursor=%s, touch=%s)\n' \
"$SCALE" "$XFT_DPI" "$GDK_SCALE" "$SCALE" "$XCURSOR_SIZE" "$TOUCH"