From 9dd592600ac91f7fe00a6e2af6651755b38f57a7 Mon Sep 17 00:00:00 2001 From: David Partouche Date: Tue, 4 Nov 2014 18:03:38 +0100 Subject: [PATCH] Retrieve the monitors and their info for osx --- Cargo.toml | 6 ++++++ src/lib.rs | 2 ++ src/osx/mod.rs | 23 +++-------------------- src/osx/monitor.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 src/osx/monitor.rs diff --git a/Cargo.toml b/Cargo.toml index 4cae79d0..7157229d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,9 @@ git = "https://github.com/servo/rust-core-foundation" [target.x86_64-apple-darwin.dependencies.core_foundation] git = "https://github.com/servo/rust-core-foundation" + +[target.i686-apple-darwin.dependencies.core_graphics] +git = "https://github.com/servo/rust-core-graphics" + +[target.x86_64-apple-darwin.dependencies.core_graphics] +git = "https://github.com/servo/rust-core-graphics" diff --git a/src/lib.rs b/src/lib.rs index d8c546a9..f1f8d5ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,8 @@ extern crate libc; extern crate cocoa; #[cfg(target_os = "macos")] extern crate core_foundation; +#[cfg(target_os = "macos")] +extern crate core_graphics; #[cfg(target_os = "linux")] extern crate sync; diff --git a/src/osx/mod.rs b/src/osx/mod.rs index 3e0d668a..3dda56aa 100644 --- a/src/osx/mod.rs +++ b/src/osx/mod.rs @@ -22,6 +22,9 @@ use {MouseInput, Pressed, Released, LeftMouseButton, RightMouseButton, MouseMove use events; +pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; + +mod monitor; mod event; static mut shift_pressed: bool = false; @@ -43,26 +46,6 @@ impl Deref for HeadlessContext { } } -pub struct MonitorID; - -pub fn get_available_monitors() -> Vec { - unimplemented!() -} - -pub fn get_primary_monitor() -> MonitorID { - unimplemented!() -} - -impl MonitorID { - pub fn get_name(&self) -> Option { - unimplemented!() - } - - pub fn get_dimensions(&self) -> (uint, uint) { - unimplemented!() - } -} - #[cfg(feature = "window")] impl Window { pub fn new(builder: WindowBuilder) -> Result { diff --git a/src/osx/monitor.rs b/src/osx/monitor.rs new file mode 100644 index 00000000..383fd322 --- /dev/null +++ b/src/osx/monitor.rs @@ -0,0 +1,46 @@ +use core_graphics::display; + +pub struct MonitorID(u32); + +pub fn get_available_monitors() -> Vec { + 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 { + 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 + } +}