File: quick-evdev.rs

package info (click to toggle)
rust-xkbcommon 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: makefile: 4
file content (75 lines) | stat: -rw-r--r-- 2,417 bytes parent folder | download
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
// This is a translation of the xkbcommon quick start guide:
// https://xkbcommon.org/doc/current/md_doc_quick_guide.html

extern crate evdev;
extern crate xkbcommon;

use xkbcommon::xkb;

// evdev constants:
const KEYCODE_OFFSET: u16 = 8;
const KEY_STATE_RELEASE: i32 = 0;
const KEY_STATE_REPEAT: i32 = 2;

fn main() {
    // Open evdev device
    let mut device = evdev::Device::open(
        std::env::args()
            .nth(1)
            .unwrap_or(String::from("/dev/input/event0")),
    )
    .unwrap();

    // Create context
    let context = xkb::Context::new(xkb::CONTEXT_NO_FLAGS);

    // Load keymap informations
    let keymap = xkb::Keymap::new_from_names(
        &context,
        "",                                          // rules
        "pc105",                                     // model
        "is",                                        // layout
        "dvorak",                                    // variant
        Some("terminate:ctrl_alt_bksp".to_string()), // options
        xkb::COMPILE_NO_FLAGS,
    )
    .unwrap();

    // Create the state tracker
    let mut state = xkb::State::new(&keymap);

    loop {
        for event in device.fetch_events().unwrap() {
            if let evdev::InputEventKind::Key(keycode) = event.kind() {
                let keycode = (keycode.0 + KEYCODE_OFFSET).into();

                //  Ask the keymap what to do with key-repeat event
                if event.value() == KEY_STATE_REPEAT && !keymap.key_repeats(keycode) {
                    continue;
                }
                print!("keycode {:?} ", keycode);

                // Get keysym
                let keysym = state.key_get_one_sym(keycode);
                print!("keysym: {} ", xkb::keysym_get_name(keysym));

                // Update state
                let _changes = if event.value() == KEY_STATE_RELEASE {
                    state.update_key(keycode, xkb::KeyDirection::Up)
                } else {
                    state.update_key(keycode, xkb::KeyDirection::Down)
                };

                // Inspect state
                if state.mod_name_is_active(xkb::MOD_NAME_CTRL, xkb::STATE_MODS_EFFECTIVE) {
                    print!("Control ");
                }
                if state.led_name_is_active(xkb::LED_NAME_NUM) {
                    print!("NumLockLED");
                }

                println!();
            }
        }
    }
}