1use std::fmt;
2use std::mem::{transmute, zeroed};
3
4use sys::js_event;
5
6use crate::{EventType, GenericEvent};
7
8#[derive(Clone, Copy, Eq, Hash, PartialEq)]
13pub struct Event {
14 pub(crate) event: js_event,
15}
16
17impl Event {
18 pub const fn number(self) -> u8 {
20 self.event.number
21 }
22
23 pub fn type_(self) -> EventType {
25 unsafe { transmute(self.event.type_) }
26 }
27}
28
29impl Default for Event {
30 fn default() -> Self {
31 Self {
32 event: unsafe { zeroed() },
33 }
34 }
35}
36
37impl fmt::Debug for Event {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 write!(
40 f,
41 "Event {{ number: {:?}, time: {:?}, type: {:?}, value: {:?} }}",
42 self.number(),
43 self.time(),
44 self.type_(),
45 self.value()
46 )
47 }
48}
49
50impl GenericEvent for Event {
51 fn is_real(&self) -> bool {
52 match self.type_() {
53 EventType::Axis | EventType::Button => true,
54 EventType::AxisSynthetic | EventType::ButtonSynthetic => false,
55 }
56 }
57
58 fn is_synthetic(&self) -> bool {
59 match self.type_() {
60 EventType::Axis | EventType::Button => false,
61 EventType::AxisSynthetic | EventType::ButtonSynthetic => true,
62 }
63 }
64
65 fn time(&self) -> u32 {
66 self.event.time
67 }
68
69 fn value(&self) -> i16 {
70 self.event.value
71 }
72}