From ac74db979c9e1daa73c5f232e9a144e9f355990a Mon Sep 17 00:00:00 2001 From: DavidPartouche Date: Fri, 19 Sep 2014 15:42:47 +0200 Subject: Get the monitors attached to the display, and their resolution for X11 --- src/x11/ffi.rs | 5 +++++ src/x11/mod.rs | 27 ++++----------------------- src/x11/monitor.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 src/x11/monitor.rs diff --git a/src/x11/ffi.rs b/src/x11/ffi.rs index f781b48..fe91033 100644 --- a/src/x11/ffi.rs +++ b/src/x11/ffi.rs @@ -29,6 +29,7 @@ pub type XrmDatabase = *const (); // TODO: not sure pub type XIC = *mut (); pub type XID = uint; pub type XIM = *mut (); +pub type Screen = (); pub static AllocNone: libc::c_int = 0; pub static AllocAll: libc::c_int = 1; @@ -1393,6 +1394,10 @@ extern "C" { pub fn XSetWMProtocols(display: *mut Display, w: Window, protocols: *mut Atom, count: libc::c_int) -> Status; pub fn XStoreName(display: *mut Display, w: Window, window_name: *const libc::c_char); + pub fn XScreenCount(display: *mut Display) -> libc::c_int; + pub fn XScreenOfDisplay(display: *mut Display, screen_number: libc::c_int) -> *const Screen; + pub fn XWidthOfScreen(screen: *const Screen) -> libc::c_int; + pub fn XHeightOfScreen(screen: *const Screen) -> libc::c_int; pub fn XCloseIM(im: XIM) -> Status; pub fn XOpenIM(display: *mut Display, db: XrmDatabase, res_name: *mut libc::c_char, diff --git a/src/x11/mod.rs b/src/x11/mod.rs index 7dd3fb0..0f5f557 100644 --- a/src/x11/mod.rs +++ b/src/x11/mod.rs @@ -3,8 +3,11 @@ use libc; use std::{mem, ptr}; use std::sync::atomics::AtomicBool; +pub use self::monitor::{MonitorID, get_available_monitors, get_primary_monitor}; + mod events; mod ffi; +mod monitor; pub struct Window { display: *mut ffi::Display, @@ -19,28 +22,6 @@ pub struct Window { is_fullscreen: bool, } -pub struct MonitorID(uint); - -pub fn get_available_monitors() -> Vec { - vec![get_primary_monitor()] -} - -pub fn get_primary_monitor() -> MonitorID { - MonitorID(0u) -} - -impl MonitorID { - pub fn get_name(&self) -> Option { - Some("".to_string()) - } - - pub fn get_dimensions(&self) -> (uint, uint) { - //unimplemented!() - // TODO: Get the real dimensions from the monitor - (1024, 768) - } -} - impl Window { pub fn new(builder: WindowBuilder) -> Result { let dimensions = builder.dimensions.unwrap_or((800, 600)); @@ -159,7 +140,7 @@ impl Window { // finally creating the window let window = unsafe { - let win = ffi::XCreateWindow(display, root, 50, 50, dimensions.val0() as libc::c_uint, + let win = ffi::XCreateWindow(display, root, 0, 0, dimensions.val0() as libc::c_uint, dimensions.val1() as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput, visual_infos.visual, window_attributes, &mut set_win_attr); diff --git a/src/x11/monitor.rs b/src/x11/monitor.rs new file mode 100644 index 0000000..2774d5f --- /dev/null +++ b/src/x11/monitor.rs @@ -0,0 +1,54 @@ +use std::{ptr}; +use super::ffi; + +pub struct MonitorID(uint); + +pub fn get_available_monitors() -> Vec { + let nb_monitors = unsafe { + let display = ffi::XOpenDisplay(ptr::null()); + if display.is_null() { + fail!("get_available_monitors failed"); + } + let nb_monitors = ffi::XScreenCount(display); + ffi::XCloseDisplay(display); + nb_monitors + }; + + let mut vec = Vec::new(); + vec.grow_fn(nb_monitors as uint, |i| MonitorID(i)); + vec +} + +pub fn get_primary_monitor() -> MonitorID { + let primary_monitor = unsafe { + let display = ffi::XOpenDisplay(ptr::null()); + if display.is_null() { + fail!("get_available_monitors failed"); + } + let primary_monitor = ffi::XDefaultScreen(display); + ffi::XCloseDisplay(display); + primary_monitor + }; + + MonitorID(primary_monitor as uint) +} + +impl MonitorID { + pub fn get_name(&self) -> Option { + Some("".to_string()) + } + + pub fn get_dimensions(&self) -> (uint, uint) { + 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); + (width as uint, height as uint) + }; + + dimensions + } +} + -- cgit v1.2.3