Skip to main content

smart_keymap_core/key/
consumer.rs

1use core::fmt::Debug;
2use core::marker::PhantomData;
3use core::ops::Index;
4
5use serde::Deserialize;
6
7use crate::key;
8
9/// Reference for a consumer key.
10#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
11pub enum Ref {
12    /// A usage code without keyboard modifiers. (Value is the HID usage code).
13    UsageCode(u8),
14    /// Index into the key data array of [System] for a [Key] (usage + modifiers).
15    Key(u8),
16}
17
18/// A consumer key: HID usage code with optional keyboard modifiers.
19///
20/// A modifiers value of zero is equivalent to no modifiers.
21#[derive(Deserialize, Debug, Clone, Copy, PartialEq)]
22pub struct Key {
23    /// HID consumer usage code.
24    pub usage_code: u8,
25    /// Keyboard modifiers.
26    #[serde(default)]
27    pub modifiers: key::KeyboardModifiers,
28}
29
30impl Key {
31    /// Constructs a key with the given usage code and no modifiers.
32    pub const fn new(usage_code: u8) -> Self {
33        Self {
34            usage_code,
35            modifiers: key::KeyboardModifiers::new(),
36        }
37    }
38
39    /// Constructs a key with the given usage code and modifiers.
40    pub const fn new_with_modifiers(usage_code: u8, modifiers: key::KeyboardModifiers) -> Self {
41        Self {
42            usage_code,
43            modifiers,
44        }
45    }
46}
47
48/// Context for consumer keys. (No context).
49#[derive(Debug, Clone, Copy, PartialEq)]
50pub struct Context;
51
52impl Context {
53    /// No runtime state to clear.
54    pub fn reset(&mut self) {}
55}
56
57/// The event type for consumer keys. (No events).
58#[derive(Debug, Clone, Copy, PartialEq)]
59pub struct Event;
60
61/// The pending key state type for consumer keys. (No pending state).
62#[derive(Debug, Clone, Copy, PartialEq)]
63pub struct PendingKeyState;
64
65/// Key state used by [System].
66#[derive(Debug, Clone, Copy, PartialEq)]
67pub struct KeyState;
68
69/// The [key::System] implementation for consumer keys.
70#[derive(Debug, Clone, Copy, PartialEq)]
71pub struct System<R: Debug, Keys: Index<usize, Output = Key>> {
72    keys: Keys,
73    marker: PhantomData<R>,
74}
75
76impl<R: Debug, Keys: Index<usize, Output = Key>> System<R, Keys> {
77    /// Constructs a new [System] with the given key data.
78    ///
79    /// The key data is for consumer keys that include keyboard modifiers.
80    pub const fn new(keys: Keys) -> Self {
81        Self {
82            keys,
83            marker: PhantomData,
84        }
85    }
86}
87
88impl<R: Debug, Keys: Debug + Index<usize, Output = Key>> key::System<R> for System<R, Keys> {
89    type Ref = Ref;
90    type Context = Context;
91    type Event = Event;
92    type PendingKeyState = PendingKeyState;
93    type KeyState = KeyState;
94
95    fn new_pressed_key(
96        &self,
97        _keymap_index: u16,
98        _context: &Self::Context,
99        _key_ref: Ref,
100    ) -> (
101        key::PressedKeyResult<R, Self::PendingKeyState, Self::KeyState>,
102        key::KeyEvents<Self::Event>,
103    ) {
104        (
105            key::PressedKeyResult::Resolved(KeyState),
106            key::KeyEvents::no_events(),
107        )
108    }
109
110    fn update_pending_state(
111        &self,
112        _pending_state: &mut Self::PendingKeyState,
113        _keymap_index: u16,
114        _context: &Self::Context,
115        _key_ref: Ref,
116        _event: key::Event<Self::Event>,
117    ) -> (Option<key::NewPressedKey<R>>, key::KeyEvents<Self::Event>) {
118        panic!()
119    }
120
121    fn update_state(
122        &self,
123        _key_state: &mut Self::KeyState,
124        _ref: &Self::Ref,
125        _context: &Self::Context,
126        _keymap_index: u16,
127        _event: key::Event<Self::Event>,
128    ) -> key::KeyEvents<Self::Event> {
129        key::KeyEvents::no_events()
130    }
131
132    fn key_output(
133        &self,
134        key_ref: &Self::Ref,
135        _key_state: &Self::KeyState,
136    ) -> Option<key::KeyOutput> {
137        match key_ref {
138            Ref::UsageCode(uc) => Some(key::KeyOutput::from_consumer_code(*uc)),
139            Ref::Key(idx) => {
140                let Key {
141                    usage_code,
142                    modifiers,
143                } = self.keys[*idx as usize];
144                Some(key::KeyOutput::from_usage_with_modifiers(
145                    key::KeyUsage::Consumer(usage_code),
146                    modifiers,
147                ))
148            }
149        }
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    #[test]
158    fn test_sizeof_ref() {
159        assert_eq!(2, core::mem::size_of::<Ref>());
160    }
161
162    #[test]
163    fn test_sizeof_event() {
164        assert_eq!(0, core::mem::size_of::<Event>());
165    }
166}