winit-sonoma-fix/src/api/x11/monitor.rs

73 lines
2.1 KiB
Rust
Raw Normal View History

use std::ptr;
use std::collections::VecDeque;
use super::ffi;
use super::ensure_thread_init;
use native_monitor::NativeMonitorId;
pub struct MonitorID(pub u32);
pub fn get_available_monitors() -> VecDeque<MonitorID> {
2015-05-07 21:14:09 +10:00
let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
ensure_thread_init(&xlib);
let nb_monitors = unsafe {
2015-05-07 21:14:09 +10:00
let display = (xlib.XOpenDisplay)(ptr::null());
if display.is_null() {
2014-10-30 18:14:09 +11:00
panic!("get_available_monitors failed");
}
2015-05-07 21:14:09 +10:00
let nb_monitors = (xlib.XScreenCount)(display);
(xlib.XCloseDisplay)(display);
nb_monitors
};
let mut monitors = VecDeque::new();
monitors.extend((0..nb_monitors).map(|i| MonitorID(i as u32)));
monitors
}
pub fn get_primary_monitor() -> MonitorID {
2015-05-07 21:14:09 +10:00
let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
ensure_thread_init(&xlib);
let primary_monitor = unsafe {
2015-05-07 21:14:09 +10:00
let display = (xlib.XOpenDisplay)(ptr::null());
if display.is_null() {
2014-10-30 18:14:09 +11:00
panic!("get_available_monitors failed");
}
2015-05-07 21:14:09 +10:00
let primary_monitor = (xlib.XDefaultScreen)(display);
(xlib.XCloseDisplay)(display);
primary_monitor
};
MonitorID(primary_monitor as u32)
}
impl MonitorID {
pub fn get_name(&self) -> Option<String> {
let MonitorID(screen_num) = *self;
Some(format!("Monitor #{}", screen_num))
}
pub fn get_native_identifier(&self) -> NativeMonitorId {
let MonitorID(screen_num) = *self;
NativeMonitorId::Numeric(screen_num)
}
pub fn get_dimensions(&self) -> (u32, u32) {
2015-05-07 21:14:09 +10:00
let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
let dimensions = unsafe {
2015-05-07 21:14:09 +10:00
let display = (xlib.XOpenDisplay)(ptr::null());
let MonitorID(screen_num) = *self;
2015-05-07 21:14:09 +10:00
let screen = (xlib.XScreenOfDisplay)(display, screen_num as i32);
let width = (xlib.XWidthOfScreen)(screen);
let height = (xlib.XHeightOfScreen)(screen);
(xlib.XCloseDisplay)(display);
(width as u32, height as u32)
};
dimensions
}
}