2019-05-01 17:03:30 -06:00
|
|
|
use std::{collections::VecDeque, fmt};
|
2018-06-14 19:42:18 -04:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
use super::ffi;
|
|
|
|
|
use crate::{
|
|
|
|
|
dpi::{PhysicalPosition, PhysicalSize},
|
|
|
|
|
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
|
|
|
|
|
platform_impl::platform::util::IdRef,
|
|
|
|
|
};
|
2019-06-12 21:07:25 +03:00
|
|
|
use cocoa::{
|
|
|
|
|
appkit::NSScreen,
|
|
|
|
|
base::{id, nil},
|
|
|
|
|
foundation::{NSString, NSUInteger},
|
|
|
|
|
};
|
2019-07-29 21:16:14 +03:00
|
|
|
use core_foundation::{
|
|
|
|
|
array::{CFArrayGetCount, CFArrayGetValueAtIndex},
|
|
|
|
|
base::{CFRelease, TCFType},
|
|
|
|
|
string::CFString,
|
|
|
|
|
};
|
|
|
|
|
use core_graphics::display::{CGDirectDisplayID, CGDisplay, CGDisplayBounds};
|
2019-06-12 21:07:25 +03:00
|
|
|
use core_video_sys::{
|
|
|
|
|
kCVReturnSuccess, kCVTimeIsIndefinite, CVDisplayLinkCreateWithCGDisplay,
|
|
|
|
|
CVDisplayLinkGetNominalOutputVideoRefreshPeriod, CVDisplayLinkRelease,
|
|
|
|
|
};
|
2018-06-14 19:42:18 -04:00
|
|
|
|
2019-10-03 21:19:10 +01:00
|
|
|
#[derive(Clone)]
|
2019-07-29 21:16:14 +03:00
|
|
|
pub struct VideoMode {
|
|
|
|
|
pub(crate) size: (u32, u32),
|
|
|
|
|
pub(crate) bit_depth: u16,
|
|
|
|
|
pub(crate) refresh_rate: u16,
|
|
|
|
|
pub(crate) monitor: MonitorHandle,
|
|
|
|
|
pub(crate) native_mode: NativeDisplayMode,
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-03 21:19:10 +01:00
|
|
|
impl PartialEq for VideoMode {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
self.size == other.size
|
|
|
|
|
&& self.bit_depth == other.bit_depth
|
|
|
|
|
&& self.refresh_rate == other.refresh_rate
|
|
|
|
|
&& self.monitor == other.monitor
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Eq for VideoMode {}
|
|
|
|
|
|
|
|
|
|
impl std::hash::Hash for VideoMode {
|
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
self.size.hash(state);
|
|
|
|
|
self.bit_depth.hash(state);
|
|
|
|
|
self.refresh_rate.hash(state);
|
|
|
|
|
self.monitor.hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Debug for VideoMode {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
f.debug_struct("VideoMode")
|
|
|
|
|
.field("size", &self.size)
|
|
|
|
|
.field("bit_depth", &self.bit_depth)
|
|
|
|
|
.field("refresh_rate", &self.refresh_rate)
|
|
|
|
|
.field("monitor", &self.monitor)
|
|
|
|
|
.finish()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
pub struct NativeDisplayMode(pub ffi::CGDisplayModeRef);
|
|
|
|
|
|
|
|
|
|
unsafe impl Send for NativeDisplayMode {}
|
|
|
|
|
|
|
|
|
|
impl Drop for NativeDisplayMode {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
ffi::CGDisplayModeRelease(self.0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Clone for NativeDisplayMode {
|
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
|
unsafe {
|
|
|
|
|
ffi::CGDisplayModeRetain(self.0);
|
|
|
|
|
}
|
|
|
|
|
NativeDisplayMode(self.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VideoMode {
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn size(&self) -> PhysicalSize<u32> {
|
2019-07-29 21:16:14 +03:00
|
|
|
self.size.into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bit_depth(&self) -> u16 {
|
|
|
|
|
self.bit_depth
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn refresh_rate(&self) -> u16 {
|
|
|
|
|
self.refresh_rate
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn monitor(&self) -> RootMonitorHandle {
|
|
|
|
|
RootMonitorHandle {
|
|
|
|
|
inner: self.monitor.clone(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-04 18:03:38 +01:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
#[derive(Clone)]
|
2019-02-05 10:30:33 -05:00
|
|
|
pub struct MonitorHandle(CGDirectDisplayID);
|
2014-11-04 18:03:38 +01:00
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
// `CGDirectDisplayID` changes on video mode change, so we cannot rely on that
|
|
|
|
|
// for comparisons, but we can use `CGDisplayCreateUUIDFromDisplayID` to get an
|
|
|
|
|
// unique identifier that persists even across system reboots
|
|
|
|
|
impl PartialEq for MonitorHandle {
|
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
|
unsafe {
|
|
|
|
|
ffi::CGDisplayCreateUUIDFromDisplayID(self.0)
|
|
|
|
|
== ffi::CGDisplayCreateUUIDFromDisplayID(other.0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Eq for MonitorHandle {}
|
|
|
|
|
|
|
|
|
|
impl PartialOrd for MonitorHandle {
|
|
|
|
|
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
|
|
|
|
Some(self.cmp(&other))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Ord for MonitorHandle {
|
|
|
|
|
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
|
|
|
|
unsafe {
|
|
|
|
|
ffi::CGDisplayCreateUUIDFromDisplayID(self.0)
|
|
|
|
|
.cmp(&ffi::CGDisplayCreateUUIDFromDisplayID(other.0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::hash::Hash for MonitorHandle {
|
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
|
unsafe {
|
|
|
|
|
ffi::CGDisplayCreateUUIDFromDisplayID(self.0).hash(state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2019-06-18 02:27:00 +08: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,
|
2020-01-04 01:33:07 -05:00
|
|
|
size: PhysicalSize<u32>,
|
|
|
|
|
position: PhysicalPosition<i32>,
|
2020-01-03 14:52:27 -05:00
|
|
|
scale_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(),
|
2019-06-13 02:33:44 -04:00
|
|
|
size: self.size(),
|
2019-05-29 21:29:54 -04:00
|
|
|
position: self.position(),
|
2020-01-03 14:52:27 -05:00
|
|
|
scale_factor: self.scale_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
|
|
|
}
|
|
|
|
|
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn size(&self) -> PhysicalSize<u32> {
|
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();
|
2020-01-03 14:52:27 -05:00
|
|
|
PhysicalSize::from_logical::<_, f64>((width as f64, height as f64), self.scale_factor())
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|
2017-09-07 09:33:46 +01:00
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-04 01:33:07 -05:00
|
|
|
pub fn position(&self) -> PhysicalPosition<i32> {
|
2019-05-29 21:29:54 -04:00
|
|
|
let bounds = unsafe { CGDisplayBounds(self.native_identifier()) };
|
2020-01-04 01:33:07 -05:00
|
|
|
PhysicalPosition::from_logical::<_, f64>(
|
2018-06-14 19:42:18 -04:00
|
|
|
(bounds.origin.x as f64, bounds.origin.y as f64),
|
2020-01-03 14:52:27 -05:00
|
|
|
self.scale_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
|
|
|
|
2020-01-03 14:52:27 -05:00
|
|
|
pub fn scale_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
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
pub fn video_modes(&self) -> impl Iterator<Item = RootVideoMode> {
|
2019-06-12 21:07:25 +03:00
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
let monitor = self.clone();
|
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
let modes = {
|
|
|
|
|
let array = ffi::CGDisplayCopyAllDisplayModes(self.0, std::ptr::null());
|
|
|
|
|
assert!(!array.is_null(), "failed to get list of display modes");
|
|
|
|
|
let array_count = CFArrayGetCount(array);
|
|
|
|
|
let modes: Vec<_> = (0..array_count)
|
|
|
|
|
.map(move |i| {
|
|
|
|
|
let mode = CFArrayGetValueAtIndex(array, i) as *mut _;
|
|
|
|
|
ffi::CGDisplayModeRetain(mode);
|
|
|
|
|
mode
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
CFRelease(array as *const _);
|
|
|
|
|
modes
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
modes.into_iter().map(move |mode| {
|
|
|
|
|
let cg_refresh_rate = ffi::CGDisplayModeGetRefreshRate(mode).round() as i64;
|
2019-06-12 21:07:25 +03:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
};
|
|
|
|
|
|
2019-07-29 21:16:14 +03:00
|
|
|
let pixel_encoding =
|
|
|
|
|
CFString::wrap_under_create_rule(ffi::CGDisplayModeCopyPixelEncoding(mode))
|
|
|
|
|
.to_string();
|
|
|
|
|
let bit_depth = if pixel_encoding.eq_ignore_ascii_case(ffi::IO32BitDirectPixels) {
|
|
|
|
|
32
|
|
|
|
|
} else if pixel_encoding.eq_ignore_ascii_case(ffi::IO16BitDirectPixels) {
|
|
|
|
|
16
|
|
|
|
|
} else if pixel_encoding.eq_ignore_ascii_case(ffi::kIO30BitDirectPixels) {
|
|
|
|
|
30
|
|
|
|
|
} else {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let video_mode = VideoMode {
|
|
|
|
|
size: (
|
|
|
|
|
ffi::CGDisplayModeGetPixelWidth(mode) as u32,
|
|
|
|
|
ffi::CGDisplayModeGetPixelHeight(mode) as u32,
|
|
|
|
|
),
|
2019-06-12 21:07:25 +03:00
|
|
|
refresh_rate: refresh_rate as u16,
|
2019-07-29 21:16:14 +03:00
|
|
|
bit_depth,
|
|
|
|
|
monitor: monitor.clone(),
|
|
|
|
|
native_mode: NativeDisplayMode(mode),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RootVideoMode { video_mode }
|
2019-06-12 21:07:25 +03:00
|
|
|
})
|
2019-07-29 21:16:14 +03:00
|
|
|
}
|
2019-06-12 21:07:25 +03:00
|
|
|
}
|
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-07-29 21:16:14 +03:00
|
|
|
let uuid = ffi::CGDisplayCreateUUIDFromDisplayID(self.0);
|
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"));
|
|
|
|
|
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 {
|
2019-07-29 21:16:14 +03:00
|
|
|
let other_native_id: NSUInteger = msg_send![value, unsignedIntegerValue];
|
|
|
|
|
let other_uuid =
|
|
|
|
|
ffi::CGDisplayCreateUUIDFromDisplayID(other_native_id as CGDirectDisplayID);
|
|
|
|
|
if uuid == other_uuid {
|
|
|
|
|
return Some(screen);
|
2018-03-06 09:35:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-29 21:16:14 +03:00
|
|
|
None
|
2018-03-06 09:35:04 +01:00
|
|
|
}
|
2017-10-17 14:56:38 +03:00
|
|
|
}
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|