From b149fca83d390c9c04277501811a617832edc682 Mon Sep 17 00:00:00 2001 From: David Partouche Date: Thu, 4 Sep 2014 11:38:33 +0200 Subject: Added fullscreen support for X11 --- src/x11/ffi.rs | 24 +++++++++++++++++ src/x11/mod.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 92 insertions(+), 17 deletions(-) (limited to 'src/x11') diff --git a/src/x11/ffi.rs b/src/x11/ffi.rs index 2bd2075..0bc52b5 100644 --- a/src/x11/ffi.rs +++ b/src/x11/ffi.rs @@ -1337,8 +1337,26 @@ pub struct XButtonEvent { pub same_screen: Bool, } +#[repr(C)] +pub struct XF86VidModeModeInfo { + pub dotclock: libc::c_uint, + pub hdisplay: libc::c_ushort, + pub hsyncstart: libc::c_ushort, + pub hsyncend: libc::c_ushort, + pub htotal: libc::c_ushort, + pub hskew: libc::c_ushort, + pub vdisplay: libc::c_ushort, + pub vsyncstart: libc::c_ushort, + pub vsyncend: libc::c_ushort, + pub vtotal: libc::c_ushort, + pub flags: libc::c_uint, + privsize: libc::c_int, + private: libc::c_long, +} + #[link(name = "GL")] #[link(name = "X11")] +#[link(name = "Xxf86vm")] extern "C" { pub fn XCloseDisplay(display: *mut Display); pub fn XCheckMaskEvent(display: *mut Display, event_mask: libc::c_long, @@ -1408,6 +1426,12 @@ extern "C" { pub fn glXSwapBuffers(dpy: *mut Display, drawable: GLXDrawable); pub fn XkbSetDetectableAutoRepeat(dpy: *mut Display, detectable: bool, supported_rtm: *mut bool) -> bool; + pub fn XF86VidModeSwitchToMode(dpy: *mut Display, screen: libc::c_int, + modeline: *mut XF86VidModeModeInfo) -> Bool; + pub fn XF86VidModeSetViewPort(dpy: *mut Display, screen: libc::c_int, + x: libc::c_int, y: libc::c_int) -> Bool; + pub fn XF86VidModeGetAllModeLines(dpy: *mut Display, screen: libc::c_int, + modecount_return: *mut libc::c_int, modesinfo: *mut *mut *mut XF86VidModeModeInfo) -> Bool; } /* diff --git a/src/x11/mod.rs b/src/x11/mod.rs index 1b1e116..21e46b7 100644 --- a/src/x11/mod.rs +++ b/src/x11/mod.rs @@ -14,16 +14,19 @@ pub struct Window { context: ffi::GLXContext, is_closed: AtomicBool, wm_delete_window: ffi::Atom, + xf86_desk_mode: *mut ffi::XF86VidModeModeInfo, + screen_id: libc::c_int, + is_fullscreen: bool, } pub struct MonitorID(uint); pub fn get_available_monitors() -> Vec { - unimplemented!() + vec![get_primary_monitor()] } pub fn get_primary_monitor() -> MonitorID { - unimplemented!() + MonitorID(0u) } impl MonitorID { @@ -32,14 +35,15 @@ impl MonitorID { } pub fn get_dimensions(&self) -> (uint, uint) { - unimplemented!() + //unimplemented!() + // TODO: Get the real dimensions from the monitor + (1024, 768) } } impl Window { pub fn new(builder: WindowBuilder) -> Result { - // TODO: temporary - let dimensions = builder.dimensions; + let dimensions = builder.dimensions.unwrap_or((800, 600)); // calling XOpenDisplay let display = unsafe { @@ -50,7 +54,9 @@ impl Window { display }; - // TODO: set error handler? + let screen_id = unsafe { + ffi::XDefaultScreen(display) + }; // getting the FBConfig let fb_config = unsafe { @@ -81,6 +87,31 @@ impl Window { preferred_fb }; + let mut best_mode = -1; + let modes = unsafe { + let mut mode_num: libc::c_int = mem::uninitialized(); + let mut modes: *mut *mut ffi::XF86VidModeModeInfo = mem::uninitialized(); + if ffi::XF86VidModeGetAllModeLines(display, screen_id, &mut mode_num, &mut modes) == 0 { + return Err(format!("Could not query the video modes")); + } + + for i in range(0, mode_num) { + let mode: ffi::XF86VidModeModeInfo = **modes.offset(i as int); + if mode.hdisplay == dimensions.val0() as u16 && mode.vdisplay == dimensions.val1() as u16 { + best_mode = i; + } + }; + if best_mode == -1 { + return Err(format!("Could not find a suitable graphics mode")); + } + + modes + }; + + let xf86_desk_mode = unsafe { + *modes.offset(0) + }; + // getting the visual infos let visual_infos = unsafe { let vi = ffi::glXGetVisualFromFBConfig(display, fb_config); @@ -109,18 +140,28 @@ impl Window { swa.colormap = cmap; swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask | ffi::VisibilityChangeMask | ffi::KeyPressMask | ffi::PointerMotionMask | - ffi::KeyPressMask | ffi::KeyReleaseMask | ffi::ButtonPressMask | + ffi::KeyReleaseMask | ffi::ButtonPressMask | ffi::ButtonReleaseMask | ffi::KeymapStateMask; + swa.border_pixel = 0; + swa.override_redirect = 0; swa }; + let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi:: CWEventMask; + if builder.is_fullscreen { + window_attributes |= ffi::CWOverrideRedirect; + unsafe { + ffi::XF86VidModeSwitchToMode(display, screen_id, *modes.offset(best_mode as int)); + ffi::XF86VidModeSetViewPort(display, screen_id, 0, 0); + set_win_attr.override_redirect = 1; + } + } + // finally creating the window let window = unsafe { - let dimensions = dimensions.unwrap_or((800, 600)); - let win = ffi::XCreateWindow(display, root, 50, 50, dimensions.val0() as libc::c_uint, dimensions.val1() as libc::c_uint, 0, visual_infos.depth, ffi::InputOutput, - visual_infos.visual, ffi::CWColormap | ffi::CWEventMask, + visual_infos.visual, window_attributes, &mut set_win_attr); win }; @@ -148,7 +189,7 @@ impl Window { addr = ffi::glXGetProcAddress(b"glXCreateContextAttribsARB".as_ptr() as *const u8) as *const (); } - + addr.to_option().map(|addr| { let addr: extern "system" fn(*mut ffi::Display, ffi::GLXFBConfig, ffi::GLXContext, ffi::Bool, *const libc::c_int) -> ffi::GLXContext = mem::transmute(addr); @@ -202,13 +243,13 @@ impl Window { } attributes.push(0); - + let context = if create_context_attribs.is_some() { let create_context_attribs = create_context_attribs.unwrap(); create_context_attribs(display, fb_config, ptr::null(), 1, attributes.as_ptr()) } else { - ffi::glXCreateNewContext(display, fb_config, ffi::GLX_RGBA_TYPE, ptr::null(), 1) + ffi::glXCreateContext(display, &visual_infos, ptr::null(), 1) }; if context.is_null() { @@ -227,6 +268,9 @@ impl Window { context: context, is_closed: AtomicBool::new(false), wm_delete_window: wm_delete_window, + xf86_desk_mode: xf86_desk_mode, + screen_id: screen_id, + is_fullscreen: builder.is_fullscreen, }; // calling glViewport @@ -300,9 +344,9 @@ impl Window { pub fn poll_events(&self) -> Vec { use std::mem; - + let mut events = Vec::new(); - + loop { use std::num::Bounded; @@ -362,7 +406,7 @@ impl Window { let mut buffer: [u8, ..16] = [mem::uninitialized(), ..16]; let raw_ev: *mut ffi::XKeyEvent = event; - let count = ffi::Xutf8LookupString(self.ic, mem::transmute(raw_ev), + let count = ffi::Xutf8LookupString(self.ic, mem::transmute(raw_ev), mem::transmute(buffer.as_mut_ptr()), buffer.len() as libc::c_int, ptr::mut_null(), ptr::mut_null()); @@ -402,7 +446,7 @@ impl Window { }; match button { - Some(button) => + Some(button) => events.push(MouseInput(state, button)), None => () }; @@ -457,7 +501,14 @@ impl Window { impl Drop for Window { fn drop(&mut self) { + unsafe { ffi::glXMakeCurrent(self.display, 0, ptr::null()); } unsafe { ffi::glXDestroyContext(self.display, self.context); } + + if self.is_fullscreen { + unsafe { ffi::XF86VidModeSwitchToMode(self.display, self.screen_id, self.xf86_desk_mode); } + unsafe { ffi::XF86VidModeSetViewPort(self.display, self.screen_id, 0, 0); } + } + unsafe { ffi::XDestroyIC(self.ic); } unsafe { ffi::XCloseIM(self.im); } unsafe { ffi::XDestroyWindow(self.display, self.window); } -- cgit v1.2.3 From bd3b06e1a527466600cb0d8cd87a323974b5e5d4 Mon Sep 17 00:00:00 2001 From: DavidPartouche Date: Thu, 18 Sep 2014 14:03:03 +0200 Subject: Changed is_fullscreen to monitor.is_some() --- src/lib.rs | 3 --- src/x11/ffi.rs | 1 + src/x11/mod.rs | 4 ++-- 3 files changed, 3 insertions(+), 5 deletions(-) (limited to 'src/x11') diff --git a/src/lib.rs b/src/lib.rs index 12bf209..af0706d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,6 @@ pub struct WindowBuilder { title: String, monitor: Option, gl_version: Option<(uint, uint)>, - is_fullscreen: bool, } impl WindowBuilder { @@ -69,7 +68,6 @@ impl WindowBuilder { title: "gl-init-rs window".to_string(), monitor: None, gl_version: None, - is_fullscreen: false, } } @@ -93,7 +91,6 @@ impl WindowBuilder { pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder { let MonitorID(monitor) = monitor; self.monitor = Some(monitor); - self.is_fullscreen = true; self } diff --git a/src/x11/ffi.rs b/src/x11/ffi.rs index 0bc52b5..f781b48 100644 --- a/src/x11/ffi.rs +++ b/src/x11/ffi.rs @@ -1426,6 +1426,7 @@ extern "C" { pub fn glXSwapBuffers(dpy: *mut Display, drawable: GLXDrawable); pub fn XkbSetDetectableAutoRepeat(dpy: *mut Display, detectable: bool, supported_rtm: *mut bool) -> bool; + pub fn XF86VidModeSwitchToMode(dpy: *mut Display, screen: libc::c_int, modeline: *mut XF86VidModeModeInfo) -> Bool; pub fn XF86VidModeSetViewPort(dpy: *mut Display, screen: libc::c_int, diff --git a/src/x11/mod.rs b/src/x11/mod.rs index 21e46b7..7dd3fb0 100644 --- a/src/x11/mod.rs +++ b/src/x11/mod.rs @@ -148,7 +148,7 @@ impl Window { }; let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi:: CWEventMask; - if builder.is_fullscreen { + if builder.monitor.is_some() { window_attributes |= ffi::CWOverrideRedirect; unsafe { ffi::XF86VidModeSwitchToMode(display, screen_id, *modes.offset(best_mode as int)); @@ -270,7 +270,7 @@ impl Window { wm_delete_window: wm_delete_window, xf86_desk_mode: xf86_desk_mode, screen_id: screen_id, - is_fullscreen: builder.is_fullscreen, + is_fullscreen: builder.monitor.is_some(), }; // calling glViewport -- cgit v1.2.3 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 (limited to 'src/x11') 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 From 55ebd4935ec649083e281cf2fa1abdcf224b7faa Mon Sep 17 00:00:00 2001 From: David Partouche Date: Thu, 4 Sep 2014 11:38:33 +0200 Subject: Added fullscreen support for X11 --- src/lib.rs | 3 +++ src/x11/mod.rs | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'src/x11') diff --git a/src/lib.rs b/src/lib.rs index af0706d..12bf209 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,7 @@ pub struct WindowBuilder { title: String, monitor: Option, gl_version: Option<(uint, uint)>, + is_fullscreen: bool, } impl WindowBuilder { @@ -68,6 +69,7 @@ impl WindowBuilder { title: "gl-init-rs window".to_string(), monitor: None, gl_version: None, + is_fullscreen: false, } } @@ -91,6 +93,7 @@ impl WindowBuilder { pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder { let MonitorID(monitor) = monitor; self.monitor = Some(monitor); + self.is_fullscreen = true; self } diff --git a/src/x11/mod.rs b/src/x11/mod.rs index 0f5f557..97552b0 100644 --- a/src/x11/mod.rs +++ b/src/x11/mod.rs @@ -129,7 +129,7 @@ impl Window { }; let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi:: CWEventMask; - if builder.monitor.is_some() { + if builder.is_fullscreen { window_attributes |= ffi::CWOverrideRedirect; unsafe { ffi::XF86VidModeSwitchToMode(display, screen_id, *modes.offset(best_mode as int)); @@ -140,7 +140,7 @@ impl Window { // finally creating the window let window = unsafe { - let win = ffi::XCreateWindow(display, root, 0, 0, dimensions.val0() as libc::c_uint, + let win = ffi::XCreateWindow(display, root, 50, 50, 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); @@ -251,7 +251,7 @@ impl Window { wm_delete_window: wm_delete_window, xf86_desk_mode: xf86_desk_mode, screen_id: screen_id, - is_fullscreen: builder.monitor.is_some(), + is_fullscreen: builder.is_fullscreen, }; // calling glViewport -- cgit v1.2.3 From 6f47e6ff8ca839899abb67b3d656e04231ca0cf8 Mon Sep 17 00:00:00 2001 From: DavidPartouche Date: Thu, 18 Sep 2014 14:03:03 +0200 Subject: Changed is_fullscreen to monitor.is_some() --- src/lib.rs | 3 --- src/x11/mod.rs | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) (limited to 'src/x11') diff --git a/src/lib.rs b/src/lib.rs index 12bf209..af0706d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,7 +58,6 @@ pub struct WindowBuilder { title: String, monitor: Option, gl_version: Option<(uint, uint)>, - is_fullscreen: bool, } impl WindowBuilder { @@ -69,7 +68,6 @@ impl WindowBuilder { title: "gl-init-rs window".to_string(), monitor: None, gl_version: None, - is_fullscreen: false, } } @@ -93,7 +91,6 @@ impl WindowBuilder { pub fn with_fullscreen(mut self, monitor: MonitorID) -> WindowBuilder { let MonitorID(monitor) = monitor; self.monitor = Some(monitor); - self.is_fullscreen = true; self } diff --git a/src/x11/mod.rs b/src/x11/mod.rs index 97552b0..7d732a2 100644 --- a/src/x11/mod.rs +++ b/src/x11/mod.rs @@ -129,7 +129,7 @@ impl Window { }; let mut window_attributes = ffi::CWBorderPixel | ffi::CWColormap | ffi:: CWEventMask; - if builder.is_fullscreen { + if builder.monitor.is_some() { window_attributes |= ffi::CWOverrideRedirect; unsafe { ffi::XF86VidModeSwitchToMode(display, screen_id, *modes.offset(best_mode as int)); @@ -251,7 +251,7 @@ impl Window { wm_delete_window: wm_delete_window, xf86_desk_mode: xf86_desk_mode, screen_id: screen_id, - is_fullscreen: builder.is_fullscreen, + is_fullscreen: builder.monitor.is_some(), }; // calling glViewport -- cgit v1.2.3 From 906324e26744a773d43b8737fdc60ae730f24141 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/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/x11') diff --git a/src/x11/mod.rs b/src/x11/mod.rs index 7d732a2..0f5f557 100644 --- a/src/x11/mod.rs +++ b/src/x11/mod.rs @@ -140,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); -- cgit v1.2.3 From 4222d12e6746bff2765e7a716eed4f4158f1906f Mon Sep 17 00:00:00 2001 From: DavidPartouche Date: Fri, 19 Sep 2014 19:57:52 +0200 Subject: Added output to get_name for x11 monitors --- src/x11/monitor.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/x11') diff --git a/src/x11/monitor.rs b/src/x11/monitor.rs index 2774d5f..f1b861b 100644 --- a/src/x11/monitor.rs +++ b/src/x11/monitor.rs @@ -35,7 +35,8 @@ pub fn get_primary_monitor() -> MonitorID { impl MonitorID { pub fn get_name(&self) -> Option { - Some("".to_string()) + let MonitorID(screen_num) = *self; + Some(format!("Monitor #{}", screen_num)) } pub fn get_dimensions(&self) -> (uint, uint) { -- cgit v1.2.3