2015-01-02 18:09:16 +11:00
|
|
|
use std::ptr;
|
2015-02-22 00:40:23 +11:00
|
|
|
use std::collections::VecDeque;
|
2014-10-05 04:17:02 +11:00
|
|
|
use super::super::ffi;
|
2014-10-24 15:49:07 +11:00
|
|
|
use super::ensure_thread_init;
|
2015-03-17 09:43:33 +11:00
|
|
|
use window::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> {
|
2014-10-24 15:49:07 +11:00
|
|
|
ensure_thread_init();
|
2014-09-19 23:42:47 +10:00
|
|
|
let nb_monitors = unsafe {
|
|
|
|
let display = ffi::XOpenDisplay(ptr::null());
|
|
|
|
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
|
|
|
}
|
|
|
|
let nb_monitors = ffi::XScreenCount(display);
|
|
|
|
ffi::XCloseDisplay(display);
|
|
|
|
nb_monitors
|
|
|
|
};
|
|
|
|
|
2015-02-22 00:40:23 +11:00
|
|
|
let mut monitors = VecDeque::new();
|
2015-01-13 23:21:36 +11:00
|
|
|
monitors.extend(range(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 {
|
2014-10-24 15:49:07 +11:00
|
|
|
ensure_thread_init();
|
2014-09-19 23:42:47 +10:00
|
|
|
let primary_monitor = unsafe {
|
|
|
|
let display = ffi::XOpenDisplay(ptr::null());
|
|
|
|
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
|
|
|
}
|
|
|
|
let primary_monitor = ffi::XDefaultScreen(display);
|
|
|
|
ffi::XCloseDisplay(display);
|
|
|
|
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-17 07:52:58 +11:00
|
|
|
pub fn get_native_identifier(&self) -> NativeMonitorID {
|
|
|
|
let MonitorID(screen_num) = *self;
|
|
|
|
NativeMonitorID::Numeric(screen_num)
|
|
|
|
}
|
|
|
|
|
2015-01-13 23:21:36 +11:00
|
|
|
pub fn get_dimensions(&self) -> (u32, u32) {
|
2014-09-19 23:42:47 +10:00
|
|
|
let dimensions = unsafe {
|
|
|
|
let display = ffi::XOpenDisplay(ptr::null());
|
|
|
|
let MonitorID(screen_num) = *self;
|
|
|
|
let screen = ffi::XScreenOfDisplay(display, screen_num as i32);
|
|
|
|
let width = ffi::XWidthOfScreen(screen);
|
|
|
|
let height = ffi::XHeightOfScreen(screen);
|
2014-10-24 15:13:19 +11:00
|
|
|
ffi::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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|