drm/
connector.rs

1// Copyright 2016 The libdrm-rs project developers
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4// and associated documentation files (the "Software"), to deal in the Software without
5// restriction, including without limitation the rights to use, copy, modify, merge, publish,
6// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7// Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
18use std;
19
20use ffi;
21use ffi::xf86drm_mode::drmModeConnection;
22use encoder;
23use mode_info;
24
25/// Type of connector id.
26pub type ConnectorId = u32;
27
28/// Type of connector type.
29pub type ConnectorType = u32;
30
31/// Type of connector type id.
32pub type ConnectorTypeId = u32;
33
34
35static TYPE_NAMES: [&'static str; 15] = ["Unknown", "VGA", "DVII", "DVID", "DVIA", "Composite",
36                                         "SVIDEO", "LVDS", "Component", "9PinDIN", "DisplayPort",
37                                         "HDMIA", "HDMIB", "TV", "eDP"];
38
39/// Enum representing state of connectors connection.
40#[derive(PartialEq)]
41pub enum Connection {
42    Connected,
43    Disconnected,
44    Unknown,
45}
46
47impl std::fmt::Debug for Connection {
48    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
49        write!(f, "{}", match *self {
50                Connection::Connected => "connected",
51                Connection::Disconnected => "disconnected",
52                Connection::Unknown => "unknown",
53            })
54    }
55}
56
57/// Structure representing connector.
58pub struct Connector {
59    connector: ffi::xf86drm_mode::drmModeConnectorPtr,
60}
61
62/// General methods
63impl Connector {
64    /// `Connector` constructor.
65    /// Does not check if passed arguments are valid.
66    pub fn new(connector: ffi::xf86drm_mode::drmModeConnectorPtr) -> Self {
67        Connector { connector: connector }
68    }
69
70    /// Get string representation of connector type.
71    pub fn get_type_name(&self) -> &'static str {
72        let connector_type = self.get_connector_type() as usize;
73        TYPE_NAMES[if connector_type < TYPE_NAMES.len() { connector_type } else { 0 }]
74    }
75}
76
77/// Getters for original members
78impl Connector {
79    #[inline]
80    pub fn get_connector_id(&self) -> ConnectorId {
81        unsafe { (*self.connector).connector_id }
82    }
83
84    /// Get id of encoder currently connected to.
85    #[inline]
86    pub fn get_encoder_id(&self) -> u32 {
87        unsafe { (*self.connector).encoder_id }
88    }
89
90    #[inline]
91    pub fn get_connector_type(&self) -> ConnectorType {
92        unsafe { (*self.connector).connector_type }
93    }
94
95    #[inline]
96    pub fn get_connector_type_id(&self) -> ConnectorTypeId {
97        unsafe { (*self.connector).connector_type_id }
98    }
99
100    #[inline]
101    pub fn get_connection(&self) -> Connection {
102        unsafe {
103            match (*self.connector).connection {
104                drmModeConnection::DRM_MODE_CONNECTED => Connection::Connected,
105                drmModeConnection::DRM_MODE_DISCONNECTED => Connection::Disconnected,
106                _ => Connection::Unknown,
107            }
108        }
109    }
110
111    /// Get width in millimeters.
112    #[inline]
113    pub fn get_mm_width(&self) -> u32 {
114        unsafe { (*self.connector).mmWidth }
115    }
116
117    /// Get height in millimeters.
118    #[inline]
119    pub fn get_mm_height(&self) -> u32 {
120        unsafe { (*self.connector).mmHeight }
121    }
122
123    /// Get count of modes.
124    #[inline]
125    pub fn get_count_modes(&self) -> i32 {
126        unsafe { (*self.connector).count_modes }
127    }
128
129    /// Get count of encoders.
130    #[inline]
131    pub fn get_count_encoders(&self) -> i32 {
132        unsafe { (*self.connector).count_encoders }
133    }
134
135    /// Return vector of modes.
136    pub fn get_modes(&self) -> Vec<mode_info::ModeInfo> {
137        let count = self.get_count_modes();
138        let mut vec = Vec::with_capacity(count as usize);
139        for pos in 0..count as isize {
140            vec.push(mode_info::ModeInfo::new(unsafe {
141                (*(*self.connector).modes.offset(pos)).clone()
142            }));
143        }
144        vec
145    }
146
147    /// Return vector of encoder ids.
148    pub fn get_encoders(&self) -> Vec<encoder::EncoderId> {
149        let count = self.get_count_encoders();
150        let mut vec = Vec::with_capacity(count as usize);
151        for pos in 0..count as isize {
152            vec.push(unsafe { *(*self.connector).encoders.offset(pos) });
153        }
154        vec
155    }
156}
157
158impl Drop for Connector {
159    fn drop(&mut self) {
160        unsafe { ffi::xf86drm_mode::drmModeFreeConnector(self.connector) };
161    }
162}
163
164impl std::fmt::Debug for Connector {
165    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
166        write!(f, "Connector {{ id: {}, encoder_id: {}, state: {:?}, type: {} }}",
167               self.get_connector_id(),
168               self.get_encoder_id(),
169               self.get_connection(),
170               self.get_type_name())
171    }
172}
173