Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mod] rename stack to queue #104

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/action.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! The different actions that can be done.

use crate::key_code::KeyCode;
use crate::layout::{StackedIter, WaitingAction};
use crate::layout::{QueuedIter, WaitingAction};
use core::fmt::Debug;

/// Behavior configuration of HoldTap.
Expand Down Expand Up @@ -30,7 +30,7 @@ pub enum HoldTapConfig {
/// supplied handler function.
///
/// The input to the custom handler will be an iterator that returns
/// [Stacked] [Events](Event). The order of the events matches the order the
/// [Queued] [Events](Event). The order of the events matches the order the
/// corresponding key was pressed/released, i.e. the first event is the
/// event first received after the HoldTap action key is pressed.
///
Expand All @@ -49,21 +49,21 @@ pub enum HoldTapConfig {
/// ```
/// use keyberon::action::{Action, HoldTapConfig, HoldTapAction};
/// use keyberon::key_code::KeyCode;
/// use keyberon::layout::{StackedIter, WaitingAction, Event};
/// use keyberon::layout::{QueuedIter, WaitingAction, Event};
///
/// /// Trigger a `Tap` action on the left side of the keyboard if another
/// /// key on the left side of the keyboard is pressed.
/// fn left_mod(stacked_iter: StackedIter) -> Option<WaitingAction> {
/// match stacked_iter.map(|s| s.event()).find(|e| e.is_press()) {
/// fn left_mod(queued_iter: QueuedIter) -> Option<WaitingAction> {
/// match queued_iter.map(|s| s.event()).find(|e| e.is_press()) {
/// Some(Event::Press(_, j)) if j < 6 => Some(WaitingAction::Tap),
/// _ => None,
/// }
/// }
///
/// /// Trigger a `Tap` action on the right side of the keyboard if another
/// /// key on the right side of the keyboard is pressed.
/// fn right_mod(stacked_iter: StackedIter) -> Option<WaitingAction> {
/// match stacked_iter.map(|s| s.event()).find(|e| e.is_press()) {
/// fn right_mod(queued_iter: QueuedIter) -> Option<WaitingAction> {
/// match queued_iter.map(|s| s.event()).find(|e| e.is_press()) {
/// Some(Event::Press(_, j)) if j > 5 => Some(WaitingAction::Tap),
/// _ => None,
/// }
Expand All @@ -89,7 +89,7 @@ pub enum HoldTapConfig {
/// tap_hold_interval: 0,
/// });
/// ```
Custom(fn(StackedIter) -> Option<WaitingAction>),
Custom(fn(QueuedIter) -> Option<WaitingAction>),
}

impl Debug for HoldTapConfig {
Expand All @@ -100,7 +100,7 @@ impl Debug for HoldTapConfig {
HoldTapConfig::PermissiveHold => f.write_str("PermissiveHold"),
HoldTapConfig::Custom(func) => f
.debug_tuple("Custom")
.field(&(*func as fn(StackedIter<'static>) -> Option<WaitingAction>) as &dyn Debug)
.field(&(*func as fn(QueuedIter<'static>) -> Option<WaitingAction>) as &dyn Debug)
.finish(),
}
}
Expand All @@ -113,7 +113,7 @@ impl PartialEq for HoldTapConfig {
| (HoldTapConfig::HoldOnOtherKeyPress, HoldTapConfig::HoldOnOtherKeyPress)
| (HoldTapConfig::PermissiveHold, HoldTapConfig::PermissiveHold) => true,
(HoldTapConfig::Custom(self_func), HoldTapConfig::Custom(other_func)) => {
*self_func as fn(StackedIter<'static>) -> Option<WaitingAction> == *other_func
*self_func as fn(QueuedIter<'static>) -> Option<WaitingAction> == *other_func
}
_ => false,
}
Expand Down
68 changes: 34 additions & 34 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ pub type Layers<
K = KeyCode,
> = [[[Action<T, K>; C]; R]; L];

/// The current event stack.
/// The current event queue.
///
/// Events can be retrieved by iterating over this struct and calling [Stacked::event].
type Stack = ArrayDeque<[Stacked; 16], arraydeque::behavior::Wrapping>;
/// Events can be retrieved by iterating over this struct and calling [Queued::event].
type Queue = ArrayDeque<[Queued; 16], arraydeque::behavior::Wrapping>;

/// The layout manager. It takes `Event`s and `tick`s as input, and
/// generate keyboard reports.
Expand All @@ -89,7 +89,7 @@ pub struct Layout<
default_layer: usize,
states: Vec<State<T, K>, 64>,
waiting: Option<WaitingState<T, K>>,
stacked: Stack,
queued: Queue,
tap_hold_tracker: TapHoldTracker,
}

Expand Down Expand Up @@ -240,33 +240,33 @@ pub enum WaitingAction {
}

impl<T, K> WaitingState<T, K> {
fn tick(&mut self, stacked: &Stack) -> Option<WaitingAction> {
fn tick(&mut self, queued: &Queue) -> Option<WaitingAction> {
self.timeout = self.timeout.saturating_sub(1);
match self.config {
HoldTapConfig::Default => (),
HoldTapConfig::HoldOnOtherKeyPress => {
if stacked.iter().any(|s| s.event.is_press()) {
if queued.iter().any(|s| s.event.is_press()) {
return Some(WaitingAction::Hold);
}
}
HoldTapConfig::PermissiveHold => {
for (x, s) in stacked.iter().enumerate() {
for (x, s) in queued.iter().enumerate() {
if s.event.is_press() {
let (i, j) = s.event.coord();
let target = Event::Release(i, j);
if stacked.iter().skip(x + 1).any(|s| s.event == target) {
if queued.iter().skip(x + 1).any(|s| s.event == target) {
return Some(WaitingAction::Hold);
}
}
}
}
HoldTapConfig::Custom(func) => {
if let waiting_action @ Some(_) = (func)(StackedIter(stacked.iter())) {
if let waiting_action @ Some(_) = (func)(QueuedIter(queued.iter())) {
return waiting_action;
}
}
}
if let Some(&Stacked { since, .. }) = stacked
if let Some(&Queued { since, .. }) = queued
.iter()
.find(|s| self.is_corresponding_release(&s.event))
{
Expand All @@ -286,13 +286,13 @@ impl<T, K> WaitingState<T, K> {
}
}

/// An iterator over the currently stacked events.
/// An iterator over the currently queued events.
///
/// Events can be retrieved by iterating over this struct and calling [Stacked::event].
pub struct StackedIter<'a>(arraydeque::Iter<'a, Stacked>);
/// Events can be retrieved by iterating over this struct and calling [Queued::event].
pub struct QueuedIter<'a>(arraydeque::Iter<'a, Queued>);

impl<'a> Iterator for StackedIter<'a> {
type Item = &'a Stacked;
impl<'a> Iterator for QueuedIter<'a> {
type Item = &'a Queued;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
Expand All @@ -301,18 +301,18 @@ impl<'a> Iterator for StackedIter<'a> {
}
}

/// An event, waiting in a stack to be processed.
/// An event, waiting in a queue to be processed.
#[derive(Debug)]
pub struct Stacked {
pub struct Queued {
event: Event,
since: u16,
}
impl From<Event> for Stacked {
impl From<Event> for Queued {
fn from(event: Event) -> Self {
Stacked { event, since: 0 }
Queued { event, since: 0 }
}
}
impl Stacked {
impl Queued {
fn tick(&mut self) {
self.since = self.since.saturating_add(1);
}
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<const C: usize, const R: usize, const L: usize, T: 'static, K: 'static + Co
default_layer: 0,
states: Vec::new(),
waiting: None,
stacked: ArrayDeque::new(),
queued: ArrayDeque::new(),
tap_hold_tracker: Default::default(),
}
}
Expand Down Expand Up @@ -388,24 +388,24 @@ impl<const C: usize, const R: usize, const L: usize, T: 'static, K: 'static + Co
/// custom actions thanks to the `Action::Custom` variant.
pub fn tick(&mut self) -> CustomEvent<T> {
self.states = self.states.iter().filter_map(State::tick).collect();
self.stacked.iter_mut().for_each(Stacked::tick);
self.queued.iter_mut().for_each(Queued::tick);
self.tap_hold_tracker.tick();
match &mut self.waiting {
Some(w) => match w.tick(&self.stacked) {
Some(w) => match w.tick(&self.queued) {
Some(WaitingAction::Hold) => self.waiting_into_hold(),
Some(WaitingAction::Tap) => self.waiting_into_tap(),
Some(WaitingAction::NoOp) => self.drop_waiting(),
None => CustomEvent::NoEvent,
},
None => match self.stacked.pop_front() {
Some(s) => self.unstack(s),
None => match self.queued.pop_front() {
Some(s) => self.resolve_queued_event(s),
None => CustomEvent::NoEvent,
},
}
}
fn unstack(&mut self, stacked: Stacked) -> CustomEvent<T> {
fn resolve_queued_event(&mut self, queued: Queued) -> CustomEvent<T> {
use Event::*;
match stacked.event {
match queued.event {
Release(i, j) => {
let mut custom = CustomEvent::NoEvent;
self.states = self
Expand All @@ -417,15 +417,15 @@ impl<const C: usize, const R: usize, const L: usize, T: 'static, K: 'static + Co
}
Press(i, j) => {
let action = self.press_as_action((i, j), self.current_layer());
self.do_action(action, (i, j), stacked.since)
self.do_action(action, (i, j), queued.since)
}
}
}
/// Register a key event.
pub fn event(&mut self, event: Event) {
if let Some(stacked) = self.stacked.push_back(event.into()) {
if let Some(queued) = self.queued.push_back(event.into()) {
self.waiting_into_hold();
self.unstack(stacked);
self.resolve_queued_event(queued);
}
}
fn press_as_action(&self, coord: (u8, u8), layer: usize) -> &'static Action<T, K> {
Expand Down Expand Up @@ -859,16 +859,16 @@ mod test {

#[test]
fn custom_handler() {
fn always_tap(_: StackedIter) -> Option<WaitingAction> {
fn always_tap(_: QueuedIter) -> Option<WaitingAction> {
Some(WaitingAction::Tap)
}
fn always_hold(_: StackedIter) -> Option<WaitingAction> {
fn always_hold(_: QueuedIter) -> Option<WaitingAction> {
Some(WaitingAction::Hold)
}
fn always_nop(_: StackedIter) -> Option<WaitingAction> {
fn always_nop(_: QueuedIter) -> Option<WaitingAction> {
Some(WaitingAction::NoOp)
}
fn always_none(_: StackedIter) -> Option<WaitingAction> {
fn always_none(_: QueuedIter) -> Option<WaitingAction> {
None
}
static LAYERS: Layers<4, 1, 1> = [[[
Expand Down