Skip to content

Commit 7df7519

Browse files
committed
Modified per TeXitoi's instructions (as best I understood them) and also added a new Delay() feature that lets you put arbitrary delays between sequence (key macro) events.
1 parent b56de4f commit 7df7519

File tree

2 files changed

+85
-84
lines changed

2 files changed

+85
-84
lines changed

src/action.rs

+22-35
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,31 @@
33
use crate::key_code::KeyCode;
44

55
/// The different types of actions we support for key macros
6-
#[non_exhaustive]
6+
#[non_exhaustive] // Definitely NOT exhaustive! Let's add more! Mouse events maybe? :)
77
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
88
pub enum SequenceEvent {
99
/// A keypress/keydown
1010
Press(KeyCode),
1111
/// Key release/keyup
1212
Release(KeyCode),
13-
/// Combination quick keydown followed by keyrelease
14-
Tap(KeyCode),
15-
/// Release all (currently) pressed keys
16-
ReleaseAll(),
13+
/// For sequences that need to wait a bit before continuing
14+
Delay {
15+
/// A delay (in ticks) to wait before executing the next SequenceEvent
16+
since: u32,
17+
/// Number of ticks to wait before removing the Delay
18+
ticks: u32,
19+
},
20+
}
21+
22+
impl SequenceEvent {
23+
/// Returns the keycode associated with the given Press/Release event
24+
pub fn keycode(&self) -> Option<KeyCode> {
25+
match *self {
26+
SequenceEvent::Press(keycode) => Some(keycode),
27+
SequenceEvent::Release(keycode) => Some(keycode),
28+
_ => None,
29+
}
30+
}
1731
}
1832

1933
/// The different actions that can be done.
@@ -55,12 +69,10 @@ pub enum Action {
5569
/// The tap action.
5670
tap: &'static Action,
5771
},
58-
/// A sequence of KeyEvents
72+
/// A sequence of SequenceEvents
5973
Sequence {
60-
/// How long to delay between events
61-
delay: u16, // NOTE: Currently unused
62-
/// The sequence of KeyEvents that will be triggered
63-
actions: &'static [SequenceEvent],
74+
/// An array of SequenceEvents that will be triggered (in order)
75+
events: &'static [SequenceEvent],
6476
},
6577
}
6678
impl Action {
@@ -76,7 +88,6 @@ impl Action {
7688
match self {
7789
Action::KeyCode(kc) => core::slice::from_ref(kc).iter().cloned(),
7890
Action::MultipleKeyCodes(kcs) => kcs.iter().cloned(),
79-
// Action::Sequence { delay, actions } => actions.iter().cloned(), // TODO (maybe unnecessary)
8091
_ => [].iter().cloned(),
8192
}
8293
}
@@ -106,27 +117,3 @@ pub const fn m(kcs: &'static [KeyCode]) -> Action {
106117
Action::MultipleKeyCodes(kcs)
107118
}
108119

109-
/// A shortcut to create `KeyEvent::Tap`, useful to create compact
110-
/// layout.
111-
pub const fn tap(kc: KeyCode) -> SequenceEvent {
112-
SequenceEvent::Tap(kc)
113-
}
114-
115-
/// A shortcut to create `KeyEvent::Press`, useful to create compact
116-
/// layout.
117-
pub const fn kp(kc: KeyCode) -> SequenceEvent {
118-
SequenceEvent::Press(kc)
119-
}
120-
121-
/// A shortcut to create `KeyEvent::Release`, useful to create compact
122-
/// layout.
123-
pub const fn kr(kc: KeyCode) -> SequenceEvent {
124-
SequenceEvent::Release(kc)
125-
}
126-
127-
// NOTE: This doesn't work yet:
128-
/// A shortcut to create `KeyEvent::Release`, useful to create compact
129-
/// layout.
130-
pub const fn ra() -> SequenceEvent {
131-
SequenceEvent::ReleaseAll()
132-
}

src/layout.rs

+63-49
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pub struct Layout {
2323
states: Vec<State, U64>,
2424
waiting: Option<WaitingState>,
2525
stacked: ArrayDeque<[Stacked; 16], arraydeque::behavior::Wrapping>,
26+
sequenced: ArrayDeque<[SequenceEvent; 32], arraydeque::behavior::Wrapping>,
27+
// Riskable NOTE: Wish we didn't have to preallocate sequenced like this.
28+
// I want to be able to have my keyboard type long sentences/quotes!
2629
}
2730

2831
/// An event on the key matrix.
@@ -32,18 +35,13 @@ pub enum Event {
3235
Press(u8, u8),
3336
/// Release event with coordinates (i, j).
3437
Release(u8, u8),
35-
/// Press event with just a keycode (for sequences)
36-
SequencePress(KeyCode),
37-
/// Release event with just a keycode (for sequences)
38-
SequenceRelease(KeyCode),
3938
}
4039
impl Event {
4140
/// Returns the coordinates (i, j) of the event.
4241
pub fn coord(self) -> (u8, u8) {
4342
match self {
4443
Event::Press(i, j) => (i, j),
4544
Event::Release(i, j) => (i, j),
46-
_ => (0, 0), // Need a NaN version of this or something
4745
}
4846
}
4947

@@ -68,28 +66,38 @@ impl Event {
6866
let (i, j) = f(i, j);
6967
Event::Release(i, j)
7068
}
71-
// Not really sure what (if anything) this needs to be. Just returning as-is
72-
Event::SequencePress(k) => {
73-
Event::SequencePress(k)
74-
}
75-
Event::SequenceRelease(k) => {
76-
Event::SequenceRelease(k)
77-
}
7869
}
7970
}
8071
}
8172

73+
/// The various states used internally by Keyberon
8274
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
8375
enum State {
84-
NormalKey { keycode: KeyCode, coord: (u8, u8) },
85-
LayerModifier { value: usize, coord: (u8, u8) },
86-
SequenceKey { keycode: KeyCode },
76+
/// Normal keys
77+
NormalKey {
78+
/// Keycode
79+
keycode: KeyCode,
80+
/// Coordinates in the matrix
81+
coord: (u8, u8),
82+
},
83+
/// Layer modifier keys
84+
LayerModifier {
85+
/// Value
86+
value: usize,
87+
/// Coordinates of this layer modifier key in the matrix
88+
coord: (u8, u8),
89+
},
90+
/// Fake key event for sequences
91+
FakeKey {
92+
/// The key to everything!
93+
keycode: KeyCode,
94+
},
8795
}
8896
impl State {
8997
fn keycode(&self) -> Option<KeyCode> {
9098
match self {
9199
NormalKey { keycode, .. } => Some(*keycode),
92-
SequenceKey { keycode } => Some(*keycode),
100+
FakeKey { keycode } => Some(*keycode),
93101
_ => None,
94102
}
95103
}
@@ -104,9 +112,9 @@ impl State {
104112
_ => Some(*self),
105113
}
106114
}
107-
fn seq_release(&self, k: KeyCode) -> Option<Self> {
115+
fn seq_release(&self, kc: KeyCode) -> Option<Self> {
108116
match *self {
109-
SequenceKey { keycode, .. } if keycode == k => None,
117+
FakeKey { keycode, .. } if keycode == kc => None,
110118
_ => Some(*self),
111119
}
112120
}
@@ -163,6 +171,7 @@ impl Layout {
163171
states: Vec::new(),
164172
waiting: None,
165173
stacked: ArrayDeque::new(),
174+
sequenced: ArrayDeque::new(),
166175
}
167176
}
168177
/// Iterates on the key codes of the current state.
@@ -205,6 +214,32 @@ impl Layout {
205214
}
206215
}
207216
}
217+
// Process sequences
218+
if let Some(event) = self.sequenced.pop_front() {
219+
match event {
220+
SequenceEvent::Press(keycode) => {
221+
// Start tracking this fake key Press() event
222+
let _ = self.states.push(FakeKey { keycode: keycode });
223+
}
224+
SequenceEvent::Release(keycode) => {
225+
// Clear out the Press() matching this Release's keycode
226+
self.states = self
227+
.states
228+
.iter()
229+
.filter_map(|s| s.seq_release(keycode))
230+
.collect()
231+
}
232+
SequenceEvent::Delay { since, ticks } => {
233+
if since < ticks {
234+
// Increment and put it back
235+
self.sequenced.push_front(SequenceEvent::Delay {
236+
since: since.saturating_add(1),
237+
ticks: ticks,
238+
});
239+
}
240+
}
241+
}
242+
}
208243
self.keycodes()
209244
}
210245
fn unstack(&mut self, stacked: Stacked) {
@@ -221,16 +256,6 @@ impl Layout {
221256
let action = self.press_as_action((i, j), self.current_layer());
222257
self.do_action(action, (i, j), stacked.since);
223258
}
224-
SequenceRelease(k) => {
225-
self.states = self
226-
.states
227-
.iter()
228-
.filter_map(|s| s.seq_release(k))
229-
.collect()
230-
}
231-
SequencePress(k) => {
232-
let _ = self.states.push(SequenceKey { keycode: k });
233-
}
234259
}
235260
}
236261
/// A key event.
@@ -308,32 +333,21 @@ impl Layout {
308333
self.do_action(action, coord, delay);
309334
}
310335
}
311-
Sequence { delay, actions } => {
312-
for key_event in actions {
336+
Sequence { events } => {
337+
// Copy the contents of the sequence events into the sequenced ArrayDeque
338+
for key_event in events {
313339
match *key_event {
314340
SequenceEvent::Press(keycode) => {
315-
self.stacked.push_back(Event::SequencePress(keycode).into());
341+
self.sequenced.push_back(SequenceEvent::Press(keycode));
316342
}
317343
SequenceEvent::Release(keycode) => {
318-
self.stacked.push_back(Event::SequenceRelease(keycode).into());
319-
}
320-
SequenceEvent::Tap(keycode) => {
321-
self.stacked.push_back(Event::SequencePress(keycode).into());
322-
self.stacked.push_back(Event::SequenceRelease(keycode).into());
344+
self.sequenced.push_back(SequenceEvent::Release(keycode));
323345
}
324-
SequenceEvent::ReleaseAll() => {
325-
// Can't figure out a good way to handle iterating over self.stacked
326-
// ...without running into borrow checker problems.
327-
// I basically don't know what I'm doing (yet) hehe
328-
// TODO:
329-
// for s in self.stacked.into_iter().collect() {
330-
// match s.event {
331-
// Event::SequencePress(keycode) => {
332-
// self.stacked.push_back(Event::SequenceRelease(keycode.clone()).into());
333-
// }
334-
// _ => { () }
335-
// }
336-
// }
346+
SequenceEvent::Delay { since, ticks } => {
347+
self.sequenced.push_back(SequenceEvent::Delay {
348+
since: since,
349+
ticks: ticks,
350+
});
337351
}
338352
}
339353
}

0 commit comments

Comments
 (0)