Cocoa: deal with mouse focus when warping the cursor from outside a window.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 31 May 2015 21:43:36 -0400
changeset 96921982dc994254
parent 9691 c0ca56ba6749
child 9693 e8d9c63536f4
Cocoa: deal with mouse focus when warping the cursor from outside a window.

Otherwise, you might not get appropriate mouse enter/leave events.

Better fix for Bugzilla #2984.
src/video/cocoa/SDL_cocoamouse.m
     1.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Sun May 31 13:58:36 2015 -0400
     1.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Sun May 31 21:43:36 2015 -0400
     1.3 @@ -195,6 +195,21 @@
     1.4      return 0;
     1.5  }}
     1.6  
     1.7 +static SDL_Window *
     1.8 +SDL_FindWindowAtPoint(const int x, const int y)
     1.9 +{
    1.10 +    const SDL_Point pt = { x, y };
    1.11 +    SDL_Window *i;
    1.12 +    for (i = SDL_GetVideoDevice()->windows; i; i = i->next) {
    1.13 +        const SDL_Rect r = { i->x, i->y, i->w, i->h };
    1.14 +        if (SDL_PointInRect(&pt, &r)) {
    1.15 +            return i;
    1.16 +        }
    1.17 +    }
    1.18 +
    1.19 +    return NULL;
    1.20 +}
    1.21 +
    1.22  static void
    1.23  Cocoa_WarpMouseGlobal(int x, int y)
    1.24  {
    1.25 @@ -207,7 +222,7 @@
    1.26              return;
    1.27          }
    1.28      }
    1.29 -    CGPoint point = CGPointMake((float)x, (float)y);
    1.30 +    const CGPoint point = CGPointMake((float)x, (float)y);
    1.31  
    1.32      Cocoa_HandleMouseWarp(point.x, point.y);
    1.33  
    1.34 @@ -219,21 +234,22 @@
    1.35      CGWarpMouseCursorPosition(point);
    1.36      CGSetLocalEventsSuppressionInterval(0.25);
    1.37  
    1.38 -    if (!mouse->relative_mode && mouse->focus) {
    1.39 -        /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
    1.40 -         * other implementations' APIs.
    1.41 -         */
    1.42 -        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x - mouse->focus->x, y - mouse->focus->y);
    1.43 +    /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
    1.44 +     * other implementations' APIs. Send what's appropriate.
    1.45 +     */
    1.46 +    if (!mouse->relative_mode) {
    1.47 +        SDL_Window *win = SDL_FindWindowAtPoint(x, y);
    1.48 +        SDL_SetMouseFocus(win);
    1.49 +        if (win) {
    1.50 +            SDL_assert(win == mouse->focus);
    1.51 +            SDL_SendMouseMotion(win, mouse->mouseID, 0, x - win->x, y - win->y);
    1.52 +        }
    1.53      }
    1.54  }
    1.55  
    1.56  static void
    1.57  Cocoa_WarpMouse(SDL_Window * window, int x, int y)
    1.58  {
    1.59 -    /* pretend we have the mouse focus, even if we don't, so
    1.60 -        Cocoa_WarpMouseGlobal() will properly fake a mouse motion event. */
    1.61 -    SDL_Mouse *mouse = SDL_GetMouse();
    1.62 -    mouse->focus = window;
    1.63      Cocoa_WarpMouseGlobal(x + window->x, y + window->y);
    1.64  }
    1.65