vkeyboard.rs 2.45 KB
Newer Older
1
2
3
/*! Managing the events belonging to virtual-keyboard interface. */

use ::keyboard::{ KeyCode, PressType };
4
use ::layout::c::LevelKeyboard;
5
use ::submission::Timestamp;
6
7
8

/// Gathers stuff defined in C or called by C
pub mod c {
9
    use super::*;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    use std::os::raw::c_void;

    #[repr(transparent)]
    #[derive(Clone, Copy)]
    pub struct ZwpVirtualKeyboardV1(*const c_void);

    #[no_mangle]
    extern "C" {
        pub fn eek_virtual_keyboard_v1_key(
            virtual_keyboard: ZwpVirtualKeyboardV1,
            timestamp: u32,
            keycode: u32,
            press: u32,
        );
24
25
26
27
28

        pub fn eek_virtual_keyboard_update_keymap(
            virtual_keyboard: ZwpVirtualKeyboardV1,
            keyboard: LevelKeyboard,
        );
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
    }
}

/// Layout-independent backend. TODO: Have one instance per program or seat
pub struct VirtualKeyboard(pub c::ZwpVirtualKeyboardV1);

impl VirtualKeyboard {
    // TODO: split out keyboard state management
    pub fn switch(
        &self,
        keycodes: &Vec<KeyCode>,
        action: PressType,
        timestamp: Timestamp,
    ) {
        let keycodes_count = keycodes.len();
        for keycode in keycodes.iter() {
            let keycode = keycode - 8;
            match (action, keycodes_count) {
                // Pressing a key made out of a single keycode is simple:
                // press on press, release on release.
                (_, 1) => unsafe {
                    c::eek_virtual_keyboard_v1_key(
                        self.0, timestamp.0, keycode, action.clone() as u32
                    );
                },
                // A key made of multiple keycodes
                // has to submit them one after the other
                (PressType::Pressed, _) => unsafe {
                    c::eek_virtual_keyboard_v1_key(
                        self.0, timestamp.0, keycode, PressType::Pressed as u32
                    );
                    c::eek_virtual_keyboard_v1_key(
                        self.0, timestamp.0, keycode, PressType::Released as u32
                    );
                },
                // Design choice here: submit multiple all at press time
                // and do nothing at release time
                (PressType::Released, _) => {},
            }
        }
    }
70
71
72
73
74
75
76
77
78
    
    pub fn update_keymap(&self, keyboard: LevelKeyboard) {
        unsafe {
            c::eek_virtual_keyboard_update_keymap(
                self.0,
                keyboard,
            );
        }
    }
79
}