libdrm_amdgpu_sys/drm_mode/
crtc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{bindings, drmModeObjectProperties, LibDrm};
use bindings::drmModeCrtcPtr;
pub use bindings::drmModeCrtc;

#[allow(dead_code)]
#[derive(Clone)]
struct WrapperDrmModeCrtcPtr {
    pub(crate) ptr: drmModeCrtcPtr,
    pub(crate) lib: LibDrm,
}

impl LibDrm {
    pub fn get_drm_mode_crtc(&self, fd: i32, crtc_id: u32) -> Option<drmModeCrtc> {
        #[cfg(feature = "link_drm")]
        let func = bindings::drmModeGetCrtc;
        #[cfg(feature = "dynamic_loading")]
        let func = self.libdrm.drmModeGetCrtc;

        let ptr = unsafe { func(fd, crtc_id) };

        if ptr.is_null() { return None; }

        let wrapper = WrapperDrmModeCrtcPtr { ptr, lib: self.clone() };

        Some(unsafe { wrapper.ptr.read() })
    }

    pub fn get_drm_mode_crtc_props(
        &self,
        fd: i32,
        crtc_id: u32,
    ) -> Option<drmModeObjectProperties> {
        self.get_drm_mode_object_properties(
            fd,
            crtc_id,
            bindings::DRM_MODE_OBJECT_CRTC,
        )
    }
}

impl drmModeCrtc {
    #[cfg(feature = "link_drm")]
    pub fn get(fd: i32, crtc_id: u32) -> Option<Self> {
        let ptr = unsafe { bindings::drmModeGetCrtc(fd, crtc_id) };

        if ptr.is_null() { return None; }

        let wrapper = WrapperDrmModeCrtcPtr { ptr, lib: LibDrm::new().unwrap() };

        Some(unsafe { wrapper.ptr.read() })
    }

    #[cfg(feature = "link_drm")]
    pub fn get_crtc_props(&self, fd: i32) -> Option<drmModeObjectProperties> {
        drmModeObjectProperties::get(
            fd,
            self.crtc_id,
            bindings::DRM_MODE_OBJECT_CRTC,
        )
    }

    pub fn mode_valid(&self) -> bool {
        self.mode_valid == 1
    }
}

impl Drop for WrapperDrmModeCrtcPtr {
    fn drop(&mut self) {
        #[cfg(feature = "link_drm")]
        let func = bindings::drmModeFreeCrtc;
        #[cfg(feature = "dynamic_loading")]
        let func = self.lib.libdrm.drmModeFreeCrtc;

	    unsafe { func(self.ptr); }
    }
}