Skip to content

Spectrum wake sync (experimental, off by default): keep devices cycling in step after a wake - #70

Open
lukethehawk wants to merge 4 commits into
infinityInfinite:mainfrom
lukethehawk:spectrum-wake-sync
Open

lukethehawk wants to merge 4 commits into
infinityInfinite:mainfrom
lukethehawk:spectrum-wake-sync

Conversation

@lukethehawk

Copy link
Copy Markdown
Contributor

Problem

Each device runs Spectrum cycling on its own clock. A keyboard and mouse set to Spectrum (for example with Sync all) start in step. They drift apart as soon as one of them sleeps and wakes, because the device that wakes restarts its cycle alone. Today the only fix is to sync again by hand every time.

What this adds

This adds two things under Settings → Startup and games. It is opt-in and off by default.

  • Re-sync Spectrum when either device wakes (experimental). Raw Input is registered at start, so Secretarybird has to be restarted after turning it on.
  • Re-sync now: a button that restarts Spectrum on every device that is set to it.

Nothing changes for anyone who leaves the setting off, or for any effect other than Spectrum.

How it works

Seeing wakes without traffic

  • Wakes come from Windows Raw Input in the background, with RIDEV_INPUTSINK | RIDEV_DEVNOTIFY for mouse and keyboard (usages 01:02 and 01:06).
  • The tray reads only the event header and the device path (VID_1532&PID_xxxx).
  • It sends nothing to a device to find out whether it is awake, so sleep timers and battery life are unaffected.

What counts as awake

  • A wireless device's first input after a gap as long as its profile's sleep timer counts as a wake. The timer is taken 5 s short, and taken as 55 s when it isn't known.
  • A device whose last input is more recent than that must be awake.
  • A device that has been quiet for longer may be asleep, and nothing is written to it.

The sync itself

  • 1.5 s after a wake, the tray restarts Spectrum on the device that woke first, then on the other devices set to Spectrum that must be awake.
  • It writes the same SET as the effect itself (0F:02) and changes no profile.
  • With fewer than two devices awake, nothing is written. The sleeping device restarts its own lights when it wakes, and that wake syncs both. So it works in either order, however long apart the devices wake.
  • Nothing is written while the lights are down for Lights when away.

When a device isn't ready

  • Dropped from the list: a rescan can't open a sleeping keyboard, for example the start-up retries or the 60 s sleeper retries. If the device that woke is missing for that reason, one rescan brings it back before the write. This was the case that failed in practice.
  • Doesn't take the write: the tray stops there, so the other devices aren't restarted out of step with it. It tries again after 2 s and 4 s, rescanning first each time, then waits for the next wake. Steady input never causes repeated writes.
  • Profile sent by the tray: when the tray sends a device its profile by itself, that restarts the device's Spectrum alone, so it starts a sync too. This happens when the device turns up again in a rescan, or answers late after the lights came back (retry_lights_back). It doesn't happen after an apply to every device (start, resume, back from away), where they all restart together anyway.

Where it lives

Code

  • wake_sync.rs holds the logic, kept free of Win32 and tested without devices (20 tests):
    • both devices asleep, in each order, waking 10 s or 90 s apart;
    • the first device falling asleep again before the second wakes;
    • one device asleep alone (no change from today);
    • the keyboard dropped from the list while asleep;
    • sleep timers changed in the editor;
    • retries, steady input, wired devices, unknown devices, Raw Input paths.
  • engine.rs adds spectrum_pids, restart_spectrum and wake_peers.
  • win.rs handles Raw Input, the timer and the hooks after a rescan and after retry_lights_back.

Log

Every attempt is logged with the result and time of each write, without serial numbers:

Spectrum wake: 00EA woke (after 92 s quiet); 00EA quiet 0 s, 02C9 quiet 66 s, may be asleep
Spectrum wake: attempt 1: nothing written, waiting for 02C9 to wake
Spectrum wake: 02C9 woke (after 137 s quiet); 00EA quiet 0 s, 02C9 quiet 0 s
Spectrum wake: attempt 1 on 02C9, 00EA: 02C9 backlight ok (39 ms), 00EA underglow ok (18 ms)

Builds on the Lights when away retry PR

This branch includes the commit from #68 ("Lights when away: retry devices that miss the write"). The sync uses what that retry returns. The diff here will shrink to one commit once #68 is merged.

Tested

Setup

Results

Case Result
Both asleep: keyboard woke, mouse 18 s later in step
Both asleep: mouse woke, keyboard 71 s later (mouse in use) in step
Keyboard dropped from the list while asleep by the start-up rescan, then woke rescan, then in step (this failed before)
Keyboard asleep alone, mouse in use in step, as before
Lights back after the screen was off, mouse answering 8 s late relit, then in step
Win+L nothing written while locked, all back together
  • Every write took 18–39 ms and no retry was needed.
  • No WM_INPUT_DEVICE_CHANGE removals appeared while the devices slept, so the Raw Input handles stay stable.

CI

cargo clippy --release --all-targets -- -D warnings and cargo test --release pass on Windows and Linux (https://github.com/lukethehawk/secbird/actions/runs/40859512622).

Not tested

  • PC sleep and resume, and several days of use.
  • On the hardware: the mouse asleep alone while the keyboard is in use, and the first device falling asleep again before the second wakes. Both are covered by unit tests only.
  • A shared HyperSpeed multi-device receiver. Raw Input then reports the mouse's input under the receiver's PID, which this doesn't handle.

Known limits

  • The first input after the tray starts counts as a wake, which costs one extra attempt or log line.
  • Two devices of the same model (same PID) aren't told apart.
  • Profile changes from the editor or from a game profile switch don't start a sync. The Re-sync now button covers those.

🤖 Generated with Claude Code

lukethehawk and others added 2 commits October 29, 2026 11:40
…nfinityInfinite#48, infinityInfinite#52)

A device can miss the write that dims or turns off its lights, and then
stays lit the whole time away: a Basilisk V4 HyperSpeed missed it 3 times
out of 5 right as the screen went off, and took it a second later. Retry
those on the existing 3 s lights beat for 30 s, the way the lights coming
back are already retried for 2 minutes.

The log now names the devices that missed a write, and says when one
answered on a retry or was given up on. A device that left the list is no
longer counted as relit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r a wake

Each device runs Spectrum on its own clock, so a keyboard and mouse that
both cycle drift apart as soon as one sleeps and wakes. With the new
setting on, the tray restarts Spectrum on the awake devices together when
one of them wakes, whichever wakes first and however long apart.

Wakes are seen through Windows Raw Input (RIDEV_INPUTSINK, event headers
only): nothing is sent to a device to find out whether it is awake, so
sleep is unaffected. A wireless device's first input after a gap as long
as its sleep timer is a wake; one quiet for longer may be asleep and is
not written to, and its own wake syncs everything again. If a device that
woke had been dropped from the list while asleep, one rescan brings it
back first. If it doesn't take the write, the others aren't restarted out
of step with it: two more tries, then wait for the next wake. A profile
the tray sends a device by itself (back in a rescan, or relit after the
lights came back) syncs too.

The logic is in wake_sync.rs, free of Win32 and tested without devices:
both orders, wakes 10 s and 90 s apart, a device that sleeps again before
the other wakes, one that dropped from the list, steady input, retries.
Each attempt is logged with the result and time of every write.
Settings also gets a "Re-sync now" button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukethehawk added a commit to lukethehawk/secbird that referenced this pull request Oct 29, 2026
…Infinite#70 opened [skip ci]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0.5.1 already has the Lights when away retry (infinityInfinite#68) this branch was built on,
so the only conflicts were in engine.rs and win.rs, where this branch has that
same code plus the wake sync. Kept this branch's side there; the diff to main
is now the wake sync alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukethehawk added a commit to lukethehawk/secbird that referenced this pull request Oct 30, 2026
infinityInfinite#71; infinityInfinite#70 merged with main [skip ci]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts in engine.rs (imports) and win.rs (timers, App fields, WM_INPUT).
Both 0.6's Reactive/Ripple key presses and the wake sync use Raw Input on
the tray window, so:
- one WM_INPUT arm feeds both (the first arm would have hidden the other);
- Input::want takes `kept`: while the wake sync keeps mouse and keyboard
  registered (INPUTSINK | DEVNOTIFY), it only starts or stops reading, since
  registering again would drop DEVNOTIFY and RIDEV_REMOVE would end the
  wake sync's registration. With the wake sync off nothing changes;
- the wake sync's timer moves to 9; Studio keeps 8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lukethehawk added a commit to lukethehawk/secbird that referenced this pull request Nov 15, 2026
…d with Studio's key presses [skip ci]

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant