Skip to main content

openlogi_core/
device.rs

1//! Serializable device-model types.
2//!
3//! These mirror the HID++ types from the `hidpp` crate but live here so the
4//! CLI and any future GUI can depend on them without dragging in the protocol
5//! crate or its async transport.
6
7use serde::{Deserialize, Serialize};
8
9use crate::hid::DeviceRoute;
10
11mod light;
12
13pub use light::{LightCapabilities, LightValueRange, LightValueRangeError, LightValueUnit};
14
15/// What a paired peripheral is. Mirrors `hidpp::receiver::bolt::BoltDeviceKind`
16/// but is owned by us so consumers don't depend on `hidpp`.
17///
18/// Several upstream "device type" vocabularies feed this one enum, and they do
19/// **not** agree on numbers: the Bolt pairing register uses `Unknown=0,
20/// Keyboard=1, Mouse=2, …`, while the HID++ `0x0005` feature uses
21/// `Keyboard=0, …, Mouse=3, …` (no `Unknown` at all). The asset registry adds a
22/// third, free-form *string* type (`"mouse"`, case-inconsistently `"MOUSE"`).
23/// They are converted to this enum at their respective boundaries — never by
24/// reinterpreting one source's raw byte with another's table — so the numeric
25/// mismatch can't leak past those mappers.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
27#[serde(rename_all = "lowercase")]
28pub enum DeviceKind {
29    /// Mice — the family OpenLogi's binding/DPI panels primarily target.
30    Mouse,
31    /// Keyboards, including lighting-capable ones.
32    Keyboard,
33    /// Standalone numeric keypads.
34    Numpad,
35    /// Presentation remotes (slide clickers).
36    Presenter,
37    /// Remote controls; the registry's `"remotecontrol"` string also folds here.
38    Remote,
39    /// Trackballs — treated like mice for presumed capabilities.
40    Trackball,
41    /// External touchpads; the registry's `"trackpad"` string also folds here.
42    Touchpad,
43    /// Pen/graphics tablets.
44    Tablet,
45    /// Game controllers, mirrored from the Bolt pairing vocabulary.
46    Gamepad,
47    /// Joysticks, mirrored from the Bolt pairing vocabulary.
48    Joystick,
49    /// Audio headsets paired through a receiver.
50    Headset,
51    /// Logitech webcam (UVC), configured through `openlogi-camera`.
52    Camera,
53    /// Not classified by any source.
54    Unknown,
55    /// Standalone light or other illumination device controlled outside HID++.
56    ///
57    /// This is an identity hint only. UI controls are gated by the dedicated
58    /// light capability descriptor, never by this variant alone.
59    Light,
60}
61
62impl DeviceKind {
63    /// Parse the OpenLogi asset registry's `type` string into a [`DeviceKind`].
64    ///
65    /// The registry field is free-form and case-inconsistent (both `"mouse"`
66    /// and `"MOUSE"` ship), so we case-fold before matching. `None` for values
67    /// we don't model — "no asset opinion", typed as the absence of one rather
68    /// than overloading [`DeviceKind::Unknown`], which stays a real wire value
69    /// a live device can carry.
70    #[must_use]
71    pub fn from_registry_type(raw: &str) -> Option<Self> {
72        match raw.trim().to_ascii_lowercase().as_str() {
73            "mouse" => Some(Self::Mouse),
74            "keyboard" => Some(Self::Keyboard),
75            "numpad" => Some(Self::Numpad),
76            "presenter" => Some(Self::Presenter),
77            "remote" | "remotecontrol" => Some(Self::Remote),
78            "trackball" => Some(Self::Trackball),
79            "touchpad" | "trackpad" => Some(Self::Touchpad),
80            "tablet" => Some(Self::Tablet),
81            "gamepad" => Some(Self::Gamepad),
82            "joystick" => Some(Self::Joystick),
83            "headset" => Some(Self::Headset),
84            "camera" => Some(Self::Camera),
85            "light" | "lighting" | "illumination_light" => Some(Self::Light),
86            _ => None,
87        }
88    }
89}
90
91/// What a device can be *configured* to do, derived from the HID++ feature
92/// table it reports (feature `0x0001`). This is the source of truth for which
93/// configuration panels the UI offers — a panel shows iff the device exposes
94/// the feature that drives it. Gating on capability rather than on
95/// [`DeviceKind`] is what keeps a misclassified device from losing its panels
96/// (issue #127): kind is an identity guess, capability is what the firmware
97/// actually announced.
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
99#[serde(deny_unknown_fields)]
100#[expect(
101    clippy::struct_excessive_bools,
102    reason = "capabilities is a serialized feature-bit DTO; independent booleans keep the IPC/config shape explicit"
103)]
104pub struct Capabilities {
105    /// Reprogrammable buttons — HID++ `0x1b00`–`0x1b04` (ReprogControls).
106    pub buttons: bool,
107    /// Adjustable pointer resolution — HID++ `0x2201` / `0x2202` (AdjustableDpi).
108    pub pointer: bool,
109    /// Solid-colour RGB the lighting panel can drive — HID++ effect engines
110    /// (`0x8070` / `0x8071`) or per-zone lighting (`0x8080` / `0x8081`).
111    /// Backlight-only families aren't driven by the panel, so they don't earn
112    /// an inert Lighting tab.
113    pub lighting: bool,
114    /// Native vertical wheel inversion — HID++ `0x2121 HiResWheel` with the
115    /// firmware-reported `has_invert` capability.
116    pub scroll_inversion: bool,
117    /// HID++ `0x2121 HiResWheel` is present, so the wheel reporting resolution
118    /// can be read and changed independently of inversion support.
119    #[serde(default)]
120    pub hires_wheel: bool,
121    /// A horizontal thumb wheel is available: either the dedicated HID++
122    /// `0x2150 Thumbwheel` feature or a legacy `0x6501 Gestures2` descriptor
123    /// (gesture id 46, used by MX Master 2S).
124    #[serde(default)]
125    pub thumbwheel: bool,
126    /// Programmable haptic feedback — reverse-engineered HID++ `0x19b0`.
127    #[serde(default)]
128    pub haptic_feedback: bool,
129    /// A divertable Haptic Sense Panel control (`0x01a0`) was found in the
130    /// device's `0x1b04` control table.
131    #[serde(default)]
132    pub haptic_panel: bool,
133    /// A DPI/ModeShift control in the device's `0x1b04` control table supports
134    /// both diversion and raw-XY reporting for hold-and-swipe gestures.
135    #[serde(default)]
136    pub dpi_gestures: bool,
137    /// Keyboard Fn-lock can be read and written — HID++ `0x40a2` or `0x40a3`
138    /// fn inversion. Appended: crosses the IPC and the persisted identity.
139    #[serde(default)]
140    pub fn_lock: bool,
141}
142
143impl Capabilities {
144    /// Derive capabilities from the set of HID++ feature IDs a device reports.
145    /// Membership of a driving feature ID flips the corresponding flag.
146    #[must_use]
147    pub fn from_feature_ids(ids: &[u16]) -> Self {
148        const BUTTONS: [u16; 5] = [0x1b00, 0x1b01, 0x1b02, 0x1b03, 0x1b04];
149        const POINTER: [u16; 2] = [0x2201, 0x2202];
150        // Every family here is driven by `set_keyboard_color`, which tries
151        // effect engines before per-zone paths. Backlight (0x198x) stays out.
152        const LIGHTING: [u16; 4] = [0x8070, 0x8071, 0x8081, 0x8080];
153        let has = |family: &[u16]| ids.iter().any(|id| family.contains(id));
154        Self {
155            buttons: has(&BUTTONS),
156            pointer: has(&POINTER),
157            lighting: has(&LIGHTING),
158            scroll_inversion: false,
159            hires_wheel: ids.contains(&0x2121),
160            thumbwheel: ids.contains(&0x2150),
161            haptic_feedback: ids.contains(&0x19b0),
162            haptic_panel: false,
163            dpi_gestures: false,
164            fn_lock: ids.contains(&0x40a2) || ids.contains(&0x40a3),
165        }
166    }
167
168    /// Best-effort capabilities for a device we could not probe (offline /
169    /// never reached), guessed from its [`DeviceKind`]. Used only as a fallback
170    /// when no measured [`Capabilities`] exist — a sleeping mouse should still
171    /// show its button/pointer panels so its bindings (host-side) stay
172    /// configurable.
173    #[must_use]
174    pub fn presumed_from_kind(kind: DeviceKind) -> Self {
175        match kind {
176            DeviceKind::Mouse | DeviceKind::Trackball => Self {
177                buttons: true,
178                pointer: true,
179                ..Self::default()
180            },
181            DeviceKind::Keyboard => Self {
182                lighting: true,
183                ..Self::default()
184            },
185            _ => Self::default(),
186        }
187    }
188}
189
190/// Coarse battery bucket reported by the device firmware.
191#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
192#[serde(rename_all = "lowercase")]
193pub enum BatteryLevel {
194    /// Almost depleted — the firmware's most urgent bucket.
195    Critical,
196    /// Running low; worth surfacing a charge hint.
197    Low,
198    /// Comfortable middle range, no user action needed.
199    Good,
200    /// At or near full charge.
201    Full,
202    /// The firmware did not report a level, or reported one we don't model.
203    Unknown,
204}
205
206/// Charging state. Mirrors `hidpp 0.2`'s `BatteryStatus` plus `Unknown` for
207/// values added in future protocol versions.
208#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
209#[serde(rename_all = "snake_case")]
210pub enum BatteryStatus {
211    /// Running on battery.
212    Discharging,
213    /// Charging at the normal rate.
214    Charging,
215    /// Charging at reduced current (e.g. from a weak power source).
216    ChargingSlow,
217    /// Charge complete while still connected to power.
218    Full,
219    /// The device reported a charging fault.
220    Error,
221    /// A status value this build doesn't model (future protocol additions).
222    Unknown,
223}
224
225/// Battery snapshot for one paired device, as last polled over HID++.
226#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
227pub struct BatteryInfo {
228    /// Reported charge percentage (`0..=100`).
229    pub percentage: u8,
230    /// Coarse bucket for UI that doesn't want the raw percentage.
231    pub level: BatteryLevel,
232    /// Charging state at poll time.
233    pub status: BatteryStatus,
234}
235
236/// Identity of an enumerated receiver — no paired-device state (that lives
237/// in [`DeviceInventory::paired`]). For a direct (Bluetooth/wired) device,
238/// a synthetic entry mirroring the device's own HID identity fills this role.
239#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
240pub struct ReceiverInfo {
241    /// Product string from the HID enumeration (e.g. `"Logi Bolt Receiver"`).
242    pub name: String,
243    /// USB vendor ID (`0x046d` for Logitech).
244    pub vendor_id: u16,
245    /// USB product ID distinguishing the receiver model.
246    pub product_id: u16,
247    /// Platform-reported serial, when one is exposed. Deliberately excluded
248    /// from diagnostics (see [`crate::diagnostics::ReceiverDiag`]).
249    pub unique_id: Option<String>,
250}
251
252/// HID++ `DeviceInformation` (feature 0x0003) snapshot used to identify a
253/// device against external registries (e.g. the OpenLogi asset index).
254///
255/// `model_ids` is the per-transport PID array reported by the firmware,
256/// ordered to match the transports flagged in [`Self::transports`] (USB,
257/// eQuad, BTLE, Bluetooth) — slots that aren't enabled stay `0`. The Logi
258/// Options+ asset registry's `modelId` (e.g. `"6b023"`) is the concatenation
259/// of an extended-model byte and one of these PIDs, so callers usually want
260/// to format `extended_model_id` + `model_ids[N]` to match.
261#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
262#[serde(deny_unknown_fields)]
263pub struct DeviceModelInfo {
264    /// Number of firmware entities (main firmware, bootloader, …) the
265    /// device reports.
266    pub entity_count: u8,
267    /// HID++ DeviceInformation serial number, when the device supports the
268    /// optional serial-number function.
269    pub serial_number: Option<String>,
270    /// Per-unit ID bytes — unique to the physical unit, unlike the
271    /// model-level fields around it.
272    pub unit_id: [u8; 4],
273    /// Which transports the firmware supports; defines the slot order of
274    /// [`Self::model_ids`].
275    pub transports: DeviceTransports,
276    /// Per-transport PIDs for the transports enabled in [`Self::transports`],
277    /// packed from the front in ascending HID++ transport-bit order:
278    /// Bluetooth (bit 0), BTLE (bit 1), eQuad (bit 2), USB (bit 3). Only
279    /// enabled transports take a slot — a disabled transport is skipped, not
280    /// zero-filled — so unused trailing slots stay `0` and the array holds at
281    /// most three PIDs.
282    ///
283    /// Note this is the reverse of the field order of [`DeviceTransports`],
284    /// which lists `usb` first. A device with `usb + equad` reports
285    /// `[eQuad PID, USB PID, 0]`; one with `equad + btle` reports
286    /// `[BTLE PID, eQuad PID, 0]`.
287    pub model_ids: [u16; 3],
288    /// Extra model byte prefixed to a PID to form the asset registry's
289    /// `modelId` — see [`Self::model_key`].
290    pub extended_model_id: u8,
291}
292
293impl DeviceModelInfo {
294    /// The model's key in the OpenLogi asset registry: the string the depot
295    /// `manifest.json` uses for its `modelId` field, formatted
296    /// `{extended_model_id:x}{model_ids[0]:04x}`. An MX Master 4 with
297    /// `extended_model_id = 0x02` and `model_ids[0] = 0xb042` resolves to
298    /// `"2b042"`. Per-device configuration is keyed by
299    /// [`crate::device_order::DeviceIdentity::config_key`], not by this.
300    #[must_use]
301    pub fn model_key(&self) -> String {
302        format!("{:x}{:04x}", self.extended_model_id, self.model_ids[0])
303    }
304}
305
306/// Mirror of hidpp's `DeviceTransport` bitfield — one bool per protocol the
307/// device firmware exposes. The shape is dictated by HID++ feature 0x0003;
308/// a state machine doesn't fit since a single device can announce multiple
309/// transports simultaneously.
310#[expect(
311    clippy::struct_excessive_bools,
312    reason = "bitfield mirroring HID++ DeviceInformation; transports are independent flags"
313)]
314#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
315#[serde(deny_unknown_fields)]
316pub struct DeviceTransports {
317    /// Wired USB.
318    pub usb: bool,
319    /// Logitech eQuad — the Unifying/Bolt receiver RF protocol.
320    pub equad: bool,
321    /// Bluetooth Low Energy.
322    pub btle: bool,
323    /// Classic Bluetooth.
324    pub bluetooth: bool,
325}
326
327/// One device in the agent's inventory snapshot: a receiver pairing slot,
328/// or a direct (Bluetooth/wired) attachment under its synthetic
329/// [`ReceiverInfo`]. Embedded in [`DeviceInventory`], so its field order is
330/// IPC wire format — see that type's contract.
331#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
332pub struct PairedDevice {
333    /// Receiver-assigned slot (1..=6 for Bolt).
334    pub slot: u8,
335    /// Firmware codename (e.g. `"MX Master 3S"`), when reported.
336    pub codename: Option<String>,
337    /// Wireless product ID. `None` for offline / unreachable devices on hidpp 0.2.
338    pub wpid: Option<u16>,
339    /// Best-guess classification. Identity only — panel gating uses
340    /// [`Self::capabilities`] instead, so a misread kind can't hide panels
341    /// (issue #127).
342    pub kind: DeviceKind,
343    /// Whether the device was reachable at enumeration time; offline devices
344    /// keep their slot with reduced detail.
345    pub online: bool,
346    /// Last battery reading, `None` when offline or the device doesn't
347    /// report battery.
348    pub battery: Option<BatteryInfo>,
349    /// Output of HID++ feature 0x0003 — populated for online devices that
350    /// expose the feature. Drives asset-registry lookups in the GUI.
351    pub model_info: Option<DeviceModelInfo>,
352    /// Configuration capabilities derived from the device's HID++ feature
353    /// table. `None` for devices we couldn't probe (offline / unreachable);
354    /// the GUI then falls back to [`Capabilities::presumed_from_kind`].
355    pub capabilities: Option<Capabilities>,
356}
357
358/// Address of a standalone raw-HID interface.
359///
360/// The identity is an opaque transport-generated string. It is deliberately
361/// kept separate from the HID++ receiver/slot address so a raw device cannot
362/// accidentally enter the HID++ `Direct` path.
363#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
364pub struct RawDeviceAddress {
365    /// HID vendor ID.
366    pub vendor_id: u16,
367    /// HID product ID.
368    pub product_id: u16,
369    /// HID usage page.
370    pub usage_page: u16,
371    /// HID usage ID.
372    pub usage_id: u16,
373    /// Identity chosen by the transport: a serial when available, otherwise
374    /// an explicitly transient OS-node identity. It is never an enumeration
375    /// index and a transient value is not persisted as a physical key.
376    pub identity: String,
377}
378
379/// A standalone device that is not a HID++ receiver pairing slot.
380///
381/// This is the inventory bridge for Litra and future non-HID++ categories.
382/// Receiver-backed devices continue to use [`PairedDevice`] inside
383/// [`DeviceInventory`].
384#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
385pub struct StandaloneDevice {
386    /// Raw HID address used to re-find the interface.
387    pub address: RawDeviceAddress,
388    /// Human-readable name supplied by the OS/HID descriptor.
389    pub display_name: String,
390    /// Human-readable manufacturer, when available.
391    pub manufacturer: Option<String>,
392    /// Device serial, when the HID backend exposes one.
393    pub serial_number: Option<String>,
394    /// Stable four-byte identity when the protocol/driver provides one.
395    /// Raw HID drivers may use zeroes when no such field exists.
396    pub unit_id: [u8; 4],
397    /// Identity classification. Capability fields gate controls.
398    pub kind: DeviceKind,
399    /// Whether this interface was present in the latest completed scan.
400    pub online: bool,
401    /// HID++ capabilities are absent for a non-HID++ device.
402    pub capabilities: Option<Capabilities>,
403    /// Standalone capability descriptor, if the selected driver recognizes it.
404    pub light_capabilities: Option<LightCapabilities>,
405    /// Stable identifier of the driver family that owns this raw interface.
406    /// This is deliberately separate from the product ID so a future family
407    /// can share a protocol driver across several product variants.
408    pub driver_id: String,
409    /// Optional model-level identity in the OpenLogi asset registry.
410    ///
411    /// This is deliberately appended: `StandaloneDevice` crosses the
412    /// append-only GUI↔agent bincode wire format.
413    #[serde(default)]
414    pub registry_model_id: Option<String>,
415}
416
417impl StandaloneDevice {
418    /// The route that reaches this device.
419    #[must_use]
420    pub fn route(&self) -> DeviceRoute {
421        DeviceRoute::from(&self.address)
422    }
423}
424
425/// One receiver and its paired devices — the unit the agent's inventory
426/// snapshot is made of.
427///
428/// Crosses the agent↔GUI IPC (everything it embeds too: [`ReceiverInfo`],
429/// [`PairedDevice`], battery/model-info/capability types). bincode encodes
430/// field and variant *order*, so reordering, retyping, or wrapping any field
431/// in this tree is a wire-format change and requires a `PROTOCOL_VERSION`
432/// bump (guarded by `openlogi-ipc/tests/wire_format.rs`).
433#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
434pub struct DeviceInventory {
435    /// The receiver's identity — synthetic (mirroring the device itself)
436    /// for a direct Bluetooth/wired attachment.
437    pub receiver: ReceiverInfo,
438    /// The devices reached through this receiver; a direct attachment
439    /// carries exactly one entry.
440    pub paired: Vec<PairedDevice>,
441}
442
443#[cfg(test)]
444mod tests {
445    use super::{
446        BatteryInfo, BatteryLevel, BatteryStatus, Capabilities, DeviceInventory, DeviceKind,
447        DeviceModelInfo, DeviceTransports, LightValueRange, LightValueUnit, PairedDevice,
448        ReceiverInfo,
449    };
450
451    fn inventory(slot: u8, wpid: Option<u16>, battery_percentage: u8) -> DeviceInventory {
452        DeviceInventory {
453            receiver: ReceiverInfo {
454                name: "Logi Bolt Receiver".to_string(),
455                vendor_id: 0x046d,
456                product_id: 0xc548,
457                unique_id: Some("receiver-1".to_string()),
458            },
459            paired: vec![PairedDevice {
460                slot,
461                codename: Some("MX Test".to_string()),
462                wpid,
463                kind: DeviceKind::Mouse,
464                online: true,
465                battery: Some(BatteryInfo {
466                    percentage: battery_percentage,
467                    level: BatteryLevel::Good,
468                    status: BatteryStatus::Discharging,
469                }),
470                model_info: Some(DeviceModelInfo {
471                    entity_count: 1,
472                    serial_number: Some("serial-1".to_string()),
473                    unit_id: [1, 2, 3, 4],
474                    transports: DeviceTransports {
475                        usb: true,
476                        equad: true,
477                        btle: false,
478                        bluetooth: false,
479                    },
480                    model_ids: [0xb023, 0, 0],
481                    extended_model_id: 0x02,
482                }),
483                capabilities: Some(Capabilities {
484                    buttons: true,
485                    pointer: true,
486                    lighting: false,
487                    scroll_inversion: false,
488                    hires_wheel: false,
489                    thumbwheel: false,
490                    haptic_feedback: false,
491                    haptic_panel: false,
492                    dpi_gestures: false,
493                    fn_lock: false,
494                }),
495            }],
496        }
497    }
498
499    #[test]
500    fn device_inventory_equality_includes_nested_device_fields() {
501        let base = inventory(1, Some(0xb023), 86);
502        assert_eq!(base, base.clone());
503
504        assert_ne!(
505            base,
506            inventory(2, Some(0xb023), 86),
507            "slot changes must affect inventory equality"
508        );
509        assert_ne!(
510            base,
511            inventory(1, Some(0xb024), 86),
512            "wireless product id changes must affect inventory equality"
513        );
514        assert_ne!(
515            base,
516            inventory(1, Some(0xb023), 87),
517            "nested battery changes must affect inventory equality"
518        );
519    }
520
521    #[test]
522    fn registry_type_is_case_folded() {
523        // The registry ships both `"mouse"` and `"MOUSE"`; both must resolve so
524        // the asset cross-check can't silently miss a depot.
525        assert_eq!(
526            DeviceKind::from_registry_type("mouse"),
527            Some(DeviceKind::Mouse)
528        );
529        assert_eq!(
530            DeviceKind::from_registry_type("MOUSE"),
531            Some(DeviceKind::Mouse)
532        );
533        assert_eq!(
534            DeviceKind::from_registry_type("  Keyboard "),
535            Some(DeviceKind::Keyboard)
536        );
537    }
538
539    #[test]
540    fn unknown_registry_type_defers_to_the_caller() {
541        // Unmodelled / empty → no asset opinion.
542        assert_eq!(DeviceKind::from_registry_type("webcam"), None);
543        assert_eq!(DeviceKind::from_registry_type(""), None);
544    }
545
546    #[test]
547    fn capabilities_track_the_driving_feature_ids() {
548        use super::Capabilities;
549        // A typical MX mouse: ReprogControls (0x1b04) + ExtendedAdjustableDpi
550        // (0x2202), no lighting.
551        let mouse =
552            Capabilities::from_feature_ids(&[0x0003, 0x1b04, 0x2121, 0x2150, 0x2202, 0x2110]);
553        assert_eq!(
554            mouse,
555            Capabilities {
556                buttons: true,
557                pointer: true,
558                lighting: false,
559                scroll_inversion: false,
560                hires_wheel: true,
561                thumbwheel: true,
562                haptic_feedback: false,
563                haptic_panel: false,
564                dpi_gestures: false,
565                fn_lock: false,
566            }
567        );
568        assert!(!Capabilities::from_feature_ids(&[0x0003, 0x1b04]).thumbwheel);
569        // A wired G-series keyboard: PerKeyLighting (0x8080), no DPI/buttons.
570        let keyboard = Capabilities::from_feature_ids(&[0x0001, 0x8080]);
571        assert_eq!(
572            keyboard,
573            Capabilities {
574                buttons: false,
575                pointer: false,
576                lighting: true,
577                scroll_inversion: false,
578                hires_wheel: false,
579                thumbwheel: false,
580                haptic_feedback: false,
581                haptic_panel: false,
582                dpi_gestures: false,
583                fn_lock: false,
584            }
585        );
586        // No driving features → nothing offered.
587        assert_eq!(
588            Capabilities::from_feature_ids(&[0x0000, 0x0003]),
589            Capabilities::default()
590        );
591    }
592
593    #[test]
594    fn every_drivable_lighting_family_earns_the_tab() {
595        // `set_keyboard_color` walks 0x8070 → 0x8071 → 0x8081 → 0x8080.
596        for id in [0x8070, 0x8071, 0x8080, 0x8081] {
597            assert!(
598                Capabilities::from_feature_ids(&[0x0001, id]).lighting,
599                "0x{id:04x} must offer the lighting tab"
600            );
601        }
602        // Backlight (0x198x) stays out — the panel cannot drive it.
603        assert!(!Capabilities::from_feature_ids(&[0x0001, 0x1982]).lighting);
604        // Either fn-inversion feature gives the keyboard an Fn-lock control.
605        assert!(Capabilities::from_feature_ids(&[0x0001, 0x40a3]).fn_lock);
606        assert!(Capabilities::from_feature_ids(&[0x0001, 0x40a2]).fn_lock);
607        assert!(!Capabilities::from_feature_ids(&[0x0001, 0x1982]).fn_lock);
608    }
609
610    #[test]
611    fn persisted_capabilities_without_appended_wheel_fields_load_as_unsupported()
612    -> Result<(), toml::de::Error> {
613        use super::Capabilities;
614
615        let capabilities: Capabilities = toml::from_str(
616            r"
617                buttons = true
618                pointer = true
619                lighting = false
620                scroll_inversion = true
621            ",
622        )?;
623
624        assert!(!capabilities.hires_wheel);
625        assert!(!capabilities.thumbwheel);
626        assert!(capabilities.scroll_inversion);
627        Ok(())
628    }
629
630    #[test]
631    fn presumed_capabilities_keep_an_unprobed_mouse_configurable() {
632        use super::Capabilities;
633        let mouse = Capabilities::presumed_from_kind(DeviceKind::Mouse);
634        assert!(mouse.buttons && mouse.pointer && !mouse.lighting);
635        assert!(!mouse.thumbwheel);
636        assert!(Capabilities::presumed_from_kind(DeviceKind::Keyboard).lighting);
637        // An unidentified device presumes nothing — it must be measured.
638        assert_eq!(
639            Capabilities::presumed_from_kind(DeviceKind::Unknown),
640            Capabilities::default()
641        );
642    }
643
644    #[test]
645    fn light_ranges_reject_invalid_grids_and_units() {
646        LightValueRange::new(10, 1, 1, LightValueUnit::Lumens)
647            .expect_err("a minimum above the maximum must be rejected");
648        LightValueRange::new(0, 10, 0, LightValueUnit::Lumens)
649            .expect_err("a zero step must be rejected");
650        LightValueRange::new(0, 10, 3, LightValueUnit::Lumens)
651            .expect_err("a step that does not divide the span must be rejected");
652        LightValueRange::new(0, 101, 1, LightValueUnit::Percent)
653            .expect_err("a percent range above 100 must be rejected");
654    }
655
656    #[test]
657    fn light_ranges_quantize_without_leaving_the_advertised_grid() {
658        let range = LightValueRange::new(20, 250, 10, LightValueUnit::Lumens).expect("valid range");
659        assert_eq!(range.native_for_percent(0), Some(20));
660        assert_eq!(range.native_for_percent(50), Some(140));
661        assert_eq!(range.native_for_percent(100), Some(250));
662        assert_eq!(range.quantize(249), 250);
663        assert!(range.contains(range.native_for_percent(65).expect("mapped value")));
664    }
665
666    #[test]
667    fn invalid_light_ranges_fail_toml_deserialization() {
668        let result = toml::from_str::<LightValueRange>(
669            "min = 2700\nmax = 6500\nstep = 0\nunit = 'kelvin'\n",
670        );
671        result.expect_err("a zero step must not survive deserialization");
672    }
673}