aboutsummaryrefslogtreecommitdiffstats
path: root/src/x11/mod.rs
diff options
context:
space:
mode:
authorPierre Krieger <pierre.krieger1708@gmail.com>2014-07-27 15:25:04 +0200
committerPierre Krieger <pierre.krieger1708@gmail.com>2014-07-27 15:49:43 +0200
commit356394cc75f69f70f9ddf3d05ae9ef2567c95554 (patch)
treefc71fca6aac59da2d341e98451b95cbbdd7fc054 /src/x11/mod.rs
parentbd0ae7476c6decc5cc9631ced95bb82a86b0411e (diff)
downloadglutin-356394cc75f69f70f9ddf3d05ae9ef2567c95554.tar.gz
glutin-356394cc75f69f70f9ddf3d05ae9ef2567c95554.zip
Better cleanup in X11 destruction
Diffstat (limited to 'src/x11/mod.rs')
-rw-r--r--src/x11/mod.rs50
1 files changed, 42 insertions, 8 deletions
diff --git a/src/x11/mod.rs b/src/x11/mod.rs
index e6297f1..d4b7da5 100644
--- a/src/x11/mod.rs
+++ b/src/x11/mod.rs
@@ -1,6 +1,7 @@
use {Event, Hints};
use libc;
use std::{mem, ptr};
+use std::sync::atomics::AtomicBool;
mod ffi;
@@ -8,6 +9,8 @@ pub struct Window {
display: *mut ffi::Display,
window: ffi::Window,
context: ffi::GLXContext,
+ should_close: AtomicBool,
+ wm_delete_window: ffi::Atom,
}
impl Window {
@@ -57,7 +60,8 @@ impl Window {
let mut set_win_attr = {
let mut swa: ffi::XSetWindowAttributes = unsafe { mem::zeroed() };
swa.colormap = cmap;
- swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask | ffi::KeyPressMask;
+ swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask |
+ ffi::VisibilityChangeMask | ffi::KeyPressMask;
swa
};
@@ -69,10 +73,19 @@ impl Window {
win
};
- // showing window
- unsafe { ffi::XMapWindow(display, window) };
- unsafe { ffi::XStoreName(display, window, mem::transmute(title.as_slice().as_ptr())); }
- unsafe { ffi::XFlush(display); }
+ // creating window, step 2
+ let wm_delete_window = unsafe {
+ use std::c_str::ToCStr;
+
+ ffi::XMapWindow(display, window);
+ let mut wm_delete_window = ffi::XInternAtom(display,
+ "WM_DELETE_WINDOW".to_c_str().as_ptr() as *const libc::c_char, 0);
+ ffi::XSetWMProtocols(display, window, &mut wm_delete_window, 1);
+ ffi::XStoreName(display, window, mem::transmute(title.as_slice().as_ptr()));
+ ffi::XFlush(display);
+
+ wm_delete_window
+ };
// creating GL context
let context = unsafe {
@@ -84,12 +97,14 @@ impl Window {
display: display,
window: window,
context: context,
+ should_close: AtomicBool::new(false),
+ wm_delete_window: wm_delete_window,
})
}
pub fn should_close(&self) -> bool {
- // TODO:
- false
+ use std::sync::atomics::Relaxed;
+ self.should_close.load(Relaxed)
}
pub fn set_title(&self, title: &str) {
@@ -124,9 +139,26 @@ impl Window {
let mut xev = unsafe { mem::uninitialized() };
unsafe { ffi::XNextEvent(self.display, &mut xev) };
+
+ let mut events = Vec::new();
+ match xev.type_ {
+ ffi::ClientMessage => {
+ use Closed;
+ use std::sync::atomics::Relaxed;
+
+ let client_msg: &ffi::XClientMessageEvent = unsafe { mem::transmute(&xev) };
+
+ if client_msg.l[0] == self.wm_delete_window as libc::c_long {
+ self.should_close.store(true, Relaxed);
+ events.push(Closed);
+ }
+ },
+
+ _ => ()
+ }
- Vec::new()
+ events
}
pub fn make_current(&self) {
@@ -157,6 +189,8 @@ impl Window {
impl Drop for Window {
fn drop(&mut self) {
+ unsafe { ffi::glXDestroyContext(self.display, self.context) }
+ unsafe { ffi::XDestroyWindow(self.display, self.window) }
unsafe { ffi::XCloseDisplay(self.display) }
}
}