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
//! Contains methods for interacting with the pointer
//! and keyboard of wlc.

// We get warnings for &Keymod, which uses bitflags.
#![allow(improper_ctypes)]

use super::types::{KeyMod, Point};
use libc::size_t;

#[link(name = "wlc")]
extern "C" {
    fn wlc_keyboard_get_current_keys(out_memb: *const size_t) -> *const u32;

    fn wlc_keyboard_get_keysym_for_key(key: u32, modifiers: &KeyMod) -> u32;

    fn wlc_keyboard_get_utf32_for_key(key: u32, modifiers: &KeyMod) -> u32;

    // Pointer functions
    fn wlc_pointer_get_position(out_position: *mut Point);

    fn wlc_pointer_set_position(position: &Point);
}

pub mod pointer {
//! Methods for interacting with the mouse
    use super::super::types::{Point};

    /// Gets the current position of the mouse.
    pub fn get_position() -> Point {
        unsafe {
            let mut point = Point { x: 0, y: 0 };
            super::wlc_pointer_get_position(&mut point);
            return point;
        }
    }

    /// Sets the current mouse position. Required on mouse move callback.
    pub fn set_position(point: &Point) {
        unsafe { super::wlc_pointer_set_position(point); }
    }
}

pub mod keyboard {
//! Methods for interacting with the keyboard
    use super::super::types::{KeyMod};
    use super::super::xkb::Keysym;
    use libc::size_t;
    use std::slice;

    /// Get currently held keys.
    pub fn get_current_keys<'a>() -> &'a[u32] {
        let mut out_memb: size_t = 0;
        unsafe {
            let keys = super::wlc_keyboard_get_current_keys(&mut out_memb);
            return slice::from_raw_parts(keys, out_memb as usize);
        }
    }

    /// Gets a keysym given a key and modifiers.
    pub fn get_keysym_for_key(key: u32, modifiers: &KeyMod) -> Keysym {
        unsafe { Keysym::from(super::wlc_keyboard_get_keysym_for_key(key, modifiers)) }
    }

    /// Gets a UTF32 value for a given key and modifiers.
    pub fn get_utf32_for_key(key: u32, modifiers: &KeyMod) -> u32 {
        unsafe { super::wlc_keyboard_get_utf32_for_key(key, modifiers) }
    }
}