src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Dec 2009 08:33:39 +0000
changeset 3517 e7eec78e4b92
parent 2940 b93965a16fe0
child 3525 455a6c47d2c6
permissions -rw-r--r--
Fixed mouse events for fullscreen windows on Mac OS X
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_events.h"
    25 #include "SDL_cocoavideo.h"
    26 
    27 #include "../../events/SDL_mouse_c.h"
    28 
    29 void
    30 Cocoa_InitMouse(_THIS)
    31 {
    32     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    33     SDL_Mouse mouse;
    34 
    35     SDL_zero(mouse);
    36     data->mouse = SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
    37 }
    38 
    39 static int
    40 ConvertMouseButtonToSDL(int button)
    41 {
    42     switch (button)
    43     {
    44         case 0:
    45             return(SDL_BUTTON_LEFT);   /* 1 */
    46         case 1:
    47             return(SDL_BUTTON_RIGHT);  /* 3 */
    48         case 2:
    49             return(SDL_BUTTON_MIDDLE); /* 2 */
    50     }
    51     return button;
    52 }
    53 
    54 void
    55 Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
    56 {
    57     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
    58     SDL_Mouse *mouse = SDL_GetMouse(data->mouse);
    59     int i;
    60     NSPoint point;
    61     SDL_Window *window;
    62 
    63     /* See if there are any fullscreen windows that might handle this event */
    64     window = NULL;
    65     for (i = 0; i < _this->num_displays; ++i) {
    66         SDL_VideoDisplay *display = &_this->displays[i];
    67         SDL_Window *candidate = display->fullscreen_window;
    68 
    69         if (candidate) {
    70             SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
    71             NSRect rect = CGDisplayBounds(displaydata->display);
    72 
    73             point = [NSEvent mouseLocation];
    74             point.x = point.x - rect.origin.x;
    75             point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - rect.origin.y;
    76             if (point.x < 0 || point.x >= candidate->w ||
    77                 point.y < 0 || point.y >= candidate->h) {
    78                 /* The mouse is out of this fullscreen display */
    79                 if (mouse->focus == candidate->id) {
    80                     SDL_SetMouseFocus(data->mouse, 0);
    81                 }
    82             } else {
    83                 /* This is it! */
    84                 window = candidate;
    85                 break;
    86             }
    87         }
    88     }
    89     if (!window) {
    90         return;
    91     }
    92 
    93     /* Set the focus appropriately */
    94     if (mouse->focus != window->id) {
    95         SDL_SetMouseFocus(data->mouse, window->id);
    96     }
    97 
    98     switch ([event type]) {
    99     case NSLeftMouseDown:
   100     case NSOtherMouseDown:
   101     case NSRightMouseDown:
   102         SDL_SendMouseButton(data->mouse, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
   103         break;
   104     case NSLeftMouseUp:
   105     case NSOtherMouseUp:
   106     case NSRightMouseUp:
   107         SDL_SendMouseButton(data->mouse, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
   108         break;
   109     case NSLeftMouseDragged:
   110     case NSRightMouseDragged:
   111     case NSOtherMouseDragged: /* usually middle mouse dragged */
   112     case NSMouseMoved:
   113         SDL_SendMouseMotion(data->mouse, 0, (int)point.x, (int)point.y, 0);
   114         break;
   115     default: /* just to avoid compiler warnings */
   116         break;
   117     }
   118 }
   119 
   120 void
   121 Cocoa_QuitMouse(_THIS)
   122 {
   123     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   124 
   125     SDL_DelMouse(data->mouse);
   126 }
   127 
   128 /* vi: set ts=4 sw=4 expandtab: */