Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix version detection by calling GetVersion before GetWindowWMInfo #972

Merged
merged 6 commits into from
Feb 20, 2020
68 changes: 34 additions & 34 deletions src/sdl2/raw_window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ use crate::{sys::SDL_Window, video::Window};
unsafe impl HasRawWindowHandle for Window {
fn raw_window_handle(&self) -> RawWindowHandle {
use self::SDL_SYSWM_TYPE::*;
let mut wm_info = SDL_SysWMinfo::default();
if unsafe { SDL_GetWindowWMInfo(self.raw(), &mut wm_info) } == SDL_bool::SDL_FALSE {
panic!("Couldn't get SDL window info: {}", crate::get_error());

let mut wm_info: SDL_SysWMinfo = unsafe { std::mem::zeroed() };

// Make certain to retrieve version before querying `SDL_GetWindowWMInfo`
// as that gives an error on certain systems
unsafe {
sys::SDL_GetVersion(&mut wm_info.version);
if SDL_GetWindowWMInfo(self.raw(), &mut wm_info) == SDL_bool::SDL_FALSE {
panic!("Couldn't get SDL window info: {}", crate::get_error());
}
}

match wm_info.subsystem {
#[cfg(target_os = "windows")]
SDL_SYSWM_WINDOWS => {
Expand All @@ -20,11 +28,11 @@ unsafe impl HasRawWindowHandle for Window {
})
},
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
SDL_SYSWM_WAYLAND => {
use self::raw_window_handle::unix::WaylandHandle;
Expand All @@ -35,11 +43,11 @@ unsafe impl HasRawWindowHandle for Window {
})
},
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
SDL_SYSWM_X11 => {
use self::raw_window_handle::unix::XlibHandle;
Expand Down Expand Up @@ -84,6 +92,7 @@ extern "C" {
fn SDL_GetWindowWMInfo(window: *mut SDL_Window, info: *mut SDL_SysWMinfo) -> SDL_bool;
}


#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types, dead_code)]
Expand All @@ -92,15 +101,6 @@ pub enum SDL_bool {
SDL_TRUE = 1,
}

#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
#[allow(non_camel_case_types, dead_code)]
pub struct SDL_version {
pub major: u8,
pub minor: u8,
pub patch: u8,
}

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[allow(non_camel_case_types, dead_code)]
Expand All @@ -126,21 +126,21 @@ impl Default for SDL_SYSWM_TYPE {
}

#[repr(C)]
#[derive(Default, Copy, Clone)]
#[derive(Copy, Clone)]
#[allow(non_camel_case_types)]
pub struct SDL_SysWMinfo {
pub version: SDL_version,
pub version: sys::SDL_version,
pub subsystem: SDL_SYSWM_TYPE,

#[cfg(target_os = "windows")]
pub info: windows::WindowsSysWMinfo,

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
pub info: linux::LinuxSysWMinfo,

Expand Down Expand Up @@ -204,11 +204,11 @@ pub mod windows {
}

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
pub mod linux {
#[repr(C)]
Expand Down