xkb-type 0.1.4

Virtual keyboard typing via evdev/uinput with XKB layout-aware key mapping
Documentation
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
//! XKB reverse lookup table: char → (Keycode, Modifiers).

use crate::{KeyMapping, KeyTap};
use std::collections::HashMap;
use std::process::Command;

// Logging via the `log` crate (only active with `logging` feature),
// matching the conditional shim used in `keyboard.rs`.
#[cfg(feature = "logging")]
use log::debug;
#[cfg(not(feature = "logging"))]
macro_rules! debug {
    ($($arg:tt)*) => {};
}

#[derive(Debug, Clone)]
pub struct KeyboardLayout {
    pub layout: String,
    pub variant: String,
}

impl KeyboardLayout {
    pub fn detect() -> Self {
        // Order matters: compositor IPC and X11 reflect the *currently active* session
        // layout (honoring runtime switches via xkb-switch and friends), so they win when
        // reachable. localectl is the systemd source-of-truth fallback for cases where
        // X env vars aren't visible (e.g. running as a systemd --user service, issue #39).
        // Env vars come last as an explicit user override escape hatch.
        if let Some(kl) = Self::from_hyprland() {
            return kl;
        }
        if let Some(kl) = Self::from_sway() {
            return kl;
        }
        if let Some(kl) = Self::from_x11() {
            return kl;
        }
        if let Some(kl) = Self::from_localectl() {
            return kl;
        }
        if let Some(kl) = Self::from_env() {
            return kl;
        }
        Self {
            layout: String::new(),
            variant: String::new(),
        }
    }

    fn from_hyprland() -> Option<Self> {
        let output = Command::new("hyprctl")
            .args(["devices", "-j"])
            .output()
            .ok()?;
        if !output.status.success() {
            return None;
        }
        let json: serde_json::Value = serde_json::from_slice(&output.stdout).ok()?;
        let keyboards = json.get("keyboards")?.as_array()?;
        let kb = keyboards
            .iter()
            .find(|k| {
                let name = k.get("name").and_then(|n| n.as_str()).unwrap_or("");
                let layout = k.get("layout").and_then(|l| l.as_str()).unwrap_or("");
                !layout.is_empty() && (name.contains("translated") || name.contains("at-"))
            })
            .or_else(|| {
                keyboards.iter().find(|k| {
                    let layout = k.get("layout").and_then(|l| l.as_str()).unwrap_or("");
                    !layout.is_empty()
                })
            })?;
        let layout = kb.get("layout")?.as_str()?.to_string();
        let variant = kb
            .get("variant")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        Some(Self { layout, variant })
    }

    /// Query Sway for the active keyboard layout via `swaymsg -t get_inputs`.
    ///
    /// Sway's `xkb_layout_names` array exposes *display* names like
    /// `"German"` or `"English (US)"`, not the XKB layout codes (`"de"`,
    /// `"us"`) that `xkbcommon` understands. Feeding a display name into
    /// `Keymap::new_from_names` causes xkbcommon to silently fall back to
    /// the compile-time default layout — which in turn would
    /// short-circuit the env-var / `/etc/default/keyboard` fallbacks that
    /// can produce the *correct* code.
    ///
    /// We therefore confirm Sway is reachable (so the function is not
    /// dead code) but always return `None`, letting the caller fall
    /// through to the next fallback. If a real display-name → XKB-code
    /// lookup table is added later, this is the place to wire it up.
    fn from_sway() -> Option<Self> {
        let output = Command::new("swaymsg")
            .args(["-t", "get_inputs", "--raw"])
            .output()
            .ok()?;
        if !output.status.success() {
            return None;
        }

        let inputs: Vec<serde_json::Value> = serde_json::from_slice(&output.stdout).ok()?;

        // Confirm there is at least one keyboard input with an active
        // layout — guards against running this on an X11 session where
        // swaymsg may exist but not return useful data.
        let kb = inputs.iter().find(|i| {
            i.get("type").and_then(|t| t.as_str()) == Some("keyboard")
                && i.get("xkb_active_layout_name").is_some()
        })?;

        // Confirm the layout-name array is well-formed; we don't use
        // the value because it's a display name, not an XKB code.
        let layout_names = kb.get("xkb_layout_names")?.as_array()?;
        let active_idx = kb
            .get("xkb_active_layout_index")
            .and_then(|i| i.as_u64())
            .unwrap_or(0) as usize;
        let _display_name = layout_names.get(active_idx)?.as_str()?;

        // Intentionally fall through — see the doc comment above.
        None
    }

    fn from_x11() -> Option<Self> {
        if std::env::var_os("DISPLAY").is_none() || std::env::var_os("WAYLAND_DISPLAY").is_some() {
            return None;
        }

        let output = match Command::new("setxkbmap").arg("-query").output() {
            Ok(out) => out,
            Err(_e) => {
                debug!("setxkbmap probe failed to spawn: {_e}");
                return None;
            }
        };
        if !output.status.success() {
            debug!(
                "setxkbmap -query exited non-zero: status={:?}",
                output.status.code()
            );
            return None;
        }

        let stdout = match String::from_utf8(output.stdout) {
            Ok(s) => s,
            Err(_e) => {
                debug!("setxkbmap -query stdout was not valid utf-8: {_e}");
                return None;
            }
        };
        let parsed = parse_setxkbmap_query(&stdout);
        if let Some(_kl) = parsed.as_ref() {
            debug!(
                "x11 layout detected via setxkbmap: layout={} variant={}",
                _kl.layout, _kl.variant
            );
        } else {
            debug!("setxkbmap -query returned no parseable layout");
        }
        parsed
    }

    /// Query `localectl status` for the system X11 layout/variant.
    ///
    /// `localectl` is a systemd binary that doesn't need `DISPLAY` or
    /// `XAUTHORITY`. On a systemd host this is the source-of-truth for
    /// the system keyboard layout when the X11 environment isn't
    /// available to the daemon (e.g. running under `systemd --user`
    /// with `DISPLAY` not imported into the user manager).
    fn from_localectl() -> Option<Self> {
        let output = match Command::new("localectl").arg("status").output() {
            Ok(out) => out,
            Err(_e) => {
                debug!("localectl probe failed to spawn: {_e}");
                return None;
            }
        };
        if !output.status.success() {
            debug!(
                "localectl status exited non-zero: status={:?}",
                output.status.code()
            );
            return None;
        }

        let stdout = match String::from_utf8(output.stdout) {
            Ok(s) => s,
            Err(_e) => {
                debug!("localectl status stdout was not valid utf-8: {_e}");
                return None;
            }
        };
        let parsed = parse_localectl_status(&stdout);
        if let Some(_kl) = parsed.as_ref() {
            debug!(
                "layout detected via localectl: layout={} variant={}",
                _kl.layout, _kl.variant
            );
        } else {
            debug!("localectl status returned no parseable X11 layout");
        }
        parsed
    }

    fn from_env() -> Option<Self> {
        let layout = std::env::var("XKB_DEFAULT_LAYOUT").ok()?;
        if layout.is_empty() {
            return None;
        }
        let variant = std::env::var("XKB_DEFAULT_VARIANT").unwrap_or_default();
        Some(Self { layout, variant })
    }
}

fn parse_setxkbmap_query(output: &str) -> Option<KeyboardLayout> {
    let mut layout = None;
    let mut variant = None;

    for line in output.lines() {
        let Some((key, value)) = line.split_once(':') else {
            continue;
        };
        match key.trim() {
            "layout" => layout = Some(value.trim().to_string()),
            "variant" => variant = Some(value.trim().to_string()),
            _ => {}
        }
    }

    let layout = layout.filter(|s| !s.is_empty())?;
    Some(KeyboardLayout {
        layout,
        variant: variant.unwrap_or_default(),
    })
}

/// Parse `localectl status` output for the X11 keyboard layout/variant.
///
/// `localectl` indents its key/value lines with a varying number of
/// leading spaces (3 or 4 in practice). Trim each line before splitting
/// on the first `:` to be robust against that. Only `X11 Layout` and
/// `X11 Variant` are extracted — other keys (`System Locale`,
/// `VC Keymap`, `X11 Model`, `X11 Options`) are intentionally ignored
/// to keep this patch tightly scoped to the layout-detection bug.
fn parse_localectl_status(output: &str) -> Option<KeyboardLayout> {
    let mut layout = None;
    let mut variant = None;

    for line in output.lines() {
        let trimmed = line.trim();
        let Some((key, value)) = trimmed.split_once(':') else {
            continue;
        };
        match key.trim() {
            "X11 Layout" => layout = Some(value.trim().to_string()),
            "X11 Variant" => variant = Some(value.trim().to_string()),
            _ => {}
        }
    }

    let layout = layout.filter(|s| !s.is_empty())?;
    Some(KeyboardLayout {
        layout,
        variant: variant.unwrap_or_default(),
    })
}

pub struct XkbKeymap {
    map: HashMap<char, KeyMapping>,
    level3_keycode: u16,
}

impl XkbKeymap {
    pub fn from_layout(detected: &KeyboardLayout) -> anyhow::Result<Self> {
        let context = xkbcommon::xkb::Context::new(xkbcommon::xkb::CONTEXT_NO_FLAGS);
        let keymap = xkbcommon::xkb::Keymap::new_from_names(
            &context,
            "",
            "",
            &detected.layout,
            &detected.variant,
            None,
            xkbcommon::xkb::KEYMAP_COMPILE_NO_FLAGS,
        )
        .ok_or_else(|| {
            anyhow::anyhow!(
                "failed to create XKB keymap for layout '{}' variant '{}'",
                detected.layout,
                detected.variant
            )
        })?;
        // Fall back to KEY_RIGHTALT when no dedicated `<LVL3>` key exists
        // — that's the common case on plain `us`, `de`, etc. where AltGr
        // *is* RightAlt. Dedicated `<LVL3>` keycodes show up on layouts
        // like `us:qwerty-fr`.
        let level3_keycode =
            find_level3_keycode(&keymap).unwrap_or_else(|| evdev::Key::KEY_RIGHTALT.code());
        let map = build_reverse_map(&keymap);
        Ok(Self {
            map,
            level3_keycode,
        })
    }

    pub fn lookup(&self, ch: char) -> Option<&KeyMapping> {
        self.map.get(&ch)
    }
    pub fn level3_keycode(&self) -> u16 {
        self.level3_keycode
    }
    pub fn len(&self) -> usize {
        self.map.len()
    }
    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }
}

use xkbcommon::xkb::keysyms::{
    KEY_ISO_Level3_Shift, KEY_dead_acute, KEY_dead_cedilla, KEY_dead_circumflex,
    KEY_dead_diaeresis, KEY_dead_grave, KEY_dead_tilde,
};

const ACCENTED_VIA_DEAD_KEY: &[(char, char, u32)] = &[
    ('ã', 'a', KEY_dead_tilde),
    ('õ', 'o', KEY_dead_tilde),
    ('ñ', 'n', KEY_dead_tilde),
    ('Ã', 'A', KEY_dead_tilde),
    ('Õ', 'O', KEY_dead_tilde),
    ('Ñ', 'N', KEY_dead_tilde),
    ('á', 'a', KEY_dead_acute),
    ('é', 'e', KEY_dead_acute),
    ('í', 'i', KEY_dead_acute),
    ('ó', 'o', KEY_dead_acute),
    ('ú', 'u', KEY_dead_acute),
    ('ý', 'y', KEY_dead_acute),
    ('Á', 'A', KEY_dead_acute),
    ('É', 'E', KEY_dead_acute),
    ('Í', 'I', KEY_dead_acute),
    ('Ó', 'O', KEY_dead_acute),
    ('Ú', 'U', KEY_dead_acute),
    ('â', 'a', KEY_dead_circumflex),
    ('ê', 'e', KEY_dead_circumflex),
    ('î', 'i', KEY_dead_circumflex),
    ('ô', 'o', KEY_dead_circumflex),
    ('û', 'u', KEY_dead_circumflex),
    ('Â', 'A', KEY_dead_circumflex),
    ('Ê', 'E', KEY_dead_circumflex),
    ('Ô', 'O', KEY_dead_circumflex),
    ('à', 'a', KEY_dead_grave),
    ('è', 'e', KEY_dead_grave),
    ('ì', 'i', KEY_dead_grave),
    ('ò', 'o', KEY_dead_grave),
    ('ù', 'u', KEY_dead_grave),
    ('À', 'A', KEY_dead_grave),
    ('ä', 'a', KEY_dead_diaeresis),
    ('ë', 'e', KEY_dead_diaeresis),
    ('ï', 'i', KEY_dead_diaeresis),
    ('ö', 'o', KEY_dead_diaeresis),
    ('ü', 'u', KEY_dead_diaeresis),
    ('ç', 'c', KEY_dead_cedilla),
    ('Ç', 'C', KEY_dead_cedilla),
];

/// Scan the keymap for a key whose **base-level** (layout 0, level 0)
/// keysym is `ISO_Level3_Shift`. Returns the evdev keycode of the first
/// such key that is **not** `KEY_RIGHTALT` — that's the dedicated
/// `<LVL3>` key on layouts like `us:qwerty-fr`.
///
/// Returns `None` when there is no dedicated `<LVL3>` key (the common
/// case: `ISO_Level3_Shift` is bound to `KEY_RIGHTALT` itself, or not at
/// all). Callers fall back to `KEY_RIGHTALT` in that case.
///
/// Restricted to layout 0 / level 0 because `ISO_Level3_Shift` is a
/// modifier and lives at the base level — scanning every level would
/// occasionally pick up keys that *produce* a Level3 modifier as a
/// shifted symbol, which is not what we want.
fn find_level3_keycode(keymap: &xkbcommon::xkb::Keymap) -> Option<u16> {
    let right_alt = evdev::Key::KEY_RIGHTALT.code();

    for raw_keycode in keymap.min_keycode().raw()..=keymap.max_keycode().raw() {
        let keycode = xkbcommon::xkb::Keycode::new(raw_keycode);
        let evdev_keycode: u16 = raw_keycode.saturating_sub(8).try_into().unwrap_or(u16::MAX);

        // Restrict to layout 0 / level 0 — `ISO_Level3_Shift` is a
        // modifier at the base level.
        let syms = keymap.key_get_syms_by_level(keycode, 0, 0);
        if !syms.iter().any(|sym| sym.raw() == KEY_ISO_Level3_Shift) {
            continue;
        }

        if evdev_keycode != right_alt {
            return Some(evdev_keycode);
        }
    }

    None
}

#[allow(non_upper_case_globals)]
pub(crate) fn build_reverse_map(keymap: &xkbcommon::xkb::Keymap) -> HashMap<char, KeyMapping> {
    let mut map: HashMap<char, KeyMapping> = HashMap::new();
    let mut dead_keys: HashMap<u32, KeyTap> = HashMap::new();
    let min = keymap.min_keycode().raw();
    let max = keymap.max_keycode().raw();
    let num_layouts = keymap.num_layouts();

    for raw_keycode in min..=max {
        let keycode = xkbcommon::xkb::Keycode::new(raw_keycode);
        for layout in 0..num_layouts {
            let num_levels = keymap.num_levels_for_key(keycode, layout);
            for level in 0..num_levels {
                if level > 3 {
                    continue;
                }
                let syms = keymap.key_get_syms_by_level(keycode, layout, level);
                let evdev_keycode: u16 =
                    raw_keycode.saturating_sub(8).try_into().unwrap_or(u16::MAX);
                let shift = level == 1 || level == 3;
                let altgr = level == 2 || level == 3;

                for &sym in syms {
                    let raw = sym.raw();
                    if matches!(
                        raw,
                        KEY_dead_grave
                            | KEY_dead_acute
                            | KEY_dead_circumflex
                            | KEY_dead_tilde
                            | KEY_dead_diaeresis
                            | KEY_dead_cedilla
                    ) {
                        dead_keys.entry(raw).or_insert(KeyTap {
                            keycode: evdev_keycode,
                            shift,
                            altgr,
                        });
                        continue;
                    }
                    let unicode = xkbcommon::xkb::keysym_to_utf32(sym);
                    if unicode == 0 {
                        continue;
                    }
                    if let Some(ch) = char::from_u32(unicode) {
                        map.entry(ch).or_insert(KeyMapping {
                            main: KeyTap {
                                keycode: evdev_keycode,
                                shift,
                                altgr,
                            },
                            follow: None,
                        });
                    }
                }
            }
        }
    }

    // Pass 1: dead_X + Space for literal punctuation
    for (ch, dead_sym) in [
        ('\'', KEY_dead_acute),
        ('"', KEY_dead_diaeresis),
        ('~', KEY_dead_tilde),
        ('`', KEY_dead_grave),
        ('^', KEY_dead_circumflex),
    ] {
        if map.contains_key(&ch) {
            continue;
        }
        if let Some(dk) = dead_keys.get(&dead_sym) {
            map.insert(
                ch,
                KeyMapping {
                    main: *dk,
                    follow: Some(KeyTap {
                        keycode: evdev::Key::KEY_SPACE.code(),
                        shift: false,
                        altgr: false,
                    }),
                },
            );
        }
    }

    // Pass 2: dead_X + base_letter for accented letters
    for &(ch, base, dead_sym) in ACCENTED_VIA_DEAD_KEY {
        if map.contains_key(&ch) {
            continue;
        }
        let Some(dk) = dead_keys.get(&dead_sym) else {
            continue;
        };
        let Some(base_map) = map.get(&base).copied() else {
            continue;
        };
        if base_map.follow.is_some() {
            continue;
        }
        map.insert(
            ch,
            KeyMapping {
                main: *dk,
                follow: Some(base_map.main),
            },
        );
    }

    map
}

#[cfg(test)]
mod tests {
    use super::*;

    fn us_layout() -> KeyboardLayout {
        KeyboardLayout {
            layout: "us".to_string(),
            variant: String::new(),
        }
    }

    fn layout(name: &str, variant: &str) -> KeyboardLayout {
        KeyboardLayout {
            layout: name.to_string(),
            variant: variant.to_string(),
        }
    }

    fn assert_key(
        km: &XkbKeymap,
        ch: char,
        expected_keycode: u16,
        expected_shift: bool,
        label: &str,
    ) {
        assert_key_full(km, ch, expected_keycode, expected_shift, false, label);
    }

    fn assert_key_full(
        km: &XkbKeymap,
        ch: char,
        expected_keycode: u16,
        expected_shift: bool,
        expected_altgr: bool,
        label: &str,
    ) {
        let mapping = km
            .lookup(ch)
            .unwrap_or_else(|| panic!("'{ch}' should be in {label} keymap"));
        assert_eq!(
            mapping.main.keycode, expected_keycode,
            "'{ch}' should be at evdev {expected_keycode} on {label}, got {}",
            mapping.main.keycode
        );
        assert_eq!(
            mapping.main.shift, expected_shift,
            "'{ch}' shift should be {expected_shift} on {label}"
        );
        assert_eq!(
            mapping.main.altgr, expected_altgr,
            "'{ch}' altgr should be {expected_altgr} on {label}"
        );
    }

    #[test]
    fn build_us_keymap() {
        let km = XkbKeymap::from_layout(&us_layout());
        if let Ok(km) = km {
            assert!(!km.is_empty(), "keymap should not be empty");
            assert!(km.lookup('a').is_some(), "'a' should be in the keymap");
        }
    }

    #[test]
    fn parse_x11_setxkbmap_query() {
        let layout = parse_setxkbmap_query(
            "rules:      evdev\nmodel:      pc104\nlayout:     us\nvariant:    qwerty-fr\n",
        )
        .unwrap();

        assert_eq!(layout.layout, "us");
        assert_eq!(layout.variant, "qwerty-fr");
    }

    #[test]
    fn us_qwerty_fr_typeable_via_uinput() {
        let km = XkbKeymap::from_layout(&layout("us", "qwerty-fr")).unwrap();

        // The real contract: on us:qwerty-fr, AltGr lives behind a
        // dedicated `<LVL3>` keycode that is *not* KEY_RIGHTALT. The
        // exact evdev keycode (currently 84 / KEY_KP4) is an XKB-
        // implementation detail and could shift between xkbcommon
        // versions, so assert the property rather than the number.
        assert_ne!(
            km.level3_keycode(),
            evdev::Key::KEY_RIGHTALT.code(),
            "us:qwerty-fr must use the dedicated XKB <LVL3> keycode for \
             AltGr, not KEY_RIGHTALT"
        );
        assert_key_full(&km, 'é', 17, false, true, "US qwerty-fr");

        for ch in ['ç', 'à', 'é', 'è', 'ù'] {
            assert!(
                km.lookup(ch).is_some(),
                "'{ch}' must be reachable via uinput on us:qwerty-fr"
            );
        }
    }

    #[test]
    fn shift_mapping_for_uppercase() {
        let km = XkbKeymap::from_layout(&us_layout());
        if let Ok(km) = km {
            if let Some(mapping) = km.lookup('A') {
                assert!(
                    mapping.main.shift,
                    "uppercase 'A' should require shift on standard layouts"
                );
            }
        }
    }

    // --- QWERTZ family (y/z swapped) ---

    #[test]
    fn german_layout() {
        let km = XkbKeymap::from_layout(&layout("de", "")).unwrap();
        assert_key(&km, 'z', 21, false, "German");
        assert_key(&km, 'y', 44, false, "German");
    }

    #[test]
    fn swiss_layout() {
        let km = XkbKeymap::from_layout(&layout("ch", "")).unwrap();
        assert_key(&km, 'z', 21, false, "Swiss");
        assert_key(&km, 'y', 44, false, "Swiss");
    }

    #[test]
    fn czech_layout() {
        let km = XkbKeymap::from_layout(&layout("cz", "")).unwrap();
        assert_key(&km, 'z', 21, false, "Czech");
        assert_key(&km, 'y', 44, false, "Czech");
        assert_key(&km, 'ů', 39, false, "Czech");
    }

    #[test]
    fn slovak_layout() {
        let km = XkbKeymap::from_layout(&layout("sk", "")).unwrap();
        assert_key(&km, 'z', 21, false, "Slovak");
        assert_key(&km, 'y', 44, false, "Slovak");
        assert_key(&km, 'ô', 39, false, "Slovak");
    }

    #[test]
    fn hungarian_layout() {
        let km = XkbKeymap::from_layout(&layout("hu", "")).unwrap();
        assert_key(&km, 'z', 21, false, "Hungarian");
        assert_key(&km, 'y', 44, false, "Hungarian");
        assert_key(&km, 'ö', 11, false, "Hungarian");
        assert_key(&km, 'ü', 12, false, "Hungarian");
    }

    // --- AZERTY family (a/q and z/w swapped) ---

    #[test]
    fn french_layout() {
        let km = XkbKeymap::from_layout(&layout("fr", "")).unwrap();
        assert_key(&km, 'a', 16, false, "French");
        assert_key(&km, 'q', 30, false, "French");
        assert_key(&km, 'z', 17, false, "French");
        assert_key(&km, 'w', 44, false, "French");
    }

    #[test]
    fn belgian_layout() {
        let km = XkbKeymap::from_layout(&layout("be", "")).unwrap();
        assert_key(&km, 'a', 16, false, "Belgian");
        assert_key(&km, 'q', 30, false, "Belgian");
        assert_key(&km, 'z', 17, false, "Belgian");
        assert_key(&km, 'w', 44, false, "Belgian");
        assert_key(&km, 'm', 39, false, "Belgian");
    }

    // --- QWERTY-based with special characters ---

    #[test]
    fn spanish_layout() {
        let km = XkbKeymap::from_layout(&layout("es", "")).unwrap();
        assert_key(&km, 'ñ', 39, false, "Spanish");
    }

    #[test]
    fn portuguese_layout() {
        let km = XkbKeymap::from_layout(&layout("pt", "")).unwrap();
        assert_key(&km, 'a', 30, false, "Portuguese");
        assert_key(&km, 'z', 44, false, "Portuguese");
        assert_key(&km, 'q', 16, false, "Portuguese");
    }

    #[test]
    fn italian_layout() {
        let km = XkbKeymap::from_layout(&layout("it", "")).unwrap();
        assert_key(&km, 'a', 30, false, "Italian");
        assert_key(&km, 'z', 44, false, "Italian");
        assert_key(&km, 'q', 16, false, "Italian");
        assert_key(&km, 'w', 17, false, "Italian");
    }

    #[test]
    fn uk_layout() {
        let km = XkbKeymap::from_layout(&layout("gb", "")).unwrap();
        assert_key(&km, 'a', 30, false, "UK");
        assert_key(&km, 'z', 44, false, "UK");
        // UK has '#' without shift (evdev 43), unlike US where it's Shift+3.
        assert_key(&km, '#', 43, false, "UK");
        assert_key(&km, '£', 4, true, "UK");
    }

    // --- Nordic layouts ---

    #[test]
    fn swedish_layout() {
        let km = XkbKeymap::from_layout(&layout("se", "")).unwrap();
        assert_key(&km, 'ö', 39, false, "Swedish");
        assert_key(&km, 'ä', 40, false, "Swedish");
    }

    #[test]
    fn norwegian_layout() {
        let km = XkbKeymap::from_layout(&layout("no", "")).unwrap();
        assert_key(&km, 'ø', 39, false, "Norwegian");
        assert_key(&km, 'æ', 40, false, "Norwegian");
    }

    #[test]
    fn danish_layout() {
        let km = XkbKeymap::from_layout(&layout("dk", "")).unwrap();
        assert_key(&km, 'ø', 40, false, "Danish");
        assert_key(&km, 'æ', 39, false, "Danish");
    }

    #[test]
    fn finnish_layout() {
        let km = XkbKeymap::from_layout(&layout("fi", "")).unwrap();
        assert_key(&km, 'ö', 39, false, "Finnish");
        assert_key(&km, 'ä', 40, false, "Finnish");
    }

    // --- Eastern European ---

    #[test]
    fn polish_layout() {
        let km = XkbKeymap::from_layout(&layout("pl", "")).unwrap();
        assert_key(&km, 'a', 30, false, "Polish");
        assert_key(&km, 'z', 44, false, "Polish");
        // Polish accented characters live at level 2 (AltGr) and now go
        // through uinput directly, no clipboard fallback needed.
        assert_key_full(&km, 'ą', 30, false, true, "Polish");
        assert_key_full(&km, 'ę', 18, false, true, "Polish");
    }

    #[test]
    fn us_intl_typeable_via_uinput() {
        let km = XkbKeymap::from_layout(&KeyboardLayout {
            layout: "us".to_string(),
            variant: "intl".to_string(),
        })
        .unwrap();
        // Every character that previously had to fall back to clipboard
        // paste (and was therefore broken in terminals like Alacritty)
        // must now have a direct uinput route — either as a level 2/3
        // AltGr key, or via the dead-key + Space fallback table.
        for ch in [
            '\'', '"', '~', '`', '^', 'ç', 'á', 'é', 'í', 'ó', 'ú', 'ã', 'ñ',
        ] {
            assert!(
                km.lookup(ch).is_some(),
                "'{ch}' must be reachable via uinput on us:intl, got no mapping"
            );
        }
    }

    // --- Synthesis routing (locks in direct vs dead-key+follow paths) ---

    /// Pass 1 (literal accent via `dead_X + Space`): for each of the
    /// chars Pass 1 covers, on us:intl the char must be reachable, and
    /// — if it ended up routed through synthesis rather than a direct
    /// level mapping — the follow tap must be unmodified Space.
    /// Whether any specific char is direct vs synthesized is layout-
    /// dependent (us:intl puts some literal accents at level 2 directly),
    /// but the invariant holds: synthesized routes always end with Space.
    #[test]
    fn us_intl_pass1_chars_synthesized_routes_end_with_space() {
        let km = XkbKeymap::from_layout(&KeyboardLayout {
            layout: "us".to_string(),
            variant: "intl".to_string(),
        })
        .unwrap();
        for ch in ['\'', '"', '~', '`', '^'] {
            let mapping = km
                .lookup(ch)
                .unwrap_or_else(|| panic!("'{ch}' must be reachable on us:intl"));
            if let Some(follow) = mapping.follow {
                assert_eq!(
                    follow.keycode,
                    evdev::Key::KEY_SPACE.code(),
                    "'{ch}' was synthesized; follow tap must be SPACE \
                     (dead_X + space sequence)"
                );
                assert!(
                    !follow.shift && !follow.altgr,
                    "'{ch}' synthesis follow tap must be unmodified SPACE"
                );
            }
        }
    }

    /// Pass 2 (accented letter via `dead_X + base_letter`): `ã` is not
    /// reachable at any level on us:intl, so it must be synthesized as
    /// `dead_tilde + a`. This is the deterministic test that proves
    /// Pass 2 actually fires; the Polish/Spanish tests below cover the
    /// no-overwrite invariant for layouts that do expose the char
    /// directly.
    #[test]
    fn us_intl_tilde_letter_uses_dead_key_synthesis() {
        let km = XkbKeymap::from_layout(&KeyboardLayout {
            layout: "us".to_string(),
            variant: "intl".to_string(),
        })
        .unwrap();
        let a_main = km.lookup('a').expect("'a' must be in keymap").main;
        let mapping = km.lookup('ã').expect("ã must be reachable on us:intl");
        let follow = mapping.follow.expect(
            "ã on us:intl must be synthesized via dead_tilde + a — \
             the literal char is not at any level on this layout",
        );
        assert_eq!(
            follow.keycode, a_main.keycode,
            "ã follow tap must target the same evdev keycode as 'a' \
             (dead_tilde + a sequence)"
        );
    }

    /// Polish exposes `ą` at level 2 directly. The synthesis pass MUST
    /// NOT overwrite that direct entry — `ą` must keep `follow=None`
    /// and use AltGr, not synthesize via dead_ogonek + a.
    #[test]
    fn polish_accented_letter_is_direct_not_synthesized() {
        let km = XkbKeymap::from_layout(&layout("pl", "")).unwrap();
        let mapping = km.lookup('ą').expect("ą must be reachable on Polish");
        assert!(
            mapping.follow.is_none(),
            "ą on Polish must be a direct AltGr tap, not synthesized — \
             synthesis pass must not overwrite a direct mapping"
        );
        assert!(mapping.main.altgr, "ą on Polish must hold AltGr");
    }

    /// Spanish exposes `ñ` at level 0 directly (it's on the dedicated
    /// `ñ` key). Even though `ñ` is in `ACCENTED_VIA_DEAD_KEY`, the
    /// synthesis pass must skip it because the direct entry already
    /// exists — locks in the cross-layout no-overwrite invariant.
    #[test]
    fn spanish_enye_is_direct_not_synthesized() {
        let km = XkbKeymap::from_layout(&layout("es", "")).unwrap();
        let mapping = km.lookup('ñ').expect("ñ must be reachable on Spanish");
        assert!(
            mapping.follow.is_none(),
            "ñ on Spanish must be a direct level-0 tap, not synthesized — \
             synthesis pass must not overwrite even when the char is in \
             the synthesis table"
        );
        assert!(
            !mapping.main.altgr && !mapping.main.shift,
            "ñ on Spanish is on a dedicated key — no modifiers required"
        );
    }

    // --- Alternative Latin layouts ---

    #[test]
    fn dvorak_layout() {
        let km = XkbKeymap::from_layout(&layout("us", "dvorak")).unwrap();
        assert_key(&km, 'o', 31, false, "Dvorak");
        assert_key(&km, 'e', 32, false, "Dvorak");
        assert_key(&km, 's', 39, false, "Dvorak");
    }

    #[test]
    fn colemak_layout() {
        let km = XkbKeymap::from_layout(&layout("us", "colemak")).unwrap();
        assert_key(&km, 'f', 18, false, "Colemak");
        assert_key(&km, 'n', 36, false, "Colemak");
        assert_key(&km, 's', 32, false, "Colemak");
    }

    // --- Non-Latin layouts ---

    #[test]
    fn russian_layout() {
        let km = XkbKeymap::from_layout(&layout("ru", "")).unwrap();
        assert_key(&km, 'ф', 30, false, "Russian");
        assert_key(&km, 'я', 44, false, "Russian");
        assert_key(&km, 'й', 16, false, "Russian");
        assert_key(&km, 'ц', 17, false, "Russian");
    }

    #[test]
    fn ukrainian_layout() {
        let km = XkbKeymap::from_layout(&layout("ua", "")).unwrap();
        assert_key(&km, 'ф', 30, false, "Ukrainian");
        assert_key(&km, 'я', 44, false, "Ukrainian");
        assert_key(&km, 'й', 16, false, "Ukrainian");
        assert_key(&km, 'і', 31, false, "Ukrainian");
    }

    #[test]
    fn greek_layout() {
        let km = XkbKeymap::from_layout(&layout("gr", "")).unwrap();
        assert_key(&km, 'α', 30, false, "Greek");
        assert_key(&km, 'ζ', 44, false, "Greek");
        assert_key(&km, 'ω', 47, false, "Greek");
    }

    #[test]
    fn japanese_layout() {
        // Japanese (jp) is QWERTY-based for Latin characters.
        let km = XkbKeymap::from_layout(&layout("jp", "")).unwrap();
        assert_key(&km, 'a', 30, false, "Japanese");
        assert_key(&km, 'z', 44, false, "Japanese");
    }

    // --- Sway detection regression ---

    /// `KeyboardLayout::from_sway` deliberately returns `None` because
    /// Sway's `xkb_layout_names` exposes display strings (e.g. `"German"`,
    /// `"English (US)"`) rather than XKB layout codes (`"de"`, `"us"`).
    /// Returning `Some(display_name)` would make the caller short-circuit
    /// the env-var fallback and silently compile against the *default*
    /// layout, which is the bug this regression test guards against.
    #[test]
    fn from_sway_always_returns_none() {
        // `from_sway` shells out to `swaymsg`; on most CI machines that
        // command doesn't exist, but if it does we must still see `None`
        // (the function intentionally never returns `Some` regardless of
        // what swaymsg replies with).
        let result = KeyboardLayout::from_sway();
        assert!(
            result.is_none(),
            "from_sway must return None (display names ≠ XKB codes); got {result:?}"
        );
    }

    // --- localectl parser tests ---

    #[test]
    fn parse_localectl_us_no_variant() {
        let output = "\
System Locale: LANG=en_US.UTF-8
    VC Keymap: us
   X11 Layout: us
    X11 Model: pc105+inet
  X11 Options: terminate:ctrl_alt_bksp
";
        let kl = parse_localectl_status(output).expect("should parse US layout");
        assert_eq!(kl.layout, "us");
        assert_eq!(kl.variant, "");
    }

    #[test]
    fn parse_localectl_fr_bepo_full() {
        let output = "\
System Locale: LANG=en_US.UTF-8
    VC Keymap: fr-bepo
   X11 Layout: fr
    X11 Model: pc104
  X11 Variant: bepo
  X11 Options: caps:swapescape,compose:rwin
";
        let kl = parse_localectl_status(output).expect("should parse fr(bepo)");
        assert_eq!(kl.layout, "fr");
        assert_eq!(kl.variant, "bepo");
    }

    #[test]
    fn parse_localectl_empty_layout_returns_none() {
        let output = "\
System Locale: LANG=en_US.UTF-8
    VC Keymap: us
   X11 Layout:
    X11 Model: pc105
";
        assert!(parse_localectl_status(output).is_none());
    }

    #[test]
    fn parse_localectl_missing_x11_section_returns_none() {
        let output = "\
System Locale: LANG=en_US.UTF-8
    VC Keymap: us
";
        assert!(parse_localectl_status(output).is_none());
    }

    #[test]
    fn parse_localectl_tolerates_whitespace_variants() {
        // Mix tabs, varying leading-space widths, and trailing whitespace.
        let output = "System Locale: LANG=en_US.UTF-8\n\
\t\tVC Keymap: fr-bepo\n\
 X11 Layout:   fr  \n\
      X11 Model: pc104\n\
\tX11 Variant:\tbepo\n";
        let kl = parse_localectl_status(output).expect("should tolerate whitespace mix");
        assert_eq!(kl.layout, "fr");
        assert_eq!(kl.variant, "bepo");
    }

    #[test]
    fn parse_localectl_ignores_unknown_keys() {
        let output = "\
System Locale: LANG=en_US.UTF-8
    VC Keymap: us
          Foo: bar
   X11 Layout: us
    X11 Model: pc105
       Random: line with: multiple colons
  X11 Variant: dvorak
";
        let kl = parse_localectl_status(output).expect("should ignore unknown keys");
        assert_eq!(kl.layout, "us");
        assert_eq!(kl.variant, "dvorak");
    }

    // --- localectl chain-gap regression ---

    /// Issue #39: detection must have at least one source that works
    /// without any X session env vars on a systemd host. `from_localectl`
    /// fills that gap. If `localectl` is installed (the case on every
    /// systemd distro), it must return `Some(layout)` with a non-empty
    /// layout code — this is what prevents the US/QWERTY fallback that
    /// produced garbled output for fr(bepo) users running under
    /// `systemd --user`.
    #[test]
    fn from_localectl_returns_layout_when_available() {
        // Skip if the binary is unavailable (e.g. non-systemd CI image).
        if Command::new("localectl").arg("--version").output().is_err() {
            eprintln!("skipping: localectl not on PATH");
            return;
        }
        let result = KeyboardLayout::from_localectl();
        let kl = result.expect(
            "from_localectl must return Some(_) on a systemd host — \
             this is the detection-chain gap that allowed issue #39",
        );
        assert!(
            !kl.layout.is_empty(),
            "from_localectl returned Some with empty layout: {kl:?}"
        );
    }
}