drivers/graphics: Move enum variant fixed size name computation to property add time
This commit is contained in:
@@ -11,6 +11,19 @@ use syscall::{Error, Result, EINVAL};
|
||||
use crate::kms::objects::{KmsObjectId, KmsObjects};
|
||||
use crate::GraphicsAdapter;
|
||||
|
||||
fn fixed_size_name<const N: usize>(context: &str, name: &str) -> [c_char; N] {
|
||||
if name.len() > N {
|
||||
panic!("{context} {name} is too long");
|
||||
}
|
||||
|
||||
let mut name_bytes = [0; N];
|
||||
for (to, &from) in name_bytes.iter_mut().zip(name.as_bytes()) {
|
||||
*to = from as c_char;
|
||||
}
|
||||
|
||||
name_bytes
|
||||
}
|
||||
|
||||
impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
pub fn add_property(
|
||||
&mut self,
|
||||
@@ -19,28 +32,14 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
atomic: bool,
|
||||
kind: KmsPropertyKind,
|
||||
) -> KmsObjectId {
|
||||
if name.len() > DRM_PROP_NAME_LEN as usize {
|
||||
panic!("Property name {name} is too long");
|
||||
}
|
||||
|
||||
match &kind {
|
||||
KmsPropertyKind::Range(start, end) => assert!(start < end),
|
||||
KmsPropertyKind::Enum(variants) => {
|
||||
KmsPropertyKind::Enum(_variants) => {
|
||||
// FIXME check duplicate variant numbers
|
||||
for (variant_name, _) in variants {
|
||||
if variant_name.len() > DRM_PROP_NAME_LEN as usize {
|
||||
panic!("Property variant name {variant_name} is too long");
|
||||
}
|
||||
}
|
||||
}
|
||||
KmsPropertyKind::Blob => {}
|
||||
KmsPropertyKind::Bitmask(bitmask_flags) => {
|
||||
KmsPropertyKind::Bitmask(_bitmask_flags) => {
|
||||
// FIXME check overlapping flag numbers
|
||||
for (flag_name, _) in bitmask_flags {
|
||||
if flag_name.len() > DRM_PROP_NAME_LEN as usize {
|
||||
panic!("Property bitflag name {flag_name} is too long");
|
||||
}
|
||||
}
|
||||
}
|
||||
KmsPropertyKind::Object => {}
|
||||
KmsPropertyKind::SignedRange(start, end) => assert!(start < end),
|
||||
@@ -52,7 +51,7 @@ impl<T: GraphicsAdapter> KmsObjects<T> {
|
||||
}
|
||||
|
||||
self.add(KmsProperty {
|
||||
name: name_bytes,
|
||||
name: fixed_size_name("Property name", name),
|
||||
immutable,
|
||||
atomic,
|
||||
kind,
|
||||
@@ -107,9 +106,9 @@ pub struct KmsProperty {
|
||||
#[derive(Debug)]
|
||||
pub enum KmsPropertyKind {
|
||||
Range(u64, u64),
|
||||
Enum(Vec<(&'static str, u64)>),
|
||||
Enum(Vec<([c_char; DRM_PROP_NAME_LEN as usize], u64)>),
|
||||
Blob,
|
||||
Bitmask(Vec<(&'static str, u64)>),
|
||||
Bitmask(Vec<([c_char; DRM_PROP_NAME_LEN as usize], u64)>),
|
||||
Object,
|
||||
SignedRange(i64, i64),
|
||||
}
|
||||
@@ -146,7 +145,9 @@ macro_rules! define_properties {
|
||||
KmsPropertyKind::Range($start, $end)
|
||||
};
|
||||
(@prop_kind enum { $($variant:ident = $value:expr,)* }) => {
|
||||
KmsPropertyKind::Enum(vec![$((stringify!($variant), $value)),*])
|
||||
KmsPropertyKind::Enum(vec![
|
||||
$((fixed_size_name("Property variant name", stringify!($variant)), $value)),*]
|
||||
)
|
||||
};
|
||||
(@prop_kind blob) => {
|
||||
KmsPropertyKind::Blob
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#![feature(macro_metavar_expr)]
|
||||
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::ffi::c_char;
|
||||
use std::fmt::Debug;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Write};
|
||||
@@ -14,7 +13,6 @@ use drm_sys::{
|
||||
drm_mode_modeinfo, drm_mode_property_enum, DRM_MODE_CURSOR_BO, DRM_MODE_CURSOR_MOVE,
|
||||
DRM_MODE_PROP_ATOMIC, DRM_MODE_PROP_BITMASK, DRM_MODE_PROP_BLOB, DRM_MODE_PROP_ENUM,
|
||||
DRM_MODE_PROP_IMMUTABLE, DRM_MODE_PROP_OBJECT, DRM_MODE_PROP_RANGE, DRM_MODE_PROP_SIGNED_RANGE,
|
||||
DRM_PROP_NAME_LEN,
|
||||
};
|
||||
use inputd::{DisplayHandle, VtEventKind};
|
||||
use libredox::Fd;
|
||||
@@ -677,18 +675,7 @@ impl<T: GraphicsAdapter> SchemeSync for GraphicsSchemeInner<T> {
|
||||
data.set_enum_blob_ptr(
|
||||
&variants
|
||||
.iter()
|
||||
.map(|&(name, value)| {
|
||||
let mut name_bytes = [0; DRM_PROP_NAME_LEN as usize];
|
||||
for (to, &from) in
|
||||
name_bytes.iter_mut().zip(name.as_bytes())
|
||||
{
|
||||
*to = from as c_char;
|
||||
}
|
||||
drm_mode_property_enum {
|
||||
name: name_bytes,
|
||||
value,
|
||||
}
|
||||
})
|
||||
.map(|&(name, value)| drm_mode_property_enum { name, value })
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
@@ -708,18 +695,7 @@ impl<T: GraphicsAdapter> SchemeSync for GraphicsSchemeInner<T> {
|
||||
data.set_enum_blob_ptr(
|
||||
&bitmask_flags
|
||||
.iter()
|
||||
.map(|&(name, value)| {
|
||||
let mut name_bytes = [0; DRM_PROP_NAME_LEN as usize];
|
||||
for (to, &from) in
|
||||
name_bytes.iter_mut().zip(name.as_bytes())
|
||||
{
|
||||
*to = from as c_char;
|
||||
}
|
||||
drm_mode_property_enum {
|
||||
name: name_bytes,
|
||||
value,
|
||||
}
|
||||
})
|
||||
.map(|&(name, value)| drm_mode_property_enum { name, value })
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user