ferrix_lib/drm.rs
1/* drm.rs
2 *
3 * Copyright 2025 Michail Krasnov <mskrasnov07@ya.ru>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21//! Get information about video
22//!
23//! ## Example
24//! ```no-test
25//! use ferrix_lib::drm::Video;
26//! use ferrix_lib::traits::ToJson;
27//!
28//! let video = Video::new().unwrap();
29//! for dev in &video.devices {
30//! dbg!(dev);
31//! }
32//! let json = video.to_json().unwrap();
33//! dbg!(json);
34//! ```
35//!
36//! ## EDID structure, version 1.4
37//!
38//! <small>From <a href="https://en.wikipedia.org/wiki/Extended_Display_Identification_Data">WikiPedia</a></small>
39//!
40//! | Bytes | Description |
41//! |-------|-------------|
42//! | 0-7 | Fixed header pattern `00 FF FF FF FF FF FF 00` |
43//! | 8-9 | Manufacturer ID. "IBM", "PHL" |
44//! | 10-11 | Manufacturer product code. 16-bit hex number, little endian. "PHL" + "C0CF" |
45//! | 12-15 | Serial number. 32 bits, little-endian |
46//! | 16 | Week of manufacture; or `FF` model year flag |
47//! | 17 | Year of manufacture, or year or model, if model year flag is set. Year = datavalue + 1990 |
48//! | 18 | EDID version, usually `01` (for 1.3 and 1.4) |
49//! | 19 | EDID revision, usually `03` (for 1.3) or `04` (for 1.4) |
50//! | 20 | Video input parameters bitmap |
51//! | 21 | Horizontal screen size, in cm (range 1-255). If vertical screen size is 0, landscape aspect ratio (range 1.00-3.54), datavalue = (ARx100) - 99 (example: 16:9, 79; 4:3, 34.) |
52//! | 22 | Vertical screen size, in cm |
53//! | 23 | Display gamma, factory default (range 1.00 - 3.54), datavalue = (gamma x 100) - 100 = (gamma - 1) x 100. If 255, gamma is defined by DI-EXT block |
54//! | 24 | Supported features bitmap |
55//! | ... | ... |
56//!
57//! **EDID Detailed Timing Descriptor** (TODO)
58//!
59//! | Bytes | Description |
60//! |-------|-----------------------------------------------------|
61//! | 0-1 | Pixel clock. `00` - reserved; otherwise in 10 kHz units (0.01 - 655.35 MHz, little-endian) |
62//! | 2 | Horizontal active pixels 8 lsbits (0-255) |
63//! | 3 | Horizontal blanking pixels 8 lsbits (0-255) |
64//! | 4 | ... |
65//! | 5 | Vertical active lines 8 lsbits (0-255) |
66//! | 6 | Vertical blanking lines 8 lsbits (0-255) |
67//! | 7 | ... |
68//! | 8 | Horizontal front porch (sync offset) pixels 8 lsbits (0-255) from blanking start |
69//! | 9 | Horizontal sync pulse width pixels 8 lsbits (0-255) |
70//! | 10 | ... |
71//! | 11 | ... |
72//! | 12 | Horizontal image size, mm, 8 lsbits (0-255 mm, 161 in) |
73//! | 13 | Vertical image size, mm, ... |
74//! | ... | ... |
75
76use crate::traits::ToJson;
77use anyhow::{Result, anyhow};
78use serde::Serialize;
79use std::{
80 fs::{read, read_dir, read_to_string},
81 path::Path,
82};
83
84/// Information about video devices
85#[derive(Debug, Serialize)]
86pub struct Video {
87 pub devices: Vec<DRM>,
88}
89
90impl Video {
91 pub fn new() -> Result<Self> {
92 let prefix = Path::new("/sys/class/drm/");
93 let mut devices = vec![];
94
95 for i in 0..=u8::MAX {
96 let path = prefix.join(format!("card{i}"));
97 if !path.is_dir() {
98 continue;
99 }
100 let dir_contents = read_dir(path)?.filter(|dir| match &dir {
101 Ok(dir) => dir.path().is_dir(),
102 Err(_) => false,
103 });
104
105 for d in dir_contents {
106 let d = d?.path(); // {prefix}/{card_i}/{card_i}-*
107 let fname = match d.file_name() {
108 Some(fname) => fname.to_str().unwrap_or(""),
109 None => "",
110 };
111 if d.is_dir() && fname.contains("card") {
112 // println!("Read drm data: {} ({fname})", d.display());
113 devices.push(DRM::new(d)?);
114 }
115 }
116 }
117 Ok(Self { devices })
118 }
119}
120
121impl ToJson for Video {}
122
123/// Information about selected display
124#[derive(Debug, Serialize)]
125pub struct DRM {
126 /// Is enabled
127 pub enabled: bool,
128
129 /// Data from EDID
130 pub edid: Option<EDID>,
131
132 /// Supported modes of this screen (in HxV format)
133 pub modes: Vec<String>,
134}
135
136impl DRM {
137 pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
138 let path = path.as_ref();
139 let enabled = {
140 let txt = read_to_string(path.join("enabled"));
141 match txt {
142 Ok(txt) => {
143 let contents = txt.trim();
144 if contents == "enabled" { true } else { false }
145 }
146 Err(_) => false,
147 }
148 };
149 let modes = read_to_string(path.join("modes"))?
150 .lines()
151 .map(|s| s.to_string())
152 .collect::<Vec<_>>();
153 let edid = EDID::new(path);
154
155 Ok(Self {
156 enabled,
157 edid: match edid {
158 Ok(edid) => Some(edid),
159 Err(why) => {
160 // может быть, просто вываливать ошибку если не смогли прочитать EDID?
161 if enabled {
162 return Err(why);
163 } else {
164 None
165 }
166 }
167 },
168 modes,
169 })
170 }
171}
172
173/// Information from `edid` file (EDID v1.4 only supported yet)
174///
175/// Read [Wikipedia](https://en.wikipedia.org/wiki/Extended_Display_Identification_Data) for details.
176#[derive(Debug, Serialize)]
177pub struct EDID {
178 // NAME TYPE BYTES
179 /// Manufacturer ID. This is a legacy Plug and Play ID assigned
180 /// by UEFI forum which is a *big-endian* 16-bit value made up
181 /// of three 5-bit letters: 00001 - 'A', 00010 - 'B', etc.
182 pub manufacturer: String, // 8-9
183
184 /// Manufacturer product code. 16-bit hex-nubmer, little-endian.
185 /// For example, "LGC" + "C0CF"
186 pub product_code: u16, // 10-11
187
188 /// Serial number. 32 bits, little-endian
189 pub serial_number: u32, // 12-15
190
191 /// Week of manufacture; or `FF` model year flag
192 ///
193 /// > **NOTE:** week numbering isn't consistent between
194 /// > manufacturers
195 pub week: u8, // 16
196
197 /// Year of manufacture, or year of model, if model year flag
198 /// is set
199 pub year: u16, // 17
200
201 /// EDID version, usually `01` for 1.3 and 1.4
202 pub edid_version: u8, // 18
203
204 /// EDID revision, usually `03` for 1.3 or `04` for 1.4
205 pub edid_revision: u8, // 19
206
207 /// Video input parameters
208 pub video_input: VideoInputParams, // 20
209
210 /// Horizontal screen size, in centimetres (range 1-255)
211 pub hscreen_size: u8, // 21
212
213 /// Vertical screen size, in centimetres
214 pub vscreen_size: u8, // 22
215
216 /// Display gamma, factory default
217 pub display_gamma: u8, // 23
218}
219
220impl EDID {
221 pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> {
222 let data = read(path.as_ref().join("edid"))?;
223 if data.len() < 128 || data[0..8] != [0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00] {
224 return Err(anyhow!(
225 "Invalid EDID header on path {}",
226 path.as_ref().display(),
227 ));
228 }
229
230 let manufacturer = {
231 let word = ((data[8] as u16) << 8) | data[9] as u16;
232
233 let c1 = ((word >> 10) & 0x1F) as u8 + 64;
234 let c2 = ((word >> 5) & 0x1f) as u8 + 64;
235 let c3 = (word & 0x1f) as u8 + 64;
236
237 format!("{}{}{}", c1 as char, c2 as char, c3 as char)
238 };
239 let product_code = u16::from_le_bytes([data[10], data[11]]);
240 let serial_number = u32::from_le_bytes([data[12], data[13], data[14], data[15]]);
241 let week = data[16];
242 let year = data[17] as u16 + 1990;
243 let edid_version = data[18];
244 let edid_revision = data[19];
245 let video_input = VideoInputParams::new(&data);
246 let hscreen_size = data[21];
247 let vscreen_size = data[22];
248 let display_gamma = data[23];
249
250 Ok(Self {
251 manufacturer,
252 product_code,
253 serial_number,
254 week,
255 year,
256 edid_version,
257 edid_revision,
258 video_input,
259 hscreen_size,
260 vscreen_size,
261 display_gamma,
262 })
263 }
264}
265
266/// Video input parameters bitmap
267#[derive(Debug, Serialize)]
268pub enum VideoInputParams {
269 Digital(VideoInputParamsDigital),
270 Analog(VideoInputParamsAnalog),
271}
272
273impl VideoInputParams {
274 pub fn new(data: &[u8]) -> Self {
275 let d = data[20];
276 let bit_depth = ((d >> 7) & 0b00000111) as u8;
277 if bit_depth == 1 {
278 Self::Digital(VideoInputParamsDigital::new(data))
279 } else if bit_depth == 0 {
280 Self::Analog(VideoInputParamsAnalog::new(data))
281 } else {
282 panic!("Unknown 7 bit of 20 byte ({bit_depth})!")
283 }
284 }
285}
286
287/// Digital input
288#[derive(Debug, Serialize)]
289pub struct VideoInputParamsDigital {
290 /// Bit depth
291 pub bit_depth: BitDepth,
292
293 /// Video interface type
294 pub video_interface: VideoInterface,
295}
296
297impl VideoInputParamsDigital {
298 pub fn new(data: &[u8]) -> Self {
299 let d = data[20];
300 let bit_depth = BitDepth::from(((d >> 4) & 0b00000111) as u8);
301 let video_interface = VideoInterface::from((d & 0b00000111) as u8);
302
303 Self {
304 bit_depth,
305 video_interface,
306 }
307 }
308}
309
310/// Bit depth
311#[derive(Debug, Serialize)]
312pub enum BitDepth {
313 Undefined,
314
315 /// 6 bits per color
316 B6,
317
318 /// 8 bits per color
319 B8,
320
321 /// 10 bits per color
322 B10,
323
324 /// 12 bits per color
325 B12,
326
327 /// 14 bits per color
328 B14,
329
330 /// 16 bits per color
331 B16,
332
333 /// Reserved value
334 Reserved,
335
336 /// Unknown value (while EDID parsing)
337 Unknown(u8),
338}
339
340impl From<u8> for BitDepth {
341 fn from(value: u8) -> Self {
342 match value {
343 0b000 => Self::Undefined,
344 0b001 => Self::B6,
345 0b010 => Self::B8,
346 0b011 => Self::B10,
347 0b100 => Self::B12,
348 0b101 => Self::B14,
349 0b110 => Self::B16,
350 0b111 => Self::Reserved,
351 _ => Self::Unknown(value),
352 }
353 }
354}
355
356/// Video interface (EDID data may be incorrect)
357#[derive(Debug, Serialize)]
358pub enum VideoInterface {
359 Undefined,
360 DVI,
361 HDMIa,
362 HDMIb,
363 MDDI,
364 DisplayPort,
365 Unknown(u8),
366}
367
368impl From<u8> for VideoInterface {
369 fn from(value: u8) -> Self {
370 match value {
371 0b0000 => Self::Undefined,
372 0b0001 => Self::DVI,
373 0b0010 => Self::HDMIa,
374 0b0011 => Self::HDMIb,
375 0b0100 => Self::MDDI,
376 0b0101 => Self::DisplayPort,
377 _ => Self::Unknown(value),
378 }
379 }
380}
381
382#[derive(Debug, Serialize)]
383pub struct VideoInputParamsAnalog {
384 /// Video white and sync levels, relative to blank:
385 ///
386 /// | Binary value | Data |
387 /// |--------------|---------|
388 /// | `00` | +0.7/-0.3 V |
389 /// | `01` | +0.714/-0.286 V |
390 /// | `10` | +1.0/-0.4 V |
391 /// | `11` | +0.7/0 V (EVC) |
392 pub white_sync_levels: u8,
393
394 /// Blank-to-black setyp (pedestal) expected
395 pub blank_to_black_setup: u8,
396
397 /// Separate sync supported
398 pub separate_sync_supported: u8,
399
400 /// Composite sync supported
401 pub composite_sync_supported: u8,
402
403 /// Sync on green supported
404 pub sync_on_green_supported: u8,
405
406 /// VSync pulse must be serrated when composite or sync-on-green
407 /// is used
408 pub sync_on_green_isused: u8,
409}
410
411impl VideoInputParamsAnalog {
412 /// NOTE: THIS FUNCTION MAY BE INCORRECT
413 pub fn new(data: &[u8]) -> Self {
414 let d = data[20];
415 let white_sync_levels = ((d >> 5) & 0b00000011) as u8;
416 let blank_to_black_setup = (d >> 4) as u8;
417 let separate_sync_supported = (d >> 3) as u8;
418 let composite_sync_supported = (d >> 2) as u8;
419 let sync_on_green_supported = (d >> 1) as u8;
420 let sync_on_green_isused = (d >> 0) as u8; // WARN: may be incorrect
421
422 Self {
423 white_sync_levels,
424 blank_to_black_setup,
425 separate_sync_supported,
426 composite_sync_supported,
427 sync_on_green_supported,
428 sync_on_green_isused,
429 }
430 }
431}