REVIEWED: GetWindowHandle(), to be consistent between backends, fix #5885

Returns the underlying window handle for platforms with some windowing system.
This commit is contained in:
Ray
2026-05-30 12:20:58 +02:00
parent f0d3e9a5c8
commit 2ee6a76464
5 changed files with 94 additions and 23 deletions

View File

@@ -473,8 +473,7 @@ void SetWindowFocused(void)
// Get native window handle
void *GetWindowHandle(void)
{
TRACELOG(LOG_WARNING, "GetWindowHandle() not implemented on target platform");
return NULL;
return (void *)platform.app->window; // Type: ANativeWindow*
}
// Get number of monitors

View File

@@ -115,7 +115,7 @@ typedef struct {
// Local storage for the window handle returned by glfwGetX11Window
// This is needed as X11 handles are integers and may not fit inside a pointer depending on platform
// Storing the handle locally and returning a pointer in GetWindowHandle allows the code to work regardless of pointer width
XID windowHandleX11;
Window windowHandleX11; // Underlying type: unsigned long (XID, Window)
#endif
} PlatformData;
@@ -764,9 +764,10 @@ void SetWindowFocused(void)
// Get native window handle
void *GetWindowHandle(void)
{
void *handle = NULL;
#if defined(_WIN32)
// NOTE: Returned handle is: void *HWND (windows.h)
return glfwGetWin32Window(platform.handle);
handle = glfwGetWin32Window(platform.handle); // Type: HWND
#endif
#if defined(__linux__)
#if defined(_GLFW_WAYLAND)
@@ -774,28 +775,27 @@ void *GetWindowHandle(void)
int platformID = glfwGetPlatform();
if (platformID == GLFW_PLATFORM_WAYLAND)
{
return glfwGetWaylandWindow(platform.handle);
handle = (void *)glfwGetWaylandWindow(platform.handle); // Type: struct wl_surface*
}
else
{
platform.windowHandleX11 = glfwGetX11Window(platform.handle);
return &platform.windowHandleX11;
platform.windowHandleX11 = glfwGetX11Window(platform.handle); // Type: Window (unsigned long)
handle = &platform.windowHandleX11;
}
#else
return glfwGetWaylandWindow(platform.handle);
handle = (void *)glfwGetWaylandWindow(platform.handle);
#endif
#elif defined(_GLFW_X11)
// Store the window handle localy and return a pointer to the variable instead
// Store the window handle locally and return a pointer to the variable instead
platform.windowHandleX11 = glfwGetX11Window(platform.handle);
return &platform.windowHandleX11;
handle = &platform.windowHandleX11;
#endif
#endif
#if defined(__APPLE__)
// NOTE: Returned handle is: (objc_object *)
return (void *)glfwGetCocoaWindow(platform.handle);
handle = (void *)glfwGetCocoaWindow(platform.handle); // Type: NSWindow*
#endif
return NULL;
return handle;
}
// Get number of monitors

View File

@@ -184,6 +184,9 @@ typedef struct {
i32 surfaceWidth;
i32 surfaceHeight;
#endif
#if defined(__linux__) && defined(RGFW_X11)
Window windowHandleX11; // Underlying type: unsigned long
#endif
} PlatformData;
//----------------------------------------------------------------------------------
@@ -851,15 +854,25 @@ void SetWindowFocused(void)
// Get native window handle
void *GetWindowHandle(void)
{
if (platform.window == NULL) return NULL;
void *handle = NULL;
#if defined(RGFW_WASM)
return (void *)&platform.window->src.ctx;
#elif defined(RGFW_WAYLAND)
return (void *)platform.window->src.surface;
#else
return (void *)platform.window->src.window;
if (platform.window != NULL)
{
#if defined(_WIN32)
handle = (void *)platform.window->src.window; // Type: HWND
#elif defined(__linux__)
#if defined(RGFW_X11)
platform.windowHandleX11 = platform.window->src.window; // Type: Window (unsigned long)
handle = &platform.window->src.window;
#elif defined(RGFW_WAYLAND)
handle = (void *)platform.window->src.surface; // Type: struct wl_surface*
#endif
#elif defined(__APPLE__)
handle = (void *)platform.window->src.window; // Type: id, NSWindow*
#endif
}
return handle;
}
// Get number of monitors

View File

@@ -56,6 +56,7 @@
#include "SDL3/SDL.h"
#elif defined(USING_SDL2_PROJECT)
#include "SDL2/SDL.h"
#include "SDL2/SDL_syswm.h" // Required to get window handlers
#else
#include "SDL.h"
#endif
@@ -101,6 +102,13 @@ typedef struct {
SDL_GameController *gamepad[MAX_GAMEPADS];
SDL_JoystickID gamepadId[MAX_GAMEPADS]; // Joystick instance ids, they do not start from 0
SDL_Cursor *cursor;
#if defined(__linux__)
// Local storage for the window handle (X11)
// NOTE: On SDL is not possible to know windowing backend at compile time, so,
// just in case, avoiding X11 specific types here
unsigned long windowHandleX11; // Underlying type for: XID, Window
#endif
} PlatformData;
//----------------------------------------------------------------------------------
@@ -920,9 +928,60 @@ void SetWindowFocused(void)
}
// Get native window handle
// NOTE: Handle type depends on OS and windowing system
void *GetWindowHandle(void)
{
return (void *)platform.window;
void *handle = NULL;
#if defined(USING_SDL3_PROJECT)
// REF: https://github.com/libsdl-org/SDL/blob/main/include/SDL3/SDL_video.h#L1590
SDL_PropertiesID props = SDL_GetWindowProperties(platform.window);
#if defined(_WIN32)
handle = (void *)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL); // Type: HWND
#elif defined(__linux__)
unsigned long windowId = (unsigned long)SDL_GetNumberProperty(props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); // Type: unsigned long (XID, Window)
if (windowId != 0)
{
// X11 window ID
platform.windowHandleX11 = windowId;
handle = &platform.windowHandleX11;
}
else
{
// Wayland, get display surface pointer
// NOTE: Alternative SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER
handle = (void *)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL); // Type: struct wl_surface*
}
#elif defined(__APPLE__)
handle = (void *)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, NULL); // Type: NSWindow*
#endif
#elif defined(USING_SDL2_PROJECT)
SDL_SysWMinfo wmInfo = { 0 };
SDL_VERSION(&wmInfo.version);
if (SDL_GetWindowWMInfo(platform.window, &wmInfo))
{
#if defined(_WIN32)
handle = (void *)wmInfo.info.win.window; // Type: HWND
#elif defined(__linux__)
if (wmInfo.subsystem == SDL_SYSWM_X11)
{
// X11, get window ID (unsigned long)
platform.windowHandleX11 = (unsigned long)wmInfo.info.x11.window;
handle = &platform.windowHandleX11;
}
else if (wmInfo.subsystem == SDL_SYSWM_WAYLAND)
{
// Wayland, get display surface pointer
// NOTE: Alternative: wmInfo.info.wl.display
handle = (void *)wmInfo.info.wl.surface; // Type: struct wl_surface*
}
#elif defined(__APPLE__)
handle = (void *)wmInfo.info.cocoa.window; // Type: NSWindow*
#endif
}
#endif
return handle;
}
// Get number of monitors

View File

@@ -1023,7 +1023,7 @@ void SetWindowFocused(void)
// Get native window handle
void *GetWindowHandle(void)
{
return platform.hwnd;
return (void *)platform.hwnd;
}
int GetMonitorCount(void)