winit-sonoma-fix/src/platform/macos/monitor.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

use core_graphics::display;
use std::collections::VecDeque;
use native_monitor::NativeMonitorId;
2015-07-19 21:53:40 +10:00
#[derive(Clone)]
2015-09-24 17:11:59 +10:00
pub struct MonitorId(u32);
2015-09-24 17:11:59 +10:00
pub fn get_available_monitors() -> VecDeque<MonitorId> {
let mut monitors = VecDeque::new();
unsafe {
let max_displays = 10u32;
2015-01-10 20:56:47 +11:00
let mut active_displays = [0u32; 10];
let mut display_count = 0;
2016-12-14 00:28:30 +11:00
display::CGGetActiveDisplayList(max_displays, &mut active_displays[0], &mut display_count);
2015-03-23 23:03:07 +11:00
for i in 0..display_count as usize {
2015-09-24 17:11:59 +10:00
monitors.push_back(MonitorId(active_displays[i]));
}
}
monitors
}
2015-09-21 22:42:05 +10:00
#[inline]
2015-09-24 17:11:59 +10:00
pub fn get_primary_monitor() -> MonitorId {
2016-12-14 00:28:30 +11:00
let id = unsafe { MonitorId(display::CGMainDisplayID()) };
id
}
2015-09-24 17:11:59 +10:00
impl MonitorId {
pub fn get_name(&self) -> Option<String> {
2015-09-24 17:11:59 +10:00
let MonitorId(display_id) = *self;
2016-12-14 00:28:30 +11:00
let screen_num = unsafe { display::CGDisplayModelNumber(display_id) };
Some(format!("Monitor #{}", screen_num))
}
2015-09-21 22:42:05 +10:00
#[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId {
2015-09-24 17:11:59 +10:00
let MonitorId(display_id) = *self;
NativeMonitorId::Numeric(display_id)
}
2015-01-16 06:20:52 +11:00
pub fn get_dimensions(&self) -> (u32, u32) {
2015-09-24 17:11:59 +10:00
let MonitorId(display_id) = *self;
let dimension = unsafe {
let height = display::CGDisplayPixelsHigh(display_id);
let width = display::CGDisplayPixelsWide(display_id);
2015-01-16 06:20:52 +11:00
(width as u32, height as u32)
};
dimension
}
}