libdrm_amdgpu_sys/drm_mode/
object_property.rs

1use crate::{bindings, query_error, LibDrm};
2use core::ptr::addr_of;
3
4pub use bindings::drmModeObjectPropertiesPtr;
5use crate::drmModeProperty;
6
7#[derive(Clone)]
8pub struct drmModeObjectProperties {
9    pub(crate) ptr: drmModeObjectPropertiesPtr,
10    pub(crate) lib: LibDrm,
11}
12
13impl LibDrm {
14    pub fn get_drm_mode_object_properties(
15        &self,
16        fd: i32,
17        object_id: u32,
18        object_type: u32,
19    ) -> Option<drmModeObjectProperties> {
20        #[cfg(feature = "link_drm")]
21        let func = bindings::drmModeObjectGetProperties;
22        #[cfg(feature = "dynamic_loading")]
23        let func = self.libdrm.drmModeObjectGetProperties;
24
25        let obj_ptr = unsafe { func(fd, object_id, object_type) };
26
27        if obj_ptr.is_null() {
28            None
29        } else {
30            Some(drmModeObjectProperties {
31                ptr: obj_ptr,
32                lib: self.clone(),
33            })
34        }
35    }
36
37    pub fn set_drm_mode_object_property(
38        &self,
39        fd: i32,
40        object_id: u32,
41        object_type: u32,
42        property_id: u32,
43        value: u64,
44    ) -> Result<(), i32> {
45        #[cfg(feature = "link_drm")]
46        let func = bindings::drmModeObjectSetProperty;
47        #[cfg(feature = "dynamic_loading")]
48        let func = self.libdrm.drmModeObjectSetProperty;
49
50        let r = unsafe { func(fd, object_id, object_type, property_id, value) };
51
52        query_error!(r);
53
54        Ok(())
55    }
56
57}
58
59impl drmModeObjectProperties {
60    #[cfg(feature = "link_drm")]
61    pub fn get(fd: i32, object_id: u32, object_type: u32) -> Option<Self> {
62        let obj_ptr = unsafe { bindings::drmModeObjectGetProperties(
63            fd,
64            object_id,
65            object_type,
66        ) };
67
68        if obj_ptr.is_null() {
69            None
70        } else {
71            Some(Self { ptr: obj_ptr, lib: LibDrm::new().unwrap() })
72        }
73    }
74
75    #[cfg(feature = "link_drm")]
76    pub fn set(
77        fd: i32,
78        object_id: u32,
79        object_type: u32,
80        property_id: u32,
81        value: u64,
82    ) -> Result<(), i32> {
83        let r = unsafe { bindings::drmModeObjectSetProperty(
84            fd,
85            object_id,
86            object_type,
87            property_id,
88            value,
89        ) };
90
91        query_error!(r);
92
93        Ok(())
94    }
95
96    pub fn get_mode_property(&self, fd: i32) -> Vec<(drmModeProperty, u64)> {
97        let props_ptr = unsafe { addr_of!((*self.ptr).props).read() };
98        let values_ptr = unsafe { addr_of!((*self.ptr).prop_values).read() };
99
100        if props_ptr.is_null() || values_ptr.is_null() {
101            return Vec::new();
102        }
103
104        let count = unsafe { addr_of!((*self.ptr).count_props).read() as usize };
105        let props = unsafe { std::slice::from_raw_parts(props_ptr, count) };
106        let values = unsafe { std::slice::from_raw_parts(values_ptr, count) };
107
108        props.iter().zip(values.iter()).filter_map(|(prop_id, value)| {
109            let prop = self.lib.get_drm_mode_property(fd, *prop_id)?;
110
111            Some((prop, *value))
112        }).collect()
113    }
114}
115
116impl Drop for drmModeObjectProperties {
117    fn drop(&mut self) {
118        #[cfg(feature = "link_drm")]
119        let func = bindings::drmModeFreeObjectProperties;
120        #[cfg(feature = "dynamic_loading")]
121        let func = self.lib.libdrm.drmModeFreeObjectProperties;
122
123	    unsafe { func(self.ptr); }
124    }
125}