Skip to content

React to device changes, and show fully charged and asleep states - #569

Open
casparas123 wants to merge 2 commits into
HeyOkay:pre-releasefrom
casparas123:feature/device-state-events
Open

casparas123 wants to merge 2 commits into
HeyOkay:pre-releasefrom
casparas123:feature/device-state-events

Conversation

@casparas123

Copy link
Copy Markdown
Collaborator

What

Three related changes around device state:

  • Device and power events (halo/device_events.py): a hidden top-level window registers for USB device, HID and Bluetooth radio interface notifications, plus WM_POWERBROADCAST. A receiver, a cable, a Bluetooth link or a resume from sleep wakes the poll loop at once instead of at the next interval. A dock's burst of interfaces is coalesced into one refresh (1.2 s after the last message, 3 s after resume). The existing HID signature check stays as the fallback, and the watcher is stopped on exit.
  • DeviceStatus.full: a charging reading at 100 % is marked full, which stops the ring's breathing animation and makes the tooltip say "fully charged" (new state.fully_charged key in all 11 catalogs). A provider with a real "charged" flag can set the same state below 100 %.
  • DeviceStatus.asleep: a reading kept for a device the receiver can no longer hear is asleep, not charging. Pulsar and Rapoo kept such readings with the last charging flag; they now drop the flag, matching the Device Support Request: Rapoo VT9pro #109 lesson that a receiver cannot confirm a charge it cannot hear.

Why

Halo notices a plug or unplug by comparing a USB signature every 2.5 s. That costs wake-ups and still misses changes that do not alter the HID tree (a Bluetooth link coming up, the machine resuming). Separately, a charging device at 100 % kept its breathing animation, and a mouse that slept after being on its cable kept a green charging arc.

How tested

  • Full suite: 4,050 tests, OK (skipped=3).
  • New tests:
    • message mapping and debounce coalescing for the watcher (7 tests);
    • full derivation, the animation gate, the tooltip text in every language and the asleep retained reading (9 tests);
    • Pulsar and Rapoo retained readings are asleep and no longer charging (updated assertions).
  • The watcher itself was started on Windows 11: the window and all three notifications register, WM_DEVICECHANGE and WM_POWERBROADCAST posted to the window both reach the callback, and stop() ends the thread cleanly. A physical plug/unplug test is still pending.

Risks

  • A hidden top-level window is created and never shown. If the notifications cannot be registered, start() returns False and the existing signature check remains the only source — no behaviour change.
  • full and asleep are appended to DeviceStatus (after the existing fields) so every positional construction keeps its meaning; no provider changes its signature.
  • Retained Pulsar/Rapoo readings no longer report charging while asleep. Intentional: the last reading may have come from the cable, but the device is no longer answering.
  • Not in this PR: a standard-HID battery fallback (page 0x85). That needs its own provider and a claim order so it cannot double-report devices the vendor drivers already read; it belongs in a separate PR.

Checklist

  • Targets pre-release (not main)
  • Tests added, including edge cases; the full suite passes
  • CHANGELOG.md line under [Unreleased]
  • New device: n/a
  • I read my own full diff and removed unrelated changes and debug code

A hidden window receives WM_DEVICECHANGE for USB, HID and Bluetooth radio
interfaces and WM_POWERBROADCAST for resume, so a receiver, a cable, a
Bluetooth link or a wake-up is polled at once instead of at the next
interval. A dock's burst of interfaces is coalesced into one refresh (1.2 s,
3 s after resume); the existing signature check stays as the fallback.

DeviceStatus gains full and asleep. A charging reading at 100 % is marked
full, which stops the breathing animation and makes the tooltip say "fully
charged" in every language; a provider with a real "charged" flag can say so
below 100 %. A reading kept for a device the receiver can no longer hear is
marked asleep and never keeps a charging flag, so a mouse that slept on the
cable does not keep looking like it is charging.
- Listen for the real Bluetooth radio interface
  (GUID_BTHPORT_DEVICE_INTERFACE, {0850302A-B344-4FDA-9BE9-90576B8D46F0}).
- Give the event window its own user32/kernel32 prototypes, so 64-bit
  handles are passed whole and the shared ctypes.windll is left alone.
  Release the window class when the loop ends or no window can be made,
  and post WM_QUIT through the same prototypes.
- A kept reading for a sleeping mouse is never charging or fully charged,
  and only a live reading at 100 % becomes fully charged. ATK, Attack
  Shark R11, Dareu, Finalmouse, HyperX and Lamzu now mark their kept
  reading asleep, like Pulsar and Rapoo.
- status.json shows the same charging and full states as the tooltip and
  gains `full` and `asleep`.
- Docs and changelog describe what changed. Tests cover each fix and the
  fully charged text in every language.
@casparas123
casparas123 force-pushed the feature/device-state-events branch from b211f4d to d46a20a Compare November 27, 2026 15:58
@casparas123

Copy link
Copy Markdown
Collaborator Author

I went through this PR again and pushed one more commit (d46a20ac) on top of a rebase onto the current pre-release. What changed:

  • Bluetooth: the app now listens for the right Windows ID for the Bluetooth radio ({0850302A-B344-4FDA-9BE9-90576B8D46F0}, checked against the registry). The old one never matched.
  • Sleeping mouse: the last level kept for a mouse that fell asleep is never shown as charging or fully charged. Only a live reading at 100 % counts as fully charged. A headset switched off on its charger still shows charging, because it really is.
  • More mice: ATK, Attack Shark R11, Dareu, Finalmouse, HyperX and Lamzu now mark that kept reading as asleep, the same way Pulsar and Rapoo already did.
  • status.json: shows the same charging and fully charged states as the tooltip, and gains full and asleep.
  • Windows calls: the hidden event window uses its own copy of the Windows calls, so 64-bit handles are passed correctly and the rest of the app is not affected. Its window class is cleaned up when it stops.
  • Docs: the changelog, docs/features.md and both READMEs describe the fully charged ring and the new status.json fields.

Tests: each fix has a test that fails on the old code, plus one that checks the "fully charged" text in all 11 languages. The full suite passes locally (4306 tests).

Still to check on real hardware: the ring at 100 % while charging, plugging a receiver or cable in and out, waking the PC from sleep, and turning Bluetooth off and on. I'm testing these locally now and will report back here.

@casparas123

Copy link
Copy Markdown
Collaborator Author

Tested on Windows with a local build of this branch (d46a20ac) and an Audeze Maxwell:

  • Charging: the ring breathes green while charging, turns steady green at 100 %, the tooltip says fully charged, and one notification appears.
  • Plugging the receiver or cable in and out: the tray updates within a couple of seconds, without waiting for the next check.
  • Turning Bluetooth off and back on: everything keeps working.

Not tested yet: waking from sleep, because sleep is turned off on this PC. Anyone who can try it: put the PC to sleep, wake it, and check that the levels refresh within a few seconds.

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