131 lines
4.2 KiB
Bash
Executable File
131 lines
4.2 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# auto-scale-virtual.sh
|
|
#
|
|
# Automatically set Plasma's display scale for the SPICE/virtio-gpu virtual
|
|
# display ("Virtual-1") based on its current resolution.
|
|
#
|
|
# This is for a QEMU/KVM guest with a virtio-gpu display. spice-vdagent resizes
|
|
# Virtual-1 to match the SPICE client window. When the host laptop is docked the
|
|
# guest sees a low resolution (e.g. 1080p) and wants 100% scale; undocked the
|
|
# laptop panel is higher-DPI and wants 200%. This script picks the right scale
|
|
# and applies it idempotently with kscreen-doctor, only acting when the scale
|
|
# actually needs to change.
|
|
#
|
|
# Triggered by a udev rule on the drm "change" event (see the accompanying
|
|
# README). Because udev runs this with a near-empty environment, the session env
|
|
# is hardcoded as defaults below (real environment values still win when the
|
|
# script is run interactively). It is also safe to run manually at any time.
|
|
|
|
set -u
|
|
|
|
OUTPUT_NAME="Virtual-1" # virtio-gpu connector name
|
|
# Threshold in pixels (height). >= this -> high-DPI laptop panel -> 200%.
|
|
# 1080p is 1080, so anything 1400 and up counts as the high-res laptop.
|
|
HIGH_DPI_MIN_HEIGHT=1400
|
|
SCALE_HIGH=2
|
|
SCALE_LOW=1
|
|
|
|
# --- Session env fallbacks (used when invoked by udev/systemd-run) ----------
|
|
# Machine-specific: this guest's single Plasma Wayland session. Adjust the
|
|
# UID/path values if your setup differs.
|
|
: "${XDG_RUNTIME_DIR:=/run/user/1001}"
|
|
: "${WAYLAND_DISPLAY:=wayland-0}"
|
|
: "${DISPLAY:=:0}"
|
|
: "${XDG_CURRENT_DESKTOP:=KDE}"
|
|
: "${DBUS_SESSION_BUS_ADDRESS:=unix:path=/run/user/1001/bus}"
|
|
: "${HOME:=/home/hendrik}"
|
|
: "${PATH:=/usr/bin:/bin}"
|
|
export XDG_RUNTIME_DIR WAYLAND_DISPLAY DISPLAY XDG_CURRENT_DESKTOP \
|
|
DBUS_SESSION_BUS_ADDRESS HOME PATH
|
|
|
|
log() { printf '[auto-scale] %s\n' "$*" >&2; }
|
|
|
|
# --- Debounce: rapid resizes fire several change events; only one instance
|
|
# applies the scale at a time, concurrent ones skip. ---------------------
|
|
exec 9>/tmp/auto-scale-virtual.lock
|
|
if ! flock -n 9; then
|
|
# Another instance is already handling it.
|
|
exit 0
|
|
fi
|
|
|
|
# --- Read the *current* scale of the target output from kscreen-doctor JSON ---
|
|
get_current_scale() {
|
|
kscreen-doctor -j 2>/dev/null \
|
|
| sed 's/\x1b\[[0-9;]*m//g' \
|
|
| python3 -c '
|
|
import sys, json
|
|
try:
|
|
d = json.load(sys.stdin)
|
|
except Exception:
|
|
sys.exit(1)
|
|
name = sys.argv[1] if len(sys.argv) > 1 else "Virtual-1"
|
|
for o in d.get("outputs", []):
|
|
if o.get("name") == name:
|
|
print(o.get("scale", 1))
|
|
break
|
|
' "$OUTPUT_NAME"
|
|
}
|
|
|
|
# --- Current resolution of the connector (from kscreen-doctor's text output) ---
|
|
# We parse the active mode name for the matching output: "1:3456x2168@75.00*"
|
|
# -> 3456x2168.
|
|
get_current_resolution() {
|
|
kscreen-doctor -o 2>/dev/null \
|
|
| sed 's/\x1b\[[0-9;]*m//g' \
|
|
| awk -v out="$OUTPUT_NAME" '
|
|
/^[[:space:]]*Output:/ { cur=0 }
|
|
$0 ~ "Output:.*"out"[[:space:]]" { cur=1 }
|
|
cur && /[0-9]+x[0-9]+@[0-9.]+\*/ {
|
|
if (match($0, /[0-9]+x[0-9]+@/, m)) {
|
|
res=m[0]; sub(/@/,"",res); print res; exit
|
|
}
|
|
}
|
|
'
|
|
}
|
|
|
|
# --- Decide the desired scale from the resolution height ---
|
|
desired_scale() {
|
|
local res="${1:-}"
|
|
local h="${res#*x}" # take part after "x"
|
|
h="${h%%[!0-9]*}" # strip any trailing junk
|
|
[ -z "$h" ] && { echo "$SCALE_LOW"; return; }
|
|
if [ "$h" -ge "$HIGH_DPI_MIN_HEIGHT" ]; then
|
|
echo "$SCALE_HIGH"
|
|
else
|
|
echo "$SCALE_LOW"
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
local res cur want
|
|
res="$(get_current_resolution)"
|
|
if [ -z "$res" ]; then
|
|
log "could not determine resolution; skipping"
|
|
return 0
|
|
fi
|
|
|
|
cur="$(get_current_scale)"
|
|
want="$(desired_scale "$res")"
|
|
|
|
if [ -z "$cur" ]; then
|
|
log "could not read current scale; will set to $want"
|
|
cur="?"
|
|
fi
|
|
|
|
if [ "$cur" = "$want" ]; then
|
|
# Already correct; nothing to do.
|
|
return 0
|
|
fi
|
|
|
|
log "Virtual-1 is ${res}; scale ${cur} -> ${want}"
|
|
if ! kscreen-doctor "output.${OUTPUT_NAME}.scale.${want}" >/dev/null 2>&1; then
|
|
log "kscreen-doctor failed to set scale"
|
|
return 1
|
|
fi
|
|
log "scale set to ${want}"
|
|
return 0
|
|
}
|
|
|
|
main "$@"
|