From 2b118a9518d13013b053da87b75e07d4b6e34ac8 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 24 Nov 2026 12:07:49 -0800
Subject: [PATCH] Added SDL_WINDOW_MOUSE_PASSTHROUGH
Also polished up SDL_SetWindowMousePassthrough()
Fixes https://github.com/libsdl-org/SDL/issues/15028
---
include/SDL3/SDL_video.h | 10 +++-
src/dynapi/SDL_dynapi.exports | 1 +
src/dynapi/SDL_dynapi.sym | 1 +
src/dynapi/SDL_dynapi_overrides.h | 1 +
src/dynapi/SDL_dynapi_procs.h | 1 +
src/events/SDL_mouse.c | 43 ++++++++++++--
src/test/SDL_test_common.c | 8 +++
src/video/SDL_sysvideo.h | 4 +-
src/video/SDL_video.c | 60 +++++++++++++++++++-
src/video/cocoa/SDL_cocoashape.m | 5 +-
src/video/cocoa/SDL_cocoawindow.h | 2 +-
src/video/cocoa/SDL_cocoawindow.m | 16 ++++--
src/video/wayland/SDL_waylandevents.c | 2 +-
src/video/wayland/SDL_waylandwindow.c | 11 +++-
src/video/wayland/SDL_waylandwindow.h | 2 +-
src/video/windows/SDL_windowswindow.c | 81 +++++++++++++--------------
src/video/windows/SDL_windowswindow.h | 3 +-
src/video/x11/SDL_x11shape.c | 42 +++++++-------
src/video/x11/SDL_x11window.c | 16 ++----
src/video/x11/SDL_x11window.h | 2 +-
src/video/x11/SDL_x11xfixes.c | 2 +-
21 files changed, 215 insertions(+), 98 deletions(-)
diff --git a/include/SDL3/SDL_video.h b/include/SDL3/SDL_video.h
index 4e5ab448a75fa..d618867e87676 100644
--- a/include/SDL3/SDL_video.h
+++ b/include/SDL3/SDL_video.h
@@ -251,6 +251,7 @@ typedef Uint64 SDL_WindowFlags;
#define SDL_WINDOW_POPUP_MENU SDL_UINT64_C(0x0000000000080000) /**< window should be treated as a popup menu, requires a parent window */
#define SDL_WINDOW_KEYBOARD_GRABBED SDL_UINT64_C(0x0000000000100000) /**< window has grabbed keyboard input */
#define SDL_WINDOW_FILL_DOCUMENT SDL_UINT64_C(0x0000000000200000) /**< window is in fill-document mode (Emscripten only), since SDL 3.4.0 */
+#define SDL_WINDOW_MOUSE_PASSTHROUGH SDL_UINT64_C(0x0000000000400000) /**< window is transparent to mouse input, since SDL 3.6.0 */
#define SDL_WINDOW_VULKAN SDL_UINT64_C(0x0000000010000000) /**< window usable for Vulkan surface */
#define SDL_WINDOW_METAL SDL_UINT64_C(0x0000000020000000) /**< window usable for Metal view */
#define SDL_WINDOW_TRANSPARENT SDL_UINT64_C(0x0000000040000000) /**< window with transparent buffer */
@@ -1154,6 +1155,7 @@ extern SDL_DECLSPEC SDL_Window ** SDLCALL SDL_GetWindows(int *count);
* buffer if possible
* - `SDL_WINDOW_MOUSE_CAPTURE`: window has mouse captured (unrelated to
* MOUSE_GRABBED)
+ * - `SDL_WINDOW_MOUSE_PASSTHROUGH`: window is transparent to mouse input
* - `SDL_WINDOW_ALWAYS_ON_TOP`: window should always be above others
* - `SDL_WINDOW_UTILITY`: window should be treated as a utility window, not
* showing in the task bar and window list
@@ -1341,6 +1343,7 @@ extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_CreatePopupWindow(SDL_Window *paren
* its parent
* - `SDL_PROP_WINDOW_CREATE_MOUSE_GRABBED_BOOLEAN`: true if the window starts
* with grabbed mouse focus
+ * - `SDL_PROP_WINDOW_CREATE_MOUSE_PASSTHROUGH_BOOLEAN`: true if the window should be transparent to mouse input
* - `SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN`: true if the window will be used
* with OpenGL rendering
* - `SDL_PROP_WINDOW_CREATE_PARENT_POINTER`: an SDL_Window that will be the
@@ -1487,6 +1490,7 @@ extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_CreateWindowWithProperties(SDL_Prop
#define SDL_PROP_WINDOW_CREATE_MINIMIZED_BOOLEAN "SDL.window.create.minimized"
#define SDL_PROP_WINDOW_CREATE_MODAL_BOOLEAN "SDL.window.create.modal"
#define SDL_PROP_WINDOW_CREATE_MOUSE_GRABBED_BOOLEAN "SDL.window.create.mouse_grabbed"
+#define SDL_PROP_WINDOW_CREATE_MOUSE_PASSTHROUGH_BOOLEAN "SDL.window.create.mouse_passthrough"
#define SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN "SDL.window.create.opengl"
#define SDL_PROP_WINDOW_CREATE_PARENT_POINTER "SDL.window.create.parent"
#define SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN "SDL.window.create.resizable"
@@ -2844,9 +2848,13 @@ extern SDL_DECLSPEC float SDLCALL SDL_GetWindowOpacity(SDL_Window *window);
/**
* Enable or disable mouse passthrough for a window.
*
+ * This will add or remove the window's `SDL_WINDOW_MOUSE_PASSTHROUGH` flag.
+ *
+ * When enabled, the window will lose mouse focus and will not get any further mouse events (except button up events for buttons held down at the time) and clicks in the window will go through and hit windows behind it.
+ *
* \param window the window to set mouse passthrough for.
* \param passthrough true to make the window transparent to mouse input,
- * false to capture mouse input.
+ * false to accept mouse input.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
diff --git a/src/dynapi/SDL_dynapi.exports b/src/dynapi/SDL_dynapi.exports
index 3423fab5f57fe..f839f8bdf3026 100644
--- a/src/dynapi/SDL_dynapi.exports
+++ b/src/dynapi/SDL_dynapi.exports
@@ -1326,3 +1326,4 @@ _SDL_LoadTexture
_SDL_GetCachePath
_SDL_SetRenderLogicalPresentationRect
_SDL_IsGooglePlayOnPC
+_SDL_SetWindowMousePassthrough
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index b8681f58f2730..8adc02fa4ccab 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -1327,6 +1327,7 @@ SDL3_0.0.0 {
SDL_GetCachePath;
SDL_SetRenderLogicalPresentationRect;
SDL_IsGooglePlayOnPC;
+ SDL_SetWindowMousePassthrough;
# extra symbols go here (don't modify this line)
local: *;
};
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 77b72bd6fe976..188a5167b9fd7 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -1353,3 +1353,4 @@
#define SDL_GetCachePath SDL_GetCachePath_REAL
#define SDL_SetRenderLogicalPresentationRect SDL_SetRenderLogicalPresentationRect_REAL
#define SDL_IsGooglePlayOnPC SDL_IsGooglePlayOnPC_REAL
+#define SDL_SetWindowMousePassthrough SDL_SetWindowMousePassthrough_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index a8d41c83ac757..895aaf9305e24 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -1361,3 +1361,4 @@ SDL_DYNAPI_PROC(SDL_Texture*,SDL_LoadTexture,(SDL_Renderer *a,const char *b),(a,
SDL_DYNAPI_PROC(char*,SDL_GetCachePath,(const char *a,const char *b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_SetRenderLogicalPresentationRect,(SDL_Renderer *a,const SDL_FRect *b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_IsGooglePlayOnPC,(void),(),return)
+SDL_DYNAPI_PROC(bool,SDL_SetWindowMousePassthrough,(SDL_Window *a,bool b),(a,b),return)
diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index 079dd7a14d803..55b10fd60301d 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -573,6 +573,10 @@ void SDL_SetMouseFocus(SDL_Window *window)
{
SDL_Mouse *mouse = SDL_GetMouse();
+ if (SDL_IsMousePassthroughWindow(window)) {
+ window = NULL;
+ }
+
if (mouse->focus == window) {
return;
}
@@ -623,7 +627,11 @@ bool SDL_MousePositionInWindow(SDL_Window *window, float x, float y)
static bool SDL_UpdateMouseFocus(SDL_Window *window, float x, float y, Uint32 buttonstate, bool send_mouse_motion)
{
SDL_Mouse *mouse = SDL_GetMouse();
- bool inWindow = SDL_MousePositionInWindow(window, x, y);
+ bool inWindow = false;
+
+ if (!SDL_IsMousePassthroughWindow(window)) {
+ inWindow = SDL_MousePositionInWindow(window, x, y);
+ }
if (!inWindow) {
if (window == mouse->focus) {
@@ -652,6 +660,10 @@ static bool SDL_UpdateMouseFocus(SDL_Window *window, float x, float y, Uint32 bu
void SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, bool relative, float x, float y)
{
+ if (SDL_IsMousePassthroughWindow(window)) {
+ return;
+ }
+
if (window && !relative) {
SDL_Mouse *mouse = SDL_GetMouse();
if (!SDL_UpdateMouseFocus(window, x, y, SDL_GetMouseButtonState(mouse, mouseID, true), (mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID))) {
@@ -943,6 +955,14 @@ static void SDL_PrivateSendMouseButton(Uint64 timestamp, SDL_Window *window, SDL
}
buttonstate = source->buttonstate;
+ if (SDL_IsMousePassthroughWindow(window)) {
+ // Releases of buttons pressed before passthrough was enabled still go through,
+ // so the button state doesn't get stuck and automatic mouse capture is released.
+ if (down || !(buttonstate & SDL_BUTTON_MASK(button))) {
+ return;
+ }
+ }
+
// SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events
if (mouse->mouse_touch_events) {
if (mouseID != SDL_TOUCH_MOUSEID && mouseID != SDL_PEN_MOUSEID && button == SDL_BUTTON_LEFT) {
@@ -1020,10 +1040,16 @@ static void SDL_PrivateSendMouseButton(Uint64 timestamp, SDL_Window *window, SDL
mouseID = source->mouseID;
}
+ SDL_Window *event_window = mouse->focus;
+ if (!event_window && SDL_IsMousePassthroughWindow(window)) {
+ // This is the release of a button that was held when mouse passthrough was enabled
+ event_window = window;
+ }
+
SDL_Event event;
event.type = type;
event.common.timestamp = timestamp;
- event.button.windowID = mouse->focus ? mouse->focus->id : 0;
+ event.button.windowID = event_window ? event_window->id : 0;
event.button.which = mouseID;
event.button.down = down;
event.button.button = button;
@@ -1059,6 +1085,10 @@ void SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseI
{
SDL_Mouse *mouse = SDL_GetMouse();
+ if (SDL_IsMousePassthroughWindow(window)) {
+ return;
+ }
+
if (window) {
SDL_SetMouseFocus(window);
}
@@ -1473,10 +1503,15 @@ bool SDL_UpdateMouseCapture(bool force_release)
}
if (!force_release) {
- if (SDL_GetMessageBoxCount() == 0 &&
- (mouse->capture_desired || (mouse->auto_capture && SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, false) != 0))) {
+ const bool auto_captured = (mouse->auto_capture && SDL_GetMouseButtonState(mouse, SDL_GLOBAL_MOUSE_ID, false) != 0);
+ if (SDL_GetMessageBoxCount() == 0 && (mouse->capture_desired || auto_captured)) {
if (!mouse->relative_mode) {
capture_window = mouse->focus;
+
+ // A window that enabled mouse passthrough while buttons were held keeps the automatic capture
+ if (!capture_window && auto_captured && SDL_IsMousePassthroughWindow(mouse->capture_window)) {
+ capture_window = mouse->capture_window;
+ }
}
}
}
diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c
index b63e2b296b540..9c43a96507c54 100644
--- a/src/test/SDL_test_common.c
+++ b/src/test/SDL_test_common.c
@@ -2582,6 +2582,14 @@ SDL_AppResult SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const
SDL_Log("%sapturing mouse %s!", shouldCapture ? "C" : "Unc", rc ? "succeeded" : "failed");
}
}
+ if (withAlt) {
+ SDL_Window *window = SDL_GetWindowFromEvent(event);
+ if (window) {
+ const bool shouldEnable = !(SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_PASSTHROUGH);
+ const bool rc = SDL_SetWindowMousePassthrough(window, shouldEnable);
+ SDL_Log("%s mouse passthrough %s!", shouldEnable ? "Enable" : "Disable", rc ? "succeeded" : "failed");
+ }
+ }
break;
case SDLK_R:
if (withControl) {
diff --git a/src/video/SDL_sysvideo.h b/src/video/SDL_sysvideo.h
index 1832f4569e59e..f7eec954a6e7c 100644
--- a/src/video/SDL_sysvideo.h
+++ b/src/video/SDL_sysvideo.h
@@ -303,7 +303,7 @@ struct SDL_VideoDevice
float (*GetWindowContentScale)(SDL_VideoDevice *_this, SDL_Window *window);
void (*GetWindowSizeInPixels)(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h);
bool (*SetWindowOpacity)(SDL_VideoDevice *_this, SDL_Window *window, float opacity);
- void (*SetWindowMousePassthrough)(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough);
+ bool (*SetWindowMousePassthrough)(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough);
bool (*SetWindowParent)(SDL_VideoDevice *_this, SDL_Window *window, SDL_Window *parent);
bool (*SetWindowModal)(SDL_VideoDevice *_this, SDL_Window *window, bool modal);
void (*ShowWindow)(SDL_VideoDevice *_this, SDL_Window *window);
@@ -644,4 +644,6 @@ extern int SDL_GetTextInputKeyboardMargin(SDL_PropertiesID props);
extern void SDL_SendScreenKeyboardShown(void);
extern void SDL_SendScreenKeyboardHidden(void);
+extern bool SDL_IsMousePassthroughWindow(SDL_Window *window);
+
#endif // SDL_sysvideo_h_
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 5afc120818257..10e874e66acda 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -2239,7 +2239,7 @@ SDL_PixelFormat SDL_GetWindowPixelFormat(SDL_Window *window)
}
#define CREATE_FLAGS \
- (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_METAL | SDL_WINDOW_TRANSPARENT | SDL_WINDOW_NOT_FOCUSABLE | SDL_WINDOW_FILL_DOCUMENT)
+ (SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY | SDL_WINDOW_ALWAYS_ON_TOP | SDL_WINDOW_POPUP_MENU | SDL_WINDOW_UTILITY | SDL_WINDOW_TOOLTIP | SDL_WINDOW_VULKAN | SDL_WINDOW_MINIMIZED | SDL_WINDOW_METAL | SDL_WINDOW_TRANSPARENT | SDL_WINDOW_NOT_FOCUSABLE | SDL_WINDOW_FILL_DOCUMENT | SDL_WINDOW_MOUSE_PASSTHROUGH)
static SDL_INLINE bool IsAcceptingDragAndDrop(void)
{
@@ -2344,6 +2344,13 @@ static void SDL_FinishWindowCreation(SDL_Window *window, SDL_WindowFlags flags)
// Whoever has created the window has already applied whatever flags are needed
} else {
ApplyWindowFlags(window, flags);
+
+ if ((window->flags & SDL_WINDOW_MOUSE_PASSTHROUGH) &&
+ (!_this->SetWindowMousePassthrough ||
+ !_this->SetWindowMousePassthrough(_this, window, true))) {
+ window->flags &= ~SDL_WINDOW_MOUSE_PASSTHROUGH;
+ }
+
if (!(flags & SDL_WINDOW_HIDDEN)) {
SDL_ShowWindow(window);
}
@@ -2389,6 +2396,7 @@ static struct {
{ SDL_PROP_WINDOW_CREATE_MINIMIZED_BOOLEAN, SDL_WINDOW_MINIMIZED, false },
{ SDL_PROP_WINDOW_CREATE_MODAL_BOOLEAN, SDL_WINDOW_MODAL, false },
{ SDL_PROP_WINDOW_CREATE_MOUSE_GRABBED_BOOLEAN, SDL_WINDOW_MOUSE_GRABBED, false },
+ { SDL_PROP_WINDOW_CREATE_MOUSE_PASSTHROUGH_BOOLEAN, SDL_WINDOW_MOUSE_PASSTHROUGH, false },
{ SDL_PROP_WINDOW_CREATE_OPENGL_BOOLEAN, SDL_WINDOW_OPENGL, false },
{ SDL_PROP_WINDOW_CREATE_RESIZABLE_BOOLEAN, SDL_WINDOW_RESIZABLE, false },
{ SDL_PROP_WINDOW_CREATE_TRANSPARENT_BOOLEAN, SDL_WINDOW_TRANSPARENT, false },
@@ -2604,6 +2612,9 @@ SDL_Window *SDL_CreateWindowWithProperties(SDL_PropertiesID props)
if (!_this->SetWindowFillDocument) {
window->flags &= ~SDL_WINDOW_FILL_DOCUMENT; // not an error, just unsupported here, so remove the flag.
}
+ if (!_this->SetWindowMousePassthrough) {
+ window->flags &= ~SDL_WINDOW_MOUSE_PASSTHROUGH; // not an error, just unsupported here, so remove the flag.
+ }
if (_this->windows) {
_this->windows->prev = window;
@@ -3870,16 +3881,57 @@ float SDL_GetWindowOpacity(SDL_Window *window)
return window->opacity;
}
+bool SDL_IsMousePassthroughWindow(SDL_Window *window)
+{
+ if (window && (window->flags & SDL_WINDOW_MOUSE_PASSTHROUGH)) {
+ return true;
+ }
+ return false;
+}
+
bool SDL_SetWindowMousePassthrough(SDL_Window *window, bool passthrough)
{
CHECK_WINDOW_MAGIC(window, false);
+ const bool want = (passthrough != false); // normalize the flag.
+ const bool have = ((window->flags & SDL_WINDOW_MOUSE_PASSTHROUGH) != 0);
+ if (want == have) {
+ return true;
+ }
+
if (!_this->SetWindowMousePassthrough) {
return SDL_Unsupported();
}
- _this->SetWindowMousePassthrough(_this, window, passthrough);
+ // Set the window flag so the backends can update appropriately
+ if (want) {
+ window->flags |= SDL_WINDOW_MOUSE_PASSTHROUGH;
+ } else {
+ window->flags &= ~SDL_WINDOW_MOUSE_PASSTHROUGH;
+ }
+ if (!_this->SetWindowMousePassthrough(_this, window, want)) {
+ // Undo the flags we set above
+ if (want) {
+ window->flags &= ~SDL_WINDOW_MOUSE_PASSTHROUGH;
+ } else {
+ window->flags |= SDL_WINDOW_MOUSE_PASSTHROUGH;
+ }
+ return false;
+ }
+ // The window doesn't get mouse events anymore, so send the mouse leave event now.
+ // Automatic mouse capture stays until any held buttons are released.
+ if (want && SDL_GetMouseFocus() == window) {
+ SDL_SetMouseFocus(NULL);
+ }
+
+ // The mouse isn't grabbed or confined while mouse passthrough is enabled
+ if (window->flags & SDL_WINDOW_MOUSE_GRABBED) {
+ SDL_UpdateWindowGrab(window);
+ }
+ if (_this->SetWindowMouseRect && !SDL_RectEmpty(&window->mouse_rect)) {
+ _this->SetWindowMouseRect(_this, window);
+ }
return true;
}
@@ -4028,7 +4080,9 @@ void SDL_UpdateWindowGrab(SDL_Window *window)
bool keyboard_grabbed, mouse_grabbed;
if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
- if (SDL_GetMouse()->relative_mode || (window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
+ if (SDL_IsMousePassthroughWindow(window)) {
+ mouse_grabbed = false;
+ } else if (SDL_GetMouse()->relative_mode || (window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
mouse_grabbed = true;
} else {
mouse_grabbed = false;
diff --git a/src/video/cocoa/SDL_cocoashape.m b/src/video/cocoa/SDL_cocoashape.m
index 3ad4f505277e6..ad09183ef5191 100644
--- a/src/video/cocoa/SDL_cocoashape.m
+++ b/src/video/cocoa/SDL_cocoashape.m
@@ -31,7 +31,10 @@ bool Cocoa_UpdateWindowShape(SDL_VideoDevice *_this, SDL_Window *window, SDL_Sur
SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
BOOL ignoresMouseEvents = NO;
- if (shape) {
+ // Tooltips and mouse passthrough windows never receive mouse input
+ if (window->flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_MOUSE_PASSTHROUGH)) {
+ ignoresMouseEvents = YES;
+ } else if (shape) {
SDL_FPoint point;
SDL_GetGlobalMouseState(&point.x, &point.y);
point.x -= window->x;
diff --git a/src/video/cocoa/SDL_cocoawindow.h b/src/video/cocoa/SDL_cocoawindow.h
index 10624d8d628ff..df73b9976a4a8 100644
--- a/src/video/cocoa/SDL_cocoawindow.h
+++ b/src/video/cocoa/SDL_cocoawindow.h
@@ -176,7 +176,7 @@ extern void Cocoa_SetWindowMaximumSize(SDL_VideoDevice *_this, SDL_Window *windo
extern void Cocoa_SetWindowAspectRatio(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window, int *w, int *h);
extern bool Cocoa_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity);
-extern void Cocoa_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough);
+extern bool Cocoa_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough);
extern void Cocoa_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_HideWindow(SDL_VideoDevice *_this, SDL_Window *window);
extern void Cocoa_RaiseWindow(SDL_VideoDevice *_this, SDL_Window *window);
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index 0b79d32c436a2..f30d4ff3e8309 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -34,6 +34,7 @@
#include "SDL_cocoamouse.h"
#include "SDL_cocoaopengl.h"
#include "SDL_cocoaopengles.h"
+#include "SDL_cocoashape.h"
#include "SDL_cocoavideo.h"
#if 0
@@ -616,7 +617,7 @@ static bool ShouldAdjustCoordinatesForGrab(SDL_Window *window)
return false;
}
- if (!(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
+ if (!(window->flags & SDL_WINDOW_INPUT_FOCUS) || SDL_IsMousePassthroughWindow(window)) {
return false;
}
@@ -1090,7 +1091,10 @@ - (void)updateIgnoreMouseState:(NSEvent *)theEvent
SDL_Surface *shape = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_SHAPE_POINTER, NULL);
BOOL ignoresMouseEvents = NO;
- if (shape) {
+ // Tooltips and mouse passthrough windows never receive mouse input
+ if (window->flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_MOUSE_PASSTHROUGH)) {
+ ignoresMouseEvents = YES;
+ } else if (shape) {
NSPoint point = [theEvent locationInWindow];
NSRect windowRect = [[_data.nswindow contentView] frame];
if (NSMouseInRect(point, windowRect, NO)) {
@@ -3532,10 +3536,12 @@ bool Cocoa_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float op
}
}
-void Cocoa_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough) {
+bool Cocoa_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough)
+{
@autoreleasepool {
- SDL_CocoaWindowData *data = (__bridge SDL_CocoaWindowData *)window->internal;
- [data.nswindow setIgnoresMouseEvents:passthrough];
+ // Update the shape to take mouse passthrough and the window shape into account
+ SDL_Surface *shape = (SDL_Surface *)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_SHAPE_POINTER, NULL);
+ return Cocoa_UpdateWindowShape(_this, window, shape);
}
}
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
index 462c672d4b007..c4b9b383e9262 100644
--- a/src/video/wayland/SDL_waylandevents.c
+++ b/src/video/wayland/SDL_waylandevents.c
@@ -3322,7 +3322,7 @@ void Wayland_UpdatePointerGrab(Wayland_Seat *seat)
SDL_Window *window = w->sdlwindow;
// Don't confine the pointer if the window doesn't have input focus, or it shouldn't be confined.
- if (!(window->flags & SDL_WINDOW_INPUT_FOCUS) ||
+ if (!(window->flags & SDL_WINDOW_INPUT_FOCUS) || SDL_IsMousePassthroughWindow(window) ||
(!(window->flags & SDL_WINDOW_MOUSE_GRABBED) && SDL_RectEmpty(&window->mouse_rect))) {
return;
}
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index e3480715078c2..9f65b9f62425f 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -3506,18 +3506,25 @@ bool Wayland_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float
return SDL_SetError("wayland: set window opacity failed; compositor lacks support for the required wp_alpha_modifier_v1 protocol");
}
-void Wayland_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough)
+bool Wayland_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough)
{
SDL_VideoData *viddata = _this->internal;
SDL_WindowData *data = window->internal;
- if (passthrough) {
+ // Tooltips and mouse passthrough windows never receive mouse input
+ if (window->flags & (SDL_WINDOW_TOOLTIP | SDL_WINDOW_MOUSE_PASSTHROUGH)) {
struct wl_region *region = wl_compositor_create_region(viddata->compositor);
+ if (!region) {
+ return SDL_OutOfMemory();
+ }
wl_surface_set_input_region(data->surface, region);
wl_region_destroy(region);
} else {
wl_surface_set_input_region(data->surface, NULL);
}
+
+ // The input region is double-buffered and is applied with the next surface commit
+ return true;
}
void Wayland_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window)
diff --git a/src/video/wayland/SDL_waylandwindow.h b/src/video/wayland/SDL_waylandwindow.h
index 0a6af93d048bd..7e18a3eee5660 100644
--- a/src/video/wayland/SDL_waylandwindow.h
+++ b/src/video/wayland/SDL_waylandwindow.h
@@ -286,7 +286,7 @@ extern SDL_DisplayID Wayland_GetDisplayForWindow(SDL_VideoDevice *_this, SDL_Win
extern bool Wayland_SetWindowParent(SDL_VideoDevice *_this, SDL_Window *window, SDL_Window *parent_window);
extern bool Wayland_SetWindowModal(SDL_VideoDevice *_this, SDL_Window *window, bool modal);
extern bool Wayland_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity);
-extern void Wayland_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough);
+extern bool Wayland_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough);
extern void Wayland_SetWindowTitle(SDL_VideoDevice *_this, SDL_Window *window);
extern void Wayland_ShowWindowSystemMenu(SDL_Window *window, int x, int y);
extern void Wayland_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window);
diff --git a/src/video/windows/SDL_windowswindow.c b/src/video/windows/SDL_windowswindow.c
index 196bf7730891a..d37e6f2f99b43 100644
--- a/src/video/windows/SDL_windowswindow.c
+++ b/src/video/windows/SDL_windowswindow.c
@@ -388,6 +388,7 @@ static bool SetupWindowData(SDL_VideoDevice *_this, SDL_Window *window, HWND hwn
#endif
data->hinstance = (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
data->mouse_button_flags = (WPARAM)-1;
+ data->opacity = 1.0f;
data->last_pointer_update = (LPARAM)-1;
data->videodata = videodata;
data->initializing = true;
@@ -1556,7 +1557,7 @@ void WIN_UpdateClipCursor(SDL_Window *window)
bool win_have_focus = (window->flags & SDL_WINDOW_INPUT_FOCUS);
bool win_is_grabbed = (window->flags & SDL_WINDOW_MOUSE_GRABBED);
bool win_in_relmode = (window->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE);
- bool cursor_confine = win_in_relmode || win_is_grabbed || win_mouse_rect;
+ bool cursor_confine = !SDL_IsMousePassthroughWindow(window) && (win_in_relmode || win_is_grabbed || win_mouse_rect);
// This is verbatim translation of the old logic,
// but I don't quite get what it's trying to do.
@@ -1682,67 +1683,63 @@ bool WIN_SetWindowHitTest(SDL_Window *window, bool enabled)
}
#endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
-bool WIN_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity)
+static bool WIN_UpdateLayeredStyle(SDL_Window *window, float opacity)
{
#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
- return false;
+ return SDL_Unsupported();
#else
const SDL_WindowData *data = window->internal;
HWND hwnd = data->hwnd;
- const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
+ LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
+ const bool passthrough = ((window->flags & SDL_WINDOW_MOUSE_PASSTHROUGH) != 0);
- if (opacity == 1.0f) {
- // want it fully opaque, just mark it unlayered if necessary.
- if (style & WS_EX_LAYERED) {
- if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0) {
- return WIN_SetError("SetWindowLong()");
- }
- }
+ // WS_EX_TRANSPARENT only passes clicks through to other windows on a layered window
+ const bool layered = (passthrough || opacity < 1.0f);
+
+ if (passthrough) {
+ style |= WS_EX_TRANSPARENT;
} else {
- const BYTE alpha = (BYTE)((int)(opacity * 255.0f));
- // want it transparent, mark it layered if necessary.
- if (!(style & WS_EX_LAYERED)) {
- if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0) {
- return WIN_SetError("SetWindowLong()");
- }
- }
+ style &= ~WS_EX_TRANSPARENT;
+ }
+ if (layered) {
+ style |= WS_EX_LAYERED;
+ } else {
+ style &= ~WS_EX_LAYERED;
+ }
+ // SetWindowLong() returns the previous style, which can legitimately be 0
+ SetLastError(0);
+ if (SetWindowLong(hwnd, GWL_EXSTYLE, style) == 0 && GetLastError() != 0) {
+ return WIN_SetError("SetWindowLong()");
+ }
+
+ if (layered) {
+ const BYTE alpha = (BYTE)((int)(opacity * 255.0f));
if (SetLayeredWindowAttributes(hwnd, 0, alpha, LWA_ALPHA) == 0) {
return WIN_SetError("SetLayeredWindowAttributes()");
}
}
-
return true;
#endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
}
-void WIN_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough)
+bool WIN_SetWindowOpacity(SDL_VideoDevice *_this, SDL_Window *window, float opacity)
{
-#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES)
-#else
SDL_WindowData *data = window->internal;
- HWND hwnd = data->hwnd;
- LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
- if (passthrough) {
- COLORREF key = 0;
- BYTE alpha = 0;
- DWORD flags = 0;
- if (style & WS_EX_LAYERED) {
- GetLayeredWindowAttributes(hwnd, &key, &alpha, &flags);
- }
- style |= (WS_EX_TRANSPARENT | WS_EX_LAYERED);
- SetWindowLong(hwnd, GWL_EXSTYLE, style);
- SetLayeredWindowAttributes(hwnd, key, alpha, flags);
- } else {
- style &= ~WS_EX_TRANSPARENT;
-
- if ((style & (WS_EX_LAYERED | LWA_ALPHA)) == WS_EX_LAYERED) {
- style &= ~WS_EX_LAYERED;
- }
- SetWindowLong(hwnd, GWL_EXSTYLE, style);
+ if (!WIN_UpdateLayeredStyle(window, opacity)) {
+ return false;
}
-#endif // !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
+
+ data->opacity = opacity;
+ return true;
+}
+
+bool WIN_SetWindowMousePassthrough(SDL_VideoDevice *_this, SDL_Window *window, bool passthrough)
+{
+ SDL_WindowData *data = window->internal;
+
+ return WIN_UpdateLayeredStyle(window, data->opacity);
}
#if !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
diff --git a/src/video/windows/SDL_windowswindow.h b/src/video/windows/SDL_windowswindow.h
index ccbfab7246979..2fd61b73fd93c 100644
--- a/src/video/windows/SDL_windowswindow.h
+++ b/src/video/windows/SDL_windowswindow.h
@@ -74,6 +74,7 @@ struct SDL_WindowData
WPARAM mouse_button_flags;
LPARAM last_pointer_update;
WCHAR high_surrogate;
+ float opacity;
bool initializing;
bool expected_resize;
bool in_border_change;
@@ -115,7 +116,7 @@ extern void WIN_SetWindowSize(SDL_VideoDevice *_this, SDL_Window *window);
extern bool WIN_GetWindowBordersSize(SDL_VideoDevice *_this, SDL_Window *window, int *top, int *left, int *bottom, int *right);
extern void WIN_GetWindowSizeInPixels(SDL_VideoDevice *_this, SDL_Window *window,
(Patch may be truncated, please check the link at the top of this post.)