Skip to main content

joydev_rs/
device_event.rs

1use crate::{AxisEvent, ButtonEvent, Device, Event, EventType, GenericEvent};
2
3/// Wrapped event
4///
5/// Device events wrap axis and button events. This is intended for use with the builtin [`Device`](struct.Device.html)
6/// abstraction.
7#[derive(Clone, Copy, Debug)]
8pub enum DeviceEvent {
9	/// Axis event
10	Axis(AxisEvent),
11	/// Button event
12	Button(ButtonEvent),
13}
14
15impl DeviceEvent {
16	/// Creates new `DeviceEvent` from `event` using mappings form `device`.
17	pub fn from_event(device: &Device, event: Event) -> Self {
18		match event.type_() {
19			EventType::Axis => Self::Axis(AxisEvent::new(
20				device.axis_mapping_at(event.number()),
21				false,
22				event.time(),
23				event.value(),
24			)),
25			EventType::AxisSynthetic => Self::Axis(AxisEvent::new(
26				device.axis_mapping_at(event.number()),
27				true,
28				event.time(),
29				event.value(),
30			)),
31			EventType::Button => Self::Button(ButtonEvent::new(
32				device.button_mapping_at(event.number()),
33				false,
34				event.time(),
35				event.value(),
36			)),
37			EventType::ButtonSynthetic => Self::Button(ButtonEvent::new(
38				device.button_mapping_at(event.number()),
39				true,
40				event.time(),
41				event.value(),
42			)),
43		}
44	}
45}
46
47impl GenericEvent for DeviceEvent {
48	fn is_real(&self) -> bool {
49		match self {
50			Self::Axis(ref event) => event.is_real(),
51			Self::Button(ref event) => event.is_real(),
52		}
53	}
54
55	fn is_synthetic(&self) -> bool {
56		match self {
57			Self::Axis(ref event) => event.is_synthetic(),
58			Self::Button(ref event) => event.is_synthetic(),
59		}
60	}
61
62	fn time(&self) -> u32 {
63		match self {
64			Self::Axis(ref event) => event.time(),
65			Self::Button(ref event) => event.time(),
66		}
67	}
68
69	fn value(&self) -> i16 {
70		match self {
71			Self::Axis(ref event) => event.value(),
72			Self::Button(ref event) => event.value(),
73		}
74	}
75}