Skip to content

0028: Touch, mouse and keyboard from Moonlight through XTest

Status: Accepted (owner, 2026-11-24). Proposed (2026-11-17, owner asked for touch; spike on a test device screen)

Context

The server dropped every Moonlight input that was not a pad (M6a ruling: a pad-only console). On the handheld and a phone, tapping Plex, Steam or YouTube is quicker than a pad, and search fields want the on-screen keyboard. Device screens run Gamescope's headless backend, which has no libinput, so a uinput mouse or touchscreen never reaches the X apps. Spike (d000000007c4e, YouTube in front): XTest motion, a click and arrow keys sent to the screen's Xwayland reached the app (the click started a video, Down opened the list), but Gamescope drew no cursor into the PipeWire capture.

Decision

A small daemon in the screen image, couchdogs-input, runs inside Gamescope's session and injects XTest events into that screen's Xwayland. It binds a datagram socket input.sock beside the screen's pipewire-0. The server turns Moonlight's mouse, scroll, keyboard, UTF-8 text and touch packets into JSON datagrams and sends them to the screen the device is watching (its own, or the one it joined), the same screen its pads go to. Touch: one finger taps (click) or drags (button held); two fingers scroll; the press waits until the finger moves or lifts, so a scroll never starts with a click. Relative mouse (Moonlight's trackpad mode) gets a cursor drawn by the daemon in a click-through overlay window, shown only while it moves. The kiosk forwards mouse and keys to Servo, so taps work on the home screen too; a tap on a bottom hint ("A Open", "B Back", ...) acts as that key.

Not chosen: a uinput touchscreen per device (no libinput in headless Gamescope, and on the TV it would need Gamescope to accept a second seat); patching Gamescope for touch (we take it unmodified, ADR 0011).

Consequences

Every datagram names the device it came from, so two devices on one screen (a handheld and a phone joined to the TV) each keep their own touch gesture, held keys, held buttons and scroll remainder, and one leaving lets go of its own only. What they cannot keep apart is X itself: two devices drive one pointer, so they share the buttons and keys that pointer holds, and a button one device presses while the other holds it ends in the app when either lets go. A touch hover is ignored (Moonlight sends it while a pen moves without contact; placing a finger there would drag and then click).

No pinch or multi-finger gestures beyond scroll. A tap lands on X apps as a mouse click, so apps that ignore the mouse stay pad-only. Text is user content: the server and the daemon never log keys or text. A character the keyboard has no keycode for borrows one of up to eight free keycodes, kept for two seconds after the last text because clients fetch the keymap asynchronously, and a slot a burst comes back to waits 80 ms after its last press before its keysym changes; the same is true of a character on a group beyond shift. The overlay cursor is a root-sized window that repaints only the arrow's boxes, so Gamescope keeps treating it as the overlay rather than scaling it up; it shares Gamescope's single overlay slot with toasts while it shows.

Amendment (2026-11-17): Gamescope's own cursor

The spike's "Gamescope drew no cursor into the capture" no longer holds: on the handheld and on the TV, Gamescope's arrow is in the PipeWire capture and stays after the pointer stops (checked with the daemon's overlay unmapped), so the drawn overlay cursor was a second arrow. The overlay cursor is gone; the screen starts Gamescope with -C 3000, which hides its pointer after 3 s still.

Amendment (2026-11-17): apps with their own compositor

Touch did nothing in Steam. Steam runs in its own nested Gamescope (ADR 0021), which reads the pointer through SDL and switches it to relative mode while Steam hides its cursor, so absolute XTest moves on the screen's display never moved Steam's pointer (it sat in the corner). An app like that names its inner X display in the string property COUCHDOGS_INPUT_DISPLAY on its window on the screen's display (Steam's image does it once its window shows); while that window has Gamescope's focus, the daemon sends pointer moves and buttons into the named display, opened as the apps' uid, scaled to fit its size. Keys stay on the screen's display, which SDL passes through. A button is let go where it went down, and a display that goes away with its app is only marked dead. Not chosen: dropping the nested Gamescope (Big Picture would fight the runner for focus again) or --force-grab-cursor (always relative, so a touch still could not place the pointer).

Amendment (2026-11-20): the server draws the pointer into device streams

The Mac saw no pointer at all. Gamescope's PipeWire capture (paint_pipewire in Gamescope 3.16) composites the focused window and its override window only, never the cursor: only the TV's HDMI output (paint_all) has Gamescope's arrow. A capture of the Mac's screen after mouse moves had none. So the server draws it: each stream keeps the device's pointer (an absolute move places it, a relative move adds the screen's pixels and stops at the edges as X does, a click or scroll shows it where it is) and blends pointer.png into that device's frames for 3 s after the last use, over the join ring. A touch places it but hides it (the finger is the pointer), and a switch to another screen hides it until the next mouse input there. Two devices on one screen each see only their own idea of the pointer; X has one. Cost: while it shows, every changed frame is copied once more (the ring's path). Not chosen: patching Gamescope to paint the cursor into the capture (unmodified upstream, ADR 0011).

Amendment (2026-11-27): accented letters through dead keys

Voice typing "vídeos" into YouTube gave "vdeos". Chromium, which every TV app is built on (YouTube's VacuumTube, Plex, Steam's CEF), types nothing for a spare keycode: it sends a keydown for "í" and never inserts it, even with the keysym mapped before the app starts. It does compose a dead key followed by a letter. So the daemon maps six dead keys (grave, acute, circumflex, tilde, diaeresis, cedilla) at start, one per free keycode below the spare pool and at level 0 (Shift with a dead key did not compose), and types an ASCII letter with one of those marks as the dead key, then the letter (Shift on the letter for a capital). Xwayland loads a new keymap after the daemon starts, which wiped them on the TV, so the worker checks them every second and maps an emptied one again before anyone types (mapping it only at the press lost that accent). That covers Portuguese, Spanish, French and German. Other characters keep the spare path, so they still fail in Chromium apps. YouTube's and Plex's TV search boxes read letters from the key code, so there the accent is dropped and the letter stays ("videos acao", "VIDEOS ACAO"); their search finds the same titles. Not chosen: stripping accents everywhere (loses them where a real text box would take them) or injecting text through each app's devtools port (per app, and opens a control port into it).