Skip to content

Commit

Permalink
remove once_cell from layout struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Univa committed Mar 31, 2024
1 parent c1ec9ea commit 6145070
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
19 changes: 15 additions & 4 deletions rumcake-macros/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,10 +1125,21 @@ pub fn build_layout(raw: TokenStream, layers: LayoutLike<TokenTree>) -> TokenStr
fn get_layout(
) -> &'static ::rumcake::keyboard::Layout<{ Self::LAYOUT_COLS }, { Self::LAYOUT_ROWS }, { Self::LAYERS }> {
use ::rumcake::keyberon;
static KEYBOARD_LAYOUT: ::rumcake::keyboard::Layout<#col_count, #row_count, #layer_count> = ::rumcake::keyboard::Layout::new();
static mut LAYERS: ::rumcake::keyberon::layout::Layers<#col_count, #row_count, #layer_count, ::rumcake::keyboard::Keycode> = ::rumcake::keyberon::layout::layout! { #raw };
KEYBOARD_LAYOUT.init(unsafe { &mut LAYERS });
&KEYBOARD_LAYOUT
static KEYBOARD_LAYOUT: ::rumcake::once_cell::sync::OnceCell<
::rumcake::keyboard::Layout<#col_count, #row_count, #layer_count>,
> = ::rumcake::once_cell::sync::OnceCell::new();
const LAYERS: ::rumcake::keyberon::layout::Layers<#col_count, #row_count, #layer_count, ::rumcake::keyboard::Keycode> = ::rumcake::keyberon::layout::layout! { #raw };
KEYBOARD_LAYOUT.get_or_init(|| {
static mut LAYERS: ::rumcake::keyberon::layout::Layers<
#col_count,
#row_count,
#layer_count,
::rumcake::keyboard::Keycode,
> = ::rumcake::keyberon::layout::layout! { #raw };
::rumcake::keyboard::Layout::new(::rumcake::keyberon::layout::Layout::new(
unsafe { &mut LAYERS }
))
})
}
}
}
Expand Down
17 changes: 4 additions & 13 deletions rumcake/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,15 @@ pub trait KeyboardLayout {
/// A mutex-guaraded [`keyberon::layout::Layout`]. This also stores the original layout, so that it
/// can be reset to it's initial state if modifications are made to it.
pub struct Layout<const C: usize, const R: usize, const L: usize> {
layout: once_cell::sync::OnceCell<Mutex<RawMutex, KeyberonLayout<C, R, L, Keycode>>>,
pub(crate) layout: Mutex<RawMutex, KeyberonLayout<C, R, L, Keycode>>,
}

impl<const C: usize, const R: usize, const L: usize> Layout<C, R, L> {
pub const fn new() -> Self {
pub const fn new(layout: KeyberonLayout<C, R, L, Keycode>) -> Self {
Self {
layout: once_cell::sync::OnceCell::new(),
layout: Mutex::new(layout),
}
}

pub fn init(&self, layers: &'static mut Layers<C, R, L, Keycode>) {
self.layout
.get_or_init(|| Mutex::new(KeyberonLayout::new(layers)));
}

pub async fn lock(&self) -> MutexGuard<RawMutex, KeyberonLayout<C, R, L, Keycode>> {
self.layout.get().unwrap().lock().await
}
}

/// A trait that must be implemented for any device that needs to poll a switch matrix.
Expand Down Expand Up @@ -451,7 +442,7 @@ where

loop {
let keys = {
let mut layout = layout.lock().await;
let mut layout = layout.layout.lock().await;

if let Ok(event) = channel.try_receive() {
layout.event(event);
Expand Down
21 changes: 17 additions & 4 deletions rumcake/src/via/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub async fn dynamic_keymap_get_keycode<K: ViaKeyboard + 'static>(
|| col as usize >= <K::Layout as KeyboardLayout>::LAYOUT_COLS)
{
if let Some(action) = <K::Layout as KeyboardLayout>::get_layout()
.layout
.lock()
.await
.get_action((row, col), layer as usize)
Expand Down Expand Up @@ -206,7 +207,10 @@ pub async fn dynamic_keymap_set_keycode<K: ViaKeyboard + 'static>(
|| col as usize >= <K::Layout as KeyboardLayout>::LAYOUT_COLS)
{
{
let mut layout = <K::Layout as KeyboardLayout>::get_layout().lock().await;
let mut layout = <K::Layout as KeyboardLayout>::get_layout()
.layout
.lock()
.await;
if let Some(action) =
convert_keycode_to_action(u16::from_be_bytes(keycode.try_into().unwrap()))
{
Expand Down Expand Up @@ -307,7 +311,10 @@ pub async fn dynamic_keymap_get_buffer<K: ViaKeyboard + 'static>(
size as usize
};

let mut layout = <K::Layout as KeyboardLayout>::get_layout().lock().await;
let mut layout = <K::Layout as KeyboardLayout>::get_layout()
.layout
.lock()
.await;

// We make the assumption that Via will never request for a buffer that requires us to send
// part a 2-byte keycode (so a partial keycode). In other words, we assume that `offset` and
Expand Down Expand Up @@ -349,7 +356,10 @@ pub async fn dynamic_keymap_set_buffer<K: ViaKeyboard + 'static>(
};

{
let mut layout = <K::Layout as KeyboardLayout>::get_layout().lock().await;
let mut layout = <K::Layout as KeyboardLayout>::get_layout()
.layout
.lock()
.await;

// We make the assumption that VIA will never write a buffer that contains part a 2-byte
// keycode (so a partial keycode). In other words, we assume that `offset` and `size` will
Expand Down Expand Up @@ -391,7 +401,10 @@ where
[(); <K::Layout as KeyboardLayout>::LAYOUT_ROWS]:,
[(); <K::Layout as KeyboardLayout>::LAYOUT_COLS]:,
{
let mut layout = <K::Layout as KeyboardLayout>::get_layout().lock().await;
let mut layout = <K::Layout as KeyboardLayout>::get_layout()
.layout
.lock()
.await;
let original = <K::Layout as KeyboardLayout>::get_original_layout();

for (layer_idx, layer) in original.iter().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions rumcake/src/via/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub mod storage {
.await
{
// Load layout from flash
let mut layout = V::Layout::get_layout().lock().await;
let mut layout = V::Layout::get_layout().layout.lock().await;
for byte in (0..stored_len).step_by(2) {
if let Some(action) = super::protocol::keycodes::convert_keycode_to_action::<V>(
u16::from_be_bytes(stored_data[byte..byte + 2].try_into().unwrap()),
Expand All @@ -354,7 +354,7 @@ pub mod storage {
}
} else {
// Save default layout to flash
let mut layout = V::Layout::get_layout().lock().await;
let mut layout = V::Layout::get_layout().layout.lock().await;
let mut buf = [0; V::DYNAMIC_KEYMAP_LAYER_COUNT
* V::Layout::LAYOUT_COLS
* V::Layout::LAYOUT_ROWS
Expand Down

0 comments on commit 6145070

Please sign in to comment.