#!/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 xdpyinfo >/dev/null 2>&1; then _xdpy_dpi="$(xdpyinfo 2>/dev/null | awk '/resolution:/ { split($2, a, "x") d = a[1] + 0 if (d >= 48 && d <= 384 && (d < 94 || d > 98)) { print d exit } }' || true)" if [ -n "${_xdpy_dpi:-}" ]; then _dpi="$_xdpy_dpi" fi fi if [ "$_dpi" = "96" ] && [ -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 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) raw_cur = 24.0 * s if (raw_cur < 20.0) cur_sz = 16 else if (raw_cur < 32.0) cur_sz = 24 else if (raw_cur < 44.0) cur_sz = 32 else if (raw_cur < 64.0) cur_sz = 48 else cur_sz = 64 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" < "$TMP_ENV" </dev/null 2>&1; then ANODE_SCALE="$SCALE" ANODE_TOUCH="$TOUCH" anode-apply-theme fi fi if [ -n "${DISPLAY:-}" ] && [ "${ANODE_NO_SIGNAL:-0}" != "1" ]; then if command -v xsetroot >/dev/null 2>&1; then XCURSOR_SIZE="$XCURSOR_SIZE" xsetroot -cursor_name left_ptr >/dev/null 2>&1 || true fi if [ "${ANODE_NO_DBUS:-0}" != "1" ]; then if command -v dbus-update-activation-environment >/dev/null 2>&1; then dbus-update-activation-environment --systemd \ ANODE_SCALE="$SCALE" ANODE_TOUCH="$TOUCH" \ QT_QPA_PLATFORMTHEME="gtk3:qt6ct:qt5ct" \ QT_SCALE_FACTOR="$SCALE" XCURSOR_SIZE="$XCURSOR_SIZE" >/dev/null 2>&1 || \ dbus-update-activation-environment \ ANODE_SCALE="$SCALE" ANODE_TOUCH="$TOUCH" \ QT_QPA_PLATFORMTHEME="gtk3:qt6ct:qt5ct" \ QT_SCALE_FACTOR="$SCALE" XCURSOR_SIZE="$XCURSOR_SIZE" >/dev/null 2>&1 || true fi if command -v systemctl >/dev/null 2>&1; then systemctl --user unset-environment GDK_SCALE GDK_DPI_SCALE >/dev/null 2>&1 || true systemctl --user set-environment \ ANODE_SCALE="$SCALE" ANODE_TOUCH="$TOUCH" \ QT_QPA_PLATFORMTHEME="gtk3:qt6ct:qt5ct" \ QT_SCALE_FACTOR="$SCALE" XCURSOR_SIZE="$XCURSOR_SIZE" >/dev/null 2>&1 || true fi fi fi printf 'ANODE scale: %s (Xft.dpi=%s, WindowScale=%s, QT_SCALE_FACTOR=%s, Xcursor=%s, touch=%s)\n' \ "$SCALE" "$XFT_DPI" "$GDK_SCALE" "$SCALE" "$XCURSOR_SIZE" "$TOUCH"