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
#![warn(clippy::cargo)]

use lazy_static::lazy_static;
use regex::Regex;
use registry::{Hive, RegKey, Security};
use std::{
    fmt::{Debug, Display},
    mem, ptr,
};
use thiserror::Error;
use tracing::info;
use winapi::{
    shared::{
        minwindef::{BOOL, LPARAM},
        windef::{HDC, HMONITOR, HWND, RECT},
    },
    um::{
        errhandlingapi::GetLastError,
        wingdi::DISPLAY_DEVICEW,
        winuser::{
            EnumDisplayDevicesW, EnumDisplayMonitors, GetMonitorInfoW, GetWindowDC,
            EDD_GET_DEVICE_INTERFACE_NAME, MONITORINFO, MONITORINFOEXW,
        },
    },
};

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Monitor {
    pub driver_id: String,
    pub id: String,
}

impl Monitor {
    const LIST_PATH: &'static str = r"SYSTEM\CurrentControlSet\Enum\DISPLAY";

    /// List all monitor-looking things we can find. Expect this to return
    /// spurious and duplicate results.
    pub fn all() -> Result<Vec<Self>, MonitorError> {
        let drivers = Hive::LocalMachine
            .open(Self::LIST_PATH, Security::Read)
            .map_err(|e| MonitorError::ListDisplayDrivers(e.into()))?;

        let mut all_monitors = vec![];
        for driver_key in drivers.keys() {
            let driver_id = driver_key
                .map_err(|e| MonitorError::ListDisplayDrivers(e.into()))?
                .to_string();
            let mut monitors = Self::for_driver(driver_id)?;
            all_monitors.append(&mut monitors);
        }

        Ok(all_monitors)
    }

    fn for_driver(driver_id: String) -> Result<Vec<Self>, MonitorError> {
        let driver_key =
            Self::driver_key(&driver_id).map_err(|e| MonitorError::ListMonitorsForDriver {
                driver_id: driver_id.clone(),
                source: e.into(),
            })?;

        let mut list = vec![];
        for monitor_key in driver_key.keys() {
            match monitor_key {
                Ok(monitor_key) => {
                    let id = monitor_key.to_string();

                    list.push(Self {
                        driver_id: driver_id.clone(),
                        id,
                    });
                }
                Err(error) => {
                    info!(
                        ?error,
                        "Can't access a sub-key of driver {}, assuming not a monitor", driver_id
                    );
                }
            }
        }

        Ok(list)
    }

    /// List all monitors a window is on.
    ///
    /// If your window is on exactly one monitor, this should return exactly
    /// one result. Unlike [`Self::all`], your should not get spurious or
    /// duplicate results.
    pub fn intersecting(window: HWND) -> Result<Vec<Self>, MonitorError> {
        assert!(!window.is_null());

        let hdc = unsafe { GetWindowDC(window) };
        if hdc.is_null() {
            return Err(MonitorError::ListIntersecting {
                window: window as usize,
                source: WinError::last(),
            });
        }
        extern "system" fn cb(h: HMONITOR, _ctx: HDC, _rect: *mut RECT, list_ptr: LPARAM) -> BOOL {
            let mut info = MONITORINFOEXW {
                cbSize: mem::size_of::<MONITORINFOEXW>() as u32,
                ..Default::default()
            };
            unsafe {
                GetMonitorInfoW(h, &mut info as *mut MONITORINFOEXW as *mut MONITORINFO);
            }

            let mut interface_name = DISPLAY_DEVICEW {
                cb: mem::size_of::<DISPLAY_DEVICEW>() as u32,
                ..Default::default()
            };
            let status = unsafe {
                EnumDisplayDevicesW(
                    &info.szDevice[0],
                    0,
                    &mut interface_name,
                    EDD_GET_DEVICE_INTERFACE_NAME,
                )
            };
            if status == 0 {
                panic!();
            }
            let interface_name = wchars_to_string(&interface_name.DeviceID);

            let list_ptr = list_ptr as *mut Vec<Result<Monitor, MonitorError>>;
            let list = unsafe { &mut *list_ptr };

            let monitor = Monitor::from_interface_name(&interface_name);
            list.push(monitor);

            BOOL::from(true) // continue enumerating
        }

        let mut monitors = Vec::<Result<Monitor, MonitorError>>::new();
        let monitors_ptr = &mut monitors as *mut Vec<_> as LPARAM;
        unsafe {
            EnumDisplayMonitors(ptr::null_mut(), ptr::null_mut(), Some(cb), monitors_ptr);
        }

        monitors.into_iter().collect()
    }

    fn from_interface_name(name: &str) -> Result<Self, MonitorError> {
        lazy_static! {
            static ref RE: Regex = Regex::new(
                r"(?x)^
                \\\\\?\\DISPLAY
                \#(?P<d>[A-Z0-9]+)
                \#(?P<m>[A-Za-z0-9&]+)
                \#\{.*?\}
                $"
            )
            .unwrap();
        }

        let caps = RE
            .captures(name)
            .ok_or_else(|| MonitorError::InvalidInterface(name.to_string()))?;

        let driver_id = caps.name("d").unwrap().as_str().to_string();
        let monitor_id = caps.name("m").unwrap().as_str().to_string();

        Ok(Self {
            driver_id,
            id: monitor_id,
        })
    }

    /// Get the Extended Device Identification Data of a monitor.
    ///
    /// You can feed this to an [EDID parser][edid-parser-crate] to get
    /// information about the display such as the model name or colorspace.
    ///
    /// [edid-parser-crate]: https://crates.io/crates/edid
    pub fn edid(&self) -> Result<Vec<u8>, MonitorError> {
        let data = self
            .params_key()?
            .value(r"EDID")
            .map_err(|err| MonitorError::GetEdid {
                monitor: self.clone(),
                source: err.into(),
            })?;

        let bytes = match data {
            registry::value::Data::Binary(bytes) => bytes,
            _ => unreachable!("EDID will always be in bytes"),
        };

        Ok(bytes)
    }

    fn params_key(&self) -> Result<RegKey, MonitorError> {
        fn helper(monitor: &Monitor) -> Result<RegKey, RegistryError> {
            let driver_key = Monitor::driver_key(&monitor.driver_id)?;
            let monitor_key = driver_key.open(&monitor.id, Security::Read)?;
            let data = monitor_key.open(r"Device Parameters", Security::Read)?;
            Ok(data)
        }

        helper(self).map_err(|source| MonitorError::GetParams {
            monitor: self.clone(),
            source,
        })
    }

    fn driver_key(driver_id: &str) -> Result<registry::RegKey, registry::key::Error> {
        let path = format!(r"{}\{}", Self::LIST_PATH, driver_id);
        Hive::LocalMachine.open(path, Security::Read)
    }
}

#[derive(Debug, Error)]
pub enum MonitorError {
    #[error("Error listing display drivers to get monitors")]
    ListDisplayDrivers(#[source] RegistryError),
    #[error("Error listing monitors for display driver {driver_id}")]
    ListMonitorsForDriver {
        driver_id: String,
        #[source]
        source: RegistryError,
    },
    #[error("Error getting EDID for monitor {monitor:?}")]
    GetEdid {
        monitor: Monitor,
        #[source]
        source: RegistryError,
    },
    #[error("Could not get monitors intersecting window with hwnd {window}. Windows error or invalid hwnd.")]
    ListIntersecting {
        window: usize,
        #[source]
        source: WinError,
    },
    #[error("Error parsing monitor interface name. Expected something like \\\\?DISPLAY#MEI96A2#4&289d...#{{...}}, got: {0}")]
    InvalidInterface(String),
    #[error("Failed to get monitor parameters from the registry")]
    GetParams {
        monitor: Monitor,
        #[source]
        source: RegistryError,
    },
}

#[derive(Debug, Error)]
pub enum RegistryError {
    #[error(transparent)]
    Key(#[from] registry::key::Error),
    #[error(transparent)]
    Value(#[from] registry::value::Error),
    #[error(transparent)]
    KeyIter(#[from] registry::iter::keys::Error),
}

pub(crate) fn wchars_to_string(wchars: &[u16]) -> String {
    // Take up to null
    let end = wchars.iter().position(|&i| i == 0).unwrap_or(wchars.len());
    let (wchars, _) = wchars.split_at(end);

    String::from_utf16_lossy(wchars)
}

#[derive(PartialEq, Eq, Clone, Copy, Error)]
pub struct WinError(u32);

impl WinError {
    pub fn code(&self) -> u32 {
        self.0
    }

    pub fn last() -> Self {
        let code = unsafe { GetLastError() };
        Self(code)
    }
}

impl Display for WinError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let code = self.0;
        f.debug_tuple("WinError")
            .field(&format!("0x{:X}", code))
            .finish()
    }
}

impl Debug for WinError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self, f)
    }
}

impl From<u32> for WinError {
    fn from(code: u32) -> Self {
        Self(code)
    }
}

impl From<i32> for WinError {
    fn from(code: i32) -> Self {
        assert!(code > 0);
        Self(code as u32)
    }
}

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

    #[test]
    fn can_list_monitors() {
        let list = Monitor::all().unwrap();
        println!("{:#?}", list)
    }

    #[test]
    fn can_get_edids() {
        let monitors = Monitor::all().unwrap();
        let edids = monitors.iter().flat_map(Monitor::edid).collect::<Vec<_>>();
        assert!(edids.len() > 0);
        println!("{:#?}", edids);
    }

    #[test]
    fn can_list_monitors_for_hwnd() {
        use winit::{
            event_loop::{ControlFlow, EventLoop},
            platform::windows::{EventLoopExtWindows, WindowExtWindows},
            window::WindowBuilder,
        };

        let event_loop = EventLoop::<()>::new_any_thread();
        let window = WindowBuilder::new().build(&event_loop).unwrap();

        let mut already_ran = false;
        event_loop.run(move |_event, _, control_flow| {
            if !already_ran {
                let hwnd = window.hwnd() as HWND;
                let monitors = Monitor::intersecting(hwnd).unwrap();

                assert!(monitors.len() == 1);
                let monitor = &monitors[0];
                eprintln!("{:#?}", monitor);

                let edid = monitor.edid().unwrap();
                eprintln!("edid: {:?}...", &edid[..20]);

                already_ran = true;
            }

            *control_flow = ControlFlow::Exit;
        });
    }
}