1
0
Fork 0
forked from wry/wry

backend: move interface ids into type crates

This commit is contained in:
kossLAN 2026-05-29 12:51:31 -04:00
parent 46d39becd4
commit be1511a7be
No known key found for this signature in database
5 changed files with 95 additions and 46 deletions

View file

@ -1,10 +1,92 @@
use {
jay_utils::numcell::NumCell,
std::{
fmt::{Display, Formatter},
hash::{Hash, Hasher},
rc::Rc,
},
};
macro_rules! linear_ids {
($ids:ident, $id:ident $(,)?) => {
linear_ids!($ids, $id, u32);
};
($ids:ident, $id:ident, $ty:ty $(,)?) => {
#[derive(Debug)]
pub struct $ids {
next: NumCell<$ty>,
}
impl Default for $ids {
fn default() -> Self {
Self {
next: NumCell::new(1),
}
}
}
impl $ids {
pub fn next(&self) -> $id {
$id(self.next.fetch_add(1))
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct $id($ty);
impl $id {
pub fn raw(&self) -> $ty {
self.0
}
pub fn from_raw(id: $ty) -> Self {
Self(id)
}
}
impl Display for $id {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
};
}
linear_ids!(ConnectorIds, ConnectorId);
linear_ids!(DrmDeviceIds, DrmDeviceId);
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
pub struct Mode {
pub width: i32,
pub height: i32,
pub refresh_rate_millihz: u32,
}
impl Mode {
pub fn refresh_nsec(&self) -> u64 {
match self.refresh_rate_millihz {
0 => u64::MAX,
n => 1_000_000_000_000 / (n as u64),
}
}
pub fn size(&self) -> (i32, i32) {
(self.width, self.height)
}
}
impl Display for Mode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}x{}@{}",
self.width,
self.height,
self.refresh_rate_millihz as f64 / 1000.0,
)
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct OutputIdHash(pub [u8; 32]);