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
//! Experimental Input API
//!
//! ## wlc's original description:
//!
//! Very recent stuff, things may change.
//!
//! XXX: This api is dumb and assumes there is only single xkb state and keymap.
//! In case of multiple keyboards, we want to each keyboard have own state
//! and layout.
//!      Thus we need `wlc_handle` for keyboards eventually.

mod consts;

/// Keyboard related functionality
pub mod keyboard {
    pub use super::consts::Key;
    use Modifiers;
    use ffi;
    use std::mem;

    use std::slice;
    pub use xkbcommon::xkb::{Keymap, Keysym, State};

    pub use xkbcommon::xkb::keysyms as Keysyms;

    /// Internal `xkb_state` exposed. You can use it to do more advanced key
    /// handling.
    /// However you should avoid messing with its state
    pub fn xkb_state() -> State {
        unsafe { State::from_raw_ptr(ffi::wlc_keyboard_get_xkb_state() as *mut _) }
    }

    /// Internal `xkb_keymap` exposed. You can use it to do more advanced key
    /// handling
    pub fn xkb_keymap() -> Keymap {
        unsafe { Keymap::from_raw_ptr(ffi::wlc_keyboard_get_xkb_keymap() as *mut _) }
    }

    /// Get currently held keys
    pub fn current_keys<'a>() -> &'a [Key] {
        let mut len = 0;
        unsafe {
            let ptr = ffi::wlc_keyboard_get_current_keys(&mut len as *mut _) as *const Key;
            slice::from_raw_parts(ptr, len)
        }
    }

    /// Utility function to convert raw keycode to keysym. Passed modifiers may
    /// transform the key
    pub fn keysym_for_key(key: Key, modifiers: Modifiers) -> Keysym {
        unsafe {
            ffi::wlc_keyboard_get_keysym_for_key(key as u32,
                                                 mem::transmute::<&Modifiers,
                                                                  &ffi::wlc_modifiers>(&modifiers) as
                                                 *const _)
        }
    }

    /// Utility function to convert raw keycode to Unicode/UTF-32 codepoint.
    /// Passed modifiers may transform the key
    pub fn utf32_for_key(key: Key, modifiers: Modifiers) -> u32 {
        unsafe {
            ffi::wlc_keyboard_get_utf32_for_key(key as u32,
                                                mem::transmute::<&Modifiers,
                                                                 &ffi::wlc_modifiers>(&modifiers) as
                                                *const _)
        }
    }
}

/// Pointer related functionality
pub mod pointer {
    pub use super::consts::Button;
    use Point;
    use ffi;
    use std::mem;

    /// Get current pointer position
    pub fn position() -> Point {
        unsafe {
            let mut point: ffi::wlc_point = mem::uninitialized();
            ffi::wlc_pointer_get_position(&mut point as *mut _);
            Point::from_ffi(&point)
        }
    }

    /// Set current pointer position
    pub fn set_position(position: Point) {
        unsafe { ffi::wlc_pointer_set_position(&position.into_ffi() as *const _) }
    }
}