1use core::fmt::Debug;
2use core::marker::PhantomData;
3
4use serde::{Deserialize, Serialize};
5
6use crate::input;
7
8pub mod callback;
10pub mod caps_word;
12pub mod chorded;
14pub mod custom;
16pub mod keyboard;
18pub mod layered;
20pub mod sticky;
22pub mod tap_dance;
24pub mod tap_hold;
26
27pub mod composite;
29
30pub const MAX_KEY_EVENTS: usize = 4;
32
33pub const MAX_KEY_PATH_LEN: usize = 4;
35
36pub type KeyPath = heapless::Vec<u16, MAX_KEY_PATH_LEN>;
38
39#[derive(Debug, PartialEq, Eq)]
41pub struct KeyEvents<E, const M: usize = { MAX_KEY_EVENTS }>(heapless::Vec<ScheduledEvent<E>, M>);
42
43impl<E: Copy + Debug> KeyEvents<E> {
44 pub fn no_events() -> Self {
46 KeyEvents(None.into_iter().collect())
47 }
48
49 pub fn event(event: Event<E>) -> Self {
51 KeyEvents(Some(ScheduledEvent::immediate(event)).into_iter().collect())
52 }
53
54 pub fn scheduled_event(sch_event: ScheduledEvent<E>) -> Self {
56 KeyEvents(Some(sch_event).into_iter().collect())
57 }
58
59 pub fn schedule_event(&mut self, delay: u16, event: Event<E>) {
61 self.0.push(ScheduledEvent::after(delay, event)).unwrap();
62 }
63
64 pub fn extend(&mut self, other: KeyEvents<E>) {
66 other.0.into_iter().for_each(|ev| self.0.push(ev).unwrap());
67 }
68
69 pub fn add_event(&mut self, ev: ScheduledEvent<E>) {
71 self.0.push(ev).unwrap();
72 }
73
74 pub fn map_events<F>(&self, f: fn(E) -> F) -> KeyEvents<F> {
76 KeyEvents(
77 self.0
78 .as_slice()
79 .iter()
80 .map(|sch_ev| sch_ev.map_scheduled_event(f))
81 .collect(),
82 )
83 }
84
85 pub fn into_events<F>(&self) -> KeyEvents<F>
87 where
88 E: Into<F>,
89 {
90 KeyEvents(
91 self.0
92 .as_slice()
93 .iter()
94 .map(|sch_ev| sch_ev.map_scheduled_event(|ev| ev.into()))
95 .collect(),
96 )
97 }
98}
99
100impl<E: Debug, const M: usize> IntoIterator for KeyEvents<E, M> {
101 type Item = ScheduledEvent<E>;
102 type IntoIter = <heapless::Vec<ScheduledEvent<E>, M> as IntoIterator>::IntoIter;
103
104 fn into_iter(self) -> Self::IntoIter {
105 self.0.into_iter()
106 }
107}
108
109pub enum PressedKeyResult<PKS, KS> {
111 Pending(KeyPath, PKS),
113 Resolved(KS),
115}
116
117pub fn key_path(keymap_index: u16) -> KeyPath {
119 let mut key_path = KeyPath::new();
120 key_path.push(keymap_index).unwrap();
121 key_path
122}
123
124impl<PKS, KS> PressedKeyResult<PKS, KS> {
125 #[cfg(feature = "std")]
127 pub fn unwrap_resolved(self) -> KS {
128 match self {
129 PressedKeyResult::Resolved(r) => r,
130 _ => panic!("PressedKeyResult::unwrap_resolved: not Resolved"),
131 }
132 }
133
134 pub fn add_path_item(self, item: u16) -> Self {
136 match self {
137 PressedKeyResult::Pending(mut key_path, pks) => {
138 key_path.push(item).unwrap();
139 PressedKeyResult::Pending(key_path, pks)
140 }
141 pkr => pkr,
142 }
143 }
144}
145
146pub trait Key: Debug {
155 type Context: Copy;
160
161 type Event: Copy + Debug + PartialEq;
164
165 type PendingKeyState;
167
168 type KeyState;
170
171 fn new_pressed_key(
176 &self,
177 context: &Self::Context,
178 key_path: KeyPath,
179 ) -> (
180 PressedKeyResult<Self::PendingKeyState, Self::KeyState>,
181 KeyEvents<Self::Event>,
182 );
183
184 fn handle_event(
186 &self,
187 pending_state: &mut Self::PendingKeyState,
188 context: &Self::Context,
189 key_path: KeyPath,
190 event: Event<Self::Event>,
191 ) -> (
192 Option<PressedKeyResult<Self::PendingKeyState, Self::KeyState>>,
193 KeyEvents<Self::Event>,
194 );
195
196 fn lookup(
198 &self,
199 path: &[u16],
200 ) -> &dyn Key<
201 Context = Self::Context,
202 Event = Self::Event,
203 PendingKeyState = Self::PendingKeyState,
204 KeyState = Self::KeyState,
205 >;
206}
207
208pub trait Context: Clone + Copy {
213 type Event;
215
216 fn handle_event(&mut self, event: Event<Self::Event>) -> KeyEvents<Self::Event>;
218}
219
220#[derive(Deserialize, Serialize, Default, Clone, Copy, PartialEq, Eq)]
222pub struct KeyboardModifiers(u8);
223
224impl core::ops::Deref for KeyboardModifiers {
225 type Target = u8;
226
227 fn deref(&self) -> &Self::Target {
228 &self.0
229 }
230}
231
232impl core::fmt::Debug for KeyboardModifiers {
233 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
234 let mut ds = f.debug_struct("KeyboardModifiers");
235 if self.0 & Self::LEFT_CTRL_U8 != 0 {
236 ds.field("left_ctrl", &true);
237 }
238 if self.0 & Self::LEFT_SHIFT_U8 != 0 {
239 ds.field("left_shift", &true);
240 }
241 if self.0 & Self::LEFT_ALT_U8 != 0 {
242 ds.field("left_alt", &true);
243 }
244 if self.0 & Self::LEFT_GUI_U8 != 0 {
245 ds.field("left_gui", &true);
246 }
247 if self.0 & Self::RIGHT_CTRL_U8 != 0 {
248 ds.field("right_ctrl", &true);
249 }
250 if self.0 & Self::RIGHT_SHIFT_U8 != 0 {
251 ds.field("right_shift", &true);
252 }
253 if self.0 & Self::RIGHT_ALT_U8 != 0 {
254 ds.field("right_alt", &true);
255 }
256 if self.0 & Self::RIGHT_GUI_U8 != 0 {
257 ds.field("right_gui", &true);
258 }
259 ds.finish_non_exhaustive()
260 }
261}
262
263impl KeyboardModifiers {
264 pub const LEFT_CTRL_U8: u8 = 0x01;
266 pub const LEFT_SHIFT_U8: u8 = 0x02;
268 pub const LEFT_ALT_U8: u8 = 0x04;
270 pub const LEFT_GUI_U8: u8 = 0x08;
272 pub const RIGHT_CTRL_U8: u8 = 0x10;
274 pub const RIGHT_SHIFT_U8: u8 = 0x20;
276 pub const RIGHT_ALT_U8: u8 = 0x40;
278 pub const RIGHT_GUI_U8: u8 = 0x80;
280
281 pub const fn new() -> Self {
283 KeyboardModifiers(0x00)
284 }
285
286 pub const fn from_byte(b: u8) -> Self {
288 KeyboardModifiers(b)
289 }
290
291 pub const fn from_key_code(key_code: u8) -> Option<Self> {
295 match key_code {
296 0xE0 => Some(Self::LEFT_CTRL),
297 0xE1 => Some(Self::LEFT_SHIFT),
298 0xE2 => Some(Self::LEFT_ALT),
299 0xE3 => Some(Self::LEFT_GUI),
300 0xE4 => Some(Self::RIGHT_CTRL),
301 0xE5 => Some(Self::RIGHT_SHIFT),
302 0xE6 => Some(Self::RIGHT_ALT),
303 0xE7 => Some(Self::RIGHT_GUI),
304 _ => None,
305 }
306 }
307
308 pub const NONE: KeyboardModifiers = KeyboardModifiers {
310 ..KeyboardModifiers::new()
311 };
312
313 pub const LEFT_CTRL: KeyboardModifiers = KeyboardModifiers(Self::LEFT_CTRL_U8);
315
316 pub const LEFT_SHIFT: KeyboardModifiers = KeyboardModifiers(Self::LEFT_SHIFT_U8);
318
319 pub const LEFT_ALT: KeyboardModifiers = KeyboardModifiers(Self::LEFT_ALT_U8);
321
322 pub const LEFT_GUI: KeyboardModifiers = KeyboardModifiers(Self::LEFT_GUI_U8);
324
325 pub const RIGHT_CTRL: KeyboardModifiers = KeyboardModifiers(Self::RIGHT_CTRL_U8);
327
328 pub const RIGHT_SHIFT: KeyboardModifiers = KeyboardModifiers(Self::RIGHT_SHIFT_U8);
330
331 pub const RIGHT_ALT: KeyboardModifiers = KeyboardModifiers(Self::RIGHT_ALT_U8);
333
334 pub const RIGHT_GUI: KeyboardModifiers = KeyboardModifiers(Self::RIGHT_GUI_U8);
336
337 pub const fn is_modifier_key_code(key_code: u8) -> bool {
339 matches!(key_code, 0xE0..=0xE7)
340 }
341
342 pub fn as_key_codes(&self) -> heapless::Vec<u8, 8> {
344 let mut key_codes = heapless::Vec::new();
345
346 if self.0 & Self::LEFT_CTRL_U8 != 0 {
347 key_codes.push(0xE0).unwrap();
348 }
349 if self.0 & Self::LEFT_SHIFT_U8 != 0 {
350 key_codes.push(0xE1).unwrap();
351 }
352 if self.0 & Self::LEFT_ALT_U8 != 0 {
353 key_codes.push(0xE2).unwrap();
354 }
355 if self.0 & Self::LEFT_GUI_U8 != 0 {
356 key_codes.push(0xE3).unwrap();
357 }
358 if self.0 & Self::RIGHT_CTRL_U8 != 0 {
359 key_codes.push(0xE4).unwrap();
360 }
361 if self.0 & Self::RIGHT_SHIFT_U8 != 0 {
362 key_codes.push(0xE5).unwrap();
363 }
364 if self.0 & Self::RIGHT_ALT_U8 != 0 {
365 key_codes.push(0xE6).unwrap();
366 }
367 if self.0 & Self::RIGHT_GUI_U8 != 0 {
368 key_codes.push(0xE7).unwrap();
369 }
370
371 key_codes
372 }
373
374 pub fn as_byte(&self) -> u8 {
376 self.as_key_codes()
377 .iter()
378 .fold(0u8, |acc, &kc| acc | (1 << (kc - 0xE0)))
379 }
380
381 pub const fn union(&self, other: &KeyboardModifiers) -> KeyboardModifiers {
383 KeyboardModifiers(self.0 | other.0)
384 }
385
386 pub const fn has_modifiers(&self, other: &KeyboardModifiers) -> bool {
388 self.0 & other.0 != 0
389 }
390}
391
392#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq)]
394pub enum KeyUsage {
395 Keyboard(u8),
397 Custom(u8),
399}
400
401#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq)]
403pub struct KeyOutput {
404 key_code: KeyUsage,
405 key_modifiers: KeyboardModifiers,
406}
407
408impl KeyOutput {
409 pub fn from_key_code(key_code: u8) -> Self {
411 if let Some(key_modifiers) = KeyboardModifiers::from_key_code(key_code) {
412 KeyOutput {
413 key_code: KeyUsage::Keyboard(0x00),
414 key_modifiers,
415 }
416 } else {
417 KeyOutput {
418 key_code: KeyUsage::Keyboard(key_code),
419 key_modifiers: KeyboardModifiers::new(),
420 }
421 }
422 }
423
424 pub fn from_key_code_with_modifiers(key_code: u8, key_modifiers: KeyboardModifiers) -> Self {
426 let KeyOutput {
427 key_code,
428 key_modifiers: km,
429 } = Self::from_key_code(key_code);
430 KeyOutput {
431 key_code,
432 key_modifiers: km.union(&key_modifiers),
433 }
434 }
435
436 pub fn from_key_modifiers(key_modifiers: KeyboardModifiers) -> Self {
438 KeyOutput {
439 key_code: KeyUsage::Keyboard(0x00),
440 key_modifiers,
441 }
442 }
443
444 pub fn from_custom_code(custom_code: u8) -> Self {
446 KeyOutput {
447 key_code: KeyUsage::Custom(custom_code),
448 key_modifiers: KeyboardModifiers::new(),
449 }
450 }
451
452 pub fn key_code(&self) -> KeyUsage {
454 self.key_code
455 }
456
457 pub fn key_modifiers(&self) -> KeyboardModifiers {
459 self.key_modifiers
460 }
461}
462
463pub trait KeyState: Debug {
465 type Context;
467 type Event: Copy + Debug;
469
470 fn handle_event(
472 &mut self,
473 _context: &Self::Context,
474 _keymap_index: u16,
475 _event: Event<Self::Event>,
476 ) -> KeyEvents<Self::Event> {
477 KeyEvents::no_events()
478 }
479
480 fn key_output(&self) -> Option<KeyOutput> {
482 None
483 }
484}
485
486#[derive(Debug, Clone, Copy, PartialEq, Eq)]
488pub struct NoOpKeyState<Ctx, Ev>(PhantomData<(Ctx, Ev)>);
489
490impl<Ctx, Ev> NoOpKeyState<Ctx, Ev> {
491 pub const fn new() -> Self {
493 NoOpKeyState(PhantomData)
494 }
495}
496
497impl<Ctx: Debug, Ev: Copy + Debug> KeyState for NoOpKeyState<Ctx, Ev> {
498 type Context = Ctx;
499 type Event = Ev;
500}
501
502#[allow(unused)]
504pub enum EventError {
505 UnmappableEvent,
509}
510
511type EventResult<T> = Result<T, EventError>;
513
514#[derive(Debug, Clone, Copy, PartialEq, Eq)]
519pub enum Event<T> {
520 Input(input::Event),
522 Key {
524 keymap_index: u16,
526 key_event: T,
528 },
529 Keymap(crate::keymap::KeymapEvent),
531}
532
533impl<T: Copy> Event<T> {
534 pub fn key_event(keymap_index: u16, key_event: T) -> Self {
536 Event::Key {
537 keymap_index,
538 key_event,
539 }
540 }
541
542 pub fn map_key_event<U>(self, f: fn(T) -> U) -> Event<U> {
544 match self {
545 Event::Input(event) => Event::Input(event),
546 Event::Key {
547 key_event,
548 keymap_index,
549 } => Event::Key {
550 key_event: f(key_event),
551 keymap_index,
552 },
553 Event::Keymap(cb) => Event::Keymap(cb),
554 }
555 }
556
557 pub fn into_key_event<U>(self) -> Event<U>
559 where
560 T: Into<U>,
561 {
562 self.map_key_event(|ke| ke.into())
563 }
564
565 pub fn try_into_key_event<U, E>(self, f: fn(T) -> Result<U, E>) -> EventResult<Event<U>> {
567 match self {
568 Event::Input(event) => Ok(Event::Input(event)),
569 Event::Key {
570 key_event,
571 keymap_index,
572 } => f(key_event)
573 .map(|key_event| Event::Key {
574 key_event,
575 keymap_index,
576 })
577 .map_err(|_| EventError::UnmappableEvent),
578 Event::Keymap(cb) => Ok(Event::Keymap(cb)),
579 }
580 }
581}
582
583impl<T> From<input::Event> for Event<T> {
584 fn from(event: input::Event) -> Self {
585 Event::Input(event)
586 }
587}
588
589#[allow(unused)]
591#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
592pub enum Schedule {
593 Immediate,
595 After(u16),
597}
598
599#[derive(Debug, Clone, Copy, PartialEq, Eq)]
601pub struct ScheduledEvent<T> {
602 pub schedule: Schedule,
604 pub event: Event<T>,
606}
607
608impl<T: Copy> ScheduledEvent<T> {
609 #[allow(unused)]
611 pub fn immediate(event: Event<T>) -> Self {
612 ScheduledEvent {
613 schedule: Schedule::Immediate,
614 event,
615 }
616 }
617
618 pub fn after(delay: u16, event: Event<T>) -> Self {
620 ScheduledEvent {
621 schedule: Schedule::After(delay),
622 event,
623 }
624 }
625
626 pub fn map_scheduled_event<U>(self, f: fn(T) -> U) -> ScheduledEvent<U> {
628 ScheduledEvent {
629 event: self.event.map_key_event(f),
630 schedule: self.schedule,
631 }
632 }
633
634 pub fn into_scheduled_event<U>(self) -> ScheduledEvent<U>
636 where
637 T: Into<U>,
638 {
639 self.map_scheduled_event(|e| e.into())
640 }
641}