Skip to content

Tray: keep looking for a wireless device after its receiver comes back (dock reconnect) - #84

Closed
a3k7dotmd wants to merge 1 commit into
infinityInfinite:mainfrom
a3k7dotmd:dock-reconnect-keeps-looking
Closed

a3k7dotmd wants to merge 1 commit into
infinityInfinite:mainfrom
a3k7dotmd:dock-reconnect-keeps-looking

Conversation

@a3k7dotmd

Copy link
Copy Markdown
Contributor

Found a new failure mode - unplugging/replugging the laptop dock stops secbird control of the keyboard and its Editor reports "Connected, not controllable yet". Text below and fix is by Fable 5.2 reviewed by GPT-6 Astra. Deployment tested working by me.

Problem

A wireless keyboard whose receiver sits on a Thunderbolt dock stops being managed after the dock is unplugged and plugged back in. The receiver is back within seconds, but the keyboard behind it doesn't answer until its next key press. The tray looks once, 1.5 s after the arrival (schedule_rescan), finds the keyboard silent, and never looks again: the retries (start_retries) only run at tray start and after a wake. The editor then lists the keyboard under "Connected, not controllable yet" with "Asleep? Press a key or move it", and pressing a key changes nothing until Rescan is clicked.

Nothing is logged when this happens. On my machine there were 12 receiver removals in the last month (Windows event 1010 in Kernel-PnP "Device Management"). Two were followed by a found: line within seconds. After the other ten none came for hours, and in eight of them not before the tray was started again.

It is the keyboard that is silent, not the tray that is early: 7 s after the receiver came back, and before any key was pressed, secbird info said error: not found: no matching Razer device. After the key press it answered.

Fix

  • A device arrival starts the retries, the same chain as at tray start and after a wake (4 s, 15 s, 45 s, then once a minute for half an hour while a known wireless device is asleep).
  • Input from a device that is asleep starts them again. While a known wireless device is plugged in but not answering, the tray registers raw input for its kind: keyboards for a keyboard or keypad, mice for a mouse (RegisterRawInputDevices, RIDEV_INPUTSINK, on the tray's hidden window). On WM_INPUT it reads the header only (RID_HEADER), asks for the sending device's path and looks for VID_1532&PID_<product id of the sleeping device>. A match drops the registration and starts a rescan plus the retries. So a device that slept for hours is found at its first key press that reaches Windows, without polling.
    • The keys and movements themselves are never read. Input that comes from no device (injected by a program) is ignored.
    • Nothing is registered while every device answers.
    • While the quick retries that input started are running, the watch is off, so the keys typed meanwhile don't restart the timers. If the device still doesn't answer after them, the watch comes back, at most 5 times (INPUT_WAKES) until the device answers or is unplugged. A device that types but never answers isn't looked for on every key.
  • The retry counter is no longer a field of App. WM_DEVICECHANGE is a sent message, so it can come in while App is borrowed (inside a shell call). start_retries then set its timer but could not reset retry_step, and a chain that had run out before ended after one more retry. The counter is a static now (RETRY_STEP, with RETRY_CHAIN counting restarts so a retry that was in its rescan when the chain was restarted leaves the new timer alone). A TIMER_RETRY that finds App borrowed is set again after SETTLE_MS, as TIMER_RESCAN already was.
  • The editor follows the tray. /api/live and the state carry a counter (list) that grows when a rescan changes what the editor lists (keys, names, reasons). The page reloads the state when it changes, so a device the tray found by itself shows up without a click on Rescan. An open editor panel is left alone and the change is shown once it is closed; a request that fails is silent and tried again on the next tick.
  • The editor's server tells the tray when it rescanned by itself (/api/rescan, and the rescan-and-retry in post), so the tray keeps following a device the editor dropped.
  • The log names what happens:
    gone: DeathStalker V2 Pro (Wireless)
    not answering: DeathStalker V2 Pro (Wireless) (asleep?), looking again at its next key press or movement
    input from DeathStalker V2 Pro (Wireless): looking for it again
    found: DeathStalker V2 Pro (Wireless)
    
  • A unit test covers matching a raw input path to the sleeping device.

Not changed: the retry delays and the half-hour cap, and what the editor's own Rescan does (a device it finds still gets no profile and no found: line).

Tested

  • Setup: Windows 11 26H2, 13 Nov 2026. DeathStalker V2 Pro (Wireless, 0290), its receiver on a Thunderbolt dock, sleeping off, Synapse not installed.
  • Build: 0.5.1 with this change, cross-built on Linux for x86_64-pc-windows-gnu (rustc 1.100.0, mingw-w64). Only secbird-tray.exe was replaced.
  • Dock unplugged and plugged back in, then keys pressed, twice. The second time Windows logged the receiver's removal at 04:49:07 and its arrival at 04:49:22 (UTC); the tray's log:
    2026-11-14 04:49:08Z gone: DeathStalker V2 Pro (Wireless)
    2026-11-14 04:49:24Z not answering: DeathStalker V2 Pro (Wireless) (asleep?), looking again at its next key press or movement
    2026-11-14 04:49:32Z input from DeathStalker V2 Pro (Wireless): looking for it again
    2026-11-14 04:49:34Z found: DeathStalker V2 Pro (Wireless)
    
    The retry 4 s after the arrival didn't find the keyboard; the key press did. This keyboard's first key press after a reconnect only wakes its radio link and never reaches Windows, so input from is the second press.
  • Editor: left open during that test, it moved the keyboard out of "Connected, not controllable yet" by itself.
  • Idle cost: after found: the tray used 0.000 s of processor time over 30 s, and secbird info still shows sleeping off.
  • Checks: cargo clippy --release --target x86_64-pc-windows-gnu --all-targets -- -D warnings is clean. Unit tests: 104 (core) and 3 (tray) pass on Windows, 97 and 1 pass on Linux. The page's refresh logic passes 24 checks against a fake tray API in Node (a small script outside this change; I can add it if it is wanted).

A receiver that is plugged back in (its Thunderbolt dock was reconnected)
answers within seconds, but the keyboard or mouse behind it stays silent
until its next key press. A device arrival got one rescan 1.5 s later and
no retries, so the device stayed under "Connected, not controllable yet"
until Rescan was clicked in the editor.

- A Razer arrival now starts the same retries as tray start and wake
  (4 s, 15 s, 45 s, then once a minute for half an hour while a known
  wireless device is asleep).
- While such a device is asleep, the tray registers raw input for its kind
  (keyboards and keypads, or mice) and reads only which device sent it.
  Input from the sleeping device starts the retries again, however long it
  slept; at most 5 times until it answers. Nothing is registered otherwise.
- The retry counter is no longer part of `App`, so an arrival that comes in
  while `App` is borrowed still starts the retries over, and a retry that
  comes in at such a moment is tried again instead of dropped.
- The editor's server tells the tray when it rescanned by itself, and an
  open editor shows a device the tray found or lost without a click on
  Rescan (a list counter in /api/live and in the state). An open editor
  panel is left alone and the change shown once it is closed.
- Log: "gone: <names>", "not answering: <names> (asleep?), looking again
  at its next key press or movement", "input from <name>: looking for it
  again".

Tested on a DeathStalker V2 Pro (Wireless, 0290) whose receiver sits on a
Thunderbolt dock: found 2 s after the first key press that reached Windows.
@infinityInfinite

Copy link
Copy Markdown
Owner

This fixes a real gap, and the logs from your dock test are exactly what was needed. It can't merge as is, though. It's built on 0.5.1, and 0.6.0 changed win.rs and editor.html enough that it conflicts.

There's also one clash that matters. In 0.6.0, RGB Studio's Reactive and Ripple effects already register raw input for keyboards and mice on the tray window (crates/secbird-tray/src/input.rs). Windows keeps one registration per input type per program, so the two features would remove each other's:

  • When your watch stops, its RIDEV_REMOVE drops the Studio's keyboard and mouse registration. Input::want still thinks it's on, so Reactive and Ripple stop reacting without any error.
  • When the Studio stops, it drops your watch, and the device that's asleep is never found.
  • Both add a separate WM_INPUT arm, and only the first one runs.

Could you open a new PR against the latest main (0.6.0)? One owner for raw input would solve it: input.rs registers while either the Studio or the sleeper watch needs input, and the one WM_INPUT handler passes each input to both.

One smaller thing while you're in there: every Razer arrival now adds three extra rescans, and a rescan reopens the device handles, which can make a running Studio animation stutter. Starting the retries only when a known wireless device is still asleep after the first look would avoid that.

The rest looks good: the input cache and flood cut-off, the 5-wake limit, the retry counter moved out of App, and the editor following the list.

@a3k7dotmd

Copy link
Copy Markdown
Contributor Author

Replaced by #92: the same fix rebuilt on main (0.6.0), with one owner for raw input and the retries only while a device is asleep.

@a3k7dotmd a3k7dotmd closed this Nov 20, 2026
infinityInfinite pushed a commit that referenced this pull request Nov 21, 2026
#92)

A receiver that is plugged back in (its Thunderbolt dock was reconnected)
answers within seconds, but the keyboard or mouse behind it stays silent
until its next key press. A device arrival got one rescan 1.5 s later and
no retries, so the device stayed under "Connected, not controllable yet"
until Rescan was clicked in the editor.

This is #84 rebuilt on 0.6.0, with the two changes asked for there.

- Raw input has one owner, `input.rs`. It registers keyboards and mice
  while the Studio (Reactive, Ripple) or the watch for a device that is
  asleep needs them, one kind at a time, so one of the two stopping never
  drops what the other uses. One `WM_INPUT` handler gives each input to
  both: the press to the Studio, the sender to the watch.
- While a known wireless device is asleep, the watch asks for input of its
  kind (keyboards and keypads, or mice). Only which device sent it is read
  while the Studio doesn't listen. Input from the sleeping device starts a
  look for it, however long it slept; at most 5 times until it answers.
- A Razer arrival, or input from a sleeping device, is followed by one
  look. The retries (4 s, 15 s, 45 s, then once a minute for half an hour)
  start only if that look still finds a known wireless device asleep, and
  end as soon as none is. With nothing asleep an arrival costs the one
  rescan it cost before. The retries after tray start and wake are as they
  were.
- The retry counter is no longer part of `App`, so a wake that comes in
  while `App` is borrowed still starts the retries over, and a retry that
  comes in at such a moment is tried again instead of dropped.
- `Input` notes a registration as changed only when Windows accepted the
  call, so a refused removal is asked for again.
- The editor's server tells the tray when it rescanned by itself, and an
  open editor shows a device the tray found or lost without a click on
  Rescan (a list counter in /api/live and in the state). An open editor
  panel and the Settings page are left alone and the change is shown once
  they are closed.
- Log: "gone: <names>", "not answering: <names> (asleep?), looking again
  at its next key press or movement", "input from <name>: looking for it
  again".

Tested on a DeathStalker V2 Pro (Wireless, 0290) whose receiver sits on a
Thunderbolt dock: found 1 s after the first key press that reached Windows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants