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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
use super::error::*;
use super::sexpr::SExpr;
use super::HashSet;
use crate::cfg::check_first_expr;
use crate::custom_action::*;
#[allow(unused)]
use crate::{anyhow_expr, anyhow_span, bail, bail_expr, bail_span};

#[derive(Debug)]
pub struct CfgOptions {
    pub process_unmapped_keys: bool,
    pub block_unmapped_keys: bool,
    pub enable_cmd: bool,
    pub sequence_timeout: u16,
    pub sequence_input_mode: SequenceInputMode,
    pub sequence_backtrack_modcancel: bool,
    pub log_layer_changes: bool,
    pub delegate_to_first_layer: bool,
    pub movemouse_inherit_accel_state: bool,
    pub movemouse_smooth_diagonals: bool,
    pub dynamic_macro_max_presses: u16,
    pub dynamic_macro_replay_delay_behaviour: ReplayDelayBehaviour,
    pub concurrent_tap_hold: bool,
    pub rapid_event_delay: u16,
    #[cfg(any(target_os = "linux", target_os = "unknown"))]
    pub linux_dev: Vec<String>,
    #[cfg(any(target_os = "linux", target_os = "unknown"))]
    pub linux_dev_names_include: Option<Vec<String>>,
    #[cfg(any(target_os = "linux", target_os = "unknown"))]
    pub linux_dev_names_exclude: Option<Vec<String>>,
    #[cfg(any(target_os = "linux", target_os = "unknown"))]
    pub linux_continue_if_no_devs_found: bool,
    #[cfg(any(target_os = "linux", target_os = "unknown"))]
    pub linux_unicode_u_code: crate::keys::OsCode,
    #[cfg(any(target_os = "linux", target_os = "unknown"))]
    pub linux_unicode_termination: UnicodeTermination,
    #[cfg(any(target_os = "linux", target_os = "unknown"))]
    pub linux_x11_repeat_delay_rate: Option<KeyRepeatSettings>,
    #[cfg(any(target_os = "windows", target_os = "unknown"))]
    pub windows_altgr: AltGrBehaviour,
    #[cfg(any(
        all(feature = "interception_driver", target_os = "windows"),
        target_os = "unknown"
    ))]
    pub windows_interception_mouse_hwids: Option<Vec<[u8; HWID_ARR_SZ]>>,
    #[cfg(any(
        all(feature = "interception_driver", target_os = "windows"),
        target_os = "unknown"
    ))]
    pub windows_interception_keyboard_hwids: Option<Vec<[u8; HWID_ARR_SZ]>>,
    #[cfg(any(target_os = "macos", target_os = "unknown"))]
    pub macos_dev_names_include: Option<Vec<String>>,
}

impl Default for CfgOptions {
    fn default() -> Self {
        Self {
            process_unmapped_keys: false,
            block_unmapped_keys: false,
            enable_cmd: false,
            sequence_timeout: 1000,
            sequence_input_mode: SequenceInputMode::HiddenSuppressed,
            sequence_backtrack_modcancel: true,
            log_layer_changes: true,
            delegate_to_first_layer: false,
            movemouse_inherit_accel_state: false,
            movemouse_smooth_diagonals: false,
            dynamic_macro_max_presses: 128,
            dynamic_macro_replay_delay_behaviour: ReplayDelayBehaviour::Recorded,
            concurrent_tap_hold: false,
            rapid_event_delay: 5,
            #[cfg(any(target_os = "linux", target_os = "unknown"))]
            linux_dev: vec![],
            #[cfg(any(target_os = "linux", target_os = "unknown"))]
            linux_dev_names_include: None,
            #[cfg(any(target_os = "linux", target_os = "unknown"))]
            linux_dev_names_exclude: None,
            #[cfg(any(target_os = "linux", target_os = "unknown"))]
            linux_continue_if_no_devs_found: false,
            #[cfg(any(target_os = "linux", target_os = "unknown"))]
            // historically was the only option, so make KEY_U the default
            linux_unicode_u_code: crate::keys::OsCode::KEY_U,
            #[cfg(any(target_os = "linux", target_os = "unknown"))]
            // historically was the only option, so make Enter the default
            linux_unicode_termination: UnicodeTermination::Enter,
            #[cfg(any(target_os = "linux", target_os = "unknown"))]
            linux_x11_repeat_delay_rate: None,
            #[cfg(any(target_os = "windows", target_os = "unknown"))]
            windows_altgr: AltGrBehaviour::default(),
            #[cfg(any(
                all(feature = "interception_driver", target_os = "windows"),
                target_os = "unknown"
            ))]
            windows_interception_mouse_hwids: None,
            #[cfg(any(
                all(feature = "interception_driver", target_os = "windows"),
                target_os = "unknown"
            ))]
            windows_interception_keyboard_hwids: None,
            #[cfg(any(target_os = "macos", target_os = "unknown"))]
            macos_dev_names_include: None,
        }
    }
}

/// Parse configuration entries from an expression starting with defcfg.
pub fn parse_defcfg(expr: &[SExpr]) -> Result<CfgOptions> {
    let mut seen_keys = HashSet::default();
    let mut cfg = CfgOptions::default();
    let mut exprs = check_first_expr(expr.iter(), "defcfg")?;
    // Read k-v pairs from the configuration
    loop {
        let key = match exprs.next() {
            Some(k) => k,
            None => return Ok(cfg),
        };
        let val = match exprs.next() {
            Some(v) => v,
            None => bail_expr!(key, "Found a defcfg option missing a value"),
        };
        match key {
            SExpr::Atom(k) => {
                let label = k.t.as_str();
                if !seen_keys.insert(label) {
                    bail_expr!(key, "Duplicate defcfg option {}", label);
                }
                match label {
                    "sequence-timeout" => {
                        cfg.sequence_timeout = parse_cfg_val_u16(val, label, true)?;
                    }
                    "sequence-input-mode" => {
                        let v = sexpr_to_str_or_err(val, label)?;
                        cfg.sequence_input_mode = SequenceInputMode::try_from_str(v)
                            .map_err(|e| anyhow_expr!(val, "{}", e.to_string()))?;
                    }
                    "dynamic-macro-max-presses" => {
                        cfg.dynamic_macro_max_presses = parse_cfg_val_u16(val, label, false)?;
                    }
                    "dynamic-macro-replay-delay-behaviour" => {
                        cfg.dynamic_macro_replay_delay_behaviour = val
                            .atom(None)
                            .map(|v| match v {
                                "constant" => Ok(ReplayDelayBehaviour::Constant),
                                "recorded" => Ok(ReplayDelayBehaviour::Recorded),
                                _ => bail_expr!(
                                    val,
                                    "this option must be one of: constant | recorded"
                                ),
                            })
                            .ok_or_else(|| {
                                anyhow_expr!(val, "this option must be one of: constant | recorded")
                            })??;
                    }
                    "linux-dev" => {
                        #[cfg(any(target_os = "linux", target_os = "unknown"))]
                        {
                            cfg.linux_dev = parse_dev(val)?;
                            if cfg.linux_dev.is_empty() {
                                bail_expr!(
                                    val,
                                    "device list is empty, no devices will be intercepted"
                                );
                            }
                        }
                    }
                    "linux-dev-names-include" => {
                        #[cfg(any(target_os = "linux", target_os = "unknown"))]
                        {
                            let dev_names = parse_dev(val)?;
                            if dev_names.is_empty() {
                                log::warn!("linux-dev-names-include is empty");
                            }
                            cfg.linux_dev_names_include = Some(dev_names);
                        }
                    }
                    "linux-dev-names-exclude" => {
                        #[cfg(any(target_os = "linux", target_os = "unknown"))]
                        {
                            cfg.linux_dev_names_exclude = Some(parse_dev(val)?);
                        }
                    }
                    "linux-unicode-u-code" => {
                        #[cfg(any(target_os = "linux", target_os = "unknown"))]
                        {
                            let v = sexpr_to_str_or_err(val, label)?;
                            cfg.linux_unicode_u_code =
                                crate::keys::str_to_oscode(v).ok_or_else(|| {
                                    anyhow_expr!(val, "unknown code for {label}: {}", v)
                                })?;
                        }
                    }
                    "linux-unicode-termination" => {
                        #[cfg(any(target_os = "linux", target_os = "unknown"))]
                        {
                            let v = sexpr_to_str_or_err(val, label)?;
                            cfg.linux_unicode_termination = match v {
                                "enter" => UnicodeTermination::Enter,
                                "space" => UnicodeTermination::Space,
                                "enter-space" => UnicodeTermination::EnterSpace,
                                "space-enter" => UnicodeTermination::SpaceEnter,
                                _ => bail_expr!(
                                    val,
                                    "{label} got {}. It accepts: enter|space|enter-space|space-enter",
                                    v
                                ),
                            }
                        }
                    }
                    "linux-x11-repeat-delay-rate" => {
                        #[cfg(any(target_os = "linux", target_os = "unknown"))]
                        {
                            let v = sexpr_to_str_or_err(val, label)?;
                            let delay_rate = v.split(',').collect::<Vec<_>>();
                            const ERRMSG: &str = "Invalid value for linux-x11-repeat-delay-rate.\nExpected two numbers 0-65535 separated by a comma, e.g. 200,25";
                            if delay_rate.len() != 2 {
                                bail_expr!(val, "{}", ERRMSG)
                            }
                            cfg.linux_x11_repeat_delay_rate = Some(KeyRepeatSettings {
                                delay: match str::parse::<u16>(delay_rate[0]) {
                                    Ok(delay) => delay,
                                    Err(_) => bail_expr!(val, "{}", ERRMSG),
                                },
                                rate: match str::parse::<u16>(delay_rate[1]) {
                                    Ok(rate) => rate,
                                    Err(_) => bail_expr!(val, "{}", ERRMSG),
                                },
                            });
                        }
                    }
                    "windows-altgr" => {
                        #[cfg(any(target_os = "windows", target_os = "unknown"))]
                        {
                            const CANCEL: &str = "cancel-lctl-press";
                            const ADD: &str = "add-lctl-release";
                            let v = sexpr_to_str_or_err(val, label)?;
                            cfg.windows_altgr = match v {
                                CANCEL => AltGrBehaviour::CancelLctlPress,
                                ADD => AltGrBehaviour::AddLctlRelease,
                                _ => bail_expr!(
                                    val,
                                    "Invalid value for {label}: {}. Valid values are {},{}",
                                    v,
                                    CANCEL,
                                    ADD
                                ),
                            }
                        }
                    }
                    "windows-interception-mouse-hwid" => {
                        #[cfg(any(
                            all(feature = "interception_driver", target_os = "windows"),
                            target_os = "unknown"
                        ))]
                        {
                            let v = sexpr_to_str_or_err(val, label)?;
                            let hwid = v;
                            log::trace!("win hwid: {hwid}");
                            let hwid_vec = hwid
                                .split(',')
                                .try_fold(vec![], |mut hwid_bytes, hwid_byte| {
                                    hwid_byte.trim_matches(' ').parse::<u8>().map(|b| {
                                        hwid_bytes.push(b);
                                        hwid_bytes
                                    })
                                }).map_err(|_| anyhow_expr!(val, "{label} format is invalid. It should consist of numbers [0,255] separated by commas"))?;
                            let hwid_slice = hwid_vec.iter().copied().enumerate()
                                .try_fold([0u8; HWID_ARR_SZ], |mut hwid, idx_byte| {
                                    let (i, b) = idx_byte;
                                    if i > HWID_ARR_SZ {
                                        bail_expr!(val, "{label} is too long; it should be up to {HWID_ARR_SZ} numbers [0,255]")
                                    }
                                    hwid[i] = b;
                                    Ok(hwid)
                            })?;
                            match cfg.windows_interception_mouse_hwids.as_mut() {
                                Some(v) => {
                                    v.push(hwid_slice);
                                }
                                None => {
                                    cfg.windows_interception_mouse_hwids = Some(vec![hwid_slice]);
                                }
                            }
                            cfg.windows_interception_mouse_hwids
                                .as_mut()
                                .unwrap()
                                .shrink_to_fit();
                        }
                    }
                    "windows-interception-mouse-hwids" => {
                        #[cfg(any(
                            all(feature = "interception_driver", target_os = "windows"),
                            target_os = "unknown"
                        ))]
                        {
                            let hwids = sexpr_to_list_or_err(val, label)?;
                            let mut parsed_hwids = vec![];
                            for hwid_expr in hwids.iter() {
                                let hwid = sexpr_to_str_or_err(
                                    hwid_expr,
                                    "entry in windows-interception-mouse-hwids",
                                )?;
                                log::trace!("win hwid: {hwid}");
                                let hwid_vec = hwid
                                .split(',')
                                .try_fold(vec![], |mut hwid_bytes, hwid_byte| {
                                    hwid_byte.trim_matches(' ').parse::<u8>().map(|b| {
                                        hwid_bytes.push(b);
                                        hwid_bytes
                                    })
                                }).map_err(|_| anyhow_expr!(hwid_expr, "Entry in {label} is invalid. Entries should be numbers [0,255] separated by commas"))?;
                                let hwid_slice = hwid_vec.iter().copied().enumerate()
                                .try_fold([0u8; HWID_ARR_SZ], |mut hwid, idx_byte| {
                                    let (i, b) = idx_byte;
                                    if i > HWID_ARR_SZ {
                                        bail_expr!(hwid_expr, "entry in {label} is too long; it should be up to {HWID_ARR_SZ} 8-bit unsigned integers")
                                    }
                                    hwid[i] = b;
                                    Ok(hwid)
                            });
                                parsed_hwids.push(hwid_slice?);
                            }
                            match cfg.windows_interception_mouse_hwids.as_mut() {
                                Some(v) => {
                                    v.extend(parsed_hwids);
                                }
                                None => {
                                    cfg.windows_interception_mouse_hwids = Some(parsed_hwids);
                                }
                            }
                            cfg.windows_interception_mouse_hwids
                                .as_mut()
                                .unwrap()
                                .shrink_to_fit();
                        }
                    }
                    "windows-interception-keyboard-hwids" => {
                        #[cfg(any(
                            all(feature = "interception_driver", target_os = "windows"),
                            target_os = "unknown"
                        ))]
                        {
                            let hwids = sexpr_to_list_or_err(val, label)?;
                            let mut parsed_hwids = vec![];
                            for hwid_expr in hwids.iter() {
                                let hwid = sexpr_to_str_or_err(
                                    hwid_expr,
                                    "entry in windows-interception-keyboard-hwids",
                                )?;
                                log::trace!("win hwid: {hwid}");
                                let hwid_vec = hwid
                                    .split(',')
                                    .try_fold(vec![], |mut hwid_bytes, hwid_byte| {
                                        hwid_byte.trim_matches(' ').parse::<u8>().map(|b| {
                                            hwid_bytes.push(b);
                                            hwid_bytes
                                        })
                                    }).map_err(|_| anyhow_expr!(hwid_expr, "Entry in {label} is invalid. Entries should be numbers [0,255] separated by commas"))?;
                                let hwid_slice = hwid_vec.iter().copied().enumerate()
                                    .try_fold([0u8; HWID_ARR_SZ], |mut hwid, idx_byte| {
                                        let (i, b) = idx_byte;
                                        if i > HWID_ARR_SZ {
                                            bail_expr!(hwid_expr, "entry in {label} is too long; it should be up to {HWID_ARR_SZ} 8-bit unsigned integers")
                                        }
                                        hwid[i] = b;
                                        Ok(hwid)
                                });
                                parsed_hwids.push(hwid_slice?);
                            }
                            parsed_hwids.shrink_to_fit();
                            cfg.windows_interception_keyboard_hwids = Some(parsed_hwids);
                        }
                    }
                    "macos-dev-names-include" => {
                        #[cfg(any(target_os = "macos", target_os = "unknown"))]
                        {
                            let dev_names = parse_dev(val)?;
                            if dev_names.is_empty() {
                                log::warn!("macos-dev-names-include is empty");
                            }
                            cfg.macos_dev_names_include = Some(dev_names);
                        }
                    }

                    "process-unmapped-keys" => {
                        cfg.process_unmapped_keys = parse_defcfg_val_bool(val, label)?
                    }
                    "block-unmapped-keys" => {
                        cfg.block_unmapped_keys = parse_defcfg_val_bool(val, label)?
                    }
                    "danger-enable-cmd" => cfg.enable_cmd = parse_defcfg_val_bool(val, label)?,
                    "sequence-backtrack-modcancel" => {
                        cfg.sequence_backtrack_modcancel = parse_defcfg_val_bool(val, label)?
                    }
                    "log-layer-changes" => {
                        cfg.log_layer_changes = parse_defcfg_val_bool(val, label)?
                    }
                    "delegate-to-first-layer" => {
                        cfg.delegate_to_first_layer = parse_defcfg_val_bool(val, label)?;
                        if cfg.delegate_to_first_layer {
                            log::info!("delegating transparent keys on other layers to first defined layer");
                        }
                    }
                    "linux-continue-if-no-devs-found" => {
                        #[cfg(any(target_os = "linux", target_os = "unknown"))]
                        {
                            cfg.linux_continue_if_no_devs_found = parse_defcfg_val_bool(val, label)?
                        }
                    }
                    "movemouse-smooth-diagonals" => {
                        cfg.movemouse_smooth_diagonals = parse_defcfg_val_bool(val, label)?
                    }
                    "movemouse-inherit-accel-state" => {
                        cfg.movemouse_inherit_accel_state = parse_defcfg_val_bool(val, label)?
                    }
                    "concurrent-tap-hold" => {
                        cfg.concurrent_tap_hold = parse_defcfg_val_bool(val, label)?
                    }
                    "rapid-event-delay" => {
                        cfg.rapid_event_delay = parse_cfg_val_u16(val, label, false)?
                    }
                    _ => bail_expr!(key, "Unknown defcfg option {}", label),
                };
            }
            SExpr::List(_) => {
                bail_expr!(key, "Lists are not allowed in as keys in defcfg");
            }
        }
    }
}

pub const FALSE_VALUES: [&str; 3] = ["no", "false", "0"];
pub const TRUE_VALUES: [&str; 3] = ["yes", "true", "1"];
pub const BOOLEAN_VALUES: [&str; 6] = ["yes", "true", "1", "no", "false", "0"];

fn parse_defcfg_val_bool(expr: &SExpr, label: &str) -> Result<bool> {
    match &expr {
        SExpr::Atom(v) => {
            let val = v.t.trim_matches('"').to_ascii_lowercase();
            if TRUE_VALUES.contains(&val.as_str()) {
                Ok(true)
            } else if FALSE_VALUES.contains(&val.as_str()) {
                Ok(false)
            } else {
                bail_expr!(
                    expr,
                    "The value for {label} must be one of: {}",
                    BOOLEAN_VALUES.join(", ")
                );
            }
        }
        SExpr::List(_) => {
            bail_expr!(
                expr,
                "The value for {label} cannot be a list, it must be one of: {}",
                BOOLEAN_VALUES.join(", "),
            )
        }
    }
}

fn parse_cfg_val_u16(expr: &SExpr, label: &str, exclude_zero: bool) -> Result<u16> {
    let start = if exclude_zero { 1 } else { 0 };
    match &expr {
        SExpr::Atom(v) => Ok(str::parse::<u16>(v.t.trim_matches('"'))
            .ok()
            .and_then(|u| {
                if exclude_zero && u == 0 {
                    None
                } else {
                    Some(u)
                }
            })
            .ok_or_else(|| anyhow_expr!(expr, "{label} must be {start}-65535"))?),
        SExpr::List(_) => {
            bail_expr!(
                expr,
                "The value for {label} cannot be a list, it must be a number {start}-65535",
            )
        }
    }
}

pub fn parse_colon_separated_text(paths: &str) -> Vec<String> {
    let mut all_paths = vec![];
    let mut full_dev_path = String::new();
    let mut dev_path_iter = paths.split(':').peekable();
    while let Some(dev_path) = dev_path_iter.next() {
        if dev_path.ends_with('\\') && dev_path_iter.peek().is_some() {
            full_dev_path.push_str(dev_path.trim_end_matches('\\'));
            full_dev_path.push(':');
            continue;
        } else {
            full_dev_path.push_str(dev_path);
        }
        all_paths.push(full_dev_path.clone());
        full_dev_path.clear();
    }
    all_paths.shrink_to_fit();
    all_paths
}

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "unknown"))]
pub fn parse_dev(val: &SExpr) -> Result<Vec<String>> {
    Ok(match val {
        SExpr::Atom(a) => {
            let devs = parse_colon_separated_text(a.t.trim_matches('"'));
            if devs.len() == 1 && devs[0].is_empty() {
                bail_expr!(val, "an empty string is not a valid device name or path")
            }
            devs
        }
        SExpr::List(l) => {
            let r: Result<Vec<String>> =
                l.t.iter()
                    .try_fold(Vec::with_capacity(l.t.len()), |mut acc, expr| match expr {
                        SExpr::Atom(path) => {
                            let trimmed_path = path.t.trim_matches('"').to_string();
                            if trimmed_path.is_empty() {
                                bail_span!(
                                    path,
                                    "an empty string is not a valid device name or path"
                                )
                            }
                            acc.push(trimmed_path);
                            Ok(acc)
                        }
                        SExpr::List(inner_list) => {
                            bail_span!(inner_list, "expected strings, found a list")
                        }
                    });

            r?
        }
    })
}

fn sexpr_to_str_or_err<'a>(expr: &'a SExpr, label: &str) -> Result<&'a str> {
    match expr {
        SExpr::Atom(a) => Ok(a.t.trim_matches('"')),
        SExpr::List(_) => bail_expr!(expr, "The value for {label} can't be a list"),
    }
}

#[cfg(any(
    all(feature = "interception_driver", target_os = "windows"),
    target_os = "unknown"
))]
fn sexpr_to_list_or_err<'a>(expr: &'a SExpr, label: &str) -> Result<&'a [SExpr]> {
    match expr {
        SExpr::Atom(_) => bail_expr!(expr, "The value for {label} must be a list"),
        SExpr::List(l) => Ok(&l.t),
    }
}

#[cfg(any(target_os = "linux", target_os = "unknown"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct KeyRepeatSettings {
    pub delay: u16,
    pub rate: u16,
}

#[cfg(any(target_os = "linux", target_os = "unknown"))]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum UnicodeTermination {
    Enter,
    Space,
    SpaceEnter,
    EnterSpace,
}

#[cfg(any(target_os = "windows", target_os = "unknown"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AltGrBehaviour {
    DoNothing,
    CancelLctlPress,
    AddLctlRelease,
}

#[cfg(any(target_os = "windows", target_os = "unknown"))]
impl Default for AltGrBehaviour {
    fn default() -> Self {
        Self::DoNothing
    }
}

#[cfg(any(
    all(feature = "interception_driver", target_os = "windows"),
    target_os = "unknown"
))]
pub const HWID_ARR_SZ: usize = 1024;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ReplayDelayBehaviour {
    /// Always use a fixed number of ticks between presses and releases.
    /// This is the original kanata behaviour.
    /// This means that held action activations like in tap-hold do not behave as intended.
    Constant,
    /// Use the recorded number of ticks between presses and releases.
    /// This is newer behaviour.
    Recorded,
}