aboutsummaryrefslogtreecommitdiffstats
path: root/src/osx/monitor.rs
blob: 383fd32240bc3ed403903768e56b9ea28528e686 (plain)
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
use core_graphics::display;

pub struct MonitorID(u32);

pub fn get_available_monitors() -> Vec<MonitorID> {
    let mut monitors = Vec::new();
    unsafe {
        let max_displays = 10u32;
        let mut active_displays = [0u32, ..10];
        let mut display_count = 0;
        display::CGGetActiveDisplayList(max_displays,
                                                        &mut active_displays[0],
                                                        &mut display_count);
        for i in range(0u, display_count as uint) {
            monitors.push(MonitorID(active_displays[i]));
        }
    }
    monitors
}

pub fn get_primary_monitor() -> MonitorID {
    let id = unsafe {
        MonitorID(display::CGMainDisplayID())
    };
    id
}

impl MonitorID {
    pub fn get_name(&self) -> Option<String> {
        let MonitorID(display_id) = *self;
        let screen_num = unsafe {
            display::CGDisplayModelNumber(display_id)
        };
        Some(format!("Monitor #{}", screen_num))
    }

    pub fn get_dimensions(&self) -> (uint, uint) {
        let MonitorID(display_id) = *self;
        let dimension = unsafe {
            let height = display::CGDisplayPixelsHigh(display_id);
            let width = display::CGDisplayPixelsWide(display_id);
            (width as uint, height as uint)
        };
        dimension
    }
}