Rust-SDL2 / rust-sdl2

SDL2 bindings for Rust
MIT License
2.74k stars 468 forks source link

Missing SysWMinfo for Windows #1263

Open KivApple opened 1 year ago

KivApple commented 1 year ago

Usually in C/C++ code you are able to retrive WinAPI window handle like this:

// SDL_Window *my_window = ...;
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(my_window, &wmInfo);
wmInfo.info.win.window // Do something

But it's impossible in Rust:

// let my_window: sdl2::video::Window = ...;
unsafe {
    let mut wminfo: SDL_SysWMinfo = MaybeUninit::zeroed().assume_init();
    SDL_GetVersion(&mut wminfo.version);
    SDL_GetWindowWMInfo(my_window.raw(), &mut wminfo);
    wminfo.info.win.window // no field `win` on type `SDL_SysWMinfo__bindgen_ty_1`
                           // available fields are: `x11`, `wl`, `dummy`
}

It seems that Rust sdl2-sys bindings missing platform-specific union values and on any platform offer only Linux union variants.

Cobrand commented 1 year ago

You need to compile your own sdl2-sys bindings with the "use-bindgen" feature, by default they are Linux variant (not that we don't want to include all of the platforms, but SDL auto-detects the host platform so we can't have generic sdl2-sys bindings).

KivApple commented 1 year ago

I've got an error:

   Compiling sdl2-sys v0.35.2
error[E0588]: packed type cannot transitively contain a `#[repr(align)]` type
     --> C:\Users\kiv\Projects\my_app\target\debug\build\sdl2-sys-2e0686f7ed6e4fb0\out/sdl_bindings.rs:74524:1
      |
74524 | pub struct _IMAGE_TLS_DIRECTORY64 {
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |
note: `_IMAGE_TLS_DIRECTORY64__bindgen_ty_1__bindgen_ty_1` has a `#[repr(align)]` attribute
     --> C:\Users\kiv\Projects\my_app\target\debug\build\sdl2-sys-2e0686f7ed6e4fb0\out/sdl_bindings.rs:74542:1
      |
74542 | pub struct _IMAGE_TLS_DIRECTORY64__bindgen_ty_1__bindgen_ty_1 {
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: `_IMAGE_TLS_DIRECTORY64` contains a field of type `_IMAGE_TLS_DIRECTORY64__bindgen_ty_1`
     --> C:\Users\kiv\Projects\my_app\target\debug\build\sdl2-sys-2e0686f7ed6e4fb0\out/sdl_bindings.rs:74530:9
      |
74530 |     pub __bindgen_anon_1: _IMAGE_TLS_DIRECTORY64__bindgen_ty_1,
      |         ^^^^^^^^^^^^^^^^
note: ...which contains a field of type `_IMAGE_TLS_DIRECTORY64__bindgen_ty_1__bindgen_ty_1`
     --> C:\Users\kiv\Projects\my_app\target\debug\build\sdl2-sys-2e0686f7ed6e4fb0\out/sdl_bindings.rs:74536:9
      |
74536 |     pub __bindgen_anon_1: _IMAGE_TLS_DIRECTORY64__bindgen_ty_1__bindgen_ty_1,
      |         ^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0588`.
The following warnings were emitted during compilation:

warning: couldn't execute `llvm-config --prefix` (error: program not found)
warning: set the LLVM_CONFIG_PATH environment variable to the full path to a valid `llvm-config` executable (including the executable itself)

error: could not compile `sdl2-sys` due to previous error

In the meantime, I think there are not a lot platform-specific structures (I actually see only two - SDL_SysWMmsg and SDL_SysWMinfo) and it might be possible to dispatch them in Rust using cfgs rather than in C.

bane9 commented 9 months ago

I aplogise for the necro, just posting this in the case anyone else needs to retrieve the HWND: https://gist.github.com/bane9/4b5839bf3828e5b10136e9ac72c76481