Rework MonitorId::get_native_identifier (#267)

* Rework MonitorId::get_native_identifier

* Try fix compilation

* Returns the monitor ID on wayland as well

* Try fix compilation

* Fix iOS compilation
This commit is contained in:
tomaka 2017-08-30 08:49:18 +02:00 committed by GitHub
parent f7a8bcddb8
commit 7dc6fcdedc
16 changed files with 67 additions and 69 deletions

View file

@ -117,7 +117,6 @@ extern crate wayland_client;
pub use events::*; pub use events::*;
pub use window::{AvailableMonitorsIter, MonitorId, get_available_monitors, get_primary_monitor}; pub use window::{AvailableMonitorsIter, MonitorId, get_available_monitors, get_primary_monitor};
pub use native_monitor::NativeMonitorId;
#[macro_use] #[macro_use]
mod api_transition; mod api_transition;
@ -453,19 +452,3 @@ impl Default for WindowAttributes {
} }
} }
} }
mod native_monitor {
/// Native platform identifier for a monitor. Different platforms use fundamentally different types
/// to represent a monitor ID.
#[derive(Clone, PartialEq, Eq)]
pub enum NativeMonitorId {
/// Cocoa and X11 use a numeric identifier to represent a monitor.
Numeric(u32),
/// Win32 uses a Unicode string to represent a monitor.
Name(String),
/// Other platforms (Android) don't support monitor identification.
Unavailable
}
}

View file

@ -3,7 +3,7 @@
use std::convert::From; use std::convert::From;
use std::os::raw::c_void; use std::os::raw::c_void;
use cocoa::appkit::NSApplicationActivationPolicy; use cocoa::appkit::NSApplicationActivationPolicy;
use {Window, WindowBuilder}; use {MonitorId, Window, WindowBuilder};
/// Additional methods on `Window` that are specific to MacOS. /// Additional methods on `Window` that are specific to MacOS.
pub trait WindowExt { pub trait WindowExt {
@ -73,3 +73,16 @@ impl WindowBuilderExt for WindowBuilder {
self self
} }
} }
/// Additional methods on `MonitorId` that are specific to MacOS.
pub trait MonitorIdExt {
/// Returns the identifier of the monitor for Cocoa.
fn native_id(&self) -> u32;
}
impl MonitorIdExt for MonitorId {
#[inline]
fn native_id(&self) -> u32 {
self.inner.get_native_identifier()
}
}

View file

@ -3,6 +3,7 @@
use std::sync::Arc; use std::sync::Arc;
use std::ptr; use std::ptr;
use libc; use libc;
use MonitorId;
use Window; use Window;
use platform::Window2 as LinuxWindow; use platform::Window2 as LinuxWindow;
use platform::{UnixBackend, UNIX_BACKEND}; use platform::{UnixBackend, UNIX_BACKEND};
@ -148,3 +149,16 @@ impl WindowBuilderExt for WindowBuilder {
self self
} }
} }
/// Additional methods on `MonitorId` that are specific to Linux.
pub trait MonitorIdExt {
/// Returns the inner identifier of the monitor.
fn native_id(&self) -> u32;
}
impl MonitorIdExt for MonitorId {
#[inline]
fn native_id(&self) -> u32 {
self.inner.get_native_identifier()
}
}

View file

@ -1,6 +1,7 @@
#![cfg(target_os = "windows")] #![cfg(target_os = "windows")]
use libc; use libc;
use MonitorId;
use Window; use Window;
use WindowBuilder; use WindowBuilder;
use winapi; use winapi;
@ -35,3 +36,16 @@ impl WindowBuilderExt for WindowBuilder {
self self
} }
} }
/// Additional methods on `MonitorId` that are specific to Windows.
pub trait MonitorIdExt {
/// Returns the name of the monitor specific to the Win32 API.
fn native_id(&self) -> String;
}
impl MonitorIdExt for MonitorId {
#[inline]
fn native_id(&self) -> String {
self.inner.get_native_identifier()
}
}

View file

@ -16,7 +16,6 @@ use std::collections::VecDeque;
use CursorState; use CursorState;
use WindowAttributes; use WindowAttributes;
use FullScreenState; use FullScreenState;
use native_monitor::NativeMonitorId;
gen_api_transition!(); gen_api_transition!();
@ -48,11 +47,6 @@ impl MonitorId {
Some("Primary".to_string()) Some("Primary".to_string())
} }
#[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId {
NativeMonitorId::Unavailable
}
#[inline] #[inline]
pub fn get_dimensions(&self) -> (u32, u32) { pub fn get_dimensions(&self) -> (u32, u32) {
unimplemented!() unimplemented!()

View file

@ -70,7 +70,6 @@ use libc::c_int;
use objc::runtime::{Class, Object, Sel, BOOL, YES }; use objc::runtime::{Class, Object, Sel, BOOL, YES };
use objc::declare::{ ClassDecl }; use objc::declare::{ ClassDecl };
use native_monitor::NativeMonitorId;
use { CreationError, CursorState, MouseCursor, WindowAttributes, FullScreenState }; use { CreationError, CursorState, MouseCursor, WindowAttributes, FullScreenState };
use WindowEvent as Event; use WindowEvent as Event;
use events::{ Touch, TouchPhase }; use events::{ Touch, TouchPhase };
@ -153,11 +152,6 @@ impl MonitorId {
Some("Primary".to_string()) Some("Primary".to_string())
} }
#[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId {
NativeMonitorId::Unavailable
}
#[inline] #[inline]
pub fn get_dimensions(&self) -> (u32, u32) { pub fn get_dimensions(&self) -> (u32, u32) {
unimplemented!() unimplemented!()

View file

@ -142,7 +142,7 @@ impl MonitorId {
} }
#[inline] #[inline]
pub fn get_native_identifier(&self) -> ::native_monitor::NativeMonitorId { pub fn get_native_identifier(&self) -> u32 {
match self { match self {
&MonitorId::X(ref m) => m.get_native_identifier(), &MonitorId::X(ref m) => m.get_native_identifier(),
&MonitorId::Wayland(ref m) => m.get_native_identifier(), &MonitorId::Wayland(ref m) => m.get_native_identifier(),

View file

@ -372,8 +372,8 @@ impl MonitorId {
} }
#[inline] #[inline]
pub fn get_native_identifier(&self) -> ::native_monitor::NativeMonitorId { pub fn get_native_identifier(&self) -> u32 {
::native_monitor::NativeMonitorId::Unavailable self.id
} }
pub fn get_dimensions(&self) -> (u32, u32) { pub fn get_dimensions(&self) -> (u32, u32) {

View file

@ -56,7 +56,7 @@ impl Window {
*(decorated.handler()) = Some(DecoratedHandler::new()); *(decorated.handler()) = Some(DecoratedHandler::new());
// set fullscreen if necessary // set fullscreen if necessary
if let FullScreenState::Exclusive(RootMonitorId(PlatformMonitorId::Wayland(ref monitor_id))) = attributes.fullscreen { if let FullScreenState::Exclusive(RootMonitorId { inner: PlatformMonitorId::Wayland(ref monitor_id) }) = attributes.fullscreen {
ctxt.with_output(monitor_id.clone(), |output| { ctxt.with_output(monitor_id.clone(), |output| {
decorated.set_fullscreen(Some(output)) decorated.set_fullscreen(Some(output))
}); });

View file

@ -2,7 +2,6 @@ use std::collections::VecDeque;
use std::sync::Arc; use std::sync::Arc;
use super::XConnection; use super::XConnection;
use native_monitor::NativeMonitorId;
#[derive(Clone)] #[derive(Clone)]
pub struct MonitorId(pub Arc<XConnection>, pub u32); pub struct MonitorId(pub Arc<XConnection>, pub u32);
@ -30,8 +29,8 @@ impl MonitorId {
} }
#[inline] #[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId { pub fn get_native_identifier(&self) -> u32 {
NativeMonitorId::Numeric(self.1) self.1
} }
pub fn get_dimensions(&self) -> (u32, u32) { pub fn get_dimensions(&self) -> (u32, u32) {

View file

@ -215,7 +215,7 @@ impl Window {
let screen_id = match pl_attribs.screen_id { let screen_id = match pl_attribs.screen_id {
Some(id) => id, Some(id) => id,
None => match window_attrs.fullscreen { None => match window_attrs.fullscreen {
FullScreenState::Exclusive(RootMonitorId(PlatformMonitorId::X(X11MonitorId(_, monitor)))) => monitor as i32, FullScreenState::Exclusive(RootMonitorId { inner: PlatformMonitorId::X(X11MonitorId(_, monitor)) }) => monitor as i32,
_ => unsafe { (display.xlib.XDefaultScreen)(display.display) }, _ => unsafe { (display.xlib.XDefaultScreen)(display.display) },
} }
}; };
@ -458,7 +458,7 @@ impl Window {
self.x.switch_from_fullscreen_mode(); self.x.switch_from_fullscreen_mode();
Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", true); Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", true);
}, },
FullScreenState::Exclusive(RootMonitorId(PlatformMonitorId::X(X11MonitorId(_, monitor)))) => { FullScreenState::Exclusive(RootMonitorId { inner: PlatformMonitorId::X(X11MonitorId(_, monitor)) }) => {
if let Some(dimensions) = self.get_inner_size() { if let Some(dimensions) = self.get_inner_size() {
self.x.switch_to_fullscreen_mode(monitor as i32, dimensions.0 as u16, dimensions.1 as u16); self.x.switch_to_fullscreen_mode(monitor as i32, dimensions.0 as u16, dimensions.1 as u16);
Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", true); Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", true);

View file

@ -1,6 +1,5 @@
use core_graphics::display; use core_graphics::display;
use std::collections::VecDeque; use std::collections::VecDeque;
use native_monitor::NativeMonitorId;
#[derive(Clone)] #[derive(Clone)]
pub struct MonitorId(u32); pub struct MonitorId(u32);
@ -33,9 +32,8 @@ impl MonitorId {
} }
#[inline] #[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId { pub fn get_native_identifier(&self) -> u32 {
let MonitorId(display_id) = *self; self.0
NativeMonitorId::Numeric(display_id)
} }
pub fn get_dimensions(&self) -> (u32, u32) { pub fn get_dimensions(&self) -> (u32, u32) {

View file

@ -4,7 +4,6 @@ use libc;
use WindowAttributes; use WindowAttributes;
use FullScreenState; use FullScreenState;
use native_monitor::NativeMonitorId;
use os::macos::ActivationPolicy; use os::macos::ActivationPolicy;
use os::macos::WindowExt; use os::macos::WindowExt;
@ -386,10 +385,7 @@ impl Window {
unsafe { unsafe {
let screen = match attrs.fullscreen { let screen = match attrs.fullscreen {
FullScreenState::Exclusive(ref monitor_id) => { FullScreenState::Exclusive(ref monitor_id) => {
let native_id = match monitor_id.get_native_identifier() { let native_id = monitor_id.inner.get_native_identifier();
NativeMonitorId::Numeric(num) => num,
_ => panic!("OS X monitors should always have a numeric native ID")
};
let matching_screen = { let matching_screen = {
let screens = appkit::NSScreen::screens(nil); let screens = appkit::NSScreen::screens(nil);
let count: NSUInteger = msg_send![screens, count]; let count: NSUInteger = msg_send![screens, count];

View file

@ -4,8 +4,6 @@ use user32;
use std::collections::VecDeque; use std::collections::VecDeque;
use std::mem; use std::mem;
use native_monitor::NativeMonitorId;
/// Win32 implementation of the main `MonitorId` object. /// Win32 implementation of the main `MonitorId` object.
#[derive(Clone)] #[derive(Clone)]
pub struct MonitorId { pub struct MonitorId {
@ -158,8 +156,8 @@ impl MonitorId {
/// See the docs of the crate root file. /// See the docs of the crate root file.
#[inline] #[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId { pub fn get_native_identifier(&self) -> String {
NativeMonitorId::Name(self.monitor_name.clone()) self.monitor_name.clone()
} }
/// See the docs if the crate root file. /// See the docs if the crate root file.

View file

@ -329,8 +329,8 @@ unsafe fn init(window: WindowAttributes, pl_attribs: PlatformSpecificWindowBuild
// switching to fullscreen if necessary // switching to fullscreen if necessary
// this means adjusting the window's position so that it overlaps the right monitor, // this means adjusting the window's position so that it overlaps the right monitor,
// and change the monitor's resolution if necessary // and change the monitor's resolution if necessary
let fullscreen = if let FullScreenState::Exclusive(RootMonitorId(ref monitor)) = window.fullscreen { let fullscreen = if let FullScreenState::Exclusive(RootMonitorId { ref inner }) = window.fullscreen {
try!(switch_to_fullscreen(&mut rect, monitor)); try!(switch_to_fullscreen(&mut rect, inner));
true true
} else { } else {
false false

View file

@ -8,7 +8,6 @@ use Window;
use WindowBuilder; use WindowBuilder;
use WindowId; use WindowId;
use FullScreenState; use FullScreenState;
use native_monitor::NativeMonitorId;
use libc; use libc;
use platform; use platform;
@ -331,7 +330,7 @@ impl Iterator for AvailableMonitorsIter {
#[inline] #[inline]
fn next(&mut self) -> Option<MonitorId> { fn next(&mut self) -> Option<MonitorId> {
self.data.next().map(|id| MonitorId(id)) self.data.next().map(|id| MonitorId { inner: id })
} }
#[inline] #[inline]
@ -349,6 +348,7 @@ impl Iterator for AvailableMonitorsIter {
/// > and if it fails will fallback on x11. /// > and if it fails will fallback on x11.
/// > /// >
/// > If this variable is set with any other value, winit will panic. /// > If this variable is set with any other value, winit will panic.
// Note: should be replaced with `-> impl Iterator` once stable.
#[inline] #[inline]
pub fn get_available_monitors() -> AvailableMonitorsIter { pub fn get_available_monitors() -> AvailableMonitorsIter {
let data = platform::get_available_monitors(); let data = platform::get_available_monitors();
@ -366,32 +366,27 @@ pub fn get_available_monitors() -> AvailableMonitorsIter {
/// > If this variable is set with any other value, winit will panic. /// > If this variable is set with any other value, winit will panic.
#[inline] #[inline]
pub fn get_primary_monitor() -> MonitorId { pub fn get_primary_monitor() -> MonitorId {
MonitorId(platform::get_primary_monitor()) MonitorId { inner: platform::get_primary_monitor() }
} }
/// Identifier for a monitor. /// Identifier for a monitor.
#[derive(Clone)] #[derive(Clone)]
pub struct MonitorId(pub platform::MonitorId); pub struct MonitorId {
pub(crate) inner: platform::MonitorId
}
impl MonitorId { impl MonitorId {
/// Returns a human-readable name of the monitor. /// Returns a human-readable name of the monitor.
///
/// Returns `None` if the monitor doesn't exist anymore.
#[inline] #[inline]
pub fn get_name(&self) -> Option<String> { pub fn get_name(&self) -> Option<String> {
let &MonitorId(ref id) = self; self.inner.get_name()
id.get_name()
}
/// Returns the native platform identifier for this monitor.
#[inline]
pub fn get_native_identifier(&self) -> NativeMonitorId {
let &MonitorId(ref id) = self;
id.get_native_identifier()
} }
/// Returns the number of pixels currently displayed on the monitor. /// Returns the number of pixels currently displayed on the monitor.
#[inline] #[inline]
pub fn get_dimensions(&self) -> (u32, u32) { pub fn get_dimensions(&self) -> (u32, u32) {
let &MonitorId(ref id) = self; self.inner.get_dimensions()
id.get_dimensions()
} }
} }