src/events/SDL_mouse.c
changeset 8927 be64f5daf64b
parent 8783 400f1d2b9e52
child 8944 045aac36daac
     1.1 --- a/src/events/SDL_mouse.c	Sat May 24 01:27:19 2014 -0400
     1.2 +++ b/src/events/SDL_mouse.c	Sat May 24 01:30:37 2014 -0400
     1.3 @@ -140,30 +140,17 @@
     1.4  SDL_UpdateMouseFocus(SDL_Window * window, int x, int y, Uint32 buttonstate)
     1.5  {
     1.6      SDL_Mouse *mouse = SDL_GetMouse();
     1.7 -    int w, h;
     1.8 -    SDL_bool inWindow;
     1.9 +    SDL_bool inWindow = SDL_TRUE;
    1.10  
    1.11 -    SDL_GetWindowSize(window, &w, &h);
    1.12 -    if (x < 0 || y < 0 || x >= w || y >= h) {
    1.13 -        inWindow = SDL_FALSE;
    1.14 -    } else {
    1.15 -        inWindow = SDL_TRUE;
    1.16 +    if ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0) {
    1.17 +        int w, h;
    1.18 +        SDL_GetWindowSize(window, &w, &h);
    1.19 +        if (x < 0 || y < 0 || x >= w || y >= h) {
    1.20 +            inWindow = SDL_FALSE;
    1.21 +        }
    1.22      }
    1.23  
    1.24 -/* Linux doesn't give you mouse events outside your window unless you grab
    1.25 -   the pointer.
    1.26 -
    1.27 -   Windows doesn't give you mouse events outside your window unless you call
    1.28 -   SetCapture().
    1.29 -
    1.30 -   Both of these are slightly scary changes, so for now we'll punt and if the
    1.31 -   mouse leaves the window you'll lose mouse focus and reset button state.
    1.32 -*/
    1.33 -#ifdef SUPPORT_DRAG_OUTSIDE_WINDOW
    1.34 -    if (!inWindow && !buttonstate) {
    1.35 -#else
    1.36      if (!inWindow) {
    1.37 -#endif
    1.38          if (window == mouse->focus) {
    1.39  #ifdef DEBUG_MOUSE
    1.40              printf("Mouse left window, synthesizing move & focus lost event\n");
    1.41 @@ -204,7 +191,6 @@
    1.42      int posted;
    1.43      int xrel;
    1.44      int yrel;
    1.45 -    int x_max = 0, y_max = 0;
    1.46  
    1.47      if (mouse->relative_mode_warp) {
    1.48          int center_x = 0, center_y = 0;
    1.49 @@ -246,24 +232,29 @@
    1.50          mouse->y += yrel;
    1.51      }
    1.52  
    1.53 -    /* !!! FIXME: shouldn't this be (window) instead of (mouse->focus)? */
    1.54 -    SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
    1.55 -    --x_max;
    1.56 -    --y_max;
    1.57 +    /* make sure that the pointers find themselves inside the windows,
    1.58 +       unless we have the mouse captured. */
    1.59 +    if ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0) {
    1.60 +        int x_max = 0, y_max = 0;
    1.61  
    1.62 -    /* make sure that the pointers find themselves inside the windows */
    1.63 -    if (mouse->x > x_max) {
    1.64 -        mouse->x = x_max;
    1.65 -    }
    1.66 -    if (mouse->x < 0) {
    1.67 -        mouse->x = 0;
    1.68 -    }
    1.69 +        // !!! FIXME: shouldn't this be (window) instead of (mouse->focus)?
    1.70 +        SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
    1.71 +        --x_max;
    1.72 +        --y_max;
    1.73  
    1.74 -    if (mouse->y > y_max) {
    1.75 -        mouse->y = y_max;
    1.76 -    }
    1.77 -    if (mouse->y < 0) {
    1.78 -        mouse->y = 0;
    1.79 +        if (mouse->x > x_max) {
    1.80 +            mouse->x = x_max;
    1.81 +        }
    1.82 +        if (mouse->x < 0) {
    1.83 +            mouse->x = 0;
    1.84 +        }
    1.85 +
    1.86 +        if (mouse->y > y_max) {
    1.87 +            mouse->y = y_max;
    1.88 +        }
    1.89 +        if (mouse->y < 0) {
    1.90 +            mouse->y = 0;
    1.91 +        }
    1.92      }
    1.93  
    1.94      mouse->xdelta += xrel;
    1.95 @@ -426,6 +417,7 @@
    1.96      SDL_Cursor *cursor, *next;
    1.97      SDL_Mouse *mouse = SDL_GetMouse();
    1.98  
    1.99 +    SDL_CaptureMouse(SDL_FALSE);
   1.100      SDL_SetRelativeMouseMode(SDL_FALSE);
   1.101      SDL_ShowCursor(1);
   1.102  
   1.103 @@ -572,6 +564,42 @@
   1.104      return mouse->relative_mode;
   1.105  }
   1.106  
   1.107 +int
   1.108 +SDL_CaptureMouse(SDL_bool enabled)
   1.109 +{
   1.110 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.111 +    SDL_Window *focusWindow;
   1.112 +    SDL_bool isCaptured;
   1.113 +
   1.114 +    if (!mouse->CaptureMouse) {
   1.115 +        return SDL_Unsupported();
   1.116 +    }
   1.117 +
   1.118 +    focusWindow = SDL_GetKeyboardFocus();
   1.119 +
   1.120 +    isCaptured = focusWindow && (focusWindow->flags & SDL_WINDOW_MOUSE_CAPTURE);
   1.121 +    if (isCaptured == enabled) {
   1.122 +        return 0;  /* already done! */
   1.123 +    }
   1.124 +
   1.125 +    if (enabled) {
   1.126 +        if (!focusWindow) {
   1.127 +            return SDL_SetError("No window has focus");
   1.128 +        } else if (mouse->CaptureMouse(focusWindow) == -1) {
   1.129 +            return -1;  /* CaptureMouse() should call SetError */
   1.130 +        }
   1.131 +        focusWindow->flags |= SDL_WINDOW_MOUSE_CAPTURE;
   1.132 +    } else {
   1.133 +        if (mouse->CaptureMouse(NULL) == -1) {
   1.134 +            return -1;  /* CaptureMouse() should call SetError */
   1.135 +        }
   1.136 +        focusWindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
   1.137 +    }
   1.138 +
   1.139 +    return 0;
   1.140 +}
   1.141 +
   1.142 +
   1.143  SDL_Cursor *
   1.144  SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
   1.145                   int w, int h, int hot_x, int hot_y)