smart_keymap/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::SetKeymapContext;
10
11/// Wrapper around a [crate::keymap::Keymap] that also tracks distinct HID reports.
12#[derive(Debug)]
13pub struct ObservedKeymap<I: Index<usize, Output = R>, R, Ctx, Ev: Debug, PKS, KS, S> {
14    keymap: Keymap<I, R, Ctx, Ev, PKS, KS, S>,
15    distinct_reports: keymap::DistinctReports,
16}
17
18impl<
19        I: Debug + Index<usize, Output = R>,
20        R: Copy + Debug,
21        Ctx: Debug + key::Context<Event = Ev> + SetKeymapContext,
22        Ev: Copy + Debug,
23        PKS: Debug,
24        KS: Copy + Debug + From<key::NoOpKeyState>,
25        S: key::System<R, Ref = R, Context = Ctx, Event = Ev, PendingKeyState = PKS, KeyState = KS>,
26    > ObservedKeymap<I, R, Ctx, Ev, PKS, KS, S>
27{
28    /// Constructs an observed keymap with a new [keymap::DistinctReports].
29    pub fn new(keymap: Keymap<I, R, Ctx, Ev, PKS, KS, S>) -> Self {
30        ObservedKeymap {
31            keymap,
32            distinct_reports: keymap::DistinctReports::new(),
33        }
34    }
35
36    /// Proxies [keymap::Keymap::handle_input], `tick`'ing the keymap appropriately.
37    pub fn handle_input(&mut self, ev: input::Event) {
38        let ObservedKeymap {
39            keymap,
40            distinct_reports,
41        } = self;
42
43        keymap.handle_input(ev);
44        distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
45
46        for _ in 0..keymap::INPUT_QUEUE_TICK_DELAY {
47            keymap.tick();
48            distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
49        }
50    }
51
52    /// Proxies [keymap::Keymap::tick], updating reports appropriately.
53    pub fn tick(&mut self) {
54        let ObservedKeymap {
55            keymap,
56            distinct_reports,
57        } = self;
58
59        keymap.tick();
60        distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
61    }
62
63    /// Proxies [keymap::Keymap::boot_keyboard_report].
64    pub fn boot_keyboard_report(&self) -> [u8; 8] {
65        let ObservedKeymap { keymap, .. } = self;
66
67        keymap::KeymapOutput::new(keymap.pressed_keys()).as_hid_boot_keyboard_report()
68    }
69
70    /// Reference to distinct reports.
71    pub fn distinct_reports(&self) -> &keymap::DistinctReports {
72        &self.distinct_reports
73    }
74
75    /// Ticks the keymap until there are no scheduled events, updating reports appropriately.
76    pub fn tick_until_no_scheduled_events(&mut self) {
77        let ObservedKeymap {
78            keymap,
79            distinct_reports,
80        } = self;
81
82        while keymap.has_scheduled_events() {
83            keymap.tick();
84            distinct_reports.update(keymap.report_output().as_hid_boot_keyboard_report());
85        }
86    }
87}