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

Mouse events on Cocoa macOS stopped working. #12323

Open
mcourteaux opened this issue Feb 18, 2025 · 1 comment
Open

Mouse events on Cocoa macOS stopped working. #12323

mcourteaux opened this issue Feb 18, 2025 · 1 comment
Assignees
Milestone

Comments

@mcourteaux
Copy link

mcourteaux commented Feb 18, 2025

After running git bisect, I found this to be the offending commit:

ea642fe9ff17e90a0ccc0ec95d007859c4ab02bc is the first bad commit
commit ea642fe9ff17e90a0ccc0ec95d007859c4ab02bc
Author: Sam Lantinga <[email protected]>
Date:   Wed Jan 15 23:34:20 2025 -0800

    cocoa: clear mouse focus based on NSEventTypeMouseExited events (#11991)
    
    We can't directly set the mouse focus since we may get spammed by entered/exited events,
    but we can process the current focus later in the mouseMoved handler in line with the
    mouse motion event sequence.
    
    Fixes https://github.com/libsdl-org/SDL/issues/8188

 src/video/cocoa/SDL_cocoaevents.m |  2 ++
 src/video/cocoa/SDL_cocoamouse.h  |  1 +
 src/video/cocoa/SDL_cocoamouse.m  | 28 ++++++++++++++++++++++------
 src/video/cocoa/SDL_cocoawindow.m |  6 ++++++
 4 files changed, 31 insertions(+), 6 deletions(-)

Mouse events behave very wonky, they 100% don't work the first few seconds/moments/events. Then they start working but very oddly. I haven't fully figured out what's going on. I'm suspecting a state-bug, given the simple changes in the commit. @slouken you seemed to have edited this code last in #11991, while fixing #8188.

@mcourteaux
Copy link
Author

mcourteaux commented Feb 18, 2025

I'm not 100% sure what the mechanic is, but I suspect that the initial mouse entered event is missing for me when the window spawns underneath the cursor. Otherwise I don't know immediately under which conditions the Cocoa_MouseFocus global is not set. Leaving and reentering the window with the mouse does not fix the issue, so I'm suspecting my macOS version (12.7) doesn't report those enter / leave events the same way as it happens on a more modern version of macOS?

This quick-and-dirty test fixes the mouse again:

--- a/src/video/cocoa/SDL_cocoamouse.m
+++ b/src/video/cocoa/SDL_cocoamouse.m
@@ -480,6 +480,9 @@ void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
         return;
     }
 
+    // Any event coming through clearly was happening inside a window.
+    Cocoa_MouseFocus = [event window];
+
     mouse = SDL_GetMouse();
     data = (SDL_MouseData *)mouse->internal;
     if (!data) {

However, perhaps even this is a slightly better fix (also works):

diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m
index 77ebf109d..b6fa254b2 100644
--- a/src/video/cocoa/SDL_cocoamouse.m
+++ b/src/video/cocoa/SDL_cocoamouse.m
@@ -447,10 +447,9 @@ void Cocoa_HandleMouseEvent(SDL_VideoDevice *_this, NSEvent *event)
     float deltaX, deltaY;
     bool seenWarp;
 
+    Cocoa_MouseFocus = [event window];
+
     switch ([event type]) {
-    case NSEventTypeMouseEntered:
-        Cocoa_MouseFocus = [event window];
-        return;
     case NSEventTypeMouseExited:
         Cocoa_MouseFocus = NULL;
         return;

@icculus icculus added this to the 3.2.6 milestone Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants