kanata_parser/
layers.rs

1use kanata_keyberon::key_code::KeyCode;
2use kanata_keyberon::layout::*;
3
4use crate::cfg::KanataAction;
5use crate::cfg::alloc::*;
6use crate::custom_action::*;
7use crate::keys::OsCode;
8
9use std::sync::Arc;
10
11// OsCode::KEY_MAX is the biggest OsCode
12pub const KEYS_IN_ROW: usize = OsCode::KEY_MAX as usize;
13pub const LAYER_ROWS: usize = 2;
14pub const DEFAULT_ACTION: KanataAction = KanataAction::KeyCode(KeyCode::ErrorUndefined);
15
16pub type IntermediateLayers = Box<[[Row; LAYER_ROWS]]>;
17
18pub type KLayers =
19    Layers<'static, KEYS_IN_ROW, LAYER_ROWS, &'static &'static [&'static CustomAction]>;
20
21pub struct KanataLayers {
22    pub(crate) layers:
23        Layers<'static, KEYS_IN_ROW, LAYER_ROWS, &'static &'static [&'static CustomAction]>,
24    _allocations: Arc<Allocations>,
25}
26
27impl std::fmt::Debug for KanataLayers {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        f.debug_struct("KanataLayers").finish()
30    }
31}
32
33pub type Row = [kanata_keyberon::action::Action<'static, &'static &'static [&'static CustomAction]>;
34    KEYS_IN_ROW];
35
36pub fn new_layers(layers: usize) -> IntermediateLayers {
37    let actual_num_layers = layers;
38    // Note: why construct it like this?
39    // Because don't want to construct KanataLayers on the stack.
40    // The stack will overflow because of lack of placement new.
41    let mut layers = Vec::with_capacity(actual_num_layers);
42    for _ in 0..actual_num_layers {
43        layers.push([[DEFAULT_ACTION; KEYS_IN_ROW], [DEFAULT_ACTION; KEYS_IN_ROW]]);
44    }
45    layers.into_boxed_slice()
46}
47
48impl KanataLayers {
49    /// # Safety
50    ///
51    /// The allocations must hold all of the &'static pointers found in layers.
52    pub(crate) unsafe fn new(layers: KLayers, allocations: Arc<Allocations>) -> Self {
53        Self {
54            layers,
55            _allocations: allocations,
56        }
57    }
58
59    pub(crate) fn get(&self) -> (KLayers, Arc<Allocations>) {
60        (self.layers, self._allocations.clone())
61    }
62}