Better implementation of SDL_SetWindowMouseGrab() and SDL_SetWindowMouseRect() on macOS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index 2486071..7908646 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -59,6 +59,11 @@
#define NSAppKitVersionNumber10_14 1671
#endif
+/* This is available as of 10.13.2, but isn't in public headers */
+@interface NSWindow (SDL)
+@property (nonatomic) NSRect mouseConfinementRect;
+@end
+
@interface SDLWindow : NSWindow <NSDraggingDestination>
/* These are needed for borderless/fullscreen windows */
- (BOOL)canBecomeKeyWindow;
@@ -381,6 +386,60 @@ AdjustCoordinatesForGrab(SDL_Window * window, int x, int y, CGPoint *adjusted)
return SDL_FALSE;
}
+static void
+Cocoa_UpdateClipCursor(SDL_Window * window)
+{
+ SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+
+ if (@available(macOS 10.13.2, *)) {
+ NSWindow *nswindow = data->nswindow;
+ SDL_Rect mouse_rect;
+
+ SDL_zero(mouse_rect);
+
+ if (ShouldAdjustCoordinatesForGrab(window)) {
+ SDL_Rect window_rect;
+
+ window_rect.x = 0;
+ window_rect.y = 0;
+ window_rect.w = window->w;
+ window_rect.h = window->h;
+
+ if (window->mouse_rect.w > 0 && window->mouse_rect.h > 0) {
+ SDL_IntersectRect(&window->mouse_rect, &window_rect, &mouse_rect);
+ }
+
+ if ((window->flags & SDL_WINDOW_MOUSE_GRABBED) != 0 &&
+ SDL_RectEmpty(&mouse_rect)) {
+ SDL_memcpy(&mouse_rect, &window_rect, sizeof(mouse_rect));
+ }
+ }
+
+ if (SDL_RectEmpty(&mouse_rect)) {
+ nswindow.mouseConfinementRect = NSZeroRect;
+ } else {
+ NSRect rect;
+ rect.origin.x = mouse_rect.x;
+ rect.origin.y = [nswindow contentLayoutRect].size.height - mouse_rect.y - mouse_rect.h;
+ rect.size.width = mouse_rect.w;
+ rect.size.height = mouse_rect.h;
+ data->nswindow.mouseConfinementRect = rect;
+ }
+ } else {
+ /* Move the cursor to the nearest point in the window */
+ if (ShouldAdjustCoordinatesForGrab(window)) {
+ int x, y;
+ CGPoint cgpoint;
+
+ SDL_GetGlobalMouseState(&x, &y);
+ if (AdjustCoordinatesForGrab(window, x, y, &cgpoint)) {
+ Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
+ CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
+ }
+ }
+ }
+}
+
@implementation Cocoa_WindowListener
@@ -628,6 +687,8 @@ AdjustCoordinatesForGrab(SDL_Window * window, int x, int y, CGPoint *adjusted)
}
mouse->SetRelativeMouseMode(SDL_TRUE);
+ } else {
+ Cocoa_UpdateClipCursor(_data->window);
}
}
}
@@ -726,6 +787,10 @@ AdjustCoordinatesForGrab(SDL_Window * window, int x, int y, CGPoint *adjusted)
- (void)windowDidMiniaturize:(NSNotification *)aNotification
{
+ if (focusClickPending) {
+ focusClickPending = 0;
+ [self onMovingOrFocusClickPendingStateCleared];
+ }
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
}
@@ -1215,12 +1280,16 @@ AdjustCoordinatesForGrab(SDL_Window * window, int x, int y, CGPoint *adjusted)
x = (int)point.x;
y = (int)(window->h - point.y);
- CGPoint cgpoint;
- if (ShouldAdjustCoordinatesForGrab(window) &&
- AdjustCoordinatesForGrab(window, window->x + x, window->y + y, &cgpoint)) {
- Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
- CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
- CGAssociateMouseAndMouseCursorPosition(YES);
+ if (@available(macOS 10.13.2, *)) {
+ /* Mouse grab is taken care of by the confinement rect */
+ } else {
+ CGPoint cgpoint;
+ if (ShouldAdjustCoordinatesForGrab(window) &&
+ AdjustCoordinatesForGrab(window, window->x + x, window->y + y, &cgpoint)) {
+ Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
+ CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
+ CGAssociateMouseAndMouseCursorPosition(YES);
+ }
}
SDL_SendMouseMotion(window, mouseID, 0, x, y);
@@ -2054,7 +2123,7 @@ Cocoa_GetWindowICCProfile(_THIS, SDL_Window * window, size_t * size)
if (iccProfileData == nil) {
SDL_SetError("Could not get ICC profile data.");
return NULL;
- }
+ }
retIccProfileData = SDL_malloc([iccProfileData length]);
if (!retIccProfileData) {
@@ -2094,17 +2163,7 @@ Cocoa_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
void
Cocoa_SetWindowMouseRect(_THIS, SDL_Window * window)
{
- /* Move the cursor to the nearest point in the mouse rect */
- if (ShouldAdjustCoordinatesForGrab(window)) {
- int x, y;
- CGPoint cgpoint;
-
- SDL_GetGlobalMouseState(&x, &y);
- if (AdjustCoordinatesForGrab(window, x, y, &cgpoint)) {
- Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
- CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
- }
- }
+ Cocoa_UpdateClipCursor(window);
}
void
@@ -2112,17 +2171,7 @@ Cocoa_SetWindowMouseGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
- /* Move the cursor to the nearest point in the window */
- if (ShouldAdjustCoordinatesForGrab(window)) {
- int x, y;
- CGPoint cgpoint;
-
- SDL_GetGlobalMouseState(&x, &y);
- if (AdjustCoordinatesForGrab(window, x, y, &cgpoint)) {
- Cocoa_HandleMouseWarp(cgpoint.x, cgpoint.y);
- CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
- }
- }
+ Cocoa_UpdateClipCursor(window);
if (data && (window->flags & SDL_WINDOW_FULLSCREEN)) {
if (SDL_ShouldAllowTopmost() && (window->flags & SDL_WINDOW_INPUT_FOCUS)