Compare commits

..
Author SHA1 Message Date
DeeJanuzandClaude Opus 5.5 60667dba1f Pointer: don't put a vanished panel back in the visibility map
The panel-edge test read visible[edgeKey], and when the last panel the cursor touched was
gone from the overlay list, that added it back as hidden. The map then had more entries than
there are handles, which made the 50 ms visibility poll run every frame, and since the last
commit it also counted as a visibility change each time, so unchanged frames were never
reused. The edge test now looks the key up without adding it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:25:16 -06:00
DeeJanuzandClaude Opus 5.5 fcb465a45a Input relay: send mouse motion at most every 4 ms
The relay sent the helper one "move" per SYN_REPORT, so a 1000 Hz mouse sent 1000 datagrams
a second to a helper whose loop runs every 8 ms, and each one went through a dozen sscanf
and strncmp tests in the helper before reaching the move handler. In a 200 ms test at
1000 Hz, 149 reports now make 45 moves with the same total.

flush() on a report now sends only once 4 ms have passed since the last move; tick() sends
the rest when due, and the select timeout shrinks to match. Buttons and the gaze
keys still flush first, unconditionally, so a click lands where the pointer was. In the
helper, "move" is now tested first in the command dispatch, and its handling is one lambda.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:23:47 -06:00
DeeJanuzandClaude Opus 5.5 597551cf23 Pause gesture: look the controllers up every 30 s, not every 3 s
The gesture reader fetched vrserver's /input/getstate.json over HTTP every 3 seconds, the
whole time the relay runs, to notice a controller's root path changing when the 3D mouse
takes or gives back its hand role.

It now looks them up when it connects, when a message comes from a device path it doesn't
know (at most every 3 s; the device is read from the message with two string searches, not
a JSON parse of all 160 a second), 1.5 s after the relay's 3D mouse connects or lets go (the
relay tells it through GamePause.controllers_changed), and otherwise every 30 s. The keys
test's pause stub gets the new method.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:21:09 -06:00
DeeJanuzandClaude Opus 5.5 83850bb1b7 Pointer driver: parse outside the lock, report only changes
Handle() held the state lock through a chain of up to a dozen sscanf calls per command, and
RunFrame, which vrserver calls every frame, takes the same lock, so a burst of commands
(about 116 poses a second, plus moves and buttons) could hold up vrserver's frame. Commands
are now parsed into locals first, and the lock is held only to store the result.

RunFrame also called UpdateBooleanComponent six times and UpdateScalarComponent twice every
frame, and TrackedDevicePoseUpdated every frame even while disconnected. Components now go to
SteamVR only when they change (all of them on the first frame). The pose still goes out every
frame while the device is connected, as a tracked device's should; the disconnected pose goes
out once. The helper now sends a pose only when it changes, so the comment says the driver
keeps the last one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:19:38 -06:00
DeeJanuzandClaude Opus 5.5 d5a15ebc36 Input relay: never block on the pointer helper's socket
The relay sent to @ft_pointer_helper on a blocking socket. When the helper stalled, a
layout placement or grabprobe holds it for seconds while ft-gazed keeps filling its socket at
90 Hz, the relay's one loop blocked with it: keyboards, the volume keys (which must never
reach gamescope), and pausing all stopped until the helper read again.

The socket is non-blocking now. A command the helper doesn't take (EAGAIN) waits in a queue,
and everything after it queues behind it so the order holds; tick() sends what it can on each
loop, and the select timeout drops to 20 ms while anything waits. Mouse moves add up into one
queued move. A scroll notch is dropped rather than queued, since scrolling seconds late is no
use; its release still goes. Presses, releases, show, hide, and the rest are kept, so no
button stays down. The queue holds at most 512 commands. While paused, the configured
pointer's queue still drains, so the releases and "hide" from standing down arrive. A
"vrbind" that hits a full socket is sent again on the next loop instead of being lost.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:18:43 -06:00
DeeJanuzandClaude Opus 5.5 e1f7ccee29 Pointer: skip unchanged work while the pointer is awake
Every frame (about 116 a second) the helper tested the cursor ray against every visible
overlay twice with ComputeOverlayIntersection, set the dot's alpha, width, transform and
visibility (five calls into SteamVR), and sent the driver a pose datagram, even with the
mouse and the head still.

Now a frame reuses the last collision result when the mouse, the anchor (1 mm) and the
eye (5 mm) haven't moved and no overlay showed, hid, or changed handle. The passes still run
at least every 100 ms, since overlays move on their own (a floating window's controls follow
it), and always while dragging. The dots' setters go to SteamVR only when their value changes:
the placement when the dot moved 0.2 mm or the eye 5 mm, which turns or resizes it by well
under 1%, and the width on a 0.5% change. The plain pose goes to the driver only when the
laser's origin moved 0.2 mm or its direction 0.04 deg (0.1 mm where it lands, 15 cm on), and
at least every 100 ms; the driver keeps the last pose and reports it every frame. A tilt's
pose, a placement, or waking sends the next one regardless.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:16:57 -06:00
DeeJanuzandClaude Opus 5.5 0680297efa Pointer: sleep on the command socket while the pointer is off
The main loop slept a fixed 8 ms, about 116 wakeups a second, whether the pointer was awake
or not, and every second it looked up every overlay's handle and read a string property from
all 64 device slots to find its own device.

With the pointer off and hand gestures off, the loop now waits in poll() on its command
socket for up to 250 ms, or 20 ms while mapped Frame controller buttons are being read
(SteamVR input has no event to wait for). A mouse command ends the wait at once. The
headset's activity level, the game check, and the "vrgame" and "gazeawake" repeats keep
going at that pace. The 50 ms visibility poll and the 1 s handle lookups run only while the
pointer is awake, and waking forces both. The device index is looked for only while it's
unknown, and again after SteamVR activates or deactivates a device. The HMD pose history is
kept only with hand gestures on, its one user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:12:33 -06:00
DeeJanuzandClaude Opus 5.5 19032f8963 Pointer: read the overlay list every 20 s, not every second
The helper ran `vrcmd --overlays` once a second while the pointer was awake, and in gaze
mode the pointer never sleeps. Each run is a shell plus vrcmd, a new SteamVR client, about
26 to 30 ms of CPU, so about 3% of a core all the time.

The list is now read every 20 seconds, and at once (at most once a second) when it may have
changed: the pointer waking, the dashboard opening or closing or creating an overlay, the
scene app changing, an "overlays" request, and a left click that hit nothing, which may be
on a panel that came up since. The thread waits on a condition variable instead of waking
every 100 ms, so it sleeps while paused. The main loop looks the keys up again as soon as a
new list is in, rather than at its next 1 s tick. Overlays already on the list still show
and hide within 50 ms, from the IsOverlayVisible poll.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 09:10:30 -06:00
14 changed files with 516 additions and 350 deletions

No files matched your search

+1 -2
View File
@@ -144,8 +144,7 @@ This is an early release, tested on one Steam Frame (SteamOS 0.3.0 build 2026092
- Dragging something from one panel to another (a screen and a floating window) works, but the dragged item's icon doesn't show while the pointer is between panels. - Dragging something from one panel to another (a screen and a floating window) works, but the dragged item's icon doesn't show while the pointer is between panels.
- Gaze mode is only as good as its calibration, and that depends on how the headset sits on your face. If the pointer lands off after you adjust the headset, run Quick check or Calibrate on the Gaze page of Frametop Input Settings. - Gaze mode is only as good as its calibration, and that depends on how the headset sits on your face. If the pointer lands off after you adjust the headset, run Quick check or Calibrate on the Gaze page of Frametop Input Settings.
- On SteamVR's Settings page, the 3D mouse shows a laser beam and a larger hit dot, like a controller. SteamVR doesn't tell other programs where that page is (unlike Steam's pages, such as Library), so the mouse used to miss most of it: clicks went through to a desktop screen behind, and the dot disappeared. As a workaround, on that page only, the laser starts near your eye and SteamVR finds the page itself. See docs/design.md. - On SteamVR's Settings page, the 3D mouse shows a laser beam and a larger hit dot, like a controller. SteamVR doesn't tell other programs where that page is (unlike Steam's pages, such as Library), so the mouse used to miss most of it: clicks went through to a desktop screen behind, and the dot disappeared. As a workaround, on that page only, the laser starts near your eye and SteamVR finds the page itself. See docs/design.md.
- Remote desktop over VNC (Frametop Remote Access in the app menu, or `./desktops.sh remote on`) needs Tailscale on the Frame. It shows the primary screen only. The app turns it on and off, shows the address, and shows, copies, or changes the VNC password. The password is made at random on the Frame and kept in `~/.config/frametop-remote` (only you can read it); VNC limits it to 8 characters, and the tailnet encrypts the connection. Turning it on in a desktop that started with it off takes a desktop restart. It costs almost nothing until a viewer connects; the picture then takes a few seconds to appear. - Remote desktop over VNC (Frametop Remote Access in the app menu, or `./desktops.sh remote on`) needs Tailscale on the Frame. It shows the primary screen only. The app turns it on and off, shows the address, and shows, copies, or changes the VNC password. The password is made at random on the Frame and kept in `~/.config/frametop-remote` (only you can read it); VNC limits it to 8 characters, and the tailnet encrypts the connection. Turning it on in a desktop that started with it off takes a desktop restart.
- The desktop has no blur behind panels and menus, and no window animations, so it leaves the headset's GPU to SteamVR. Turn them back on in the Frametop desktop's System Settings (Desktop Effects, and Animation speed under General Behavior); Frametop won't turn them off again.
- Turning the displays off on a stand only turns their backlight off. SteamVR has no way for other programs to put the headset in standby, so tracking and rendering keep running, and the headset draws nearly its full power. - Turning the displays off on a stand only turns their backlight off. SteamVR has no way for other programs to put the headset in standby, so tracking and rendering keep running, and the headset draws nearly its full power.
## Reporting problems ## Reporting problems
+6 -10
View File
@@ -116,9 +116,9 @@ The driver starts disconnected, because holding the right-hand role while SteamV
### The cursor ### The cursor
Mouse motion turns into yaw and pitch around an anchor, the head position at the last recenter. A ray from the anchor is tested against every visible overlay with `ComputeOverlayIntersection`. On a hit, the cursor sits on that surface; otherwise it floats at `POINTER_DISTANCE`. Since the anchor isn't your current eye position, a second test runs along your line of sight to the cursor point, and anything nearer wins, so the cursor always lands on what you see under it. Overlays in `POINTER_IGNORE` are left out of both tests. A display-only panel, like a performance overlay locked to your view, has no input method, so SteamVR's laser passes through it, but `ComputeOverlayIntersection` still hits it, and the cursor stuck to it. The laser starts just before the cursor point, so an ignored panel nearer to you doesn't catch it either. Mouse motion turns into yaw and pitch around an anchor, the head position at the last recenter. A ray from the anchor is tested against every visible overlay with `ComputeOverlayIntersection`. On a hit, the cursor sits on that surface; otherwise it floats at `POINTER_DISTANCE`. Since the anchor isn't your current eye position, a second test runs along your line of sight to the cursor point, and anything nearer wins, so the cursor always lands on what you see under it. Overlays in `POINTER_IGNORE` are left out of both tests. A display-only panel, like a performance overlay locked to your view, has no input method, so SteamVR's laser passes through it, but `ComputeOverlayIntersection` still hits it, and the cursor stuck to it. The laser starts just before the cursor point, so an ignored panel nearer to you doesn't catch it either. Both tests ask SteamVR about every visible overlay, so a frame where nothing moved (the mouse, the anchor, the eye by more than 5 mm, which overlays show) reuses the last result, for up to 100 ms, since overlays can also move on their own. The dots' overlay settings go to SteamVR only when they change, and the pose goes to the driver, which keeps the last one, only when the laser would land 0.1 mm or more elsewhere, and at least every 100 ms.
OpenVR has no call to list other programs' overlays, so the helper runs `vrcmd --overlays` in the background. It includes hidden overlays, because a floating window's controls only appear while something hovers the window, and the cursor has to find them immediately. OpenVR has no call to list other programs' overlays, so the helper runs `vrcmd --overlays` in the background. It includes hidden overlays, because a floating window's controls only appear while something hovers the window, and the cursor has to find them immediately. Each run is a shell and a new SteamVR client, about 30 ms of CPU, and it ran every second while the pointer was awake, which in gaze mode is all the time. Now it runs every 20 seconds, and at once when the pointer wakes, when the dashboard opens or closes, when a game starts or ends, and when a left click hits nothing (a panel that came up since). An overlay already on the list showing or hiding needs no new list: the helper checks the visibility of the ones it knows every 50 ms.
The laser starts partway along your line of sight to the cursor rather than at your eye. SteamVR sizes its hit dot by distance from the laser's origin, and a laser from the eye still shows a beam in each eye. Starting it close to the target makes the beam and the dot tiny, while `POINTER_ORIGIN_MARGIN` keeps the origin in front of the small window controls, which float a few centimetres in front of their panels. The helper's own white dot is the visible cursor. In empty space it's an interactive overlay that the laser lands on, so SteamVR never draws a laser into nothing. The laser starts partway along your line of sight to the cursor rather than at your eye. SteamVR sizes its hit dot by distance from the laser's origin, and a laser from the eye still shows a beam in each eye. Starting it close to the target makes the beam and the dot tiny, while `POINTER_ORIGIN_MARGIN` keeps the origin in front of the small window controls, which float a few centimetres in front of their panels. The helper's own white dot is the visible cursor. In empty space it's an interactive overlay that the laser lands on, so SteamVR never draws a laser into nothing.
@@ -141,7 +141,7 @@ Replacing a loaded driver's files, as re-running the installer used to do, leave
The dashboard follows whichever device summoned it or last pressed its trigger. Frametop adds "last used wins": moving a real controller releases the pointer, and the next mouse movement takes the laser back. Moving means faster than 0.35 m/s or 2 rad/s (both times `POINTER_CONTROLLER_PICKUP`, 1 by default) for 100 ms in a row, while the controller is tracked normally. A single sample over the limit used to be enough, and controllers resting on a desk took the laser back on a knock or a tracking jump while the mouse was in use. Small movements don't count; waking needs `POINTER_WAKE_COUNTS` of mouse motion within a second, so desk jitter doesn't steal the laser. While the pointer is awake, a tiny transparent overlay with `MakeOverlaysInteractiveIfVisible` keeps SteamVR's laser mouse on, since otherwise the first click would only switch the laser on. The dashboard follows whichever device summoned it or last pressed its trigger. Frametop adds "last used wins": moving a real controller releases the pointer, and the next mouse movement takes the laser back. Moving means faster than 0.35 m/s or 2 rad/s (both times `POINTER_CONTROLLER_PICKUP`, 1 by default) for 100 ms in a row, while the controller is tracked normally. A single sample over the limit used to be enough, and controllers resting on a desk took the laser back on a knock or a tracking jump while the mouse was in use. Small movements don't count; waking needs `POINTER_WAKE_COUNTS` of mouse motion within a second, so desk jitter doesn't steal the laser. While the pointer is awake, a tiny transparent overlay with `MakeOverlaysInteractiveIfVisible` keeps SteamVR's laser mouse on, since otherwise the first click would only switch the laser on.
When the headset comes off, SteamVR reports its activity level as idle at once and turns the displays off 5 seconds later (`power.turnOffScreensTimeout`), unless something keeps it awake. An awake pointer did, and so did the helper's `vrcmd` runs: each is a new SteamVR client, and a new client every second kept SteamVR out of standby. The helper now releases the pointer as soon as the headset is idle, ignores the mouse until you're wearing it again, and pauses the overlay list whenever the pointer is off. When the headset comes off, SteamVR reports its activity level as idle at once and turns the displays off 5 seconds later (`power.turnOffScreensTimeout`), unless something keeps it awake. An awake pointer did, and so did the helper's `vrcmd` runs: each is a new SteamVR client, and a new client every second kept SteamVR out of standby. The helper now releases the pointer as soon as the headset is idle, ignores the mouse until you're wearing it again, and pauses the overlay list whenever the pointer is off. With the pointer off it also stops running its loop every 8 ms, about 116 wakeups a second for nothing: it waits up to 250 ms for a command on its socket (20 ms while it reads mapped controller buttons, which SteamVR input only offers by polling), and leaves the overlay lookups until the pointer wakes.
### Moving floating windows ### Moving floating windows
@@ -153,6 +153,8 @@ SteamVR opens every input device only when it starts. When a Bluetooth mouse sle
Keyboards aren't grabbed by default, because a grabbed keyboard's keys went into a virtual keyboard nothing typed from; the relay forwards them to ft-screens instead. Keyboards aren't grabbed by default, because a grabbed keyboard's keys went into a virtual keyboard nothing typed from; the relay forwards them to ft-screens instead.
The relay never waits on the pointer helper. Its socket to the helper used to block, so when the helper stalled (a layout placement or `grabprobe` holds it for seconds, and the gaze service fills its socket 90 times a second meanwhile), the whole relay stopped with it: keyboards, the volume keys, and pausing. Now what the helper doesn't take waits in order and goes out on the next loops. Mouse moves add up into one while they wait, and a scroll notch is dropped, since scrolling seconds late is no use; presses and releases are kept, so no button stays down. Mouse motion goes to the helper at most every 4 ms, rather than once per report, which from a 1000 Hz mouse was 1000 datagrams a second to a helper that runs every 8 ms; a button sends the motion before it first, so the click lands where the pointer was.
An ungrabbed keyboard reaches both sides at once. In VR, gamescope reads every input device itself (the SteamOS build's `InputStealer`, libinput with udev hotplug, so new devices too) and types into its focused app, and ft-screens types the same keys into the desktop. So Space in the desktop also paused Spotify on the dashboard. Typing now follows the last click. ft-screens sees clicks on its own screens, from the mouse or a controller. A click anywhere else is only visible for the mouse: overlay apps get SteamVR's `OverlayFocusChanged` (which panel the laser is on) but no controller button events, so the pointer helper reports the panel under the dot on each left press. ft-screens tells the relay where typing goes every second, from an unbound socket so the relay's replies can't loop back into its control socket, and the relay grabs pass-through keyboards while it's the desktop. A grab waits until the keyboard has no key down, so no key stays held on either side, and the relay lets go if ft-screens stops reporting. A program that reads every keyboard for a hotkey (a dictation tool, say) loses a grabbed keyboard. Repeating the keys on another input device doesn't work: gamescope reads that device too, whether it's the relay's virtual keyboard or one created later, and every Space, typed or dictated, paused Spotify again. So with `SHARE_KEYS=1` the relay sends a grabbed keyboard's keys to `@frametop_keys` as datagrams (`key <code> <value> <device name>`). It's off by default, because the relay can't tell who is listening: abstract sockets have no permissions, and any local process that binds the name first gets every key typed into the desktop, passwords included. A listener should accept only its own user (`SO_PASSCRED`) and skip any keyboard of its own that the relay grabs too. An ungrabbed keyboard reaches both sides at once. In VR, gamescope reads every input device itself (the SteamOS build's `InputStealer`, libinput with udev hotplug, so new devices too) and types into its focused app, and ft-screens types the same keys into the desktop. So Space in the desktop also paused Spotify on the dashboard. Typing now follows the last click. ft-screens sees clicks on its own screens, from the mouse or a controller. A click anywhere else is only visible for the mouse: overlay apps get SteamVR's `OverlayFocusChanged` (which panel the laser is on) but no controller button events, so the pointer helper reports the panel under the dot on each left press. ft-screens tells the relay where typing goes every second, from an unbound socket so the relay's replies can't loop back into its control socket, and the relay grabs pass-through keyboards while it's the desktop. A grab waits until the keyboard has no key down, so no key stays held on either side, and the relay lets go if ft-screens stops reporting. A program that reads every keyboard for a hotkey (a dictation tool, say) loses a grabbed keyboard. Repeating the keys on another input device doesn't work: gamescope reads that device too, whether it's the relay's virtual keyboard or one created later, and every Space, typed or dictated, paused Spotify again. So with `SHARE_KEYS=1` the relay sends a grabbed keyboard's keys to `@frametop_keys` as datagrams (`key <code> <value> <device name>`). It's off by default, because the relay can't tell who is listening: abstract sockets have no permissions, and any local process that binds the name first gets every key typed into the desktop, passwords included. A listener should accept only its own user (`SO_PASSCRED`) and skip any keyboard of its own that the relay grabs too.
Volume keys must never reach gamescope. With the openvr backend, gamescope sends volume up and down to Steam by moving keyboard focus to Steam for the key and then back to the previously focused surface. When nothing had focus, the one it moves back to is null, and wlroots aborts on a null focus surface (`wlr_seat_keyboard_notify_enter: Assertion 'surface' failed`), which ends the whole VR session. Keyboard focus is often empty while you work in VR, so one press of the headset's volume button could take everything down. gamescope reads the headset's buttons and every keyboard itself (`InputStealer`), as do SteamVR's processes, so the relay has to stop volume keys at the device. Grabbing `gpio-keys` would also take the headset's click button, so the relay remaps the volume entries in each device's keymap (`EVIOCSKEYCODE`) and handles the stand-in codes itself. That fix covers every device at once, including keyboards that aren't grabbed. Volume keys must never reach gamescope. With the openvr backend, gamescope sends volume up and down to Steam by moving keyboard focus to Steam for the key and then back to the previously focused surface. When nothing had focus, the one it moves back to is null, and wlroots aborts on a null focus surface (`wlr_seat_keyboard_notify_enter: Assertion 'surface' failed`), which ends the whole VR session. Keyboard focus is often empty while you work in VR, so one press of the headset's volume button could take everything down. gamescope reads the headset's buttons and every keyboard itself (`InputStealer`), as do SteamVR's processes, so the relay has to stop volume keys at the device. Grabbing `gpio-keys` would also take the headset's click button, so the relay remaps the volume entries in each device's keymap (`EVIOCSKEYCODE`) and handles the stand-in codes itself. That fix covers every device at once, including keyboards that aren't grabbed.
@@ -177,12 +179,6 @@ The private runtime directory also moves the session's document portal to `$XDG_
A podman container's monitor process (conmon) stays in the cgroup of whatever started the container, and `distrobox enter` starts it on demand. When a Frametop service happened to start the `dev` container, stopping that service stopped the container and everything in it, including the desktop's compositor. `scripts/container-up.sh` starts the container in a systemd scope of its own before anything enters it. It then waits for distrobox-init to log `container_setup_done`, as `distrobox enter` does only for containers it starts itself. A new container's first start takes a minute or more (it installs distrobox's dependencies and sets up passwordless sudo), and an install that entered right away met a sudo password prompt with no terminal to answer it ([#9](https://github.com/DeeJanuz/frametop/issues/9)). A podman container's monitor process (conmon) stays in the cgroup of whatever started the container, and `distrobox enter` starts it on demand. When a Frametop service happened to start the `dev` container, stopping that service stopped the container and everything in it, including the desktop's compositor. `scripts/container-up.sh` starts the container in a systemd scope of its own before anything enters it. It then waits for distrobox-init to log `container_setup_done`, as `distrobox enter` does only for containers it starts itself. A new container's first start takes a minute or more (it installs distrobox's dependencies and sets up passwordless sudo), and an install that entered right away met a sudo password prompt with no terminal to answer it ([#9](https://github.com/DeeJanuz/frametop/issues/9)).
KWin renders with OpenGL through zink on Turnip, Vulkan on the same GPU vrcompositor needs to hit its frame time, and on the Frame that costs CPU too. The nested session started with KWin's defaults: blur and background contrast on (no `[Plugins]` group in its kwinrc) and animations at full length. Blur re-renders what's behind every translucent panel and menu each time it changes, and every animated frame is one more frame for KWin and ft-screens to draw and send. They're off by default in the Frametop desktop. The session script writes them before KWin starts, only where the desktop's own config has no value, once: System Settings deletes a setting put back to KDE's default rather than writing it, so without the marker in `frametoprc` a user who turned blur back on would lose it at the next start. The effect ids (`blur`, `contrast`) are the ones built into KWin 6.2.5 on SteamOS; KWin reads `<id>Enabled` from `[Plugins]`.
The nested session also runs the system's XDG autostart entries, being a KDE session. Discover's update notifier started `plasma-discover --mode update` in it (520 to 620 MB resident and about 9% of a core, plus `flatpak-system-helper` and AppStream downloads), and IBus started a daemon, the kimpanel panel and its GTK extension that nothing can use: KWin hands text input to the one input method it starts (`ft-textinput`), and the session drops `QT_IM_MODULE`, `GTK_IM_MODULE` and `XMODIFIERS`. The session hides both for this desktop only, with `Hidden=true` copies in its own autostart folder. The geoclue demo agent stays: it's what answers apps' location requests to Geoclue outside GNOME, and it costs nothing while idle. Orca's entry only starts in GNOME-family desktops.
Remote desktop is a chain (krdp, then FreeRDP inside Xvnc) because nothing on SteamOS serves KWin over VNC directly. Kept connected all the time, it cost about a core with nobody watching: krdpserver 55 to 78% (it encodes H.264 in software with openh264: VA-API finds no driver for the Frame's GPU in the container), FreeRDP 16 to 27%, Xvnc 6 to 11%, and the bridge's layout check every 5 seconds another 4%. krdp creates its screencast session per RDP connection and drops it when the connection closes (`SessionController::onNewConnection` in krdp 6.7), so an idle krdpserver costs nothing and can stay up; only the RDP connection has to go. The bridge connects FreeRDP when a VNC client appears and disconnects 45 seconds after the last one leaves. Xvnc has no hook for its clients, so the bridge counts established connections to its port with `ss`, woken early by Xvnc's log output; looking with `ss` once a second cost about 0.9% of a core in bash, against about 0.1% this way. `Xvnc -inetd` from a systemd socket would start a server per connection and lose sharing between viewers. The layout check (`ft-layout remote-view`, which scans `/proc` for plasmashell and runs `kscreen-doctor -j`) now runs only while FreeRDP runs, and then only after `kwinoutputconfig.json` or `frametop-layout.json` changes, with one check a minute in case a change touched neither.
Program names stay within 15 characters, because Linux truncates process names there and the scripts find programs with `pgrep -x` and `pkill -x`. That's why the prefix is `ft-`. Program names stay within 15 characters, because Linux truncates process names there and the scripts find programs with `pgrep -x` and `pkill -x`. That's why the prefix is `ft-`.
## Displays off on a stand ## Displays off on a stand
@@ -201,7 +197,7 @@ Hiding the screens during a game kept them out of view, but Frametop kept using
- A hidden screen still cost as much as a visible one. ft-screens sent every committed screen its frame callback at 90 Hz whether its overlay showed or not, so KWin kept drawing, and its apps with it. Paused, ft-screens sends the callbacks once a second. A Wayland client draws again only after its last frame's callback, so KWin's output stalls, KWin's own clients stop getting theirs, and the whole desktop idles, without anything losing its connection. A second's pace, rather than none, keeps any client that waits on a callback from waiting forever. Stopping KWin or the apps with SIGSTOP would free the same, but a Wayland peer that stops reading overflows the other side's 4 KB socket buffer, which ends the connection: that's how the live desktop died once when its KWin stalled (`Data too big for buffer`). They also sit in different cgroups (KWin under steam.service when the VR launcher starts it, ft-screens in the dev container's), so no single freeze stops them together. - A hidden screen still cost as much as a visible one. ft-screens sent every committed screen its frame callback at 90 Hz whether its overlay showed or not, so KWin kept drawing, and its apps with it. Paused, ft-screens sends the callbacks once a second. A Wayland client draws again only after its last frame's callback, so KWin's output stalls, KWin's own clients stop getting theirs, and the whole desktop idles, without anything losing its connection. A second's pace, rather than none, keeps any client that waits on a callback from waiting forever. Stopping KWin or the apps with SIGSTOP would free the same, but a Wayland peer that stops reading overflows the other side's 4 KB socket buffer, which ends the connection: that's how the live desktop died once when its KWin stalled (`Data too big for buffer`). They also sit in different cgroups (KWin under steam.service when the VR launcher starts it, ft-screens in the dev container's), so no single freeze stops them together.
- The relay does the pausing because it's the one part that always runs, and the pointer helper keeps running because stopping it leaves its virtual controller connected with its last pose (the driver has no staleness timeout), maybe holding a hand role, with the 3D mouse dead. Releasing it does the job. The helper already checks for a scene app twice a second, so it's what tells the relay a game started. - The relay does the pausing because it's the one part that always runs, and the pointer helper keeps running because stopping it leaves its virtual controller connected with its last pose (the driver has no staleness timeout), maybe holding a hand role, with the 3D mouse dead. Releasing it does the job. The helper already checks for a scene app twice a second, so it's what tells the relay a game started.
- The gesture has to work during a game, but SteamVR input reaches only the app with input focus, and an overlay with global input (`steamvr/globalActionSetPriority`) takes the buttons it binds from the game. vrserver's web socket on 127.0.0.1:27062, which its controller binding page uses for the live view, reports every controller component whatever has focus, and reading it takes nothing. The game sees the clicks too, so the default is a gesture games hardly use: both thumbsticks, together, twice. "Together" means within 0.3 seconds of each other, so a stick held down to sprint while the other clicks doesn't count. The stream is about 160 messages a second, nearly all capacitive sensing, so the reader parses only the few that mention a gesture's button. A controller's root path changes while the 3D mouse holds its hand role (`/devices/cv/<serial>` instead of `/user/hand/right`), so the reader looks the controllers up again every 3 seconds. - The gesture has to work during a game, but SteamVR input reaches only the app with input focus, and an overlay with global input (`steamvr/globalActionSetPriority`) takes the buttons it binds from the game. vrserver's web socket on 127.0.0.1:27062, which its controller binding page uses for the live view, reports every controller component whatever has focus, and reading it takes nothing. The game sees the clicks too, so the default is a gesture games hardly use: both thumbsticks, together, twice. "Together" means within 0.3 seconds of each other, so a stick held down to sprint while the other clicks doesn't count. The stream is about 160 messages a second, nearly all capacitive sensing, so the reader parses only the few that mention a gesture's button. A controller's root path changes while the 3D mouse holds its hand role (`/devices/cv/<serial>` instead of `/user/hand/right`), so the reader looks the controllers up again (an HTTP request to vrserver): when the relay's 3D mouse connects or lets go, when a message comes from a device it doesn't know, and every 30 seconds. It used to be every 3 seconds.
- Resuming starts remote desktop through `systemd-run --scope`: started straight from the relay, it would join the relay's cgroup and end with the next relay restart. - Resuming starts remote desktop through `systemd-run --scope`: started straight from the relay, it would join the relay's cgroup and end with the next relay restart.
## SteamOS updates ## SteamOS updates
-12
View File
@@ -18,16 +18,6 @@ desktops.sh start | stop | restart | status | log [lines]
When the VR launcher starts the desktop, it inherits the Steam client's environment. The session script drops the client's runtime from it (`LD_LIBRARY_PATH`, the `STEAM_*` settings, and the Steam overlay's Vulkan layer), so apps in the desktop use the system's libraries, including its video codecs, just as they would after a normal login. When the VR launcher starts the desktop, it inherits the Steam client's environment. The session script drops the client's runtime from it (`LD_LIBRARY_PATH`, the `STEAM_*` settings, and the Steam overlay's Vulkan layer), so apps in the desktop use the system's libraries, including its video codecs, just as they would after a normal login.
KWin's blur and background contrast effects and its animations are off in this desktop, because KWin draws on the headset's GPU, which SteamVR needs. The session script turns them off once, the first time it starts (it leaves a setting you already have alone, and marks it done in `~/.config/frametop/frametoprc`), so turning them back on sticks. In the Frametop desktop, System Settings → Window Management → Desktop Effects has Blur and Background Contrast, and General Behavior has Animation speed. Or from a terminal, then restart the desktop:
```
kwriteconfig6 --file ~/.config/frametop/kwinrc --group Plugins --key blurEnabled true
kwriteconfig6 --file ~/.config/frametop/kwinrc --group Plugins --key contrastEnabled true
kwriteconfig6 --file ~/.config/frametop/kdeglobals --group KDE --key AnimationDurationFactor 1
```
Two of the system's autostart programs don't start in this desktop: Discover's update notifier (`org.kde.discover.notifier`), which starts Discover to check for updates, and IBus (`ibus`), which can't reach the desktop's apps because KWin's input method is `input/ft-textinput`. The session script puts copies with `Hidden=true` in `~/.config/frametop/autostart` once (marked in `frametoprc`), and skips a name you already have a file for. Delete a copy to start that program again.
Settings are in two files, and Frametop Display Settings edits both. The screens (resolution, width in metres, scale, curve, which one has the taskbar) and their layout are in `~/.config/frametop-layout.json`. The backend, remote desktop, and pointer settings are in `~/.config/frametop.conf`; `session/frametop.conf.example` lists every key. Settings are in two files, and Frametop Display Settings edits both. The screens (resolution, width in metres, scale, curve, which one has the taskbar) and their layout are in `~/.config/frametop-layout.json`. The backend, remote desktop, and pointer settings are in `~/.config/frametop.conf`; `session/frametop.conf.example` lists every key.
Restarting the desktop closes its windows. Before the unit stops, `session/keep-apps.sh` moves every program started in the desktop into a systemd scope of its own, so background work such as servers, tmux, and builds keeps running. An app that shuts down its own helper processes when its window closes will still lose them; run that kind of work outside the desktop, for example as a systemd user service. Restarting the desktop closes its windows. Before the unit stops, `session/keep-apps.sh` moves every program started in the desktop into a systemd scope of its own, so background work such as servers, tmux, and builds keeps running. An app that shuts down its own helper processes when its window closes will still lose them; run that kind of work outside the desktop, for example as a systemd user service.
@@ -269,8 +259,6 @@ It listens on port 5900 on the Frame's Tailscale address only, not the LAN, so i
No VNC server can capture KWin on SteamOS directly: `krfb` needs `xdg-desktop-portal-kde`, which SteamOS doesn't ship, and `wayvnc` only works with wlroots compositors. So `session/remote-desktop.sh` captures the desktop with KDE's `krdpserver --plasma` on `127.0.0.1:3390`, and `session/vnc-bridge.sh` runs TigerVNC's `Xvnc` on display `:20` with a FreeRDP client inside it and serves that. Both run in the `dev` container, and the extra hop adds a little latency. krdp streams every screen; the VNC screen is the primary's size, and the FreeRDP window is shifted so the primary fills it (`ft-layout remote-view` gives the offset). krdp's own `--monitor` would stream just one screen, but it maps the pointer as if that screen sat at 0,0, so clicks would miss. When the layout changes, the VNC screen resizes and FreeRDP reconnects within a few seconds. No VNC server can capture KWin on SteamOS directly: `krfb` needs `xdg-desktop-portal-kde`, which SteamOS doesn't ship, and `wayvnc` only works with wlroots compositors. So `session/remote-desktop.sh` captures the desktop with KDE's `krdpserver --plasma` on `127.0.0.1:3390`, and `session/vnc-bridge.sh` runs TigerVNC's `Xvnc` on display `:20` with a FreeRDP client inside it and serves that. Both run in the `dev` container, and the extra hop adds a little latency. krdp streams every screen; the VNC screen is the primary's size, and the FreeRDP window is shifted so the primary fills it (`ft-layout remote-view` gives the offset). krdp's own `--monitor` would stream just one screen, but it maps the pointer as if that screen sat at 0,0, so clicks would miss. When the layout changes, the VNC screen resizes and FreeRDP reconnects within a few seconds.
FreeRDP runs only while a VNC viewer is connected, because while it's connected krdp captures and encodes every redraw. With no viewer, krdp has no RDP connection and so captures nothing, and Xvnc shows a black screen. When a viewer connects, the bridge starts FreeRDP, and the desktop appears about 3 seconds later; FreeRDP stops 45 seconds after the last viewer leaves (`VNC_IDLE_SEC` in the bridge's environment). The bridge looks for viewers with `ss` whenever Xvnc logs something, as it does for every connection, and every 5 seconds otherwise. While a viewer is connected, the bridge asks ft-screens to draw every screen at full rate (`watch 15` on `@ft_screens`, renewed every 5 seconds), so screens you aren't looking at in the headset, or a headset on a stand, don't stream at a low rate. It reads the primary screen's place again (`ft-layout remote-view`) only while FreeRDP runs, after `~/.config/frametop/kwinoutputconfig.json` or `~/.config/frametop-layout.json` changes, and once a minute.
With remote access on, the nested KWin runs with `KWIN_WAYLAND_NO_PERMISSION_CHECKS=1` and `KWIN_SCREENSHOT_NO_PERMISSION_CHECKS=1`, so any app in the Frametop desktop could capture its screens or inject input. The second one lets scripts take screenshots through KWin's `org.kde.KWin.ScreenShot2` D-Bus interface. This applies only to that desktop, not the stock one. Port 3389 is SteamOS's own `xrdp`, which starts a separate X11 session rather than showing the VR desktop. With remote access on, the nested KWin runs with `KWIN_WAYLAND_NO_PERMISSION_CHECKS=1` and `KWIN_SCREENSHOT_NO_PERMISSION_CHECKS=1`, so any app in the Frametop desktop could capture its screens or inject input. The second one lets scripts take screenshots through KWin's `org.kde.KWin.ScreenShot2` D-Bus interface. This applies only to that desktop, not the stock one. Port 3389 is SteamOS's own `xrdp`, which starts a separate X11 session rather than showing the VR desktop.
## Limits ## Limits
+34 -2
View File
@@ -65,6 +65,18 @@ RESUME_DELAY = 5.0 # after a VR game ends: loading the next one can end one sce
GAME_STALE = 12.0 # the helper repeats "vrgame" every 5 s; silence this long means no game (or no SteamVR) GAME_STALE = 12.0 # the helper repeats "vrgame" every 5 s; silence this long means no game (or no SteamVR)
CHORD_WINDOW = 0.3 # a two-button gesture's buttons go down within this of each other CHORD_WINDOW = 0.3 # a two-button gesture's buttons go down within this of each other
DOUBLE_WINDOW = 0.7 # a double press's second press comes within this of the first DOUBLE_WINDOW = 0.7 # a double press's second press comes within this of the first
LOOKUP_EVERY = 30.0 # the controllers are looked up again this often (and sooner when they may have changed)
LOOKUP_GAP = 3.0 # but not more often than this for a message from a device it doesn't know
def device_of(text):
"""A web socket message's "device", without parsing all of it (there are about 160 a second)."""
at = text.find('"device"')
if at < 0:
return None
start = text.find('"', text.find(":", at) + 1)
end = text.find('"', start + 1)
return text[start + 1:end] if 0 <= start < end else None
def gesture_of(rules, buttons): def gesture_of(rules, buttons):
@@ -137,6 +149,12 @@ class ControllerWatch(threading.Thread):
self.changed = threading.Event() self.changed = threading.Event()
self.connected = False self.connected = False
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.lookup_at = None # look the controllers up again then (see recheck)
def recheck(self, delay=1.5):
"""The controllers' root paths may change soon: the 3D mouse's virtual controller is taking
or giving back its hand role. SteamVR takes a moment to hand the role over."""
self.lookup_at = time.monotonic() + delay
def set_gesture(self, spec): def set_gesture(self, spec):
"""(buttons, presses), or None for no gesture.""" """(buttons, presses), or None for no gesture."""
@@ -176,15 +194,22 @@ class ControllerWatch(threading.Thread):
ws.open(f"frametop_pause_{os.getpid()}") ws.open(f"frametop_pause_{os.getpid()}")
sides = {} # root path -> side sides = {} # root path -> side
next_poll = 0.0 next_poll = 0.0
last_poll = 0.0
while True: while True:
g = self.current() g = self.current()
if g is None: if g is None:
return return
now = time.monotonic() now = time.monotonic()
lookup_at = self.lookup_at
if lookup_at is not None and now >= lookup_at:
self.lookup_at = None
next_poll = now
if now >= next_poll: if now >= next_poll:
# A controller connects later, or changes its root path when the 3D mouse's # A controller connects later, or changes its root path when the 3D mouse's
# virtual controller takes or gives back its hand role. # virtual controller takes or gives back its hand role. Each lookup is an HTTP
next_poll = now + 3.0 # request to vrserver, so it's at connect, every LOOKUP_EVERY, when a message
# comes from a device not on the list, and when the relay says (recheck).
next_poll, last_poll = now + LOOKUP_EVERY, now
found = vrws.controllers() found = vrws.controllers()
if found != sides: if found != sides:
for path in sides.keys() - found.keys(): for path in sides.keys() - found.keys():
@@ -199,6 +224,9 @@ class ControllerWatch(threading.Thread):
f"controller{'s' if len(sides) != 1 else ''} on SteamVR's web socket") f"controller{'s' if len(sides) != 1 else ''} on SteamVR's web socket")
wanted = {b: (b.split("/", 1)[0], f"/input/{b.split('/', 1)[1]}/click") for b in g.buttons} wanted = {b: (b.split("/", 1)[0], f"/input/{b.split('/', 1)[1]}/click") for b in g.buttons}
for text in ws.messages(timeout=1.0): for text in ws.messages(timeout=1.0):
device = device_of(text)
if device and device not in sides:
next_poll = min(next_poll, last_poll + LOOKUP_GAP) # its root path changed, maybe
# About 160 messages a second, nearly all capacitive sensing: parse only the # About 160 messages a second, nearly all capacitive sensing: parse only the
# few that carry one of the gesture's buttons. # few that carry one of the gesture's buttons.
if not any(c in text for _, c in wanted.values()): if not any(c in text for _, c in wanted.values()):
@@ -470,6 +498,10 @@ class GamePause:
return max(0.05, self.resume_at - now) return max(0.05, self.resume_at - now)
return 1.0 if self.game else 3600.0 return 1.0 if self.game else 3600.0
def controllers_changed(self):
"""The 3D mouse took or gave back its hand role, which changes a controller's root path."""
self.watch.recheck()
def helper_started(self): def helper_started(self):
"""The pointer helper (re)started, so SteamVR did too, maybe with the gaze service.""" """The pointer helper (re)started, so SteamVR did too, maybe with the gaze service."""
if self.paused: if self.paused:
+92 -11
View File
@@ -128,6 +128,7 @@ kernel's evdev and uinput interfaces.
""" """
import array import array
import atexit import atexit
import collections
import errno import errno
import fcntl import fcntl
import json import json
@@ -459,14 +460,22 @@ class Pointer:
CLAIM_PULSE = 0.06 # seconds the claim button (switchlaserhand, no click) is held CLAIM_PULSE = 0.06 # seconds the claim button (switchlaserhand, no click) is held
RESUME_PAUSE = 1.5 # mouse idle this long, then moving again, re-claims the laser RESUME_PAUSE = 1.5 # mouse idle this long, then moving again, re-claims the laser
WAKE_WINDOW = 1.0 # seconds in which WAKE_COUNTS of motion must add up WAKE_WINDOW = 1.0 # seconds in which WAKE_COUNTS of motion must add up
QUEUE_MAX = 512 # commands kept while the helper is behind (see send)
MOVE_EVERY = 0.004 # mouse motion goes to the helper at most this often (see flush)
def __init__(self, sensitivity, idle, wake_counts=40): def __init__(self, sensitivity, idle, wake_counts=40):
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) # Never blocks (see send): a stalled helper must not stall the keyboard, volume keys and pausing.
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM | socket.SOCK_NONBLOCK)
# Commands the helper's full socket didn't take yet, in order: text, or [dyaw, dpitch] for
# mouse moves, which add up into one while they wait.
self.queue = collections.deque()
self.behind_logged = -60.0
self.sensitivity = sensitivity # degrees per mouse count self.sensitivity = sensitivity # degrees per mouse count
self.idle = idle self.idle = idle
self.active = False self.active = False
self.last_used = 0.0 self.last_used = 0.0
self.dx = self.dy = 0 self.dx = self.dy = 0
self.move_at = 0.0 # motion last went to the helper then
self.scroll_until = None self.scroll_until = None
self.claim_at = None # when to press the claim button self.claim_at = None # when to press the claim button
self.claim_release = None self.claim_release = None
@@ -479,11 +488,59 @@ class Pointer:
self.pending_since = 0.0 self.pending_since = 0.0
self.gaze_awake_until = 0.0 # the helper's gaze mode keeps the pointer until then self.gaze_awake_until = 0.0 # the helper's gaze mode keeps the pointer until then
def send(self, command): def send(self, command, droppable=False):
"""To the helper, in order, without blocking. While the helper doesn't keep up (place and
grabprobe hold it for seconds, and ft-gazed's 90 Hz gaze fills its socket meanwhile), commands
wait in the queue and go out from tick(). A droppable one (a scroll notch: scrolling seconds
late is no use) is dropped instead; presses, releases and the rest are kept, so no button
stays down. Moves add up (_move)."""
if not self.queue:
try: try:
self.sock.sendto(command.encode(), HELPER) self.sock.sendto(command.encode(), HELPER)
return
except BlockingIOError:
pass
except OSError: except OSError:
pass # helper not running (SteamVR not running) return # helper not running (SteamVR not running)
if not droppable:
self._queue(command)
def _move(self, dyaw, dpitch):
if self.queue and isinstance(self.queue[-1], list):
self.queue[-1][0] += dyaw
self.queue[-1][1] += dpitch
return
if not self.queue:
try:
self.sock.sendto(f"move {dyaw:.4f} {dpitch:.4f}".encode(), HELPER)
return
except BlockingIOError:
pass
except OSError:
return
self._queue([dyaw, dpitch])
def _queue(self, item):
if not self.queue and time.monotonic() - self.behind_logged > 60:
self.behind_logged = time.monotonic()
log("pointer helper is behind: holding its commands (logged once a minute)")
self.queue.append(item)
if len(self.queue) > self.QUEUE_MAX:
self.queue.popleft() # stalled for long: the oldest goes
def drain(self):
"""Send what waits, in order, as far as the helper takes it."""
while self.queue:
item = self.queue[0]
text = f"move {item[0]:.4f} {item[1]:.4f}" if isinstance(item, list) else item
try:
self.sock.sendto(text.encode(), HELPER)
except BlockingIOError:
return
except OSError:
self.queue.clear() # the helper went away: nothing to deliver to
return
self.queue.popleft()
def wake(self, now): def wake(self, now):
if not self.active: if not self.active:
@@ -511,7 +568,7 @@ class Pointer:
elif code == REL_Y: elif code == REL_Y:
self.dy += value self.dy += value
elif code == REL_WHEEL and value: elif code == REL_WHEEL and value:
self.send(f"scroll 0 {1 if value > 0 else -1}") self.send(f"scroll 0 {1 if value > 0 else -1}", droppable=True)
self.scroll_until = now + self.SCROLL_PULSE self.scroll_until = now + self.SCROLL_PULSE
def action(self, name, value, now, source="mouse"): def action(self, name, value, now, source="mouse"):
@@ -543,7 +600,7 @@ class Pointer:
return # the rest act on press return # the rest act on press
elif name in ("scroll_up", "scroll_down"): elif name in ("scroll_up", "scroll_down"):
self.wake(now) self.wake(now)
self.send(f"scroll 0 {1 if name == 'scroll_up' else -1}") self.send(f"scroll 0 {1 if name == 'scroll_up' else -1}", droppable=True)
self.scroll_until = now + self.SCROLL_PULSE self.scroll_until = now + self.SCROLL_PULSE
elif name == "dashboard": elif name == "dashboard":
self.dashboard(now) self.dashboard(now)
@@ -582,10 +639,15 @@ class Pointer:
self.sensitivity *= 1.25 if name == "sens_up" else 0.8 self.sensitivity *= 1.25 if name == "sens_up" else 0.8
log(f"sensitivity {self.sensitivity:.4f} deg/count") log(f"sensitivity {self.sensitivity:.4f} deg/count")
def flush(self): def flush(self, now=None):
if self.dx or self.dy: """Send the motion so far. With now (a mouse's SYN_REPORT), only once MOVE_EVERY has passed
since the last: a 1000 Hz mouse sent the helper, which runs every 8 ms, 1000 datagrams a
second. tick() sends the rest when it's due; a button sends it first, so it lands there."""
if not (self.dx or self.dy) or (now is not None and now - self.move_at < self.MOVE_EVERY):
return
self.move_at = time.monotonic() if now is None else now
# Mouse right turns the ray right (negative yaw); mouse down tilts it down. # Mouse right turns the ray right (negative yaw); mouse down tilts it down.
self.send(f"move {-self.dx * self.sensitivity:.4f} {-self.dy * self.sensitivity:.4f}") self._move(-self.dx * self.sensitivity, -self.dy * self.sensitivity)
self.dx = self.dy = 0 self.dx = self.dy = 0
def dashboard(self, now=None): def dashboard(self, now=None):
@@ -601,6 +663,8 @@ class Pointer:
self.system_at = now + (0.4 if woke else 0.0) self.system_at = now + (0.4 if woke else 0.0)
def tick(self, now): def tick(self, now):
self.drain()
self.flush(now)
if self.system_at is not None and now >= self.system_at: if self.system_at is not None and now >= self.system_at:
self.send("btn system 1") self.send("btn system 1")
self.system_at = None self.system_at = None
@@ -625,7 +689,10 @@ class Pointer:
def timeout(self): def timeout(self):
pending = (self.scroll_until, self.claim_at, self.claim_release, self.system_at, self.system_release) pending = (self.scroll_until, self.claim_at, self.claim_release, self.system_at, self.system_release)
return 0.02 if any(t is not None for t in pending) else 0.5 wait = 0.02 if self.queue or any(t is not None for t in pending) else 0.5
if self.dx or self.dy: # motion held back by flush
wait = max(0.0, min(wait, self.move_at + self.MOVE_EVERY - time.monotonic()))
return wait
def stand_down(self): def stand_down(self):
"""Frametop is pausing: a pulse under way ends now, and the pointer lets go.""" """Frametop is pausing: a pulse under way ends now, and the pointer lets go."""
@@ -729,7 +796,8 @@ def main():
# pointer: the 3D mouse while it's in use, pointer_conf: the one the config asks for (they # pointer: the 3D mouse while it's in use, pointer_conf: the one the config asks for (they
# differ while Frametop is paused). # differ while Frametop is paused).
state = {"pointer": None, "pointer_conf": None, "rules": {}, "share_keys": False, state = {"pointer": None, "pointer_conf": None, "rules": {}, "share_keys": False,
"desktop_until": 0.0, "typing_applied": None, "vr_capture_until": 0.0} "desktop_until": 0.0, "typing_applied": None, "vr_capture_until": 0.0, "vr_bind_retry": False,
"pointer_holding": False}
def pause_changed(paused): def pause_changed(paused):
"""Frametop paused or resumed (game_pause.py): the relay's own part.""" """Frametop paused or resumed (game_pause.py): the relay's own part."""
@@ -779,8 +847,11 @@ def main():
and a not in GAZE_ACTIONS and (not pause.paused or works_paused(a))) or "-" and a not in GAZE_ACTIONS and (not pause.paused or works_paused(a))) or "-"
if state["rules"].get("controller_in_games"): if state["rules"].get("controller_in_games"):
buttons = "+games " + buttons buttons = "+games " + buttons
state["vr_bind_retry"] = False
try: try:
screens_sock.sendto(f"vrbind {buttons}".encode(), HELPER) screens_sock.sendto(f"vrbind {buttons}".encode(), HELPER)
except BlockingIOError:
state["vr_bind_retry"] = True # the helper is behind: again on the next loop
except OSError: except OSError:
pass # helper not running; it says vrhello when it starts pass # helper not running; it says vrhello when it starts
@@ -1233,11 +1304,21 @@ def main():
if added: if added:
apply_roles() apply_roles()
# The 3D mouse connecting or letting go moves a hand role, so the pause gesture's reader
# looks the controllers up again (game_pause.py).
holding = bool(state["pointer_conf"] and state["pointer_conf"].active)
if holding != state["pointer_holding"]:
state["pointer_holding"] = holding
pause.controllers_changed()
ready, _, _ = select.select(list(nodes) + [control], [], [], ready, _, _ = select.select(list(nodes) + [control], [], [],
min(volume.timeout(now, pointer.timeout() if pointer else 0.5), pause.timeout(now))) min(volume.timeout(now, pointer.timeout() if pointer else 0.5), pause.timeout(now)))
now = time.monotonic() now = time.monotonic()
if pointer: if pointer:
pointer.tick(now) pointer.tick(now)
elif state["pointer_conf"]:
state["pointer_conf"].drain() # paused: what waited still goes, in order
if state["vr_bind_retry"]:
vr_bind(now)
volume.tick(now) volume.tick(now)
pause.tick(now) pause.tick(now)
if state["vr_capture_until"] and now >= state["vr_capture_until"]: if state["vr_capture_until"] and now >= state["vr_capture_until"]:
@@ -1319,7 +1400,7 @@ def main():
mouse.emit(etype, code, value) mouse.emit(etype, code, value)
elif etype == EV_SYN and code == SYN_REPORT: elif etype == EV_SYN and code == SYN_REPORT:
if pointer: if pointer:
pointer.flush() pointer.flush(now)
mouse.sync() mouse.sync()
keyboard.sync() keyboard.sync()
+1 -1
View File
@@ -56,7 +56,7 @@ class StubPause:
def configure(self, *args): def configure(self, *args):
pass pass
game_state = tick = helper_started = configure game_state = tick = helper_started = controllers_changed = configure
stub = {} stub = {}
+97 -45
View File
@@ -12,7 +12,7 @@
// aim <yaw_deg> <pitch_deg> absolute direction (yaw 0 = -Z, the SteamVR forward) // aim <yaw_deg> <pitch_deg> absolute direction (yaw 0 = -Z, the SteamVR forward)
// gaze follow the head (origin and direction) again // gaze follow the head (origin and direction) again
// distance <metres> cursor distance from the anchor (default 1.5) // distance <metres> cursor distance from the anchor (default 1.5)
// pose <x> <y> <z> <yaw> <pitch> exact pose, sent every frame by the ft-pointer helper // pose <x> <y> <z> <yaw> <pitch> exact pose, from the ft-pointer helper when it changes (it holds)
// posq <x> <y> <z> <qw> <qx> <qy> <qz> exact pose with a full rotation (tilting a panel while moving it) // posq <x> <y> <z> <qw> <qx> <qy> <qz> exact pose with a full rotation (tilting a panel while moving it)
// btn <name> <0|1> name: trigger, b, x, system, joystick, a (a = claim the laser, no click) // btn <name> <0|1> name: trigger, b, x, system, joystick, a (a = claim the laser, no click)
// scroll <x> <y> joystick deflection -1..1 // scroll <x> <y> joystick deflection -1..1
@@ -224,12 +224,23 @@ public:
pose.result = ok ? TrackingResult_Running_OK : TrackingResult_Uninitialized; pose.result = ok ? TrackingResult_Running_OK : TrackingResult_Uninitialized;
pose.deviceIsConnected = snapshot.visible; pose.deviceIsConnected = snapshot.visible;
pose_ = pose; pose_ = pose;
// Connected, the pose goes out every frame, as SteamVR expects of a tracked device.
// Disconnected, once: it says the same thing every frame after.
if (snapshot.visible || !reportedOff_) {
VRServerDriverHost()->TrackedDevicePoseUpdated(objectId_, pose_, sizeof(DriverPose_t)); VRServerDriverHost()->TrackedDevicePoseUpdated(objectId_, pose_, sizeof(DriverPose_t));
reportedOff_ = !snapshot.visible;
}
// Inputs only when they change (and all of them the first time).
auto input = VRDriverInput(); auto input = VRDriverInput();
for (int i = 0; i < kButtons; ++i) input->UpdateBooleanComponent(buttons_[i], snapshot.buttons[i], 0); for (int i = 0; i < kButtons; ++i)
input->UpdateScalarComponent(scrollX_, snapshot.scrollX, 0); if (!inputsSent_ || snapshot.buttons[i] != sentButtons_[i])
input->UpdateScalarComponent(scrollY_, snapshot.scrollY, 0); input->UpdateBooleanComponent(buttons_[i], sentButtons_[i] = snapshot.buttons[i], 0);
if (!inputsSent_ || snapshot.scrollX != sentScrollX_)
input->UpdateScalarComponent(scrollX_, sentScrollX_ = snapshot.scrollX, 0);
if (!inputsSent_ || snapshot.scrollY != sentScrollY_)
input->UpdateScalarComponent(scrollY_, sentScrollY_ = snapshot.scrollY, 0);
inputsSent_ = true;
} }
private: private:
@@ -239,6 +250,10 @@ private:
int32_t role_ = TrackedControllerRole_RightHand; int32_t role_ = TrackedControllerRole_RightHand;
bool hinted_ = false; // whether the role hint currently claims role_ bool hinted_ = false; // whether the role hint currently claims role_
DriverPose_t pose_{}; DriverPose_t pose_{};
bool reportedOff_ = false; // the disconnected pose has gone out (it isn't repeated)
// What the input components were last set to.
bool inputsSent_ = false, sentButtons_[kButtons] = {};
float sentScrollX_ = 0.f, sentScrollY_ = 0.f;
VRInputComponentHandle_t buttons_[kButtons] = {}; VRInputComponentHandle_t buttons_[kButtons] = {};
VRInputComponentHandle_t scrollX_ = 0, scrollY_ = 0; VRInputComponentHandle_t scrollX_ = 0, scrollY_ = 0;
}; };
@@ -293,65 +308,102 @@ private:
} }
} }
// Parsed first, then applied under the lock, which RunFrame (vrserver's frame) takes too.
void Handle(const char *cmd) { void Handle(const char *cmd) {
std::lock_guard<std::mutex> guard(state_.lock); enum { kNone, kPosq, kPose, kMove, kRecenter, kAim, kGaze, kHide, kShow, kRole, kBtn, kDistance, kScroll };
int kind = kNone;
char name[32]; char name[32];
float a, b; float f[7];
int v; int v = 0, role = 0, button = -1;
float x, y, z, qw, qx, qy, qz; if (std::sscanf(cmd, "posq %f %f %f %f %f %f %f", &f[0], &f[1], &f[2], &f[3], &f[4], &f[5], &f[6]) == 7) {
if (std::sscanf(cmd, "posq %f %f %f %f %f %f %f", &x, &y, &z, &qw, &qx, &qy, &qz) == 7) { kind = kPosq;
} else if (std::sscanf(cmd, "pose %f %f %f %f %f", &f[0], &f[1], &f[2], &f[3], &f[4]) == 5) {
kind = kPose;
} else if (std::sscanf(cmd, "move %f %f", &f[0], &f[1]) == 2) {
kind = kMove;
} else if (std::strncmp(cmd, "recenter", 8) == 0) {
kind = kRecenter;
} else if (std::sscanf(cmd, "aim %f %f", &f[0], &f[1]) == 2) {
kind = kAim;
} else if (std::strncmp(cmd, "gaze", 4) == 0) {
kind = kGaze;
} else if (std::strncmp(cmd, "hide", 4) == 0) {
kind = kHide;
} else if (std::strncmp(cmd, "show", 4) == 0) {
kind = kShow;
} else if (std::sscanf(cmd, "role %31s", name) == 1) {
role = !std::strcmp(name, "left") ? TrackedControllerRole_LeftHand
: !std::strcmp(name, "right") ? TrackedControllerRole_RightHand
: !std::strcmp(name, "stylus") ? TrackedControllerRole_Stylus
: 0;
if (role) kind = kRole;
} else if (std::sscanf(cmd, "btn %31s %d", name, &v) == 2) {
for (int i = 0; i < kButtons; ++i)
if (std::strcmp(name, kButtonNames[i]) == 0) button = i;
if (button >= 0) kind = kBtn;
} else if (std::sscanf(cmd, "distance %f", &f[0]) == 1) {
kind = kDistance;
} else if (std::sscanf(cmd, "scroll %f %f", &f[0], &f[1]) == 2) {
kind = kScroll;
}
if (kind == kNone) return;
std::lock_guard<std::mutex> guard(state_.lock);
switch (kind) {
case kPosq:
state_.explicitPose = true; state_.explicitPose = true;
state_.hasQuat = true; state_.hasQuat = true;
state_.gaze = false; state_.gaze = false;
state_.pos[0] = x; std::memcpy(state_.pos, f, sizeof state_.pos);
state_.pos[1] = y; std::memcpy(state_.quat, f + 3, sizeof state_.quat);
state_.pos[2] = z; break;
state_.quat[0] = qw; case kPose:
state_.quat[1] = qx;
state_.quat[2] = qy;
state_.quat[3] = qz;
} else if (std::sscanf(cmd, "pose %f %f %f %f %f", &x, &y, &z, &a, &b) == 5) {
state_.explicitPose = true; state_.explicitPose = true;
state_.hasQuat = false; state_.hasQuat = false;
state_.gaze = false; state_.gaze = false;
state_.pos[0] = x; std::memcpy(state_.pos, f, sizeof state_.pos);
state_.pos[1] = y; state_.yaw = f[3];
state_.pos[2] = z; state_.pitch = f[4];
state_.yaw = a; break;
state_.pitch = b; case kMove:
} else if (std::sscanf(cmd, "move %f %f", &a, &b) == 2) {
state_.explicitPose = false; state_.explicitPose = false;
if (state_.gaze) state_.recenter = true; // first move starts from the gaze if (state_.gaze) state_.recenter = true; // first move starts from the gaze
state_.yaw += a; // wrap by hand: libm remainder() is GLIBC_2.43 in the build container state_.yaw += f[0]; // wrap by hand: libm remainder() is GLIBC_2.43 in the build container
while (state_.yaw > 180.f) state_.yaw -= 360.f; while (state_.yaw > 180.f) state_.yaw -= 360.f;
while (state_.yaw < -180.f) state_.yaw += 360.f; while (state_.yaw < -180.f) state_.yaw += 360.f;
state_.pitch = std::fmax(-85.f, std::fmin(85.f, state_.pitch + b)); state_.pitch = std::fmax(-85.f, std::fmin(85.f, state_.pitch + f[1]));
} else if (std::strncmp(cmd, "recenter", 8) == 0) { break;
case kRecenter:
state_.explicitPose = false; state_.explicitPose = false;
state_.recenter = true; state_.recenter = true;
} else if (std::sscanf(cmd, "aim %f %f", &a, &b) == 2) { break;
case kAim:
state_.gaze = false; state_.gaze = false;
state_.yaw = a; state_.yaw = f[0];
state_.pitch = b; state_.pitch = f[1];
} else if (std::strncmp(cmd, "gaze", 4) == 0) { break;
case kGaze:
state_.gaze = true; state_.gaze = true;
} else if (std::strncmp(cmd, "hide", 4) == 0) { break;
case kHide:
state_.visible = false; state_.visible = false;
} else if (std::strncmp(cmd, "show", 4) == 0) { break;
case kShow:
state_.visible = true; state_.visible = true;
} else if (std::sscanf(cmd, "role %31s", name) == 1) { break;
state_.role = !std::strcmp(name, "left") ? TrackedControllerRole_LeftHand case kRole:
: !std::strcmp(name, "right") ? TrackedControllerRole_RightHand state_.role = role;
: !std::strcmp(name, "stylus") ? TrackedControllerRole_Stylus break;
: state_.role; case kBtn:
} else if (std::sscanf(cmd, "btn %31s %d", name, &v) == 2) { state_.buttons[button] = v != 0;
for (int i = 0; i < kButtons; ++i) break;
if (std::strcmp(name, kButtonNames[i]) == 0) state_.buttons[i] = v != 0; case kDistance:
} else if (std::sscanf(cmd, "distance %f", &a) == 1) { state_.distance = std::fmax(0.3f, std::fmin(10.f, f[0]));
state_.distance = std::fmax(0.3f, std::fmin(10.f, a)); break;
} else if (std::sscanf(cmd, "scroll %f %f", &a, &b) == 2) { case kScroll:
state_.scrollX = a; state_.scrollX = f[0];
state_.scrollY = b; state_.scrollY = f[1];
break;
} }
} }
+254 -92
View File
@@ -36,6 +36,22 @@
// pointer is off the helper also stops listing overlays with vrcmd, whose connection every // pointer is off the helper also stops listing overlays with vrcmd, whose connection every
// second kept SteamVR from going to standby. // second kept SteamVR from going to standby.
// //
// Idle: while the pointer is off (and hand gestures are off), the main loop waits for a command
// on its socket for up to 250 ms instead of running every 8 ms, or 20 ms while it reads mapped
// Frame controller buttons (vrbuttons.h), which have no event to wait for. What has to go on
// meanwhile still does: the headset's activity level, the game check and the relay's "vrgame"
// and "gazeawake" repeats, and a mouse command wakes it at once. The overlay lookups (the 50 ms
// visibility poll, the once-a-second handles) wait for the pointer to wake, and run right then.
//
// Unchanged frames: while the pointer is awake, a frame where nothing moved reuses the last one's
// result. The collision passes (ComputeOverlayIntersection on every visible overlay, twice) run
// again when the mouse or the anchor moved, the eye moved 5 mm, an overlay showed or hid, or
// 100 ms have passed (overlays also move by themselves), and always while dragging. The dots'
// setters go to SteamVR only when their value changes, and a dot's placement only when it moved
// 0.2 mm or the eye 5 mm (its facing and size then change by well under 1%). The pose goes to the
// driver, which keeps the last one, when the laser's origin moved 0.2 mm or its direction 0.04
// deg (0.1 mm where it lands, 15 cm on), and at least every 100 ms.
//
// Last used wins: when a real controller moves (picked up), the pointer is released // Last used wins: when a real controller moves (picked up), the pointer is released
// (driver "hide", which also drops its hand role hint), so the controller gets // (driver "hide", which also drops its hand role hint), so the controller gets
// its role and laser back. The next mouse input reconnects and claims the laser again. // its role and laser back. The next mouse input reconnects and claims the laser again.
@@ -311,6 +327,7 @@ extern "C" {
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <cmath> #include <cmath>
#include <condition_variable>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
@@ -328,6 +345,7 @@ extern "C" {
#include <fcntl.h> #include <fcntl.h>
#include <fnmatch.h> #include <fnmatch.h>
#include <poll.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -500,29 +518,62 @@ std::string JsonQuote(const std::string &s) {
// public call to enumerate other apps' overlays). Hidden ones are listed too: the // public call to enumerate other apps' overlays). Hidden ones are listed too: the
// window controls under a floating panel only appear while something hovers the // window controls under a floating panel only appear while something hovers the
// panel, and the cursor has to find them the moment they do, not a second later. // panel, and the cursor has to find them the moment they do, not a second later.
// Each vrcmd run is a shell and a new SteamVR client, about 30 ms of CPU, so the list is read
// every 20 seconds (kEvery) while the pointer is awake, and at once when something says it may
// have changed (Kick: the pointer waking, the dashboard opening or closing, a click that hit
// nothing), at most once a second. Showing and hiding the overlays it knows doesn't need a new
// list: the main loop polls their visibility (IsOverlayVisible) every 50 ms.
// Paused while the pointer is off: each vrcmd run connects to SteamVR as a new app, and a new // Paused while the pointer is off: each vrcmd run connects to SteamVR as a new app, and a new
// app every second kept SteamVR (and the headset's displays) from going to standby. // app every second kept SteamVR (and the headset's displays) from going to standby.
// "overlays" requests (Frametop Input Settings' Ignored panels page) refresh the list even // "overlays" requests (Frametop Input Settings' Ignored panels page) refresh the list even
// while paused, and are answered from this thread once it's fresh. // while paused, and are answered from this thread once it's fresh.
class OverlayList { class OverlayList {
public: public:
static constexpr auto kEvery = std::chrono::seconds(20), kGap = std::chrono::seconds(1);
void Start() { void Start() {
out_ = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); out_ = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
thread_ = std::thread([this] { thread_ = std::thread([this] {
std::unique_lock<std::mutex> lk(lock_);
while (running_) { while (running_) {
if (!paused_ || requested_) Refresh(); // Sleeps while paused (until a request or the wake); otherwise until it's time, or
// Wait a second, or less when the pointer wakes (refresh right away then). // a kick once kGap has passed since the last read.
for (int i = 0; i < 10 && running_; ++i) { const auto due = std::max(last_ + kGap, kicked_ ? last_ : last_ + kEvery);
const bool wasPaused = paused_; if (!waiting_.empty()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); } else if (paused_) {
if ((wasPaused && !paused_) || requested_) break; cv_.wait(lk);
continue;
} else if (std::chrono::steady_clock::now() < due) {
cv_.wait_until(lk, due);
continue;
} }
kicked_ = false;
lk.unlock();
Refresh();
lk.lock();
last_ = std::chrono::steady_clock::now();
} }
}); });
} }
void SetPaused(bool paused) { paused_ = paused; } // Unpausing (the pointer woke) reads the list again at once.
void SetPaused(bool paused) {
std::lock_guard<std::mutex> guard(lock_);
if (paused == paused_) return;
if (!paused) kicked_ = true, last_ = {};
paused_ = paused;
cv_.notify_one();
}
// The list may have changed: read it again soon (not while paused).
void Kick() {
std::lock_guard<std::mutex> guard(lock_);
kicked_ = true;
cv_.notify_one();
}
void Stop() { void Stop() {
{
std::lock_guard<std::mutex> guard(lock_);
running_ = false; running_ = false;
cv_.notify_one();
}
if (thread_.joinable()) thread_.join(); if (thread_.joinable()) thread_.join();
} }
std::vector<std::string> Keys() { std::vector<std::string> Keys() {
@@ -531,12 +582,14 @@ public:
for (const auto &e : entries_) keys.push_back(e.key); for (const auto &e : entries_) keys.push_back(e.key);
return keys; return keys;
} }
// Goes up by one each time the list is read, so the main loop knows to look the keys up again.
unsigned Generation() const { return generation_; }
// Answer `to` with {"t":"overlays","list":[{"key","name","visible"}...]} after the next refresh. // Answer `to` with {"t":"overlays","list":[{"key","name","visible"}...]} after the next refresh.
void Request(const sockaddr_un &to, socklen_t len) { void Request(const sockaddr_un &to, socklen_t len) {
if (len <= offsetof(sockaddr_un, sun_path)) return; if (len <= offsetof(sockaddr_un, sun_path)) return;
std::lock_guard<std::mutex> guard(lock_); std::lock_guard<std::mutex> guard(lock_);
if (waiting_.size() < 8) waiting_.push_back({to, len}); if (waiting_.size() < 8) waiting_.push_back({to, len});
requested_ = true; cv_.notify_one();
} }
private: private:
@@ -545,7 +598,6 @@ private:
bool visible; bool visible;
}; };
void Refresh() { void Refresh() {
requested_ = false;
FILE *p = popen("LD_LIBRARY_PATH=/opt/steamvr/bin/linuxarm64 /opt/steamvr/bin/linuxarm64/vrcmd --overlays 2>/dev/null", "r"); FILE *p = popen("LD_LIBRARY_PATH=/opt/steamvr/bin/linuxarm64 /opt/steamvr/bin/linuxarm64/vrcmd --overlays 2>/dev/null", "r");
if (!p) return; if (!p) return;
std::vector<Entry> entries; std::vector<Entry> entries;
@@ -577,6 +629,7 @@ private:
entries_ = std::move(entries); entries_ = std::move(entries);
waiting.swap(waiting_); waiting.swap(waiting_);
} }
++generation_;
if (waiting.empty()) return; if (waiting.empty()) return;
std::string msg = "{\"t\":\"overlays\",\"list\":["; std::string msg = "{\"t\":\"overlays\",\"list\":[";
for (size_t i = 0; i < entries_.size(); ++i) for (size_t i = 0; i < entries_.size(); ++i)
@@ -589,10 +642,11 @@ private:
std::thread thread_; std::thread thread_;
int out_ = -1; int out_ = -1;
std::atomic<bool> paused_{false}; std::mutex lock_; // guards everything below
std::atomic<bool> running_{true}; std::condition_variable cv_;
std::atomic<bool> requested_{false}; bool paused_ = false, running_ = true, kicked_ = false;
std::mutex lock_; std::chrono::steady_clock::time_point last_{}; // the last read
std::atomic<unsigned> generation_{0};
std::vector<Entry> entries_; // written only by the thread; the lock guards readers std::vector<Entry> entries_; // written only by the thread; the lock guards readers
std::vector<std::pair<sockaddr_un, socklen_t>> waiting_; std::vector<std::pair<sockaddr_un, socklen_t>> waiting_;
}; };
@@ -620,6 +674,36 @@ bool Ignored(const std::vector<std::string> &patterns, const std::string &key) {
return false; return false;
} }
vr::HmdMatrix34_t Billboard(Vec3 at, Vec3 eye);
// One of our dots, with what was last set on it: each setter goes to SteamVR only when its value
// changes (see "Unchanged frames" at the top).
struct DotOverlay {
vr::VROverlayHandle_t h = vr::k_ulOverlayHandleInvalid;
int shown = -1; // unknown at first
float alpha = -1, width = -1;
bool placed = false;
Vec3 at, eye;
void Show(bool on) {
if (shown == int(on)) return;
shown = on;
if (on) vr::VROverlay()->ShowOverlay(h);
else vr::VROverlay()->HideOverlay(h);
}
void Alpha(float a) {
if (a != alpha) vr::VROverlay()->SetOverlayAlpha(h, alpha = a);
}
// Width w, at `to`, facing `from`: left as it is for a change under 0.5% in width, and while
// it moved less than 0.2 mm and the eye less than 5 mm.
void Place(Vec3 to, Vec3 from, float w) {
if (std::fabs(w - width) > 0.005f * width) vr::VROverlay()->SetOverlayWidthInMeters(h, width = w);
if (placed && Length(to - at) < 0.0002 && Length(from - eye) < 0.005) return;
placed = true, at = to, eye = from;
const auto m = Billboard(to, from);
vr::VROverlay()->SetOverlayTransformAbsolute(h, vr::TrackingUniverseStanding, &m);
}
};
vr::HmdMatrix34_t Billboard(Vec3 at, Vec3 eye) { vr::HmdMatrix34_t Billboard(Vec3 at, Vec3 eye) {
// Overlay faces +Z; point +Z at the eye, keep +Y roughly up. // Overlay faces +Z; point +Z at the eye, keep +Y roughly up.
const Vec3 z = Normalize(eye - at); const Vec3 z = Normalize(eye - at);
@@ -803,6 +887,8 @@ int main() {
below.m[1][3] = -50; below.m[1][3] = -50;
overlay->SetOverlayTransformTrackedDeviceRelative(laserMode, vr::k_unTrackedDeviceIndex_Hmd, &below); overlay->SetOverlayTransformTrackedDeviceRelative(laserMode, vr::k_unTrackedDeviceIndex_Hmd, &below);
bool laserModeShown = false; bool laserModeShown = false;
DotOverlay cursorDot, markerDot; // the setters for `cursor` and `marker` (see "Unchanged frames" at the top)
cursorDot.h = cursor, markerDot.h = marker;
// Controller beams keep the user's width; nothing here changes it any more. // Controller beams keep the user's width; nothing here changes it any more.
vr::VRSettings()->SetFloat("dashboard", "laserRayWidthScale", laserWidth); vr::VRSettings()->SetFloat("dashboard", "laserRayWidthScale", laserWidth);
@@ -822,12 +908,34 @@ int main() {
std::map<std::string, bool> sceneGraph; // no texture: plane test instead of ComputeOverlayIntersection std::map<std::string, bool> sceneGraph; // no texture: plane test instead of ComputeOverlayIntersection
std::map<std::string, bool> visible; // refreshed every 50 ms std::map<std::string, bool> visible; // refreshed every 50 ms
auto lastVisible = std::chrono::steady_clock::now(); auto lastVisible = std::chrono::steady_clock::now();
auto lastSlow = std::chrono::steady_clock::now() - std::chrono::seconds(10);
unsigned listGeneration = 0; // the overlay list the handles were looked up from
// The plane of the last panel the cursor was on, and the last point on it (panel edges). // The plane of the last panel the cursor was on, and the last point on it (panel edges).
Vec3 edgePoint, edgeNormal, edgeLast; Vec3 edgePoint, edgeNormal, edgeLast;
std::string edgeKey; std::string edgeKey;
bool active = false, recenter = false, anchored = false; bool active = false, recenter = false, anchored = false;
using Clock = std::chrono::steady_clock; using Clock = std::chrono::steady_clock;
// Unchanged frames (see the top): visibleGen goes up when an overlay shows or hides or the
// handles change; pass is the last collision pass, posed the last plain pose sent.
unsigned visibleGen = 0;
struct Pass {
bool valid = false;
Clock::time_point at{};
unsigned gen = 0;
double yaw = 0, pitch = 0;
Vec3 anchor, eye;
double best = 1e9, distance = 0;
std::string key;
bool scene = false, onEdge = false, occluded = false;
Vec3 point;
} pass;
struct Posed {
bool valid = false;
Clock::time_point at{};
Vec3 origin;
double yaw = 0, pitch = 0;
} posed;
Clock::time_point lastMouse{}, claimAt{}, claimRelease{}, wokeAt{}, noWakeUntil{}; Clock::time_point lastMouse{}, claimAt{}, claimRelease{}, wokeAt{}, noWakeUntil{};
bool claimPending = false, claimHeld = false; bool claimPending = false, claimHeld = false;
// Last used wins: since when each controller has been moving (zero: it isn't). // Last used wins: since when each controller has been moving (zero: it isn't).
@@ -864,6 +972,9 @@ int main() {
wokeAt = t; wokeAt = t;
active = true; active = true;
recenter = true; recenter = true;
// Overlay handles and visibility weren't looked at while it was off (see "Idle" at the top).
lastSlow = lastVisible = t - std::chrono::seconds(10);
pass.valid = posed.valid = false;
SendTo(out, "ft_pointer", "role " + role); // POINTER_ROLE, before it takes it SendTo(out, "ft_pointer", "role " + role); // POINTER_ROLE, before it takes it
SendTo(out, "ft_pointer", "show"); SendTo(out, "ft_pointer", "show");
claimPending = true; // take the laser without clicking, once SteamVR has bound the device claimPending = true; // take the laser without clicking, once SteamVR has bound the device
@@ -965,6 +1076,8 @@ int main() {
if (debug) std::fflush(stdout); if (debug) std::fflush(stdout);
} }
nudging = confirmLesson = false; nudging = confirmLesson = false;
// A click on nothing: maybe on an overlay that came up since the list was read.
if (lastHit.empty()) overlays.Kick();
leftHeld = true; leftHeld = true;
dragDistance = lastDistance; dragDistance = lastDistance;
pressKey.clear(); pressKey.clear();
@@ -1088,7 +1201,6 @@ int main() {
return gazeOn && gazeMouseHeld && !inGame && !headsetOff && Clock::now() - gz.at < std::chrono::seconds(1) && return gazeOn && gazeMouseHeld && !inGame && !headsetOff && Clock::now() - gz.at < std::chrono::seconds(1) &&
!aimHeld && !leftHeld && !tilting && hold.src == Src::None && !clickPress && !clickRelease; !aimHeld && !leftHeld && !tilting && hold.src == Src::None && !clickPress && !clickRelease;
}; };
auto lastSlow = std::chrono::steady_clock::now() - std::chrono::seconds(10);
// --- Panel placement (see "Placement" at the top of the file) --- // --- Panel placement (see "Placement" at the top of the file) ---
// Device pose, given in the standing universe, sent to the driver in raw space. // Device pose, given in the standing universe, sent to the driver in raw space.
@@ -1104,6 +1216,7 @@ int main() {
char msg[200]; char msg[200];
std::snprintf(msg, sizeof msg, "posq %.5f %.5f %.5f %.6f %.6f %.6f %.6f", o.x, o.y, o.z, q[0], q[1], q[2], q[3]); std::snprintf(msg, sizeof msg, "posq %.5f %.5f %.5f %.6f %.6f %.6f %.6f", o.x, o.y, o.z, q[0], q[1], q[2], q[3]);
SendTo(out, "ft_pointer", msg); SendTo(out, "ft_pointer", msg);
posed.valid = false;
}; };
auto sleepMs = [](int ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); }; auto sleepMs = [](int ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); };
auto headPos = [&] { auto headPos = [&] {
@@ -1115,8 +1228,8 @@ int main() {
auto borrow = [&] { auto borrow = [&] {
SendTo(out, "ft_pointer", "show"); SendTo(out, "ft_pointer", "show");
overlay->ShowOverlay(laserMode); overlay->ShowOverlay(laserMode);
overlay->HideOverlay(cursor); cursorDot.Show(false);
overlay->HideOverlay(marker); markerDot.Show(false);
sleepMs(active ? 50 : 400); // a fresh connect needs SteamVR to bind the device sleepMs(active ? 50 : 400); // a fresh connect needs SteamVR to bind the device
SendTo(out, "ft_pointer", "btn a 1"); SendTo(out, "ft_pointer", "btn a 1");
sleepMs(60); sleepMs(60);
@@ -1243,6 +1356,36 @@ int main() {
return msg; return msg;
}; };
// "move <dyaw> <dpitch>" from the relay.
auto mouseMove = [&](double a, double b) {
// A move held back (POINTER_GAZE_MOUSE_MOVE=held) only wakes the pointer: it isn't using
// the mouse, so a drifting mouse doesn't keep the gaze from taking the pointer back.
const bool heldBack = mouseMoveHeld();
if (!heldBack) lastMouse = Clock::now();
// Any mouse input wakes the pointer (after a controller took over, or a helper restart).
if (!active) wake(Clock::now());
if (heldBack) return;
if (tilting) {
tiltYaw += a;
tiltPitch = std::clamp(tiltPitch + b, -80.0, 80.0);
return;
}
lastMove = Clock::now(); // the dot shows while the mouse moves it (gaze mode)
if (gazeOn && gazeOwns) {
// The mouse takes the pointer from the gaze, from where the gaze left it.
gazeOwns = false;
nudging = haveHead && Clock::now() - gz.at < std::chrono::milliseconds(200);
nudgeRawHy = gz.rhy, nudgeRawHp = gz.rhp, nudgeHead = lastHead;
nudgeAt = Clock::now(), nudgeMoved = 0;
}
if (nudging || aimHeld) nudgeMoved += std::hypot(a, b);
if (!anchored) recenter = true;
yaw += a;
while (yaw > 180) yaw -= 360;
while (yaw < -180) yaw += 360;
pitch = std::clamp(pitch + b, -85.0, 85.0);
};
std::printf("ft-pointer running: free distance %.2f m, dot %.2f deg\n", freeDistance, cursorDeg); std::printf("ft-pointer running: free distance %.2f m, dot %.2f deg\n", freeDistance, cursorDeg);
std::fflush(stdout); std::fflush(stdout);
@@ -1257,8 +1400,8 @@ int main() {
if (headsetOff && active) { if (headsetOff && active) {
active = false; active = false;
claimPending = claimHeld = false; claimPending = claimHeld = false;
overlay->HideOverlay(cursor); cursorDot.Show(false);
overlay->HideOverlay(marker); markerDot.Show(false);
SendTo(out, "ft_pointer", "btn a 0"); SendTo(out, "ft_pointer", "btn a 0");
SendTo(out, "ft_pointer", "hide"); SendTo(out, "ft_pointer", "hide");
std::printf("headset off: pointer released\n"); std::printf("headset off: pointer released\n");
@@ -1308,6 +1451,12 @@ int main() {
if (senderLen > offsetof(sockaddr_un, sun_path)) if (senderLen > offsetof(sockaddr_un, sun_path))
sendto(out, msg.data(), msg.size(), 0, reinterpret_cast<const sockaddr *>(&sender), senderLen); sendto(out, msg.data(), msg.size(), 0, reinterpret_cast<const sockaddr *>(&sender), senderLen);
}; };
// Mouse moves first, the most frequent command.
double mx, my;
if (std::strncmp(buf, "move ", 5) == 0 && std::sscanf(buf + 5, "%lf %lf", &mx, &my) == 2) {
mouseMove(mx, my);
continue;
}
// The gaze, from ft-gazed: not mouse input, it never wakes the pointer. // The gaze, from ft-gazed: not mouse input, it never wakes the pointer.
double g[4]; double g[4];
if (std::sscanf(buf, "gz %lf %lf %lf %lf", &g[0], &g[1], &g[2], &g[3]) == 4) { if (std::sscanf(buf, "gz %lf %lf %lf %lf", &g[0], &g[1], &g[2], &g[3]) == 4) {
@@ -1396,16 +1545,11 @@ int main() {
reply(gazeOn ? "ok on" : "ok off"); reply(gazeOn ? "ok on" : "ok off");
continue; continue;
} }
const bool mouseInput = std::strncmp(buf, "move", 4) == 0 || std::strncmp(buf, "btn", 3) == 0 || // (Moves were taken first, above.)
std::strncmp(buf, "scroll", 6) == 0; const bool mouseInput = std::strncmp(buf, "btn", 3) == 0 || std::strncmp(buf, "scroll", 6) == 0;
// A move held back (POINTER_GAZE_MOUSE_MOVE=held) only wakes the pointer: it isn't using if (mouseInput) lastMouse = Clock::now();
// the mouse, so a drifting mouse doesn't keep the gaze from taking the pointer back.
const bool moveHeldBack = std::strncmp(buf, "move", 4) == 0 && mouseMoveHeld();
if (mouseInput && !moveHeldBack) lastMouse = Clock::now();
// Any mouse input wakes the pointer (after a controller took over, or a helper restart). // Any mouse input wakes the pointer (after a controller took over, or a helper restart).
if (!active && mouseInput) wake(Clock::now()); if (!active && mouseInput) wake(Clock::now());
if (moveHeldBack) continue;
double a, b;
char key[128]; char key[128];
double px, py, pz, pyaw, ppitch, proll = 0, pgrab = -1; double px, py, pz, pyaw, ppitch, proll = 0, pgrab = -1;
if (std::sscanf(buf, "grabprobe %127s", key) == 1) { if (std::sscanf(buf, "grabprobe %127s", key) == 1) {
@@ -1464,31 +1608,12 @@ int main() {
tilting = swallowedRight = false; tilting = swallowedRight = false;
continue; continue;
} }
if (tilting && std::sscanf(buf, "move %lf %lf", &a, &b) == 2) { if (std::strncmp(buf, "recenter", 8) == 0) {
tiltYaw += a;
tiltPitch = std::clamp(tiltPitch + b, -80.0, 80.0);
continue;
}
if (std::sscanf(buf, "move %lf %lf", &a, &b) == 2) {
lastMove = Clock::now(); // the dot shows while the mouse moves it (gaze mode)
if (gazeOn && gazeOwns) {
// The mouse takes the pointer from the gaze, from where the gaze left it.
gazeOwns = false;
nudging = haveHead && Clock::now() - gz.at < std::chrono::milliseconds(200);
nudgeRawHy = gz.rhy, nudgeRawHp = gz.rhp, nudgeHead = lastHead;
nudgeAt = Clock::now(), nudgeMoved = 0;
}
if (nudging || aimHeld) nudgeMoved += std::hypot(a, b);
if (!anchored) recenter = true;
yaw += a;
while (yaw > 180) yaw -= 360;
while (yaw < -180) yaw += 360;
pitch = std::clamp(pitch + b, -85.0, 85.0);
} else if (std::strncmp(buf, "recenter", 8) == 0) {
recenter = true; recenter = true;
} else if (std::strncmp(buf, "reload", 6) == 0) { } else if (std::strncmp(buf, "reload", 6) == 0) {
loadConfig(); loadConfig();
lastSlow = Clock::now() - std::chrono::seconds(10); // apply POINTER_IGNORE now lastSlow = Clock::now() - std::chrono::seconds(10); // apply POINTER_IGNORE now
pass.valid = false;
std::printf("reloaded: free distance %.2f m, dot %.2f deg, origin %.2f, head follow %s, leash %.0f deg, " std::printf("reloaded: free distance %.2f m, dot %.2f deg, origin %.2f, head follow %s, leash %.0f deg, "
"controller pickup %.1fx, %zu ignored\n", "controller pickup %.1fx, %zu ignored\n",
freeDistance, cursorDeg, originFraction, follow ? "on" : "off", leashDeg, pickupScale, freeDistance, cursorDeg, originFraction, follow ? "on" : "off", leashDeg, pickupScale,
@@ -1506,8 +1631,8 @@ int main() {
if (!active) wake(Clock::now()); if (!active) wake(Clock::now());
} else if (std::strncmp(buf, "hide", 4) == 0) { } else if (std::strncmp(buf, "hide", 4) == 0) {
active = false; active = false;
overlay->HideOverlay(cursor); cursorDot.Show(false);
overlay->HideOverlay(marker); markerDot.Show(false);
SendTo(out, "ft_pointer", "hide"); SendTo(out, "ft_pointer", "hide");
} else { } else {
SendTo(out, "ft_pointer", buf); // btn, scroll SendTo(out, "ft_pointer", buf); // btn, scroll
@@ -1549,8 +1674,8 @@ int main() {
sys->GetControllerRoleForTrackedDeviceIndex(ours) == vr::TrackedControllerRole_Invalid) { sys->GetControllerRoleForTrackedDeviceIndex(ours) == vr::TrackedControllerRole_Invalid) {
active = false; active = false;
claimPending = claimHeld = false; claimPending = claimHeld = false;
overlay->HideOverlay(cursor); cursorDot.Show(false);
overlay->HideOverlay(marker); markerDot.Show(false);
SendTo(out, "ft_pointer", "btn a 0"); SendTo(out, "ft_pointer", "btn a 0");
SendTo(out, "ft_pointer", "hide"); SendTo(out, "ft_pointer", "hide");
noWakeUntil = tnow + std::chrono::seconds(2); noWakeUntil = tnow + std::chrono::seconds(2);
@@ -1578,8 +1703,8 @@ int main() {
if (tnow - movingSince[i] >= std::chrono::milliseconds(100)) { if (tnow - movingSince[i] >= std::chrono::milliseconds(100)) {
active = false; active = false;
claimPending = claimHeld = false; claimPending = claimHeld = false;
overlay->HideOverlay(cursor); cursorDot.Show(false);
overlay->HideOverlay(marker); markerDot.Show(false);
SendTo(out, "ft_pointer", "btn a 0"); SendTo(out, "ft_pointer", "btn a 0");
SendTo(out, "ft_pointer", "hide"); SendTo(out, "ft_pointer", "hide");
std::printf("controller %u moved (%.2f m/s, %.1f rad/s): pointer released\n", i, speed, spin); std::printf("controller %u moved (%.2f m/s, %.1f rad/s): pointer released\n", i, speed, spin);
@@ -1629,7 +1754,7 @@ int main() {
} }
// Hands (see the top): pinches and grips from ft-hands. // Hands (see the top): pinches and grips from ft-hands.
{ if (handsOn) {
vr::TrackedDevicePose_t h0; vr::TrackedDevicePose_t h0;
sys->GetDeviceToAbsoluteTrackingPose(vr::TrackingUniverseStanding, 0, &h0, 1); sys->GetDeviceToAbsoluteTrackingPose(vr::TrackingUniverseStanding, 0, &h0, 1);
if (h0.bPoseIsValid) poses.Add(tnow, h0.mDeviceToAbsoluteTracking); if (h0.bPoseIsValid) poses.Add(tnow, h0.mDeviceToAbsoluteTracking);
@@ -1928,10 +2053,13 @@ int main() {
} }
followAt = tnow; followAt = tnow;
// Slow work, once a second: overlay handles, our device index, laser width. // Slow work, once a second and when the overlay list is new: overlay handles, our device
// index. Only while the pointer is awake (see "Idle" at the top).
const auto now = std::chrono::steady_clock::now(); const auto now = std::chrono::steady_clock::now();
if (now - lastSlow > std::chrono::seconds(1)) { if (active && (now - lastSlow > std::chrono::seconds(1) || overlays.Generation() != listGeneration)) {
lastSlow = now; lastSlow = now;
listGeneration = overlays.Generation();
const auto before = handles;
handles.clear(); handles.clear();
for (const auto &key : overlays.Keys()) { for (const auto &key : overlays.Keys()) {
if (Ignored(ignore, key)) continue; if (Ignored(ignore, key)) continue;
@@ -1951,19 +2079,21 @@ int main() {
sceneGraph[key] = (tw == 0 || th == 0) && tt == vr::VROverlayTransform_Absolute && sceneGraph[key] = (tw == 0 || th == 0) && tt == vr::VROverlayTransform_Absolute &&
key.rfind("frametop.", 0) != 0; key.rfind("frametop.", 0) != 0;
} }
ours = vr::k_unTrackedDeviceIndexInvalid; if (handles != before) ++visibleGen;
for (vr::TrackedDeviceIndex_t i = 0; i < vr::k_unMaxTrackedDeviceCount; ++i) { // Our device keeps its index; it's looked for again when a device is activated or
// deactivated (the events below).
for (vr::TrackedDeviceIndex_t i = 0; ours == vr::k_unTrackedDeviceIndexInvalid && i < vr::k_unMaxTrackedDeviceCount; ++i) {
char type[64] = ""; char type[64] = "";
sys->GetStringTrackedDeviceProperty(i, vr::Prop_ControllerType_String, type, sizeof type); sys->GetStringTrackedDeviceProperty(i, vr::Prop_ControllerType_String, type, sizeof type);
if (std::strcmp(type, "ft_pointer") == 0) ours = i; if (std::strcmp(type, "ft_pointer") == 0) ours = i;
} }
} }
if (now - lastVisible > std::chrono::milliseconds(50) || visible.size() != handles.size()) { if (active && (now - lastVisible > std::chrono::milliseconds(50) || visible.size() != handles.size())) {
lastVisible = now; lastVisible = now;
visible.clear(); std::map<std::string, bool> seen;
for (const auto &[key, h] : handles) visible[key] = overlay->IsOverlayVisible(h); for (const auto &[key, h] : handles) seen[key] = overlay->IsOverlayVisible(h);
if (seen != visible) visible.swap(seen), ++visibleGen;
} }
if (active && anchored && hmd.bPoseIsValid && tilting) { if (active && anchored && hmd.bPoseIsValid && tilting) {
@@ -1973,8 +2103,8 @@ int main() {
tiltOrigin = lastOrigin; tiltOrigin = lastOrigin;
tiltBasis = AimBasis(lastAim); tiltBasis = AimBasis(lastAim);
tiltStart = false; // angles carry on from any earlier tilt in this drag tiltStart = false; // angles carry on from any earlier tilt in this drag
overlay->HideOverlay(cursor); cursorDot.Show(false);
overlay->HideOverlay(marker); markerDot.Show(false);
} }
const double yr = tiltYaw * M_PI / 180, pr = tiltPitch * M_PI / 180; const double yr = tiltYaw * M_PI / 180, pr = tiltPitch * M_PI / 180;
const Vec3 up{0, 1, 0}, side = tiltBasis.x; const Vec3 up{0, 1, 0}, side = tiltBasis.x;
@@ -1990,6 +2120,7 @@ int main() {
std::snprintf(msg, sizeof msg, "posq %.5f %.5f %.5f %.6f %.6f %.6f %.6f", originRaw.x, originRaw.y, std::snprintf(msg, sizeof msg, "posq %.5f %.5f %.5f %.6f %.6f %.6f %.6f", originRaw.x, originRaw.y,
originRaw.z, q[0], q[1], q[2], q[3]); originRaw.z, q[0], q[1], q[2], q[3]);
SendTo(out, "ft_pointer", msg); SendTo(out, "ft_pointer", msg);
posed.valid = false;
} else if (active && anchored && hmd.bPoseIsValid) { } else if (active && anchored && hmd.bPoseIsValid) {
const bool dragging = leftHeld || tnow < dropHoldUntil; const bool dragging = leftHeld || tnow < dropHoldUntil;
if (!dragging) tiltYaw = tiltPitch = 0; // drop finished: back to plain pointing if (!dragging) tiltYaw = tiltPitch = 0; // drop finished: back to plain pointing
@@ -2033,16 +2164,27 @@ int main() {
} }
return h; return h;
}; };
// Unchanged frames (see the top): the last pass still holds.
const bool reuse = !dragging && pass.valid && tnow - pass.at < std::chrono::milliseconds(100) &&
pass.gen == visibleGen && std::fabs(yaw - pass.yaw) < 1e-6 &&
std::fabs(pitch - pass.pitch) < 1e-6 && Length(anchor - pass.anchor) < 0.001 &&
Length(eye - pass.eye) < 0.005;
double best = pass.best, distance = pass.distance;
std::string bestKey = pass.key;
bool bestScene = pass.scene, onEdge = pass.onEdge, occluded = pass.occluded;
Vec3 point = pass.point;
if (!reuse) {
Hit first; Hit first;
if (!dragging) first = nearest(anchor, dir); if (!dragging) first = nearest(anchor, dir);
double best = first.along; best = first.along;
std::string bestKey = first.key; bestKey = first.key;
bool bestScene = first.scene; bestScene = first.scene;
Vec3 bestPoint = first.point, bestNormal = first.normal; Vec3 bestPoint = first.point, bestNormal = first.normal;
bool onEdge = false; onEdge = false;
if (!dragging && best < 1e8 && !bestScene) { if (!dragging && best < 1e8 && !bestScene) {
edgeKey = bestKey, edgePoint = bestPoint, edgeNormal = Normalize(bestNormal), edgeLast = bestPoint; edgeKey = bestKey, edgePoint = bestPoint, edgeNormal = Normalize(bestNormal), edgeLast = bestPoint;
} else if (!dragging && best >= 1e8 && !edgeKey.empty() && visible[edgeKey]) { } else if (!dragging && best >= 1e8 && !edgeKey.empty() && visible.count(edgeKey) &&
visible.at(edgeKey)) {
// Just off a panel: stay on its plane (see "Panel edges" at the top). // Just off a panel: stay on its plane (see "Panel edges" at the top).
const double denom = Dot(dir, edgeNormal); const double denom = Dot(dir, edgeNormal);
if (std::fabs(denom) > 1e-4) { if (std::fabs(denom) > 1e-4) {
@@ -2086,9 +2228,9 @@ int main() {
} }
// While dragging: keep the press-time distance and show the non-interactive marker. // While dragging: keep the press-time distance and show the non-interactive marker.
// On a scene-graph plane or a panel's edge: the laser-catching dot goes 5 cm behind it. // On a scene-graph plane or a panel's edge: the laser-catching dot goes 5 cm behind it.
double distance = dragging ? dragDistance : (best < 1e8 ? best : freeDistance); distance = dragging ? dragDistance : (best < 1e8 ? best : freeDistance);
Vec3 point = anchor + dir * distance; point = anchor + dir * distance;
bool occluded = false; occluded = false;
if (!dragging) { if (!dragging) {
// The cursor lands on what you see under it: the ray above starts at the anchor, // The cursor lands on what you see under it: the ray above starts at the anchor,
// not the eye, so after leaning it can pick a panel that something nearer // not the eye, so after leaning it can pick a panel that something nearer
@@ -2100,7 +2242,8 @@ int main() {
occluded = true; occluded = true;
bestKey = front.key, bestScene = front.scene; bestKey = front.key, bestScene = front.scene;
point = front.point; point = front.point;
if (!front.scene) edgeKey = front.key, edgePoint = front.point, edgeNormal = Normalize(front.normal), if (!front.scene)
edgeKey = front.key, edgePoint = front.point, edgeNormal = Normalize(front.normal),
edgeLast = front.point; edgeLast = front.point;
distance = std::sqrt(Dot(point - anchor, point - anchor)); distance = std::sqrt(Dot(point - anchor, point - anchor));
best = distance; best = distance;
@@ -2108,10 +2251,11 @@ int main() {
} }
lastDistance = distance, lastHit = bestKey; lastDistance = distance, lastHit = bestKey;
} }
}
const bool onScene = !dragging && ((bestScene && best < 1e8) || onEdge); const bool onScene = !dragging && ((bestScene && best < 1e8) || onEdge);
const bool onPanel = dragging || (best < 1e8 && !onScene); const bool onPanel = dragging || (best < 1e8 && !onScene);
const Vec3 sight = Normalize(point - eye); const Vec3 sight = Normalize(point - eye);
if (!dragging) { if (!dragging && !reuse) {
// SteamVR Settings (see the top): the dashboard's main panel is hidden and its // SteamVR Settings (see the top): the dashboard's main panel is hidden and its
// scene-graph panel shows the page. // scene-graph panel shows the page.
onVrSettings = false; onVrSettings = false;
@@ -2127,6 +2271,10 @@ int main() {
} }
} }
} }
if (!reuse) {
pass = {!dragging, tnow, visibleGen, yaw, pitch, anchor, eye,
best, distance, bestKey, bestScene, onEdge, occluded, point};
}
if (onVrSettings != catcherHidesHit) { if (onVrSettings != catcherHidesHit) {
overlay->SetOverlayFlag(cursor, vr::VROverlayFlags_HideLaserIntersection, onVrSettings); overlay->SetOverlayFlag(cursor, vr::VROverlayFlags_HideLaserIntersection, onVrSettings);
catcherHidesHit = onVrSettings; catcherHidesHit = onVrSettings;
@@ -2143,20 +2291,16 @@ int main() {
alpha = std::clamp(1 - std::min(secs(lastMove) - gazeShow, secs(lastHeld)) / 0.25, 0.0, 1.0); alpha = std::clamp(1 - std::min(secs(lastMove) - gazeShow, secs(lastHeld)) / 0.25, 0.0, 1.0);
} }
if (tnow < calPanelUntil) alpha = 0; if (tnow < calPanelUntil) alpha = 0;
overlay->SetOverlayAlpha(marker, float(alpha)); markerDot.Alpha(float(alpha));
overlay->SetOverlayWidthInMeters(marker, float(2 * SETTINGS_DOT * std::tan(cursorDeg * M_PI / 360))); markerDot.Place(near, eye, float(2 * SETTINGS_DOT * std::tan(cursorDeg * M_PI / 360)));
auto mm = Billboard(near, eye); cursorDot.Alpha(0);
overlay->SetOverlayTransformAbsolute(marker, vr::TrackingUniverseStanding, &mm); cursorDot.Place(far, eye, float(2 * SETTINGS_CATCHER * std::tan(cursorDeg * M_PI / 360)));
overlay->SetOverlayAlpha(cursor, 0); markerDot.Show(true);
overlay->SetOverlayWidthInMeters(cursor, float(2 * SETTINGS_CATCHER * std::tan(cursorDeg * M_PI / 360))); cursorDot.Show(true);
auto mc = Billboard(far, eye);
overlay->SetOverlayTransformAbsolute(cursor, vr::TrackingUniverseStanding, &mc);
overlay->ShowOverlay(marker);
overlay->ShowOverlay(cursor);
} else { } else {
// On a panel: the non-interactive marker, pulled 5 mm toward the eye so it // On a panel: the non-interactive marker, pulled 5 mm toward the eye so it
// draws on top. In free space: the interactive dot the laser lands on. // draws on top. In free space: the interactive dot the laser lands on.
const vr::VROverlayHandle_t show = onPanel ? marker : cursor, hide = onPanel ? cursor : marker; DotOverlay &show = onPanel ? markerDot : cursorDot, &hide = onPanel ? cursorDot : markerDot;
const Vec3 at = onPanel ? point + Normalize(eye - point) * 0.005 : onScene ? point + dir * 0.05 : point; const Vec3 at = onPanel ? point + Normalize(eye - point) * 0.005 : onScene ? point + dir * 0.05 : point;
const double dist = std::sqrt(Dot(at - eye, at - eye)); const double dist = std::sqrt(Dot(at - eye, at - eye));
// Gaze mode: a pulse for each click, and with POINTER_GAZE_DOT=moving, shown only // Gaze mode: a pulse for each click, and with POINTER_GAZE_DOT=moving, shown only
@@ -2173,12 +2317,10 @@ int main() {
} }
} }
if (tnow < calPanelUntil) alpha = 0; // the calibration panel is up (see the top) if (tnow < calPanelUntil) alpha = 0; // the calibration panel is up (see the top)
overlay->SetOverlayAlpha(show, float(alpha)); show.Alpha(float(alpha));
overlay->SetOverlayWidthInMeters(show, float(2 * dist * std::tan(scale * cursorDeg * M_PI / 360))); show.Place(at, eye, float(2 * dist * std::tan(scale * cursorDeg * M_PI / 360)));
auto m = Billboard(at, eye); show.Show(true);
overlay->SetOverlayTransformAbsolute(show, vr::TrackingUniverseStanding, &m); hide.Show(false);
overlay->ShowOverlay(show);
overlay->HideOverlay(hide);
} }
// Controller ray: from the eye, aimed at the cursor point, converted from the // Controller ray: from the eye, aimed at the cursor point, converted from the
@@ -2223,10 +2365,15 @@ int main() {
std::snprintf(msg, sizeof msg, "posq %.5f %.5f %.5f %.6f %.6f %.6f %.6f", oRaw.x, oRaw.y, oRaw.z, q[0], std::snprintf(msg, sizeof msg, "posq %.5f %.5f %.5f %.6f %.6f %.6f %.6f", oRaw.x, oRaw.y, oRaw.z, q[0],
q[1], q[2], q[3]); q[1], q[2], q[3]);
SendTo(out, "ft_pointer", msg); SendTo(out, "ft_pointer", msg);
} else { posed.valid = false;
} else if (!posed.valid || tnow - posed.at >= std::chrono::milliseconds(100) ||
Length(eyeRaw - posed.origin) >= 0.0002 || std::fabs(std::remainder(ayaw - posed.yaw, 360.0)) >= 0.04 ||
std::fabs(apitch - posed.pitch) >= 0.04) {
// The driver keeps the last pose: only a change goes out (see "Unchanged frames" at the top).
char msg[160]; char msg[160];
std::snprintf(msg, sizeof msg, "pose %.5f %.5f %.5f %.4f %.4f", eyeRaw.x, eyeRaw.y, eyeRaw.z, ayaw, apitch); std::snprintf(msg, sizeof msg, "pose %.5f %.5f %.5f %.4f %.4f", eyeRaw.x, eyeRaw.y, eyeRaw.z, ayaw, apitch);
SendTo(out, "ft_pointer", msg); SendTo(out, "ft_pointer", msg);
posed = {true, tnow, eyeRaw, ayaw, apitch};
} }
} }
@@ -2275,6 +2422,12 @@ int main() {
vr::VREvent_t ev; vr::VREvent_t ev;
while (sys->PollNextEvent(&ev, sizeof ev)) { while (sys->PollNextEvent(&ev, sizeof ev)) {
// The dashboard's overlays come and go with it, and a game brings its own.
if (ev.eventType == vr::VREvent_DashboardActivated || ev.eventType == vr::VREvent_DashboardDeactivated ||
ev.eventType == vr::VREvent_DashboardOverlayCreated || ev.eventType == vr::VREvent_SceneApplicationChanged)
overlays.Kick();
if (ev.eventType == vr::VREvent_TrackedDeviceActivated || ev.eventType == vr::VREvent_TrackedDeviceDeactivated)
ours = vr::k_unTrackedDeviceIndexInvalid;
if (ev.eventType == vr::VREvent_Quit) { if (ev.eventType == vr::VREvent_Quit) {
sys->AcknowledgeQuit_Exiting(); sys->AcknowledgeQuit_Exiting();
@@ -2283,6 +2436,15 @@ int main() {
return 0; return 0;
} }
} }
// Idle (see the top): wait for a command instead. Not with anything still to finish (the
// claim pulse, a click's release, a press).
const bool idle = !active && !handsOn && !claimPending && !claimHeld && !clickRelease && !leftHeld &&
hold.src == Src::None;
if (idle) {
pollfd p{in, POLLIN, 0};
poll(&p, 1, controllerButtons.Watching(inGame) ? 20 : 250);
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(8)); std::this_thread::sleep_for(std::chrono::milliseconds(8));
} }
}
} }
+8
View File
@@ -111,6 +111,14 @@ class ControllerButtons {
} }
} }
// Whether Poll reads any button now (the main loop can't sleep long while it does).
bool Watching(bool inGame) const {
if (!ok_ || (inGame && !games_ && !capture_)) return false;
for (int i = 0; i < kCount; ++i)
if (bound_[i]) return true;
return false;
}
// {"manifest":true,"global":false,"bound":[...],"active":[...]}: active = bound and // {"manifest":true,"global":false,"bound":[...],"active":[...]}: active = bound and
// delivered (a controller that has the button is on, and SteamVR lets us have it). // delivered (a controller that has the button is on, and SteamVR lets us have it).
std::string Status() const { std::string Status() const {
+2 -10
View File
@@ -306,19 +306,14 @@ int main() {
std::printf("displays on: %s\n", why.c_str()); std::printf("displays on: %s\n", why.c_str());
}; };
// SteamVR is asked every 100 ms (events, the activity level, poses: IPC calls to
// vrserver). Input wakes the loop too, to wake the displays at once, but a moving mouse
// sends hundreds of events a second, so those wakes only drain the devices.
constexpr auto kTick = std::chrono::milliseconds(100);
auto lastVr = now - kTick;
std::vector<pollfd> fds; std::vector<pollfd> fds;
while (!stopSignal) { while (!stopSignal) {
// Sleep until input or 100 ms: input wakes the displays at once, poses every tick.
fds.clear(); fds.clear();
fds.push_back({ctl, POLLIN, 0}); fds.push_back({ctl, POLLIN, 0});
for (auto &[path, d] : inputs) for (auto &[path, d] : inputs)
if (d.fd >= 0) fds.push_back({d.fd, POLLIN, 0}); if (d.fd >= 0) fds.push_back({d.fd, POLLIN, 0});
const auto wait = std::chrono::ceil<std::chrono::milliseconds>(kTick - (Clock::now() - lastVr)); poll(fds.data(), fds.size(), 100);
poll(fds.data(), fds.size(), std::clamp<int>(wait.count(), 0, kTick.count()));
now = Clock::now(); now = Clock::now();
const bool grace = now < graceUntil; const bool grace = now < graceUntil;
std::string use; // why this tick counts as use std::string use; // why this tick counts as use
@@ -334,8 +329,6 @@ int main() {
lastConf = now; lastConf = now;
} }
if (now - lastVr >= kTick) {
lastVr = now;
vr::VREvent_t ev; vr::VREvent_t ev;
while (sys->PollNextEvent(&ev, sizeof ev)) { while (sys->PollNextEvent(&ev, sizeof ev)) {
if (ev.eventType == vr::VREvent_Quit) { if (ev.eventType == vr::VREvent_Quit) {
@@ -402,7 +395,6 @@ int main() {
use = "turned on elsewhere"; use = "turned on elsewhere";
std::printf("displays on: turned on elsewhere\n"); std::printf("displays on: turned on elsewhere\n");
} }
}
if (!use.empty()) { if (!use.empty()) {
lastUse = now; lastUse = now;
turnOn(use); turnOn(use);
+3 -15
View File
@@ -83,7 +83,6 @@ class Window(Adw.ApplicationWindow):
self.status = {} self.status = {}
self.revealed = False self.revealed = False
self.setting = False # the switch is being set from the status, not by the user self.setting = False # the switch is being set from the status, not by the user
self.asking = False # a status check is running
self.toasts = Adw.ToastOverlay() self.toasts = Adw.ToastOverlay()
view = Adw.ToolbarView() view = Adw.ToolbarView()
@@ -140,27 +139,16 @@ class Window(Adw.ApplicationWindow):
"/tmp/frametop-remote.log and /tmp/frametop-vnc.log.") "/tmp/frametop-remote.log and /tmp/frametop-vnc.log.")
page.add(about) page.add(about)
# Each check runs remote-ctl.sh (bash, curl, and python3 for the tailnet name), so
# it runs every 5 seconds, when the window comes to the front, and after a change.
self.refresh() self.refresh()
GLib.timeout_add_seconds(5, self.refresh) GLib.timeout_add_seconds(2, self.refresh)
self.connect("notify::is-active", lambda win, _param: win.is_active() and win.refresh())
# --- status --- # --- status ---
def refresh(self): def refresh(self):
if not self.asking:
self.asking = True
proc = Gio.Subprocess.new([CTL, "status"], Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_SILENCE) proc = Gio.Subprocess.new([CTL, "status"], Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_SILENCE)
proc.communicate_utf8_async(None, None, self.on_status) proc.communicate_utf8_async(None, None, self.on_status)
return True return True
def refresh_soon(self):
"""Again in a moment: remote access takes a second or two to start or stop."""
self.refresh()
GLib.timeout_add_seconds(2, lambda: self.refresh() and False)
def on_status(self, proc, result): def on_status(self, proc, result):
self.asking = False
try: try:
_, out, _ = proc.communicate_utf8_finish(result) _, out, _ = proc.communicate_utf8_finish(result)
except GLib.Error: except GLib.Error:
@@ -209,7 +197,7 @@ class Window(Adw.ApplicationWindow):
self.toast("Remote access turns on at the next desktop restart") self.toast("Remote access turns on at the next desktop restart")
else: else:
self.toast("Remote access on") self.toast("Remote access on")
self.refresh_soon() self.refresh()
def on_reveal(self, button): def on_reveal(self, button):
self.revealed = button.get_active() self.revealed = button.get_active()
@@ -233,7 +221,7 @@ class Window(Adw.ApplicationWindow):
if self.status.get("running") == "1": if self.status.get("running") == "1":
subprocess.run([CTL, "restart"], stdin=subprocess.DEVNULL, capture_output=True) subprocess.run([CTL, "restart"], stdin=subprocess.DEVNULL, capture_output=True)
self.toast("New password set" + (": remote access restarted" if self.status.get("running") == "1" else "")) self.toast("New password set" + (": remote access restarted" if self.status.get("running") == "1" else ""))
self.refresh_soon() self.refresh()
def copy(self, text, done): def copy(self, text, done):
if text: if text:
+6 -14
View File
@@ -46,9 +46,8 @@ PACKAGES = {
"deckard-steamvr-rel": "the 3D mouse on the dashboard, on SteamVR Settings, and on a SteamVR " "deckard-steamvr-rel": "the 3D mouse on the dashboard, on SteamVR Settings, and on a SteamVR "
"window's grab bar; picking up a controller; mapped controller buttons; gaze", "window's grab bar; picking up a controller; mapped controller buttons; gaze",
"kwin": "clicks near the far edge of a screen whose scale isn't 1; every screen comes back " "kwin": "clicks near the far edge of a screen whose scale isn't 1; every screen comes back "
"after a desktop restart; floating a window; no blur behind the taskbar's menus", "after a desktop restart; floating a window",
"plasma-workspace": "the taskbar and panels after a desktop restart; no DiscoverNotifier or " "plasma-workspace": "the taskbar and panels after a desktop restart",
"ibus-daemon inside the desktop",
"gamescope": "the headset's volume buttons with nothing focused; typing goes where you last clicked", "gamescope": "the headset's volume buttons with nothing focused; typing goes where you last clicked",
"bluez": "a Bluetooth mouse reconnecting after it sleeps", "bluez": "a Bluetooth mouse reconnecting after it sleeps",
} }
@@ -177,21 +176,14 @@ def check_host():
try: try:
with open("/usr/bin/kwin_wayland", "rb") as f: with open("/usr/bin/kwin_wayland", "rb") as f:
kwin = f.read()
except OSError:
kwin = b""
# Qt keeps the option name as a UTF-16 string literal. # Qt keeps the option name as a UTF-16 string literal.
if "output-count".encode("utf-16-le") in kwin: output_count = "output-count".encode("utf-16-le") in f.read()
except OSError:
output_count = False
if output_count:
report("ok", "KWin", "has --output-count (one output per screen)") report("ok", "KWin", "has --output-count (one output per screen)")
else: else:
report("FAIL", "KWin", "no --output-count option: the desktop gets one screen at most") report("FAIL", "KWin", "no --output-count option: the desktop gets one screen at most")
# The session turns these built-in effects off by id (blurEnabled, contrastEnabled in kwinrc).
gone = [e for e in ("blur", "contrast") if b"KWin::%s_factory" % e.encode() not in kwin]
if not gone:
report("ok", "KWin effects", "blur and contrast, which the desktop turns off, keep their ids")
else:
report("warn", "KWin effects", f"no built-in {', '.join(gone)} effect: renamed? The desktop's "
"kwinrc may no longer turn it off (session/frametop-session.sh)")
if systemctl("cat", "steamvr.service"): if systemctl("cat", "steamvr.service"):
report("ok", "steamvr.service", "Frametop's services start and stop with it") report("ok", "steamvr.service", "Frametop's services start and stop with it")
-34
View File
@@ -234,40 +234,6 @@ elif [ "$(kreadconfig6 --file "$kwinrc" --group org.kde.kdecoration2 --key theme
kwriteconfig6 --file "$kwinrc" --group org.kde.kdecoration2 --key theme --delete kwriteconfig6 --file "$kwinrc" --group org.kde.kdecoration2 --key theme --delete
fi fi
# KWin draws through zink on the headset's GPU, which vrcompositor needs, so the blur and
# background contrast behind panels and menus and the window animations cost frames for
# little. They're off by default in this desktop. Each is written once, before KWin first
# reads it, and only if this desktop's config doesn't have it yet; the marker keeps it
# from coming back, since System Settings deletes a setting put back to KDE's default.
# docs/reference.md says how to turn them on again.
default() { # file group key value
[ -n "$(kreadconfig6 --file "$1" --group "$2" --key "$3")" ] || kwriteconfig6 --file "$1" --group "$2" --key "$3" "$4"
}
frametoprc=$XDG_CONFIG_HOME/frametoprc
if [ "$(kreadconfig6 --file "$frametoprc" --group Defaults --key effects)" != 1 ]; then
default "$kwinrc" Plugins blurEnabled false
default "$kwinrc" Plugins contrastEnabled false
default "$XDG_CONFIG_HOME/kdeglobals" KDE AnimationDurationFactor 0
kwriteconfig6 --file "$frametoprc" --group Defaults --key effects 1
fi
# System autostart entries this desktop doesn't need, hidden for it alone by a copy with
# Hidden=true in its own autostart folder, once and unless there's a file of that name
# already. Discover's update notifier starts Discover itself to check for updates (about
# 600 MB and a share of a core, with Flatpak's helper and AppStream behind it); updates
# come with SteamOS and from Discover in the stock desktop. IBus can't reach the
# desktop's apps: KWin's input method is ft-textinput, and the session drops the
# variables that point apps at IBus or XIM. Deleting the copy brings an entry back.
if [ "$(kreadconfig6 --file "$frametoprc" --group Defaults --key autostart)" != 1 ]; then
for entry in org.kde.discover.notifier ibus; do
src=/etc/xdg/autostart/$entry.desktop dst=$XDG_CONFIG_HOME/autostart/$entry.desktop
[ -r "$src" ] && [ ! -e "$dst" ] || continue
mkdir -p "$(dirname "$dst")"
sed '/^\[Desktop Entry\]$/a Hidden=true' "$src" > "$dst"
done
kwriteconfig6 --file "$frametoprc" --group Defaults --key autostart 1
fi
# Profiles reopen apps (docs/profiles.md), so Plasma's own session restore stays off here; # Profiles reopen apps (docs/profiles.md), so Plasma's own session restore stays off here;
# with both, apps would open twice. # with both, apps would open twice.
kwriteconfig6 --file "$XDG_CONFIG_HOME/ksmserverrc" --group General --key loginMode emptySession kwriteconfig6 --file "$XDG_CONFIG_HOME/ksmserverrc" --group General --key loginMode emptySession
+12 -102
View File
@@ -11,16 +11,6 @@
# screen placed lower or further right. Instead the VNC screen is the primary's size, and # screen placed lower or further right. Instead the VNC screen is the primary's size, and
# the workspace-sized RDP window inside it is shifted so the primary fills it. The pointer # the workspace-sized RDP window inside it is shifted so the primary fills it. The pointer
# maps 1:1. When the layout changes, the VNC screen resizes and the RDP client reconnects. # maps 1:1. When the layout changes, the VNC screen resizes and the RDP client reconnects.
#
# The RDP client runs only while someone watches. While connected, krdp captures and
# H.264-encodes every redraw in software, about 60% of a core, with FreeRDP and Xvnc adding
# about 30% more, even with no VNC viewer. krdp starts its capture per RDP connection and
# stops it when the connection closes, so it idles without one. So FreeRDP starts when a
# VNC client connects (the screen is black for the few seconds that takes) and stops
# VNC_IDLE_SEC (45) seconds after the last one leaves. Xvnc has no hook for clients, so ss
# counts them: whenever Xvnc writes to its log (it logs each connection), every second
# while FreeRDP runs, and every 5 seconds otherwise. The log only wakes this script up;
# what it says doesn't matter.
set -eu set -eu
here=$(dirname "$(readlink -f "$0")") here=$(dirname "$(readlink -f "$0")")
@@ -67,51 +57,16 @@ stop_rdp() { pkill -f "[x]freerdp /v:127.0.0.1:$rdp_port " 2>/dev/null || true;
trap 'stop_rdp; pkill -f "[X]vnc $display " 2>/dev/null || true' EXIT trap 'stop_rdp; pkill -f "[X]vnc $display " 2>/dev/null || true' EXIT
box bash -c 'vncpasswd -f < "$1/vnc-password" > "$1/vnc-passwd.bin" && chmod 600 "$1/vnc-passwd.bin"' - "$creds" box bash -c 'vncpasswd -f < "$1/vnc-password" > "$1/vnc-passwd.bin" && chmod 600 "$1/vnc-passwd.bin"' - "$creds"
exec {xlog}< <(box Xvnc "$display" -geometry "${w}x${h}" -depth 24 \ box Xvnc "$display" -geometry "${w}x${h}" -depth 24 \
-interface "$addr" -rfbport "$vnc_port" \ -interface "$addr" -rfbport "$vnc_port" \
-SecurityTypes VncAuth -PasswordFile "$creds/vnc-passwd.bin" \ -SecurityTypes VncAuth -PasswordFile "$creds/vnc-passwd.bin" \
-AlwaysShared -desktop "Steam Frame (Frametop)" 2>&1) -AlwaysShared -desktop "Steam Frame (Frametop)" &
xvnc=$! xvnc=$!
sleep 2
# Wait up to $1 seconds, less if Xvnc logs something; its lines go on to this log. # Keep an RDP connection open inside the VNC screen. Reconnect if it drops or the layout changes.
nap() {
local line rc=0
IFS= read -rt "$1" -u "$xlog" line || rc=$?
if [ $rc -eq 0 ]; then
printf '%s\n' "$line"
while IFS= read -rt 0.1 -u "$xlog" line; do printf '%s\n' "$line"; done
elif [ $rc -le 128 ]; then
sleep 1 # Xvnc's output closed: it's exiting
fi
return 0
}
nap 2
# A connected VNC client: an established TCP connection to Xvnc's port. Any connection
# counts, authenticated or not; it's on the tailnet only.
clients() { [ -n "$(ss -Htn state established "( sport = :$vnc_port )" 2>/dev/null)" ]; }
# ft-screens drops screens you aren't looking at to a low frame rate, and krdp would
# stream that. "watch SECONDS" asks it for full rate on every screen for that long: sent
# when a client connects and renewed every few seconds while one stays, so it lapses by
# itself if this script dies. An older ft-screens just answers that it doesn't know it.
watch() {
if command -v socat >/dev/null; then
printf 'watch 15' | socat -u - ABSTRACT-SENDTO:ft_screens 2>/dev/null
else
python3 -c 'import socket; socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM).sendto(b"watch 15", "\0ft_screens")' 2>/dev/null
fi || true
}
# What remote-view depends on: KWin's saved outputs (positions, scales, primary) and
# Frametop's layout (screen sizes). It's read again only after one of these changes, and
# once a minute in case a change didn't touch them, and only while FreeRDP runs.
layout_files=("$HOME/.config/frametop/kwinoutputconfig.json" "$HOME/.config/frametop-layout.json")
stamp() { stat -c %y "${layout_files[@]}" 2>/dev/null || true; }
# Start FreeRDP inside the VNC screen, sized and shifted for $v.
# /cert:ignore is fine here: the connection never leaves this host. # /cert:ignore is fine here: the connection never leaves this host.
start_rdp() { while kill -0 $xvnc 2>/dev/null; do
read -r x y w h ww wh <<< "$v" read -r x y w h ww wh <<< "$v"
box env DISPLAY=$display bash -c ' box env DISPLAY=$display bash -c '
size=$1 x=$2 y=$3 ww=$4 wh=$5 creds=$6 rdp_port=$7 size=$1 x=$2 y=$3 ww=$4 wh=$5 creds=$6 rdp_port=$7
@@ -125,7 +80,6 @@ start_rdp() {
rdp=$! rdp=$!
# FreeRDP takes no negative position, so move its window once it is up. # FreeRDP takes no negative position, so move its window once it is up.
for _ in $(seq 60); do for _ in $(seq 60); do
kill -0 $rdp 2>/dev/null || break
win=$(xdotool search --class xfreerdp 2>/dev/null | tail -1) win=$(xdotool search --class xfreerdp 2>/dev/null | tail -1)
[ -n "$win" ] && break [ -n "$win" ] && break
sleep 0.5 sleep 0.5
@@ -134,58 +88,14 @@ start_rdp() {
wait $rdp wait $rdp
' vnc-rdp "${w}x$h" "$x" "$y" "$ww" "$wh" "$creds" "$rdp_port" || true & ' vnc-rdp "${w}x$h" "$x" "$y" "$ww" "$wh" "$creds" "$rdp_port" || true &
rdp=$! rdp=$!
} while kill -0 $rdp 2>/dev/null; do
sleep 5
idle_sec=${VNC_IDLE_SEC:-45} now=$(view) || continue
rdp= # FreeRDP's job while it runs [ -n "$now" ] && [ "$now" != "$v" ] || continue
seen=0 # when a client was last seen ($SECONDS)
watched=-99 # when "watch" was last sent
stamp_v= # stamp() when $v was read
recheck=0 # read the layout every 2 seconds until then
next_view=0 # next time to read it
while kill -0 $xvnc 2>/dev/null; do
if clients; then
if [ $((SECONDS - watched)) -ge 5 ]; then watch; watched=$SECONDS; fi
seen=$SECONDS
if [ -z "$rdp" ]; then
echo "VNC client connected, starting the RDP client"
now=$(view) && [ -n "$now" ] && v=$now
stamp_v=$(stamp) recheck=0 next_view=$((SECONDS + 60))
start_rdp
fi
elif [ "$seen" -ne 0 ]; then
watched=-99
if [ $((SECONDS - seen)) -ge "$idle_sec" ]; then
seen=0
if [ -n "$rdp" ]; then
echo "no VNC client for ${idle_sec}s, stopping the RDP client"
stop_rdp
wait "$rdp" 2>/dev/null || true
rdp=
fi
fi
fi
if [ -n "$rdp" ] && ! kill -0 "$rdp" 2>/dev/null; then
# It dropped, or the layout changed: reconnect next round if a client is still there.
wait "$rdp" 2>/dev/null || true
rdp=
nap 2
continue
fi
if [ -n "$rdp" ]; then
# A layout change shows up in KWin's outputs a few seconds after the files change.
s=$(stamp)
[ "$s" = "$stamp_v" ] || { stamp_v=$s; recheck=$((SECONDS + 10)) next_view=$SECONDS; }
if [ "$SECONDS" -ge "$next_view" ]; then
next_view=$((SECONDS + 60))
[ "$SECONDS" -ge "$recheck" ] || next_view=$((SECONDS + 2))
now=$(view) || now=
if [ -n "$now" ] && [ "$now" != "$v" ]; then
echo "layout changed: $v -> $now" echo "layout changed: $v -> $now"
v=$now v=$now
stop_rdp stop_rdp
fi done
fi wait $rdp 2>/dev/null || true
fi sleep 2
if [ -n "$rdp" ] || [ "$seen" -ne 0 ]; then nap 1; else nap 5; fi
done done