1use std;
19
20use ffi;
21use ffi::xf86drm_mode::drmModeConnection;
22use encoder;
23use mode_info;
24
25pub type ConnectorId = u32;
27
28pub type ConnectorType = u32;
30
31pub 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#[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
57pub struct Connector {
59 connector: ffi::xf86drm_mode::drmModeConnectorPtr,
60}
61
62impl Connector {
64 pub fn new(connector: ffi::xf86drm_mode::drmModeConnectorPtr) -> Self {
67 Connector { connector: connector }
68 }
69
70 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
77impl Connector {
79 #[inline]
80 pub fn get_connector_id(&self) -> ConnectorId {
81 unsafe { (*self.connector).connector_id }
82 }
83
84 #[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 #[inline]
113 pub fn get_mm_width(&self) -> u32 {
114 unsafe { (*self.connector).mmWidth }
115 }
116
117 #[inline]
119 pub fn get_mm_height(&self) -> u32 {
120 unsafe { (*self.connector).mmHeight }
121 }
122
123 #[inline]
125 pub fn get_count_modes(&self) -> i32 {
126 unsafe { (*self.connector).count_modes }
127 }
128
129 #[inline]
131 pub fn get_count_encoders(&self) -> i32 {
132 unsafe { (*self.connector).count_encoders }
133 }
134
135 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 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