Fixed mouse events for fullscreen windows on Mac OS X
authorSam Lantinga <slouken@libsdl.org>
Thu, 03 Dec 2009 08:33:39 +0000
changeset 3517e7eec78e4b92
parent 3516 72e70a8c30d5
child 3518 abefdc334970
Fixed mouse events for fullscreen windows on Mac OS X
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoaevents.m
src/video/cocoa/SDL_cocoamouse.h
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/SDL_sysvideo.h	Thu Dec 03 05:05:26 2009 +0000
     1.2 +++ b/src/video/SDL_sysvideo.h	Thu Dec 03 08:33:39 2009 +0000
     1.3 @@ -171,6 +171,7 @@
     1.4  
     1.5      int num_windows;
     1.6      SDL_Window *windows;
     1.7 +    SDL_Window *fullscreen_window;
     1.8  
     1.9      SDL_Renderer *current_renderer;
    1.10  
     2.1 --- a/src/video/SDL_video.c	Thu Dec 03 05:05:26 2009 +0000
     2.2 +++ b/src/video/SDL_video.c	Thu Dec 03 08:33:39 2009 +0000
     2.3 @@ -768,6 +768,7 @@
     2.4              SDL_DisplayMode fullscreen_mode;
     2.5              if (SDL_GetWindowDisplayMode(window->id, &fullscreen_mode) == 0) {
     2.6                  SDL_SetDisplayModeForDisplay(display, &fullscreen_mode);
     2.7 +                display->fullscreen_window = window;
     2.8                  return;
     2.9              }
    2.10          }
    2.11 @@ -775,6 +776,7 @@
    2.12  
    2.13      /* Nope, restore the desktop mode */
    2.14      SDL_SetDisplayModeForDisplay(display, NULL);
    2.15 +    display->fullscreen_window = NULL;
    2.16  }
    2.17  
    2.18  int
     3.1 --- a/src/video/cocoa/SDL_cocoaevents.m	Thu Dec 03 05:05:26 2009 +0000
     3.2 +++ b/src/video/cocoa/SDL_cocoaevents.m	Thu Dec 03 08:33:39 2009 +0000
     3.3 @@ -187,6 +187,20 @@
     3.4              break;
     3.5          }
     3.6          switch ([event type]) {
     3.7 +        case NSLeftMouseDown:
     3.8 +        case NSOtherMouseDown:
     3.9 +        case NSRightMouseDown:
    3.10 +        case NSLeftMouseUp:
    3.11 +        case NSOtherMouseUp:
    3.12 +        case NSRightMouseUp:
    3.13 +        case NSLeftMouseDragged:
    3.14 +        case NSRightMouseDragged:
    3.15 +        case NSOtherMouseDragged: /* usually middle mouse dragged */
    3.16 +        case NSMouseMoved:
    3.17 +            Cocoa_HandleMouseEvent(_this, event);
    3.18 +            /* Pass through to NSApp to make sure everything stays in sync */
    3.19 +            [NSApp sendEvent:event];
    3.20 +            break;
    3.21          case NSKeyDown:
    3.22          case NSKeyUp:
    3.23          case NSFlagsChanged:
     4.1 --- a/src/video/cocoa/SDL_cocoamouse.h	Thu Dec 03 05:05:26 2009 +0000
     4.2 +++ b/src/video/cocoa/SDL_cocoamouse.h	Thu Dec 03 08:33:39 2009 +0000
     4.3 @@ -25,6 +25,7 @@
     4.4  #define _SDL_cocoamouse_h
     4.5  
     4.6  extern void Cocoa_InitMouse(_THIS);
     4.7 +extern void Cocoa_HandleMouseEvent(_THIS, NSEvent * event);
     4.8  extern void Cocoa_QuitMouse(_THIS);
     4.9  
    4.10  #endif /* _SDL_cocoamouse_h */
     5.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Thu Dec 03 05:05:26 2009 +0000
     5.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Thu Dec 03 08:33:39 2009 +0000
     5.3 @@ -21,6 +21,7 @@
     5.4  */
     5.5  #include "SDL_config.h"
     5.6  
     5.7 +#include "SDL_events.h"
     5.8  #include "SDL_cocoavideo.h"
     5.9  
    5.10  #include "../../events/SDL_mouse_c.h"
    5.11 @@ -35,6 +36,87 @@
    5.12      data->mouse = SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
    5.13  }
    5.14  
    5.15 +static int
    5.16 +ConvertMouseButtonToSDL(int button)
    5.17 +{
    5.18 +    switch (button)
    5.19 +    {
    5.20 +        case 0:
    5.21 +            return(SDL_BUTTON_LEFT);   /* 1 */
    5.22 +        case 1:
    5.23 +            return(SDL_BUTTON_RIGHT);  /* 3 */
    5.24 +        case 2:
    5.25 +            return(SDL_BUTTON_MIDDLE); /* 2 */
    5.26 +    }
    5.27 +    return button;
    5.28 +}
    5.29 +
    5.30 +void
    5.31 +Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
    5.32 +{
    5.33 +    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    5.34 +    SDL_Mouse *mouse = SDL_GetMouse(data->mouse);
    5.35 +    int i;
    5.36 +    NSPoint point;
    5.37 +    SDL_Window *window;
    5.38 +
    5.39 +    /* See if there are any fullscreen windows that might handle this event */
    5.40 +    window = NULL;
    5.41 +    for (i = 0; i < _this->num_displays; ++i) {
    5.42 +        SDL_VideoDisplay *display = &_this->displays[i];
    5.43 +        SDL_Window *candidate = display->fullscreen_window;
    5.44 +
    5.45 +        if (candidate) {
    5.46 +            SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
    5.47 +            NSRect rect = CGDisplayBounds(displaydata->display);
    5.48 +
    5.49 +            point = [NSEvent mouseLocation];
    5.50 +            point.x = point.x - rect.origin.x;
    5.51 +            point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - rect.origin.y;
    5.52 +            if (point.x < 0 || point.x >= candidate->w ||
    5.53 +                point.y < 0 || point.y >= candidate->h) {
    5.54 +                /* The mouse is out of this fullscreen display */
    5.55 +                if (mouse->focus == candidate->id) {
    5.56 +                    SDL_SetMouseFocus(data->mouse, 0);
    5.57 +                }
    5.58 +            } else {
    5.59 +                /* This is it! */
    5.60 +                window = candidate;
    5.61 +                break;
    5.62 +            }
    5.63 +        }
    5.64 +    }
    5.65 +    if (!window) {
    5.66 +        return;
    5.67 +    }
    5.68 +
    5.69 +    /* Set the focus appropriately */
    5.70 +    if (mouse->focus != window->id) {
    5.71 +        SDL_SetMouseFocus(data->mouse, window->id);
    5.72 +    }
    5.73 +
    5.74 +    switch ([event type]) {
    5.75 +    case NSLeftMouseDown:
    5.76 +    case NSOtherMouseDown:
    5.77 +    case NSRightMouseDown:
    5.78 +        SDL_SendMouseButton(data->mouse, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
    5.79 +        break;
    5.80 +    case NSLeftMouseUp:
    5.81 +    case NSOtherMouseUp:
    5.82 +    case NSRightMouseUp:
    5.83 +        SDL_SendMouseButton(data->mouse, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
    5.84 +        break;
    5.85 +    case NSLeftMouseDragged:
    5.86 +    case NSRightMouseDragged:
    5.87 +    case NSOtherMouseDragged: /* usually middle mouse dragged */
    5.88 +    case NSMouseMoved:
    5.89 +        SDL_SendMouseMotion(data->mouse, 0, (int)point.x, (int)point.y, 0);
    5.90 +        break;
    5.91 +    default: /* just to avoid compiler warnings */
    5.92 +        break;
    5.93 +    }
    5.94 +}
    5.95 +
    5.96  void
    5.97  Cocoa_QuitMouse(_THIS)
    5.98  {
     6.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Thu Dec 03 05:05:26 2009 +0000
     6.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Thu Dec 03 08:33:39 2009 +0000
     6.3 @@ -235,17 +235,8 @@
     6.4  
     6.5      index = _data->videodata->mouse;
     6.6      mouse = SDL_GetMouse(index);
     6.7 -
     6.8 -    point = [NSEvent mouseLocation];
     6.9 -    if ( (window->flags & SDL_WINDOW_FULLSCREEN) ) {
    6.10 -        NSRect rect = CGDisplayBounds(_data->display);
    6.11 -
    6.12 -        point.x = point.x - rect.origin.x;
    6.13 -        point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - rect.origin.y;
    6.14 -    } else {
    6.15 -        point.x = point.x - window->x;
    6.16 -        point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - window->y;
    6.17 -    }
    6.18 +    point = [theEvent locationInWindow];
    6.19 +    point.y = window->h - point.y;
    6.20      if ( point.x < 0 || point.x >= window->w ||
    6.21           point.y < 0 || point.y >= window->h ) {
    6.22          if (mouse->focus != 0) {