linux_video_core/impls/
events.rs

1use crate::{calls, types::*, Internal, IsTimestamp, Result};
2use core::mem::MaybeUninit;
3use std::os::unix::io::RawFd;
4
5pub trait IsEventData {
6    const TYPE: EventType;
7}
8
9macro_rules! event_data_impl {
10    ($($type:ident: $event_type:ident,)*) => {
11        $(
12            impl IsEventData for $type {
13                const TYPE: EventType = EventType::$event_type;
14            }
15        )*
16    }
17}
18
19event_data_impl! {
20    EventVsync: Vsync,
21    EventCtrl: Ctrl,
22    EventFrameSync: FrameSync,
23    EventSrcChange: SourceChange,
24    EventMotionDet: MotionDet,
25}
26
27impl Event {
28    /// Try get reference to data of specific type
29    pub fn data<T: IsEventData>(&self) -> Option<&T> {
30        if self.type_ == T::TYPE {
31            Some(unsafe { &*(&self.u as *const _ as *const T) })
32        } else {
33            None
34        }
35    }
36
37    /// Get timestamp
38    pub fn timestamp<T: IsTimestamp>(&self) -> T {
39        T::from_time_spec(self.timestamp)
40    }
41}
42
43impl Internal<Event> {
44    /// Dequeue event
45    pub fn dequeue(fd: RawFd) -> Result<Self> {
46        let event = MaybeUninit::<Event>::uninit();
47
48        unsafe_call!({
49            let mut event = event.assume_init();
50
51            calls::dq_event(fd, &mut event).map(|_| event.into())
52        })
53    }
54}