2015-02-22 00:40:23 +11:00
|
|
|
use std::collections::VecDeque;
|
2015-05-17 11:19:06 +02:00
|
|
|
use std::sync::Arc;
|
2014-09-19 15:42:47 +02:00
|
|
|
|
2015-05-17 11:19:06 +02:00
|
|
|
use super::XConnection;
|
2014-09-19 15:42:47 +02:00
|
|
|
|
2015-07-19 13:53:40 +02:00
|
|
|
#[derive(Clone)]
|
2015-09-24 09:11:59 +02:00
|
|
|
pub struct MonitorId(pub Arc<XConnection>, pub u32);
|
2015-05-07 13:14:09 +02:00
|
|
|
|
2015-09-24 09:11:59 +02:00
|
|
|
pub fn get_available_monitors(x: &Arc<XConnection>) -> VecDeque<MonitorId> {
|
2015-05-17 11:19:06 +02:00
|
|
|
let nb_monitors = unsafe { (x.xlib.XScreenCount)(x.display) };
|
2015-12-24 10:57:08 +01:00
|
|
|
x.check_errors().expect("Failed to call XScreenCount");
|
2014-09-19 15:42:47 +02:00
|
|
|
|
2015-02-22 00:40:23 +11:00
|
|
|
let mut monitors = VecDeque::new();
|
2015-09-24 09:11:59 +02:00
|
|
|
monitors.extend((0 .. nb_monitors).map(|i| MonitorId(x.clone(), i as u32)));
|
2015-01-01 23:09:16 -08:00
|
|
|
monitors
|
2014-09-19 15:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2015-09-24 09:11:59 +02:00
|
|
|
pub fn get_primary_monitor(x: &Arc<XConnection>) -> MonitorId {
|
2015-05-17 11:19:06 +02:00
|
|
|
let primary_monitor = unsafe { (x.xlib.XDefaultScreen)(x.display) };
|
2015-12-24 10:57:08 +01:00
|
|
|
x.check_errors().expect("Failed to call XDefaultScreen");
|
2015-09-24 09:11:59 +02:00
|
|
|
MonitorId(x.clone(), primary_monitor as u32)
|
2014-09-19 15:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-24 09:11:59 +02:00
|
|
|
impl MonitorId {
|
2014-09-19 15:42:47 +02:00
|
|
|
pub fn get_name(&self) -> Option<String> {
|
2015-09-24 09:11:59 +02:00
|
|
|
let MonitorId(_, screen_num) = *self;
|
2014-09-19 19:57:52 +02:00
|
|
|
Some(format!("Monitor #{}", screen_num))
|
2014-09-19 15:42:47 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2017-08-30 08:49:18 +02:00
|
|
|
pub fn get_native_identifier(&self) -> u32 {
|
|
|
|
|
self.1
|
2015-03-16 13:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-13 15:21:36 +03:00
|
|
|
pub fn get_dimensions(&self) -> (u32, u32) {
|
2015-05-17 11:19:06 +02:00
|
|
|
let screen = unsafe { (self.0.xlib.XScreenOfDisplay)(self.0.display, self.1 as i32) };
|
|
|
|
|
let width = unsafe { (self.0.xlib.XWidthOfScreen)(screen) };
|
|
|
|
|
let height = unsafe { (self.0.xlib.XHeightOfScreen)(screen) };
|
2015-12-24 10:57:08 +01:00
|
|
|
self.0.check_errors().expect("Failed to get monitor dimensions");
|
2015-05-17 11:19:06 +02:00
|
|
|
(width as u32, height as u32)
|
2014-09-19 15:42:47 +02:00
|
|
|
}
|
|
|
|
|
}
|