blob: 29c23c9833d9ebb7746615760629c4097a386e30 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
use std::ptr;
use std::collections::RingBuf;
use super::super::ffi;
use super::ensure_thread_init;
pub struct MonitorID(pub usize);
pub fn get_available_monitors() -> RingBuf<MonitorID> {
ensure_thread_init();
let nb_monitors = unsafe {
let display = ffi::XOpenDisplay(ptr::null());
if display.is_null() {
panic!("get_available_monitors failed");
}
let nb_monitors = ffi::XScreenCount(display);
ffi::XCloseDisplay(display);
nb_monitors
};
let mut monitors = RingBuf::new();
monitors.extend(range(0, nb_monitors).map(|i| MonitorID(i as usize)));
monitors
}
pub fn get_primary_monitor() -> MonitorID {
ensure_thread_init();
let primary_monitor = unsafe {
let display = ffi::XOpenDisplay(ptr::null());
if display.is_null() {
panic!("get_available_monitors failed");
}
let primary_monitor = ffi::XDefaultScreen(display);
ffi::XCloseDisplay(display);
primary_monitor
};
MonitorID(primary_monitor as usize)
}
impl MonitorID {
pub fn get_name(&self) -> Option<String> {
let MonitorID(screen_num) = *self;
Some(format!("Monitor #{}", screen_num))
}
pub fn get_dimensions(&self) -> (usize, usize) {
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);
ffi::XCloseDisplay(display);
(width as usize, height as usize)
};
dimensions
}
}
|