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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
use enums::*;
use libc::{c_char, c_uint};
use raw;
use std::fmt;
use std::ffi::{CStr, CString};
pub(crate) fn ptr_to_str(ptr: *const c_char) -> Option<&'static str> {
let slice : Option<&CStr> = unsafe {
if ptr.is_null() {
return None
}
Some(CStr::from_ptr(ptr))
};
match slice {
None => None,
Some(s) => {
let buf : &[u8] = s.to_bytes();
Some(std::str::from_utf8(buf).unwrap())
}
}
}
pub struct EventTypeIterator {
current: EventType
}
pub struct EventCodeIterator {
current: EventCode
}
pub struct InputPropIterator {
current: InputProp
}
pub fn event_code_to_int(event_code: &EventCode) -> (c_uint, c_uint) {
let mut ev_type: c_uint = 0;
let mut ev_code: c_uint = 0;
match event_code.clone() {
EventCode::EV_SYN(code) => {
ev_type = EventType::EV_SYN as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_KEY(code) => {
ev_type = EventType::EV_KEY as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_REL(code) => {
ev_type = EventType::EV_REL as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_ABS(code) => {
ev_type = EventType::EV_ABS as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_MSC(code) => {
ev_type = EventType::EV_MSC as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_SW(code) => {
ev_type = EventType::EV_SW as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_LED(code) => {
ev_type = EventType::EV_LED as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_SND(code) => {
ev_type = EventType::EV_SND as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_REP(code) => {
ev_type = EventType::EV_REP as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_FF(code) => {
ev_type = EventType::EV_FF as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_FF_STATUS(code) => {
ev_type = EventType::EV_FF_STATUS as c_uint;
ev_code = code as c_uint;
},
EventCode::EV_UNK { event_type, event_code } => {
ev_type = event_type as c_uint;
ev_code = event_code as c_uint;
},
_ => {
warn!("Event code not found");
}
}
(ev_type, ev_code)
}
pub fn int_to_event_code(event_type: c_uint, event_code: c_uint) -> EventCode {
let ev_type: EventType = int_to_event_type(event_type as u32).unwrap();
let ev_code = match ev_type {
EventType::EV_SYN => match int_to_ev_syn(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_SYN(k)),
},
EventType::EV_KEY => match int_to_ev_key(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_KEY(k)),
},
EventType::EV_ABS => match int_to_ev_abs(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_ABS(k)),
},
EventType::EV_REL => match int_to_ev_rel(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_REL(k)),
},
EventType::EV_MSC => match int_to_ev_msc(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_MSC(k)),
},
EventType::EV_SW => match int_to_ev_sw(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_SW(k)),
},
EventType::EV_LED => match int_to_ev_led(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_LED(k)),
},
EventType::EV_SND => match int_to_ev_snd(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_SND(k)),
},
EventType::EV_REP => match int_to_ev_rep(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_REP(k)),
},
EventType::EV_FF => match int_to_ev_ff(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_FF(k)),
},
EventType::EV_PWR => Some(EventCode::EV_PWR),
EventType::EV_FF_STATUS => match int_to_ev_ff(event_code as u32) {
None => None,
Some(k) => Some(EventCode::EV_FF_STATUS(k)),
},
EventType::EV_UNK => None,
EventType::EV_MAX => Some(EventCode::EV_MAX),
};
match ev_code {
Some(c) => c,
None => EventCode::EV_UNK {
event_type: event_type as u32,
event_code: event_code as u32,
},
}
}
impl fmt::Display for EventType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ptr_to_str(unsafe {
raw::libevdev_event_type_get_name(self.clone() as c_uint)
}).unwrap_or(""))
}
}
impl fmt::Display for EventCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (ev_type, ev_code) = event_code_to_int(self);
write!(f, "{}", ptr_to_str(unsafe {
raw::libevdev_event_code_get_name(ev_type, ev_code)
}).unwrap_or(""))
}
}
impl fmt::Display for InputProp {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ptr_to_str(unsafe {
raw::libevdev_property_get_name(self.clone() as c_uint)
}).unwrap_or(""))
}
}
impl EventType {
pub fn iter(&self) -> EventTypeIterator {
EventTypeIterator { current: self.clone() }
}
pub fn from_str(name: &str) -> Option<EventType> {
let name = CString::new(name).unwrap();
let result = unsafe {
raw::libevdev_event_type_from_name(name.as_ptr())
};
match result {
-1 => None,
k => int_to_event_type(k as u32),
}
}
pub fn get_max(ev_type: &EventType) -> Option<i32> {
let result = unsafe {
raw::libevdev_event_type_get_max(ev_type.clone() as c_uint)
};
match result {
-1 => None,
k => Some(k),
}
}
}
impl EventCode {
pub fn iter(&self) -> EventCodeIterator {
EventCodeIterator { current: self.clone() }
}
pub fn from_str(ev_type: &EventType, name: &str) -> Option<EventCode> {
let name = CString::new(name).unwrap();
let result = unsafe {
raw::libevdev_event_code_from_name(ev_type.clone() as c_uint, name.as_ptr())
};
match result {
-1 => None,
k => Some(int_to_event_code(ev_type.clone() as u32, k as u32)),
}
}
}
impl InputProp {
pub fn iter(&self) -> InputPropIterator {
InputPropIterator { current: self.clone() }
}
pub fn from_str(name: &str) -> Option<InputProp> {
let name = CString::new(name).unwrap();
let result = unsafe {
raw::libevdev_property_from_name(name.as_ptr())
};
match result {
-1 => None,
k => int_to_input_prop(k as u32),
}
}
}
impl Iterator for EventTypeIterator {
type Item = EventType;
fn next(&mut self) -> Option<EventType> {
match self.current {
EventType::EV_MAX => {
return None;
}
_ => {
let mut raw_code = (self.current.clone() as u32) + 1;
loop {
match int_to_event_type(raw_code) {
Some(EventType::EV_UNK) => raw_code += 1,
Some(x) => {
let code = self.current.clone();
self.current = x;
return Some(code);
}
None => raw_code += 1,
}
}
}
}
}
}
impl Iterator for EventCodeIterator {
type Item = EventCode;
fn next(&mut self) -> Option<EventCode> {
match self.current.clone() {
EventCode::EV_SYN(code) => {
match code {
EV_SYN::SYN_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_KEY(EV_KEY::KEY_RESERVED);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_syn(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_SYN(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_KEY(code) => {
match code {
EV_KEY::KEY_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_REL(EV_REL::REL_X);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_key(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_KEY(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_REL(code) => {
match code {
EV_REL::REL_MAX=> {
let ev_code = self.current.clone();
self.current = EventCode::EV_ABS(EV_ABS::ABS_X);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_rel(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_REL(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_ABS(code) => {
match code {
EV_ABS::ABS_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_MSC(EV_MSC::MSC_SERIAL);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_abs(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_ABS(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_MSC(code) => {
match code {
EV_MSC::MSC_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_SW(EV_SW::SW_LID);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_msc(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_MSC(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_SW(code) => {
match code {
EV_SW::SW_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_LED(EV_LED::LED_NUML);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_sw(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_SW(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_LED(code) => {
match code {
EV_LED::LED_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_SND(EV_SND::SND_CLICK);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_led(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_LED(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_SND(code) => {
match code {
EV_SND::SND_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_REP(EV_REP::REP_DELAY);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_snd(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_SND(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_REP(code) => {
match code {
EV_REP::REP_MAX => {
let ev_code = self.current.clone();
self.current = EventCode::EV_FF(EV_FF::FF_STATUS_STOPPED);
return Some(ev_code);
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_rep(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_REP(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
EventCode::EV_FF(code) => {
match code {
EV_FF::FF_MAX => {
return None
}
_ => {
let mut raw_code = (code as u32) + 1;
loop {
match int_to_ev_ff(raw_code) {
Some(x) => {
let ev_code = self.current.clone();
self.current = EventCode::EV_FF(x);
return Some(ev_code);
}
None => raw_code += 1,
}
}
}
}
}
_ => None,
}
}
}
impl Iterator for InputPropIterator {
type Item = InputProp;
fn next(&mut self) -> Option<InputProp> {
match self.current {
InputProp::INPUT_PROP_MAX => {
return None;
}
_ => {
let mut raw_enum = (self.current.clone() as u32) + 1;
loop {
match int_to_input_prop(raw_enum) {
Some(x) => {
let prop = self.current.clone();
self.current = x;
return Some(prop);
}
None => raw_enum += 1,
}
}
}
}
}
}