2015-01-02 18:09:16 +11:00
|
|
|
use std::ptr;
|
2015-02-22 00:40:23 +11:00
|
|
|
use std::collections::VecDeque;
|
2015-04-24 22:22:57 +10:00
|
|
|
use super::ffi;
|
2014-10-24 15:49:07 +11:00
|
|
|
use super::ensure_thread_init;
|
2015-03-19 08:16:35 +11:00
|
|
|
use native_monitor::NativeMonitorId;
|
2014-09-19 23:42:47 +10:00
|
|
|
|
2015-01-13 23:21:36 +11:00
|
|
|
pub struct MonitorID(pub u32);
|
2014-09-19 23:42:47 +10:00
|
|
|
|
2015-02-22 00:40:23 +11:00
|
|
|
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);
|
2014-09-19 23:42:47 +10:00
|
|
|
let nb_monitors = unsafe {
|
2015-05-07 21:14:09 +10:00
|
|
|
let display = (xlib.XOpenDisplay)(ptr::null());
|
2014-09-19 23:42:47 +10:00
|
|
|
if display.is_null() {
|
2014-10-30 18:14:09 +11:00
|
|
|
panic!("get_available_monitors failed");
|
2014-09-19 23:42:47 +10:00
|
|
|
}
|
2015-05-07 21:14:09 +10:00
|
|
|
let nb_monitors = (xlib.XScreenCount)(display);
|
|
|
|
(xlib.XCloseDisplay)(display);
|
2014-09-19 23:42:47 +10:00
|
|
|
nb_monitors
|
|
|
|
};
|
|
|
|
|
2015-02-22 00:40:23 +11:00
|
|
|
let mut monitors = VecDeque::new();
|
2015-03-23 09:54:18 +11:00
|
|
|
monitors.extend((0..nb_monitors).map(|i| MonitorID(i as u32)));
|
2015-01-02 18:09:16 +11:00
|
|
|
monitors
|
2014-09-19 23:42:47 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-09-19 23:42:47 +10:00
|
|
|
let primary_monitor = unsafe {
|
2015-05-07 21:14:09 +10:00
|
|
|
let display = (xlib.XOpenDisplay)(ptr::null());
|
2014-09-19 23:42:47 +10:00
|
|
|
if display.is_null() {
|
2014-10-30 18:14:09 +11:00
|
|
|
panic!("get_available_monitors failed");
|
2014-09-19 23:42:47 +10:00
|
|
|
}
|
2015-05-07 21:14:09 +10:00
|
|
|
let primary_monitor = (xlib.XDefaultScreen)(display);
|
|
|
|
(xlib.XCloseDisplay)(display);
|
2014-09-19 23:42:47 +10:00
|
|
|
primary_monitor
|
|
|
|
};
|
|
|
|
|
2015-01-13 23:21:36 +11:00
|
|
|
MonitorID(primary_monitor as u32)
|
2014-09-19 23:42:47 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MonitorID {
|
|
|
|
pub fn get_name(&self) -> Option<String> {
|
2014-09-20 03:57:52 +10:00
|
|
|
let MonitorID(screen_num) = *self;
|
|
|
|
Some(format!("Monitor #{}", screen_num))
|
2014-09-19 23:42:47 +10:00
|
|
|
}
|
|
|
|
|
2015-03-19 08:16:35 +11:00
|
|
|
pub fn get_native_identifier(&self) -> NativeMonitorId {
|
2015-03-17 07:52:58 +11:00
|
|
|
let MonitorID(screen_num) = *self;
|
2015-03-19 08:16:35 +11:00
|
|
|
NativeMonitorId::Numeric(screen_num)
|
2015-03-17 07:52:58 +11:00
|
|
|
}
|
|
|
|
|
2015-01-13 23:21:36 +11:00
|
|
|
pub fn get_dimensions(&self) -> (u32, u32) {
|
2015-05-07 21:14:09 +10:00
|
|
|
let xlib = ffi::Xlib::open().unwrap(); // FIXME: gracious handling
|
|
|
|
|
2014-09-19 23:42:47 +10:00
|
|
|
let dimensions = unsafe {
|
2015-05-07 21:14:09 +10:00
|
|
|
let display = (xlib.XOpenDisplay)(ptr::null());
|
2014-09-19 23:42:47 +10:00
|
|
|
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);
|
2015-01-13 23:21:36 +11:00
|
|
|
(width as u32, height as u32)
|
2014-09-19 23:42:47 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
dimensions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|