# VR and 3D apps in c8's XR: Monado owns the glasses

## First (the user's ask, 2026-12-02)

1. Commit the current working tree on `main` as one commit, as lowtide,
   with this session's attribution line. It is the reflow work of
   2026-12-01: c8ui's narrow setters and Miller columns (`miller.c`);
   Cirrus as a panorama hub with trails (`home.c`); News, Weather,
   Files, Tailscale, Tiles, PC settings and the Lua apps built once and
   reflowing; docs. No VR work is in it.
2. `git switch -c monado-xr`. All work below goes on that branch, and
   this plan is copied into it as `compositor/MONADO-XR.md`. Commit only
   when asked.

## Context

c8's XR mode draws Wayland windows in space itself (glasses.c, imu.c),
so no existing VR software can use it: games need an OpenXR runtime,
and SteamVR games need OpenVR. The user wants the most seamless and most
compatible support for VR and 3D apps, FOSS only. Decided with the user:

- **Monado owns the glasses in XR.** c8 leases the glasses' output to
  `monado-service`. Our IMU fusion becomes a Monado driver. c8's space
  (windows, Start, charms, sky) is drawn as an OpenXR **overlay client**
  (XR_EXTX_overlay). VR games run as the main session, with the desktop
  in the same space over them. SteamVR games come through **xrizer**
  (OpenVR→OpenXR); SteamVR itself is skipped (proprietary, and no gain).
- **Flat 3D games:** a per-window "Show as 3D" toggle that marks any
  window (Xwayland/Proton included) as side by side or top and bottom.
  It is remembered per app. The SBS picture comes from whatever the user
  chooses (emulators etc.); we ship no closed tools.
- **Any headset, not just ours.** Monado already drives Valve Index,
  Vive and Vive Pro (Platinum), Bigscreen Beyond and Pimax (Gold),
  Windows MR, Rift, PSVR2 and XREAL Air. Quest and Pico go through
  WiVRn, which is Monado-based. c8 is device-agnostic: it leases
  whatever the runtime asks for (every `non_desktop` output, like sway,
  plus our glasses). It uses the user's own Monado or WiVRn when one is
  installed (e.g. through Envision). Only the XREAL 1 Pro's network IMU
  needs our driver.
- **Proprietary is fine where it is clearly better.** For lighthouse
  headsets, Monado's `steamvr_lh` driver uses SteamVR's own tracking,
  which is better than libsurvive. That is the user's choice in their
  Monado setup, and c8 needs nothing for it. SteamVR as the whole
  runtime is not used: c8's space needs XR_EXTX_overlay (unconfirmed in
  SteamVR), and SteamVR would need a second, OpenVR-overlay version of
  the client.
- Today's direct path stays as a fallback (`[glasses] runtime = builtin`, or
  when Monado isn't installed), so XR keeps working without Monado.

Research facts this rests on (Dec 2026):

- Monado supports only the USB-HID XREAL Air family. The 1 Pro's IMU on
  TCP 52998 is unsupported, so we write the driver. Prior art:
  kevindurb/xreal-linux (a SteamVR driver, same port; it also switches
  the glasses to SBS).
- Monado has no driver plugin API. Out-of-tree drivers build Monado
  from source with the driver added (the WiVRn pattern).
- Monado's "wayland direct" target leases through
  `wp_drm_lease_device_v1` (`XRT_COMPOSITOR_WAYLAND_CONNECTOR`).
- wlroots 0.20 has `wlr_drm_lease_v1.h` (checked locally). Granting a
  lease **destroys the wlr_output**; it comes back through `new_output`
  when the lease is revoked.
- Neither Monado nor the OpenXR loader is in the configured zypper
  repos. Both get built from source, or the user adds a repo (ask).

## Phase 0: de-risk (spike, nothing merged)

Prove the three things the design leans on before refactoring
glasses.c. Use Monado's simulated HMD and its Wayland-window target,
inside a nested c8:

1. An XR_EXTX_overlay session runs and is shown with **no** main
   session (c8's space must be visible when no game runs). If it can't
   be, c8 runs a main session while idle and hands off to overlay when
   a game starts. Find out which.
2. An overlay client can render with c8's GLES context via
   XR_MNDX_egl_enable (wlroots' EGL display and context). The fallback
   is Vulkan interop, which is much more work.
3. `hello_xr` (OpenXR SDK sample) as the main session plus our overlay:
   both are drawn, and the overlay gets the main-session-visibility
   event.

Read Monado's current docs, source and the OpenXR spec for each piece
before writing calls, and say so (the user's rule).

## Phase 1: DRM lease in c8

- New `compositor/src/lease.c`. Call `wlr_drm_lease_v1_manager_create` on
  the backend.
  - Always offer every `non_desktop` output (Index, Vive, ...). These
    are kept out of the layout, as sway does.
  - Offer the XREAL glasses only while in XR with `[glasses] runtime =
    monado` (auto: when a runtime is found), and withdraw them when
    leaving XR.
- Glasses only: hide the lease global from clients other than the
  runtime. c8 spawns `monado-service` itself through `c8_spawn_client`
  (`WAYLAND_SOCKET`, as shell.c does) and adds the global to
  settings.c's `global_filter`. Restart it with backoff, as the shell
  is. With another headset, a user-run Monado or WiVRn connects
  normally.
- The output vanishing on grant must not drop XR. Add a "glasses leased"
  state that `c8_xr_update` (glasses.c:257) and
  `c8_project_now`/`project_hides` (output.c:114, :153) treat as glasses
  present. XR-1 stays the primary. When the output comes back on revoke,
  it is re-offered and not drawn on by us.

## Phase 2: Monado driver for the glasses

- New component **`xr/`** (root table, build.sh, DEPENDENCIES.md,
  CLAUDE.md). It fetches a pinned Monado release and builds it with our
  driver `d/c8_glasses` added. It installs `monado-service` and
  `openxr_monado.json` under the prefix. It is optional: the desktop
  must still build and run without it, like the apps.
- Move IMU reading and fusion (imu.c: Mahony, the bias and temperature
  table, prediction; tracker.c's neck model) into a small library both
  sides build. c8 keeps it for the builtin path, and the driver uses it
  for Monado. Only one owner of the stream at a time (XR.md: never two
  connections): c8 stops its tracker before offering the lease.
- The driver gives Monado the HMD's distortion-free display, FOV from
  `[glasses] fov`, IPD, and poses from `c8_imu_orientation`. It reads
  `[glasses]` from c8's config.ini. Recentre and the bias save move
  with it.
- SBS: Monado needs the 3840x1080 mode for two eyes. Find the command
  that switches the glasses (kevindurb/xreal-linux does it), then switch
  on session start and back afterwards. Until then, the user presses
  the button.

## Phase 3: c8 as an OpenXR overlay client

- New `compositor/src/xrclient.c`, loaded only on the Monado path. It
  sets up an overlay session with GLES through XR_MNDX_egl_enable, a
  LOCAL reference space and a swapchain per eye.
- Frame loop: `xrWaitFrame` blocks, so it runs on a thread that wakes
  the wlroots loop through an eventfd. Begin, draw and end happen on
  the main thread, as the spec allows.
- Drawing reuses `draw_space` (glasses.c:2289) with its target swapped:
  - It renders into swapchain images instead of the output buffer.
  - View and projection come from `xrLocateViews` instead of `eyes_for`,
    `eye_fov` and our scanout and latency prediction. Monado reprojects.
  - It submits one projection layer with alpha.
  - Later step: windows as quad and cylinder layers for sharper text.
    `quad_matrix` and `curve_strip` already match them.
- `head_at` (glasses.c:777) takes its pose from `xrLocateSpace`, so
  hit-testing, placement and recentre keep working unchanged.
- While a game's main session is visible: no sky, floor or dome.
  Windows are hidden until Super, then Start, the charms and windows
  float over the game. Keys and the pointer go to the game's own
  window as now.

## Phase 4: games

- `session.c` exports `XR_RUNTIME_JSON` to our Monado's json (only for
  the glasses, and never over a runtime the user already chose) and
  `PRESSURE_VESSEL_IMPORT_OPENXR_1_RUNTIMES=1` into systemd and D-Bus,
  so Steam and Proton games find it without launch options. It also
  sets the OpenVR override to xrizer if installed (check xrizer's docs
  for the current variable or vrpathreg file).
- xrizer: built by c8-xr since 2026-12-03 (v0.6; xr/CLAUDE.md).
  Paused there by the user (2026-12-03), to pick up later:
  - Their Steam is the Flatpak, which sees none of it (Monado in
    `/usr/local`, its socket, `~/.local/share/xrizer`). Native Steam
    (openSUSE's `steam`, non-oss) is what c8's setup targets; whether
    to install it is the user's call, and Steam is never a dependency
    of ours, not even an optional one.
  - Then: `build.sh --install --xr` puts xrizer in
    `~/.local/share/xrizer`, log in again (c8 sets `VR_OVERRIDE` at
    start), enter XR, start one light game from Steam (not SteamVR);
    xrizer's log is `~/.local/state/xrizer/xrizer.txt`.
  - Nothing has run on it yet: no OpenVR app on this machine to test
    with outside Steam.

## Phase 5: per-window "Show as 3D"

- stereo.c: a compositor-side layout per view
  (`view->stereo_override`). `c8_stereo_for` returns it when the client
  hasn't tagged the surface. It works for Xwayland, since
  `draw_surface` (glasses.c:971) already crops by `stereo_src`.
- UI: Off / Side by side / Top and bottom, in the slate bar's menu
  (slatebar.c). It is shown only in XR.
- Remembered per app id in placement.c's file (`c8_ini_parse`).
- Works on both paths, builtin and Monado.

## Files

- **New:** `compositor/src/lease.c`, `compositor/src/xrclient.c`, `xr/`
  (Monado build and driver), a shared IMU lib split from
  `compositor/src/imu.c` and `tracker.c`.
- **Changed:** `glasses.c` (draw target and views abstracted; leased
  state), `output.c` (roles while leased), `settings.c` (global filter),
  `shell.c`/`c8_spawn_client` (runtime spawn), `session.c` (env),
  `stereo.c`, `slatebar.c`, `placement.c`, `config.c` and
  `data/config.ini` (`[glasses] runtime`).
- **Build and docs:** `build.sh`, `install.sh`, `meson.build`;
  `compositor/XR.md`, `CLAUDE.md` (root table), `DEPENDENCIES.md`,
  `README.md` (licence list if Monado's source ships: BSL-1.0).
- **Needed from the user:** the openxr-loader, Monado's build deps and
  xrizer (ask before building anything that isn't installed).

## Verification

- **Headless (nested c8, scratch XDG dirs, short TMPDIR,
  dbus-run-session):**
  - monado-service with the simulated HMD on its Wayland-window target.
  - Our overlay shows Start and windows (`C8_XR_DUMP` or grim of the
    Monado window).
  - `hello_xr` started as the main session: the game is drawn with our
    windows over it, and Super brings up Start.
  - The 3D toggle on a side-by-side test window: LEFT in one eye and
    RIGHT in the other, on both paths. The toggle is remembered after a
    restart.
  - `[glasses] runtime = builtin` behaves exactly as today.
- **On the glasses (real session, user present):**
  - The lease is granted and the glasses come back after leaving XR.
  - Tracking from the driver matches today's: drift, recentre, no
    second IMU connection.
  - A native OpenXR game, and a Steam game through xrizer under Proton.
  - An emulator in SBS with the toggle.
- Any test left untested gets recorded in XR.md, per the project's
  convention.

## Risks

- An overlay session with no main session may not be shown. Phase 0
  settles this; the fallback is the idle main-session handoff.
- EGL interop missing or broken → Vulkan swapchains.
- The glasses' SBS switch command is unknown.
- Monado's internal API churn. Pin a release and update deliberately.
- Leasing a non-`non_desktop` output: Monado must be pointed at the
  connector explicitly.

## Status (2026-12-02)

The setting is `[glasses] runtime` (with the rest of the glasses'),
not a new `[xr]` section. What was built and how is in XR.md's Monado
section and `xr/CLAUDE.md`.

| Phase | State |
|---|---|
| 0. De-risk | Done. 1 and 2 settled from Monado's source and the spec, then shown: in a nested c8 our overlay runs in Monado with no game, `hello_xr -g Vulkan2` joins as the main session, and the overlay hears it come and go (2026-12-02). |
| 1. Lease | Done: `lease.c`, `xrruntime.c` (spawn, private global, `xr.leased`, restarts, fallback). Tested headless with a stand-in service; never against DRM. |
| 2. Driver | Done and tried on the 1 Pro+ (2026-12-02): Monado picks it, the IMU stream is clean (1 ms apart, none lost), the bias and temperature table carry over. In a nested c8 with `runtime = monado` the whole chain ran on the glasses (driver → Monado → our overlay): the overlay's frames turn with the head (left 76°, up 44°) and come back to the same view, no drift seen. The 3D switch: `glassesmode.c`, opt-in `[glasses] sbs_switch`, the 1S's command, untried on the 1 Pro+. |
| 3. Overlay | Done and tested headless (Monado's qwerty head, its window target): draws c8's space in both eyes, nothing over a game until Super; without GLES in Monado it gives up after 5 s and builtin takes over. Never on the glasses. Later: windows as quad/cylinder layers. |
| 4. Games | Done: `XR_RUNTIME_JSON`, pressure-vessel variables, xrizer's `VR_OVERRIDE` and paths file, exported by session.c; xrizer built by c8-xr. No game run yet; Flatpak Steam not reached (xr/CLAUDE.md). |
| 5. Show as 3D | Done and tested headless (builtin path). |

Next, with the user and the glasses, in the real session (the branch
installed with `./build.sh --install --xr`): the lease granted and the
glasses back after leaving XR; how tracking feels in the glasses
against the builtin path; a native OpenXR game and a Steam game through
xrizer under Proton; an emulator in SBS with Show as 3D; `sbs_switch`
on the 1 Pro+ (the 1S's command, never tried). `hello_xr` is no test of
tracking on these glasses: its cubes sit at the hands, the head and the
floor, so a rotation-only head sees an even blue-grey.

Layers (decided 2026-12-02 after the second real session, the user's
call: "just utilising monado's built in concepts"): the overlay's one
projection picture of the whole space had edges that showed on every
turn and wobbled, however it was patched (overscan, eye gaps). c8's
space moves onto Monado's composition layers, each placed with the
newest head pose by Monado itself:

1. The background: equirect2 layers, drawn once. Done, tested nested
   (XR.md).
2. Windows: a quad layer each (content, frame and chrome drawn flat
   into its own swapchain), back to front by distance; the ones not
   ready, 3D ones and the all-round video stay in the projection.
   Done, tested nested.
3. Start, toasts, the splash (quads in LOCAL, placed as before, not
   VIEW space), the charms bar (cylinder; needs
   upstream's cylinder fix, on our Monado branch over v25.1.0). Done; Start and the bar
   tested nested, toasts and the splash not.
4. What's left in the projection: the ray, the cursor, beacons. The
   dot is a layer now, and an empty projection isn't submitted
   (nested, 2026-12-02); beacons, the floor and outlines remain. Then
   OVERSCAN and VIEW_GAP can go.

First real session (2026-12-02): the lease was granted and Monado drew
c8's overlay on the glasses, but in their 2D mode as two halves, a
refresh late and floaty; the 3D button then replugged them, c8 drew
them itself beside a Monado that had lost them (two IMU readers, a
stale head: wobble, the ray off to the left). Fixed, untried: the
driver takes the mode from c8 (2D: one picture for both eyes; 3D:
halves) at its real refresh with rolling scanout, Monado renders at
100%, and the glasses coming back restart Monado (XR.md, the Monado
path; `xr/CLAUDE.md`).

Third real session, layered (2026-12-02): text sharper, edges gone,
Start over dimmed windows; but choppy (Monado missing most frames by
~25 ms), and leaving XR left both screens black until the session was
killed. Fixed since, tested nested only (XR.md): leaving XR while
leased (`update_again`, leased glasses count as external), windows'
layers redrawn only when changed (`slate_sig`, `C8_XR_SAME`), Monado
on its graphics compositor (`XRT_COMPOSITOR_COMPUTE=0`), room for the
chrome kept in each window's image. Then, also nested
only: the dot as a layer (step 4's ray; beacons, the floor and
outlines are still ours), the projection left out when empty, Start
and toasts redrawn only on change, `[output glasses]` for the
glasses' mode whatever the port, and Monado stopped through its
stdin, cleanly (it never stopped on SIGTERM, and nested hung on a
present nobody answered). Before the glasses go on again: install
only with the user's say-so, and never change their config or project
mode while Monado holds the lease.
