Skip to content

Commit 64fb16e

Browse files
committed
Add patch
1 parent 09f5317 commit 64fb16e

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
diff --git a/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m b/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m
2+
index 3425737..859b9f3 100644
3+
--- a/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m
4+
+++ b/Sources/SwiftSDL3/src/video/cocoa/SDL_cocoawindow.m
5+
@@ -1703,6 +1703,56 @@ - (BOOL)processHitTest:(NSEvent *)theEvent
6+
return NO; // not a special area, carry on.
7+
}
8+
9+
+static void Cocoa_SyncMouseButtonState(SDL_Mouse *mouse, SDL_MouseID mouseID)
10+
+{
11+
+ const NSUInteger actualButtons = [NSEvent pressedMouseButtons];
12+
+ Uint32 actualButtonState = 0;
13+
+ Uint32 sdlButtonState = 0;
14+
+
15+
+ // Convert NSEvent button mask to SDL button mask
16+
+ if (actualButtons & (1 << 0)) {
17+
+ actualButtonState |= SDL_BUTTON_LMASK;
18+
+ }
19+
+ if (actualButtons & (1 << 1)) {
20+
+ actualButtonState |= SDL_BUTTON_RMASK;
21+
+ }
22+
+ if (actualButtons & (1 << 2)) {
23+
+ actualButtonState |= SDL_BUTTON_MMASK;
24+
+ }
25+
+ if (actualButtons & (1 << 3)) {
26+
+ actualButtonState |= SDL_BUTTON_X1MASK;
27+
+ }
28+
+ if (actualButtons & (1 << 4)) {
29+
+ actualButtonState |= SDL_BUTTON_X2MASK;
30+
+ }
31+
+
32+
+ // Get SDL's tracked button state
33+
+ for (int i = 0; i < mouse->num_sources; ++i) {
34+
+ if (mouseID == SDL_GLOBAL_MOUSE_ID || mouseID == SDL_TOUCH_MOUSEID) {
35+
+ if (mouse->sources[i].mouseID != SDL_TOUCH_MOUSEID) {
36+
+ sdlButtonState |= mouse->sources[i].buttonstate;
37+
+ }
38+
+ } else {
39+
+ if (mouseID == mouse->sources[i].mouseID) {
40+
+ sdlButtonState = mouse->sources[i].buttonstate;
41+
+ break;
42+
+ }
43+
+ }
44+
+ }
45+
+
46+
+ // Sync any buttons that are out of sync
47+
+ for (Uint8 btn = SDL_BUTTON_LEFT; btn <= SDL_BUTTON_X2; ++btn) {
48+
+ Uint32 buttonMask = SDL_BUTTON_MASK(btn);
49+
+ bool sdlPressed = (sdlButtonState & buttonMask) != 0;
50+
+ bool actuallyPressed = (actualButtonState & buttonMask) != 0;
51+
+
52+
+ if (sdlPressed != actuallyPressed) {
53+
+ // Send synthetic event to sync state
54+
+ SDL_SendMouseButton(0, mouse->focus, mouseID, btn, actuallyPressed);
55+
+ }
56+
+ }
57+
+}
58+
+
59+
static void Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_Window *window, Uint8 button, bool down)
60+
{
61+
SDL_MouseID mouseID = SDL_DEFAULT_MOUSE_ID;
62+
@@ -1919,6 +1969,9 @@ - (void)mouseMoved:(NSEvent *)theEvent
63+
}
64+
}
65+
66+
+ // Sync button state with hardware before sending motion event
67+
+ Cocoa_SyncMouseButtonState(mouse, mouseID);
68+
+
69+
SDL_SendMouseMotion(Cocoa_GetEventTimestamp([theEvent timestamp]), window, mouseID, false, x, y);
70+
}
71+

patches/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ This directory contains custom patches that have been applied to the SDL3 source
2121
- Comments out the precise scrolling delta scaling code in SDL_cocoamouse.m
2222
- Prevents over-scaling of trackpad scroll events
2323

24+
7. **0007-sync-mouse-button-state.patch** - Sync mouse button state with hardware
25+
- Adds Cocoa_SyncMouseButtonState function that uses NSEvent.pressedMouseButtons to synchronize SDL's tracked button state with macOS hardware state
26+
- Ensures event.motion.state accurately reflects actual button presses
27+
- Calls sync in mouseMoved: handler before sending motion events to maintain consistency
28+
2429
## How to Apply Patches
2530

2631
After updating the SDL3 source code, apply these patches in order:
@@ -33,6 +38,7 @@ git apply patches/0003-commit-a56a797.patch
3338
git apply patches/0004-commit-f20e32b.patch
3439
git apply patches/0005-add-modulemap.patch
3540
git apply patches/0006-comment-precise-scrolling.patch
41+
git apply patches/0007-sync-mouse-button-state.patch
3642
```
3743

3844
Or apply them all at once:

0 commit comments

Comments
 (0)