2019-05-01 17:03:30 -06:00
|
|
|
use std::{collections::VecDeque, fmt};
|
2018-06-14 19:42:18 -04:00
|
|
|
|
2019-06-12 21:07:25 +03:00
|
|
|
use cocoa::{
|
|
|
|
|
appkit::NSScreen,
|
|
|
|
|
base::{id, nil},
|
|
|
|
|
foundation::{NSString, NSUInteger},
|
|
|
|
|
};
|
|
|
|
|
use core_graphics::display::{CGDirectDisplayID, CGDisplay, CGDisplayBounds, CGDisplayMode};
|
|
|
|
|
use core_video_sys::{
|
|
|
|
|
kCVReturnSuccess, kCVTimeIsIndefinite, CVDisplayLinkCreateWithCGDisplay,
|
|
|
|
|
CVDisplayLinkGetNominalOutputVideoRefreshPeriod, CVDisplayLinkRelease,
|
|
|
|
|
};
|
2018-06-14 19:42:18 -04:00
|
|
|
|
2019-05-01 17:03:30 -06:00
|
|
|
use dpi::{PhysicalPosition, PhysicalSize};
|
2019-06-12 21:07:25 +03:00
|
|
|
use monitor::VideoMode;
|
2019-05-01 17:03:30 -06:00
|
|
|
use platform_impl::platform::util::IdRef;
|
2014-11-04 18:03:38 +01:00
|
|
|
|
2018-04-18 02:07:54 +08:00
|
|
|
#[derive(Clone, PartialEq)]
|
2019-02-05 10:30:33 -05:00
|
|
|
pub struct MonitorHandle(CGDirectDisplayID);
|
2014-11-04 18:03:38 +01:00
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn available_monitors() -> VecDeque<MonitorHandle> {
|
2018-06-16 10:14:12 -04:00
|
|
|
if let Ok(displays) = CGDisplay::active_displays() {
|
|
|
|
|
let mut monitors = VecDeque::with_capacity(displays.len());
|
2019-05-01 17:03:30 -06:00
|
|
|
for display in displays {
|
|
|
|
|
monitors.push_back(MonitorHandle(display));
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|
2017-09-01 11:04:57 +02:00
|
|
|
monitors
|
2018-06-16 10:14:12 -04:00
|
|
|
} else {
|
|
|
|
|
VecDeque::with_capacity(0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn primary_monitor() -> MonitorHandle {
|
2019-05-01 17:03:30 -06:00
|
|
|
MonitorHandle(CGDisplay::main().id)
|
2018-06-16 10:14:12 -04:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
impl fmt::Debug for MonitorHandle {
|
2018-05-14 08:14:57 -04:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-05-01 17:03:30 -06:00
|
|
|
// TODO: Do this using the proper fmt API
|
2018-05-14 08:14:57 -04:00
|
|
|
#[derive(Debug)]
|
2019-02-05 10:30:33 -05:00
|
|
|
struct MonitorHandle {
|
2018-05-14 08:14:57 -04:00
|
|
|
name: Option<String>,
|
|
|
|
|
native_identifier: u32,
|
2018-06-14 19:42:18 -04:00
|
|
|
dimensions: PhysicalSize,
|
|
|
|
|
position: PhysicalPosition,
|
|
|
|
|
hidpi_factor: f64,
|
2018-05-14 08:14:57 -04:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
let monitor_id_proxy = MonitorHandle {
|
2019-05-29 21:29:54 -04:00
|
|
|
name: self.name(),
|
|
|
|
|
native_identifier: self.native_identifier(),
|
|
|
|
|
dimensions: self.dimensions(),
|
|
|
|
|
position: self.position(),
|
|
|
|
|
hidpi_factor: self.hidpi_factor(),
|
2018-05-14 08:14:57 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
monitor_id_proxy.fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
impl MonitorHandle {
|
2019-05-01 17:03:30 -06:00
|
|
|
pub fn new(id: CGDirectDisplayID) -> Self {
|
|
|
|
|
MonitorHandle(id)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn name(&self) -> Option<String> {
|
2019-02-05 10:30:33 -05:00
|
|
|
let MonitorHandle(display_id) = *self;
|
2017-10-31 12:03:18 +02:00
|
|
|
let screen_num = CGDisplay::new(display_id).model_number();
|
2014-11-04 18:03:38 +01:00
|
|
|
Some(format!("Monitor #{}", screen_num))
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn native_identifier(&self) -> u32 {
|
2017-08-30 08:49:18 +02:00
|
|
|
self.0
|
2015-03-16 13:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn dimensions(&self) -> PhysicalSize {
|
2019-02-05 10:30:33 -05:00
|
|
|
let MonitorHandle(display_id) = *self;
|
2017-10-31 12:03:18 +02:00
|
|
|
let display = CGDisplay::new(display_id);
|
2018-06-14 19:42:18 -04:00
|
|
|
let height = display.pixels_high();
|
|
|
|
|
let width = display.pixels_wide();
|
|
|
|
|
PhysicalSize::from_logical(
|
|
|
|
|
(width as f64, height as f64),
|
2019-05-29 21:29:54 -04:00
|
|
|
self.hidpi_factor(),
|
2018-06-14 19:42:18 -04:00
|
|
|
)
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|
2017-09-07 09:33:46 +01:00
|
|
|
|
|
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn position(&self) -> PhysicalPosition {
|
|
|
|
|
let bounds = unsafe { CGDisplayBounds(self.native_identifier()) };
|
2018-06-14 19:42:18 -04:00
|
|
|
PhysicalPosition::from_logical(
|
|
|
|
|
(bounds.origin.x as f64, bounds.origin.y as f64),
|
2019-05-29 21:29:54 -04:00
|
|
|
self.hidpi_factor(),
|
2018-06-14 19:42:18 -04:00
|
|
|
)
|
2017-09-07 09:33:46 +01:00
|
|
|
}
|
2017-10-17 14:56:38 +03:00
|
|
|
|
2019-05-29 21:29:54 -04:00
|
|
|
pub fn hidpi_factor(&self) -> f64 {
|
2019-06-11 01:09:38 +02:00
|
|
|
let screen = match self.ns_screen() {
|
2018-03-06 09:35:04 +01:00
|
|
|
Some(screen) => screen,
|
|
|
|
|
None => return 1.0, // default to 1.0 when we can't find the screen
|
|
|
|
|
};
|
2018-06-14 19:42:18 -04:00
|
|
|
unsafe { NSScreen::backingScaleFactor(screen) as f64 }
|
2018-03-06 09:35:04 +01:00
|
|
|
}
|
2019-06-12 21:07:25 +03:00
|
|
|
|
|
|
|
|
pub fn video_modes(&self) -> impl Iterator<Item = VideoMode> {
|
|
|
|
|
let cv_refresh_rate = unsafe {
|
|
|
|
|
let mut display_link = std::ptr::null_mut();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
CVDisplayLinkCreateWithCGDisplay(self.0, &mut display_link),
|
|
|
|
|
kCVReturnSuccess
|
|
|
|
|
);
|
|
|
|
|
let time = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(display_link);
|
|
|
|
|
CVDisplayLinkRelease(display_link);
|
|
|
|
|
|
|
|
|
|
// This value is indefinite if an invalid display link was specified
|
|
|
|
|
assert!(time.flags & kCVTimeIsIndefinite == 0);
|
|
|
|
|
|
|
|
|
|
time.timeScale as i64 / time.timeValue
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CGDisplayMode::all_display_modes(self.0, std::ptr::null())
|
|
|
|
|
.expect("failed to obtain list of display modes")
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(move |mode| {
|
|
|
|
|
let cg_refresh_rate = mode.refresh_rate().round() as i64;
|
|
|
|
|
|
|
|
|
|
// CGDisplayModeGetRefreshRate returns 0.0 for any display that
|
|
|
|
|
// isn't a CRT
|
|
|
|
|
let refresh_rate = if cg_refresh_rate > 0 {
|
|
|
|
|
cg_refresh_rate
|
|
|
|
|
} else {
|
|
|
|
|
cv_refresh_rate
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VideoMode {
|
|
|
|
|
dimensions: (mode.width() as u32, mode.height() as u32),
|
|
|
|
|
refresh_rate: refresh_rate as u16,
|
|
|
|
|
bit_depth: mode.bit_depth() as u16,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-03-06 09:35:04 +01:00
|
|
|
|
2019-06-11 01:09:38 +02:00
|
|
|
pub(crate) fn ns_screen(&self) -> Option<id> {
|
2018-03-06 09:35:04 +01:00
|
|
|
unsafe {
|
2019-05-29 21:29:54 -04:00
|
|
|
let native_id = self.native_identifier();
|
2018-03-06 09:35:04 +01:00
|
|
|
let screens = NSScreen::screens(nil);
|
|
|
|
|
let count: NSUInteger = msg_send![screens, count];
|
|
|
|
|
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
|
|
|
|
|
let mut matching_screen: Option<id> = None;
|
|
|
|
|
for i in 0..count {
|
|
|
|
|
let screen = msg_send![screens, objectAtIndex: i as NSUInteger];
|
|
|
|
|
let device_description = NSScreen::deviceDescription(screen);
|
|
|
|
|
let value: id = msg_send![device_description, objectForKey:*key];
|
|
|
|
|
if value != nil {
|
|
|
|
|
let screen_number: NSUInteger = msg_send![value, unsignedIntegerValue];
|
|
|
|
|
if screen_number as u32 == native_id {
|
|
|
|
|
matching_screen = Some(screen);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
matching_screen
|
|
|
|
|
}
|
2017-10-17 14:56:38 +03:00
|
|
|
}
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|