src/video/uikit/SDL_uikitview.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 11 Aug 2012 10:15:59 -0700
changeset 6392 fa7eb111f994
parent 6342 ac83b73f0edd
child 6436 29a35c72905a
child 8311 40f2e97c4051
permissions -rwxr-xr-x
Fixed bug 1564 - SDL has no function to open a screen keyboard on Android.

Philipp Wiesemann implemented a general on-screen keyboard API for SDL, and I switched iOS code over to use it.
slouken@6079
     1
 /*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@5262
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5262
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5262
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5262
    20
*/
slouken@6044
    21
#include "SDL_config.h"
slouken@6044
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
slouken@2765
    24
slouken@2765
    25
#import "SDL_uikitview.h"
slouken@2765
    26
slouken@4490
    27
#include "../../events/SDL_keyboard_c.h"
slouken@4490
    28
#include "../../events/SDL_mouse_c.h"
jimtla@4677
    29
#include "../../events/SDL_touch_c.h"
slouken@4490
    30
slouken@2765
    31
#if SDL_IPHONE_KEYBOARD
slouken@2765
    32
#import "keyinfotable.h"
slouken@2765
    33
#import "SDL_uikitappdelegate.h"
slouken@2765
    34
#import "SDL_uikitwindow.h"
slouken@2765
    35
#endif
slouken@2765
    36
slouken@2765
    37
@implementation SDL_uikitview
slouken@2765
    38
kees@6003
    39
- (void)dealloc
kees@6003
    40
{
slouken@5131
    41
    [super dealloc];
slouken@2765
    42
}
slouken@2765
    43
kees@6003
    44
- (id)initWithFrame:(CGRect)frame
kees@6003
    45
{
slouken@5131
    46
    self = [super initWithFrame: frame];
kees@6001
    47
slouken@2765
    48
#if SDL_IPHONE_KEYBOARD
slouken@5131
    49
    [self initializeKeyboard];
kees@6001
    50
#endif
slouken@2765
    51
jimtla@4677
    52
#ifdef FIXED_MULTITOUCH
slouken@5445
    53
    self.multipleTouchEnabled = YES;
slouken@5445
    54
slouken@5131
    55
    SDL_Touch touch;
slouken@5131
    56
    touch.id = 0; //TODO: Should be -1?
jim@4660
    57
slouken@5131
    58
    //touch.driverdata = SDL_malloc(sizeof(EventTouchData));
slouken@5131
    59
    //EventTouchData* data = (EventTouchData*)(touch.driverdata);
kees@6001
    60
slouken@5131
    61
    touch.x_min = 0;
slouken@6079
    62
    touch.x_max = 1;
slouken@5131
    63
    touch.native_xres = touch.x_max - touch.x_min;
slouken@5131
    64
    touch.y_min = 0;
slouken@6079
    65
    touch.y_max = 1;
slouken@5131
    66
    touch.native_yres = touch.y_max - touch.y_min;
slouken@5131
    67
    touch.pressure_min = 0;
slouken@5131
    68
    touch.pressure_max = 1;
slouken@5131
    69
    touch.native_pressureres = touch.pressure_max - touch.pressure_min;
jim@4660
    70
jim@4660
    71
slouken@5131
    72
    touchId = SDL_AddTouch(&touch, "IPHONE SCREEN");
slouken@4465
    73
#endif
slouken@4661
    74
slouken@5131
    75
    return self;
slouken@2765
    76
slouken@2765
    77
}
slouken@2765
    78
slouken@6079
    79
- (CGPoint)touchLocation:(UITouch *)touch
slouken@6079
    80
{
slouken@6079
    81
    CGPoint point = [touch locationInView: self];
slouken@6079
    82
    CGRect frame = [self frame];
slouken@6079
    83
slouken@6079
    84
    frame = CGRectApplyAffineTransform(frame, [self transform]);
slouken@6079
    85
    point.x /= frame.size.width;
slouken@6079
    86
    point.y /= frame.size.height;
slouken@6079
    87
    return point;
slouken@6079
    88
}
slouken@6079
    89
kees@6003
    90
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
    91
{
slouken@5131
    92
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
    93
    UITouch *touch = (UITouch*)[enumerator nextObject];
slouken@5459
    94
slouken@5131
    95
    if (touch) {
slouken@5131
    96
        CGPoint locationInView = [touch locationInView: self];
kees@6001
    97
slouken@5131
    98
        /* send moved event */
slouken@5131
    99
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@4488
   100
slouken@5131
   101
        /* send mouse down event */
slouken@5131
   102
        SDL_SendMouseButton(NULL, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@5131
   103
    }
jim@4660
   104
jimtla@4677
   105
#ifdef FIXED_MULTITOUCH
slouken@5131
   106
    while(touch) {
slouken@6079
   107
        CGPoint locationInView = [self touchLocation:touch];
jim@4662
   108
jim@4662
   109
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
   110
        //FIXME: TODO: Using touch as the fingerId is potentially dangerous
kees@6003
   111
        //It is also much more efficient than storing the UITouch pointer
kees@6003
   112
        //and comparing it to the incoming event.
kees@6003
   113
        SDL_SendFingerDown(touchId, (long)touch,
kees@6003
   114
                           SDL_TRUE, locationInView.x, locationInView.y,
kees@6003
   115
                           1);
jim@4662
   116
#else
kees@6003
   117
        int i;
kees@6003
   118
        for(i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   119
            if (finger[i] == NULL) {
kees@6003
   120
                finger[i] = touch;
kees@6003
   121
                SDL_SendFingerDown(touchId, i,
kees@6003
   122
                                   SDL_TRUE, locationInView.x, locationInView.y,
kees@6003
   123
                                   1);
kees@6003
   124
                break;
kees@6003
   125
            }
slouken@5131
   126
        }
jim@4662
   127
#endif
jim@4660
   128
kees@6003
   129
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   130
    }
slouken@4465
   131
#endif
slouken@2765
   132
}
slouken@2765
   133
kees@6003
   134
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   135
{
slouken@5131
   136
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   137
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   138
slouken@5131
   139
    if (touch) {
slouken@5131
   140
        /* send mouse up */
slouken@5131
   141
        SDL_SendMouseButton(NULL, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@5131
   142
    }
slouken@4661
   143
jimtla@4677
   144
#ifdef FIXED_MULTITOUCH
slouken@5131
   145
    while(touch) {
slouken@6079
   146
        CGPoint locationInView = [self touchLocation:touch];
jim@4662
   147
jim@4662
   148
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
   149
        SDL_SendFingerDown(touchId, (long)touch,
kees@6003
   150
                           SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   151
                           1);
jim@4662
   152
#else
kees@6003
   153
        int i;
kees@6003
   154
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   155
            if (finger[i] == touch) {
kees@6003
   156
                SDL_SendFingerDown(touchId, i,
kees@6003
   157
                                   SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   158
                                   1);
kees@6003
   159
                finger[i] = NULL;
kees@6003
   160
                break;
kees@6003
   161
            }
slouken@5131
   162
        }
jim@4662
   163
#endif
jim@4660
   164
kees@6003
   165
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   166
    }
slouken@4465
   167
#endif
slouken@2765
   168
}
slouken@2765
   169
kees@6003
   170
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   171
{
slouken@5131
   172
    /*
slouken@5131
   173
        this can happen if the user puts more than 5 touches on the screen
slouken@5131
   174
        at once, or perhaps in other circumstances.  Usually (it seems)
slouken@5131
   175
        all active touches are canceled.
slouken@5131
   176
    */
slouken@5131
   177
    [self touchesEnded: touches withEvent: event];
slouken@2765
   178
}
slouken@2765
   179
kees@6003
   180
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
kees@6003
   181
{
slouken@5131
   182
    NSEnumerator *enumerator = [touches objectEnumerator];
slouken@5131
   183
    UITouch *touch = (UITouch*)[enumerator nextObject];
kees@6001
   184
slouken@5131
   185
    if (touch) {
slouken@5131
   186
        CGPoint locationInView = [touch locationInView: self];
slouken@4488
   187
slouken@5131
   188
        /* send moved event */
slouken@5131
   189
        SDL_SendMouseMotion(NULL, 0, locationInView.x, locationInView.y);
slouken@5131
   190
    }
jim@4660
   191
jimtla@4677
   192
#ifdef FIXED_MULTITOUCH
slouken@5131
   193
    while(touch) {
slouken@6079
   194
        CGPoint locationInView = [self touchLocation:touch];
jim@4662
   195
jim@4662
   196
#ifdef IPHONE_TOUCH_EFFICIENT_DANGEROUS
kees@6003
   197
        SDL_SendTouchMotion(touchId, (long)touch,
kees@6003
   198
                            SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   199
                            1);
jim@4662
   200
#else
kees@6003
   201
        int i;
kees@6003
   202
        for (i = 0; i < MAX_SIMULTANEOUS_TOUCHES; i++) {
kees@6003
   203
            if (finger[i] == touch) {
kees@6003
   204
                SDL_SendTouchMotion(touchId, i,
kees@6003
   205
                                    SDL_FALSE, locationInView.x, locationInView.y,
kees@6003
   206
                                    1);
kees@6003
   207
                break;
kees@6003
   208
            }
slouken@5131
   209
        }
jim@4662
   210
#endif
jim@4660
   211
kees@6003
   212
        touch = (UITouch*)[enumerator nextObject];
slouken@5131
   213
    }
slouken@4465
   214
#endif
slouken@2765
   215
}
slouken@2765
   216
slouken@2765
   217
/*
slouken@5131
   218
    ---- Keyboard related functionality below this line ----
slouken@2765
   219
*/
slouken@2765
   220
#if SDL_IPHONE_KEYBOARD
slouken@2765
   221
slouken@2765
   222
/* Is the iPhone virtual keyboard visible onscreen? */
kees@6003
   223
- (BOOL)keyboardVisible
kees@6003
   224
{
slouken@5131
   225
    return keyboardVisible;
slouken@2765
   226
}
slouken@2765
   227
slouken@2765
   228
/* Set ourselves up as a UITextFieldDelegate */
kees@6003
   229
- (void)initializeKeyboard
kees@6003
   230
{
slouken@5131
   231
    textField = [[UITextField alloc] initWithFrame: CGRectZero];
slouken@5131
   232
    textField.delegate = self;
slouken@5131
   233
    /* placeholder so there is something to delete! */
kees@6001
   234
    textField.text = @" ";
kees@6001
   235
slouken@5131
   236
    /* set UITextInputTrait properties, mostly to defaults */
slouken@5131
   237
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slouken@5131
   238
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slouken@5131
   239
    textField.enablesReturnKeyAutomatically = NO;
slouken@5131
   240
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slouken@5131
   241
    textField.keyboardType = UIKeyboardTypeDefault;
slouken@5131
   242
    textField.returnKeyType = UIReturnKeyDefault;
kees@6001
   243
    textField.secureTextEntry = NO;
kees@6001
   244
slouken@5131
   245
    textField.hidden = YES;
slouken@5131
   246
    keyboardVisible = NO;
slouken@5131
   247
    /* add the UITextField (hidden) to our view */
slouken@5131
   248
    [self addSubview: textField];
slouken@5131
   249
    [textField release];
slouken@2765
   250
}
slouken@2765
   251
slouken@2765
   252
/* reveal onscreen virtual keyboard */
kees@6003
   253
- (void)showKeyboard
kees@6003
   254
{
slouken@5131
   255
    keyboardVisible = YES;
slouken@5131
   256
    [textField becomeFirstResponder];
slouken@2765
   257
}
slouken@2765
   258
slouken@2765
   259
/* hide onscreen virtual keyboard */
kees@6003
   260
- (void)hideKeyboard
kees@6003
   261
{
slouken@5131
   262
    keyboardVisible = NO;
slouken@5131
   263
    [textField resignFirstResponder];
slouken@2765
   264
}
slouken@2765
   265
slouken@2765
   266
/* UITextFieldDelegate method.  Invoked when user types something. */
kees@6003
   267
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
kees@6003
   268
{
slouken@5131
   269
    if ([string length] == 0) {
slouken@5131
   270
        /* it wants to replace text with nothing, ie a delete */
slouken@5131
   271
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DELETE);
slouken@5131
   272
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DELETE);
slouken@5131
   273
    }
slouken@5131
   274
    else {
slouken@5131
   275
        /* go through all the characters in the string we've been sent
slouken@5131
   276
           and convert them to key presses */
slouken@5131
   277
        int i;
kees@6003
   278
        for (i = 0; i < [string length]; i++) {
kees@6001
   279
slouken@5131
   280
            unichar c = [string characterAtIndex: i];
kees@6001
   281
slouken@5131
   282
            Uint16 mod = 0;
slouken@5218
   283
            SDL_Scancode code;
kees@6001
   284
slouken@5131
   285
            if (c < 127) {
slouken@5218
   286
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slouken@5131
   287
                code = unicharToUIKeyInfoTable[c].code;
slouken@5131
   288
                mod  = unicharToUIKeyInfoTable[c].mod;
slouken@5131
   289
            }
slouken@5131
   290
            else {
slouken@5131
   291
                /* we only deal with ASCII right now */
slouken@5131
   292
                code = SDL_SCANCODE_UNKNOWN;
slouken@5131
   293
                mod = 0;
slouken@5131
   294
            }
kees@6001
   295
slouken@5131
   296
            if (mod & KMOD_SHIFT) {
slouken@5131
   297
                /* If character uses shift, press shift down */
slouken@5131
   298
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slouken@5131
   299
            }
slouken@5131
   300
            /* send a keydown and keyup even for the character */
slouken@5131
   301
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@5131
   302
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@5131
   303
            if (mod & KMOD_SHIFT) {
slouken@5131
   304
                /* If character uses shift, press shift back up */
slouken@5131
   305
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
kees@6001
   306
            }
slouken@5131
   307
        }
slouken@5461
   308
        SDL_SendKeyboardText([string UTF8String]);
slouken@5131
   309
    }
slouken@5131
   310
    return NO; /* don't allow the edit! (keep placeholder text there) */
slouken@2765
   311
}
slouken@2765
   312
slouken@2765
   313
/* Terminates the editing session */
kees@6003
   314
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
kees@6003
   315
{
slouken@5134
   316
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slouken@6054
   317
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
slouken@5131
   318
    [self hideKeyboard];
slouken@5131
   319
    return YES;
slouken@2765
   320
}
slouken@2765
   321
slouken@2765
   322
#endif
slouken@2765
   323
slouken@2765
   324
@end
slouken@2765
   325
slouken@2765
   326
/* iPhone keyboard addition functions */
slouken@2765
   327
#if SDL_IPHONE_KEYBOARD
slouken@2765
   328
slouken@6044
   329
static SDL_uikitview * getWindowView(SDL_Window * window)
kees@6010
   330
{
kees@6010
   331
    if (window == NULL) {
kees@6010
   332
        SDL_SetError("Window does not exist");
kees@6010
   333
        return nil;
kees@6010
   334
    }
kees@6010
   335
kees@6010
   336
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
kees@6010
   337
    SDL_uikitview *view = data != NULL ? data->view : nil;
kees@6010
   338
kees@6010
   339
    if (view == nil) {
kees@6010
   340
        SDL_SetError("Window has no view");
kees@6010
   341
    }
kees@6010
   342
kees@6010
   343
    return view;
kees@6010
   344
}
kees@6010
   345
slouken@6392
   346
SDL_bool UIKit_HasScreenKeyboardSupport(_THIS, SDL_Window *window)
slouken@6392
   347
{
slouken@6392
   348
    SDL_uikitview *view = getWindowView(window);
slouken@6392
   349
    if (view == nil) {
slouken@6392
   350
        return SDL_FALSE;
slouken@6392
   351
    }
slouken@6392
   352
slouken@6392
   353
    return SDL_TRUE;
slouken@6392
   354
}
slouken@6392
   355
slouken@6392
   356
int UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   357
{
kees@6010
   358
    SDL_uikitview *view = getWindowView(window);
kees@6010
   359
    if (view == nil) {
slouken@5131
   360
        return -1;
slouken@5131
   361
    }
kees@6001
   362
kees@6010
   363
    [view showKeyboard];
kees@6010
   364
    return 0;
slouken@2765
   365
}
slouken@2765
   366
slouken@6392
   367
int UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   368
{
kees@6010
   369
    SDL_uikitview *view = getWindowView(window);
kees@6010
   370
    if (view == nil) {
slouken@5131
   371
        return -1;
kees@6001
   372
    }
kees@6001
   373
kees@6010
   374
    [view hideKeyboard];
kees@6010
   375
    return 0;
slouken@2765
   376
}
slouken@2765
   377
slouken@6392
   378
SDL_bool UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
kees@6003
   379
{
kees@6010
   380
    SDL_uikitview *view = getWindowView(window);
kees@6010
   381
    if (view == nil) {
kees@6010
   382
        return 0;
kees@6001
   383
    }
kees@6001
   384
kees@6010
   385
    return view.keyboardVisible;
slouken@2765
   386
}
slouken@2765
   387
slouken@6392
   388
int UIKit_ToggleScreenKeyboard(_THIS, SDL_Window *window)
kees@6003
   389
{
kees@6010
   390
    SDL_uikitview *view = getWindowView(window);
kees@6010
   391
    if (view == nil) {
slouken@5131
   392
        return -1;
kees@6001
   393
    }
kees@6001
   394
slouken@6392
   395
    if (UIKit_IsScreenKeyboardShown(_this, window)) {
slouken@6392
   396
        UIKit_HideScreenKeyboard(_this, window);
slouken@5131
   397
    }
slouken@5131
   398
    else {
slouken@6392
   399
        UIKit_ShowScreenKeyboard(_this, window);
slouken@5131
   400
    }
kees@6010
   401
    return 0;
slouken@2765
   402
}
slouken@2765
   403
slouken@5132
   404
#endif /* SDL_IPHONE_KEYBOARD */
slouken@2765
   405
slouken@6044
   406
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   407
slouken@5132
   408
/* vi: set ts=4 sw=4 expandtab: */