forked from jtroo/kanata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefhands.rs
More file actions
414 lines (389 loc) · 14.2 KB
/
defhands.rs
File metadata and controls
414 lines (389 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use super::sexpr::*;
use super::*;
use crate::{anyhow_expr, bail, bail_expr};
pub(super) fn parse_defhands(expr: &[SExpr], s: &ParserState) -> Result<custom_tap_hold::HandMap> {
use custom_tap_hold::Hand;
let exprs_iter = check_first_expr(expr.iter(), "defhands")?;
let mut keys: Vec<u16> = Vec::new();
let mut hands: Vec<Hand> = Vec::new();
let mut seen_left = false;
let mut seen_right = false;
for group_expr in exprs_iter {
let group = group_expr
.list(s.vars())
.ok_or_else(|| anyhow_expr!(group_expr, "expected (left ...) or (right ...)"))?;
if group.is_empty() {
bail_expr!(group_expr, "expected (left ...) or (right ...)");
}
let hand_name = group[0]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(&group[0], "expected 'left' or 'right'"))?;
let hand = match hand_name {
"left" => {
if seen_left {
bail_expr!(&group[0], "duplicate 'left' group in defhands");
}
seen_left = true;
Hand::Left
}
"right" => {
if seen_right {
bail_expr!(&group[0], "duplicate 'right' group in defhands");
}
seen_right = true;
Hand::Right
}
_ => bail_expr!(&group[0], "expected 'left' or 'right', got '{}'", hand_name),
};
for key_expr in &group[1..] {
let key_name = key_expr
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(key_expr, "expected a key name, found list"))?;
let osc = str_to_oscode(key_name)
.ok_or_else(|| anyhow_expr!(key_expr, "unknown key '{}'", key_name))?;
let code = u16::from(osc);
if let Some(pos) = keys.iter().position(|&k| k == code) {
let existing_name = if hands[pos] == Hand::Left {
"left"
} else {
"right"
};
bail_expr!(
key_expr,
"Key already assigned to '{}' hand, cannot also be in '{}'",
existing_name,
hand_name
);
}
keys.push(code);
hands.push(hand);
}
}
let keys_static = s.a.sref_vec(keys);
let hands_static = s.a.sref_vec(hands);
Ok(custom_tap_hold::HandMap {
keys: keys_static,
hands: hands_static,
})
}
pub(super) fn parse_tap_hold_opposite_hand(
ac_params: &[SExpr],
s: &ParserState,
) -> Result<&'static KanataAction> {
use custom_tap_hold::{DecisionBehavior, custom_tap_hold_opposite_hand};
const ARITY_MSG: &str = "tap-hold-opposite-hand expects at least 3 items: \
<timeout> <tap> <hold> [options...]";
if ac_params.is_empty() {
bail!(ARITY_MSG);
}
if ac_params.len() < 3 {
bail_expr!(&ac_params[0], "{}", ARITY_MSG);
}
let hand_map = s.hand_map.ok_or_else(|| {
anyhow_expr!(
&ac_params[0],
"tap-hold-opposite-hand requires defhands to be defined"
)
})?;
let hold_timeout = parse_non_zero_u16(&ac_params[0], s, "timeout")?;
let tap_action = parse_action(&ac_params[1], s)?;
let hold_action = parse_action(&ac_params[2], s)?;
if matches!(tap_action, Action::HoldTap { .. }) {
bail_expr!(
&ac_params[1],
"tap-hold does not work in the tap-action of tap-hold"
);
}
let mut timeout_behavior = DecisionBehavior::Tap;
let mut same_hand = DecisionBehavior::Tap;
let mut neutral_behavior = DecisionBehavior::Ignore;
let mut unknown_hand = DecisionBehavior::Ignore;
let mut neutral_keys: Vec<OsCode> = Vec::new();
let mut require_prior_idle: Option<u16> = None;
let mut seen_options: HashSet<&str> = HashSet::default();
for option_expr in &ac_params[3..] {
let Some(option) = option_expr.list(s.vars()) else {
bail_expr!(
option_expr,
"expected option list, e.g. `(timeout hold)` or `(neutral-keys spc tab)`"
);
};
if option.is_empty() {
bail_expr!(option_expr, "option list cannot be empty");
}
let kw = option[0]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(&option[0], "option name must be a string"))?;
if !seen_options.insert(kw) {
bail_expr!(
&option[0],
"duplicate option '{}' in tap-hold-opposite-hand",
kw
);
}
match kw {
"timeout" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
timeout_behavior = parse_decision_behavior_tap_hold(&option[1], s)?;
}
"same-hand" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
same_hand = parse_decision_behavior(&option[1], s)?;
}
"neutral" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
neutral_behavior = parse_decision_behavior(&option[1], s)?;
}
"unknown-hand" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
unknown_hand = parse_decision_behavior(&option[1], s)?;
}
"neutral-keys" => {
if option.len() < 2 {
bail_expr!(
option_expr,
"neutral-keys expects one or more key atoms, e.g. `(neutral-keys spc tab)`"
);
}
neutral_keys = parse_key_atoms(&option[1..], s, "neutral-keys")?;
}
"require-prior-idle" => {
require_prior_idle = Some(tap_hold::parse_require_prior_idle_option(
option,
option_expr,
s,
)?);
}
_ => bail_expr!(
&option[0],
"unknown option '{}' for tap-hold-opposite-hand. \
Valid options: timeout, same-hand, neutral, unknown-hand, neutral-keys, require-prior-idle",
kw
),
}
}
let timeout_action = match timeout_behavior {
DecisionBehavior::Tap => tap_action,
DecisionBehavior::Hold => hold_action,
DecisionBehavior::Ignore => unreachable!(),
};
let neutral_keys_static = s.a.sref_vec(neutral_keys);
Ok(s.a.sref(Action::HoldTap(s.a.sref(HoldTapAction {
config: HoldTapConfig::Custom(custom_tap_hold_opposite_hand(
hand_map,
same_hand,
neutral_behavior,
unknown_hand,
neutral_keys_static,
&s.a,
)),
tap_hold_interval: 0,
timeout: hold_timeout,
tap: *tap_action,
hold: *hold_action,
timeout_action: *timeout_action,
on_press_reset_timeout_to: None,
require_prior_idle,
}))))
}
pub(super) fn parse_tap_hold_opposite_hand_release(
ac_params: &[SExpr],
s: &ParserState,
) -> Result<&'static KanataAction> {
use custom_tap_hold::{DecisionBehavior, custom_tap_hold_opposite_hand_release};
const ARITY_MSG: &str = "tap-hold-opposite-hand-release expects at least 3 items: \
<timeout> <tap> <hold> [options...]";
if ac_params.is_empty() {
bail!(ARITY_MSG);
}
if ac_params.len() < 3 {
bail_expr!(&ac_params[0], "{}", ARITY_MSG);
}
let hand_map = s.hand_map.ok_or_else(|| {
anyhow_expr!(
&ac_params[0],
"tap-hold-opposite-hand-release requires defhands to be defined"
)
})?;
let hold_timeout = parse_non_zero_u16(&ac_params[0], s, "timeout")?;
let tap_action = parse_action(&ac_params[1], s)?;
let hold_action = parse_action(&ac_params[2], s)?;
if matches!(tap_action, Action::HoldTap { .. }) {
bail_expr!(
&ac_params[1],
"tap-hold does not work in the tap-action of tap-hold"
);
}
let mut timeout_behavior = DecisionBehavior::Tap;
let mut same_hand = DecisionBehavior::Tap;
let mut neutral_behavior = DecisionBehavior::Ignore;
let mut unknown_hand = DecisionBehavior::Ignore;
let mut neutral_keys: Vec<OsCode> = Vec::new();
let mut require_prior_idle: Option<u16> = None;
let mut seen_options: HashSet<&str> = HashSet::default();
for option_expr in &ac_params[3..] {
let Some(option) = option_expr.list(s.vars()) else {
bail_expr!(
option_expr,
"expected option list, e.g. `(timeout hold)` or `(neutral-keys spc tab)`"
);
};
if option.is_empty() {
bail_expr!(option_expr, "option list cannot be empty");
}
let kw = option[0]
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(&option[0], "option name must be a string"))?;
if !seen_options.insert(kw) {
bail_expr!(
&option[0],
"duplicate option '{}' in tap-hold-opposite-hand-release",
kw
);
}
match kw {
"timeout" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
timeout_behavior = parse_decision_behavior_tap_hold(&option[1], s)?;
}
"same-hand" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
same_hand = parse_decision_behavior(&option[1], s)?;
}
"neutral" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
neutral_behavior = parse_decision_behavior(&option[1], s)?;
}
"unknown-hand" => {
if option.len() != 2 {
bail_expr!(
option_expr,
"option must contain exactly 2 items: `(name value)`"
);
}
unknown_hand = parse_decision_behavior(&option[1], s)?;
}
"neutral-keys" => {
if option.len() < 2 {
bail_expr!(
option_expr,
"neutral-keys expects one or more key atoms, e.g. `(neutral-keys spc tab)`"
);
}
neutral_keys = parse_key_atoms(&option[1..], s, "neutral-keys")?;
}
"require-prior-idle" => {
require_prior_idle = Some(tap_hold::parse_require_prior_idle_option(
option,
option_expr,
s,
)?);
}
_ => bail_expr!(
&option[0],
"unknown option '{}' for tap-hold-opposite-hand-release. \
Valid options: timeout, same-hand, neutral, unknown-hand, neutral-keys, require-prior-idle",
kw
),
}
}
let timeout_action = match timeout_behavior {
DecisionBehavior::Tap => tap_action,
DecisionBehavior::Hold => hold_action,
DecisionBehavior::Ignore => unreachable!(),
};
let neutral_keys_static = s.a.sref_vec(neutral_keys);
Ok(s.a.sref(Action::HoldTap(s.a.sref(HoldTapAction {
config: HoldTapConfig::Custom(custom_tap_hold_opposite_hand_release(
hand_map,
same_hand,
neutral_behavior,
unknown_hand,
neutral_keys_static,
&s.a,
)),
tap_hold_interval: 0,
timeout: hold_timeout,
tap: *tap_action,
hold: *hold_action,
timeout_action: *timeout_action,
on_press_reset_timeout_to: None,
require_prior_idle,
}))))
}
fn parse_key_atoms(exprs: &[SExpr], s: &ParserState, label: &str) -> Result<Vec<OsCode>> {
exprs
.iter()
.map(|key_expr| {
let key_name = key_expr
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(key_expr, "{label} expects key atoms, found list"))?;
str_to_oscode(key_name)
.ok_or_else(|| anyhow_expr!(key_expr, "unknown key '{key_name}'"))
})
.collect()
}
fn parse_decision_behavior(
expr: &SExpr,
s: &ParserState,
) -> Result<custom_tap_hold::DecisionBehavior> {
use custom_tap_hold::DecisionBehavior;
match expr
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(expr, "expected tap, hold, or ignore"))?
{
"tap" => Ok(DecisionBehavior::Tap),
"hold" => Ok(DecisionBehavior::Hold),
"ignore" => Ok(DecisionBehavior::Ignore),
v => bail_expr!(expr, "expected tap, hold, or ignore; got '{}'", v),
}
}
fn parse_decision_behavior_tap_hold(
expr: &SExpr,
s: &ParserState,
) -> Result<custom_tap_hold::DecisionBehavior> {
use custom_tap_hold::DecisionBehavior;
match expr
.atom(s.vars())
.ok_or_else(|| anyhow_expr!(expr, "expected tap or hold"))?
{
"tap" => Ok(DecisionBehavior::Tap),
"hold" => Ok(DecisionBehavior::Hold),
v => bail_expr!(expr, "expected tap or hold for timeout; got '{}'", v),
}
}