From 021b94a0999f837e8f0a5ca158b068189d937743 Mon Sep 17 00:00:00 2001 From: a3k7dotmd <92009667+a3k7dotmd@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:13:28 -0800 Subject: [PATCH] Tray: keep looking for a wireless device after its receiver comes back 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: ", "not answering: (asleep?), looking again at its next key press or movement", "input from : 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. --- crates/secbird-tray/Cargo.toml | 1 + crates/secbird-tray/src/editor.html | 44 +++-- crates/secbird-tray/src/engine.rs | 53 ++++-- crates/secbird-tray/src/server.rs | 6 +- crates/secbird-tray/src/win.rs | 260 ++++++++++++++++++++++++++-- 5 files changed, 323 insertions(+), 41 deletions(-) diff --git a/crates/secbird-tray/Cargo.toml b/crates/secbird-tray/Cargo.toml index 6e0448f..68eca96 100644 --- a/crates/secbird-tray/Cargo.toml +++ b/crates/secbird-tray/Cargo.toml @@ -25,6 +25,7 @@ windows-sys = { version = "0.59", features = [ "Win32_System_SystemServices", "Win32_System_Threading", "Win32_UI_Accessibility", + "Win32_UI_Input", "Win32_UI_Shell", "Win32_UI_WindowsAndMessaging", ] } diff --git a/crates/secbird-tray/src/editor.html b/crates/secbird-tray/src/editor.html index b5a7e40..619fab7 100644 --- a/crates/secbird-tray/src/editor.html +++ b/crates/secbird-tray/src/editor.html @@ -2548,11 +2548,24 @@

// Follow DPI changes made with the mouse's own DPI button. The tray answers from memory // (no device access), and only reports newer than the last poll are applied, so a DPI // just set here is never undone. Paused while the page is hidden. -let liveSeq = null; +let liveSeq = null, listSeq = null, listLoading = false; +// The tray found or lost a device by itself (one that was asleep answered): show it. Quiet +// when the tray is busy, and an editor opened in the meantime is left alone; either way +// `listSeq` stays behind, so the next tick that finds no editor open tries again. A first +// load that failed is made up for the same way (`listSeq` is still null then). +async function followList() { + listLoading = true; + try { + const s = await api('/api/state'); + if (!sel) showState(s); + } catch { /* try again next tick */ } + listLoading = false; +} async function pollLive() { if (document.hidden) return; try { const r = await api('/api/live'); + if (r.list !== undefined && r.list !== listSeq && !sel && !listLoading) followList(); if (liveSeq !== null && r.seq !== liveSeq) { for (const d of devices) { const v = r.dpi[d.id]; @@ -2568,19 +2581,23 @@

} setInterval(pollLive, 1000); +// Show a device list the tray sent, and note which one it is (`list`, see followList). +function showState(s) { + devices = s.devices; unsupported = s.unsupported || []; synapse = s.synapse || []; + if (s.list !== undefined) listSeq = s.list; + devices.forEach(d => { d.profile = d.profile || {}; d.profilesData = d.profilesData || {}; d.profilesData[d.active] = d.profile; }); + if (s.version) $('home').title = 'Secretarybird ' + s.version + ': all devices'; + const want = new URLSearchParams(location.search).get('d'); + const wanted = d => d.pid === want || d.id === want; + dev = devices.find(d => dev && d.id === dev.id) || devices.find(wanted) || devices[0] || null; + if (showAll === null) showAll = !devices.some(wanted) && (devices.length > 1 || unsupported.length > 0); + if (!dev) sel = null; + render(); +} async function load(path = '/api/state', body) { - try { - const s = body ? await api(path, body) : await api(path); - devices = s.devices; unsupported = s.unsupported || []; synapse = s.synapse || []; - devices.forEach(d => { d.profile = d.profile || {}; d.profilesData = d.profilesData || {}; d.profilesData[d.active] = d.profile; }); - if (s.version) $('home').title = 'Secretarybird ' + s.version + ': all devices'; - const want = new URLSearchParams(location.search).get('d'); - const wanted = d => d.pid === want || d.id === want; - dev = devices.find(d => dev && d.id === dev.id) || devices.find(wanted) || devices[0] || null; - if (showAll === null) showAll = !devices.some(wanted) && (devices.length > 1 || unsupported.length > 0); - if (!dev) sel = null; - render(); - } catch (e) { toast(e.message, true); } + listLoading = true; + try { showState(body ? await api(path, body) : await api(path)); } catch (e) { toast(e.message, true); } + listLoading = false; } // Sync: one color (the top bar), or a zone's color or effect (its panel), on every zone of // every device; effects start together. Dragging the swatch syncs live. @@ -2589,6 +2606,7 @@

syncing = what; const r = await api('/api/sync', { color: what.color || '', effect: what.effect || '' }); devices = r.state.devices; unsupported = r.state.unsupported || []; synapse = r.state.synapse || []; + if (r.state.list !== undefined) listSeq = r.state.list; devices.forEach(d => { d.profile = d.profile || {}; d.profilesData = d.profilesData || {}; d.profilesData[d.active] = d.profile; }); dev = devices.find(d => d.id === dev.id) || devices[0]; render(); diff --git a/crates/secbird-tray/src/engine.rs b/crates/secbird-tray/src/engine.rs index 90adc96..f182655 100644 --- a/crates/secbird-tray/src/engine.rs +++ b/crates/secbird-tray/src/engine.rs @@ -26,6 +26,8 @@ pub struct Engine { relight: HashMap, /// Devices that didn't answer [`Engine::lights_away`], and until when to keep trying. dim: HashMap, + /// Grows each time a rescan finds other devices than the one before it. + list_seq: u64, } const FINDER_PAUSE: Duration = Duration::from_secs(10 * 60); @@ -52,6 +54,8 @@ pub struct Unsupported { pub pid: u16, pub name: String, pub reason: &'static str, + /// What the database says it is: the kind of input that shows it woke up. + pub kind: Option, } /// What a retry of [`Engine::lights_away`] or [`Engine::lights_back`] did, by device key. @@ -92,6 +96,7 @@ impl Engine { away: None, relight: HashMap::new(), dim: HashMap::new(), + list_seq: 0, }) } @@ -376,6 +381,7 @@ impl Engine { /// Re-enumerate devices. Returns the keys of devices that were not present before. pub fn rescan(&mut self) -> Vec { let before: HashSet = self.devices.iter().map(|d| d.key()).collect(); + let listed_before = self.listed(); self.devices.clear(); // close stale handles before reopening let _ = self.api.refresh_devices(); let db = Db::load(Some(&config_dir().join("devices.json"))) @@ -384,25 +390,48 @@ impl Engine { self.devices = discover(&self.api, &db); self.unsupported = secbird_core::device::unanswered(&self.api, &self.devices) .into_iter() - .map(|pid| match db.get(pid) { - Some(spec) if spec.lighting == secbird_core::Lighting::Kraken => { - Unsupported { pid, name: spec.name.clone(), reason: "Headset protocol not supported yet" } - } - Some(spec) if spec.wireless => Unsupported { pid, name: spec.name.clone(), reason: ASLEEP }, - Some(spec) => Unsupported { pid, name: spec.name.clone(), reason: "Not answering. Is Synapse running?" }, - None => match db.bluetooth_name(pid) { - Some(name) => Unsupported { pid, name, reason: BLUETOOTH }, - None => Unsupported { pid, name: format!("Razer device {pid:04X}"), reason: "Unknown model. Run secbird report" }, - }, + .map(|pid| { + let spec = db.get(pid); + let (name, reason) = match spec { + Some(spec) if spec.lighting == secbird_core::Lighting::Kraken => (spec.name.clone(), "Headset protocol not supported yet"), + Some(spec) if spec.wireless => (spec.name.clone(), ASLEEP), + Some(spec) => (spec.name.clone(), "Not answering. Is Synapse running?"), + None => match db.bluetooth_name(pid) { + Some(name) => (name, BLUETOOTH), + None => (format!("Razer device {pid:04X}"), "Unknown model. Run secbird report"), + }, + }; + Unsupported { pid, name, reason, kind: spec.map(|s| s.kind) } }) .collect(); self.synapse = secbird_core::debloat::synapse_running(); + if self.listed() != listed_before { + self.list_seq += 1; + } self.devices.iter().map(|d| d.key()).filter(|k| !before.contains(k)).collect() } - /// A known wireless device is plugged in (its receiver is) but not answering yet. + /// Everything the editor lists, by name: the devices that answer, then the ones that + /// don't. A receiver keeps its key when it learns which mouse it carries, but not its name. + fn listed(&self) -> Vec { + let answering = self.devices.iter().map(|d| format!("+{} {}", d.key(), d.name())); + let silent = self.unsupported.iter().map(|u| format!("-{:04X} {} {}", u.pid, u.name, u.reason)); + answering.chain(silent).collect() + } + + /// A counter that grows when a rescan changes that list, so an open editor shows a + /// device the tray found by itself (one that was asleep) without a click on Rescan. + pub fn list_seq(&self) -> u64 { + self.list_seq + } + + /// Known wireless devices that are plugged in (their receiver is) but not answering yet. + pub fn sleeping(&self) -> impl Iterator { + self.unsupported.iter().filter(|u| u.reason == ASLEEP) + } + pub fn has_sleeping(&self) -> bool { - self.unsupported.iter().any(|u| u.reason == ASLEEP) + self.sleeping().next().is_some() } /// Read profiles fresh from disk, so changes made with the CLI are picked up. diff --git a/crates/secbird-tray/src/server.rs b/crates/secbird-tray/src/server.rs index 42c9704..8ebbdb7 100644 --- a/crates/secbird-tray/src/server.rs +++ b/crates/secbird-tray/src/server.rs @@ -219,10 +219,12 @@ impl Server { ("GET", "/api/apps") => Ok(json!({ "apps": crate::apps::running() })), ("GET", "/api/live") => { let (seq, dpi) = engine.live_dpi(); - Ok(json!({ "seq": seq, "dpi": dpi.iter().map(|(k, (x, y))| (k.clone(), json!([x, y]))).collect::>() })) + let dpi = dpi.iter().map(|(k, (x, y))| (k.clone(), json!([x, y]))).collect::>(); + Ok(json!({ "seq": seq, "dpi": dpi, "list": engine.list_seq() })) } ("POST", "/api/rescan") => { engine.rescan(); + crate::win::rescanned(); Ok(state(&engine)) } ("GET", "/api/settings") => Ok(settings_json(&engine)), @@ -534,6 +536,7 @@ fn post(engine: &mut Engine, path: &str, body: &[u8]) -> Result { match attempt(engine) { Err(Error::Hid(_)) | Err(Error::NotFound(_)) | Err(Error::NoReply(_)) => { engine.rescan(); + crate::win::rescanned(); attempt(engine) } other => other, @@ -669,6 +672,7 @@ fn state(engine: &Engine) -> Value { "pid": format!("{:04X}", u.pid), "name": u.name, "reason": u.reason, })).collect::>(), "synapse": engine.synapse(), + "list": engine.list_seq(), }) } diff --git a/crates/secbird-tray/src/win.rs b/crates/secbird-tray/src/win.rs index 022d68f..ba3d7dc 100644 --- a/crates/secbird-tray/src/win.rs +++ b/crates/secbird-tray/src/win.rs @@ -1,5 +1,6 @@ use crate::engine::{ApplyReport, Battery, Engine, LightsRetry, DIM_FOR, RELIGHT_FOR}; -use secbird_core::log; +use secbird_core::db::RAZER_VID; +use secbird_core::{log, Kind}; use std::collections::{HashMap, HashSet}; use crate::settings::{autostart_enabled, open_config_folder, set_autostart, TraySettings}; use crate::{apps, osd, server}; @@ -10,7 +11,7 @@ use std::cell::RefCell; use std::ffi::c_void; use std::mem::{size_of, zeroed}; use std::ptr::{null, null_mut}; -use std::sync::atomic::{AtomicPtr, AtomicU32, Ordering}; +use std::sync::atomic::{AtomicPtr, AtomicU32, AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, MutexGuard}; use std::time::Duration; use windows_sys::core::GUID; @@ -21,6 +22,10 @@ use windows_sys::Win32::System::RemoteDesktop::{WTSRegisterSessionNotification, use windows_sys::Win32::System::SystemServices::GUID_CONSOLE_DISPLAY_STATE; use windows_sys::Win32::System::Threading::CreateMutexW; use windows_sys::Win32::UI::Accessibility::{SetWinEventHook, HWINEVENTHOOK}; +use windows_sys::Win32::UI::Input::{ + GetRawInputData, GetRawInputDeviceInfoW, RegisterRawInputDevices, HRAWINPUT, RAWINPUTDEVICE, RAWINPUTHEADER, + RIDEV_INPUTSINK, RIDEV_REMOVE, RIDI_DEVICENAME, RID_HEADER, +}; use windows_sys::Win32::UI::Shell::*; use windows_sys::Win32::UI::WindowsAndMessaging::*; @@ -40,6 +45,8 @@ const WM_UPDATE: u32 = WM_APP + 6; const WM_UPDATE_INSTALL: u32 = WM_APP + 7; /// Sent by the editor's "Update now": the release is waiting in `REQUESTED_UPDATE`. const WM_START_UPDATE: u32 = WM_APP + 8; +/// Sent by the editor's server after it rescanned by itself: who is asleep may have changed. +const WM_RESCANNED: u32 = WM_APP + 9; const CLASS_NAME: &str = "SecretarybirdTray"; const TIMER_RESCAN: usize = 1; /// Extra rescans after start and wake: devices often answer late at boot. @@ -49,6 +56,16 @@ const RETRY_DELAYS_MS: [u32; 3] = [4_000, 15_000, 45_000]; /// once a minute for half an hour, so it still gets its profile (#37). const SLEEPER_RETRY_MS: u32 = 60_000; const SLEEPER_RETRIES: usize = 30; +/// A device that is asleep says it woke by sending input: a key press, a movement. Windows +/// tells us who sent it (raw input), and that starts the retries again, however long it +/// slept. Registered only while a device is asleep, and only for its kind of input. +const USAGE_PAGE_DESKTOP: u16 = 0x01; +const USAGE_MOUSE: u16 = 0x02; +const USAGE_KEYBOARD: u16 = 0x06; +/// How many times input may start the retries for a device that still doesn't answer, so +/// one that types but never answers isn't looked for on every key. Starts over once +/// nothing is asleep, and after a wake. +const INPUT_WAKES: u8 = 5; /// Battery check for wireless devices (cheap: two short USB requests per device). const TIMER_BATTERY: usize = 3; const BATTERY_MS: u32 = 5 * 60 * 1000; @@ -75,6 +92,8 @@ const BUTTONS_AGAIN_SECS: u64 = 60; /// set to stay lit get a read on the same beat (#34). const TIMER_LIGHTS: usize = 7; const LIGHTS_MS: u32 = 3_000; +/// [`App::follow_sleepers`] again, when the editor's server asked for it at a busy moment. +const TIMER_SLEEPERS: usize = 8; const ID_REAPPLY: u32 = 1; const ID_FOLDER: u32 = 2; @@ -95,6 +114,11 @@ const GUID_DEVINTERFACE_HID: GUID = GUID::from_u128(0x4d1e55b2_f16f_11cf_88cb_00 static TASKBAR_CREATED: AtomicU32 = AtomicU32::new(0); /// The tray window, for the foreground hook (which gets no user data). static TRAY_HWND: AtomicPtr = AtomicPtr::new(null_mut()); +/// Index into RETRY_DELAYS_MS of the next retry, and how many times the retries were started +/// over. Not in `App`: a device arrival is a sent message, which can come in while `App` is +/// borrowed (inside a shell call), and it must still start them over. +static RETRY_STEP: AtomicUsize = AtomicUsize::new(0); +static RETRY_CHAIN: AtomicU32 = AtomicU32::new(0); struct App { hwnd: HWND, @@ -107,8 +131,6 @@ struct App { profile_items: Vec<(String, String)>, /// Apply to every device on the next rescan, not just newly arrived ones. apply_all_pending: bool, - /// Index into RETRY_DELAYS_MS of the next retry. - retry_step: usize, /// Warned about Synapse since it last started. synapse_warned: bool, /// Devices already warned about low battery (cleared once charging or recovered). @@ -136,6 +158,24 @@ struct App { /// The session is locked, or the screen is off: the lights go down if set (#48). locked: bool, screen_off: bool, + /// Known wireless devices that are plugged in but not answering. + asleep: Vec, + /// Raw input registered right now: from keyboards, from mice. + input_watch: (bool, bool), + /// Input from a device that is asleep started the retries; no watching until the quick + /// ones are over, or every key typed meanwhile would start them again. + input_woke: bool, + /// How many more times input may start them (see [`INPUT_WAKES`]). + input_wakes_left: u8, + /// The last raw input device that wasn't one of them, to pass over its next reports. + input_other: HANDLE, +} + +/// A device in [`App::asleep`]. +struct Sleeper { + pid: u16, + name: String, + kind: Option, } /// What clicking a notification does. @@ -160,6 +200,17 @@ pub fn request_update(r: Release) -> bool { } } +/// The editor's server rescanned by itself (its Rescan button, a request that failed): let +/// the tray follow who is asleep now. Nothing to do when no tray is running (`--serve`). +pub fn rescanned() { + unsafe { + let tray = FindWindowW(wide(CLASS_NAME).as_ptr(), null()); + if !tray.is_null() { + PostMessageW(tray, WM_RESCANNED, 0, 0); + } + } +} + /// A new-version check's answer, and whether it was asked for from the menu (then /// "you're up to date" and errors are shown too). struct UpdateResult { @@ -299,7 +350,6 @@ pub fn run() { editor_url: None, profile_items: Vec::new(), apply_all_pending: true, - retry_step: 0, synapse_warned: false, low_battery: HashSet::new(), buttons_restored: HashMap::new(), @@ -318,6 +368,11 @@ pub fn run() { installs, locked: false, screen_off: false, + asleep: Vec::new(), + input_watch: (false, false), + input_woke: false, + input_wakes_left: INPUT_WAKES, + input_other: null_mut(), }) }); log::append("tray started"); @@ -383,6 +438,20 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: } 0 } + WM_RESCANNED => { + if with_app(|app| app.follow_sleepers()).is_none() { + SetTimer(hwnd, TIMER_SLEEPERS, SETTLE_MS, None); + } + 0 + } + WM_INPUT => { + if with_app(|app| app.woken_by(lparam as HRAWINPUT)) == Some(true) { + schedule_rescan(hwnd, false); + start_retries(hwnd); + } + // Windows releases the input's buffer here. + DefWindowProcW(hwnd, msg, wparam, lparam) + } WM_REPORT_READY => { with_app(|app| { if wparam == 1 { @@ -400,12 +469,19 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: let event = wparam as u32; if (event == DBT_DEVICEARRIVAL || event == DBT_DEVICEREMOVECOMPLETE) && is_razer(lparam) { schedule_rescan(hwnd, false); + // A receiver answers before the keyboard or mouse behind it does: after its + // dock is plugged back in the device stays quiet until its next key press. + // One look isn't enough, so keep looking, as after start and wake. + if event == DBT_DEVICEARRIVAL { + start_retries(hwnd); + } } TRUE as LRESULT } WM_POWERBROADCAST => { if wparam as u32 == PBT_APMRESUMEAUTOMATIC { log::append("resumed from sleep"); + with_app(|app| app.input_wakes_left = INPUT_WAKES); schedule_rescan(hwnd, true); start_retries(hwnd); } else if wparam as u32 == PBT_POWERSETTINGCHANGE { @@ -435,16 +511,42 @@ unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: } WM_TIMER if wparam == TIMER_RETRY => { KillTimer(hwnd, TIMER_RETRY); + let chain = RETRY_CHAIN.load(Ordering::Relaxed); let next = with_app(|app| { app.rescan(); - app.retry_step += 1; - RETRY_DELAYS_MS.get(app.retry_step).copied().or_else(|| { - let more = app.retry_step < RETRY_DELAYS_MS.len() + SLEEPER_RETRIES; + // A device arrived during that rescan and started the retries over: its + // timer stands, and this retry was the old chain's last. + if RETRY_CHAIN.load(Ordering::Relaxed) != chain { + return None; + } + let step = RETRY_STEP.fetch_add(1, Ordering::Relaxed) + 1; + let next = RETRY_DELAYS_MS.get(step).copied().or_else(|| { + let more = step < RETRY_DELAYS_MS.len() + SLEEPER_RETRIES; (more && app.engine().has_sleeping()).then_some(SLEEPER_RETRY_MS) - }) + }); + // The quick retries that input started are over: watch for input again. + if app.input_woke && step >= RETRY_DELAYS_MS.len() { + app.input_woke = false; + app.follow_sleepers(); + } + next }); - if let Some(Some(ms)) = next { - SetTimer(hwnd, TIMER_RETRY, ms, None); + match next { + Some(Some(ms)) => { + SetTimer(hwnd, TIMER_RETRY, ms, None); + } + Some(None) => {} + // Busy: this retry came in during another message. Try it again shortly. + None => { + SetTimer(hwnd, TIMER_RETRY, SETTLE_MS, None); + } + } + 0 + } + WM_TIMER if wparam == TIMER_SLEEPERS => { + KillTimer(hwnd, TIMER_SLEEPERS); + if with_app(|app| app.follow_sleepers()).is_none() { + SetTimer(hwnd, TIMER_SLEEPERS, SETTLE_MS, None); } 0 } @@ -527,7 +629,8 @@ unsafe extern "system" fn on_foreground(_: HWINEVENTHOOK, _: u32, _: HWND, _: i3 /// Rescan a few more times over the next minute (new devices get their profile). fn start_retries(hwnd: HWND) { - with_app(|app| app.retry_step = 0); + RETRY_STEP.store(0, Ordering::Relaxed); + RETRY_CHAIN.fetch_add(1, Ordering::Relaxed); unsafe { SetTimer(hwnd, TIMER_RETRY, RETRY_DELAYS_MS[0], None) }; } @@ -544,17 +647,24 @@ impl App { fn rescan(&mut self) { let apply_all = std::mem::take(&mut self.apply_all_pending); - let (reports, new_names, synapse) = { + let (reports, new_names, gone, synapse) = { let mut engine = self.engine(); + let before: Vec<(String, String)> = engine.devices().iter().map(|d| (d.key(), engine.label(d))).collect(); let new = engine.rescan(); let names: Vec = engine.devices().iter().filter(|d| new.contains(&d.key())).map(|d| engine.label(d)).collect(); - let targets = if apply_all { engine.all_keys() } else { new }; - (engine.apply(&targets), names, engine.synapse().to_vec()) + let now = engine.all_keys(); + let gone: Vec = before.into_iter().filter(|(key, _)| !now.contains(key)).map(|(_, name)| name).collect(); + let targets = if apply_all { now } else { new }; + (engine.apply(&targets), names, gone, engine.synapse().to_vec()) }; + if !gone.is_empty() { + log::append(&format!("gone: {}", gone.join(", "))); + } if !new_names.is_empty() { log::append(&format!("found: {}", new_names.join(", "))); } + self.follow_sleepers(); self.notify_failures(&reports); self.warn_synapse(&synapse); self.check_battery(); @@ -565,6 +675,82 @@ impl App { } } + /// Note which known wireless devices are plugged in but not answering, and watch for + /// input from them: the first key press or movement means the device is awake, and + /// starts the retries. Called after everything that may change who is asleep. + fn follow_sleepers(&mut self) { + let asleep: Vec = + self.engine().sleeping().map(|u| Sleeper { pid: u.pid, name: u.name.clone(), kind: u.kind }).collect(); + let fresh: Vec<&str> = + asleep.iter().filter(|s| !self.asleep.iter().any(|old| old.pid == s.pid)).map(|s| s.name.as_str()).collect(); + if asleep.is_empty() { + (self.input_woke, self.input_wakes_left) = (false, INPUT_WAKES); + } + let watch = !self.input_woke && self.input_wakes_left > 0; + let keys = watch && asleep.iter().any(|s| matches!(s.kind, Some(Kind::Keyboard | Kind::Keypad))); + let mice = watch && asleep.iter().any(|s| s.kind == Some(Kind::Mouse)); + self.watch_input(keys, mice); + if !fresh.is_empty() { + let how = if self.input_watch == (false, false) { "" } else { ", looking again at its next key press or movement" }; + log::append(&format!("not answering: {} (asleep?){how}", fresh.join(", "))); + } + self.asleep = asleep; + } + + /// Register or drop raw input from keyboards and from mice, whichever changed. + fn watch_input(&mut self, keys: bool, mice: bool) { + self.input_other = null_mut(); + let (had_keys, had_mice) = self.input_watch; + for (usage, had, want) in [(USAGE_KEYBOARD, had_keys, keys), (USAGE_MOUSE, had_mice, mice)] { + if had == want { + continue; + } + let device = RAWINPUTDEVICE { + usUsagePage: USAGE_PAGE_DESKTOP, + usUsage: usage, + // INPUTSINK: also while another window has the focus (this one never does). + dwFlags: if want { RIDEV_INPUTSINK } else { RIDEV_REMOVE }, + hwndTarget: if want { self.hwnd } else { null_mut() }, + }; + let done = unsafe { RegisterRawInputDevices(&device, 1, size_of::() as u32) } != 0; + // A watch that couldn't start isn't one; one that couldn't stop only costs + // a look at each input, and is asked to stop again next time. + let now = if done { want } else { had && !want }; + if usage == USAGE_KEYBOARD { + self.input_watch.0 = now; + } else { + self.input_watch.1 = now; + } + } + } + + /// Raw input arrived. True when a device that is asleep sent it: it is awake, so look + /// for it. Only who sent the input is read, never the input itself. + fn woken_by(&mut self, input: HRAWINPUT) -> bool { + if self.input_woke || self.input_wakes_left == 0 || self.asleep.is_empty() { + return false; + } + // Input made by a program (remote desktop, a macro tool) comes from no device. + let Some(source) = (unsafe { raw_input_source(input) }) else { return false }; + if source == self.input_other { + return false; + } + let path = unsafe { raw_device_path(source) }; + let Some(sleeper) = self.asleep.iter().find(|s| path.contains(&hardware_id(s.pid))) else { + // A device Windows couldn't name is asked about again at its next report. + if !path.is_empty() { + self.input_other = source; + } + return false; + }; + log::append(&format!("input from {}: looking for it again", sleeper.name)); + self.input_woke = true; + self.input_wakes_left = self.input_wakes_left.saturating_sub(1); + // A mouse reports a thousand times a second: no more input until this search is over. + self.watch_input(false, false); + true + } + /// The lights go down while the PC is locked or its screen is off, if that's set (#48), /// and come back after. fn set_away(&mut self, locked: bool, screen_off: bool) { @@ -1185,6 +1371,32 @@ unsafe fn is_razer(lparam: LPARAM) -> bool { } +/// How a Razer product shows in a device path: `VID_1532&PID_0290`. +fn hardware_id(pid: u16) -> String { + format!("VID_{RAZER_VID:04X}&PID_{pid:04X}") +} + +/// The device a raw input came from; `None` for input no device sent. +unsafe fn raw_input_source(input: HRAWINPUT) -> Option { + let mut header: RAWINPUTHEADER = zeroed(); + let mut size = size_of::() as u32; + let read = GetRawInputData(input, RID_HEADER, &mut header as *mut _ as *mut c_void, &mut size, size_of::() as u32); + (read != u32::MAX && !header.hDevice.is_null()).then_some(header.hDevice) +} + +/// A raw input device's path in upper case, like `\\?\HID#VID_1532&PID_0290&MI_00#...`; +/// empty when Windows doesn't give one. +unsafe fn raw_device_path(device: HANDLE) -> String { + let mut len = 0u32; + GetRawInputDeviceInfoW(device, RIDI_DEVICENAME, null_mut(), &mut len); + let mut buf = vec![0u16; len as usize]; + if buf.is_empty() || GetRawInputDeviceInfoW(device, RIDI_DEVICENAME, buf.as_mut_ptr() as *mut c_void, &mut len) == u32::MAX { + return String::new(); + } + let end = buf.iter().position(|&c| c == 0).unwrap_or(buf.len()); + String::from_utf16_lossy(&buf[..end]).to_ascii_uppercase() +} + /// Put text on the clipboard. False if another program holds it. fn set_clipboard(text: &str) -> bool { use windows_sys::Win32::System::DataExchange::{CloseClipboard, EmptyClipboard, OpenClipboard, SetClipboardData}; @@ -1239,3 +1451,21 @@ unsafe fn load_icon() -> HICON { icon as HICON } } + +#[cfg(test)] +mod tests { + use super::hardware_id; + + /// The keyboard collection of a DeathStalker V2 Pro receiver, as raw input names it + /// (upper-cased, as [`super::raw_device_path`] returns it). + const RECEIVER_KEYS: &str = r"\\?\HID#VID_1532&PID_0290&MI_00#A&1463A5&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD}"; + + #[test] + fn input_is_matched_to_the_device_that_is_asleep() { + assert_eq!(hardware_id(0x0290), "VID_1532&PID_0290"); + assert!(RECEIVER_KEYS.contains(&hardware_id(0x0290))); + // The same keyboard on its cable is another product, and so is another vendor's. + assert!(!RECEIVER_KEYS.contains(&hardware_id(0x0292))); + assert!(!r"\\?\HID#VID_046D&PID_0290&MI_00#7&0&0000#{884B96C3-56EF-11D1-BC8C-00A0C91405DD}".contains(&hardware_id(0x0290))); + } +}