output: decouple identity from wayland output
This commit is contained in:
parent
59e4e6dfb7
commit
a20deb0628
13 changed files with 110 additions and 113 deletions
10
output-types/Cargo.toml
Normal file
10
output-types/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "jay-output-types"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "GPL-3.0-only"
|
||||
description = "Output identity types for the Jay compositor"
|
||||
repository = "https://github.com/mahkoh/jay"
|
||||
|
||||
[dependencies]
|
||||
blake3 = "1.8.2"
|
||||
77
output-types/src/lib.rs
Normal file
77
output-types/src/lib.rs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
use {
|
||||
std::{
|
||||
hash::{Hash, Hasher},
|
||||
rc::Rc,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct OutputIdHash(pub [u8; 32]);
|
||||
|
||||
impl OutputIdHash {
|
||||
pub fn hash(t: impl AsRef<[u8]>) -> Self {
|
||||
Self(*blake3::hash(t.as_ref()).as_bytes())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, Debug)]
|
||||
pub struct OutputId {
|
||||
pub _connector: Option<String>,
|
||||
pub manufacturer: String,
|
||||
pub model: String,
|
||||
pub serial_number: String,
|
||||
pub hash: OutputIdHash,
|
||||
}
|
||||
|
||||
impl PartialEq for OutputId {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.hash == other.hash
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for OutputId {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.hash.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl OutputId {
|
||||
pub fn new(
|
||||
connector: impl Into<String>,
|
||||
manufacturer: impl Into<String>,
|
||||
model: impl Into<String>,
|
||||
serial_number: impl Into<String>,
|
||||
) -> Rc<Self> {
|
||||
let connector = connector.into();
|
||||
let manufacturer = manufacturer.into();
|
||||
let model = model.into();
|
||||
let serial_number = serial_number.into();
|
||||
Self::new_(connector, manufacturer, model, serial_number)
|
||||
}
|
||||
|
||||
fn new_(
|
||||
connector: String,
|
||||
manufacturer: String,
|
||||
model: String,
|
||||
serial_number: String,
|
||||
) -> Rc<Self> {
|
||||
let connector = serial_number.is_empty().then_some(connector);
|
||||
let mut hasher = blake3::Hasher::new();
|
||||
hasher.update(&[connector.is_some() as u8]);
|
||||
let mut hash = |s: &str| {
|
||||
hasher.update(&(s.len() as u64).to_le_bytes());
|
||||
hasher.update(s.as_bytes());
|
||||
};
|
||||
connector.as_deref().map(&mut hash);
|
||||
hash(&manufacturer);
|
||||
hash(&model);
|
||||
hash(&serial_number);
|
||||
Rc::new(Self {
|
||||
_connector: connector,
|
||||
manufacturer,
|
||||
model,
|
||||
serial_number,
|
||||
hash: OutputIdHash(*hasher.finalize().as_bytes()),
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue