Mac: Improve moving relative mode windows.
authorJørgen P. Tjernø <jorgen@valvesoftware.com>
Tue, 25 Feb 2014 17:27:41 -0800
changeset 8260028ed8da2524
parent 8259 1816872373d5
child 8261 841b66e4397a
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
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Tue Feb 25 17:25:49 2014 -0800
     1.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Tue Feb 25 17:27:41 2014 -0800
     1.3 @@ -29,6 +29,14 @@
     1.4  
     1.5  #include "../../events/SDL_mouse_c.h"
     1.6  
     1.7 +/* #define DEBUG_COCOAMOUSE */
     1.8 +
     1.9 +#ifdef DEBUG_COCOAMOUSE
    1.10 +#define DLog(fmt, ...) printf("%s: " fmt "\n", __func__, ##__VA_ARGS__)
    1.11 +#else
    1.12 +#define DLog(...) do { } while (0)
    1.13 +#endif
    1.14 +
    1.15  @implementation NSCursor (InvisibleCursor)
    1.16  + (NSCursor *)invisibleCursor
    1.17  {
    1.18 @@ -203,6 +211,15 @@
    1.19  static void
    1.20  Cocoa_WarpMouse(SDL_Window * window, int x, int y)
    1.21  {
    1.22 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    1.23 +    if ([data->listener isMoving])
    1.24 +    {
    1.25 +        DLog("Postponing warp, window being moved.");
    1.26 +        [data->listener setPendingMoveX:x
    1.27 +                                      Y:y];
    1.28 +        return;
    1.29 +    }
    1.30 +
    1.31      SDL_Mouse *mouse = SDL_GetMouse();
    1.32      CGPoint point = CGPointMake(x + (float)window->x, y + (float)window->y);
    1.33  
    1.34 @@ -214,6 +231,7 @@
    1.35          NSPoint location =  [NSEvent mouseLocation];
    1.36          driverdata->deltaXOffset = location.x - point.x;
    1.37          driverdata->deltaYOffset = point.y - location.y;
    1.38 +        DLog("Warp to (%g, %g), offsetting next movement event by (%i, %i)", point.x, point.y, driverdata->deltaXOffset, driverdata->deltaYOffset);
    1.39      }
    1.40  
    1.41      /* According to the docs, this was deprecated in 10.6, but it's still
    1.42 @@ -238,8 +256,10 @@
    1.43      CGError result;
    1.44  
    1.45      if (enabled) {
    1.46 +        DLog("Turning on.");
    1.47          result = CGAssociateMouseAndMouseCursorPosition(NO);
    1.48      } else {
    1.49 +        DLog("Turning off.");
    1.50          result = CGAssociateMouseAndMouseCursorPosition(YES);
    1.51      }
    1.52      if (result != kCGErrorSuccess) {
    1.53 @@ -272,18 +292,42 @@
    1.54  {
    1.55      SDL_Mouse *mouse = SDL_GetMouse();
    1.56  
    1.57 -    if (mouse->relative_mode &&
    1.58 -        ([event type] == NSMouseMoved ||
    1.59 -         [event type] == NSLeftMouseDragged ||
    1.60 -         [event type] == NSRightMouseDragged ||
    1.61 -         [event type] == NSOtherMouseDragged)) {
    1.62 -        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
    1.63 -        float x = [event deltaX] + driverdata->deltaXOffset;
    1.64 -        float y = [event deltaY] + driverdata->deltaYOffset;
    1.65 -        driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
    1.66 +    /* Non-relative movement is handled in -[Cocoa_WindowListener mouseMoved:] */
    1.67 +    if (!mouse->relative_mode) {
    1.68 +        return;
    1.69 +    }
    1.70  
    1.71 -        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
    1.72 +    switch ([event type])
    1.73 +    {
    1.74 +        case NSMouseMoved:
    1.75 +        case NSLeftMouseDragged:
    1.76 +        case NSRightMouseDragged:
    1.77 +        case NSOtherMouseDragged:
    1.78 +            break;
    1.79 +
    1.80 +        default:
    1.81 +            /* Ignore any other events. */
    1.82 +            return;
    1.83      }
    1.84 +
    1.85 +    /* Ignore events that aren't inside the client area (i.e. title bar.) */
    1.86 +    if ([event window]) {
    1.87 +        NSRect windowRect = [[[event window] contentView] frame];
    1.88 +        if (!NSPointInRect([event locationInWindow], windowRect)) {
    1.89 +            return;
    1.90 +        }
    1.91 +    }
    1.92 +
    1.93 +    SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
    1.94 +    float x = [event deltaX] + driverdata->deltaXOffset;
    1.95 +    float y = [event deltaY] + driverdata->deltaYOffset;
    1.96 +    driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
    1.97 +
    1.98 +    if (driverdata->deltaYOffset > 0 || driverdata->deltaXOffset > 0) {
    1.99 +        DLog("Relative move was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], x, y);
   1.100 +    }
   1.101 +
   1.102 +    SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
   1.103  }
   1.104  
   1.105  void
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.h	Tue Feb 25 17:25:49 2014 -0800
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Tue Feb 25 17:27:41 2014 -0800
     2.3 @@ -43,6 +43,8 @@
     2.4      BOOL isFullscreenSpace;
     2.5      BOOL inFullscreenTransition;
     2.6      PendingWindowOperation pendingWindowOperation;
     2.7 +    BOOL isMoving;
     2.8 +    int pendingWindowWarpX, pendingWindowWarpY;
     2.9  }
    2.10  
    2.11  -(void) listen:(SDL_WindowData *) data;
    2.12 @@ -54,6 +56,10 @@
    2.13  -(void) addPendingWindowOperation:(PendingWindowOperation) operation;
    2.14  -(void) close;
    2.15  
    2.16 +-(BOOL) isMoving;
    2.17 +-(void) setPendingMoveX:(int)x Y:(int)y;
    2.18 +-(void) windowDidFinishMoving;
    2.19 +
    2.20  /* Window delegate functionality */
    2.21  -(BOOL) windowShouldClose:(id) sender;
    2.22  -(void) windowDidExpose:(NSNotification *) aNotification;
    2.23 @@ -105,6 +111,7 @@
    2.24      NSWindow *nswindow;
    2.25      NSMutableArray *nscontexts;
    2.26      SDL_bool created;
    2.27 +    SDL_bool inWindowMove;
    2.28      Cocoa_WindowListener *listener;
    2.29      struct SDL_VideoData *videodata;
    2.30  };
     3.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Tue Feb 25 17:25:49 2014 -0800
     3.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Tue Feb 25 17:27:41 2014 -0800
     3.3 @@ -39,6 +39,44 @@
     3.4  #include "SDL_cocoamouse.h"
     3.5  #include "SDL_cocoaopengl.h"
     3.6  
     3.7 +@interface SDLWindow : NSWindow
     3.8 +/* These are needed for borderless/fullscreen windows */
     3.9 +- (BOOL)canBecomeKeyWindow;
    3.10 +- (BOOL)canBecomeMainWindow;
    3.11 +- (void)sendEvent:(NSEvent *)event;
    3.12 +@end
    3.13 +
    3.14 +@implementation SDLWindow
    3.15 +- (BOOL)canBecomeKeyWindow
    3.16 +{
    3.17 +    return YES;
    3.18 +}
    3.19 +
    3.20 +- (BOOL)canBecomeMainWindow
    3.21 +{
    3.22 +    return YES;
    3.23 +}
    3.24 +
    3.25 +- (void)sendEvent:(NSEvent *)event
    3.26 +{
    3.27 +  [super sendEvent:event];
    3.28 +
    3.29 +  if ([event type] != NSLeftMouseUp) {
    3.30 +      return;
    3.31 +  }
    3.32 +
    3.33 +  id delegate = [self delegate];
    3.34 +  if (![delegate isKindOfClass:[Cocoa_WindowListener class]]) {
    3.35 +      return;
    3.36 +  }
    3.37 +
    3.38 +  if ([delegate isMoving]) {
    3.39 +      [delegate windowDidFinishMoving];
    3.40 +  }
    3.41 +}
    3.42 +@end
    3.43 +
    3.44 +
    3.45  static Uint32 s_moveHack;
    3.46  
    3.47  static void ConvertNSRect(NSRect *r)
    3.48 @@ -130,6 +168,7 @@
    3.49      isFullscreenSpace = NO;
    3.50      inFullscreenTransition = NO;
    3.51      pendingWindowOperation = PENDING_OPERATION_NONE;
    3.52 +    isMoving = NO;
    3.53  
    3.54      center = [NSNotificationCenter defaultCenter];
    3.55  
    3.56 @@ -315,6 +354,34 @@
    3.57      }
    3.58  }
    3.59  
    3.60 +- (BOOL)isMoving
    3.61 +{
    3.62 +    return isMoving;
    3.63 +}
    3.64 +
    3.65 +-(void) setPendingMoveX:(int)x Y:(int)y
    3.66 +{
    3.67 +    pendingWindowWarpX = x;
    3.68 +    pendingWindowWarpY = y;
    3.69 +}
    3.70 +
    3.71 +- (void)windowDidFinishMoving
    3.72 +{
    3.73 +    if ([self isMoving])
    3.74 +    {
    3.75 +        isMoving = NO;
    3.76 +
    3.77 +        SDL_Mouse *mouse = SDL_GetMouse();
    3.78 +        if (pendingWindowWarpX >= 0 && pendingWindowWarpY >= 0) {
    3.79 +            mouse->WarpMouse(_data->window, pendingWindowWarpX, pendingWindowWarpY);
    3.80 +            pendingWindowWarpX = pendingWindowWarpY = -1;
    3.81 +        }
    3.82 +        if (mouse->relative_mode && SDL_GetMouseFocus() == _data->window) {
    3.83 +            mouse->SetRelativeMouseMode(SDL_TRUE);
    3.84 +        }
    3.85 +    }
    3.86 +}
    3.87 +
    3.88  - (BOOL)windowShouldClose:(id)sender
    3.89  {
    3.90      SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
    3.91 @@ -326,6 +393,14 @@
    3.92      SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
    3.93  }
    3.94  
    3.95 +- (void)windowWillMove:(NSNotification *)aNotification
    3.96 +{
    3.97 +    if ([_data->nswindow isKindOfClass:[SDLWindow class]]) {
    3.98 +        pendingWindowWarpX = pendingWindowWarpY = -1;
    3.99 +        isMoving = YES;
   3.100 +    }
   3.101 +}
   3.102 +
   3.103  - (void)windowDidMove:(NSNotification *)aNotification
   3.104  {
   3.105      int x, y;
   3.106 @@ -407,6 +482,9 @@
   3.107  {
   3.108      SDL_Window *window = _data->window;
   3.109      SDL_Mouse *mouse = SDL_GetMouse();
   3.110 +    if (mouse->relative_mode && ![self isMoving]) {
   3.111 +        mouse->SetRelativeMouseMode(SDL_TRUE);
   3.112 +    }
   3.113  
   3.114      /* We're going to get keyboard events, since we're key. */
   3.115      SDL_SetKeyboardFocus(window);
   3.116 @@ -431,6 +509,11 @@
   3.117  
   3.118  - (void)windowDidResignKey:(NSNotification *)aNotification
   3.119  {
   3.120 +    SDL_Mouse *mouse = SDL_GetMouse();
   3.121 +    if (mouse->relative_mode) {
   3.122 +        mouse->SetRelativeMouseMode(SDL_FALSE);
   3.123 +    }
   3.124 +
   3.125      /* Some other window will get mouse events, since we're not key. */
   3.126      if (SDL_GetMouseFocus() == _data->window) {
   3.127          SDL_SetMouseFocus(NULL);
   3.128 @@ -624,8 +707,6 @@
   3.129  
   3.130      if (x < 0 || x >= window->w || y < 0 || y >= window->h) {
   3.131          if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
   3.132 -            CGPoint cgpoint;
   3.133 -
   3.134              if (x < 0) {
   3.135                  x = 0;
   3.136              } else if (x >= window->w) {
   3.137 @@ -638,6 +719,8 @@
   3.138              }
   3.139  
   3.140  #if !SDL_MAC_NO_SANDBOX
   3.141 +            CGPoint cgpoint;
   3.142 +
   3.143              /* When SDL_MAC_NO_SANDBOX is set, this is handled by
   3.144               * SDL_cocoamousetap.m.
   3.145               */
   3.146 @@ -754,24 +837,6 @@
   3.147  
   3.148  @end
   3.149  
   3.150 -@interface SDLWindow : NSWindow
   3.151 -/* These are needed for borderless/fullscreen windows */
   3.152 -- (BOOL)canBecomeKeyWindow;
   3.153 -- (BOOL)canBecomeMainWindow;
   3.154 -@end
   3.155 -
   3.156 -@implementation SDLWindow
   3.157 -- (BOOL)canBecomeKeyWindow
   3.158 -{
   3.159 -    return YES;
   3.160 -}
   3.161 -
   3.162 -- (BOOL)canBecomeMainWindow
   3.163 -{
   3.164 -    return YES;
   3.165 -}
   3.166 -@end
   3.167 -
   3.168  @interface SDLView : NSView
   3.169  
   3.170  /* The default implementation doesn't pass rightMouseDown to responder chain */
   3.171 @@ -1338,7 +1403,8 @@
   3.172  Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
   3.173  {
   3.174      /* Move the cursor to the nearest point in the window */
   3.175 -    if (grabbed) {
   3.176 +    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   3.177 +    if (grabbed && data && ![data->listener isMoving]) {
   3.178          int x, y;
   3.179          CGPoint cgpoint;
   3.180