src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 09 May 2010 20:47:22 -0700
changeset 4465 3e69e077cb95
parent 3685 64ce267332c6
child 4484 9322f7db8603
permissions -rw-r--r--
Removed multi-mouse / multi-keyboard support in anticipation of a real multi-mouse and multi-touch API.

Plus, this lets me start implementing cursor support.
     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 }
    33 
    34 static int
    35 ConvertMouseButtonToSDL(int button)
    36 {
    37     switch (button)
    38     {
    39         case 0:
    40             return(SDL_BUTTON_LEFT);   /* 1 */
    41         case 1:
    42             return(SDL_BUTTON_RIGHT);  /* 3 */
    43         case 2:
    44             return(SDL_BUTTON_MIDDLE); /* 2 */
    45     }
    46     return button;
    47 }
    48 
    49 void
    50 Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
    51 {
    52     int i;
    53     NSPoint point = { 0, 0 };
    54     SDL_Window *window;
    55 
    56     /* See if there are any fullscreen windows that might handle this event */
    57     window = NULL;
    58     for (i = 0; i < _this->num_displays; ++i) {
    59         SDL_VideoDisplay *display = &_this->displays[i];
    60         SDL_Window *candidate = display->fullscreen_window;
    61 
    62         if (candidate) {
    63             SDL_Rect bounds;
    64 
    65             Cocoa_GetDisplayBounds(_this, display, &bounds);
    66             point = [NSEvent mouseLocation];
    67             point.x = point.x - bounds.x;
    68             point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - bounds.y;
    69             if ((point.x >= 0 && point.x < candidate->w) ||
    70                 (point.y >= 0 && point.y < candidate->h)) {
    71                 /* This is it! */
    72                 window = candidate;
    73                 break;
    74             }
    75         }
    76     }
    77 
    78     /* Set the focus appropriately */
    79     SDL_SetMouseFocus(window);
    80 
    81     if (window) {
    82         return;
    83     }
    84 
    85     switch ([event type]) {
    86     case NSLeftMouseDown:
    87     case NSOtherMouseDown:
    88     case NSRightMouseDown:
    89         SDL_SendMouseButton(SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
    90         break;
    91     case NSLeftMouseUp:
    92     case NSOtherMouseUp:
    93     case NSRightMouseUp:
    94         SDL_SendMouseButton(SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
    95         break;
    96     case NSLeftMouseDragged:
    97     case NSRightMouseDragged:
    98     case NSOtherMouseDragged: /* usually middle mouse dragged */
    99     case NSMouseMoved:
   100         SDL_SendMouseMotion(0, (int)point.x, (int)point.y);
   101         break;
   102     default: /* just to avoid compiler warnings */
   103         break;
   104     }
   105 }
   106 
   107 void
   108 Cocoa_QuitMouse(_THIS)
   109 {
   110 }
   111 
   112 /* vi: set ts=4 sw=4 expandtab: */