mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-12 05:31:31 +11:00
commit
2ccc7b8458
|
@ -86,7 +86,6 @@ fn main() {
|
||||||
### Win32
|
### Win32
|
||||||
|
|
||||||
- You must call `glFlush` before `swap_buffers`, or else on Windows 8 nothing will be visible on the window
|
- You must call `glFlush` before `swap_buffers`, or else on Windows 8 nothing will be visible on the window
|
||||||
- Pixel formats are not implemented
|
|
||||||
- Changing the cursor (set_cursor) is not implemented
|
- Changing the cursor (set_cursor) is not implemented
|
||||||
|
|
||||||
### X11
|
### X11
|
||||||
|
|
|
@ -2,9 +2,14 @@ use std::sync::atomic::AtomicBool;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os;
|
use std::os;
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
use super::callback;
|
use super::callback;
|
||||||
use super::Window;
|
use super::Window;
|
||||||
use super::MonitorID;
|
use super::MonitorID;
|
||||||
|
use super::ContextWrapper;
|
||||||
|
use super::WindowWrapper;
|
||||||
|
use super::make_current_guard::CurrentContextGuard;
|
||||||
|
|
||||||
use Api;
|
use Api;
|
||||||
use BuilderAttribs;
|
use BuilderAttribs;
|
||||||
|
@ -33,14 +38,14 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<C
|
||||||
// initializing variables to be sent to the task
|
// initializing variables to be sent to the task
|
||||||
let title = builder.title.as_slice().utf16_units()
|
let title = builder.title.as_slice().utf16_units()
|
||||||
.chain(Some(0).into_iter()).collect::<Vec<u16>>(); // title to utf16
|
.chain(Some(0).into_iter()).collect::<Vec<u16>>(); // title to utf16
|
||||||
//let hints = hints.clone();
|
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
|
|
||||||
// GetMessage must be called in the same thread as CreateWindow,
|
// `GetMessage` must be called in the same thread as CreateWindow, so we create a new thread
|
||||||
// so we create a new thread dedicated to this window.
|
// dedicated to this window.
|
||||||
// This is the only safe method. Using `nosend` wouldn't work for non-native runtime.
|
thread::spawn(move || {
|
||||||
::std::thread::Thread::spawn(move || {
|
unsafe {
|
||||||
// sending
|
// creating and sending the `Window`
|
||||||
match init(title, builder, builder_sharelists) {
|
match init(title, builder, builder_sharelists) {
|
||||||
Ok(w) => tx.send(Ok(w)).ok(),
|
Ok(w) => tx.send(Ok(w)).ok(),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -52,22 +57,23 @@ pub fn new_window(builder: BuilderAttribs<'static>, builder_sharelists: Option<C
|
||||||
// now that the `Window` struct is initialized, the main `Window::new()` function will
|
// now that the `Window` struct is initialized, the main `Window::new()` function will
|
||||||
// return and this events loop will run in parallel
|
// return and this events loop will run in parallel
|
||||||
loop {
|
loop {
|
||||||
let mut msg = unsafe { mem::uninitialized() };
|
let mut msg = mem::uninitialized();
|
||||||
|
|
||||||
if unsafe { user32::GetMessageW(&mut msg, ptr::null_mut(), 0, 0) } == 0 {
|
if user32::GetMessageW(&mut msg, ptr::null_mut(), 0, 0) == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe { user32::TranslateMessage(&msg) };
|
user32::TranslateMessage(&msg);
|
||||||
unsafe { user32::DispatchMessageW(&msg) }; // calls `callback` (see below)
|
user32::DispatchMessageW(&msg); // calls `callback` (see the callback module)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
rx.recv().unwrap()
|
rx.recv().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: Option<ContextHack>)
|
unsafe fn init(title: Vec<u16>, builder: BuilderAttribs<'static>,
|
||||||
-> Result<Window, CreationError>
|
builder_sharelists: Option<ContextHack>) -> Result<Window, CreationError>
|
||||||
{
|
{
|
||||||
let builder_sharelists = builder_sharelists.map(|s| s.0);
|
let builder_sharelists = builder_sharelists.map(|s| s.0);
|
||||||
|
|
||||||
|
@ -85,7 +91,7 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O
|
||||||
// and change the monitor's resolution if necessary
|
// and change the monitor's resolution if necessary
|
||||||
if builder.monitor.is_some() {
|
if builder.monitor.is_some() {
|
||||||
let monitor = builder.monitor.as_ref().unwrap();
|
let monitor = builder.monitor.as_ref().unwrap();
|
||||||
switch_to_fullscreen(&mut rect, monitor);
|
try!(switch_to_fullscreen(&mut rect, monitor));
|
||||||
}
|
}
|
||||||
|
|
||||||
// computing the style and extended style of the window
|
// computing the style and extended style of the window
|
||||||
|
@ -97,12 +103,13 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O
|
||||||
};
|
};
|
||||||
|
|
||||||
// adjusting the window coordinates using the style
|
// adjusting the window coordinates using the style
|
||||||
unsafe { user32::AdjustWindowRectEx(&mut rect, style, 0, ex_style) };
|
user32::AdjustWindowRectEx(&mut rect, style, 0, ex_style);
|
||||||
|
|
||||||
// getting the address of wglCreateContextAttribsARB
|
// the first step is to create a dummy window and a dummy context which we will use
|
||||||
|
// to load the pointers to some functions in the OpenGL driver in `extra_functions`
|
||||||
let extra_functions = {
|
let extra_functions = {
|
||||||
// creating a dummy invisible window for GL initialization
|
// creating a dummy invisible window
|
||||||
let dummy_window = unsafe {
|
let dummy_window = {
|
||||||
let handle = user32::CreateWindowExW(ex_style, class_name.as_ptr(),
|
let handle = user32::CreateWindowExW(ex_style, class_name.as_ptr(),
|
||||||
title.as_ptr() as winapi::LPCWSTR,
|
title.as_ptr() as winapi::LPCWSTR,
|
||||||
style | winapi::WS_CLIPSIBLINGS | winapi::WS_CLIPCHILDREN,
|
style | winapi::WS_CLIPSIBLINGS | winapi::WS_CLIPCHILDREN,
|
||||||
|
@ -116,59 +123,41 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O
|
||||||
os::error_string(os::errno()))));
|
os::error_string(os::errno()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
handle
|
let hdc = user32::GetDC(handle);
|
||||||
};
|
|
||||||
|
|
||||||
// getting the HDC of the dummy window
|
|
||||||
let dummy_hdc = {
|
|
||||||
let hdc = unsafe { user32::GetDC(dummy_window) };
|
|
||||||
if hdc.is_null() {
|
if hdc.is_null() {
|
||||||
let err = Err(OsError(format!("GetDC function failed: {}",
|
let err = Err(OsError(format!("GetDC function failed: {}",
|
||||||
os::error_string(os::errno()))));
|
os::error_string(os::errno()))));
|
||||||
unsafe { user32::DestroyWindow(dummy_window); }
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
hdc
|
|
||||||
|
WindowWrapper(handle, hdc)
|
||||||
};
|
};
|
||||||
|
|
||||||
// getting the pixel format that we will use and setting it
|
// getting the pixel format that we will use and setting it
|
||||||
{
|
{
|
||||||
let formats = enumerate_native_pixel_formats(dummy_hdc);
|
let formats = enumerate_native_pixel_formats(&dummy_window);
|
||||||
let (id, _) = builder.choose_pixel_format(formats.into_iter().map(|(a, b)| (b, a)));
|
let (id, _) = builder.choose_pixel_format(formats.into_iter().map(|(a, b)| (b, a)));
|
||||||
try!(set_pixel_format(dummy_hdc, id));
|
try!(set_pixel_format(&dummy_window, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
// creating the dummy OpenGL context
|
// creating the dummy OpenGL context and making it current
|
||||||
let dummy_context = try!(create_context(None, dummy_hdc, None));
|
let dummy_context = try!(create_context(None, &dummy_window, None));
|
||||||
|
let current_context = try!(CurrentContextGuard::make_current(&dummy_window,
|
||||||
// making context current
|
&dummy_context));
|
||||||
unsafe { gl::wgl::MakeCurrent(dummy_hdc as *const libc::c_void, dummy_context as *const libc::c_void); }
|
|
||||||
|
|
||||||
// loading the extra WGL functions
|
// loading the extra WGL functions
|
||||||
let extra_functions = gl::wgl_extra::Wgl::load_with(|addr| {
|
gl::wgl_extra::Wgl::load_with(|addr| {
|
||||||
use libc;
|
use libc;
|
||||||
|
|
||||||
let addr = CString::from_slice(addr.as_bytes());
|
let addr = CString::new(addr.as_bytes()).unwrap();
|
||||||
let addr = addr.as_ptr();
|
let addr = addr.as_ptr();
|
||||||
|
|
||||||
unsafe {
|
|
||||||
gl::wgl::GetProcAddress(addr) as *const libc::c_void
|
gl::wgl::GetProcAddress(addr) as *const libc::c_void
|
||||||
}
|
})
|
||||||
});
|
|
||||||
|
|
||||||
// removing current context
|
|
||||||
unsafe { gl::wgl::MakeCurrent(ptr::null(), ptr::null()); }
|
|
||||||
|
|
||||||
// destroying the context and the window
|
|
||||||
unsafe { gl::wgl::DeleteContext(dummy_context as *const libc::c_void); }
|
|
||||||
unsafe { user32::DestroyWindow(dummy_window); }
|
|
||||||
|
|
||||||
// returning the address
|
|
||||||
extra_functions
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// creating the real window this time
|
// creating the real window this time, by using the functions in `extra_functions`
|
||||||
let real_window = unsafe {
|
let real_window = {
|
||||||
let (width, height) = if builder.monitor.is_some() || builder.dimensions.is_some() {
|
let (width, height) = if builder.monitor.is_some() || builder.dimensions.is_some() {
|
||||||
(Some(rect.right - rect.left), Some(rect.bottom - rect.top))
|
(Some(rect.right - rect.left), Some(rect.bottom - rect.top))
|
||||||
} else {
|
} else {
|
||||||
|
@ -195,47 +184,42 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O
|
||||||
os::error_string(os::errno()))));
|
os::error_string(os::errno()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
handle
|
let hdc = user32::GetDC(handle);
|
||||||
};
|
|
||||||
|
|
||||||
// getting the HDC of the window
|
|
||||||
let hdc = {
|
|
||||||
let hdc = unsafe { user32::GetDC(real_window) };
|
|
||||||
if hdc.is_null() {
|
if hdc.is_null() {
|
||||||
let err = Err(OsError(format!("GetDC function failed: {}",
|
return Err(OsError(format!("GetDC function failed: {}",
|
||||||
os::error_string(os::errno()))));
|
os::error_string(os::errno()))));
|
||||||
unsafe { user32::DestroyWindow(real_window); }
|
|
||||||
return err;
|
|
||||||
}
|
}
|
||||||
hdc
|
|
||||||
|
WindowWrapper(handle, hdc)
|
||||||
};
|
};
|
||||||
|
|
||||||
// calling SetPixelFormat
|
// calling SetPixelFormat
|
||||||
{
|
{
|
||||||
let formats = if extra_functions.GetPixelFormatAttribivARB.is_loaded() {
|
let formats = if extra_functions.GetPixelFormatAttribivARB.is_loaded() {
|
||||||
enumerate_arb_pixel_formats(&extra_functions, hdc)
|
enumerate_arb_pixel_formats(&extra_functions, &real_window)
|
||||||
} else {
|
} else {
|
||||||
enumerate_native_pixel_formats(hdc)
|
enumerate_native_pixel_formats(&real_window)
|
||||||
};
|
};
|
||||||
|
|
||||||
let (id, _) = builder.choose_pixel_format(formats.into_iter().map(|(a, b)| (b, a)));
|
let (id, _) = builder.choose_pixel_format(formats.into_iter().map(|(a, b)| (b, a)));
|
||||||
try!(set_pixel_format(hdc, id));
|
try!(set_pixel_format(&real_window, id));
|
||||||
}
|
}
|
||||||
|
|
||||||
// creating the OpenGL context
|
// creating the OpenGL context
|
||||||
let context = try!(create_context(Some((&extra_functions, &builder)), hdc, builder_sharelists));
|
let context = try!(create_context(Some((&extra_functions, &builder)), &real_window,
|
||||||
|
builder_sharelists));
|
||||||
|
|
||||||
// calling SetForegroundWindow if fullscreen
|
// calling SetForegroundWindow if fullscreen
|
||||||
if builder.monitor.is_some() {
|
if builder.monitor.is_some() {
|
||||||
unsafe { user32::SetForegroundWindow(real_window) };
|
user32::SetForegroundWindow(real_window.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// filling the WINDOW task-local storage
|
// filling the WINDOW task-local storage so that we can start receiving events
|
||||||
let events_receiver = {
|
let events_receiver = {
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut tx = Some(tx);
|
let mut tx = Some(tx);
|
||||||
callback::WINDOW.with(|window| {
|
callback::WINDOW.with(|window| {
|
||||||
(*window.borrow_mut()) = Some((real_window, tx.take().unwrap()));
|
(*window.borrow_mut()) = Some((real_window.0, tx.take().unwrap()));
|
||||||
});
|
});
|
||||||
rx
|
rx
|
||||||
};
|
};
|
||||||
|
@ -246,23 +230,17 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O
|
||||||
// handling vsync
|
// handling vsync
|
||||||
if builder.vsync {
|
if builder.vsync {
|
||||||
if extra_functions.SwapIntervalEXT.is_loaded() {
|
if extra_functions.SwapIntervalEXT.is_loaded() {
|
||||||
unsafe { gl::wgl::MakeCurrent(hdc as *const libc::c_void, context as *const libc::c_void) };
|
let guard = try!(CurrentContextGuard::make_current(&real_window, &context));
|
||||||
if unsafe { extra_functions.SwapIntervalEXT(1) } == 0 {
|
|
||||||
unsafe { gl::wgl::DeleteContext(context as *const libc::c_void); }
|
if extra_functions.SwapIntervalEXT(1) == 0 {
|
||||||
unsafe { user32::DestroyWindow(real_window); }
|
|
||||||
return Err(OsError(format!("wglSwapIntervalEXT failed")));
|
return Err(OsError(format!("wglSwapIntervalEXT failed")));
|
||||||
}
|
}
|
||||||
|
|
||||||
// it is important to remove the current context, otherwise you get very weird
|
|
||||||
// errors
|
|
||||||
unsafe { gl::wgl::MakeCurrent(ptr::null(), ptr::null()); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// building the struct
|
// building the struct
|
||||||
Ok(Window {
|
Ok(Window {
|
||||||
window: real_window,
|
window: real_window,
|
||||||
hdc: hdc as winapi::HDC,
|
|
||||||
context: context,
|
context: context,
|
||||||
gl_library: gl_library,
|
gl_library: gl_library,
|
||||||
events_receiver: events_receiver,
|
events_receiver: events_receiver,
|
||||||
|
@ -270,7 +248,7 @@ fn init(title: Vec<u16>, builder: BuilderAttribs<'static>, builder_sharelists: O
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn register_window_class() -> Vec<u16> {
|
unsafe fn register_window_class() -> Vec<u16> {
|
||||||
let class_name: Vec<u16> = "Window Class".utf16_units().chain(Some(0).into_iter())
|
let class_name: Vec<u16> = "Window Class".utf16_units().chain(Some(0).into_iter())
|
||||||
.collect::<Vec<u16>>();
|
.collect::<Vec<u16>>();
|
||||||
|
|
||||||
|
@ -280,7 +258,7 @@ fn register_window_class() -> Vec<u16> {
|
||||||
lpfnWndProc: Some(callback::callback),
|
lpfnWndProc: Some(callback::callback),
|
||||||
cbClsExtra: 0,
|
cbClsExtra: 0,
|
||||||
cbWndExtra: 0,
|
cbWndExtra: 0,
|
||||||
hInstance: unsafe { kernel32::GetModuleHandleW(ptr::null()) },
|
hInstance: kernel32::GetModuleHandleW(ptr::null()),
|
||||||
hIcon: ptr::null_mut(),
|
hIcon: ptr::null_mut(),
|
||||||
hCursor: ptr::null_mut(),
|
hCursor: ptr::null_mut(),
|
||||||
hbrBackground: ptr::null_mut(),
|
hbrBackground: ptr::null_mut(),
|
||||||
|
@ -293,12 +271,14 @@ fn register_window_class() -> Vec<u16> {
|
||||||
// an error, and because errors here are detected during CreateWindowEx anyway.
|
// an error, and because errors here are detected during CreateWindowEx anyway.
|
||||||
// Also since there is no weird element in the struct, there is no reason for this
|
// Also since there is no weird element in the struct, there is no reason for this
|
||||||
// call to fail.
|
// call to fail.
|
||||||
unsafe { user32::RegisterClassExW(&class) };
|
user32::RegisterClassExW(&class);
|
||||||
|
|
||||||
class_name
|
class_name
|
||||||
}
|
}
|
||||||
|
|
||||||
fn switch_to_fullscreen(rect: &mut winapi::RECT, monitor: &MonitorID) -> Result<(), CreationError> {
|
unsafe fn switch_to_fullscreen(rect: &mut winapi::RECT, monitor: &MonitorID)
|
||||||
|
-> Result<(), CreationError>
|
||||||
|
{
|
||||||
// adjusting the rect
|
// adjusting the rect
|
||||||
{
|
{
|
||||||
let pos = monitor.get_position();
|
let pos = monitor.get_position();
|
||||||
|
@ -309,15 +289,16 @@ fn switch_to_fullscreen(rect: &mut winapi::RECT, monitor: &MonitorID) -> Result<
|
||||||
}
|
}
|
||||||
|
|
||||||
// changing device settings
|
// changing device settings
|
||||||
let mut screen_settings: winapi::DEVMODEW = unsafe { mem::zeroed() };
|
let mut screen_settings: winapi::DEVMODEW = mem::zeroed();
|
||||||
screen_settings.dmSize = mem::size_of::<winapi::DEVMODEW>() as winapi::WORD;
|
screen_settings.dmSize = mem::size_of::<winapi::DEVMODEW>() as winapi::WORD;
|
||||||
screen_settings.dmPelsWidth = (rect.right - rect.left) as winapi::DWORD;
|
screen_settings.dmPelsWidth = (rect.right - rect.left) as winapi::DWORD;
|
||||||
screen_settings.dmPelsHeight = (rect.bottom - rect.top) as winapi::DWORD;
|
screen_settings.dmPelsHeight = (rect.bottom - rect.top) as winapi::DWORD;
|
||||||
screen_settings.dmBitsPerPel = 32; // TODO: ?
|
screen_settings.dmBitsPerPel = 32; // TODO: ?
|
||||||
screen_settings.dmFields = winapi::DM_BITSPERPEL | winapi::DM_PELSWIDTH | winapi::DM_PELSHEIGHT;
|
screen_settings.dmFields = winapi::DM_BITSPERPEL | winapi::DM_PELSWIDTH | winapi::DM_PELSHEIGHT;
|
||||||
|
|
||||||
let result = unsafe { user32::ChangeDisplaySettingsExW(monitor.get_system_name().as_ptr(),
|
let result = user32::ChangeDisplaySettingsExW(monitor.get_system_name().as_ptr(),
|
||||||
&mut screen_settings, ptr::null_mut(), winapi::CDS_FULLSCREEN, ptr::null_mut()) };
|
&mut screen_settings, ptr::null_mut(),
|
||||||
|
winapi::CDS_FULLSCREEN, ptr::null_mut());
|
||||||
|
|
||||||
if result != winapi::DISP_CHANGE_SUCCESSFUL {
|
if result != winapi::DISP_CHANGE_SUCCESSFUL {
|
||||||
return Err(OsError(format!("ChangeDisplaySettings failed: {}", result)));
|
return Err(OsError(format!("ChangeDisplaySettings failed: {}", result)));
|
||||||
|
@ -326,9 +307,9 @@ fn switch_to_fullscreen(rect: &mut winapi::RECT, monitor: &MonitorID) -> Result<
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'static>)>,
|
unsafe fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'static>)>,
|
||||||
hdc: winapi::HDC, share: Option<winapi::HGLRC>)
|
hdc: &WindowWrapper, share: Option<winapi::HGLRC>)
|
||||||
-> Result<winapi::HGLRC, CreationError>
|
-> Result<ContextWrapper, CreationError>
|
||||||
{
|
{
|
||||||
let share = share.unwrap_or(ptr::null_mut());
|
let share = share.unwrap_or(ptr::null_mut());
|
||||||
|
|
||||||
|
@ -360,11 +341,10 @@ fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'static>)>
|
||||||
|
|
||||||
attributes.push(0);
|
attributes.push(0);
|
||||||
|
|
||||||
Some(unsafe {
|
Some(extra_functions.CreateContextAttribsARB(hdc.1 as *const libc::c_void,
|
||||||
extra_functions.CreateContextAttribsARB(hdc as *const libc::c_void,
|
|
||||||
share as *const libc::c_void,
|
share as *const libc::c_void,
|
||||||
attributes.as_slice().as_ptr())
|
attributes.as_slice().as_ptr()))
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -375,14 +355,12 @@ fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'static>)>
|
||||||
let ctxt = match ctxt {
|
let ctxt = match ctxt {
|
||||||
Some(ctxt) => ctxt,
|
Some(ctxt) => ctxt,
|
||||||
None => {
|
None => {
|
||||||
unsafe {
|
let ctxt = gl::wgl::CreateContext(hdc.1 as *const libc::c_void);
|
||||||
let ctxt = gl::wgl::CreateContext(hdc as *const libc::c_void);
|
|
||||||
if !ctxt.is_null() && !share.is_null() {
|
if !ctxt.is_null() && !share.is_null() {
|
||||||
gl::wgl::ShareLists(share as *const libc::c_void, ctxt);
|
gl::wgl::ShareLists(share as *const libc::c_void, ctxt);
|
||||||
};
|
};
|
||||||
ctxt
|
ctxt
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if ctxt.is_null() {
|
if ctxt.is_null() {
|
||||||
|
@ -390,21 +368,19 @@ fn create_context(extra: Option<(&gl::wgl_extra::Wgl, &BuilderAttribs<'static>)>
|
||||||
os::error_string(os::errno()))));
|
os::error_string(os::errno()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(ctxt as winapi::HGLRC)
|
Ok(ContextWrapper(ctxt as winapi::HGLRC))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enumerate_native_pixel_formats(hdc: winapi::HDC) -> Vec<(PixelFormat, libc::c_int)> {
|
unsafe fn enumerate_native_pixel_formats(hdc: &WindowWrapper) -> Vec<(PixelFormat, libc::c_int)> {
|
||||||
let size_of_pxfmtdescr = mem::size_of::<winapi::PIXELFORMATDESCRIPTOR>() as u32;
|
let size_of_pxfmtdescr = mem::size_of::<winapi::PIXELFORMATDESCRIPTOR>() as u32;
|
||||||
let num = unsafe { gdi32::DescribePixelFormat(hdc, 1, size_of_pxfmtdescr, ptr::null_mut()) };
|
let num = gdi32::DescribePixelFormat(hdc.1, 1, size_of_pxfmtdescr, ptr::null_mut());
|
||||||
|
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
|
|
||||||
for index in (0 .. num) {
|
for index in (0 .. num) {
|
||||||
let mut output: winapi::PIXELFORMATDESCRIPTOR = unsafe { mem::zeroed() };
|
let mut output: winapi::PIXELFORMATDESCRIPTOR = mem::zeroed();
|
||||||
|
|
||||||
if unsafe { gdi32::DescribePixelFormat(hdc, index, size_of_pxfmtdescr,
|
if gdi32::DescribePixelFormat(hdc.1, index, size_of_pxfmtdescr, &mut output) == 0 {
|
||||||
&mut output) } == 0
|
|
||||||
{
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -438,14 +414,14 @@ fn enumerate_native_pixel_formats(hdc: winapi::HDC) -> Vec<(PixelFormat, libc::c
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enumerate_arb_pixel_formats(extra: &gl::wgl_extra::Wgl, hdc: winapi::HDC)
|
unsafe fn enumerate_arb_pixel_formats(extra: &gl::wgl_extra::Wgl, hdc: &WindowWrapper)
|
||||||
-> Vec<(PixelFormat, libc::c_int)>
|
-> Vec<(PixelFormat, libc::c_int)>
|
||||||
{
|
{
|
||||||
let get_info = |index: u32, attrib: u32| {
|
let get_info = |index: u32, attrib: u32| {
|
||||||
let mut value = unsafe { mem::uninitialized() };
|
let mut value = mem::uninitialized();
|
||||||
unsafe { extra.GetPixelFormatAttribivARB(hdc as *const libc::c_void, index as libc::c_int,
|
extra.GetPixelFormatAttribivARB(hdc.1 as *const libc::c_void, index as libc::c_int,
|
||||||
0, 1, [attrib as libc::c_int].as_ptr(),
|
0, 1, [attrib as libc::c_int].as_ptr(),
|
||||||
&mut value) };
|
&mut value);
|
||||||
value as u32
|
value as u32
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -489,17 +465,17 @@ fn enumerate_arb_pixel_formats(extra: &gl::wgl_extra::Wgl, hdc: winapi::HDC)
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pixel_format(hdc: winapi::HDC, id: libc::c_int) -> Result<(), CreationError> {
|
unsafe fn set_pixel_format(hdc: &WindowWrapper, id: libc::c_int) -> Result<(), CreationError> {
|
||||||
let mut output: winapi::PIXELFORMATDESCRIPTOR = unsafe { mem::zeroed() };
|
let mut output: winapi::PIXELFORMATDESCRIPTOR = mem::zeroed();
|
||||||
|
|
||||||
if unsafe { gdi32::DescribePixelFormat(hdc, id,
|
if gdi32::DescribePixelFormat(hdc.1, id, mem::size_of::<winapi::PIXELFORMATDESCRIPTOR>()
|
||||||
mem::size_of::<winapi::PIXELFORMATDESCRIPTOR>() as winapi::UINT, &mut output) } == 0
|
as winapi::UINT, &mut output) == 0
|
||||||
{
|
{
|
||||||
return Err(OsError(format!("DescribePixelFormat function failed: {}",
|
return Err(OsError(format!("DescribePixelFormat function failed: {}",
|
||||||
os::error_string(os::errno()))));
|
os::error_string(os::errno()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
if unsafe { gdi32::SetPixelFormat(hdc, id, &output) } == 0 {
|
if gdi32::SetPixelFormat(hdc.1, id, &output) == 0 {
|
||||||
return Err(OsError(format!("SetPixelFormat function failed: {}",
|
return Err(OsError(format!("SetPixelFormat function failed: {}",
|
||||||
os::error_string(os::errno()))));
|
os::error_string(os::errno()))));
|
||||||
}
|
}
|
||||||
|
@ -507,11 +483,11 @@ fn set_pixel_format(hdc: winapi::HDC, id: libc::c_int) -> Result<(), CreationErr
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_opengl32_dll() -> Result<winapi::HMODULE, CreationError> {
|
unsafe fn load_opengl32_dll() -> Result<winapi::HMODULE, CreationError> {
|
||||||
let name = "opengl32.dll".utf16_units().chain(Some(0).into_iter())
|
let name = "opengl32.dll".utf16_units().chain(Some(0).into_iter())
|
||||||
.collect::<Vec<u16>>().as_ptr();
|
.collect::<Vec<u16>>().as_ptr();
|
||||||
|
|
||||||
let lib = unsafe { kernel32::LoadLibraryW(name) };
|
let lib = kernel32::LoadLibraryW(name);
|
||||||
|
|
||||||
if lib.is_null() {
|
if lib.is_null() {
|
||||||
return Err(OsError(format!("LoadLibrary function failed: {}",
|
return Err(OsError(format!("LoadLibrary function failed: {}",
|
||||||
|
|
53
src/win32/make_current_guard.rs
Normal file
53
src/win32/make_current_guard.rs
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
use std::os;
|
||||||
|
|
||||||
|
use libc;
|
||||||
|
use winapi;
|
||||||
|
use CreationError;
|
||||||
|
|
||||||
|
use super::gl;
|
||||||
|
use super::ContextWrapper;
|
||||||
|
use super::WindowWrapper;
|
||||||
|
|
||||||
|
/// A guard for when you want to make the context current. Destroying the guard restores the
|
||||||
|
/// previously-current context.
|
||||||
|
pub struct CurrentContextGuard<'a, 'b> {
|
||||||
|
previous_hdc: winapi::HDC,
|
||||||
|
previous_hglrc: winapi::HGLRC,
|
||||||
|
marker1: PhantomData<&'a ()>,
|
||||||
|
marker2: PhantomData<&'b ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'b> CurrentContextGuard<'a, 'b> {
|
||||||
|
pub unsafe fn make_current(window: &'a WindowWrapper, context: &'b ContextWrapper)
|
||||||
|
-> Result<CurrentContextGuard<'a, 'b>, CreationError>
|
||||||
|
{
|
||||||
|
let previous_hdc = gl::wgl::GetCurrentDC() as winapi::HDC;
|
||||||
|
let previous_hglrc = gl::wgl::GetCurrentContext() as winapi::HGLRC;
|
||||||
|
|
||||||
|
let result = gl::wgl::MakeCurrent(window.1 as *const libc::c_void,
|
||||||
|
context.0 as *const libc::c_void);
|
||||||
|
|
||||||
|
if result == 0 {
|
||||||
|
return Err(CreationError::OsError(format!("wglMakeCurrent function failed: {}",
|
||||||
|
os::error_string(os::errno()))));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(CurrentContextGuard {
|
||||||
|
previous_hdc: previous_hdc,
|
||||||
|
previous_hglrc: previous_hglrc,
|
||||||
|
marker1: PhantomData,
|
||||||
|
marker2: PhantomData,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe_destructor]
|
||||||
|
impl<'a, 'b> Drop for CurrentContextGuard<'a, 'b> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
gl::wgl::MakeCurrent(self.previous_hdc as *const libc::c_void,
|
||||||
|
self.previous_hglrc as *const libc::c_void);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,18 +21,16 @@ mod event;
|
||||||
mod gl;
|
mod gl;
|
||||||
mod headless;
|
mod headless;
|
||||||
mod init;
|
mod init;
|
||||||
|
mod make_current_guard;
|
||||||
mod monitor;
|
mod monitor;
|
||||||
|
|
||||||
/// The Win32 implementation of the main `Window` object.
|
/// The Win32 implementation of the main `Window` object.
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
/// Main handle for the window.
|
/// Main handle for the window.
|
||||||
window: winapi::HWND,
|
window: WindowWrapper,
|
||||||
|
|
||||||
/// This represents a "draw context" for the surface of the window.
|
|
||||||
hdc: winapi::HDC,
|
|
||||||
|
|
||||||
/// OpenGL context.
|
/// OpenGL context.
|
||||||
context: winapi::HGLRC,
|
context: ContextWrapper,
|
||||||
|
|
||||||
/// Binded to `opengl32.dll`.
|
/// Binded to `opengl32.dll`.
|
||||||
///
|
///
|
||||||
|
@ -50,12 +48,25 @@ pub struct Window {
|
||||||
unsafe impl Send for Window {}
|
unsafe impl Send for Window {}
|
||||||
unsafe impl Sync for Window {}
|
unsafe impl Sync for Window {}
|
||||||
|
|
||||||
impl Window {
|
/// A simple wrapper that destroys the context when it is destroyed.
|
||||||
/// See the docs in the crate root file.
|
struct ContextWrapper(pub winapi::HGLRC);
|
||||||
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
|
|
||||||
let (builder, sharing) = builder.extract_non_static();
|
impl Drop for ContextWrapper {
|
||||||
let sharing = sharing.map(|w| init::ContextHack(w.context));
|
fn drop(&mut self) {
|
||||||
init::new_window(builder, sharing)
|
unsafe {
|
||||||
|
gl::wgl::DeleteContext(self.0 as *const libc::c_void);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A simple wrapper that destroys the window when it is destroyed.
|
||||||
|
struct WindowWrapper(pub winapi::HWND, pub winapi::HDC);
|
||||||
|
|
||||||
|
impl Drop for WindowWrapper {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
user32::DestroyWindow(self.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +80,13 @@ impl WindowProxy {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Window {
|
impl Window {
|
||||||
|
/// See the docs in the crate root file.
|
||||||
|
pub fn new(builder: BuilderAttribs) -> Result<Window, CreationError> {
|
||||||
|
let (builder, sharing) = builder.extract_non_static();
|
||||||
|
let sharing = sharing.map(|w| init::ContextHack(w.context.0));
|
||||||
|
init::new_window(builder, sharing)
|
||||||
|
}
|
||||||
|
|
||||||
/// See the docs in the crate root file.
|
/// See the docs in the crate root file.
|
||||||
pub fn is_closed(&self) -> bool {
|
pub fn is_closed(&self) -> bool {
|
||||||
use std::sync::atomic::Ordering::Relaxed;
|
use std::sync::atomic::Ordering::Relaxed;
|
||||||
|
@ -80,7 +98,7 @@ impl Window {
|
||||||
/// Calls SetWindowText on the HWND.
|
/// Calls SetWindowText on the HWND.
|
||||||
pub fn set_title(&self, text: &str) {
|
pub fn set_title(&self, text: &str) {
|
||||||
unsafe {
|
unsafe {
|
||||||
user32::SetWindowTextW(self.window,
|
user32::SetWindowTextW(self.window.0,
|
||||||
text.utf16_units().chain(Some(0).into_iter())
|
text.utf16_units().chain(Some(0).into_iter())
|
||||||
.collect::<Vec<u16>>().as_ptr() as winapi::LPCWSTR);
|
.collect::<Vec<u16>>().as_ptr() as winapi::LPCWSTR);
|
||||||
}
|
}
|
||||||
|
@ -88,13 +106,13 @@ impl Window {
|
||||||
|
|
||||||
pub fn show(&self) {
|
pub fn show(&self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
user32::ShowWindow(self.window, winapi::SW_SHOW);
|
user32::ShowWindow(self.window.0, winapi::SW_SHOW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hide(&self) {
|
pub fn hide(&self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
user32::ShowWindow(self.window, winapi::SW_HIDE);
|
user32::ShowWindow(self.window.0, winapi::SW_HIDE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,7 +123,7 @@ impl Window {
|
||||||
let mut placement: winapi::WINDOWPLACEMENT = unsafe { mem::zeroed() };
|
let mut placement: winapi::WINDOWPLACEMENT = unsafe { mem::zeroed() };
|
||||||
placement.length = mem::size_of::<winapi::WINDOWPLACEMENT>() as winapi::UINT;
|
placement.length = mem::size_of::<winapi::WINDOWPLACEMENT>() as winapi::UINT;
|
||||||
|
|
||||||
if unsafe { user32::GetWindowPlacement(self.window, &mut placement) } == 0 {
|
if unsafe { user32::GetWindowPlacement(self.window.0, &mut placement) } == 0 {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,9 +136,9 @@ impl Window {
|
||||||
use libc;
|
use libc;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
user32::SetWindowPos(self.window, ptr::null_mut(), x as libc::c_int, y as libc::c_int,
|
user32::SetWindowPos(self.window.0, ptr::null_mut(), x as libc::c_int, y as libc::c_int,
|
||||||
0, 0, winapi::SWP_NOZORDER | winapi::SWP_NOSIZE);
|
0, 0, winapi::SWP_NOZORDER | winapi::SWP_NOSIZE);
|
||||||
user32::UpdateWindow(self.window);
|
user32::UpdateWindow(self.window.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +147,7 @@ impl Window {
|
||||||
use std::mem;
|
use std::mem;
|
||||||
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
|
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
|
||||||
|
|
||||||
if unsafe { user32::GetClientRect(self.window, &mut rect) } == 0 {
|
if unsafe { user32::GetClientRect(self.window.0, &mut rect) } == 0 {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +162,7 @@ impl Window {
|
||||||
use std::mem;
|
use std::mem;
|
||||||
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
|
let mut rect: winapi::RECT = unsafe { mem::uninitialized() };
|
||||||
|
|
||||||
if unsafe { user32::GetWindowRect(self.window, &mut rect) } == 0 {
|
if unsafe { user32::GetWindowRect(self.window.0, &mut rect) } == 0 {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,9 +177,9 @@ impl Window {
|
||||||
use libc;
|
use libc;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
user32::SetWindowPos(self.window, ptr::null_mut(), 0, 0, x as libc::c_int,
|
user32::SetWindowPos(self.window.0, ptr::null_mut(), 0, 0, x as libc::c_int,
|
||||||
y as libc::c_int, winapi::SWP_NOZORDER | winapi::SWP_NOREPOSITION);
|
y as libc::c_int, winapi::SWP_NOZORDER | winapi::SWP_NOREPOSITION);
|
||||||
user32::UpdateWindow(self.window);
|
user32::UpdateWindow(self.window.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -186,12 +204,13 @@ impl Window {
|
||||||
/// See the docs in the crate root file.
|
/// See the docs in the crate root file.
|
||||||
pub unsafe fn make_current(&self) {
|
pub unsafe fn make_current(&self) {
|
||||||
// TODO: check return value
|
// TODO: check return value
|
||||||
gl::wgl::MakeCurrent(self.hdc as *const libc::c_void, self.context as *const libc::c_void);
|
gl::wgl::MakeCurrent(self.window.1 as *const libc::c_void,
|
||||||
|
self.context.0 as *const libc::c_void);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See the docs in the crate root file.
|
/// See the docs in the crate root file.
|
||||||
pub fn get_proc_address(&self, addr: &str) -> *const () {
|
pub fn get_proc_address(&self, addr: &str) -> *const () {
|
||||||
let addr = CString::from_slice(addr.as_bytes());
|
let addr = CString::new(addr.as_bytes()).unwrap();
|
||||||
let addr = addr.as_ptr();
|
let addr = addr.as_ptr();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -204,7 +223,7 @@ impl Window {
|
||||||
/// See the docs in the crate root file.
|
/// See the docs in the crate root file.
|
||||||
pub fn swap_buffers(&self) {
|
pub fn swap_buffers(&self) {
|
||||||
unsafe {
|
unsafe {
|
||||||
gdi32::SwapBuffers(self.hdc);
|
gdi32::SwapBuffers(self.window.1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -213,7 +232,7 @@ impl Window {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn platform_window(&self) -> *mut libc::c_void {
|
pub fn platform_window(&self) -> *mut libc::c_void {
|
||||||
self.window as *mut libc::c_void
|
self.window.0 as *mut libc::c_void
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See the docs in the crate root file.
|
/// See the docs in the crate root file.
|
||||||
|
@ -224,7 +243,7 @@ impl Window {
|
||||||
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
|
pub fn set_window_resize_callback(&mut self, _: Option<fn(u32, u32)>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_cursor(&self, cursor: MouseCursor) {
|
pub fn set_cursor(&self, _cursor: MouseCursor) {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,11 +299,10 @@ impl<'a> Iterator for WaitEventsIterator<'a> {
|
||||||
#[unsafe_destructor]
|
#[unsafe_destructor]
|
||||||
impl Drop for Window {
|
impl Drop for Window {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
use std::ptr;
|
unsafe {
|
||||||
// we don't call MakeCurrent(0, 0) because we are not sure that the context
|
// we don't call MakeCurrent(0, 0) because we are not sure that the context
|
||||||
// is still the current one
|
// is still the current one
|
||||||
unsafe { user32::PostMessageW(self.window, winapi::WM_DESTROY, 0, 0); }
|
user32::PostMessageW(self.window.0, winapi::WM_DESTROY, 0, 0);
|
||||||
unsafe { gl::wgl::DeleteContext(self.context as *const libc::c_void); }
|
}
|
||||||
unsafe { user32::DestroyWindow(self.window); }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue