Unify fullscreen and fullscreen_windowed APIs

Use the enum to make a single fullscreen API that's much more
consistent. Both set_fullscreen() and with_fullscreen() take the
same enum and support all the variations so you can build the window
however you want and switch between the modes at runtime.
This commit is contained in:
Pedro Côrte-Real 2017-08-29 01:36:24 +01:00
parent b35c4a5ee5
commit 1382adbf11
4 changed files with 170 additions and 134 deletions

View file

@ -357,17 +357,17 @@ pub enum MouseCursor {
}
/// Describes if the Window is in one of the fullscreen modes
#[derive(Clone, PartialEq)]
#[derive(Clone)]
pub enum FullScreenState {
None,
Windowed,
Exclusive(platform::MonitorId),
Exclusive(MonitorId),
}
impl FullScreenState {
pub fn get_monitor(&self) -> Option<platform::MonitorId> {
if let FullScreenState::Exclusive(ref monitor) = *self {
Some(monitor.clone())
Some(monitor.0.clone())
} else {
None
}

View file

@ -4,7 +4,7 @@ use std::collections::VecDeque;
use std::sync::Arc;
use std::env;
use {CreationError, CursorState, EventsLoopClosed, MouseCursor, ControlFlow};
use {CreationError, CursorState, EventsLoopClosed, MouseCursor, ControlFlow, FullScreenState};
use libc;
use self::x11::XConnection;
@ -319,9 +319,9 @@ impl Window2 {
}
#[inline]
pub fn set_fullscreen_windowed(&self, fullscreen: bool) {
pub fn set_fullscreen(&self, state: FullScreenState) {
match self {
&Window2::X(ref w) => w.set_fullscreen_windowed(fullscreen),
&Window2::X(ref w) => w.set_fullscreen(state),
&Window2::Wayland(ref _w) => {},
}
}

View file

@ -15,9 +15,12 @@ use FullScreenState;
use platform::PlatformSpecificWindowBuilderAttributes;
use platform::MonitorId as PlatformMonitorId;
use window::MonitorId as RootMonitorId;
use super::{ffi};
use super::{MonitorId, XConnection, WindowId, EventsLoop};
use super::{XConnection, WindowId, EventsLoop};
use super::MonitorId as X11MonitorId;
// TODO: remove me
fn with_c_str<F, T>(s: &str, f: F) -> T where F: FnOnce(*const libc::c_char) -> T {
@ -37,9 +40,8 @@ pub struct XWindow {
display: Arc<XConnection>,
window: ffi::Window,
root: ffi::Window,
is_fullscreen: bool,
screen_id: libc::c_int,
xf86_desk_mode: Option<ffi::XF86VidModeModeInfo>,
// screen we're using, original screen mode if we've switched
fullscreen: Arc<Mutex<(i32, Option<ffi::XF86VidModeModeInfo>)>>,
window_proxy_data: Arc<Mutex<Option<WindowProxyData>>>,
}
@ -49,22 +51,105 @@ unsafe impl Sync for XWindow {}
unsafe impl Send for Window {}
unsafe impl Sync for Window {}
impl XWindow {
fn switch_to_fullscreen_mode(&self, monitor: i32, width: u16, height: u16) {
let original_monitor = {
let fullscreen = self.fullscreen.lock().unwrap();
fullscreen.0
};
if monitor != original_monitor {
// We're setting fullscreen on a new screen so first revert the original screen
self.switch_from_fullscreen_mode();
}
let current_mode = unsafe {
let mut mode_num: libc::c_int = mem::uninitialized();
let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized();
if (self.display.xf86vmode.XF86VidModeGetAllModeLines)(self.display.display, monitor, &mut mode_num, &mut modes) == 0 {
eprintln!("[winit] Couldn't get current resolution mode");
return
}
ptr::read(*modes.offset(0))
};
let new_mode = unsafe {
let mut mode_num: libc::c_int = mem::uninitialized();
let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized();
if (self.display.xf86vmode.XF86VidModeGetAllModeLines)(self.display.display, monitor, &mut mode_num, &mut modes) == 0 {
// There are no modes, mighty weird
eprintln!("[winit] X has no valid modes");
return
} else {
let matching_mode = (0 .. mode_num).map(|i| {
let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m
}).find(|m| m.hdisplay == width && m.vdisplay == height);
if let Some(matching_mode) = matching_mode {
matching_mode
} else {
let m = (0 .. mode_num).map(|i| {
let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m
}).find(|m| m.hdisplay >= width && m.vdisplay >= height);
match m {
Some(m) => m,
None => {
eprintln!("[winit] Could not find a suitable graphics mode");
return
}
}
}
}
};
if new_mode != current_mode {
// We actually need to change modes
self.set_mode(monitor, new_mode);
let mut fullscreen = self.fullscreen.lock().unwrap();
if fullscreen.1.is_none() {
// It's our first mode switch, save the original mode
fullscreen.1 = Some(current_mode);
}
}
}
fn switch_from_fullscreen_mode(&self) {
let (monitor, mode) = {
let fullscreen = self.fullscreen.lock().unwrap();
(fullscreen.0, fullscreen.1)
};
if let Some(mode) = mode {
self.set_mode(monitor, mode);
let mut fullscreen = self.fullscreen.lock().unwrap();
fullscreen.1 = None;
}
}
pub fn set_mode(&self, monitor: i32, mode: ffi::XF86VidModeModeInfo) {
unsafe {
let mut mode_to_switch_to = mode;
(self.display.xf86vmode.XF86VidModeSwitchToMode)(
self.display.display,
monitor,
&mut mode_to_switch_to
);
self.display.check_errors().expect("Failed to call XF86VidModeSwitchToMode");
(self.display.xf86vmode.XF86VidModeSetViewPort)(self.display.display, monitor, 0, 0);
self.display.check_errors().expect("Failed to call XF86VidModeSetViewPort");
}
}
}
impl Drop for XWindow {
fn drop(&mut self) {
unsafe {
// Clear out the window proxy data arc, so that any window proxy objects
// are no longer able to send messages to this window.
*self.window_proxy_data.lock().unwrap() = None;
if self.is_fullscreen {
if let Some(mut xf86_desk_mode) = self.xf86_desk_mode {
(self.display.xf86vmode.XF86VidModeSwitchToMode)(self.display.display, self.screen_id, &mut xf86_desk_mode);
}
(self.display.xf86vmode.XF86VidModeSetViewPort)(self.display.display, self.screen_id, 0, 0);
}
(self.display.xlib.XDestroyWindow)(self.display.display, self.window);
}
// Make sure we return the display to the original resolution if we've changed it
self.switch_from_fullscreen_mode();
}
}
@ -130,45 +215,11 @@ impl Window {
let screen_id = match pl_attribs.screen_id {
Some(id) => id,
None => match window_attrs.fullscreen {
FullScreenState::Exclusive(PlatformMonitorId::X(MonitorId(_, monitor))) => monitor as i32,
FullScreenState::Exclusive(RootMonitorId(PlatformMonitorId::X(X11MonitorId(_, monitor)))) => monitor as i32,
_ => unsafe { (display.xlib.XDefaultScreen)(display.display) },
}
};
let is_fullscreen = window_attrs.fullscreen.get_monitor().is_some();
// finding the mode to switch to if necessary
let (mode_to_switch_to, xf86_desk_mode) = unsafe {
let mut mode_num: libc::c_int = mem::uninitialized();
let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized();
if (display.xf86vmode.XF86VidModeGetAllModeLines)(display.display, screen_id, &mut mode_num, &mut modes) == 0 {
(None, None)
} else {
let xf86_desk_mode: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(0));
let mode_to_switch_to = if is_fullscreen {
let matching_mode = (0 .. mode_num).map(|i| {
let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m
}).find(|m| m.hdisplay == dimensions.0 as u16 && m.vdisplay == dimensions.1 as u16);
if let Some(matching_mode) = matching_mode {
Some(matching_mode)
} else {
let m = (0 .. mode_num).map(|i| {
let m: ffi::XF86VidModeModeInfo = ptr::read(*modes.offset(i as isize) as *const _); m
}).find(|m| m.hdisplay >= dimensions.0 as u16 && m.vdisplay >= dimensions.1 as u16);
match m {
Some(m) => Some(m),
None => return Err(OsError(format!("Could not find a suitable graphics mode")))
}
}
} else {
None
};
(display.xlib.XFree)(modes as *mut _);
(mode_to_switch_to, Some(xf86_desk_mode))
}
};
// getting the root window
let root = ctx.root;
@ -258,54 +309,26 @@ impl Window {
}
}
if is_fullscreen {
Window::set_netwm(display, window, root, "_NET_WM_STATE_FULLSCREEN", true);
if let Some(mut mode_to_switch_to) = mode_to_switch_to {
unsafe {
(display.xf86vmode.XF86VidModeSwitchToMode)(
display.display,
screen_id,
&mut mode_to_switch_to
);
display.check_errors().expect("Failed to call XF86VidModeSwitchToMode");
}
}
else {
println!("[glutin] Unexpected state: `mode` is None creating fullscreen window");
}
unsafe {
(display.xf86vmode.XF86VidModeSetViewPort)(display.display, screen_id, 0, 0);
display.check_errors().expect("Failed to call XF86VidModeSetViewPort");
}
} else {
// set size hints
let mut size_hints: ffi::XSizeHints = unsafe { mem::zeroed() };
size_hints.flags = ffi::PSize;
size_hints.width = dimensions.0 as i32;
size_hints.height = dimensions.1 as i32;
if let Some(dimensions) = window_attrs.min_dimensions {
size_hints.flags |= ffi::PMinSize;
size_hints.min_width = dimensions.0 as i32;
size_hints.min_height = dimensions.1 as i32;
}
if let Some(dimensions) = window_attrs.max_dimensions {
size_hints.flags |= ffi::PMaxSize;
size_hints.max_width = dimensions.0 as i32;
size_hints.max_height = dimensions.1 as i32;
}
unsafe {
(display.xlib.XSetNormalHints)(display.display, window, &mut size_hints);
display.check_errors().expect("Failed to call XSetNormalHints");
}
}
// Select XInput2 events
{
let mask = ffi::XI_MotionMask
@ -337,9 +360,7 @@ impl Window {
display: display.clone(),
window: window,
root: root,
screen_id: screen_id,
is_fullscreen: is_fullscreen,
xf86_desk_mode: xf86_desk_mode,
fullscreen: Arc::new(Mutex::new((screen_id, None))),
window_proxy_data: window_proxy_data,
}),
cursor_state: Mutex::new(CursorState::Normal),
@ -348,7 +369,7 @@ impl Window {
window.set_title(&window_attrs.title);
window.set_decorations(window_attrs.decorations);
window.set_maximized(window_attrs.maximized);
window.set_fullscreen_windowed(window_attrs.fullscreen == FullScreenState::Windowed);
window.set_fullscreen(window_attrs.fullscreen.clone());
if window_attrs.visible {
unsafe {
@ -427,8 +448,26 @@ impl Window {
}
}
pub fn set_fullscreen_windowed(&self, fullscreen: bool) {
Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", fullscreen);
pub fn set_fullscreen(&self, state: FullScreenState) {
match state {
FullScreenState::None => {
self.x.switch_from_fullscreen_mode();
Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", false);
},
FullScreenState::Windowed => {
self.x.switch_from_fullscreen_mode();
Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", true);
},
FullScreenState::Exclusive(RootMonitorId(PlatformMonitorId::X(X11MonitorId(_, monitor)))) => {
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);
Window::set_netwm(&self.x.display, self.x.window, self.x.root, "_NET_WM_STATE_FULLSCREEN", true);
} else {
eprintln!("[winit] Couldn't get window dimensions to go fullscreen");
}
}
_ => (),
}
}
pub fn set_maximized(&self, maximized: bool) {
@ -581,7 +620,12 @@ impl Window {
#[inline]
pub fn get_xlib_screen_id(&self) -> *mut libc::c_void {
self.x.screen_id as *mut libc::c_void
let screen_id = {
let fullscreen = self.x.fullscreen.lock().unwrap();
fullscreen.0
};
screen_id as *mut libc::c_void
}
#[inline]
@ -792,11 +836,16 @@ impl Window {
}
pub fn hidpi_factor(&self) -> f32 {
let screen_id = {
let fullscreen = self.x.fullscreen.lock().unwrap();
fullscreen.0
};
unsafe {
let x_px = (self.x.display.xlib.XDisplayWidth)(self.x.display.display, self.x.screen_id);
let y_px = (self.x.display.xlib.XDisplayHeight)(self.x.display.display, self.x.screen_id);
let x_mm = (self.x.display.xlib.XDisplayWidthMM)(self.x.display.display, self.x.screen_id);
let y_mm = (self.x.display.xlib.XDisplayHeightMM)(self.x.display.display, self.x.screen_id);
let x_px = (self.x.display.xlib.XDisplayWidth)(self.x.display.display, screen_id);
let y_px = (self.x.display.xlib.XDisplayHeight)(self.x.display.display, screen_id);
let x_mm = (self.x.display.xlib.XDisplayWidthMM)(self.x.display.display, screen_id);
let y_mm = (self.x.display.xlib.XDisplayHeightMM)(self.x.display.display, screen_id);
let ppmm = ((x_px as f32 * y_px as f32) / (x_mm as f32 * y_mm as f32)).sqrt();
((ppmm * (12.0 * 25.4 / 96.0)).round() / 12.0).max(1.0) // quantize with 1/12 step size.
}

View file

@ -57,25 +57,12 @@ impl WindowBuilder {
self
}
/// Requests fullscreen mode.
/// Sets the fullscreen mode.
///
/// If you don't specify dimensions for the window, it will match the monitor's.
/// Disables fullscreen windowed mode if set.
#[inline]
pub fn with_fullscreen(mut self, monitor: MonitorId) -> WindowBuilder {
let MonitorId(monitor) = monitor;
self.window.fullscreen = FullScreenState::Exclusive(monitor);
self
}
/// Requests fullscreen windowed mode.
///
/// Disables the non-windowed fullscreen mode if set
#[inline]
pub fn with_fullscreen_windowed(mut self, fullscreen: bool) -> WindowBuilder {
if fullscreen {
self.window.fullscreen = FullScreenState::Windowed;
}
pub fn with_fullscreen(mut self, state: FullScreenState) -> WindowBuilder {
self.window.fullscreen = state;
self
}
@ -322,8 +309,8 @@ impl Window {
/// Sets the window to fullscreen or back
#[inline]
pub fn set_fullscreen_windowed(&self, fullscreen: bool) {
self.window.set_fullscreen_windowed(fullscreen)
pub fn set_fullscreen(&self, state: FullScreenState) {
self.window.set_fullscreen(state)
}
#[inline]
@ -384,7 +371,7 @@ pub fn get_primary_monitor() -> MonitorId {
/// Identifier for a monitor.
#[derive(Clone, PartialEq)]
pub struct MonitorId(platform::MonitorId);
pub struct MonitorId(pub platform::MonitorId);
impl MonitorId {
/// Returns a human-readable name of the monitor.