No more bindgen! Using separate bindings for SDL_SysWMinfo

This commit is contained in:
Logan McGrath
2020-01-25 12:15:38 -08:00
parent efe7d9b8b7
commit eedfc2a1f2
4 changed files with 256 additions and 53 deletions

View File

@@ -35,10 +35,6 @@ rand = "^0.7"
version = "0.3.3"
optional = true
[target.'cfg(target_os = "macos")'.dependencies.objc]
version = "0.2.7"
optional = true
[features]
unsafe_textures = []
@@ -54,8 +50,6 @@ use_mac_framework = ["sdl2-sys/use_mac_framework"]
bundled = ["sdl2-sys/bundled"]
static-link= ["sdl2-sys/static-link"]
with-raw-window-handle = ["raw-window-handle", "objc", "use-bindgen"]
[[example]]
name = "animation"

View File

@@ -104,5 +104,5 @@ mod common;
// Export return types and such from the common module.
pub use crate::common::IntegerOrSdlError;
#[cfg(feature = "with-raw-window-handle")]
mod raw_window_handle;
#[cfg(feature = "raw-window-handle")]
pub mod raw_window_handle;

View File

@@ -1,49 +1,21 @@
#[cfg(target_os = "macos")]
extern crate objc;
#[cfg(feature = "with-raw-window-handle")]
extern crate raw_window_handle;
use std::alloc::{alloc, dealloc, Layout};
use self::raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use crate::{
sys::{SDL_bool::SDL_FALSE, SDL_GetWindowWMInfo, SDL_SYSWM_TYPE::*, SDL_SysWMinfo},
video::Window
};
use crate::{sys::SDL_Window, video::Window as VideoWindow};
struct InfoHandle {
layout: Layout,
pub ptr: *mut SDL_SysWMinfo,
}
impl InfoHandle {
pub fn new() -> InfoHandle {
let layout = Layout::new::<SDL_SysWMinfo>();
let ptr = unsafe { alloc(layout) as *mut SDL_SysWMinfo };
InfoHandle { layout, ptr }
}
}
impl Drop for InfoHandle {
fn drop(&mut self) {
unsafe {
dealloc(self.ptr as *mut u8, self.layout);
}
}
}
unsafe impl HasRawWindowHandle for Window {
unsafe impl HasRawWindowHandle for VideoWindow {
fn raw_window_handle(&self) -> RawWindowHandle {
let info_handle = InfoHandle::new();
if unsafe { SDL_GetWindowWMInfo(self.raw(), info_handle.ptr) } == SDL_FALSE {
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());
}
match unsafe { *info_handle.ptr }.subsystem {
match wm_info.subsystem {
#[cfg(target_os = "windows")]
SDL_SYSWM_WINDOWS => {
use self::raw_window_handle::windows::WindowsHandle;
RawWindowHandle::Windows(WindowsHandle {
hwnd: unsafe { (*info_handle.ptr).info.win }.window as *mut libc::c_void,
hwnd: unsafe { wm_info.info.win }.window as *mut libc::c_void,
..WindowsHandle::empty()
})
},
@@ -57,8 +29,8 @@ unsafe impl HasRawWindowHandle for Window {
SDL_SYSWM_WAYLAND => {
use self::raw_window_handle::unix::WaylandHandle;
RawWindowHandle::Wayland(WaylandHandle {
surface: unsafe { (*info_handle.ptr).info.wl }.surface as *mut libc::c_void,
display: unsafe { (*info_handle.ptr).info.wl }.display as *mut libc::c_void,
surface: unsafe { wm_info.info.wl }.surface as *mut libc::c_void,
display: unsafe { wm_info.info.wl }.display as *mut libc::c_void,
..WaylandHandle::empty()
})
},
@@ -72,25 +44,22 @@ unsafe impl HasRawWindowHandle for Window {
SDL_SYSWM_X11 => {
use self::raw_window_handle::unix::XlibHandle;
RawWindowHandle::Xlib(XlibHandle {
window: unsafe { (*info_handle.ptr).info.x11 }.window as *mut c_void,
display: unsafe { (*info_handle.ptr).info.x11 }.display as *mut c_void,
window: unsafe { wm_info.info.x11 }.window,
display: unsafe { wm_info.info.x11 }.display as *mut libc::c_void,
..XlibHandle::empty()
})
},
#[cfg(target_os = "macos")]
SDL_SYSWM_COCOA => {
use self::raw_window_handle::macos::MacOSHandle;
use self::objc::{msg_send, runtime::Object, sel, sel_impl};
let ns_window = unsafe { (*info_handle.ptr).info.cocoa }.window as *mut libc::c_void;
let ns_view = unsafe { msg_send![ns_window as *mut Object, contentView] };
RawWindowHandle::MacOS(MacOSHandle {
ns_window,
ns_view,
ns_window: unsafe { wm_info.info.cocoa }.window as *mut libc::c_void,
ns_view: 0 as *mut libc::c_void, // consumer of RawWindowHandle should determine this
..MacOSHandle::empty()
})
},
SDL_SYSWM_ANDROID | SDL_SYSWM_UIKIT => {
let window_system = match unsafe { (*info_handle.ptr).subsystem } {
let window_system = match wm_info.subsystem {
SDL_SYSWM_ANDROID => "Android",
SDL_SYSWM_UIKIT => "iOS",
_ => unreachable!(),
@@ -110,3 +79,242 @@ unsafe impl HasRawWindowHandle for Window {
}
}
}
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)]
pub enum SDL_bool {
SDL_FALSE = 0,
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)]
pub enum SDL_SYSWM_TYPE {
SDL_SYSWM_UNKNOWN = 0,
SDL_SYSWM_WINDOWS = 1,
SDL_SYSWM_X11 = 2,
SDL_SYSWM_DIRECTFB = 3,
SDL_SYSWM_COCOA = 4,
SDL_SYSWM_UIKIT = 5,
SDL_SYSWM_WAYLAND = 6,
SDL_SYSWM_MIR = 7,
SDL_SYSWM_WINRT = 8,
SDL_SYSWM_ANDROID = 9,
SDL_SYSWM_VIVANTE = 10,
SDL_SYSWM_OS2 = 11,
}
impl Default for SDL_SYSWM_TYPE {
fn default() -> Self {
SDL_SYSWM_TYPE::SDL_SYSWM_UNKNOWN
}
}
#[repr(C)]
#[derive(Default, Copy, Clone)]
#[allow(non_camel_case_types)]
pub struct SDL_SysWMinfo {
pub version: 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",
))]
pub info: linux::LinuxSysWMinfo,
#[cfg(target_os = "macos")]
pub info: macos::MacOSSysWMinfo,
}
#[cfg(target_os = "windows")]
pub mod windows {
#[repr(C)]
#[derive(Copy, Clone)]
pub union WindowsSysWMinfo {
pub win: Handles,
pub dummy: [u8; 64usize],
_bindgen_union_align: [u64; 8usize],
}
impl Default for WindowsSysWMinfo {
fn default() -> Self {
WindowsSysWMinfo {
win: Handles::default(),
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Handles {
pub window: *mut HWND,
pub hdc: *mut HDC,
pub hinstance: *mut HINSTANCE,
}
impl Default for Handles {
fn default() -> Self {
Handles {
window: 0 as *mut HWND,
hdc: 0 as *mut HDC,
hinstance: 0 as *mut HINSTANCE,
}
}
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct HWND {
pub unused: libc::c_int,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct HDC {
pub unused: libc::c_int,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct HINSTANCE {
pub unused: libc::c_int,
}
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
))]
pub mod linux {
#[repr(C)]
#[derive(Copy, Clone)]
pub union LinuxSysWMinfo {
pub x11: X11Info,
pub wl: WaylandInfo,
pub dummy: [u8; 64usize],
_bindgen_union_align: [u32; 16usize],
}
impl Default for LinuxSysWMinfo {
fn default() -> Self {
LinuxSysWMinfo {
wl: WaylandInfo::default(),
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct X11Info {
pub display: *mut Display,
pub window: Window,
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct WaylandInfo {
pub display: *mut WLDisplay,
pub surface: *mut WLSurface,
pub shell_surface: *mut WLShellSurface,
}
impl Default for WaylandInfo {
fn default() -> Self {
WaylandInfo {
display: 0 as *mut WLDisplay,
surface: 0 as *mut WLSurface,
shell_surface: 0 as *mut WLShellSurface,
}
}
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct WLDisplay {
pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct WLSurface {
pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct WLShellSurface {
pub _address: u8,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Display {
_unused: [u8; 0],
}
pub type Window = libc::c_ulong;
}
#[cfg(target_os = "macos")]
pub mod macos {
#[repr(C)]
#[derive(Copy, Clone)]
pub union MacOSSysWMinfo {
pub cocoa: CocoaInfo,
pub dummy: [u8; 64usize],
_bindgen_union_align: [u64; 8usize],
}
impl Default for MacOSSysWMinfo {
fn default() -> Self {
MacOSSysWMinfo {
cocoa: CocoaInfo::default(),
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct CocoaInfo {
pub window: *mut NSWindow,
}
impl Default for CocoaInfo {
fn default() -> Self {
CocoaInfo {
window: 0 as *mut NSWindow,
}
}
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct NSWindow {
_unused: [u8; 0],
}
}

View File

@@ -20,6 +20,7 @@ use crate::sys;
pub use crate::sys::{VkInstance, VkSurfaceKHR};
pub struct WindowSurfaceRef<'a>(&'a mut SurfaceRef, &'a Window);
impl<'a> Deref for WindowSurfaceRef<'a> {