#[cfg_attr(feature = "stable_polyfill", derive(ptr_meta::Pointee))]
#[repr(C)]
pub struct DrmEvent {
pub hdr: DrmEventHeader,
pub body: [u8],
}
impl DrmEvent {
const HEADER_LEN: usize = core::mem::size_of::<DrmEventHeader>();
pub unsafe fn from_event_header<'a>(hdr: &'a DrmEventHeader) -> &'a DrmEvent {
let ptr = hdr as *const _ as *const ();
#[cfg(feature = "stable_polyfill")]
let ptr = ptr_meta::from_raw_parts(ptr, hdr.len as usize - Self::HEADER_LEN);
#[cfg(not(feature = "stable_polyfill"))]
let ptr = core::ptr::from_raw_parts(ptr, hdr.len as usize - Self::HEADER_LEN);
&*ptr
}
pub fn from_bytes<'a>(buf: &'a [u8]) -> Option<(&'a DrmEvent, &'a [u8])> {
if buf.len() < Self::HEADER_LEN {
return None;
}
let hdr_bytes = &buf[0..Self::HEADER_LEN];
let hdr = unsafe { &*(hdr_bytes.as_ptr() as *const DrmEventHeader) };
let claimed_len = hdr.len as usize;
if buf.len() < claimed_len {
return None;
}
let ret = unsafe { Self::from_event_header(hdr) };
Some((ret, &buf[claimed_len..]))
}
pub fn body_len(&self) -> usize {
let ptr = self as *const DrmEvent;
#[cfg(not(feature = "stable_polyfill"))]
let (_, body_len) = ptr.to_raw_parts();
#[cfg(feature = "stable_polyfill")]
let (_, body_len) = ptr_meta::to_raw_parts(ptr);
body_len
}
#[inline(always)]
pub fn body_bytes(&self) -> &[u8] {
&self.body
}
#[inline(always)]
pub fn body_ptr<T: Sized>(&self) -> *const T {
&self.body as *const _ as *const T
}
pub unsafe fn body_as<T>(&self) -> Option<&T> {
let min_size = core::mem::size_of::<T>();
if self.body_len() < min_size {
return None;
}
Some(self.body_as_unchecked::<T>())
}
#[inline(always)]
pub unsafe fn body_as_unchecked<T>(&self) -> &T {
let ptr = self.body_ptr::<T>();
&*ptr
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrmEventHeader {
pub typ: u32,
pub len: u32,
}
pub const DRM_EVENT_VBLANK: u32 = 0x01;
pub const DRM_EVENT_FLIP_COMPLETE: u32 = 0x02;
pub const DRM_EVENT_CRTC_SEQUENCE: u32 = 0x03;
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrmEventVblank {
pub user_data: u64,
pub tv_sec: u32,
pub tv_usec: u32,
pub sequence: u32,
pub crtc_id: u32, }
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct DrmEventCrtcSequence {
pub user_data: u64,
pub time_ns: i64,
pub sequence: u64,
}
pub fn events_from_bytes<'a>(buf: &'a [u8]) -> impl Iterator<Item = &'a DrmEvent> + 'a {
DrmEventsFromBytes { remain: buf }
}
struct DrmEventsFromBytes<'a> {
remain: &'a [u8],
}
impl<'a> Iterator for DrmEventsFromBytes<'a> {
type Item = &'a DrmEvent;
fn next(&mut self) -> Option<Self::Item> {
let (ret, remain) = DrmEvent::from_bytes(self.remain)?;
self.remain = remain;
Some(ret)
}
}