Mac: Improve moving relative mode windows. This makes it possible to move windows by their title bar, even if they're in relative mode, if you click the title bar when the window does not have focus. Bug: https://bugzilla.libsdl.org/show_bug.cgi?id=2396
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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
diff --git a/src/video/cocoa/SDL_cocoamouse.m b/src/video/cocoa/SDL_cocoamouse.m
index 56f6796..12f506c 100644
--- a/src/video/cocoa/SDL_cocoamouse.m
+++ b/src/video/cocoa/SDL_cocoamouse.m
@@ -29,6 +29,14 @@
#include "../../events/SDL_mouse_c.h"
+/* #define DEBUG_COCOAMOUSE */
+
+#ifdef DEBUG_COCOAMOUSE
+#define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__)
+#else
+#define DLog(...) do { } while (0)
+#endif
+
@implementation NSCursor (InvisibleCursor)
+ (NSCursor *)invisibleCursor
{
@@ -203,6 +211,15 @@ Cocoa_ShowCursor(SDL_Cursor * cursor)
static void
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
{
+ SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+ if ([data->listener isMoving])
+ {
+ DLog("Postponing warp, window being moved.");
+ [data->listener setPendingMoveX:x
+ Y:y];
+ return;
+ }
+
SDL_Mouse *mouse = SDL_GetMouse();
CGPoint point = CGPointMake(x + (float)window->x, y + (float)window->y);
@@ -214,6 +231,7 @@ Cocoa_WarpMouse(SDL_Window * window, int x, int y)
NSPoint location = [NSEvent mouseLocation];
driverdata->deltaXOffset = location.x - point.x;
driverdata->deltaYOffset = point.y - location.y;
+ DLog("Warp to (%g, %g), offsetting next movement event by (%i, %i)", point.x, point.y, driverdata->deltaXOffset, driverdata->deltaYOffset);
}
/* According to the docs, this was deprecated in 10.6, but it's still
@@ -238,8 +256,10 @@ Cocoa_SetRelativeMouseMode(SDL_bool enabled)
CGError result;
if (enabled) {
+ DLog("Turning on.");
result = CGAssociateMouseAndMouseCursorPosition(NO);
} else {
+ DLog("Turning off.");
result = CGAssociateMouseAndMouseCursorPosition(YES);
}
if (result != kCGErrorSuccess) {
@@ -272,18 +292,42 @@ Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
{
SDL_Mouse *mouse = SDL_GetMouse();
- if (mouse->relative_mode &&
- ([event type] == NSMouseMoved ||
- [event type] == NSLeftMouseDragged ||
- [event type] == NSRightMouseDragged ||
- [event type] == NSOtherMouseDragged)) {
- SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
- float x = [event deltaX] + driverdata->deltaXOffset;
- float y = [event deltaY] + driverdata->deltaYOffset;
- driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
+ /* Non-relative movement is handled in -[Cocoa_WindowListener mouseMoved:] */
+ if (!mouse->relative_mode) {
+ return;
+ }
- SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
+ switch ([event type])
+ {
+ case NSMouseMoved:
+ case NSLeftMouseDragged:
+ case NSRightMouseDragged:
+ case NSOtherMouseDragged:
+ break;
+
+ default:
+ /* Ignore any other events. */
+ return;
+ }
+
+ /* Ignore events that aren't inside the client area (i.e. title bar.) */
+ if ([event window]) {
+ NSRect windowRect = [[[event window] contentView] frame];
+ if (!NSPointInRect([event locationInWindow], windowRect)) {
+ return;
+ }
}
+
+ SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
+ float x = [event deltaX] + driverdata->deltaXOffset;
+ float y = [event deltaY] + driverdata->deltaYOffset;
+ driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
+
+ if (driverdata->deltaYOffset > 0 || driverdata->deltaXOffset > 0) {
+ DLog("Relative move was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], x, y);
+ }
+
+ SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
}
void
diff --git a/src/video/cocoa/SDL_cocoawindow.h b/src/video/cocoa/SDL_cocoawindow.h
index 65e0d4b..4f88ce1 100644
--- a/src/video/cocoa/SDL_cocoawindow.h
+++ b/src/video/cocoa/SDL_cocoawindow.h
@@ -43,6 +43,8 @@ typedef enum
BOOL isFullscreenSpace;
BOOL inFullscreenTransition;
PendingWindowOperation pendingWindowOperation;
+ BOOL isMoving;
+ int pendingWindowWarpX, pendingWindowWarpY;
}
-(void) listen:(SDL_WindowData *) data;
@@ -54,6 +56,10 @@ typedef enum
-(void) addPendingWindowOperation:(PendingWindowOperation) operation;
-(void) close;
+-(BOOL) isMoving;
+-(void) setPendingMoveX:(int)x Y:(int)y;
+-(void) windowDidFinishMoving;
+
/* Window delegate functionality */
-(BOOL) windowShouldClose:(id) sender;
-(void) windowDidExpose:(NSNotification *) aNotification;
@@ -105,6 +111,7 @@ struct SDL_WindowData
NSWindow *nswindow;
NSMutableArray *nscontexts;
SDL_bool created;
+ SDL_bool inWindowMove;
Cocoa_WindowListener *listener;
struct SDL_VideoData *videodata;
};
diff --git a/src/video/cocoa/SDL_cocoawindow.m b/src/video/cocoa/SDL_cocoawindow.m
index a773cb6..e391ea7 100644
--- a/src/video/cocoa/SDL_cocoawindow.m
+++ b/src/video/cocoa/SDL_cocoawindow.m
@@ -39,6 +39,44 @@
#include "SDL_cocoamouse.h"
#include "SDL_cocoaopengl.h"
+@interface SDLWindow : NSWindow
+/* These are needed for borderless/fullscreen windows */
+- (BOOL)canBecomeKeyWindow;
+- (BOOL)canBecomeMainWindow;
+- (void)sendEvent:(NSEvent *)event;
+@end
+
+@implementation SDLWindow
+- (BOOL)canBecomeKeyWindow
+{
+ return YES;
+}
+
+- (BOOL)canBecomeMainWindow
+{
+ return YES;
+}
+
+- (void)sendEvent:(NSEvent *)event
+{
+ [super sendEvent:event];
+
+ if ([event type] != NSLeftMouseUp) {
+ return;
+ }
+
+ id delegate = [self delegate];
+ if (![delegate isKindOfClass:[Cocoa_WindowListener class]]) {
+ return;
+ }
+
+ if ([delegate isMoving]) {
+ [delegate windowDidFinishMoving];
+ }
+}
+@end
+
+
static Uint32 s_moveHack;
static void ConvertNSRect(NSRect *r)
@@ -130,6 +168,7 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
isFullscreenSpace = NO;
inFullscreenTransition = NO;
pendingWindowOperation = PENDING_OPERATION_NONE;
+ isMoving = NO;
center = [NSNotificationCenter defaultCenter];
@@ -315,6 +354,34 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
}
}
+- (BOOL)isMoving
+{
+ return isMoving;
+}
+
+-(void) setPendingMoveX:(int)x Y:(int)y
+{
+ pendingWindowWarpX = x;
+ pendingWindowWarpY = y;
+}
+
+- (void)windowDidFinishMoving
+{
+ if ([self isMoving])
+ {
+ isMoving = NO;
+
+ SDL_Mouse *mouse = SDL_GetMouse();
+ if (pendingWindowWarpX >= 0 && pendingWindowWarpY >= 0) {
+ mouse->WarpMouse(_data->window, pendingWindowWarpX, pendingWindowWarpY);
+ pendingWindowWarpX = pendingWindowWarpY = -1;
+ }
+ if (mouse->relative_mode && SDL_GetMouseFocus() == _data->window) {
+ mouse->SetRelativeMouseMode(SDL_TRUE);
+ }
+ }
+}
+
- (BOOL)windowShouldClose:(id)sender
{
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
@@ -326,6 +393,14 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
}
+- (void)windowWillMove:(NSNotification *)aNotification
+{
+ if ([_data->nswindow isKindOfClass:[SDLWindow class]]) {
+ pendingWindowWarpX = pendingWindowWarpY = -1;
+ isMoving = YES;
+ }
+}
+
- (void)windowDidMove:(NSNotification *)aNotification
{
int x, y;
@@ -407,6 +482,9 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
{
SDL_Window *window = _data->window;
SDL_Mouse *mouse = SDL_GetMouse();
+ if (mouse->relative_mode && ![self isMoving]) {
+ mouse->SetRelativeMouseMode(SDL_TRUE);
+ }
/* We're going to get keyboard events, since we're key. */
SDL_SetKeyboardFocus(window);
@@ -431,6 +509,11 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
- (void)windowDidResignKey:(NSNotification *)aNotification
{
+ SDL_Mouse *mouse = SDL_GetMouse();
+ if (mouse->relative_mode) {
+ mouse->SetRelativeMouseMode(SDL_FALSE);
+ }
+
/* Some other window will get mouse events, since we're not key. */
if (SDL_GetMouseFocus() == _data->window) {
SDL_SetMouseFocus(NULL);
@@ -624,8 +707,6 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
- CGPoint cgpoint;
-
if (x < 0) {
x = 0;
} else if (x >= window->w) {
@@ -638,6 +719,8 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
}
#if !SDL_MAC_NO_SANDBOX
+ CGPoint cgpoint;
+
/* When SDL_MAC_NO_SANDBOX is set, this is handled by
* SDL_cocoamousetap.m.
*/
@@ -754,24 +837,6 @@ SetWindowStyle(SDL_Window * window, unsigned int style)
@end
-@interface SDLWindow : NSWindow
-/* These are needed for borderless/fullscreen windows */
-- (BOOL)canBecomeKeyWindow;
-- (BOOL)canBecomeMainWindow;
-@end
-
-@implementation SDLWindow
-- (BOOL)canBecomeKeyWindow
-{
- return YES;
-}
-
-- (BOOL)canBecomeMainWindow
-{
- return YES;
-}
-@end
-
@interface SDLView : NSView
/* The default implementation doesn't pass rightMouseDown to responder chain */
@@ -1338,7 +1403,8 @@ void
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
/* Move the cursor to the nearest point in the window */
- if (grabbed) {
+ SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
+ if (grabbed && data && ![data->listener isMoving]) {
int x, y;
CGPoint cgpoint;