From 5c6969b08a067ee597e445483a26131ab2e14ec4 Mon Sep 17 00:00:00 2001 From: Atlas agent Date: Sat, 14 Nov 2026 22:09:03 +0200 Subject: [PATCH] MACBOX_VIEW_SWITCH: macbox view moves the running VM's window, no restart (Reims 0036) --- docs/architecture.md | 5 + docs/setup.md | 3 + macbox | 61 +- src/patches/README.md | 3 +- ...U_WINDOW_COMPOSITOR_FILE-moves-the-w.patch | 923 ++++++++++++++++++ 5 files changed, 986 insertions(+), 9 deletions(-) create mode 100644 src/patches/reims-vgpu/0036-window-REIMS_VGPU_WINDOW_COMPOSITOR_FILE-moves-the-w.patch diff --git a/docs/architecture.md b/docs/architecture.md index f74fcfc..b367f3e 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -83,6 +83,11 @@ back from the GPU, 2 to 3 frames a second, so macbox never uses it. `macbox stream novnc` switches to noVNC over wayvnc (no rate control). - **Local.** `macbox view local` mounts the invoking user's Wayland socket into the container, and the window opens on the host's desktop, full screen and 1:1: nothing is captured or encoded. +- **Switching** (`MACBOX_VIEW_SWITCH=on`). The container gets the host's socket whatever the view, + sway runs too, and Reims reads the socket to show on from `run/compositor` + (`REIMS_VGPU_WINDOW_COMPOSITOR_FILE`, Reims 0036). `macbox view` rewrites the file and waits for + `run/compositor.on`: the window closes on one compositor and opens on the other, while the + guest and its display run on. Nothing renders into the place that isn't shown. The screen's size is `$MACBOX_HOME/run/resolution` (`macbox resolution WxH`); sway's output and Selkies follow it. diff --git a/docs/setup.md b/docs/setup.md index 794cd48..07a522b 100644 --- a/docs/setup.md +++ b/docs/setup.md @@ -40,6 +40,7 @@ node), makes an SSH key `~/.ssh/macbox_ed25519`, and adds a `Host macbox` block | `MACBOX_DISK_CACHE` | none | `writeback` panicked macOS's NVMe driver under heavy swap; `unsafe` is faster, but a crash of the host itself can corrupt the VM's disk. Applies at the next `macbox up` | | `MACBOX_COHERENT` | auto | `auto`: on with an Arrow Lake iGPU. Coherent, zero-copy surfaces, and the switches measured with it: a 3x simulator 50 → 120 fps there ([performance.md](performance.md#coherent-and-the-switches-measured-with-it-on-by-default-on-arrow-lake)); `on` with any GPU, `off` | | `MACBOX_VIDEO` | off | `on`: Reims's hardware VideoToolbox and M2 scaler devices, so the guest's H.264/HEVC encoding (the Device panel's stream) and decoding run on the host GPU through VA-API instead of VideoToolbox's software encoder. Needs the video kexts in OpenCore first (`macbox video-kexts`); macOS 26.7 (25G241) only. See [performance.md](performance.md#hardware-video-encoding-macbox_video) | +| `MACBOX_VIEW_SWITCH` | off | `on`: `macbox view local\|stream` moves the running VM's window between the host's desktop and the browser stream without a restart (Reims 0036). Takes effect at the next `macbox up`, which must run from a shell in the Wayland session | | `MACBOX_HOME` | ~/.local/share/macbox | everything macbox keeps | Then: @@ -74,6 +75,8 @@ builds Reims's current master as it is, without macbox's patches. The screen: open http://127.0.0.1:6081/ in a browser on the host (Selkies). Or, if you sit at the host and it runs a Wayland desktop, `./macbox view local` opens macOS in a window there, with no encoding at all. Click into it to give it the keyboard; Ctrl+Alt+Super gives the keyboard back. +`view` restarts the VM; with `MACBOX_VIEW_SWITCH=on` the window moves between the two while macOS +runs (after one `macbox down && macbox up` from a shell on the desktop). To watch from another machine, forward the port: `ssh -L 6081:127.0.0.1:6081 `, then open http://127.0.0.1:6081/ there. Or publish it on a tailnet (`tailscale serve --bg --https=8481 diff --git a/macbox b/macbox index eee2fa4..5cb6dd3 100755 --- a/macbox +++ b/macbox @@ -15,7 +15,7 @@ eval "$_env" : "${MACBOX_GUEST_USER:=dev}" "${MACBOX_CPUS:=8}" "${MACBOX_MEMORY:=12G}" "${MACBOX_DISK_SIZE:=160G}" : "${MACBOX_RESOLUTION:=1920x1080}" "${MACBOX_REFRESH:=60}" "${MACBOX_CPUSET:=}" : "${MACBOX_RENDER_NODE:=/dev/dri/renderD128}" "${MACBOX_DISK_CACHE:=none}" "${MACBOX_GPU_TUNING:=auto}" -: "${MACBOX_COHERENT:=auto}" "${MACBOX_VIDEO:=off}" +: "${MACBOX_COHERENT:=auto}" "${MACBOX_VIDEO:=off}" "${MACBOX_VIEW_SWITCH:=off}" : "${MACBOX_SIM_PREFIX:=macbox}" "${MACBOX_POOL:=full slim-1 slim-2 sharp small}" : "${MACBOX_T3_HOST_ID:=$MACBOX_SSH_HOST}" "${MACBOX_T3_SETTINGS:=$HOME/.t3/userdata/settings.json}" DOCKER="${MACBOX_DOCKER:-docker}" @@ -59,9 +59,10 @@ container_start() { local e dev=(--device /dev/kvm --device "$MACBOX_RENDER_NODE") vol=() wl mapfile -t e < <(cenv) [ -e /dev/udmabuf ] && dev+=(--device /dev/udmabuf) # AMD GPUs import guest memory through it - if [ "$(view)" = local ]; then + # With MACBOX_VIEW_SWITCH=on the host's desktop is reachable whatever the view, for a later move. + if [ "$(view)" = local ] || [ "$MACBOX_VIEW_SWITCH" = on ]; then if wl=$(host_wayland); then vol=(-v "$wl:/tmp/host-wayland/wayland-0") - else warn "view local, but this shell has no Wayland session (WAYLAND_DISPLAY): the window goes to the browser stream"; fi + elif [ "$(view)" = local ]; then warn "view local, but this shell has no Wayland session (WAYLAND_DISPLAY): the window goes to the browser stream"; fi fi # seccomp/AppArmor unconfined: passt, the VM's network, needs a user namespace. $DOCKER run -d --name "$C" --init "${e[@]}" \ @@ -74,6 +75,29 @@ vm_exists() { [ -f "$VMDIR/vm.toml" ]; } vm_running() { container_up && ctl list 2>/dev/null | grep -qE "^${MACBOX_VM}[[:space:]]+running"; } qemu_pid() { $DOCKER top "$C" -eo pid,args 2>/dev/null | awk '/qemu-system-x86_64/ {print $1; exit}'; } view() { cat "$RUN/view" 2>/dev/null || echo stream; } +stream_socket() { # sway's socket in the container, the stream's (display-up.sh starts it if needed) + echo "/tmp/xdg/$(in_c "$REPO/src/container/display-up.sh" | sed -n 's/^WAYLAND_DISPLAY=//p')" +} +# Moves the running VM's window to view $1 without a restart (MACBOX_VIEW_SWITCH=on): Reims reads +# the socket from run/compositor once a second and says where its windows are in run/compositor.on. +view_move() { + local to _ + case "$1" in + local) in_c test -S /tmp/host-wayland/wayland-0 || + die "this desktop isn't reachable from the container (it started without WAYLAND_DISPLAY): macbox down && macbox up from a shell on the desktop" + to=/tmp/host-wayland/wayland-0 ;; + stream) to=$(stream_socket) ;; + esac + [ "$(cat "$RUN/compositor.on" 2>/dev/null)" = "$to" ] && { echo "$1" >"$RUN/view"; say "view: $1 (already)"; return 0; } + echo "$to" >"$RUN/compositor.tmp" && mv "$RUN/compositor.tmp" "$RUN/compositor" + for _ in $(seq 40); do + if [ "$(cat "$RUN/compositor.on" 2>/dev/null)" = "$to" ]; then + echo "$1" >"$RUN/view"; say "view: $1, no restart"; return 0 + fi + sleep 0.5 + done + die "the window didn't move to $to in 20 s (macbox logs qemu); run/compositor.on: $(cat "$RUN/compositor.on" 2>/dev/null)" +} resolution() { cat "$RUN/resolution" 2>/dev/null || echo "$MACBOX_RESOLUTION"; } build_id() { readlink "$DATA/qemu/current" 2>/dev/null || true; } ssh_ok() { ssh -o ConnectTimeout=5 -o BatchMode=yes "$MACBOX_SSH_HOST" true >/dev/null 2>&1; } @@ -215,6 +239,9 @@ MACBOX_COHERENT=$MACBOX_COHERENT # on: Reims's hardware VideoToolbox (H.264/HEVC encode and decode on the host's GPU through VA-API) # and M2 scaler devices; needs the video kexts in OpenCore (macbox video-kexts). off: as before MACBOX_VIDEO=$MACBOX_VIDEO +# on: macbox view local|stream moves the running VM's window between this desktop and the browser +# stream without a restart (Reims's REIMS_VGPU_WINDOW_COMPOSITOR_FILE); off: view restarts the VM +MACBOX_VIEW_SWITCH=$MACBOX_VIEW_SWITCH # iOS Simulator pool: names are " (