-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!(rpt-any): repeat one-shotted chord (#641)
Implements #596. This commit adds unsafe code to deal with lifetime/reference shenanigans in keyberon.
- Loading branch information
Showing
3 changed files
with
152 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
pub mod action; | ||
pub mod key_code; | ||
pub mod layout; | ||
mod multikey_buffer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//! Module for `MultiKeyBuffer`. | ||
use std::ptr::null_mut; | ||
use std::{array, slice}; | ||
|
||
use crate::action::{Action, ONE_SHOT_MAX_ACTIVE}; | ||
use crate::key_code::KeyCode; | ||
|
||
// Presumably this should be plenty. | ||
// ONE_SHOT_MAX_ACTIVE is already likely unreasonably large enough. | ||
// This buffer capacity adds more onto that, | ||
// just in case somebody finds a way to use all of the one-shot capacity. | ||
const BUFCAP: usize = ONE_SHOT_MAX_ACTIVE + 4; | ||
|
||
/// This is an unsafe container that enables a mutable Action::MultipleKeyCodes. | ||
pub(crate) struct MultiKeyBuffer<'a, T> { | ||
buf: [KeyCode; BUFCAP], | ||
size: usize, | ||
ptr: *mut &'static [KeyCode], | ||
ac: *mut Action<'a, T>, | ||
} | ||
|
||
unsafe impl<'a, T> Send for MultiKeyBuffer<'a, T> {} | ||
|
||
impl<'a, T> MultiKeyBuffer<'a, T> { | ||
/// Create a new instance of `MultiKeyBuffer`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The program should not have any references to the inner buffer when the struct is dropped. | ||
pub(crate) unsafe fn new() -> Self { | ||
Self { | ||
buf: array::from_fn(|_| KeyCode::Escape), | ||
size: 0, | ||
ptr: Box::leak(Box::new(slice::from_raw_parts(null_mut(), 0))), | ||
ac: Box::leak(Box::new(Action::NoOp)), | ||
} | ||
} | ||
|
||
/// Set the current size of the buffer to zero. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The program should not have any references to the inner buffer. | ||
pub(crate) unsafe fn clear(&mut self) { | ||
self.size = 0; | ||
} | ||
|
||
/// Push to the end of the buffer. If the buffer is full, this silently fails. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The program should not have any references to the inner buffer. | ||
pub(crate) unsafe fn push(&mut self, kc: KeyCode) { | ||
if self.size < BUFCAP { | ||
self.buf[self.size] = kc; | ||
self.size += 1; | ||
} | ||
} | ||
|
||
/// Get a reference to the inner buffer in the form of an `Action`. | ||
/// The `Action` will be the variant `MultipleKeyCodes`, | ||
/// containing all keys that have been pushed. | ||
/// | ||
/// # Safety | ||
/// | ||
/// The program should not have any references to the inner buffer before calling. | ||
/// The program should not mutate the buffer after calling this function until after the returned reference is dropped. | ||
pub(crate) unsafe fn get_ref(&self) -> &'a Action<'a, T> { | ||
*self.ac = Action::NoOp; | ||
*self.ptr = slice::from_raw_parts(self.buf.as_ptr(), self.size); | ||
*self.ac = Action::MultipleKeyCodes(&*self.ptr); | ||
&*self.ac | ||
} | ||
} | ||
|
||
impl<'a, T> Drop for MultiKeyBuffer<'a, T> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
drop(Box::from_raw(self.ac)); | ||
drop(Box::from_raw(self.ptr)); | ||
} | ||
} | ||
} |