Skip to main content

leenfetch_core/modules/linux/desktop/
resolution.rs

1use std::fs;
2
3pub fn get_resolution() -> Option<String> {
4    // DRM/KMS path
5    if let Some(res) = try_drm() {
6        return Some(res);
7    }
8
9    // Framebuffer path
10    if let Some(res) = try_fb() {
11        return Some(res);
12    }
13
14    // Wayland path (unsupported without compositor protocol)
15    if std::env::var("WAYLAND_DISPLAY").is_ok() {
16        return Some("Wayland: resolution unavailable (restricted by compositor)".to_string());
17    }
18
19    None
20}
21
22fn try_drm() -> Option<String> {
23    let entries = fs::read_dir("/sys/class/drm/").ok()?;
24
25    for entry in entries.flatten() {
26        let path = entry.path();
27        if path.file_name()?.to_str()?.contains("card") && path.join("status").exists() {
28            let status = fs::read_to_string(path.join("status")).ok()?;
29            if status.trim() != "connected" {
30                continue;
31            }
32
33            let mode_path = path.join("modes");
34            if mode_path.exists() {
35                let mode = fs::read_to_string(mode_path)
36                    .ok()?
37                    .lines()
38                    .next()?
39                    .to_string();
40                return Some(mode);
41            }
42        }
43    }
44
45    None
46}
47
48fn try_fb() -> Option<String> {
49    let contents = fs::read_to_string("/sys/class/graphics/fb0/virtual_size").ok()?;
50    let (w, h) = contents.trim().split_once(',')?;
51    Some(format!("{}x{}", w.trim(), h.trim()))
52}