comparison src/video/cocoa/SDL_cocoakeyboard.m @ 4673:c17ac64abb70

Fixed the code so we receive Cocoa touch events
author Sam Lantinga <slouken@libsdl.org>
date Wed, 28 Jul 2010 23:32:13 -0700
parents 1f0e8f6417d9
children c24ba2cc9583
comparison
equal deleted inserted replaced
4672:013b0ea263dd 4673:c17ac64abb70
24 #include "SDL_cocoavideo.h" 24 #include "SDL_cocoavideo.h"
25 25
26 #include "../../events/SDL_keyboard_c.h" 26 #include "../../events/SDL_keyboard_c.h"
27 #include "../../events/scancodes_darwin.h" 27 #include "../../events/scancodes_darwin.h"
28 28
29 //Touch Code
30 #include "../../events/SDL_touch_c.h"
31 #include "SDL_cocoatouch.h"
32
33 #include <Carbon/Carbon.h> 29 #include <Carbon/Carbon.h>
34 30
35 //#define DEBUG_IME NSLog 31 //#define DEBUG_IME NSLog
36 #define DEBUG_IME 32 #define DEBUG_IME
37
38 #define DEBUG_TOUCH NSLog
39 33
40 #ifndef NX_DEVICERCTLKEYMASK 34 #ifndef NX_DEVICERCTLKEYMASK
41 #define NX_DEVICELCTLKEYMASK 0x00000001 35 #define NX_DEVICELCTLKEYMASK 0x00000001
42 #endif 36 #endif
43 #ifndef NX_DEVICELSHIFTKEYMASK 37 #ifndef NX_DEVICELSHIFTKEYMASK
69 NSRange _selectedRange; 63 NSRange _selectedRange;
70 SDL_Rect _inputRect; 64 SDL_Rect _inputRect;
71 } 65 }
72 - (void) doCommandBySelector:(SEL)myselector; 66 - (void) doCommandBySelector:(SEL)myselector;
73 - (void) setInputRect:(SDL_Rect *) rect; 67 - (void) setInputRect:(SDL_Rect *) rect;
74 - (void) handleTouches:(cocoaTouchType)type WithEvent:(NSEvent*) event;
75 @end 68 @end
76 69
77 @implementation SDLTranslatorResponder 70 @implementation SDLTranslatorResponder
78 71
79 - (void) setInputRect:(SDL_Rect *) rect 72 - (void) setInputRect:(SDL_Rect *) rect
195 // method & constructs appropriate attributed string. 188 // method & constructs appropriate attributed string.
196 - (NSArray *) validAttributesForMarkedText 189 - (NSArray *) validAttributesForMarkedText
197 { 190 {
198 return [NSArray array]; 191 return [NSArray array];
199 } 192 }
200
201 // Touch Code Begins -----------
202
203 - (id)initWithFrame:(CGRect)frame {
204 if (self = [super initWithFrame:frame]) {
205 [self setAcceptsTouchEvents:YES];
206 [self setWantsRestingTouches:YES];
207 DEBUG_TOUCH(@"Initializing Cocoa Touch System....");
208
209 }
210 return self;
211 }
212
213 //Not an API function
214 - (void)handleTouches:(cocoaTouchType)type WithEvent:(NSEvent *)event {
215 NSSet *touches = [event touchesMatchingPhase:NSTouchPhaseBegan inView:self];
216
217 NSEnumerator *enumerator = [touches objectEnumerator];
218 NSTouch *touch = (NSTouch*)[enumerator nextObject];
219 while (touch) {
220 long touchId = (long)[touch device];
221 if(!SDL_GetTouchIndex(touchId)) {
222 if(Cocoa_AddTouch(touch) < 0) continue;
223 }
224 float x = [touch normalizedPosition].x;
225 float y = [touch normalizedPosition].y;
226 long fingerId = (long)[touch identity];
227 switch (type) {
228 case COCOA_TOUCH_DOWN:
229 SDL_SendFingerDown(touchId,fingerId,
230 SDL_TRUE,x,y,1);
231 break;
232 case COCOA_TOUCH_UP:
233 case COCOA_TOUCH_CANCELLED:
234 SDL_SendFingerDown(touchId,fingerId,
235 SDL_FALSE,x,y,1);
236 case COCOA_TOUCH_MOVE:
237 SDL_SendTouchMotion(touchId,fingerId,
238 SDL_FALSE,x,y,1);
239 }
240
241 touch = (NSTouch*)[enumerator nextObject];
242 }
243 }
244
245 - (void)touchesBeganWithEvent:(NSEvent *)event {
246 DEBUG_TOUCH(@"Finger Down");
247
248 [self handleTouches: COCOA_TOUCH_DOWN WithEvent: event];
249
250 //Documentation said to call super, but examples do not
251 //[super touchesBeganWithEvent:event]
252 }
253 - (void)touchesMovedWithEvent:(NSEvent *)event {
254 DEBUG_TOUCH(@"Finger Moved");
255
256 [self handleTouches: COCOA_TOUCH_MOVE WithEvent: event];
257
258 //[super touchesMovedWithEvent:event]
259 }
260 - (void)touchesEndedWithEvent:(NSEvent *)event {
261 DEBUG_TOUCH(@"Finger Up");
262
263 [self handleTouches: COCOA_TOUCH_UP WithEvent: event];
264
265 //[super touchesEndedWithEvent:event]
266 }
267 - (void)touchesCancelledWithEvent:(NSEvent *)event {
268 DEBUG_TOUCH(@"Finger Cancelled");
269
270 [self handleTouches: COCOA_TOUCH_CANCELLED WithEvent: event];
271
272 //[super touchesCancelledWithEvent:event]
273 }
274
275 //Touch Code Ends --------------
276
277 193
278 @end 194 @end
279 195
280 /* This is the original behavior, before support was added for 196 /* This is the original behavior, before support was added for
281 * differentiating between left and right versions of the keys. 197 * differentiating between left and right versions of the keys.
710 * than one copy. When we switched to another window and requesting for 626 * than one copy. When we switched to another window and requesting for
711 * text input, simply remove the field editor from its superview then add 627 * text input, simply remove the field editor from its superview then add
712 * it to the front most window's content view */ 628 * it to the front most window's content view */
713 if (!data->fieldEdit) { 629 if (!data->fieldEdit) {
714 data->fieldEdit = 630 data->fieldEdit =
715 [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)]; 631 [[SDLTranslatorResponder alloc] initWithFrame: NSMakeRect(0.0, 0.0, 0.0, 0.0)];
716 DEBUG_TOUCH(@"Accepts Touch events? %i",[data->fieldEdit acceptsTouchEvents]);
717 } 632 }
718 633
719 if (![[data->fieldEdit superview] isEqual: parentView]) 634 if (![[data->fieldEdit superview] isEqual: parentView])
720 { 635 {
721 // DEBUG_IME(@"add fieldEdit to window contentView"); 636 // DEBUG_IME(@"add fieldEdit to window contentView");
722 [data->fieldEdit removeFromSuperview]; 637 [data->fieldEdit removeFromSuperview];
723 [parentView addSubview: data->fieldEdit]; 638 [parentView addSubview: data->fieldEdit];
724 [[NSApp keyWindow] makeFirstResponder: data->fieldEdit]; 639 [[NSApp keyWindow] makeFirstResponder: data->fieldEdit];
725 } 640 }
726 641
727 [pool release]; 642 [pool release];
774 switch ([event type]) { 689 switch ([event type]) {
775 case NSKeyDown: 690 case NSKeyDown:
776 if (![event isARepeat]) { 691 if (![event isARepeat]) {
777 /* See if we need to rebuild the keyboard layout */ 692 /* See if we need to rebuild the keyboard layout */
778 UpdateKeymap(data); 693 UpdateKeymap(data);
779 694 }
780 SDL_SendKeyboardKey(SDL_PRESSED, code); 695
696 SDL_SendKeyboardKey(SDL_PRESSED, code);
781 #if 1 697 #if 1
782 if (code == SDL_SCANCODE_UNKNOWN) { 698 if (code == SDL_SCANCODE_UNKNOWN) {
783 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); 699 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);
784 } 700 }
785 #endif 701 #endif
786 }
787 if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) { 702 if (SDL_EventState(SDL_TEXTINPUT, SDL_QUERY)) {
788 /* 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. */ 703 /* 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. */
789 [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]]; 704 [data->fieldEdit interpretKeyEvents:[NSArray arrayWithObject:event]];
790 #if 0 705 #if 0
791 text = [[event characters] UTF8String]; 706 text = [[event characters] UTF8String];
806 default: /* just to avoid compiler warnings */ 721 default: /* just to avoid compiler warnings */
807 break; 722 break;
808 } 723 }
809 } 724 }
810 725
811 Cocoa_AddTouch(NSTouch* finger) {
812 SDL_Touch touch;
813 touch.id = (long)[finger device];
814 //NSSize size = [finger deviceSize];
815 //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
816 //EventTouchData* data = (EventTouchData*)(touch.driverdata);
817
818 touch.x_min = 0;
819 touch.x_max = 1;
820 touch.xres = touch.x_max - touch.x_min;
821 touch.y_min = 0;
822 touch.y_max = 1;
823 touch.yres = touch.y_max - touch.y_min;
824 touch.pressure_min = 0;
825 touch.pressure_max = 1;
826 touch.pressureres = touch.pressure_max - touch.pressure_min;
827
828
829 return SDL_AddTouch(&touch, "");
830
831 }
832
833
834 void 726 void
835 Cocoa_QuitKeyboard(_THIS) 727 Cocoa_QuitKeyboard(_THIS)
836 { 728 {
837 } 729 }
838 730