src/video/uikit/SDL_uikitviewcontroller.m
author Alex Szpakowski
Tue, 13 Sep 2016 22:18:06 -0300
changeset 10340 5724f5087acd
parent 10141 fe9cf7c678f4
child 10418 ff5f3b3e6940
permissions -rw-r--r--
Initial Apple TV / tvOS support.

The Apple TV remote is currently exposed as a joystick with its touch surface treated as two axes. Key presses are also generated when its buttons and touch surface are used.

A new hint has been added to help deal with deciding whether to background the app when the remote's menu button is pressed: SDL_HINT_APPLE_TV_CONTROLLER_UI_EVENTS.
kees@5640
     1
/*
slouken@6044
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
kees@6001
     4
slouken@6044
     5
  This software is provided 'as-is', without any express or implied
slouken@6044
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6044
     7
  arising from the use of this software.
slouken@6044
     8
slouken@6044
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6044
    10
  including commercial applications, and to alter it and redistribute it
slouken@6044
    11
  freely, subject to the following restrictions:
kees@6001
    12
slouken@6044
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6044
    14
     claim that you wrote the original software. If you use this software
slouken@6044
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6044
    16
     appreciated but is not required.
slouken@6044
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6044
    18
     misrepresented as being the original software.
slouken@6044
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6044
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
kees@6001
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_UIKIT
kees@5640
    24
kees@5640
    25
#include "SDL_video.h"
kees@5640
    26
#include "SDL_assert.h"
kees@5640
    27
#include "SDL_hints.h"
kees@5640
    28
#include "../SDL_sysvideo.h"
kees@5640
    29
#include "../../events/SDL_events_c.h"
kees@5640
    30
slime73@9532
    31
#import "SDL_uikitviewcontroller.h"
slime73@9532
    32
#import "SDL_uikitmessagebox.h"
icculus@6093
    33
#include "SDL_uikitvideo.h"
slouken@6518
    34
#include "SDL_uikitmodes.h"
slouken@6518
    35
#include "SDL_uikitwindow.h"
slime73@10141
    36
#include "SDL_uikitopengles.h"
slouken@6518
    37
slime73@9532
    38
#if SDL_IPHONE_KEYBOARD
slime73@9532
    39
#include "keyinfotable.h"
slime73@9532
    40
#endif
kees@5640
    41
slime73@10340
    42
#if TARGET_OS_TV
slime73@10340
    43
static void
slime73@10340
    44
SDL_AppleTVControllerUIHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slime73@10340
    45
{
slime73@10340
    46
    @autoreleasepool {
slime73@10340
    47
        SDL_uikitviewcontroller *viewcontroller = (__bridge SDL_uikitviewcontroller *) userdata;
slime73@10340
    48
        viewcontroller.controllerUserInteractionEnabled = hint && (*hint != '0');
slime73@10340
    49
    }
slime73@10340
    50
}
slime73@10340
    51
#endif
slime73@10340
    52
slime73@9532
    53
@implementation SDL_uikitviewcontroller {
slime73@9532
    54
    CADisplayLink *displayLink;
slime73@9532
    55
    int animationInterval;
slime73@9532
    56
    void (*animationCallback)(void*);
slime73@9532
    57
    void *animationCallbackParam;
slime73@9532
    58
slime73@9532
    59
#if SDL_IPHONE_KEYBOARD
slime73@9532
    60
    UITextField *textField;
slime73@9532
    61
#endif
slime73@9532
    62
}
kees@5640
    63
kees@6011
    64
@synthesize window;
kees@6011
    65
slime73@9532
    66
- (instancetype)initWithSDLWindow:(SDL_Window *)_window
kees@6003
    67
{
slime73@9505
    68
    if (self = [super initWithNibName:nil bundle:nil]) {
slime73@9505
    69
        self.window = _window;
slime73@9532
    70
slime73@9532
    71
#if SDL_IPHONE_KEYBOARD
slime73@9532
    72
        [self initKeyboard];
slime73@9532
    73
#endif
slime73@10340
    74
slime73@10340
    75
#if TARGET_OS_TV
slime73@10340
    76
        SDL_AddHintCallback(SDL_HINT_APPLE_TV_CONTROLLER_UI_EVENTS,
slime73@10340
    77
                            SDL_AppleTVControllerUIHintChanged,
slime73@10340
    78
                            (__bridge void *) self);
slime73@10340
    79
#endif
kees@5640
    80
    }
kees@5640
    81
    return self;
kees@5640
    82
}
kees@5640
    83
slime73@9532
    84
- (void)dealloc
slime73@9532
    85
{
slime73@9532
    86
#if SDL_IPHONE_KEYBOARD
slime73@9532
    87
    [self deinitKeyboard];
slime73@9532
    88
#endif
slime73@10340
    89
slime73@10340
    90
#if TARGET_OS_TV
slime73@10340
    91
    SDL_DelHintCallback(SDL_HINT_APPLE_TV_CONTROLLER_UI_EVENTS,
slime73@10340
    92
                        SDL_AppleTVControllerUIHintChanged,
slime73@10340
    93
                        (__bridge void *) self);
slime73@10340
    94
#endif
slime73@9532
    95
}
slime73@9532
    96
slime73@9532
    97
- (void)setAnimationCallback:(int)interval
slime73@9532
    98
                    callback:(void (*)(void*))callback
slime73@9532
    99
               callbackParam:(void*)callbackParam
slime73@9532
   100
{
slime73@9532
   101
    [self stopAnimation];
slime73@9532
   102
slime73@9532
   103
    animationInterval = interval;
slime73@9532
   104
    animationCallback = callback;
slime73@9532
   105
    animationCallbackParam = callbackParam;
slime73@9532
   106
slime73@9532
   107
    if (animationCallback) {
slime73@9532
   108
        [self startAnimation];
slime73@9532
   109
    }
slime73@9532
   110
}
slime73@9532
   111
slime73@9532
   112
- (void)startAnimation
slime73@9532
   113
{
slime73@9532
   114
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(doLoop:)];
slime73@9532
   115
    [displayLink setFrameInterval:animationInterval];
slime73@9532
   116
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
slime73@9532
   117
}
slime73@9532
   118
slime73@9532
   119
- (void)stopAnimation
slime73@9532
   120
{
slime73@9532
   121
    [displayLink invalidate];
slime73@9532
   122
    displayLink = nil;
slime73@9532
   123
}
slime73@9532
   124
slime73@9532
   125
- (void)doLoop:(CADisplayLink*)sender
slime73@9532
   126
{
slime73@9532
   127
    /* Don't run the game loop while a messagebox is up */
slime73@9532
   128
    if (!UIKit_ShowingMessageBox()) {
slime73@10141
   129
        /* See the comment in the function definition. */
slime73@10141
   130
        UIKit_GL_RestoreCurrentContext();
slime73@10141
   131
slime73@9532
   132
        animationCallback(animationCallbackParam);
slime73@9532
   133
    }
slime73@9532
   134
}
slime73@9532
   135
slouken@6440
   136
- (void)loadView
slouken@6440
   137
{
slime73@9532
   138
    /* Do nothing. */
slouken@6440
   139
}
slouken@6440
   140
slouken@6451
   141
- (void)viewDidLayoutSubviews
slouken@6440
   142
{
slime73@9505
   143
    const CGSize size = self.view.bounds.size;
slime73@9494
   144
    int w = (int) size.width;
slime73@9494
   145
    int h = (int) size.height;
slouken@7191
   146
slime73@9494
   147
    SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
slouken@6440
   148
}
slouken@6440
   149
slime73@10340
   150
#if !TARGET_OS_TV
slouken@6451
   151
- (NSUInteger)supportedInterfaceOrientations
kees@6003
   152
{
slime73@9523
   153
    return UIKit_GetSupportedOrientations(window);
kees@5640
   154
}
kees@5640
   155
slouken@6451
   156
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
kees@6003
   157
{
slime73@9532
   158
    return ([self supportedInterfaceOrientations] & (1 << orient)) != 0;
kees@5640
   159
}
kees@5640
   160
slouken@7862
   161
- (BOOL)prefersStatusBarHidden
slouken@7862
   162
{
slime73@9529
   163
    return (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) != 0;
slouken@7862
   164
}
slime73@10340
   165
#endif
slouken@7862
   166
slime73@9532
   167
/*
slime73@9532
   168
 ---- Keyboard related functionality below this line ----
slime73@9532
   169
 */
slime73@9532
   170
#if SDL_IPHONE_KEYBOARD
slime73@9532
   171
slime73@9532
   172
@synthesize textInputRect;
slime73@9532
   173
@synthesize keyboardHeight;
slime73@9532
   174
@synthesize keyboardVisible;
slime73@9532
   175
slime73@9532
   176
/* Set ourselves up as a UITextFieldDelegate */
slime73@9532
   177
- (void)initKeyboard
slime73@9532
   178
{
slime73@9532
   179
    textField = [[UITextField alloc] initWithFrame:CGRectZero];
slime73@9532
   180
    textField.delegate = self;
slime73@9532
   181
    /* placeholder so there is something to delete! */
slime73@9532
   182
    textField.text = @" ";
slime73@9532
   183
slime73@9532
   184
    /* set UITextInputTrait properties, mostly to defaults */
slime73@9532
   185
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
slime73@9532
   186
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
slime73@9532
   187
    textField.enablesReturnKeyAutomatically = NO;
slime73@9532
   188
    textField.keyboardAppearance = UIKeyboardAppearanceDefault;
slime73@9532
   189
    textField.keyboardType = UIKeyboardTypeDefault;
slime73@9532
   190
    textField.returnKeyType = UIReturnKeyDefault;
slime73@9532
   191
    textField.secureTextEntry = NO;
slime73@9532
   192
slime73@9532
   193
    textField.hidden = YES;
slime73@9532
   194
    keyboardVisible = NO;
slime73@9532
   195
slime73@10340
   196
#if !TARGET_OS_TV
slime73@9532
   197
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9532
   198
    [center addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
slime73@9532
   199
    [center addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
slime73@10340
   200
#endif
slime73@9532
   201
}
slime73@9532
   202
slime73@9532
   203
- (void)setView:(UIView *)view
slime73@9532
   204
{
slime73@9532
   205
    [super setView:view];
slime73@9532
   206
slime73@9532
   207
    [view addSubview:textField];
slime73@9532
   208
slime73@9532
   209
    if (keyboardVisible) {
slime73@9532
   210
        [self showKeyboard];
slime73@9532
   211
    }
slime73@9532
   212
}
slime73@9532
   213
slime73@9532
   214
- (void)deinitKeyboard
slime73@9532
   215
{
slime73@10340
   216
#if !TARGET_OS_TV
slime73@9532
   217
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9532
   218
    [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
slime73@9532
   219
    [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
slime73@10340
   220
#endif
slime73@9532
   221
}
slime73@9532
   222
slime73@9532
   223
/* reveal onscreen virtual keyboard */
slime73@9532
   224
- (void)showKeyboard
slime73@9532
   225
{
slime73@9532
   226
    keyboardVisible = YES;
slime73@9532
   227
    if (textField.window) {
slime73@9532
   228
        [textField becomeFirstResponder];
slime73@9532
   229
    }
slime73@9532
   230
}
slime73@9532
   231
slime73@9532
   232
/* hide onscreen virtual keyboard */
slime73@9532
   233
- (void)hideKeyboard
slime73@9532
   234
{
slime73@9532
   235
    keyboardVisible = NO;
slime73@9532
   236
    [textField resignFirstResponder];
slime73@9532
   237
}
slime73@9532
   238
slime73@9532
   239
- (void)keyboardWillShow:(NSNotification *)notification
slime73@9532
   240
{
slime73@10340
   241
#if !TARGET_OS_TV
slime73@9532
   242
    CGRect kbrect = [[notification userInfo][UIKeyboardFrameBeginUserInfoKey] CGRectValue];
slime73@9532
   243
slime73@9585
   244
    /* The keyboard rect is in the coordinate space of the screen/window, but we
slime73@9585
   245
     * want its height in the coordinate space of the view. */
slime73@9585
   246
    kbrect = [self.view convertRect:kbrect fromView:nil];
slime73@9532
   247
slime73@9585
   248
    [self setKeyboardHeight:(int)kbrect.size.height];
slime73@10340
   249
#endif
slime73@9532
   250
}
slime73@9532
   251
slime73@9532
   252
- (void)keyboardWillHide:(NSNotification *)notification
slime73@9532
   253
{
slime73@9532
   254
    [self setKeyboardHeight:0];
slime73@9532
   255
}
slime73@9532
   256
slime73@9532
   257
- (void)updateKeyboard
slime73@9532
   258
{
slime73@9532
   259
    CGAffineTransform t = self.view.transform;
slime73@9532
   260
    CGPoint offset = CGPointMake(0.0, 0.0);
slime73@9585
   261
    CGRect frame = UIKit_ComputeViewFrame(window, self.view.window.screen);
slime73@9532
   262
slime73@9532
   263
    if (self.keyboardHeight) {
slime73@9585
   264
        int rectbottom = self.textInputRect.y + self.textInputRect.h;
slime73@9585
   265
        int keybottom = self.view.bounds.size.height - self.keyboardHeight;
slime73@9585
   266
        if (keybottom < rectbottom) {
slime73@9585
   267
            offset.y = keybottom - rectbottom;
slime73@9532
   268
        }
slime73@9532
   269
    }
slime73@9532
   270
slime73@9585
   271
    /* Apply this view's transform (except any translation) to the offset, in
slime73@9585
   272
     * order to orient it correctly relative to the frame's coordinate space. */
slime73@9532
   273
    t.tx = 0.0;
slime73@9532
   274
    t.ty = 0.0;
slime73@9532
   275
    offset = CGPointApplyAffineTransform(offset, t);
slime73@9532
   276
slime73@9585
   277
    /* Apply the updated offset to the view's frame. */
slime73@9585
   278
    frame.origin.x += offset.x;
slime73@9585
   279
    frame.origin.y += offset.y;
slime73@9532
   280
slime73@9585
   281
    self.view.frame = frame;
slime73@9532
   282
}
slime73@9532
   283
slime73@9532
   284
- (void)setKeyboardHeight:(int)height
slime73@9532
   285
{
slime73@9532
   286
    keyboardVisible = height > 0;
slime73@9532
   287
    keyboardHeight = height;
slime73@9532
   288
    [self updateKeyboard];
slime73@9532
   289
}
slime73@9532
   290
slime73@9532
   291
/* UITextFieldDelegate method.  Invoked when user types something. */
slime73@9532
   292
- (BOOL)textField:(UITextField *)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
slime73@9532
   293
{
slime73@9532
   294
    NSUInteger len = string.length;
slime73@9532
   295
slime73@9532
   296
    if (len == 0) {
slime73@9532
   297
        /* it wants to replace text with nothing, ie a delete */
slime73@9532
   298
        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_BACKSPACE);
slime73@9532
   299
        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_BACKSPACE);
slime73@9532
   300
    } else {
slime73@9532
   301
        /* go through all the characters in the string we've been sent and
slime73@9532
   302
         * convert them to key presses */
slime73@9532
   303
        int i;
slime73@9532
   304
        for (i = 0; i < len; i++) {
slime73@9532
   305
            unichar c = [string characterAtIndex:i];
slime73@9532
   306
            Uint16 mod = 0;
slime73@9532
   307
            SDL_Scancode code;
slime73@9532
   308
slime73@9532
   309
            if (c < 127) {
slime73@9532
   310
                /* figure out the SDL_Scancode and SDL_keymod for this unichar */
slime73@9532
   311
                code = unicharToUIKeyInfoTable[c].code;
slime73@9532
   312
                mod  = unicharToUIKeyInfoTable[c].mod;
slime73@9532
   313
            } else {
slime73@9532
   314
                /* we only deal with ASCII right now */
slime73@9532
   315
                code = SDL_SCANCODE_UNKNOWN;
slime73@9532
   316
                mod = 0;
slime73@9532
   317
            }
slime73@9532
   318
slime73@9532
   319
            if (mod & KMOD_SHIFT) {
slime73@9532
   320
                /* If character uses shift, press shift down */
slime73@9532
   321
                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LSHIFT);
slime73@9532
   322
            }
slime73@9532
   323
slime73@9532
   324
            /* send a keydown and keyup even for the character */
slime73@9532
   325
            SDL_SendKeyboardKey(SDL_PRESSED, code);
slime73@9532
   326
            SDL_SendKeyboardKey(SDL_RELEASED, code);
slime73@9532
   327
slime73@9532
   328
            if (mod & KMOD_SHIFT) {
slime73@9532
   329
                /* If character uses shift, press shift back up */
slime73@9532
   330
                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
slime73@9532
   331
            }
slime73@9532
   332
        }
slime73@9532
   333
slime73@9532
   334
        SDL_SendKeyboardText([string UTF8String]);
slime73@9532
   335
    }
slime73@9532
   336
slime73@9532
   337
    return NO; /* don't allow the edit! (keep placeholder text there) */
slime73@9532
   338
}
slime73@9532
   339
slime73@9532
   340
/* Terminates the editing session */
slime73@9532
   341
- (BOOL)textFieldShouldReturn:(UITextField*)_textField
slime73@9532
   342
{
slime73@9532
   343
    SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RETURN);
slime73@9532
   344
    SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RETURN);
slime73@9532
   345
    SDL_StopTextInput();
slime73@9532
   346
    return YES;
slime73@9532
   347
}
slime73@9532
   348
slime73@9532
   349
#endif
slime73@9532
   350
slouken@6451
   351
@end
slouken@6451
   352
slime73@9532
   353
/* iPhone keyboard addition functions */
slime73@9532
   354
#if SDL_IPHONE_KEYBOARD
slime73@9532
   355
slime73@9532
   356
static SDL_uikitviewcontroller *
slime73@9532
   357
GetWindowViewController(SDL_Window * window)
slime73@9532
   358
{
slime73@9532
   359
    if (!window || !window->driverdata) {
slime73@9532
   360
        SDL_SetError("Invalid window");
slime73@9532
   361
        return nil;
slime73@9532
   362
    }
slime73@9532
   363
slime73@9532
   364
    SDL_WindowData *data = (__bridge SDL_WindowData *)window->driverdata;
slime73@9532
   365
slime73@9532
   366
    return data.viewcontroller;
slime73@9532
   367
}
slime73@9532
   368
slime73@9532
   369
SDL_bool
slime73@9532
   370
UIKit_HasScreenKeyboardSupport(_THIS)
slime73@9532
   371
{
slime73@9532
   372
    return SDL_TRUE;
slime73@9532
   373
}
slime73@9532
   374
slime73@9532
   375
void
slime73@9532
   376
UIKit_ShowScreenKeyboard(_THIS, SDL_Window *window)
slime73@9532
   377
{
slime73@9532
   378
    @autoreleasepool {
slime73@9532
   379
        SDL_uikitviewcontroller *vc = GetWindowViewController(window);
slime73@9532
   380
        [vc showKeyboard];
slime73@9532
   381
    }
slime73@9532
   382
}
slime73@9532
   383
slime73@9532
   384
void
slime73@9532
   385
UIKit_HideScreenKeyboard(_THIS, SDL_Window *window)
slime73@9532
   386
{
slime73@9532
   387
    @autoreleasepool {
slime73@9532
   388
        SDL_uikitviewcontroller *vc = GetWindowViewController(window);
slime73@9532
   389
        [vc hideKeyboard];
slime73@9532
   390
    }
slime73@9532
   391
}
slime73@9532
   392
slime73@9532
   393
SDL_bool
slime73@9532
   394
UIKit_IsScreenKeyboardShown(_THIS, SDL_Window *window)
slime73@9532
   395
{
slime73@9532
   396
    @autoreleasepool {
slime73@9532
   397
        SDL_uikitviewcontroller *vc = GetWindowViewController(window);
slime73@9532
   398
        if (vc != nil) {
slime73@9532
   399
            return vc.isKeyboardVisible;
slime73@9532
   400
        }
slime73@9532
   401
        return SDL_FALSE;
slime73@9532
   402
    }
slime73@9532
   403
}
slime73@9532
   404
slime73@9532
   405
void
slime73@9532
   406
UIKit_SetTextInputRect(_THIS, SDL_Rect *rect)
slime73@9532
   407
{
slime73@9532
   408
    if (!rect) {
slime73@9532
   409
        SDL_InvalidParamError("rect");
slime73@9532
   410
        return;
slime73@9532
   411
    }
slime73@9532
   412
slime73@9532
   413
    @autoreleasepool {
slime73@9532
   414
        SDL_uikitviewcontroller *vc = GetWindowViewController(SDL_GetFocusWindow());
slime73@9532
   415
        if (vc != nil) {
slime73@9532
   416
            vc.textInputRect = *rect;
slime73@9532
   417
slime73@9532
   418
            if (vc.keyboardVisible) {
slime73@9532
   419
                [vc updateKeyboard];
slime73@9532
   420
            }
slime73@9532
   421
        }
slime73@9532
   422
    }
slime73@9532
   423
}
slime73@9532
   424
slime73@9532
   425
slime73@9532
   426
#endif /* SDL_IPHONE_KEYBOARD */
slime73@9532
   427
slouken@6044
   428
#endif /* SDL_VIDEO_DRIVER_UIKIT */
slouken@6044
   429
slouken@6440
   430
/* vi: set ts=4 sw=4 expandtab: */