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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! # Encoder
//!
//! An encoder is a bridge between a CRTC and a connector that takes the pixel
//! data of the CRTC and encodes it into a format the connector understands.

use control;
use drm_ffi as ffi;

/// A handle to an encoder
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Handle(control::RawResourceHandle);

impl From<Handle> for control::RawResourceHandle {
    fn from(handle: Handle) -> Self {
        handle.0
    }
}

impl From<Handle> for u32 {
    fn from(handle: Handle) -> Self {
        handle.0.into()
    }
}

impl From<control::RawResourceHandle> for Handle {
    fn from(handle: control::RawResourceHandle) -> Self {
        Handle(handle)
    }
}

impl control::ResourceHandle for Handle {
    const FFI_TYPE: u32 = ffi::DRM_MODE_OBJECT_ENCODER;
}

impl std::fmt::Debug for Handle {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_tuple("encoder::Handle").field(&self.0).finish()
    }
}

/// Information about an encoder
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub struct Info {
    pub(crate) handle: Handle,
    pub(crate) enc_type: Kind,
    pub(crate) crtc: Option<control::crtc::Handle>,
    pub(crate) pos_crtcs: u32,
    pub(crate) pos_clones: u32,
}

impl Info {
    /// Returns the handle to this encoder.
    pub fn handle(&self) -> Handle {
        self.handle
    }

    /// Returns the `Kind` of encoder this is.
    pub fn kind(&self) -> Kind {
        self.enc_type
    }

    /// Returns a handle to the CRTC this encoder is attached to.
    pub fn crtc(&self) -> Option<control::crtc::Handle> {
        self.crtc
    }

    /// Returns a filter for the possible CRTCs that can use this encoder.
    ///
    /// Use with [`control::ResourceHandles::filter_crtcs`]
    /// to receive a list of crtcs.
    pub fn possible_crtcs(&self) -> control::CrtcListFilter {
        control::CrtcListFilter(self.pos_crtcs)
    }

    /// Returns a filter for the possible encoders that clones this one.
    pub fn possible_clones(&self) {
        unimplemented!()
    }
}

/// The type of encoder.
#[allow(missing_docs)]
#[allow(clippy::upper_case_acronyms)]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
pub enum Kind {
    None,
    DAC,
    TMDS,
    LVDS,
    TVDAC,
    Virtual,
    DSI,
    DPMST,
    DPI,
}

impl From<u32> for Kind {
    fn from(n: u32) -> Self {
        match n {
            ffi::DRM_MODE_ENCODER_NONE => Kind::None,
            ffi::DRM_MODE_ENCODER_DAC => Kind::DAC,
            ffi::DRM_MODE_ENCODER_TMDS => Kind::TMDS,
            ffi::DRM_MODE_ENCODER_LVDS => Kind::LVDS,
            ffi::DRM_MODE_ENCODER_TVDAC => Kind::TVDAC,
            ffi::DRM_MODE_ENCODER_VIRTUAL => Kind::Virtual,
            ffi::DRM_MODE_ENCODER_DSI => Kind::DSI,
            ffi::DRM_MODE_ENCODER_DPMST => Kind::DPMST,
            ffi::DRM_MODE_ENCODER_DPI => Kind::DPI,
            _ => Kind::None,
        }
    }
}

impl From<Kind> for u32 {
    fn from(kind: Kind) -> Self {
        match kind {
            Kind::None => ffi::DRM_MODE_ENCODER_NONE,
            Kind::DAC => ffi::DRM_MODE_ENCODER_DAC,
            Kind::TMDS => ffi::DRM_MODE_ENCODER_TMDS,
            Kind::LVDS => ffi::DRM_MODE_ENCODER_LVDS,
            Kind::TVDAC => ffi::DRM_MODE_ENCODER_TVDAC,
            Kind::Virtual => ffi::DRM_MODE_ENCODER_VIRTUAL,
            Kind::DSI => ffi::DRM_MODE_ENCODER_DSI,
            Kind::DPMST => ffi::DRM_MODE_ENCODER_DPMST,
            Kind::DPI => ffi::DRM_MODE_ENCODER_DPI,
        }
    }
}