retch-sysinfo 0.1.27

System information gathering library for retch
Documentation
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
// SPDX-FileCopyrightText: 2026 Ken Tobias
// SPDX-License-Identifier: GPL-3.0-or-later

//! Display detection and EDID parsing.
//!
//! This module provides cross-platform detection of connected displays,
//! including resolution, refresh rate, and monitor name/serial extraction
//! from raw EDID blobs.
//!
//! ## Platform Support
//!
//! - **Linux**: Reads natively from `/sys/class/drm` (status, modes, and raw EDID
//!   bytes). Falls back to `xrandr --current` when no sysfs displays are found.
//! - **macOS**: Parses `system_profiler SPDisplaysDataType` output.
//! - **Windows**: Calls `EnumDisplayDevicesW` + `EnumDisplaySettingsW` via user32.dll FFI.

/// Parse the monitor's human-readable name from a raw EDID binary blob.
///
/// Searches the four 18-byte descriptor blocks (at EDID offsets 54, 72, 90,
/// and 108) for a Monitor Name Descriptor (tag `0xFC`) and returns the name
/// string, or `None` if no such descriptor is found or the EDID is too short.
#[allow(dead_code)]
pub fn parse_monitor_name_from_edid(edid: &[u8]) -> Option<String> {
    if edid.len() < 128 {
        return None;
    }
    let offsets = [54, 72, 90, 108];
    for &offset in &offsets {
        if offset + 18 <= edid.len() {
            let block = &edid[offset..offset + 18];
            if block[0] == 0x00 && block[1] == 0x00 && block[2] == 0x00 && block[3] == 0xFC {
                let name_bytes = &block[4..17];
                let name = String::from_utf8_lossy(name_bytes);
                let cleaned = name.trim().replace('\0', "").to_string();
                if !cleaned.is_empty() {
                    return Some(cleaned);
                }
            }
        }
    }
    None
}

/// Parse the refresh rate (in Hz) from the first Detailed Timing Descriptor
/// (DTD) block in a raw EDID binary blob.
///
/// Reads the pixel clock, horizontal/vertical active and blanking values from
/// bytes 54–71 of the EDID and computes the refresh rate as:
/// `pixel_clock_hz / (h_total * v_total)`.
///
/// Returns `None` if the EDID is too short or the pixel clock is zero.
#[allow(dead_code)]
pub fn parse_refresh_rate_from_edid(edid: &[u8]) -> Option<f64> {
    if edid.len() < 72 {
        return None;
    }
    let block = &edid[54..72];
    let pixel_clock = ((block[1] as u32) << 8) | (block[0] as u32);
    if pixel_clock == 0 {
        return None;
    }
    let pixel_clock_hz = pixel_clock * 10_000;
    let h_active = (block[2] as u32) | (((block[4] as u32) & 0xF0) << 4);
    let h_blanking = (block[3] as u32) | (((block[4] as u32) & 0x0F) << 8);
    let v_active = (block[5] as u32) | (((block[7] as u32) & 0xF0) << 4);
    let v_blanking = (block[6] as u32) | (((block[7] as u32) & 0x0F) << 8);

    let h_total = h_active + h_blanking;
    let v_total = v_active + v_blanking;
    if h_total == 0 || v_total == 0 {
        return None;
    }

    let refresh = (pixel_clock_hz as f64) / ((h_total * v_total) as f64);
    Some((refresh * 100.0).round() / 100.0)
}

/// Format a refresh rate value for display.
///
/// Integers (e.g. 60.0) are rendered without decimals; non-integer values
/// (e.g. 59.94) are rounded to two decimal places.
#[allow(dead_code)]
pub fn format_refresh_rate(refresh: f64) -> String {
    if (refresh - refresh.round()).abs() < 0.01 {
        format!("{:.0}", refresh)
    } else {
        format!("{:.2}", refresh)
    }
}

/// Parse the serial number from a raw EDID binary blob.
///
/// First searches for an ASCII Serial Number Descriptor (tag `0xFF`) in the
/// four 18-byte descriptor blocks at offsets 54, 72, 90, and 108. If not
/// found, falls back to the 32-bit numeric serial number at EDID bytes 12–15.
///
/// Returns `None` if no serial number is found or the EDID is too short.
#[allow(dead_code)]
pub fn parse_serial_number_from_edid(edid: &[u8]) -> Option<String> {
    if edid.len() < 128 {
        return None;
    }
    // 1. Try finding ASCII Serial Number descriptor block (tag 0xFF)
    let offsets = [54, 72, 90, 108];
    for &offset in &offsets {
        if offset + 18 <= edid.len() {
            let block = &edid[offset..offset + 18];
            if block[0] == 0x00 && block[1] == 0x00 && block[2] == 0x00 && block[3] == 0xFF {
                let serial_bytes = &block[4..17];
                let serial = String::from_utf8_lossy(serial_bytes);
                let cleaned = serial.trim().replace('\0', "").to_string();
                if !cleaned.is_empty() {
                    return Some(cleaned);
                }
            }
        }
    }

    // 2. Fallback to the 32-bit numeric serial number at offset 12-15
    let serial_num = ((edid[15] as u32) << 24)
        | ((edid[14] as u32) << 16)
        | ((edid[13] as u32) << 8)
        | (edid[12] as u32);
    if serial_num != 0 && serial_num != 0xFFFFFFFF {
        return Some(serial_num.to_string());
    }

    None
}

/// Look up the monitor name for a given DRM connector port name by reading
/// the EDID from sysfs (`/sys/class/drm/<entry>/edid`).
///
/// Iterates `/sys/class/drm` for entries whose name ends with `port`, reads
/// the EDID bytes, and extracts the monitor name via [`parse_monitor_name_from_edid`].
/// Returns `None` if not found on Linux or not compiled for Linux.
#[allow(dead_code)]
pub fn get_monitor_name_for_port(port: &str) -> Option<String> {
    if let Ok(entries) = std::fs::read_dir("/sys/class/drm") {
        for entry in entries.filter_map(|e| e.ok()) {
            let name = entry.file_name().to_string_lossy().to_string();
            if name.ends_with(port) {
                let edid_path = entry.path().join("edid");
                if edid_path.exists() {
                    if let Ok(edid_bytes) = std::fs::read(&edid_path) {
                        if let Some(monitor_name) = parse_monitor_name_from_edid(&edid_bytes) {
                            return Some(monitor_name);
                        }
                    }
                }
            }
        }
    }
    None
}

/// Detect connected displays and return a list of human-readable strings
/// describing each display (name, resolution, and refresh rate).
///
/// Platform-specific implementations:
/// - **Linux**: Reads `/sys/class/drm` natively; falls back to `xrandr --current`.
/// - **macOS**: Parses `system_profiler SPDisplaysDataType` output.
/// - **Windows**: Calls `EnumDisplayDevicesW` + `EnumDisplaySettingsW` via user32.dll FFI.
pub fn detect_displays() -> Vec<String> {
    #[cfg(target_os = "macos")]
    {
        crate::macos_ffi::get_displays()
    }

    #[cfg(target_os = "windows")]
    {
        // Enumerate displays via Win32 EnumDisplayDevicesW + EnumDisplaySettingsW (user32.dll).
        // This avoids spawning wmic and works on all modern Windows versions.
        #[repr(C)]
        struct DisplayDevice {
            cb: u32,
            device_name: [u16; 32],
            device_string: [u16; 128],
            state_flags: u32,
            device_id: [u16; 128],
            device_key: [u16; 128],
        }

        // Full DEVMODEW layout (220 bytes). Offsets must match winuser.h exactly
        // to avoid stack corruption when EnumDisplaySettingsW writes into this.
        #[repr(C)]
        struct DevMode {
            device_name: [u16; 32], // offset   0, 64 bytes
            spec_version: u16,      // offset  64
            driver_version: u16,    // offset  66
            size: u16,              // offset  68
            driver_extra: u16,      // offset  70
            fields: u32,            // offset  72
            // display union: dmPosition(8) + dmDisplayOrientation(4) + dmDisplayFixedOutput(4)
            position_x: i32,           // offset  76
            position_y: i32,           // offset  80
            display_orientation: u32,  // offset  84
            display_fixed_output: u32, // offset  88
            // post-union fields
            color: u16,             // offset  92
            duplex: u16,            // offset  94
            y_resolution: u16,      // offset  96
            tt_option: u16,         // offset  98
            collate: u16,           // offset 100
            form_name: [u16; 32],   // offset 102, 64 bytes
            log_pixels: u16,        // offset 166
            bits_per_pel: u32,      // offset 168 (4-byte aligned)
            pels_width: u32,        // offset 172
            pels_height: u32,       // offset 176
            display_flags: u32,     // offset 180
            display_frequency: u32, // offset 184
            // extended fields
            icm_method: u32,     // offset 188
            icm_intent: u32,     // offset 192
            media_type: u32,     // offset 196
            dither_type: u32,    // offset 200
            reserved1: u32,      // offset 204
            reserved2: u32,      // offset 208
            panning_width: u32,  // offset 212
            panning_height: u32, // offset 216
        } // total: 220 bytes

        #[link(name = "user32")]
        extern "system" {
            fn EnumDisplayDevicesW(
                lpDevice: *const u16,
                iDevNum: u32,
                lpDisplayDevice: *mut DisplayDevice,
                dwFlags: u32,
            ) -> i32;

            fn EnumDisplaySettingsW(
                lpszDeviceName: *const u16,
                iModeNum: u32,
                lpDevMode: *mut DevMode,
            ) -> i32;
        }

        const DISPLAY_DEVICE_ACTIVE: u32 = 0x00000001;
        const ENUM_CURRENT_SETTINGS: u32 = 0xFFFF_FFFF;

        fn u16_to_string(buf: &[u16]) -> String {
            let len = buf.iter().position(|&c| c == 0).unwrap_or(buf.len());
            String::from_utf16_lossy(&buf[..len])
        }

        let mut displays = Vec::new();
        let mut dev_num = 0u32;
        loop {
            let mut dd = DisplayDevice {
                cb: std::mem::size_of::<DisplayDevice>() as u32,
                device_name: [0u16; 32],
                device_string: [0u16; 128],
                state_flags: 0,
                device_id: [0u16; 128],
                device_key: [0u16; 128],
            };
            let ok = unsafe { EnumDisplayDevicesW(std::ptr::null(), dev_num, &mut dd, 0) };
            if ok == 0 {
                break;
            }
            dev_num += 1;

            if dd.state_flags & DISPLAY_DEVICE_ACTIVE == 0 {
                continue;
            }

            let adapter_name = u16_to_string(&dd.device_string);

            let mut dm = unsafe { std::mem::zeroed::<DevMode>() };
            dm.size = std::mem::size_of::<DevMode>() as u16;
            let settings_ok = unsafe {
                EnumDisplaySettingsW(dd.device_name.as_ptr(), ENUM_CURRENT_SETTINGS, &mut dm)
            };

            let entry = if settings_ok != 0 && dm.pels_width > 0 && dm.pels_height > 0 {
                if dm.display_frequency > 0 {
                    format!(
                        "{} ({}x{} @ {}Hz)",
                        adapter_name, dm.pels_width, dm.pels_height, dm.display_frequency
                    )
                } else {
                    format!("{} ({}x{})", adapter_name, dm.pels_width, dm.pels_height)
                }
            } else {
                adapter_name
            };

            if !entry.is_empty() {
                displays.push(entry);
            }
        }

        displays
    }

    #[cfg(not(any(target_os = "macos", target_os = "windows")))]
    {
        let mut displays = Vec::new();

        // Try reading directly from sysfs first for best performance
        if let Ok(entries) = std::fs::read_dir("/sys/class/drm") {
            for entry in entries.filter_map(|e| e.ok()) {
                let path = entry.path();
                let status_path = path.join("status");
                let modes_path = path.join("modes");
                let edid_path = path.join("edid");
                if status_path.exists() && modes_path.exists() {
                    if let Ok(status) = std::fs::read_to_string(&status_path) {
                        if status.trim() == "connected" {
                            if let Ok(modes) = std::fs::read_to_string(&modes_path) {
                                if let Some(first_mode) = modes.lines().next() {
                                    let res = first_mode.trim().to_string();
                                    let port = entry.file_name().to_string_lossy().to_string();
                                    let clean_port = if let Some(idx) = port.find('-') {
                                        port[idx + 1..].to_string()
                                    } else {
                                        port
                                    };

                                    let edid_bytes = if edid_path.exists() {
                                        std::fs::read(&edid_path).ok()
                                    } else {
                                        None
                                    };

                                    let name = edid_bytes
                                        .as_ref()
                                        .and_then(|bytes| parse_monitor_name_from_edid(bytes))
                                        .unwrap_or(clean_port);

                                    let refresh = edid_bytes
                                        .as_ref()
                                        .and_then(|bytes| parse_refresh_rate_from_edid(bytes));

                                    let serial = edid_bytes
                                        .as_ref()
                                        .and_then(|bytes| parse_serial_number_from_edid(bytes));

                                    let display_name = if let Some(ref s) = serial {
                                        format!("{} #{}", name, s)
                                    } else {
                                        name
                                    };

                                    if let Some(r) = refresh {
                                        displays.push(format!(
                                            "{} ({} @ {}Hz)",
                                            display_name,
                                            res,
                                            format_refresh_rate(r)
                                        ));
                                    } else {
                                        displays.push(format!("{} ({})", display_name, res));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Fallback to xrandr only if sysfs yielded no connected displays
        if displays.is_empty() {
            if let Ok(output) = std::process::Command::new("xrandr")
                .arg("--current")
                .output()
            {
                if let Ok(stdout) = String::from_utf8(output.stdout) {
                    displays = parse_xrandr_displays(&stdout);
                }
            }
        }

        displays
    }
}

/// Parse connected display information from `system_profiler SPDisplaysDataType` output.
///
/// Returns a list of strings in the form `"<Name> (<Resolution> @ <Rate>)"`.
#[cfg(target_os = "macos")]
pub fn parse_macos_displays(stdout: &str) -> Vec<String> {
    let mut displays = Vec::new();
    let mut current_name = None;
    let mut current_res = None;
    let mut in_displays = false;

    for line in stdout.lines() {
        let trimmed = line.trim();
        let indent = line.len() - line.trim_start().len();

        if trimmed.starts_with("Displays:") {
            in_displays = true;
            continue;
        }

        if in_displays {
            if indent < 8 && !trimmed.is_empty() && !trimmed.starts_with("Displays:") {
                in_displays = false;
                continue;
            }

            if trimmed.ends_with(':') && !trimmed.starts_with("UI Looks like:") {
                let name = trimmed.trim_end_matches(':').trim().to_string();
                current_name = Some(name);
            } else if trimmed.starts_with("Resolution:") {
                let res = trimmed.strip_prefix("Resolution:").unwrap_or("").trim();
                let cleaned = res.replace(" ", "");
                current_res = Some(cleaned);
            } else if trimmed.starts_with("UI Looks like:") {
                if let Some(res) = current_res.take() {
                    let name_str = current_name.take().unwrap_or_else(|| "Display".to_string());
                    if let Some(idx) = trimmed.find('@') {
                        let freq = trimmed[idx..].trim();
                        let freq_clean = freq.replace(" ", "").replace(".00", "");
                        displays.push(format!(
                            "{} ({} @ {})",
                            name_str,
                            res,
                            freq_clean.trim_start_matches('@')
                        ));
                    } else {
                        displays.push(format!("{} ({})", name_str, res));
                    }
                }
            }
        }
    }
    if let Some(res) = current_res {
        let name_str = current_name.unwrap_or_else(|| "Display".to_string());
        displays.push(format!("{} ({})", name_str, res));
    }
    displays
}

/// Parse connected display information from `xrandr --current` output.
///
/// Returns a list of strings in the form `"<Name> (<Resolution> @ <Rate>Hz)"`.
/// For each connected port, looks up the monitor name from EDID via
/// [`get_monitor_name_for_port`], falling back to the port name itself.
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
pub fn parse_xrandr_displays(stdout: &str) -> Vec<String> {
    let mut displays = Vec::new();
    let mut current_display = None;
    let mut current_port = None;
    for line in stdout.lines() {
        let line = line.trim();
        if line.contains(" connected ") {
            let parts: Vec<&str> = line.split_whitespace().collect();
            if let Some(&port) = parts.first() {
                current_port = Some(port.to_string());
            }
            for part in parts {
                if part.contains('x') && part.contains('+') {
                    if let Some(res) = part.split('+').next() {
                        current_display = Some(res.to_string());
                    }
                }
            }
        } else if line.contains('*') {
            if let Some(res) = current_display.take() {
                let port = current_port.take().unwrap_or_default();
                let name = get_monitor_name_for_port(&port).unwrap_or_else(|| port.clone());
                let parts: Vec<&str> = line.split_whitespace().collect();
                let mut added = false;
                for part in parts {
                    if part.contains('*') {
                        let freq = part.trim_end_matches(['*', '+']);
                        displays.push(format!("{} ({} @ {}Hz)", name, res, freq));
                        added = true;
                        break;
                    }
                }
                if !added {
                    displays.push(format!("{} ({})", name, res));
                }
            }
        }
    }
    displays
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── EDID helpers ─────────────────────────────────────────────────────────

    /// Build a 128-byte all-zero EDID with the 1080p60 DTD at offset 54.
    fn edid_1080p60() -> Vec<u8> {
        let mut edid = vec![0u8; 128];
        // Pixel clock 14850 (x 10 kHz = 148.5 MHz)
        edid[54] = 0x02;
        edid[55] = 0x3A;
        // H Active 1920, H Blanking 280
        edid[56] = 0x80;
        edid[57] = 0x18;
        edid[58] = 0x71;
        // V Active 1080, V Blanking 45
        edid[59] = 0x38;
        edid[60] = 0x2D;
        edid[61] = 0x40;
        edid
    }

    /// Inject a Monitor Name Descriptor (tag 0xFC) at EDID offset 72.
    fn inject_monitor_name(edid: &mut Vec<u8>, name: &[u8]) {
        edid[72] = 0x00;
        edid[73] = 0x00;
        edid[74] = 0x00;
        edid[75] = 0xFC;
        for (i, &b) in name.iter().enumerate().take(13) {
            edid[76 + i] = b;
        }
    }

    // ── parse_monitor_name_from_edid ──────────────────────────────────────────

    #[test]
    fn test_monitor_name_too_short_edid() {
        assert_eq!(parse_monitor_name_from_edid(&[0u8; 64]), None);
    }

    #[test]
    fn test_monitor_name_no_descriptor() {
        // 128-byte EDID with no 0xFC descriptor block -> None
        assert_eq!(parse_monitor_name_from_edid(&[0u8; 128]), None);
    }

    #[test]
    fn test_monitor_name_at_offset_72() {
        let mut edid = vec![0u8; 128];
        inject_monitor_name(&mut edid, b"DELL S3422DW\n");
        assert_eq!(
            parse_monitor_name_from_edid(&edid),
            Some("DELL S3422DW".to_string())
        );
    }

    #[test]
    fn test_monitor_name_at_offset_54() {
        let mut edid = vec![0u8; 128];
        edid[54] = 0x00;
        edid[55] = 0x00;
        edid[56] = 0x00;
        edid[57] = 0xFC;
        let name = b"LG 27UK850\n  ";
        for (i, &b) in name.iter().enumerate().take(13) {
            edid[58 + i] = b;
        }
        assert_eq!(
            parse_monitor_name_from_edid(&edid),
            Some("LG 27UK850".to_string())
        );
    }

    // ── parse_refresh_rate_from_edid ──────────────────────────────────────────

    #[test]
    fn test_parse_refresh_rate_from_edid() {
        let edid = edid_1080p60();
        let refresh = parse_refresh_rate_from_edid(&edid);
        assert!(refresh.is_some());
        // 148,500,000 / ((1920 + 280) * (1080 + 45)) = 60 Hz
        assert_eq!(refresh.unwrap(), 60.0);
    }

    #[test]
    fn test_refresh_rate_too_short_edid() {
        assert_eq!(parse_refresh_rate_from_edid(&[0u8; 71]), None);
    }

    #[test]
    fn test_refresh_rate_zero_pixel_clock() {
        // All-zero bytes -> pixel clock == 0 -> None
        let edid = vec![0u8; 128];
        assert_eq!(parse_refresh_rate_from_edid(&edid), None);
    }

    #[test]
    fn test_refresh_rate_zero_totals() {
        // Non-zero pixel clock but all dimension bytes zero -> h_total == 0 -> None
        let mut edid = vec![0u8; 128];
        edid[54] = 0x01; // pixel clock byte != 0
                         // all active/blanking bytes remain 0
        assert_eq!(parse_refresh_rate_from_edid(&edid), None);
    }

    // ── format_refresh_rate ───────────────────────────────────────────────────

    #[test]
    fn test_format_refresh_rate() {
        assert_eq!(format_refresh_rate(60.0), "60");
        assert_eq!(format_refresh_rate(59.94), "59.94");
        assert_eq!(format_refresh_rate(143.971), "143.97");
        assert_eq!(format_refresh_rate(120.0), "120");
        assert_eq!(format_refresh_rate(240.0), "240");
    }

    // ── parse_serial_number_from_edid ─────────────────────────────────────────

    #[test]
    fn test_serial_too_short_edid() {
        assert_eq!(parse_serial_number_from_edid(&[0u8; 64]), None);
    }

    #[test]
    fn test_parse_serial_number_from_edid() {
        let mut edid = vec![0u8; 128];
        // 1. Fallback 32-bit numeric serial
        edid[12] = 0x78;
        edid[13] = 0x56;
        edid[14] = 0x34;
        edid[15] = 0x12; // 0x12345678 = 305419896
        assert_eq!(
            parse_serial_number_from_edid(&edid),
            Some("305419896".to_string())
        );

        // 2. ASCII Monitor Serial Number descriptor block (tag 0xFF) at offset 72
        edid[72] = 0x00;
        edid[73] = 0x00;
        edid[74] = 0x00;
        edid[75] = 0xFF; // ASCII Serial Number descriptor tag
        let serial_str = b"CN0123456789\n";
        for i in 0..serial_str.len() {
            edid[76 + i] = serial_str[i];
        }
        assert_eq!(
            parse_serial_number_from_edid(&edid),
            Some("CN0123456789".to_string())
        );
    }

    #[test]
    fn test_serial_numeric_zero_is_none() {
        // All-zero EDID -> serial_num == 0 -> None
        let edid = vec![0u8; 128];
        assert_eq!(parse_serial_number_from_edid(&edid), None);
    }

    #[test]
    fn test_serial_numeric_all_ff_is_none() {
        let mut edid = vec![0u8; 128];
        edid[12] = 0xFF;
        edid[13] = 0xFF;
        edid[14] = 0xFF;
        edid[15] = 0xFF;
        assert_eq!(parse_serial_number_from_edid(&edid), None);
    }

    #[test]
    fn test_serial_ascii_takes_precedence_over_numeric() {
        let mut edid = vec![0u8; 128];
        // Non-zero numeric serial
        edid[12] = 0x01;
        edid[13] = 0x02;
        edid[14] = 0x03;
        edid[15] = 0x04;
        // ASCII serial descriptor at offset 54
        edid[54] = 0x00;
        edid[55] = 0x00;
        edid[56] = 0x00;
        edid[57] = 0xFF;
        let serial = b"ASCIIWIN0001\n";
        for (i, &b) in serial.iter().enumerate().take(13) {
            edid[58 + i] = b;
        }
        // ASCII descriptor should win over the numeric value
        assert_eq!(
            parse_serial_number_from_edid(&edid),
            Some("ASCIIWIN0001".to_string())
        );
    }

    // ── parse_xrandr_displays ─────────────────────────────────────────────────

    #[cfg(not(any(target_os = "macos", target_os = "windows")))]
    #[test]
    fn test_parse_xrandr_displays() {
        let sample = "Screen 0: minimum 320 x 200, current 2560 x 1440\n\
                      DP-1 connected primary 2560x1440+0+0\n\
                         2560x1440     143.97*+\n\
                         1920x1080     60.00\n";
        let parsed = parse_xrandr_displays(sample);
        assert_eq!(parsed, vec!["DP-1 (2560x1440 @ 143.97Hz)".to_string()]);
    }

    #[cfg(not(any(target_os = "macos", target_os = "windows")))]
    #[test]
    fn test_parse_xrandr_multiple_displays() {
        let sample = "Screen 0: minimum 320 x 200, current 3840 x 1080\n\
                      HDMI-1 connected 1920x1080+0+0\n\
                         1920x1080     60.00*+\n\
                      DP-1 connected 1920x1080+1920+0\n\
                         1920x1080    144.00*+\n";
        let parsed = parse_xrandr_displays(sample);
        assert_eq!(
            parsed,
            vec![
                "HDMI-1 (1920x1080 @ 60.00Hz)".to_string(),
                "DP-1 (1920x1080 @ 144.00Hz)".to_string(),
            ]
        );
    }

    #[cfg(not(any(target_os = "macos", target_os = "windows")))]
    #[test]
    fn test_parse_xrandr_no_connected_displays() {
        let sample = "Screen 0: minimum 320 x 200, current 0 x 0\n\
                      HDMI-1 disconnected\n\
                      DP-1 disconnected\n";
        assert_eq!(parse_xrandr_displays(sample), Vec::<String>::new());
    }

    // ── parse_macos_displays ──────────────────────────────────────────────────

    #[cfg(target_os = "macos")]
    #[test]
    fn test_parse_macos_displays() {
        let sample = "Graphics/Displays:\n\n    Apple M2:\n\n      Chipset Model: Apple M2\n      Displays:\n        Color LCD:\n          Resolution: 3024 x 1964\n          UI Looks like: 1512 x 982 @ 60.00Hz\n";
        let parsed = parse_macos_displays(sample);
        assert_eq!(parsed, vec!["Color LCD (3024x1964 @ 60Hz)".to_string()]);
    }

    #[cfg(target_os = "macos")]
    #[test]
    fn test_parse_macos_no_frequency() {
        let sample = "Graphics/Displays:\n      Displays:\n        ASUS PA329C:\n          Resolution: 3840 x 2160\n";
        let parsed = parse_macos_displays(sample);
        assert_eq!(parsed, vec!["ASUS PA329C (3840x2160)".to_string()]);
    }
}