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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use std::ffi::CStr;
use crate::{NcDim, NcKey};
pub(crate) mod reimplemented;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NcReceived {
Char(char),
Event(NcKey),
NoInput,
Other(u32),
}
impl NcReceived {
pub fn new(num: u32) -> Self {
if num == 0 {
Self::NoInput
} else if NcKey::is(num) {
Self::Event(NcKey::new(num).unwrap())
} else if let Some(c) = core::char::from_u32(num) {
Self::Char(c)
} else {
Self::Other(num)
}
}
}
impl From<NcInput> for NcReceived {
fn from(i: NcInput) -> Self {
Self::new(i.id)
}
}
impl From<&NcInput> for NcReceived {
fn from(i: &NcInput) -> Self {
Self::new(i.id)
}
}
impl From<&mut NcInput> for NcReceived {
fn from(i: &mut NcInput) -> Self {
Self::new(i.id)
}
}
impl From<NcReceived> for u32 {
fn from(r: NcReceived) -> Self {
use NcReceived::*;
match r {
Char(c) => c.into(),
Event(e) => e.into(),
NoInput => 0,
Other(o) => o,
}
}
}
pub type NcInput = crate::bindings::ffi::ncinput;
impl NcInput {
pub const fn new_empty() -> NcInput {
NcInput {
id: 0,
y: 0,
x: 0,
utf8: [0; 5],
alt: false,
shift: false,
ctrl: false,
evtype: NcEvType::UNKNOWN,
ypx: -1,
xpx: -1,
}
}
pub const fn new(id: char) -> NcInput {
Self::with_all_args(id, None, None, false, false, false, 0)
}
pub const fn with_alt(id: char) -> NcInput {
Self::with_all_args(id, None, None, true, false, false, 0)
}
pub const fn with_shift(id: char) -> NcInput {
Self::with_all_args(id, None, None, false, true, false, 0)
}
pub const fn with_ctrl(id: char) -> NcInput {
Self::with_all_args(id, None, None, false, false, true, 0)
}
pub const fn with_all_args(
id: char,
x: Option<NcDim>,
y: Option<NcDim>,
alt: bool,
shift: bool,
ctrl: bool,
evtype: NcEvType,
) -> NcInput {
let (ix, iy);
if let Some(x) = x {
ix = x as i32
} else {
ix = -1
};
if let Some(y) = y {
iy = y as i32
} else {
iy = -1
};
NcInput {
id: id as u32,
y: ix,
x: iy,
utf8: [0; 5],
alt,
shift,
ctrl,
evtype,
ypx: -1,
xpx: -1,
}
}
}
impl NcInput {
pub fn char(&self) -> Option<char> {
let cstr = unsafe { CStr::from_ptr(self.utf8.as_ptr()) };
let string = cstr.to_string_lossy();
let raw_char = string.chars().next();
if let Some(ch) = raw_char {
if ch.is_ascii_control() {
None
} else {
Some(ch)
}
} else {
None
}
}
}
pub type NcEvType = u32;
crate::impl_api![
NcEvType,
NcEvTypeApi,
const UNKNOWN: NcEvType = constants::NCEVTYPE_UNKNOWN;,
const PRESS: NcEvType = constants::NCEVTYPE_PRESS;,
const REPEAT: NcEvType = constants::NCEVTYPE_REPEAT;,
const RELEASE: NcEvType = constants::NCEVTYPE_RELEASE;
];
pub type NcMiceEvents = u32;
crate::impl_api![
NcMiceEvents,
NcMiceEventsApi,
const NO_EVENTS: NcMiceEvents = constants::NCMICE_NO_EVENTS;,
const MOVE_EVENTS: NcMiceEvents = constants::NCMICE_MOVE_EVENTS;,
const BUTTON_EVENTS: NcMiceEvents = constants::NCMICE_BUTTON_EVENTS;,
const DRAG_EVENTS: NcMiceEvents = constants::NCMICE_DRAG_EVENTS;,
const ALL_EVENTS: NcMiceEvents = constants::NCMICE_ALL_EVENTS;
];
pub(crate) mod constants {
use crate::{NcEvType, NcMiceEvents};
pub const NCEVTYPE_UNKNOWN: NcEvType = crate::bindings::ffi::ncinput_NCTYPE_UNKNOWN;
pub const NCEVTYPE_PRESS: NcEvType = crate::bindings::ffi::ncinput_NCTYPE_PRESS;
pub const NCEVTYPE_REPEAT: NcEvType = crate::bindings::ffi::ncinput_NCTYPE_REPEAT;
pub const NCEVTYPE_RELEASE: NcEvType = crate::bindings::ffi::ncinput_NCTYPE_RELEASE;
pub const NCMICE_NO_EVENTS: NcMiceEvents = crate::bindings::ffi::NCMICE_NO_EVENTS;
pub const NCMICE_MOVE_EVENTS: NcMiceEvents = crate::bindings::ffi::NCMICE_MOVE_EVENT;
pub const NCMICE_BUTTON_EVENTS: NcMiceEvents = crate::bindings::ffi::NCMICE_BUTTON_EVENT;
pub const NCMICE_DRAG_EVENTS: NcMiceEvents = crate::bindings::ffi::NCMICE_DRAG_EVENT;
pub const NCMICE_ALL_EVENTS: NcMiceEvents = crate::bindings::ffi::NCMICE_ALL_EVENTS;
}