kbvm 0.1.5

An implementation of the XKB specification
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
//! The client-side [`LookupTable`].
//!
//! This module contains types to map key events to keysyms and characters.
//!
//! The main entry point to this module is the [`LookupTable`] type.
//!
//! # Example
//!
//! ```
//! # use kbvm::{GroupIndex, Keycode, ModifierMask};
//! # use kbvm::lookup::LookupTable;
//! # use kbvm::xkb::Context;
//! # use kbvm::xkb::diagnostic::WriteToLog;
//! fn create_lookup_table(keymap: &[u8]) -> LookupTable {
//!     let context = Context::default();
//!     let keymap = context.keymap_from_bytes(WriteToLog, None, keymap).unwrap();
//!     keymap.to_builder().build_lookup_table()
//! }
//!
//! fn key_press_to_string(
//!     lookup_table: &LookupTable,
//!     group: GroupIndex,
//!     mods: ModifierMask,
//!     keycode: Keycode,
//! ) -> String {
//!     lookup_table
//!         .lookup(group, mods, keycode)
//!         .into_iter()
//!         .flat_map(|p| p.char())
//!         .collect()
//! }
//! ```

#[cfg(test)]
mod tests;

#[expect(unused_imports)]
use crate::{Components, builder::Builder};
use {
    crate::{
        GroupType, Keycode, Keysym, ModifierMask, builder::Redirect, group::GroupIndex,
        key_storage::KeyStorage,
    },
    smallvec::SmallVec,
    std::fmt::{Debug, Formatter},
};

/// A keysym lookup table.
///
/// This type allows looking up keysyms and characters generated by key-press events. It
/// is created by calling [`Builder::build_lookup_table`].
///
/// # Example
///
/// ```
/// # use kbvm::GroupIndex;
/// # use kbvm::lookup::LookupTable;
/// # use kbvm::ModifierMask;
/// # use kbvm::Keycode;
/// fn get_string(
///     table: &LookupTable,
///     group: GroupIndex,
///     modifiers: ModifierMask,
///     keycode: Keycode,
/// ) -> String {
///     table
///         .lookup(group, modifiers, keycode)
///         .into_iter()
///         .flat_map(|p| p.char())
///         .collect()
/// }
/// ```
///
/// # Key Repeat
///
/// A key can be configured to repeat. In this case, the client should periodically emulate
/// key-press events until the next repeating key is pressed or this key is released.
///
/// This property can be accessed with the [`Lookup::repeats`] function.
///
/// ```
/// # use kbvm::GroupIndex;
/// # use kbvm::lookup::LookupTable;
/// # use kbvm::ModifierMask;
/// # use kbvm::Keycode;
/// fn need_key_repeat_events(
///     table: &LookupTable,
///     group: GroupIndex,
///     modifiers: ModifierMask,
///     keycode: Keycode,
/// ) -> bool {
///     table
///         .lookup(group, modifiers, keycode)
///         .repeats()
/// }
/// ```
///
/// # Consumed Modifiers
///
/// The lookup process might consume some of the input modifiers. These modifiers should be ignored
/// when the produced keysyms are used in keyboard shortcuts.
///
/// This remaining modifiers can be accessed with the [`Lookup::remaining_mods`] function.
///
/// ```
/// # use kbvm::GroupIndex;
/// # use kbvm::Keysym;
/// # use kbvm::lookup::LookupTable;
/// # use kbvm::ModifierMask;
/// # use kbvm::Keycode;
/// fn get_keysyms_for_shortcuts(
///     table: &LookupTable,
///     group: GroupIndex,
///     modifiers: ModifierMask,
///     keycode: Keycode,
/// ) -> impl Iterator<Item = (ModifierMask, Keysym)> + use<'_> {
///     let lookup = table.lookup(group, modifiers, keycode);
///     lookup.into_iter().map(move |p| (lookup.remaining_mods(), p.keysym()))
/// }
/// ```
///
/// # Caps Transformation
///
/// If the remaining modifiers contain the caps modifier, the keysym iterator will automatically
/// perform caps transformation. That is, all keysyms will be replaced by their uppercase version.
///
/// This behavior can be disabled by using [`Lookup::with_caps_transform`] and
/// [`Lookup::set_caps_transform`].
///
/// ```
/// # use kbvm::GroupIndex;
/// # use kbvm::Keysym;
/// # use kbvm::lookup::{KeysymProps, LookupTable};
/// # use kbvm::ModifierMask;
/// # use kbvm::Keycode;
/// fn disable_caps_transform(
///     table: &LookupTable,
///     group: GroupIndex,
///     modifiers: ModifierMask,
///     keycode: Keycode,
/// ) -> impl Iterator<Item = KeysymProps> + use<'_> {
///     table
///         .lookup(group, modifiers, keycode)
///         .with_caps_transform(false)
///         .into_iter()
/// }
/// ```
///
/// # Ctrl Transformation
///
/// If the remaining modifiers contain the ctrl modifier, the keysym iterator will automatically
/// perform ctrl transformation. This transform only affects the produced characters but not the
/// produced keysyms.
///
/// This behavior can be disabled by using [`Lookup::with_ctrl_transform`] and
/// [`Lookup::set_ctrl_transform`].
///
/// ```
/// # use kbvm::GroupIndex;
/// # use kbvm::Keysym;
/// # use kbvm::lookup::{KeysymProps, LookupTable};
/// # use kbvm::ModifierMask;
/// # use kbvm::Keycode;
/// fn disable_ctrl_transform(
///     table: &LookupTable,
///     group: GroupIndex,
///     modifiers: ModifierMask,
///     keycode: Keycode,
/// ) -> impl Iterator<Item = char> + use<'_> {
///     table
///         .lookup(group, modifiers, keycode)
///         .with_ctrl_transform(false)
///         .into_iter()
///         .flat_map(|p| p.char())
/// }
/// ```
///
/// Ctrl transformation applies the following modification:
///
/// ```
/// # let input = 0u8;
/// let output = match input {
///    b'@'..=b'~' => input & 0x1f,
///    b' ' | b'2' => 0,
///    b'3'..=b'7' => input - 0x1b,
///    b'8' => 0x7f,
///    b'/' => 0x1f,
///    _ => input,
/// };
/// ```
///
/// # Ctrl Transformation Fallback
///
/// If
///
/// - the preconditions for ctrl transformation are met, and
/// - the key press would otherwise produce a single keysym, and
/// - that keysym has a value greater than 127, and
/// - the key has another group that, when used with the same modifiers, would produce a single
///   keysym less than or equal to 127,
///
/// then, instead of using the original keysym, the keysym form the first such group is used.
///
/// Caps and ctrl transformations are then applied to that keysym.
///
/// This is sometimes used to allow users of non-latin keyboard layouts to use latin
/// keysyms in keyboard shortcuts.
///
/// This behavior can be disabled by using [`Lookup::with_ctrl_fallback`] and
/// [`Lookup::set_ctrl_fallback`].
///
/// ```
/// # use kbvm::GroupIndex;
/// # use kbvm::Keysym;
/// # use kbvm::lookup::{KeysymProps, LookupTable};
/// # use kbvm::ModifierMask;
/// # use kbvm::Keycode;
/// fn disable_ctrl_fallback(
///     table: &LookupTable,
///     group: GroupIndex,
///     modifiers: ModifierMask,
///     keycode: Keycode,
/// ) -> impl Iterator<Item = char> + use<'_> {
///     table
///         .lookup(group, modifiers, keycode)
///         .with_ctrl_fallback(false)
///         .into_iter()
///         .flat_map(|p| p.char())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct LookupTable {
    pub(crate) ctrl: Option<ModifierMask>,
    pub(crate) caps: Option<ModifierMask>,
    pub(crate) keys: KeyStorage<KeyGroups>,
}

#[derive(Default, Clone, Debug)]
pub(crate) struct KeyGroups {
    pub(crate) repeats: bool,
    pub(crate) redirect: Redirect,
    pub(crate) groups: Box<[Option<KeyGroup>]>,
}

#[derive(Clone, Debug)]
pub(crate) struct KeyGroup {
    pub(crate) ty: GroupType,
    pub(crate) levels: Box<[KeyLevel]>,
}

#[derive(Default, Clone, Debug)]
pub(crate) struct KeyLevel {
    pub(crate) symbols: SmallVec<[Keysym; 1]>,
}

/// The result of a [`LookupTable::lookup`] call.
///
/// This object represents the result of a call to [`LookupTable::lookup`]. To get the
/// produced keysyms, use the [`IntoIterator`] implementation of this type.
///
/// You can modify the output of the iterator by calling the various functions of this
/// type. This is described in detail in the [`LookupTable`] documentation.
///
/// # Example
///
/// ```
/// # use kbvm::{evdev, GroupIndex, ModifierMask};
/// # use kbvm::lookup::LookupTable;
/// fn lookup(table: &LookupTable) {
///     for output in table.lookup(GroupIndex::ZERO, ModifierMask::SHIFT, evdev::A) {
///         println!("{:?}", output.keysym());
///     }
/// }
/// ```
///
/// This might print
///
/// ```text
/// A
/// ```
#[derive(Copy, Clone)]
pub struct Lookup<'a> {
    original_mods: ModifierMask,
    remaining_mods: ModifierMask,
    use_ctrl_fallback: bool,
    do_ctrl_transform: bool,
    do_caps_transform: bool,
    repeats: bool,
    lookup: &'a LookupTable,
    groups: &'a [Option<KeyGroup>],
    syms: &'a [Keysym],
}

/// An iterator over [`KeysymProps`].
///
/// This type is created via the [`IntoIterator`] implementation of [`Lookup`].
#[derive(Clone, Debug)]
pub struct LookupIter<'a> {
    did_ctrl_fallback: bool,
    do_ctrl_transform: bool,
    do_caps_transform: bool,
    syms: &'a [Keysym],
}

/// A keysym produced by the lookup process.
#[derive(Copy, Clone, Debug)]
pub struct KeysymProps {
    keysym: Keysym,
    char: Option<char>,
    did_ctrl_fallback: bool,
    did_ctrl_transform: bool,
    did_caps_transform: bool,
}

impl Lookup<'_> {
    /// Enables or disables Ctrl Transformation Fallback.
    ///
    /// See the documentation of [`LookupTable`] for more details.
    pub fn with_ctrl_fallback(mut self, fallback: bool) -> Self {
        self.use_ctrl_fallback = fallback;
        self
    }

    /// Enables or disables Ctrl Transformation Fallback.
    ///
    /// See the documentation of [`LookupTable`] for more details.
    pub fn set_ctrl_fallback(&mut self, fallback: bool) {
        self.use_ctrl_fallback = fallback;
    }

    /// Enables or disables Ctrl Transformation.
    ///
    /// See the documentation of [`LookupTable`] for more details.
    pub fn with_ctrl_transform(mut self, transform: bool) -> Self {
        self.do_ctrl_transform = transform;
        self
    }

    /// Enables or disables Ctrl Transformation.
    ///
    /// See the documentation of [`LookupTable`] for more details.
    pub fn set_ctrl_transform(&mut self, transform: bool) {
        self.do_ctrl_transform = transform;
    }

    /// Enables or disables Caps Transformation.
    ///
    /// See the documentation of [`LookupTable`] for more details.
    pub fn with_caps_transform(mut self, transform: bool) -> Self {
        self.do_caps_transform = transform;
        self
    }

    /// Enables or disables Caps Transformation.
    ///
    /// See the documentation of [`LookupTable`] for more details.
    pub fn set_caps_transform(&mut self, transform: bool) {
        self.do_caps_transform = transform;
    }

    /// Returns whether the key repeats.
    pub fn repeats(&self) -> bool {
        self.repeats
    }

    /// Returns the remaining modifiers.
    ///
    /// These are the modifiers that should be used for keyboard shortcuts.
    pub fn remaining_mods(&self) -> ModifierMask {
        self.remaining_mods
    }
}

impl KeysymProps {
    /// The keysym.
    pub fn keysym(&self) -> Keysym {
        self.keysym
    }

    /// The character produced by the lookup.
    ///
    /// If ctrl transformation was applied to the lookup, then this character is different
    /// from [`Keysym::char`].
    pub fn char(&self) -> Option<char> {
        self.char
    }

    /// Whether ctrl fallback was applied to this keysym.
    pub fn did_ctrl_fallback(&self) -> bool {
        self.did_ctrl_fallback
    }

    /// Whether ctrl transformation was applied to this keysym and changed the character.
    pub fn did_ctrl_transform(&self) -> bool {
        self.did_ctrl_transform
    }

    /// Whether caps transformation was applied to this keysym and changed the keysym.
    pub fn did_caps_transform(&self) -> bool {
        self.did_caps_transform
    }
}

impl Debug for Lookup<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Keysyms")
            .field("original_mods", &self.original_mods)
            .field("remaining_mods", &self.remaining_mods)
            .field("use_ctrl_fallback", &self.use_ctrl_fallback)
            .field("do_ctrl_transform", &self.do_ctrl_transform)
            .field("do_caps_transform", &self.do_caps_transform)
            .field("repeats", &self.repeats)
            .finish_non_exhaustive()
    }
}

impl LookupTable {
    /// Looks up the keysyms associated with a key press.
    ///
    /// The `group` and `mods` should be the effective group and effective modifiers
    /// computed by the compositor.
    ///
    /// In wayland, the effective modifiers are the bitwise OR of the pressed, latched,
    /// and locked modifiers. This can be managed with the [`Components`] type.
    ///
    /// # Example
    ///
    /// An application using the wayland-client crate might use this function as follows:
    ///
    /// ```ignore
    /// struct State {
    ///     lookup_table: LookupTable,
    ///     group: GroupIndex,
    ///     mods: ModifierMask,
    /// }
    ///
    /// impl Dispatch<WlKeyboard, ()> for State {
    ///     fn event(
    ///         state: &mut State,
    ///         _: &WlKeyboard,
    ///         event: wl_keyboard::Event,
    ///         _: &(),
    ///         _: &Connection,
    ///         _: &QueueHandle<State>,
    ///     ) {
    ///         use wl_keyboard::Event;
    ///         match event {
    ///             Event::Modifiers {
    ///                 mods_depressed, mods_latched, mods_locked, group, ..
    ///             } => {
    ///                 state.group = GroupIndex(group);
    ///                 state.mods = ModifierMask(mods_depressed | mods_latched | mods_locked);
    ///             }
    ///             Event::Key { key, state: WEnum::Value(WlKeyState::Pressed), .. } => {
    ///                 let key = Keycode::from_evdev(key);
    ///                 for keysym in state.lookup_table.lookup(state.group, state.mods, key) {
    ///                     println!("{keysym:?}");
    ///                 }
    ///             }
    ///             _ => { },
    ///         }
    ///     }
    /// }
    /// ```
    pub fn lookup(&self, group: GroupIndex, mods: ModifierMask, keycode: Keycode) -> Lookup<'_> {
        let mut consumed = ModifierMask::default();
        let mut groups = &[][..];
        let mut syms = &[][..];
        let mut repeats = true;
        if let Some(key) = self.keys.get(keycode) {
            repeats = key.repeats;
            groups = &key.groups;
            if key.groups.len() > 0 {
                let group = key.redirect.apply(group, key.groups.len());
                if let Some(group) = &key.groups[group] {
                    let mapping = group.ty.map(mods);
                    consumed = mapping.consumed;
                    // println!("{:?}", group.ty);
                    // println!("{:?}", mapping);
                    if let Some(level) = group.levels.get(mapping.level) {
                        syms = &level.symbols;
                    }
                }
            }
        }
        Lookup {
            original_mods: mods,
            remaining_mods: mods & !consumed,
            use_ctrl_fallback: true,
            do_ctrl_transform: true,
            do_caps_transform: true,
            lookup: self,
            repeats,
            groups,
            syms,
        }
    }

    /// Returns the effective group that will be used for the keycode.
    ///
    /// The `group` parameter should be the effective group computed by the compositor.
    /// This function will then return the effective group for the specific key.
    ///
    /// These values can differ when the key has fewer groups than the maximum number of
    /// groups in the keymap. In this case, the effective group is calculated using
    /// the [`Redirect`] setting of the key.
    pub fn effective_group(&self, group: GroupIndex, keycode: Keycode) -> Option<GroupIndex> {
        if let Some(key) = self.keys.get(keycode) {
            if key.groups.len() > 0 {
                let group = key.redirect.apply(group, key.groups.len());
                return Some(GroupIndex(group as u32));
            }
        }
        None
    }

    /// Returns whether the key repeats.
    pub fn repeats(&self, keycode: Keycode) -> bool {
        if let Some(key) = self.keys.get(keycode) {
            return key.repeats;
        }
        true
    }
}

impl<'a> IntoIterator for Lookup<'a> {
    type Item = KeysymProps;
    type IntoIter = LookupIter<'a>;

    fn into_iter(self) -> Self::IntoIter {
        let mut do_ctrl_transform = false;
        if let Some(mask) = self.lookup.ctrl {
            if self.do_ctrl_transform && self.remaining_mods.contains(mask) {
                do_ctrl_transform = true;
            }
        }
        let mut do_caps_transform = false;
        if let Some(mask) = self.lookup.caps {
            if self.do_caps_transform && self.remaining_mods.contains(mask) {
                do_caps_transform = true;
            }
        }
        let mut syms = self.syms;
        let mut did_ctrl_fallback = false;
        if self.use_ctrl_fallback && do_ctrl_transform && syms.len() == 1 && syms[0].0 > 127 {
            for group in self.groups.iter().flatten() {
                let level = group.ty.map(self.original_mods).level;
                if let Some(level) = group.levels.get(level) {
                    if level.symbols.len() == 1 && level.symbols[0].0 <= 127 {
                        syms = &level.symbols;
                        did_ctrl_fallback = true;
                        break;
                    }
                }
            }
        }
        LookupIter {
            did_ctrl_fallback,
            do_ctrl_transform,
            do_caps_transform,
            syms,
        }
    }
}

impl Iterator for LookupIter<'_> {
    type Item = KeysymProps;

    fn next(&mut self) -> Option<Self::Item> {
        let mut sym = self.syms.first().copied()?;
        self.syms = &self.syms[1..];
        let mut did_caps_transform = false;
        if self.do_caps_transform {
            let prev = sym;
            sym = sym.to_uppercase();
            did_caps_transform = prev != sym;
        }
        let mut char = None;
        let mut did_ctrl_transform = false;
        if self.do_ctrl_transform && sym.0 <= 127 {
            let s = sym.0 as u8;
            'transform: {
                let c = match s {
                    b'@'..=b'~' | b' ' => s & 0x1f,
                    b'2' => 0,
                    b'3'..=b'7' => s - 0x1b,
                    b'8' => 0x7f,
                    b'/' => 0x1f,
                    _ => break 'transform,
                };
                char = Some(c as char);
                did_ctrl_transform = true;
            }
        }
        if char.is_none() {
            char = sym.char();
        }
        Some(KeysymProps {
            keysym: sym,
            char,
            did_ctrl_fallback: self.did_ctrl_fallback,
            did_ctrl_transform,
            did_caps_transform,
        })
    }
}