src/video/uikit/SDL_uikitview.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 01 Jan 2009 07:59:08 +0000
changeset 2940 b93965a16fe0
parent 2859 99210400e8b9
child 3685 64ce267332c6
permissions -rw-r--r--
Fixed X11 mouse motion/button events - it's not actually safe to cast mouse events to device events.
Fixed building SDL without XInput support
Simplified the process of registering a mouse device
slouken@2765
     1
/*
slouken@2765
     2
 SDL - Simple DirectMedia Layer
slouken@2859
     3
 Copyright (C) 1997-2009 Sam Lantinga
slouken@2765
     4
 
slouken@2765
     5
 This library is free software; you can redistribute it and/or
slouken@2765
     6
 modify it under the terms of the GNU Lesser General Public
slouken@2765
     7
 License as published by the Free Software Foundation; either
slouken@2765
     8
 version 2.1 of the License, or (at your option) any later version.
slouken@2765
     9
 
slouken@2765
    10
 This library is distributed in the hope that it will be useful,
slouken@2765
    11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2765
    12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2765
    13
 Lesser General Public License for more details.
slouken@2765
    14
 
slouken@2765
    15
 You should have received a copy of the GNU Lesser General Public
slouken@2765
    16
 License along with this library; if not, write to the Free Software
slouken@2765
    17
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2765
    18
 
slouken@2765
    19
 Sam Lantinga
slouken@2765
    20
 slouken@libsdl.org
slouken@2765
    21
 */
slouken@2765
    22
slouken@2765
    23
#import "SDL_uikitview.h"
slouken@2765
    24
slouken@2765
    25
#if SDL_IPHONE_KEYBOARD
slouken@2765
    26
#import "SDL_keyboard_c.h"
slouken@2765
    27
#import "keyinfotable.h"
slouken@2765
    28
#import "SDL_uikitappdelegate.h"
slouken@2765
    29
#import "SDL_uikitwindow.h"
slouken@2765
    30
#endif
slouken@2765
    31
slouken@2765
    32
@implementation SDL_uikitview
slouken@2765
    33
slouken@2765
    34
- (void)dealloc {
slouken@2765
    35
#if SDL_IPHONE_KEYBOARD
slouken@2765
    36
	SDL_DelKeyboard(0);
slouken@2765
    37
	[textField release];
slouken@2765
    38
#endif
slouken@2765
    39
	[super dealloc];
slouken@2765
    40
}
slouken@2765
    41
slouken@2765
    42
- (id)initWithFrame:(CGRect)frame {
slouken@2765
    43
slouken@2765
    44
	self = [super initWithFrame: frame];
slouken@2765
    45
	
slouken@2765
    46
#if SDL_IPHONE_KEYBOARD
slouken@2765
    47
	[self initializeKeyboard];
slouken@2765
    48
#endif	
slouken@2765
    49
slouken@2765
    50
	int i;
slouken@2765
    51
	for (i=0; i<MAX_SIMULTANEOUS_TOUCHES; i++) {
slouken@2940
    52
        mice[i].id = i;
slouken@2765
    53
		mice[i].driverdata = NULL;
slouken@2940
    54
		SDL_AddMouse(&mice[i], "Mouse", 0, 0, 1);
slouken@2765
    55
	}
slouken@2765
    56
	self.multipleTouchEnabled = YES;
slouken@2765
    57
			
slouken@2765
    58
	return self;
slouken@2765
    59
slouken@2765
    60
}
slouken@2765
    61
slouken@2765
    62
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
    63
slouken@2765
    64
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@2765
    65
	UITouch *touch =(UITouch*)[enumerator nextObject];
slouken@2765
    66
	
slouken@2765
    67
	/* associate touches with mice, so long as we have slots */
slouken@2765
    68
	int i;
slouken@2765
    69
	int found = 0;
slouken@2765
    70
	for(i=0; touch && i < MAX_SIMULTANEOUS_TOUCHES; i++) {
slouken@2765
    71
	
slouken@2765
    72
		/* check if this mouse is already tracking a touch */
slouken@2765
    73
		if (mice[i].driverdata != NULL) {
slouken@2765
    74
			continue;
slouken@2765
    75
		}
slouken@2765
    76
		/*	
slouken@2765
    77
			mouse not associated with anything right now,
slouken@2765
    78
			associate the touch with this mouse
slouken@2765
    79
		*/
slouken@2765
    80
		found = 1;
slouken@2765
    81
		
slouken@2765
    82
		/* save old mouse so we can switch back */
slouken@2765
    83
		int oldMouse = SDL_SelectMouse(-1);
slouken@2765
    84
		
slouken@2765
    85
		/* select this slot's mouse */
slouken@2765
    86
		SDL_SelectMouse(i);
slouken@2765
    87
		CGPoint locationInView = [touch locationInView: self];
slouken@2765
    88
		
slouken@2765
    89
		/* set driver data to touch object, we'll use touch object later */
slouken@2765
    90
		mice[i].driverdata = [touch retain];
slouken@2765
    91
		
slouken@2765
    92
		/* send moved event */
slouken@2765
    93
		SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
slouken@2765
    94
		
slouken@2765
    95
		/* send mouse down event */
slouken@2765
    96
		SDL_SendMouseButton(i, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@2765
    97
		
slouken@2765
    98
		/* re-calibrate relative mouse motion */
slouken@2765
    99
		SDL_GetRelativeMouseState(i, NULL, NULL);
slouken@2765
   100
		
slouken@2765
   101
		/* grab next touch */
slouken@2765
   102
		touch = (UITouch*)[enumerator nextObject]; 
slouken@2765
   103
		
slouken@2765
   104
		/* switch back to our old mouse */
slouken@2765
   105
		SDL_SelectMouse(oldMouse);
slouken@2765
   106
		
slouken@2765
   107
	}	
slouken@2765
   108
}
slouken@2765
   109
slouken@2765
   110
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   111
	
slouken@2765
   112
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@2765
   113
	UITouch *touch=nil;
slouken@2765
   114
	
slouken@2765
   115
	while(touch = (UITouch *)[enumerator nextObject]) {
slouken@2765
   116
		/* search for the mouse slot associated with this touch */
slouken@2765
   117
		int i, found = NO;
slouken@2765
   118
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
slouken@2765
   119
			if (mice[i].driverdata == touch) {
slouken@2765
   120
				/* found the mouse associate with the touch */
slouken@2765
   121
				[(UITouch*)(mice[i].driverdata) release];
slouken@2765
   122
				mice[i].driverdata = NULL;
slouken@2765
   123
				/* send mouse up */
slouken@2765
   124
				SDL_SendMouseButton(i, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@2765
   125
				/* discontinue search for this touch */
slouken@2765
   126
				found = YES;
slouken@2765
   127
			}
slouken@2765
   128
		}
slouken@2765
   129
	}
slouken@2765
   130
}
slouken@2765
   131
slouken@2765
   132
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   133
	/*
slouken@2765
   134
		this can happen if the user puts more than 5 touches on the screen
slouken@2765
   135
		at once, or perhaps in other circumstances.  Usually (it seems)
slouken@2765
   136
		all active touches are canceled.
slouken@2765
   137
	*/
slouken@2765
   138
	[self touchesEnded: touches withEvent: event];
slouken@2765
   139
}
slouken@2765
   140
slouken@2765
   141
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
   142
	
slouken@2765
   143
	NSEnumerator *enumerator = [touches objectEnumerator];
slouken@2765
   144
	UITouch *touch=nil;
slouken@2765
   145
	
slouken@2765
   146
	while(touch = (UITouch *)[enumerator nextObject]) {
slouken@2765
   147
		/* try to find the mouse associated with this touch */
slouken@2765
   148
		int i, found = NO;
slouken@2765
   149
		for (i=0; i<MAX_SIMULTANEOUS_TOUCHES && !found; i++) {
slouken@2765
   150
			if (mice[i].driverdata == touch) {
slouken@2765
   151
				/* found proper mouse */
slouken@2765
   152
				CGPoint locationInView = [touch locationInView: self];
slouken@2765
   153
				/* send moved event */
slouken@2765
   154
				SDL_SendMouseMotion(i, 0, locationInView.x, locationInView.y, 0);
slouken@2765
   155
				/* discontinue search */
slouken@2765
   156
				found = YES;
slouken@2765
   157
			}
slouken@2765
   158
		}
slouken@2765
   159
	}
slouken@2765
   160
}
slouken@2765
   161
slouken@2765
   162
/*
slouken@2765
   163
	---- Keyboard related functionality below this line ----
slouken@2765
   164
*/
slouken@2765
   165
#if SDL_IPHONE_KEYBOARD
slouken@2765
   166
slouken@2765
   167
/* Is the iPhone virtual keyboard visible onscreen? */
slouken@2765
   168
- (BOOL)keyboardVisible {
slouken@2765
   169
	return keyboardVisible;
slouken@2765
   170
}
slouken@2765
   171
slouken@2765
   172
/* Set ourselves up as a UITextFieldDelegate */
slouken@2765
   173
- (void)initializeKeyboard {
slouken@2765
   174
		
slouken@2765
   175
	textField = [[[UITextField alloc] initWithFrame: CGRectZero] autorelease];
slouken@2765
   176
	textField.delegate = self;
slouken@2765
   177
	/* placeholder so there is something to delete! */
slouken@2765
   178
	textField.text = @" ";	
slouken@2765
   179
	
slouken@2765
   180
	/* set UITextInputTrait properties, mostly to defaults */
slouken@2765
   181
	textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@2765
   182
	textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@2765
   183
	textField.enablesReturnKeyAutomatically = NO;
slouken@2765
   184
	textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@2765
   185
	textField.keyboardType = UIKeyboardTypeDefault;
slouken@2765
   186
	textField.returnKeyType = UIReturnKeyDefault;
slouken@2765
   187
	textField.secureTextEntry = NO;	
slouken@2765
   188
	
slouken@2765
   189
	textField.hidden = YES;
slouken@2765
   190
	keyboardVisible = NO;
slouken@2765
   191
	/* add the UITextField (hidden) to our view */
slouken@2765
   192
	[self addSubview: textField];
slouken@2765
   193
	
slouken@2765
   194
	/* create our SDL_Keyboard */
slouken@2765
   195
	SDL_Keyboard keyboard;
slouken@2765
   196
	SDL_zero(keyboard);
slouken@2765
   197
	SDL_AddKeyboard(&keyboard, 0);
slouken@2765
   198
	SDLKey keymap[SDL_NUM_SCANCODES];
slouken@2765
   199
	SDL_GetDefaultKeymap(keymap);
slouken@2765
   200
	SDL_SetKeymap(0, 0, keymap, SDL_NUM_SCANCODES);
slouken@2765
   201
	
slouken@2765
   202
}
slouken@2765
   203
slouken@2765
   204
/* reveal onscreen virtual keyboard */
slouken@2765
   205
- (void)showKeyboard {
slouken@2765
   206
	keyboardVisible = YES;
slouken@2765
   207
	[textField becomeFirstResponder];
slouken@2765
   208
}
slouken@2765
   209
slouken@2765
   210
/* hide onscreen virtual keyboard */
slouken@2765
   211
- (void)hideKeyboard {
slouken@2765
   212
	keyboardVisible = NO;
slouken@2765
   213
	[textField resignFirstResponder];
slouken@2765
   214
}
slouken@2765
   215
slouken@2765
   216
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   217
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
slouken@2765
   218
	
slouken@2765
   219
	if ([string length] == 0) {
slouken@2765
   220
		/* it wants to replace text with nothing, ie a delete */
slouken@2765
   221
		SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@2765
   222
		SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@2765
   223
	}
slouken@2765
   224
	else {
slouken@2765
   225
		/* go through all the characters in the string we've been sent
slouken@2765
   226
		   and convert them to key presses */
slouken@2765
   227
		int i;
slouken@2765
   228
		for (i=0; i<[string length]; i++) {
slouken@2765
   229
			
slouken@2765
   230
			unichar c = [string characterAtIndex: i];
slouken@2765
   231
			
slouken@2765
   232
			Uint16 mod = 0;
slouken@2765
   233
			SDL_scancode code;
slouken@2765
   234
			
slouken@2765
   235
			if (c < 127) {
slouken@2765
   236
				/* figure out the SDL_scancode and SDL_keymod for this unichar */
slouken@2765
   237
				code = unicharToUIKeyInfoTable[c].code;
slouken@2765
   238
				mod  = unicharToUIKeyInfoTable[c].mod;
slouken@2765
   239
			}
slouken@2765
   240
			else {
slouken@2765
   241
				/* we only deal with ASCII right now */
slouken@2765
   242
				code = SDL_SCANCODE_UNKNOWN;
slouken@2765
   243
				mod = 0;
slouken@2765
   244
			}
slouken@2765
   245
			
slouken@2765
   246
			if (mod & KMOD_SHIFT) {
slouken@2765
   247
				/* If character uses shift, press shift down */
slouken@2765
   248
				SDL_SendKeyboardKey( 0, SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@2765
   249
			}
slouken@2765
   250
			/* send a keydown and keyup even for the character */
slouken@2765
   251
			SDL_SendKeyboardKey( 0, SDL_PRESSED, code);
slouken@2765
   252
			SDL_SendKeyboardKey( 0, SDL_RELEASED, code);
slouken@2765
   253
			if (mod & KMOD_SHIFT) {
slouken@2765
   254
				/* If character uses shift, press shift back up */
slouken@2765
   255
				SDL_SendKeyboardKey( 0, SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slouken@2765
   256
			}			
slouken@2765
   257
		}
slouken@2765
   258
	}
slouken@2765
   259
	return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   260
}
slouken@2765
   261
slouken@2765
   262
/* Terminates the editing session */
slouken@2765
   263
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@2765
   264
	[self hideKeyboard];
slouken@2765
   265
	return YES;
slouken@2765
   266
}
slouken@2765
   267
slouken@2765
   268
#endif
slouken@2765
   269
slouken@2765
   270
@end
slouken@2765
   271
slouken@2765
   272
/* iPhone keyboard addition functions */
slouken@2765
   273
#if SDL_IPHONE_KEYBOARD
slouken@2765
   274
slouken@2765
   275
int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
slouken@2765
   276
	
slouken@2765
   277
	SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2765
   278
	SDL_WindowData *data;
slouken@2765
   279
	SDL_uikitview *view;
slouken@2765
   280
	
slouken@2765
   281
	if (NULL == window) {
slouken@2765
   282
		SDL_SetError("Window does not exist");
slouken@2765
   283
		return -1;
slouken@2765
   284
	}
slouken@2765
   285
	
slouken@2765
   286
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   287
	view = data->view;
slouken@2765
   288
	
slouken@2765
   289
	if (nil == view) {
slouken@2765
   290
		SDL_SetError("Window has no view");
slouken@2765
   291
		return -1;
slouken@2765
   292
	}
slouken@2765
   293
	else {
slouken@2765
   294
		[view showKeyboard];
slouken@2765
   295
		return 0;
slouken@2765
   296
	}
slouken@2765
   297
}
slouken@2765
   298
slouken@2765
   299
int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
slouken@2765
   300
	
slouken@2765
   301
	SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2765
   302
	SDL_WindowData *data;
slouken@2765
   303
	SDL_uikitview *view;
slouken@2765
   304
	
slouken@2765
   305
	if (NULL == window) {
slouken@2765
   306
		SDL_SetError("Window does not exist");
slouken@2765
   307
		return -1;
slouken@2765
   308
	}	
slouken@2765
   309
	
slouken@2765
   310
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   311
	view = data->view;
slouken@2765
   312
	
slouken@2765
   313
	if (NULL == view) {
slouken@2765
   314
		SDL_SetError("Window has no view");
slouken@2765
   315
		return -1;
slouken@2765
   316
	}
slouken@2765
   317
	else {
slouken@2765
   318
		[view hideKeyboard];
slouken@2765
   319
		return 0;
slouken@2765
   320
	}
slouken@2765
   321
}
slouken@2765
   322
slouken@2765
   323
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
slouken@2765
   324
	
slouken@2765
   325
	SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2765
   326
	SDL_WindowData *data;
slouken@2765
   327
	SDL_uikitview *view;
slouken@2765
   328
	
slouken@2765
   329
	if (NULL == window) {
slouken@2765
   330
		SDL_SetError("Window does not exist");
slouken@2765
   331
		return -1;
slouken@2765
   332
	}	
slouken@2765
   333
	
slouken@2765
   334
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   335
	view = data->view;
slouken@2765
   336
	
slouken@2765
   337
	if (NULL == view) {
slouken@2765
   338
		SDL_SetError("Window has no view");
slouken@2765
   339
		return 0;
slouken@2765
   340
	}
slouken@2765
   341
	else {
slouken@2765
   342
		return view.keyboardVisible;
slouken@2765
   343
	}
slouken@2765
   344
}
slouken@2765
   345
slouken@2765
   346
int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
slouken@2765
   347
	
slouken@2765
   348
	SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2765
   349
	SDL_WindowData *data;
slouken@2765
   350
	SDL_uikitview *view;
slouken@2765
   351
	
slouken@2765
   352
	if (NULL == window) {
slouken@2765
   353
		SDL_SetError("Window does not exist");
slouken@2765
   354
		return -1;
slouken@2765
   355
	}	
slouken@2765
   356
	
slouken@2765
   357
	data = (SDL_WindowData *)window->driverdata;
slouken@2765
   358
	view = data->view;
slouken@2765
   359
	
slouken@2765
   360
	if (NULL == view) {
slouken@2765
   361
		SDL_SetError("Window has no view");
slouken@2765
   362
		return -1;
slouken@2765
   363
	}
slouken@2765
   364
	else {
slouken@2765
   365
		if (SDL_iPhoneKeyboardIsShown(windowID)) {
slouken@2765
   366
			SDL_iPhoneKeyboardHide(windowID);
slouken@2765
   367
		}
slouken@2765
   368
		else {
slouken@2765
   369
			SDL_iPhoneKeyboardShow(windowID);
slouken@2765
   370
		}
slouken@2765
   371
		return 0;
slouken@2765
   372
	}
slouken@2765
   373
}
slouken@2765
   374
slouken@2765
   375
#else
slouken@2765
   376
slouken@2765
   377
/* stubs, used if compiled without keyboard support */
slouken@2765
   378
slouken@2765
   379
int SDL_iPhoneKeyboardShow(SDL_WindowID windowID) {
slouken@2765
   380
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   381
	return -1;
slouken@2765
   382
}
slouken@2765
   383
slouken@2765
   384
int SDL_iPhoneKeyboardHide(SDL_WindowID windowID) {
slouken@2765
   385
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   386
	return -1;
slouken@2765
   387
}
slouken@2765
   388
slouken@2765
   389
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_WindowID windowID) {
slouken@2765
   390
	return 0;
slouken@2765
   391
}
slouken@2765
   392
slouken@2765
   393
int SDL_iPhoneKeyboardToggle(SDL_WindowID windowID) {
slouken@2765
   394
	SDL_SetError("Not compiled with keyboard support");
slouken@2765
   395
	return -1;
slouken@2765
   396
}
slouken@2765
   397
slouken@2765
   398
slouken@2859
   399
#endif /* SDL_IPHONE_KEYBOARD */