src/video/uikit/SDL_uikitview.m
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 09:04:43 -0800
changeset 5134 c63b901d97ab
parent 5132 5f09cb749d75
child 5218 572a73d71b5f
permissions -rw-r--r--
Fixed bug #1025 (iphone keyboard doesn't send 'return' and 'backspace' events)

Vittorio Giovara 2011-02-01 02:25:48 PST

i have attached an updated patch that fixes this behaviour
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@4490
    25
#include "../../events/SDL_keyboard_c.h"
slouken@4490
    26
#include "../../events/SDL_mouse_c.h"
jimtla@4677
    27
#include "../../events/SDL_touch_c.h"
slouken@4490
    28
slouken@2765
    29
#if SDL_IPHONE_KEYBOARD
slouken@2765
    30
#import "keyinfotable.h"
slouken@2765
    31
#import "SDL_uikitappdelegate.h"
slouken@2765
    32
#import "SDL_uikitwindow.h"
slouken@2765
    33
#endif
slouken@2765
    34
slouken@2765
    35
@implementation SDL_uikitview
slouken@2765
    36
slouken@2765
    37
- (void)dealloc {
slouken@5131
    38
    [super dealloc];
slouken@2765
    39
}
slouken@2765
    40
slouken@2765
    41
- (id)initWithFrame:(CGRect)frame {
slouken@2765
    42
slouken@5131
    43
    self = [super initWithFrame: frame];
slouken@5131
    44
    
slouken@2765
    45
#if SDL_IPHONE_KEYBOARD
slouken@5131
    46
    [self initializeKeyboard];
slouken@5131
    47
#endif    
slouken@2765
    48
jimtla@4677
    49
#ifdef FIXED_MULTITOUCH
slouken@5131
    50
    SDL_Touch touch;
slouken@5131
    51
    touch.id = 0; //TODO: Should be -1?
jim@4660
    52
slouken@5131
    53
    //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
slouken@5131
    54
    //EventTouchData* data = (EventTouchData*)(touch.driverdata);
slouken@5131
    55
    
slouken@5131
    56
    touch.x_min = 0;
slouken@5131
    57
    touch.x_max = frame.size.width;
slouken@5131
    58
    touch.native_xres = touch.x_max - touch.x_min;
slouken@5131
    59
    touch.y_min = 0;
slouken@5131
    60
    touch.y_max = frame.size.height;
slouken@5131
    61
    touch.native_yres = touch.y_max - touch.y_min;
slouken@5131
    62
    touch.pressure_min = 0;
slouken@5131
    63
    touch.pressure_max = 1;
slouken@5131
    64
    touch.native_pressureres = touch.pressure_max - touch.pressure_min;
jim@4660
    65
jim@4660
    66
slouken@5131
    67
    touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
slouken@4465
    68
#endif
slouken@4661
    69
slouken@5131
    70
    return self;
slouken@2765
    71
slouken@2765
    72
}
slouken@2765
    73
slouken@2765
    74
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@2765
    75
slouken@5131
    76
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
    77
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5131
    78
    
slouken@5131
    79
    //NSLog("Click");
slouken@5131
    80
    
slouken@5131
    81
    if (touch) {
slouken@5131
    82
        CGPoint locationInView = [touch locationInView: self];
slouken@5131
    83
            
slouken@5131
    84
        /* send moved event */
slouken@5131
    85
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@4488
    86
slouken@5131
    87
        /* send mouse down event */
slouken@5131
    88
        SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@5131
    89
    }
jim@4660
    90
jimtla@4677
    91
#ifdef FIXED_MULTITOUCH
slouken@5131
    92
    while(touch) {
slouken@5131
    93
      CGPoint locationInView = [touch locationInView: self];
jim@4662
    94
jim@4662
    95
jim@4662
    96
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
    97
      //FIXME: TODO: Using touch as the fingerId is potentially dangerous
slouken@5131
    98
      //It is also much more efficient than storing the UITouch pointer
slouken@5131
    99
      //and comparing it to the incoming event.
slouken@5131
   100
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   101
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   102
                 1);
jim@4662
   103
#else
slouken@5131
   104
      int i;
slouken@5131
   105
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   106
        if(finger[i] == NULL) {
slouken@5131
   107
          finger[i] = touch;
slouken@5131
   108
          SDL_SendFingerDown(touchId,i,
slouken@5131
   109
                 SDL_TRUE,locationInView.x,locationInView.y,
slouken@5131
   110
                 1);
slouken@5131
   111
          break;
slouken@5131
   112
        }
slouken@5131
   113
      }
jim@4662
   114
#endif
slouken@5131
   115
      
jim@4662
   116
slouken@5131
   117
      
jim@4660
   118
slouken@5131
   119
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   120
    }
slouken@4465
   121
#endif
slouken@2765
   122
}
slouken@2765
   123
slouken@2765
   124
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   125
    
slouken@5131
   126
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   127
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5131
   128
    
slouken@5131
   129
    if (touch) {
slouken@5131
   130
        /* send mouse up */
slouken@5131
   131
        SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@5131
   132
    }
slouken@4661
   133
jimtla@4677
   134
#ifdef FIXED_MULTITOUCH
slouken@5131
   135
    while(touch) {
slouken@5131
   136
      CGPoint locationInView = [touch locationInView: self];
slouken@5131
   137
      
jim@4662
   138
jim@4662
   139
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   140
      SDL_SendFingerDown(touchId,(long)touch,
slouken@5131
   141
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   142
                 1);
jim@4662
   143
#else
slouken@5131
   144
      int i;
slouken@5131
   145
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   146
        if(finger[i] == touch) {
slouken@5131
   147
          SDL_SendFingerDown(touchId,i,
slouken@5131
   148
                 SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   149
                 1);
slouken@5131
   150
          break;
slouken@5131
   151
        }
slouken@5131
   152
      }
jim@4662
   153
#endif
jim@4660
   154
slouken@5131
   155
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   156
    }
slouken@4465
   157
#endif
slouken@2765
   158
}
slouken@2765
   159
slouken@2765
   160
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   161
    /*
slouken@5131
   162
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   163
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   164
        all active touches are canceled.
slouken@5131
   165
    */
slouken@5131
   166
    [self touchesEnded: touches withEvent: event];
slouken@2765
   167
}
slouken@2765
   168
slouken@2765
   169
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
slouken@5131
   170
    
slouken@5131
   171
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   172
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5131
   173
    
slouken@5131
   174
    if (touch) {
slouken@5131
   175
        CGPoint locationInView = [touch locationInView: self];
slouken@4488
   176
slouken@5131
   177
        /* send moved event */
slouken@5131
   178
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@5131
   179
    }
jim@4660
   180
jimtla@4677
   181
#ifdef FIXED_MULTITOUCH
slouken@5131
   182
    while(touch) {
slouken@5131
   183
      CGPoint locationInView = [touch locationInView: self];
slouken@5131
   184
      
jim@4662
   185
jim@4662
   186
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
slouken@5131
   187
      SDL_SendTouchMotion(touchId,(long)touch,
slouken@5131
   188
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   189
                  1);
jim@4662
   190
#else
slouken@5131
   191
      int i;
slouken@5131
   192
      for(i = 0;i < MAX_SIMULTANEOUS_TOUCHES;i++) {
slouken@5131
   193
        if(finger[i] == touch) {
slouken@5131
   194
          SDL_SendTouchMotion(touchId,i,
slouken@5131
   195
                  SDL_FALSE,locationInView.x,locationInView.y,
slouken@5131
   196
                  1);
slouken@5131
   197
          break;
slouken@5131
   198
        }
slouken@5131
   199
      }
jim@4662
   200
#endif
jim@4660
   201
slouken@5131
   202
      touch = (UITouch*)[enumerator nextObject]; 
slouken@5131
   203
    }
slouken@4465
   204
#endif
slouken@2765
   205
}
slouken@2765
   206
slouken@2765
   207
/*
slouken@5131
   208
    ---- Keyboard related functionality below this line ----
slouken@2765
   209
*/
slouken@2765
   210
#if SDL_IPHONE_KEYBOARD
slouken@2765
   211
slouken@2765
   212
/* Is the iPhone virtual keyboard visible onscreen? */
slouken@2765
   213
- (BOOL)keyboardVisible {
slouken@5131
   214
    return keyboardVisible;
slouken@2765
   215
}
slouken@2765
   216
slouken@2765
   217
/* Set ourselves up as a UITextFieldDelegate */
slouken@2765
   218
- (void)initializeKeyboard {
slouken@5131
   219
        
slouken@5131
   220
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
slouken@5131
   221
    textField.delegate = self;
slouken@5131
   222
    /* placeholder so there is something to delete! */
slouken@5131
   223
    textField.text = @" ";    
slouken@5131
   224
    
slouken@5131
   225
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   226
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   227
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   228
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   229
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   230
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   231
    textField.returnKeyType = UIReturnKeyDefault;
slouken@5131
   232
    textField.secureTextEntry = NO;    
slouken@5131
   233
    
slouken@5131
   234
    textField.hidden = YES;
slouken@5131
   235
    keyboardVisible = NO;
slouken@5131
   236
    /* add the UITextField (hidden) to our view */
slouken@5131
   237
    [self addSubview: textField];
slouken@5131
   238
    [textField release];
slouken@2765
   239
}
slouken@2765
   240
slouken@2765
   241
/* reveal onscreen virtual keyboard */
slouken@2765
   242
- (void)showKeyboard {
slouken@5131
   243
    keyboardVisible = YES;
slouken@5131
   244
    [textField becomeFirstResponder];
slouken@2765
   245
}
slouken@2765
   246
slouken@2765
   247
/* hide onscreen virtual keyboard */
slouken@2765
   248
- (void)hideKeyboard {
slouken@5131
   249
    keyboardVisible = NO;
slouken@5131
   250
    [textField resignFirstResponder];
slouken@2765
   251
}
slouken@2765
   252
slouken@2765
   253
/* UITextFieldDelegate method.  Invoked when user types something. */
slouken@2765
   254
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
slouken@5131
   255
    
slouken@5131
   256
    if ([string length] == 0) {
slouken@5131
   257
        /* it wants to replace text with nothing, ie a delete */
slouken@5131
   258
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@5131
   259
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@5131
   260
    }
slouken@5131
   261
    else {
slouken@5131
   262
        /* go through all the characters in the string we've been sent
slouken@5131
   263
           and convert them to key presses */
slouken@5131
   264
        int i;
slouken@5131
   265
        for (i=0; i<[string length]; i++) {
slouken@5131
   266
            
slouken@5131
   267
            unichar c = [string characterAtIndex: i];
slouken@5131
   268
            
slouken@5131
   269
            Uint16 mod = 0;
slouken@5131
   270
            SDL_ScanCode code;
slouken@5131
   271
            
slouken@5131
   272
            if (c < 127) {
slouken@5131
   273
                /* figure out the SDL_ScanCode and SDL_keymod for this unichar */
slouken@5131
   274
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   275
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   276
            }
slouken@5131
   277
            else {
slouken@5131
   278
                /* we only deal with ASCII right now */
slouken@5131
   279
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   280
                mod = 0;
slouken@5131
   281
            }
slouken@5131
   282
            
slouken@5131
   283
            if (mod & KMOD_SHIFT) {
slouken@5131
   284
                /* If character uses shift, press shift down */
slouken@5131
   285
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   286
            }
slouken@5131
   287
            /* send a keydown and keyup even for the character */
slouken@5131
   288
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   289
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   290
            if (mod & KMOD_SHIFT) {
slouken@5131
   291
                /* If character uses shift, press shift back up */
slouken@5131
   292
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slouken@5131
   293
            }            
slouken@5131
   294
        }
slouken@5131
   295
    }
slouken@5131
   296
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   297
}
slouken@2765
   298
slouken@2765
   299
/* Terminates the editing session */
slouken@2765
   300
- (BOOL)textFieldShouldReturn:(UITextField*)_textField {
slouken@5134
   301
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@5131
   302
    [self hideKeyboard];
slouken@5131
   303
    return YES;
slouken@2765
   304
}
slouken@2765
   305
slouken@2765
   306
#endif
slouken@2765
   307
slouken@2765
   308
@end
slouken@2765
   309
slouken@2765
   310
/* iPhone keyboard addition functions */
slouken@2765
   311
#if SDL_IPHONE_KEYBOARD
slouken@2765
   312
slouken@3685
   313
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@5131
   314
    
slouken@5131
   315
    SDL_WindowData *data;
slouken@5131
   316
    SDL_uikitview *view;
slouken@5131
   317
    
slouken@5131
   318
    if (NULL == window) {
slouken@5131
   319
        SDL_SetError("Window does not exist");
slouken@5131
   320
        return -1;
slouken@5131
   321
    }
slouken@5131
   322
    
slouken@5131
   323
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   324
    view = data->view;
slouken@5131
   325
    
slouken@5131
   326
    if (nil == view) {
slouken@5131
   327
        SDL_SetError("Window has no view");
slouken@5131
   328
        return -1;
slouken@5131
   329
    }
slouken@5131
   330
    else {
slouken@5131
   331
        [view showKeyboard];
slouken@5131
   332
        return 0;
slouken@5131
   333
    }
slouken@2765
   334
}
slouken@2765
   335
slouken@3685
   336
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@5131
   337
    
slouken@5131
   338
    SDL_WindowData *data;
slouken@5131
   339
    SDL_uikitview *view;
slouken@5131
   340
    
slouken@5131
   341
    if (NULL == window) {
slouken@5131
   342
        SDL_SetError("Window does not exist");
slouken@5131
   343
        return -1;
slouken@5131
   344
    }    
slouken@5131
   345
    
slouken@5131
   346
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   347
    view = data->view;
slouken@5131
   348
    
slouken@5131
   349
    if (NULL == view) {
slouken@5131
   350
        SDL_SetError("Window has no view");
slouken@5131
   351
        return -1;
slouken@5131
   352
    }
slouken@5131
   353
    else {
slouken@5131
   354
        [view hideKeyboard];
slouken@5131
   355
        return 0;
slouken@5131
   356
    }
slouken@2765
   357
}
slouken@2765
   358
slouken@3685
   359
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@5131
   360
    
slouken@5131
   361
    SDL_WindowData *data;
slouken@5131
   362
    SDL_uikitview *view;
slouken@5131
   363
    
slouken@5131
   364
    if (NULL == window) {
slouken@5131
   365
        SDL_SetError("Window does not exist");
slouken@5131
   366
        return -1;
slouken@5131
   367
    }    
slouken@5131
   368
    
slouken@5131
   369
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   370
    view = data->view;
slouken@5131
   371
    
slouken@5131
   372
    if (NULL == view) {
slouken@5131
   373
        SDL_SetError("Window has no view");
slouken@5131
   374
        return 0;
slouken@5131
   375
    }
slouken@5131
   376
    else {
slouken@5131
   377
        return view.keyboardVisible;
slouken@5131
   378
    }
slouken@2765
   379
}
slouken@2765
   380
slouken@3685
   381
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@5131
   382
    
slouken@5131
   383
    SDL_WindowData *data;
slouken@5131
   384
    SDL_uikitview *view;
slouken@5131
   385
    
slouken@5131
   386
    if (NULL == window) {
slouken@5131
   387
        SDL_SetError("Window does not exist");
slouken@5131
   388
        return -1;
slouken@5131
   389
    }    
slouken@5131
   390
    
slouken@5131
   391
    data = (SDL_WindowData *)window->driverdata;
slouken@5131
   392
    view = data->view;
slouken@5131
   393
    
slouken@5131
   394
    if (NULL == view) {
slouken@5131
   395
        SDL_SetError("Window has no view");
slouken@5131
   396
        return -1;
slouken@5131
   397
    }
slouken@5131
   398
    else {
slouken@5131
   399
        if (SDL_iPhoneKeyboardIsShown(window)) {
slouken@5131
   400
            SDL_iPhoneKeyboardHide(window);
slouken@5131
   401
        }
slouken@5131
   402
        else {
slouken@5131
   403
            SDL_iPhoneKeyboardShow(window);
slouken@5131
   404
        }
slouken@5131
   405
        return 0;
slouken@5131
   406
    }
slouken@2765
   407
}
slouken@2765
   408
slouken@2765
   409
#else
slouken@2765
   410
slouken@2765
   411
/* stubs, used if compiled without keyboard support */
slouken@2765
   412
slouken@3685
   413
int SDL_iPhoneKeyboardShow(SDL_Window * window) {
slouken@5131
   414
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   415
    return -1;
slouken@2765
   416
}
slouken@2765
   417
slouken@3685
   418
int SDL_iPhoneKeyboardHide(SDL_Window * window) {
slouken@5131
   419
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   420
    return -1;
slouken@2765
   421
}
slouken@2765
   422
slouken@3685
   423
SDL_bool SDL_iPhoneKeyboardIsShown(SDL_Window * window) {
slouken@5131
   424
    return 0;
slouken@2765
   425
}
slouken@2765
   426
slouken@3685
   427
int SDL_iPhoneKeyboardToggle(SDL_Window * window) {
slouken@5131
   428
    SDL_SetError("Not compiled with keyboard support");
slouken@5131
   429
    return -1;
slouken@2765
   430
}
slouken@2765
   431
slouken@5132
   432
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   433
slouken@5132
   434
/* vi: set ts=4 sw=4 expandtab: */