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
use crate::geometry::*;
use windows::Win32::{Foundation::*, Graphics::Gdi::*, UI::WindowsAndMessaging::*};

/// Describes monitor info.
#[derive(Clone, Debug)]
pub struct Monitor {
    hmonitor: HMONITOR,
    pub position: ScreenPosition,
    pub size: PhysicalSize<u32>,
    pub is_primary: bool,
}

impl PartialEq for Monitor {
    #[inline]
    fn eq(&self, other: &Monitor) -> bool {
        self.hmonitor == other.hmonitor
    }
}

extern "system" fn get_monitors_proc(
    hmonitor: HMONITOR,
    _: HDC,
    rc: *mut RECT,
    lparam: LPARAM,
) -> BOOL {
    unsafe {
        let v = &mut *(lparam.0 as *mut Vec<Monitor>);
        let rc = &*rc;
        let mut info = MONITORINFO {
            cbSize: std::mem::size_of::<MONITORINFO>() as u32,
            ..Default::default()
        };
        GetMonitorInfoW(hmonitor, &mut info);
        v.push(Monitor {
            hmonitor,
            position: ScreenPosition::new(rc.left, rc.top),
            size: PhysicalSize::new((rc.right - rc.left) as u32, (rc.bottom - rc.top) as u32),
            is_primary: (info.dwFlags & MONITORINFOF_PRIMARY) != 0,
        });
        true.into()
    }
}

/// Return monitors info.
pub fn get_monitors() -> Vec<Monitor> {
    unsafe {
        let len = GetSystemMetrics(SM_CMONITORS) as usize;
        let mut v = Vec::with_capacity(len);
        EnumDisplayMonitors(
            HDC(0),
            None,
            Some(get_monitors_proc),
            LPARAM((&mut v) as *mut Vec<Monitor> as _),
        );
        v
    }
}

/// A screen position to a monitor.
pub fn monitor_from_point(point: ScreenPosition) -> Option<Monitor> {
    unsafe {
        let hmonitor = MonitorFromPoint(
            POINT {
                x: point.x,
                y: point.y,
            },
            MONITOR_DEFAULTTONULL,
        );
        if hmonitor == HMONITOR(0) {
            return None;
        }
        let mut info = MONITORINFO {
            cbSize: std::mem::size_of::<MONITORINFO>() as u32,
            ..Default::default()
        };
        GetMonitorInfoW(hmonitor, &mut info);
        Some(Monitor {
            hmonitor,
            position: ScreenPosition::new(info.rcMonitor.left, info.rcMonitor.top),
            size: PhysicalSize::new(
                (info.rcMonitor.right - info.rcMonitor.left) as u32,
                (info.rcMonitor.bottom - info.rcMonitor.top) as u32,
            ),
            is_primary: (info.dwFlags & MONITORINFOF_PRIMARY) != 0,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn monitors_len() {
        let monitors = get_monitors();
        unsafe {
            assert_eq!(monitors.len(), GetSystemMetrics(SM_CMONITORS) as usize);
        }
    }
}