Fixed the code so we receive Cocoa touch events
authorSam Lantinga <slouken@libsdl.org>
Wed, 28 Jul 2010 23:32:13 -0700
changeset 4673c17ac64abb70
parent 4672 013b0ea263dd
child 4674 89d5e2201b00
Fixed the code so we receive Cocoa touch events
src/video/cocoa/SDL_cocoakeyboard.m
src/video/cocoa/SDL_cocoatouch.h
src/video/cocoa/SDL_cocoawindow.h
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoakeyboard.m	Tue Jul 27 18:14:09 2010 +0400
     1.2 +++ b/src/video/cocoa/SDL_cocoakeyboard.m	Wed Jul 28 23:32:13 2010 -0700
     1.3 @@ -26,17 +26,11 @@
     1.4  #include "../../events/SDL_keyboard_c.h"
     1.5  #include "../../events/scancodes_darwin.h"
     1.6  
     1.7 -//Touch Code
     1.8 -#include "../../events/SDL_touch_c.h"
     1.9 -#include "SDL_cocoatouch.h"
    1.10 -
    1.11  #include <Carbon/Carbon.h>
    1.12  
    1.13  //#define DEBUG_IME NSLog
    1.14  #define DEBUG_IME
    1.15  
    1.16 -#define DEBUG_TOUCH NSLog
    1.17 -
    1.18  #ifndef NX_DEVICERCTLKEYMASK
    1.19      #define NX_DEVICELCTLKEYMASK    0x00000001
    1.20  #endif
    1.21 @@ -71,7 +65,6 @@
    1.22  }
    1.23  - (void) doCommandBySelector:(SEL)myselector;
    1.24  - (void) setInputRect:(SDL_Rect *) rect;
    1.25 -- (void) handleTouches:(cocoaTouchType)type WithEvent:(NSEvent*) event;
    1.26  @end
    1.27  
    1.28  @implementation SDLTranslatorResponder
    1.29 @@ -198,83 +191,6 @@
    1.30      return [NSArray array];
    1.31  }
    1.32  
    1.33 -// Touch Code Begins -----------
    1.34 -
    1.35 -- (id)initWithFrame:(CGRect)frame {
    1.36 -  if (self = [super initWithFrame:frame]) {
    1.37 -    [self setAcceptsTouchEvents:YES];
    1.38 -    [self setWantsRestingTouches:YES];
    1.39 -    DEBUG_TOUCH(@"Initializing Cocoa Touch System....");
    1.40 -    
    1.41 -  }
    1.42 -  return self;
    1.43 -}
    1.44 -
    1.45 -//Not an API function
    1.46 -- (void)handleTouches:(cocoaTouchType)type WithEvent:(NSEvent *)event {
    1.47 -  NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:self];
    1.48 -
    1.49 -  NSEnumerator *enumerator = [touches objectEnumerator];
    1.50 -  NSTouch *touch = (NSTouch*)[enumerator nextObject];
    1.51 -  while (touch) {
    1.52 -    long touchId = (long)[touch device];
    1.53 -    if(!SDL_GetTouchIndex(touchId)) {
    1.54 -      if(Cocoa_AddTouch(touch) < 0) continue;
    1.55 -    } 
    1.56 -    float x = [touch normalizedPosition].x;
    1.57 -    float y = [touch normalizedPosition].y;
    1.58 -    long fingerId = (long)[touch identity];
    1.59 -    switch (type) {
    1.60 -    case COCOA_TOUCH_DOWN:
    1.61 -      SDL_SendFingerDown(touchId,fingerId,
    1.62 -			 SDL_TRUE,x,y,1);
    1.63 -      break;
    1.64 -    case COCOA_TOUCH_UP:
    1.65 -    case COCOA_TOUCH_CANCELLED:
    1.66 -      SDL_SendFingerDown(touchId,fingerId,
    1.67 -			 SDL_FALSE,x,y,1);
    1.68 -    case COCOA_TOUCH_MOVE:
    1.69 -      SDL_SendTouchMotion(touchId,fingerId,
    1.70 -			  SDL_FALSE,x,y,1);
    1.71 -    }
    1.72 -    
    1.73 -    touch = (NSTouch*)[enumerator nextObject];
    1.74 -  }
    1.75 -}
    1.76 -
    1.77 -- (void)touchesBeganWithEvent:(NSEvent *)event {
    1.78 -  DEBUG_TOUCH(@"Finger Down");
    1.79 -  
    1.80 -  [self handleTouches: COCOA_TOUCH_DOWN WithEvent: event];
    1.81 -
    1.82 -  //Documentation said to call super, but examples do not
    1.83 -  //[super touchesBeganWithEvent:event]
    1.84 -}
    1.85 -- (void)touchesMovedWithEvent:(NSEvent *)event {
    1.86 -  DEBUG_TOUCH(@"Finger Moved");
    1.87 -
    1.88 -  [self handleTouches: COCOA_TOUCH_MOVE WithEvent: event];
    1.89 -
    1.90 -  //[super touchesMovedWithEvent:event]
    1.91 -}
    1.92 -- (void)touchesEndedWithEvent:(NSEvent *)event {
    1.93 -  DEBUG_TOUCH(@"Finger Up");
    1.94 -
    1.95 -  [self handleTouches: COCOA_TOUCH_UP WithEvent: event];
    1.96 -
    1.97 -  //[super touchesEndedWithEvent:event]
    1.98 -}
    1.99 -- (void)touchesCancelledWithEvent:(NSEvent *)event {
   1.100 -  DEBUG_TOUCH(@"Finger Cancelled");
   1.101 -
   1.102 -  [self handleTouches: COCOA_TOUCH_CANCELLED WithEvent: event];
   1.103 -
   1.104 -  //[super touchesCancelledWithEvent:event]
   1.105 -}
   1.106 -
   1.107 -//Touch Code Ends --------------
   1.108 -
   1.109 -
   1.110  @end
   1.111  
   1.112  /* This is the original behavior, before support was added for 
   1.113 @@ -712,14 +628,13 @@
   1.114       * it to the front most window's content view */
   1.115      if (!data->fieldEdit) {
   1.116          data->fieldEdit =
   1.117 -            [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];	
   1.118 -	DEBUG_TOUCH(@"Accepts Touch events? %i",[data->fieldEdit acceptsTouchEvents]);
   1.119 +            [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
   1.120      }
   1.121  
   1.122      if (![[data->fieldEdit superview] isEqual: parentView])
   1.123      {
   1.124          // DEBUG_IME(@"add fieldEdit to window contentView");
   1.125 -       [data->fieldEdit removeFromSuperview];
   1.126 +        [data->fieldEdit removeFromSuperview];
   1.127          [parentView addSubview: data->fieldEdit];
   1.128          [[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
   1.129      }
   1.130 @@ -776,14 +691,14 @@
   1.131          if (![event isARepeat]) {
   1.132              /* See if we need to rebuild the keyboard layout */
   1.133              UpdateKeymap(data);
   1.134 +        }
   1.135  
   1.136 -            SDL_SendKeyboardKey(SDL_PRESSED, code);
   1.137 +        SDL_SendKeyboardKey(SDL_PRESSED, code);
   1.138  #if 1
   1.139 -            if (code == SDL_SCANCODE_UNKNOWN) {
   1.140 -                fprintf(stderr, "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode);
   1.141 -            }
   1.142 +        if (code == SDL_SCANCODE_UNKNOWN) {
   1.143 +            fprintf(stderr, "The key you just pressed is not recognized by SDL. To help get this fixed, report this to the SDL mailing list <sdl@libsdl.org> or to Christian Walther <cwalther@gmx.ch>. Mac virtual key code is %d.\n", scancode);
   1.144 +        }
   1.145  #endif
   1.146 -        }
   1.147          if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
   1.148              /* FIXME CW 2007-08-16: only send those events to the field editor for which we actually want text events, not e.g. esc or function keys. Arrow keys in particular seem to produce crashes sometimes. */
   1.149              [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
   1.150 @@ -808,29 +723,6 @@
   1.151      }
   1.152  }
   1.153  
   1.154 -Cocoa_AddTouch(NSTouch* finger) {  
   1.155 -  SDL_Touch touch;
   1.156 -  touch.id = (long)[finger device]; 
   1.157 -  //NSSize size = [finger deviceSize];
   1.158 -  //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
   1.159 -  //EventTouchData* data = (EventTouchData*)(touch.driverdata);
   1.160 -  
   1.161 -  touch.x_min = 0;
   1.162 -  touch.x_max = 1;
   1.163 -  touch.xres = touch.x_max - touch.x_min;
   1.164 -  touch.y_min = 0;
   1.165 -  touch.y_max = 1;
   1.166 -  touch.yres = touch.y_max - touch.y_min;
   1.167 -  touch.pressure_min = 0;
   1.168 -  touch.pressure_max = 1;
   1.169 -  touch.pressureres = touch.pressure_max - touch.pressure_min;
   1.170 -  
   1.171 -  
   1.172 -  return SDL_AddTouch(&touch, ""); 
   1.173 -
   1.174 -}
   1.175 -
   1.176 -
   1.177  void
   1.178  Cocoa_QuitKeyboard(_THIS)
   1.179  {
     2.1 --- a/src/video/cocoa/SDL_cocoatouch.h	Tue Jul 27 18:14:09 2010 +0400
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,35 +0,0 @@
     2.4 -/*
     2.5 -    SDL - Simple DirectMedia Layer
     2.6 -    Copyright (C) 1997-2010 Sam Lantinga
     2.7 -
     2.8 -    This library is free software; you can redistribute it and/or
     2.9 -    modify it under the terms of the GNU Lesser General Public
    2.10 -    License as published by the Free Software Foundation; either
    2.11 -    version 2.1 of the License, or (at your option) any later version.
    2.12 -
    2.13 -    This library is distributed in the hope that it will be useful,
    2.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 -    Lesser General Public License for more details.
    2.17 -
    2.18 -    You should have received a copy of the GNU Lesser General Public
    2.19 -    License along with this library; if not, write to the Free Software
    2.20 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 -
    2.22 -    Sam Lantinga
    2.23 -    slouken@libsdl.org
    2.24 -*/
    2.25 -#include "SDL_config.h"
    2.26 -
    2.27 -#ifndef _SDL_cocoatouch_h
    2.28 -#define _SDL_cocoatouch_h
    2.29 -
    2.30 -extern void Cocoa_InitTouch(_THIS);
    2.31 -extern int Cocoa_AddTouch(NSTouch* finger);
    2.32 -extern void Cocoa_QuitTouch(_THIS);
    2.33 -
    2.34 -typedef enum {COCOA_TOUCH_DOWN,COCOA_TOUCH_UP,COCOA_TOUCH_MOVE,COCOA_TOUCH_CANCELLED} cocoaTouchType;
    2.35 -
    2.36 -#endif /* _SDL_touch_h */
    2.37 -
    2.38 -/* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/cocoa/SDL_cocoawindow.h	Tue Jul 27 18:14:09 2010 +0400
     3.2 +++ b/src/video/cocoa/SDL_cocoawindow.h	Wed Jul 28 23:32:13 2010 -0700
     3.3 @@ -64,6 +64,20 @@
     3.4  -(void) rightMouseDragged:(NSEvent *) theEvent;
     3.5  -(void) otherMouseDragged:(NSEvent *) theEvent;
     3.6  -(void) scrollWheel:(NSEvent *) theEvent;
     3.7 +-(void) touchesBeganWithEvent:(NSEvent *) theEvent;
     3.8 +-(void) touchesMovedWithEvent:(NSEvent *) theEvent;
     3.9 +-(void) touchesEndedWithEvent:(NSEvent *) theEvent;
    3.10 +-(void) touchesCancelledWithEvent:(NSEvent *) theEvent;
    3.11 +
    3.12 +/* Touch event handling */
    3.13 +typedef enum {
    3.14 +    COCOA_TOUCH_DOWN,
    3.15 +    COCOA_TOUCH_UP,
    3.16 +    COCOA_TOUCH_MOVE,
    3.17 +    COCOA_TOUCH_CANCELLED
    3.18 +} cocoaTouchType;
    3.19 +-(void) handleTouches:(cocoaTouchType)type withEvent:(NSEvent*) event;
    3.20 +
    3.21  @end
    3.22  /* *INDENT-ON* */
    3.23  
     4.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Tue Jul 27 18:14:09 2010 +0400
     4.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Wed Jul 28 23:32:13 2010 -0700
     4.3 @@ -25,6 +25,7 @@
     4.4  #include "../SDL_sysvideo.h"
     4.5  #include "../../events/SDL_keyboard_c.h"
     4.6  #include "../../events/SDL_mouse_c.h"
     4.7 +#include "../../events/SDL_touch_c.h"
     4.8  #include "../../events/SDL_windowevents_c.h"
     4.9  
    4.10  #include "SDL_cocoavideo.h"
    4.11 @@ -60,6 +61,7 @@
    4.12      [center addObserver:self selector:@selector(windowDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp];
    4.13  
    4.14      [_data->nswindow setAcceptsMouseMovedEvents:YES];
    4.15 +    [[_data->nswindow contentView] setAcceptsTouchEvents:YES];
    4.16  }
    4.17  
    4.18  - (void)close
    4.19 @@ -268,6 +270,72 @@
    4.20      SDL_SendMouseWheel(_data->window, (int)x, (int)y);
    4.21  }
    4.22  
    4.23 +- (void)touchesBeganWithEvent:(NSEvent *) theEvent
    4.24 +{
    4.25 +    [self handleTouches:COCOA_TOUCH_DOWN withEvent:theEvent];
    4.26 +}
    4.27 +
    4.28 +- (void)touchesMovedWithEvent:(NSEvent *) theEvent
    4.29 +{
    4.30 +    [self handleTouches:COCOA_TOUCH_MOVE withEvent:theEvent];
    4.31 +}
    4.32 +
    4.33 +- (void)touchesEndedWithEvent:(NSEvent *) theEvent
    4.34 +{
    4.35 +    [self handleTouches:COCOA_TOUCH_UP withEvent:theEvent];
    4.36 +}
    4.37 +
    4.38 +- (void)touchesCancelledWithEvent:(NSEvent *) theEvent
    4.39 +{
    4.40 +    [self handleTouches:COCOA_TOUCH_CANCELLED withEvent:theEvent];
    4.41 +}
    4.42 +
    4.43 +- (void)handleTouches:(cocoaTouchType)type withEvent:(NSEvent *)event
    4.44 +{
    4.45 +    NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil];
    4.46 +
    4.47 +    NSEnumerator *enumerator = [touches objectEnumerator];
    4.48 +    NSTouch *touch = (NSTouch*)[enumerator nextObject];
    4.49 +    while (touch) {
    4.50 +        long touchId = (long)[touch device];
    4.51 +        if (!SDL_GetTouch(touchId)) {
    4.52 +            SDL_Touch touch;
    4.53 +
    4.54 +            touch.id = touchId;
    4.55 +            touch.x_min = 0;
    4.56 +            touch.x_max = 1;
    4.57 +            touch.xres = touch.x_max - touch.x_min;
    4.58 +            touch.y_min = 0;
    4.59 +            touch.y_max = 1;
    4.60 +            touch.yres = touch.y_max - touch.y_min;
    4.61 +            touch.pressure_min = 0;
    4.62 +            touch.pressure_max = 1;
    4.63 +            touch.pressureres = touch.pressure_max - touch.pressure_min;
    4.64 +            
    4.65 +            if (SDL_AddTouch(&touch, "") < 0) {
    4.66 +                return; 
    4.67 +            }
    4.68 +        } 
    4.69 +        float x = [touch normalizedPosition].x;
    4.70 +        float y = [touch normalizedPosition].y;
    4.71 +        long fingerId = (long)[touch identity];
    4.72 +        switch (type) {
    4.73 +        case COCOA_TOUCH_DOWN:
    4.74 +            SDL_SendFingerDown(touchId, fingerId, SDL_TRUE, x, y, 1);
    4.75 +            break;
    4.76 +        case COCOA_TOUCH_UP:
    4.77 +        case COCOA_TOUCH_CANCELLED:
    4.78 +            SDL_SendFingerDown(touchId, fingerId, SDL_FALSE, x, y, 1);
    4.79 +            break;
    4.80 +        case COCOA_TOUCH_MOVE:
    4.81 +            SDL_SendTouchMotion(touchId, fingerId, SDL_FALSE, x, y, 1);
    4.82 +            break;
    4.83 +        }
    4.84 +        
    4.85 +        touch = (NSTouch*)[enumerator nextObject];
    4.86 +    }
    4.87 +}
    4.88 +
    4.89  @end
    4.90  
    4.91  @interface SDLWindow : NSWindow