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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use std::convert::TryFrom;
use sys::input_event;
use ::{
	EventTime, RangeError, KeyState,
	EventKind, SynchronizeKind, Key, RelativeAxis, AbsoluteAxis,
	SwitchKind, MiscKind, LedKind, AutorepeatKind, SoundKind, UInputKind,
};

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// Synchronization events are used by evdev to group events or convey other
/// out-of-band information.
pub struct SynchronizeEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The type of synchronization event.
	pub kind: SynchronizeKind,
	/// An associated value with the event.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// An event that indicates the state of a key has changed.
pub struct KeyEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The key that triggered the event.
	pub key: Key,
	/// The value of the event.
	pub value: KeyState,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// Events that occur when the state of a relative axis is changed.
pub struct RelativeEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The axis associated with the event.
	pub axis: RelativeAxis,
	/// The relative distance of the axis event.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// Events that occur when the state of an absolute axis is changed.
pub struct AbsoluteEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The axis associated with the event.
	pub axis: AbsoluteAxis,
	/// The absolute value of the axis.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// Special switch events.
pub struct SwitchEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The switch that triggered the event.
	pub switch: SwitchKind,
	/// The state of the switch.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// Miscellaneous events.
pub struct MiscEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The kind of miscellaneous event.
	pub kind: MiscKind,
	/// The state/value of the event.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// An event that indicates whether the specified LED should turn on/off.
pub struct LedEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The led associated with the event.
	pub led: LedKind,
	/// The state of the led.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// An event that configures the autorepeat behaviour of the input device.
pub struct AutorepeatEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The kind of autorepeat event.
	pub kind: AutorepeatKind,
	/// The value of the event.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// An event that indicates the device should play a specified sound.
pub struct SoundEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The sound to play.
	pub sound: SoundKind,
	/// The value or state associated with the sound.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// A special event type used to send force-feedback events to uinput.
pub struct UInputEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	event: EventKind,
	/// The code of the event.
	pub code: UInputKind,
	/// The unique request ID.
	pub value: i32,
}

#[repr(C)]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
/// A generic event.
pub struct InputEvent {
	/// The timestamp associated with the event.
	pub time: EventTime,
	/// The type of event that occurred.
	pub kind: EventKind,
	/// The code of the event.
	///
	/// The meaning of this code depends on the `kind` of event. Using the typed
	/// events via `Event` and `EventRef` is recommended.
	pub code: u16,
	/// The value of the event.
	///
	/// The interpretation of this value depends on the `kind` of event.
	pub value: i32,
}

impl InputEvent {
	/// Reinterpret this event as an array of bytes
	pub fn as_bytes(&self) -> &[u8; core::mem::size_of::<InputEvent>()] {
		unsafe {
			core::mem::transmute(self)
		}
	}
}

impl SynchronizeEvent {
	/// Synchronization event
	pub const fn report(time: EventTime) -> Self {
		Self::new(time, SynchronizeKind::Report, 0)
	}
}

macro_rules! event_impl {
	(@impl_event InputEvent $($tt:tt)*) => { };
	(@impl_event $name:ident $code:ident $kind:path, $codekind:ident, $valuekind:ident) => {
		impl GenericEvent for $name {
			fn event_kind(&self) -> EventKind { $kind }
			fn time(&self) -> &EventTime { &self.time }
			fn code(&self) -> u16 { self.$code as _ }
			fn value(&self) -> i32 { self.value.into() }

			fn from_ref(event: &InputEvent) -> Result<&Self, RangeError> {
				$codekind::from_code(event.code)
					.and_then(move |_| if event.kind == $kind {
						Ok(unsafe { Self::from_event(event) })
					} else {
						Err(Default::default())
					})
			}

			fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError> {
				$codekind::from_code(event.code)
					.and_then(move |_| if event.kind == $kind {
						Ok(unsafe { Self::from_event_mut(event) })
					} else {
						Err(Default::default())
					})
			}
		}

		impl<'a> TryFrom<&'a InputEvent> for &'a $name {
			type Error = RangeError;

			fn try_from(event: &'a InputEvent) -> Result<Self, Self::Error> {
				$name::from_ref(event)
			}
		}

		impl TryFrom<InputEvent> for $name {
			type Error = RangeError;

			fn try_from(event: InputEvent) -> Result<Self, Self::Error> {
				$name::from_ref(&event).map(|&e| e)
			}
		}

		impl<'a> TryFrom<&'a mut InputEvent> for &'a mut $name {
			type Error = RangeError;

			fn try_from(event: &'a mut InputEvent) -> Result<Self, Self::Error> {
				$name::from_mut(event)
			}
		}

		impl<'a> From<&'a $name> for &'a InputEvent {
			fn from(event: &'a $name) -> Self {
				event.as_event()
			}
		}

		impl<'a> From<&'a $name> for InputEvent {
			fn from(event: &'a $name) -> Self {
				event.as_event().clone()
			}
		}

		impl From<$name> for InputEvent {
			fn from(event: $name) -> Self {
				From::from(&event)
			}
		}

		impl AsRef<InputEvent> for $name {
			fn as_ref(&self) -> &InputEvent {
				self.as_event()
			}
		}

		impl<'a> $name {
			/// Creates a new event from the given code and value.
			pub const fn new(time: EventTime, $code: $codekind, value: $valuekind) -> Self {
				$name {
					time,
					event: $kind,
					$code: $code,
					value,
				}
			}

			/// Reinterpret a generic event without checking for validity.
			pub unsafe fn from_event<E: AsRef<input_event>>(event: &E) -> &Self {
				let raw = event.as_ref() as *const _ as *const _;
				&*raw
			}

			/// Reinterpret a mutable generic event without checking for validity.
			pub unsafe fn from_event_mut(event: &mut InputEvent) -> &mut Self {
				let raw = event as *mut _ as *mut _;
				&mut *raw
			}

			/// A generic input event reference.
			pub fn as_event(&self) -> &InputEvent {
				let raw = self as *const _ as *const _;
				unsafe { &*raw }
			}

			/// A mutable generic input event reference.
			pub unsafe fn as_event_mut(&mut self) -> &mut InputEvent {
				let raw = self as *mut _ as *mut _;
				&mut *raw
			}
		}
	};
	(@impl $name:ident $code:ident $kind:path, $codekind:ident, $valuekind:ident) => {
		event_impl! {
			@impl_event $name $code $kind, $codekind, $valuekind
		}

		impl AsRef<input_event> for $name {
			fn as_ref(&self) -> &input_event {
				let raw = self as *const _ as *const _;
				unsafe { &*raw }
			}
		}
	};
	($(struct $name:ident : $kind:path { $code:ident: $codekind:ident, value: $valuekind:ident })*) => {
		$(
			event_impl! {
				@impl $name $code $kind, $codekind, $valuekind
			}
		)*
	};
}

/// A generic linux input event.
pub trait GenericEvent: AsRef<InputEvent> + AsRef<input_event> {
	/// The event kind.
	fn event_kind(&self) -> EventKind;
	/// The timestamp associated with the event.
	fn time(&self) -> &EventTime;
	/// The type code value of the event.
	fn code(&self) -> u16;
	/// The value associated with the event.
	fn value(&self) -> i32;

	/// Interprets a generic event reference into a concrete event type.
	fn from_ref(event: &InputEvent) -> Result<&Self, RangeError>;
	/// Interprets a mutable generic event reference into a concrete event type.
	fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError>;
}

event_impl! {
	struct SynchronizeEvent : EventKind::Synchronize { kind: SynchronizeKind, value: i32 }
	struct KeyEvent : EventKind::Key { key: Key, value: KeyState }
	struct RelativeEvent : EventKind::Relative { axis: RelativeAxis, value: i32 }
	struct AbsoluteEvent : EventKind::Absolute { axis: AbsoluteAxis, value: i32 }
	struct SwitchEvent : EventKind::Switch { switch: SwitchKind, value: i32 }
	struct MiscEvent : EventKind::Misc { kind: MiscKind, value: i32 }
	struct LedEvent : EventKind::Led { led: LedKind, value: i32 }
	struct AutorepeatEvent : EventKind::Autorepeat { kind: AutorepeatKind, value: i32 }
	struct SoundEvent : EventKind::Sound { sound: SoundKind, value: i32 }
	struct UInputEvent : EventKind::UInput { code: UInputKind, value: i32 }
	struct InputEvent : Unknown { code: u16, value: i32 }
}

impl GenericEvent for InputEvent {
	fn event_kind(&self) -> EventKind { self.kind }
	fn time(&self) -> &EventTime { &self.time }
	fn code(&self) -> u16 { self.code }
	fn value(&self) -> i32 { self.value }

	fn from_ref(event: &InputEvent) -> Result<&Self, RangeError> {
		Ok(event)
	}

	fn from_mut(event: &mut InputEvent) -> Result<&mut Self, RangeError> {
		Ok(event)
	}
}

impl AsRef<InputEvent> for InputEvent {
	fn as_ref(&self) -> &InputEvent {
		self
	}
}

impl InputEvent {
	/// Reinterprets a raw `input_event`.
	pub fn from_raw(event: &input_event) -> Result<&Self, RangeError> {
		EventKind::from_type(event.type_).map(|_| {
			let raw = event as *const _ as *const _;
			unsafe { &*raw }
		})
	}

	/// Reinterprets a raw `input_event` into a mutable reference.
	pub fn from_raw_mut(event: &mut input_event) -> Result<&mut Self, RangeError> {
		EventKind::from_type(event.type_).map(|_| {
			let raw = event as *mut _ as *mut _;
			unsafe { &mut *raw }
		})
	}

	/// Reinterprets the event as a raw `input_event`.
	pub fn as_raw(&self) -> &input_event {
		let raw = self as *const _ as *const _;
		unsafe { &*raw }
	}

	/// Reinterprets the event as a mutable raw `input_event`.
	pub unsafe fn as_raw_mut(&mut self) -> &mut input_event {
		let raw = self as *mut _ as *mut _;
		&mut *raw
	}
}

macro_rules! input_event_enum {
	($($variant:ident($ty:ident),)*) => {
		/// An owned and typed input event.
		#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
		#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
		pub enum Event {
		$(
			#[allow(missing_docs)]
			$variant($ty),
		)*
			/// Unknown event type.
			Unknown(InputEvent),
		}

		/// A reference to an input event.
		#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
		pub enum EventRef<'a> {
		$(
			#[allow(missing_docs)]
			$variant(&'a $ty),
		)*
			/// Unknown event type.
			Unknown(&'a InputEvent),
		}

		/// A mutable reference to an input event.
		#[derive(PartialEq, Eq, Hash, Debug)]
		pub enum EventMut<'a> {
		$(
			#[allow(missing_docs)]
			$variant(&'a mut $ty),
		)*
			/// Unknown event type.
			Unknown(&'a mut InputEvent),
		}

		impl Event {
			/// Converts a generic `InputEvent` to a typed event.
			pub fn new(event: InputEvent) -> Result<Self, RangeError> {
				match event.kind {
				$(
					EventKind::$variant => $ty::from_ref(&event).map(Clone::clone).map(Event::$variant),
				)*
					_ => Ok(Event::Unknown(event)),
				}
			}
		}

		impl<'a> EventRef<'a> {
			/// Wraps the generic `InputEvent` into an event.
			pub fn new(event: &'a InputEvent) -> Result<Self, RangeError> {
				match event.kind {
				$(
					EventKind::$variant => $ty::from_ref(event).map(EventRef::$variant),
				)*
					_ => Ok(EventRef::Unknown(event)),
				}
			}
		}

		impl<'a> EventMut<'a> {
			/// Wraps the generic `InputEvent` into a mutable event.
			pub fn new(event: &'a mut InputEvent) -> Result<Self, RangeError> {
				match event.kind {
				$(
					EventKind::$variant => $ty::from_mut(event).map(EventMut::$variant),
				)*
					_ => Ok(EventMut::Unknown(event)),
				}
			}
		}

		$(
		impl From<$ty> for Event {
			fn from(event: $ty) -> Self {
				Event::$variant(event)
			}
		}
		)*

		$(
		impl<'a> From<&'a $ty> for EventRef<'a> {
			fn from(event: &'a $ty) -> Self {
				EventRef::$variant(event)
			}
		}
		)*

		$(
		impl<'a> From<&'a mut $ty> for EventMut<'a> {
			fn from(event: &'a mut $ty) -> Self {
				EventMut::$variant(event)
			}
		}
		)*

		impl From<Event> for InputEvent {
			fn from(event: Event) -> Self {
				match event {
				$(
					Event::$variant(ref event) => <&InputEvent as From<_>>::from(event).clone(),
				)*
					Event::Unknown(event) => event,
				}
			}
		}

		impl<'a> From<&'a Event> for EventRef<'a> {
			fn from(event: &'a Event) -> Self {
				match *event {
				$(
					Event::$variant(ref event) => EventRef::$variant(event),
				)*
					Event::Unknown(ref event) => EventRef::Unknown(event),
				}
			}
		}

		impl<'a> From<&'a mut Event> for EventMut<'a> {
			fn from(event: &'a mut Event) -> Self {
				match *event {
				$(
					Event::$variant(ref mut event) => EventMut::$variant(event),
				)*
					Event::Unknown(ref mut event) => EventMut::Unknown(event),
				}
			}
		}

		impl<'a> From<EventRef<'a>> for &'a InputEvent {
			fn from(event: EventRef<'a>) -> Self {
				match event {
				$(
					EventRef::$variant(event) => event.as_ref(),
				)*
					EventRef::Unknown(event) => event,
				}
			}
		}

		impl<'a> From<&'a EventMut<'a>> for &'a InputEvent {
			fn from(event: &'a EventMut<'a>) -> Self {
				match *event {
				$(
					EventMut::$variant(ref event) => event.as_ref(),
				)*
					EventMut::Unknown(ref event) => event,
				}
			}
		}

		impl AsRef<InputEvent> for Event {
			fn as_ref(&self) -> &InputEvent {
				match *self {
				$(
					Event::$variant(ref event) => event.as_ref(),
				)*
					Event::Unknown(ref event) => event.as_ref(),
				}
			}
		}

		impl<'a> AsRef<InputEvent> for EventRef<'a> {
			fn as_ref(&self) -> &InputEvent {
				From::from(*self)
			}
		}

		impl<'a> AsRef<InputEvent> for EventMut<'a> {
			fn as_ref(&self) -> &InputEvent {
				From::from(self)
			}
		}
	};
}

input_event_enum! {
	Synchronize(SynchronizeEvent),
	Key(KeyEvent),
	Relative(RelativeEvent),
	Absolute(AbsoluteEvent),
	Switch(SwitchEvent),
	Misc(MiscEvent),
	Led(LedEvent),
	Autorepeat(AutorepeatEvent),
	Sound(SoundEvent),
	UInput(UInputEvent),
}