Add a script for automatically scaling KDE VM when resolution changes
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
# kde-virtual-display-autoscale
|
||||
|
||||
Automatically switch the Plasma display scale when a QEMU/KVM guest's
|
||||
virtio-gpu display is resized — e.g. when docking or undocking the host laptop.
|
||||
|
||||
The SPICE guest agent (`spice-vdagent`) resizes the guest's `Virtual-1` display
|
||||
to match the SPICE client window. When the laptop is docked the guest sees a
|
||||
low resolution (1080p) and wants 100% scale; undocked the laptop panel is
|
||||
higher-DPI and wants 200%. A udev rule fires a script on the DRM `change` event
|
||||
that reads the new resolution with `kscreen-doctor` and applies the matching
|
||||
scale — no polling, ~0 ms reaction, idle CPU cost of zero.
|
||||
|
||||
## Requirements
|
||||
|
||||
- KDE Plasma 6 on Wayland (uses `kscreen-doctor`)
|
||||
- `python3` (parses `kscreen-doctor` JSON)
|
||||
- A single virtio-gpu display (`Virtual-1`) driven by `spice-vdagent`
|
||||
|
||||
## Install
|
||||
|
||||
The easiest way is the installer, which copies the script into `~/.local/bin`
|
||||
and installs the udev rule (rewriting the rule's UID/GID to match your user):
|
||||
|
||||
```bash
|
||||
./install.sh
|
||||
```
|
||||
|
||||
It needs `sudo` for the udev-rule install and reloads the rules when done.
|
||||
Re-running it is safe — it overwrites the files in place.
|
||||
|
||||
### Manual install
|
||||
|
||||
Copy the script into `~/.local/bin`:
|
||||
|
||||
```bash
|
||||
cp auto-scale-virtual.sh ~/.local/bin/
|
||||
chmod +x ~/.local/bin/auto-scale-virtual.sh
|
||||
```
|
||||
|
||||
Install the udev rule (run as root). If your UID/GID is not `1001/1001`, edit
|
||||
the `--uid`/`--gid` in the rule to match (run `id -u` / `id -g`):
|
||||
|
||||
```bash
|
||||
sudo install -m 644 99-auto-scale-virtual.rules \
|
||||
/etc/udev/rules.d/99-auto-scale-virtual.rules
|
||||
sudo udevadm control --reload-rules
|
||||
```
|
||||
|
||||
Make sure `~/.local/bin` is in your `PATH` (only needed for manual runs; the
|
||||
udev rule calls the script by absolute path).
|
||||
|
||||
## Usage
|
||||
|
||||
Once installed, docking/undocking the host resizes the guest display and the
|
||||
scale is applied automatically. You can also run it manually at any time:
|
||||
|
||||
```bash
|
||||
auto-scale-virtual.sh
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
The script is idempotent: it only changes the scale when the current one does
|
||||
not match the desired one for the current resolution, so it never fights a
|
||||
manually chosen scale between events.
|
||||
|
||||
Tune these variables near the top of `auto-scale-virtual.sh`:
|
||||
|
||||
- `OUTPUT_NAME` — the virtio-gpu connector name (`Virtual-1` by default).
|
||||
- `HIGH_DPI_MIN_HEIGHT` — the resolution height (in pixels) at or above which
|
||||
the high-DPI scale is used. `1400` makes 1080p (1080) → 100% and anything
|
||||
taller → 200%.
|
||||
- `SCALE_HIGH` / `SCALE_LOW` — the scales to apply (default `2` / `1`).
|
||||
|
||||
The udev rule pins the user UID/GID (`--uid=1001 --gid=1001`) and the script
|
||||
hardcodes the Plasma Wayland session env (`/run/user/1001`, `wayland-0`, etc.)
|
||||
so it can reach `kscreen-doctor` even though udev starts it with an empty
|
||||
environment. Update both the rule and the script's env-fallback block if your
|
||||
UID or session differs.
|
||||
Reference in New Issue
Block a user