Skip to main content

smart_keymap_core/keymap/
observed_keymap.rs

1use core::fmt::Debug;
2use core::ops::Index;
3
4use crate::input;
5use crate::key;
6use crate::keymap;
7
8use keymap::Keymap;
9use keymap::ReportHints;
10use keymap::SetKeymapContext;
11
12/// Upper bound on ticks when draining scheduled events in tests.
13///
14/// Prevents integration tests from hanging indefinitely if a key keeps
15/// rescheduling work (e.g. an automation / string-macro loop).
16pub const MAX_TICKS_UNTIL_NO_SCHEDULED_EVENTS: usize = 10_000;
17
18/// Wrapper around a [crate::keymap::Keymap] that also tracks distinct HID reports.
19#[derive(Debug)]
20pub struct ObservedKeymap<I: Index<usize, Output = R>, R, Ctx, Ev: Debug, PKS, KS, S> {
21    keymap: Keymap<I, R, Ctx, Ev, PKS, KS, S>,
22    distinct_reports: keymap::DistinctReports,
23}
24
25impl<
26        I: Debug + Index<usize, Output = R>,
27        R: Copy + Debug,
28        Ctx: Debug + key::Context<Event = Ev> + SetKeymapContext + ReportHints,
29        Ev: Copy + Debug,
30        PKS: Debug,
31        KS: Copy + Debug + From<key::NoOpKeyState>,
32        S: key::System<R, Ref = R, Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS>,
33    > ObservedKeymap<I, R, Ctx, Ev, PKS, KS, S>
34{
35    /// Constructs an observed keymap with a new [keymap::DistinctReports].
36    pub fn new(keymap: Keymap<I, R, Ctx, Ev, PKS, KS, S>) -> Self {
37        ObservedKeymap {
38            keymap,
39            distinct_reports: keymap::DistinctReports::new(),
40        }
41    }
42
43    /// Proxies [keymap::Keymap::init] and clears observed reports.
44    pub fn init(&mut self) {
45        self.keymap.init();
46        self.distinct_reports = keymap::DistinctReports::new();
47    }
48
49    /// Proxies [keymap::Keymap::handle_input], `tick`'ing the keymap appropriately.
50    pub fn handle_input(&mut self, ev: input::Event) {
51        let ObservedKeymap {
52            keymap,
53            distinct_reports,
54        } = self;
55
56        keymap.handle_input(ev);
57        distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
58
59        // Clear input-queue pacing so a follow-up handle_input can process next.
60        keymap.tick();
61        distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
62    }
63
64    /// Proxies [keymap::Keymap::tick], updating reports appropriately.
65    pub fn tick(&mut self) {
66        let ObservedKeymap {
67            keymap,
68            distinct_reports,
69        } = self;
70
71        keymap.tick();
72        distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
73    }
74
75    /// Proxies [keymap::Keymap::boot_keyboard_report].
76    pub fn boot_keyboard_report(&self) -> [u8; 8] {
77        let ObservedKeymap { keymap, .. } = self;
78
79        keymap::KeymapOutput::new(keymap.pressed_keys()).as_hid_boot_keyboard_report()
80    }
81
82    /// Reference to distinct reports.
83    pub fn distinct_reports(&self) -> &keymap::DistinctReports {
84        &self.distinct_reports
85    }
86
87    /// Ticks the keymap until there are no scheduled events, updating reports appropriately.
88    ///
89    /// Panics if the keymap still has scheduled events after
90    /// `MAX_TICKS_UNTIL_NO_SCHEDULED_EVENTS` ticks (likely an infinite
91    /// reschedule loop).
92    pub fn tick_until_no_scheduled_events(&mut self) {
93        let ObservedKeymap {
94            keymap,
95            distinct_reports,
96        } = self;
97
98        for _ in 0..MAX_TICKS_UNTIL_NO_SCHEDULED_EVENTS {
99            if !keymap.has_scheduled_events() {
100                return;
101            }
102            keymap.tick();
103            distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
104        }
105        if !keymap.has_scheduled_events() {
106            return;
107        }
108        panic!(
109            "keymap did not become idle after {} ticks; possible infinite scheduled-event loop",
110            MAX_TICKS_UNTIL_NO_SCHEDULED_EVENTS
111        );
112    }
113}