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