-
Notifications
You must be signed in to change notification settings - Fork 943
/
Copy pathinput.rs
127 lines (114 loc) · 4.1 KB
/
input.rs
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
use std::{slice, str};
use x11rb::errors::{ConnectionError, ReplyError};
use x11rb::protocol::{
xinput::{self, ConnectionExt as _},
xkb::{self, ConnectionExt as _},
};
use super::*;
pub const VIRTUAL_CORE_POINTER: u16 = 2;
pub const VIRTUAL_CORE_KEYBOARD: u16 = 3;
// A base buffer size of 1kB uses a negligible amount of RAM while preventing us from having to
// re-allocate (and make another round-trip) in the *vast* majority of cases.
// To test if `lookup_utf8` works correctly, set this to 1.
const TEXT_BUFFER_SIZE: usize = 1024;
impl XConnection {
pub fn select_xinput_events(
&self,
window: xproto::Window,
device_id: u16,
mask: xinput::XIEventMask,
) -> Result<VoidCookie<'_>, X11Error> {
self.xcb_connection()
.xinput_xi_select_events(
window,
&[xinput::EventMask {
deviceid: device_id,
mask: vec![mask],
}],
)
.map_err(Into::into)
}
pub fn select_xkb_events(
&self,
device_id: xkb::DeviceSpec,
mask: xkb::EventType,
) -> Result<bool, X11Error> {
let result = self
.xcb_connection()
.xkb_select_events(
device_id,
xkb::EventType::from(0u8),
mask,
xkb::MapPart::from(u16::from(mask)),
xkb::MapPart::EXPLICIT_COMPONENTS
| xkb::MapPart::KEY_ACTIONS
| xkb::MapPart::KEY_BEHAVIORS
| xkb::MapPart::VIRTUAL_MODS
| xkb::MapPart::MODIFIER_MAP
| xkb::MapPart::VIRTUAL_MOD_MAP,
&xkb::SelectEventsAux::new(),
)
.map_err(ReplyError::from)
.and_then(|x| x.check());
match result {
Ok(()) => Ok(true),
Err(ReplyError::ConnectionError(ConnectionError::UnsupportedExtension)) => {
error!("Could not select XKB events: The XKB extension is not initialized!");
Ok(false)
}
Err(e) => Err(e.into()),
}
}
pub fn query_pointer(
&self,
window: xproto::Window,
device_id: u16,
) -> Result<xinput::XIQueryPointerReply, X11Error> {
self.xcb_connection()
.xinput_xi_query_pointer(window, device_id)?
.reply()
.map_err(Into::into)
}
fn lookup_utf8_inner(
&self,
ic: ffi::XIC,
key_event: &mut ffi::XKeyEvent,
buffer: *mut u8,
size: usize,
) -> (ffi::KeySym, ffi::Status, c_int) {
let mut keysym: ffi::KeySym = 0;
let mut status: ffi::Status = 0;
let count = unsafe {
(self.xlib.Xutf8LookupString)(
ic,
key_event,
buffer as *mut c_char,
size as c_int,
&mut keysym,
&mut status,
)
};
(keysym, status, count)
}
pub fn lookup_utf8(&self, ic: ffi::XIC, key_event: &mut ffi::XKeyEvent) -> String {
// `assume_init` is safe here because the array consists of `MaybeUninit` values,
// which do not require initialization.
let mut buffer: [MaybeUninit<u8>; TEXT_BUFFER_SIZE] =
unsafe { MaybeUninit::uninit().assume_init() };
// If the buffer overflows, we'll make a new one on the heap.
let mut vec;
let (_, status, count) =
self.lookup_utf8_inner(ic, key_event, buffer.as_mut_ptr() as *mut u8, buffer.len());
let bytes = if status == ffi::XBufferOverflow {
vec = Vec::with_capacity(count as usize);
let (_, _, new_count) =
self.lookup_utf8_inner(ic, key_event, vec.as_mut_ptr(), vec.capacity());
debug_assert_eq!(count, new_count);
unsafe { vec.set_len(count as usize) };
&vec[..count as usize]
} else {
unsafe { slice::from_raw_parts(buffer.as_ptr() as *const u8, count as usize) }
};
str::from_utf8(bytes).unwrap_or("").to_string()
}
}