mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-10 05:01:31 +11:00
Better cleanup in X11 destruction
This commit is contained in:
parent
bd0ae7476c
commit
356394cc75
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use libc;
|
use libc;
|
||||||
|
|
||||||
|
pub type Atom = libc::c_ulong;
|
||||||
pub type Bool = libc::c_int;
|
pub type Bool = libc::c_int;
|
||||||
pub type Colormap = XID;
|
pub type Colormap = XID;
|
||||||
pub type Cursor = XID;
|
pub type Cursor = XID;
|
||||||
|
@ -16,6 +17,7 @@ pub type GLXPbuffer = XID;
|
||||||
pub type GLXPixmap = XID;
|
pub type GLXPixmap = XID;
|
||||||
pub type GLXWindow = XID;
|
pub type GLXWindow = XID;
|
||||||
pub type Pixmap = XID;
|
pub type Pixmap = XID;
|
||||||
|
pub type Status = libc::c_int; // TODO: not sure
|
||||||
pub type Visual = (); // TODO: not sure
|
pub type Visual = (); // TODO: not sure
|
||||||
pub type VisualID = libc::c_ulong; // TODO: not sure
|
pub type VisualID = libc::c_ulong; // TODO: not sure
|
||||||
pub type Window = XID;
|
pub type Window = XID;
|
||||||
|
@ -70,6 +72,40 @@ pub static PropertyChangeMask: libc::c_long = (1<<22);
|
||||||
pub static ColormapChangeMask: libc::c_long = (1<<23);
|
pub static ColormapChangeMask: libc::c_long = (1<<23);
|
||||||
pub static OwnerGrabButtonMask: libc::c_long = (1<<24);
|
pub static OwnerGrabButtonMask: libc::c_long = (1<<24);
|
||||||
|
|
||||||
|
pub static KeyPress: libc::c_int = 2;
|
||||||
|
pub static KeyRelease: libc::c_int = 3;
|
||||||
|
pub static ButtonPress: libc::c_int = 4;
|
||||||
|
pub static ButtonRelease: libc::c_int = 5;
|
||||||
|
pub static MotionNotify: libc::c_int = 6;
|
||||||
|
pub static EnterNotify: libc::c_int = 7;
|
||||||
|
pub static LeaveNotify: libc::c_int = 8;
|
||||||
|
pub static FocusIn: libc::c_int = 9;
|
||||||
|
pub static FocusOut: libc::c_int = 10;
|
||||||
|
pub static KeymapNotify: libc::c_int = 11;
|
||||||
|
pub static Expose: libc::c_int = 12;
|
||||||
|
pub static GraphicsExpose: libc::c_int = 13;
|
||||||
|
pub static NoExpose: libc::c_int = 14;
|
||||||
|
pub static VisibilityNotify: libc::c_int = 15;
|
||||||
|
pub static CreateNotify: libc::c_int = 16;
|
||||||
|
pub static DestroyNotify: libc::c_int = 17;
|
||||||
|
pub static UnmapNotify: libc::c_int = 18;
|
||||||
|
pub static MapNotify: libc::c_int = 19;
|
||||||
|
pub static MapRequest: libc::c_int = 20;
|
||||||
|
pub static ReparentNotify: libc::c_int = 21;
|
||||||
|
pub static ConfigureNotify: libc::c_int = 22;
|
||||||
|
pub static ConfigureRequest: libc::c_int = 23;
|
||||||
|
pub static GravityNotify: libc::c_int = 24;
|
||||||
|
pub static ResizeRequest: libc::c_int = 25;
|
||||||
|
pub static CirculateNotify: libc::c_int = 26;
|
||||||
|
pub static CirculateRequest: libc::c_int = 27;
|
||||||
|
pub static PropertyNotify: libc::c_int = 28;
|
||||||
|
pub static SelectionClear: libc::c_int = 29;
|
||||||
|
pub static SelectionRequest: libc::c_int = 30;
|
||||||
|
pub static SelectionNotify: libc::c_int = 31;
|
||||||
|
pub static ColormapNotify: libc::c_int = 32;
|
||||||
|
pub static ClientMessage: libc::c_int = 33;
|
||||||
|
pub static MappingNotify: libc::c_int = 34;
|
||||||
|
|
||||||
pub static GLX_USE_GL: libc::c_int = 1;
|
pub static GLX_USE_GL: libc::c_int = 1;
|
||||||
pub static GLX_BUFFER_SIZE: libc::c_int = 2;
|
pub static GLX_BUFFER_SIZE: libc::c_int = 2;
|
||||||
pub static GLX_LEVEL: libc::c_int = 3;
|
pub static GLX_LEVEL: libc::c_int = 3;
|
||||||
|
@ -189,10 +225,22 @@ pub struct XSetWindowAttributes {
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct XEvent {
|
pub struct XEvent {
|
||||||
type_: libc::c_int,
|
pub type_: libc::c_int,
|
||||||
pad: [libc::c_long, ..24],
|
pad: [libc::c_long, ..24],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct XClientMessageEvent {
|
||||||
|
pub type_: libc::c_int,
|
||||||
|
pub serial: libc::c_ulong,
|
||||||
|
pub send_event: Bool,
|
||||||
|
pub display: *mut Display,
|
||||||
|
pub window: Window,
|
||||||
|
pub message_type: Atom,
|
||||||
|
pub format: libc::c_int,
|
||||||
|
pub l: [libc::c_long, ..5],
|
||||||
|
}
|
||||||
|
|
||||||
#[link(name = "GL")]
|
#[link(name = "GL")]
|
||||||
#[link(name = "X11")]
|
#[link(name = "X11")]
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
@ -206,15 +254,22 @@ extern "C" {
|
||||||
attributes: *mut XSetWindowAttributes) -> Window;
|
attributes: *mut XSetWindowAttributes) -> Window;
|
||||||
pub fn XDefaultRootWindow(display: *mut Display) -> Window;
|
pub fn XDefaultRootWindow(display: *mut Display) -> Window;
|
||||||
pub fn XDefaultScreen(display: *mut Display) -> libc::c_int;
|
pub fn XDefaultScreen(display: *mut Display) -> libc::c_int;
|
||||||
|
pub fn XDestroyWindow(display: *mut Display, w: Window);
|
||||||
pub fn XFlush(display: *mut Display);
|
pub fn XFlush(display: *mut Display);
|
||||||
|
pub fn XInternAtom(display: *mut Display, atom_name: *const libc::c_char,
|
||||||
|
only_if_exists: Bool) -> Atom;
|
||||||
pub fn XMapWindow(display: *mut Display, w: Window);
|
pub fn XMapWindow(display: *mut Display, w: Window);
|
||||||
pub fn XNextEvent(display: *mut Display, event_return: *mut XEvent);
|
pub fn XNextEvent(display: *mut Display, event_return: *mut XEvent);
|
||||||
pub fn XOpenDisplay(display_name: *const libc::c_char) -> *mut Display;
|
pub fn XOpenDisplay(display_name: *const libc::c_char) -> *mut Display;
|
||||||
|
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 XStoreName(display: *mut Display, w: Window, window_name: *const libc::c_char);
|
||||||
|
|
||||||
pub fn glXCreateContext(dpy: *mut Display, vis: *const XVisualInfo,
|
pub fn glXCreateContext(dpy: *mut Display, vis: *const XVisualInfo,
|
||||||
shareList: GLXContext, direct: Bool) -> GLXContext;
|
shareList: GLXContext, direct: Bool) -> GLXContext;
|
||||||
|
|
||||||
|
pub fn glXDestroyContext(dpy: *mut Display, ctx: GLXContext);
|
||||||
|
|
||||||
pub fn glXChooseFBConfig(dpy: *mut Display, screen: libc::c_int,
|
pub fn glXChooseFBConfig(dpy: *mut Display, screen: libc::c_int,
|
||||||
attrib_list: *const libc::c_int, nelements: *mut libc::c_int);
|
attrib_list: *const libc::c_int, nelements: *mut libc::c_int);
|
||||||
|
|
||||||
|
@ -247,8 +302,6 @@ int glXQueryContext (Display *dpy, GLXContext ctx, int attribute, int *value);
|
||||||
void glXSelectEvent (Display *dpy, GLXDrawable draw, unsigned long event_mask);
|
void glXSelectEvent (Display *dpy, GLXDrawable draw, unsigned long event_mask);
|
||||||
void glXGetSelectedEvent (Display *dpy, GLXDrawable draw, unsigned long *event_mask);
|
void glXGetSelectedEvent (Display *dpy, GLXDrawable draw, unsigned long *event_mask);
|
||||||
|
|
||||||
extern void glXDestroyContext( Display *dpy, GLXContext ctx );
|
|
||||||
|
|
||||||
|
|
||||||
extern void glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
|
extern void glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
|
||||||
unsigned long mask );
|
unsigned long mask );
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use {Event, Hints};
|
use {Event, Hints};
|
||||||
use libc;
|
use libc;
|
||||||
use std::{mem, ptr};
|
use std::{mem, ptr};
|
||||||
|
use std::sync::atomics::AtomicBool;
|
||||||
|
|
||||||
mod ffi;
|
mod ffi;
|
||||||
|
|
||||||
|
@ -8,6 +9,8 @@ pub struct Window {
|
||||||
display: *mut ffi::Display,
|
display: *mut ffi::Display,
|
||||||
window: ffi::Window,
|
window: ffi::Window,
|
||||||
context: ffi::GLXContext,
|
context: ffi::GLXContext,
|
||||||
|
should_close: AtomicBool,
|
||||||
|
wm_delete_window: ffi::Atom,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
|
@ -57,7 +60,8 @@ impl Window {
|
||||||
let mut set_win_attr = {
|
let mut set_win_attr = {
|
||||||
let mut swa: ffi::XSetWindowAttributes = unsafe { mem::zeroed() };
|
let mut swa: ffi::XSetWindowAttributes = unsafe { mem::zeroed() };
|
||||||
swa.colormap = cmap;
|
swa.colormap = cmap;
|
||||||
swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask | ffi::KeyPressMask;
|
swa.event_mask = ffi::ExposureMask | ffi::ResizeRedirectMask |
|
||||||
|
ffi::VisibilityChangeMask | ffi::KeyPressMask;
|
||||||
swa
|
swa
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -69,10 +73,19 @@ impl Window {
|
||||||
win
|
win
|
||||||
};
|
};
|
||||||
|
|
||||||
// showing window
|
// creating window, step 2
|
||||||
unsafe { ffi::XMapWindow(display, window) };
|
let wm_delete_window = unsafe {
|
||||||
unsafe { ffi::XStoreName(display, window, mem::transmute(title.as_slice().as_ptr())); }
|
use std::c_str::ToCStr;
|
||||||
unsafe { ffi::XFlush(display); }
|
|
||||||
|
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
|
// creating GL context
|
||||||
let context = unsafe {
|
let context = unsafe {
|
||||||
|
@ -84,12 +97,14 @@ impl Window {
|
||||||
display: display,
|
display: display,
|
||||||
window: window,
|
window: window,
|
||||||
context: context,
|
context: context,
|
||||||
|
should_close: AtomicBool::new(false),
|
||||||
|
wm_delete_window: wm_delete_window,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn should_close(&self) -> bool {
|
pub fn should_close(&self) -> bool {
|
||||||
// TODO:
|
use std::sync::atomics::Relaxed;
|
||||||
false
|
self.should_close.load(Relaxed)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_title(&self, title: &str) {
|
pub fn set_title(&self, title: &str) {
|
||||||
|
@ -124,9 +139,26 @@ impl Window {
|
||||||
|
|
||||||
let mut xev = unsafe { mem::uninitialized() };
|
let mut xev = unsafe { mem::uninitialized() };
|
||||||
unsafe { ffi::XNextEvent(self.display, &mut xev) };
|
unsafe { ffi::XNextEvent(self.display, &mut xev) };
|
||||||
|
|
||||||
|
|
||||||
Vec::new()
|
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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
|
||||||
|
events
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_current(&self) {
|
pub fn make_current(&self) {
|
||||||
|
@ -157,6 +189,8 @@ impl Window {
|
||||||
|
|
||||||
impl Drop for Window {
|
impl Drop for Window {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
unsafe { ffi::glXDestroyContext(self.display, self.context) }
|
||||||
|
unsafe { ffi::XDestroyWindow(self.display, self.window) }
|
||||||
unsafe { ffi::XCloseDisplay(self.display) }
|
unsafe { ffi::XCloseDisplay(self.display) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue