# Looking Glass — multi-monitor fork A fork of [LookingGlass](https://github.com/gnif/LookingGlass) that carries a multi-monitor guest over a single session. The host packs every guest display into one frame (the **atlas**) and the client composes it back into the guest's desktop. Work lives on branch **`multi-monitor`**. Remote is `gitea` (`ssh://git@gitea.dev.blueoceanswim.com:2222/donny/looking-glass-2.git`). The SSH key used previously was generated on a live boot and no longer exists. If pushing fails with `Permission denied (publickey)`, generate a key at `~/.ssh/id_ed25519_gitea` (the path `~/.ssh/config` expects, port 2222) and have the user add the public half in Gitea under Settings → SSH Keys. Ask before creating credentials. --- ## Start here **The immediate task is bringing up DXGI multi-monitor capture on real hardware.** Everything else is implemented and tested. The DXGI path is written and cross-compiles, but has never run on a machine with more than one output attached. Two things to establish from the user before assuming anything: 1. **How does the Windows guest get its displays?** Dummy plugs on the passed-through GPU, the Looking Glass IDD, or something else. This changes what the outputs look like to DXGI. 2. **Where does the client run?** The guest is captured once and any number of clients can subscribe, so one client per physical monitor is viable. ## Environment setup The environment may be ephemeral — earlier work was done from a live USB boot where nothing survives a reboot. If the build tools are missing: ```bash ./scripts/setup-dev.sh # everything, incl. tests and cross compiler ./scripts/setup-dev.sh --minimal # just the client and Linux host ``` Builds (each is an out-of-tree directory; in-source builds are refused): ```bash mkdir -p client/build && cd client/build && cmake .. && make -j$(nproc) mkdir -p host/build && cd host/build && cmake .. && make -j$(nproc) # Windows host, cross compiled from Linux — type checks DXGI without Windows mkdir -p host/build-win && cd host/build-win cmake -DCMAKE_TOOLCHAIN_FILE=../toolchain-mingw64.cmake .. && make -j$(nproc) ``` ## Testing ```bash host/tests/run-tests.sh # unit tests, live capture, client render test host/tests/demo-multimon.sh ./host/build/looking-glass-host \ ./client/build/looking-glass-client # visible end to end demo, writes a PNG ``` `host/tests/lg-verify-atlas.c` subscribes to a *running host* as a client would and checks the published layout — displays inside the frame, no overlaps, packed without scaling. It is backend agnostic, so pointing it at a Windows host over the same shared memory is the fastest way to validate DXGI output. ## Bringing up DXGI 1. Build `host/build-win/looking-glass-host.exe`, copy it into the guest, run it. 2. Look for this in the host log: ``` Multi-monitor : 3 outputs, desktop 5120x1440, atlas 2560x2880 \\.\DISPLAY1 2560x1440 desktop +0+0 -> atlas +0+0 (primary) ``` If it is absent, the backend fell back to a single output and **logged the reason** — read it. Fallback is deliberate, not a bug. 3. Start the client and look for `Composing N displays from a WxH atlas`. 4. Run `lg-verify-atlas` against the same shared memory to check the layout independently of what it looks like on screen. If anything misbehaves, **first set `dxgi:multiMonitor=no`** to confirm the fault is in this path. `host/platform/Windows/capture/DXGI/MULTIMONITOR.md` has a symptom-to-cause table (frozen display, torn pixels, stale content). Size IVSHMEM generously: the atlas holds every display. Roughly `total pixels x 4 x 2`, rounded up to a power of two. The host logs the atlas size at startup. ## Architecture Deliberate decisions — **understand before changing**: - **`displayCount == 0` means a conventional single-display frame.** All the old behaviour is preserved on that path. Backward compatibility with single-display guests is not optional. - **The client composes as its own pass** (`client/renderers/EGL/compose.c`), before post processing. Everything downstream — scaling, HDR, cursor, damage, input mapping — then works on one ordinary frame and needs no multi-display awareness. - **Anything unsafe falls back to single-display capture.** Mixed capture formats, rotated outputs, a specific output requested. A wrong atlas is worse than one working display. Always log why. - **Packing is shared** (`common/src/displays.c`) so backends do not each invent one. `lgDisplaysPack` picks a minimum-area layout; `lgDisplaysRepack` does CPU copies; `lgDisplaysRectToAtlas` translates damage. All unit tested — extend the tests when changing them. - **Every display's atlas offset is aligned for direct scanout** (`LG_DISPLAY_ALIGN_PIXELS`). This exists so a consumer can point display hardware straight at one display's region of the frame instead of copying it. Do not drop it to save a few pixels of padding; a unit test enforces it. ### Downstream consumer The user is building a custom Wayland compositor (Smithay) that consumes this frame directly — importing the shared memory as a dmabuf and page-flipping each display's region independently, so guest pixels never pass through an extra copy. That is why the alignment guarantee and the damage-in-atlas-space semantics matter beyond the Looking Glass client itself. The contract that consumer relies on is written up under "Consuming frames directly" in `doc/multimonitor.rst` — layout, alignment, frame lifetime (two-deep queue, one second subscriber timeout) and damage. **Treat it as an interface**: changing it breaks a separate codebase. ## Gotchas Things learned the hard way; re-learning them costs hours. - **`compose.vert` must not flip Y.** It renders into a framebuffer that the desktop shader samples, and that shader treats row 0 as the top. Flipping produced an upside-down desktop that passed every geometry check. The rendering test exists to catch exactly this — each test display carries a marker in its top-left corner. - **`framebuffer_write` needs a 32-byte aligned source.** It uses aligned streaming loads. `malloc` only guarantees 16 and will segfault; use `aligned_alloc`. Real capture buffers are page aligned, so only test code hits this. - **Damage scaling comes after atlas placement, never before.** The downscale factor applies to the frame as a whole. - **Under a Wayland session the XWayland root window is blank**, so XCB capture of `:0`/`:1` yields black. This is environmental, not a bug. Use `demo-multimon.sh`, which runs its own X server with real content. - **Xvfb and Xephyr accept `xrandr --setmonitor` and silently ignore it.** Use `xcb:monitors=1280x800+0+0,1280x800+1280+0` to describe a layout directly. ### Pre-existing upstream bugs fixed here These are unrelated to multi-monitor but were blocking. Do not "clean them up" as unrelated changes: - `captureStop()` never called the capture interface's `stop()`, so backend threads were not joined before teardown — segfault. - The XCB pointer thread ran before the host had allocated pointer buffers. - XCB wrote one row per frame instead of the whole image (`pitch` vs `pitch * height`). ## Known gaps - **DXGI multi-output is unproven on hardware.** The top priority. - **HDR metadata is per output**, but the frame format carries one set. Mixed HDR and SDR monitors are refused rather than guessed at. - **Rotation is refused, not handled.** `KVMFRDisplay::rotation` exists for it, but composition would have to rotate during the copy, which `CopySubresourceRegion` cannot do — it needs a shader pass. - **The client redraws the whole composed view each frame.** Damage governs the transfer, which is the expensive part; per-region redraw after composition is a smaller, GPU-side win. - **All displays share one frame rate**, inherent to packing them into one frame. Monitors of differing refresh rates cannot update independently. ## Docs - `doc/multimonitor.rst` — user facing: configuration, shared memory sizing, running one client per guest display, limitations. - `host/platform/Windows/capture/DXGI/MULTIMONITOR.md` — DXGI internals, design decisions and the bring-up troubleshooting table. ## Conventions Match the upstream style: two-space indent, brace on its own line, `this` as the module state pointer, `DEBUG_*` macros for logging. Builds use `-Wall -Werror`, so warnings break the build. Comments should explain *why*, not restate the code — upstream is sparse and consistent about this.