Send Apple TV remote input as key events unless it's opened as a joystick, to match Android behavior.
authorSam Lantinga <slouken@libsdl.org>
Tue, 06 Feb 2018 16:43:31 -0800
changeset 11846a1b2d62d5d73
parent 11845 cf80a56f6d8d
child 11847 4249379583bc
Send Apple TV remote input as key events unless it's opened as a joystick, to match Android behavior.
src/joystick/iphoneos/SDL_sysjoystick.m
src/joystick/iphoneos/SDL_sysjoystick_c.h
src/video/uikit/SDL_uikitview.m
     1.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Feb 06 15:03:38 2018 -0800
     1.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Feb 06 16:43:31 2018 -0800
     1.3 @@ -60,6 +60,7 @@
     1.4  
     1.5  static int numjoysticks = 0;
     1.6  static SDL_JoystickID instancecounter = 0;
     1.7 +int SDL_AppleTVRemoteOpenedAsJoystick = 0;
     1.8  
     1.9  static SDL_JoystickDeviceItem *
    1.10  GetDeviceForIndex(int device_index)
    1.11 @@ -116,6 +117,7 @@
    1.12  #if TARGET_OS_TV
    1.13      else if (controller.microGamepad) {
    1.14          device->guid.data[10] = 3;
    1.15 +        device->remote = SDL_TRUE;
    1.16      }
    1.17  #endif /* TARGET_OS_TV */
    1.18  
    1.19 @@ -455,6 +457,9 @@
    1.20  #endif /* SDL_JOYSTICK_MFI */
    1.21          }
    1.22      }
    1.23 +    if (device->remote) {
    1.24 +        ++SDL_AppleTVRemoteOpenedAsJoystick;
    1.25 +    }
    1.26  
    1.27      return 0;
    1.28  }
    1.29 @@ -719,6 +724,9 @@
    1.30  #endif
    1.31          }
    1.32      }
    1.33 +    if (device->remote) {
    1.34 +        --SDL_AppleTVRemoteOpenedAsJoystick;
    1.35 +    }
    1.36  }
    1.37  
    1.38  /* Function to perform any system-specific joystick related cleanup */
     2.1 --- a/src/joystick/iphoneos/SDL_sysjoystick_c.h	Tue Feb 06 15:03:38 2018 -0800
     2.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick_c.h	Tue Feb 06 16:43:31 2018 -0800
     2.3 @@ -31,6 +31,7 @@
     2.4  typedef struct joystick_hwdata
     2.5  {
     2.6      SDL_bool accelerometer;
     2.7 +    SDL_bool remote;
     2.8  
     2.9      GCController __unsafe_unretained *controller;
    2.10      int num_pause_presses;
     3.1 --- a/src/video/uikit/SDL_uikitview.m	Tue Feb 06 15:03:38 2018 -0800
     3.2 +++ b/src/video/uikit/SDL_uikitview.m	Tue Feb 06 16:43:31 2018 -0800
     3.3 @@ -33,6 +33,9 @@
     3.4  #import "SDL_uikitmodes.h"
     3.5  #import "SDL_uikitwindow.h"
     3.6  
     3.7 +/* This is defined in SDL_sysjoystick.m */
     3.8 +extern int SDL_AppleTVRemoteOpenedAsJoystick;
     3.9 +
    3.10  @implementation SDL_uikitview {
    3.11      SDL_Window *sdlwindow;
    3.12  
    3.13 @@ -44,24 +47,22 @@
    3.14  {
    3.15      if ((self = [super initWithFrame:frame])) {
    3.16  #if TARGET_OS_TV
    3.17 -        if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    3.18 -            /* Apple TV Remote touchpad swipe gestures. */
    3.19 -            UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.20 -            swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    3.21 -            [self addGestureRecognizer:swipeUp];
    3.22 +        /* Apple TV Remote touchpad swipe gestures. */
    3.23 +        UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.24 +        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    3.25 +        [self addGestureRecognizer:swipeUp];
    3.26  
    3.27 -            UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.28 -            swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    3.29 -            [self addGestureRecognizer:swipeDown];
    3.30 +        UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.31 +        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
    3.32 +        [self addGestureRecognizer:swipeDown];
    3.33  
    3.34 -            UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.35 -            swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    3.36 -            [self addGestureRecognizer:swipeLeft];
    3.37 +        UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.38 +        swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    3.39 +        [self addGestureRecognizer:swipeLeft];
    3.40  
    3.41 -            UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.42 -            swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    3.43 -            [self addGestureRecognizer:swipeRight];
    3.44 -        }
    3.45 +        UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    3.46 +        swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    3.47 +        [self addGestureRecognizer:swipeRight];
    3.48  #endif
    3.49  
    3.50          self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    3.51 @@ -251,7 +252,7 @@
    3.52  
    3.53  - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
    3.54  {
    3.55 -	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    3.56 +	if (!SDL_AppleTVRemoteOpenedAsJoystick) {
    3.57      	for (UIPress *press in presses) {
    3.58          	SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    3.59          	SDL_SendKeyboardKey(SDL_PRESSED, scancode);
    3.60 @@ -262,7 +263,7 @@
    3.61  
    3.62  - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
    3.63  {
    3.64 -	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    3.65 +	if (!SDL_AppleTVRemoteOpenedAsJoystick) {
    3.66  		for (UIPress *press in presses) {
    3.67  			SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    3.68  			SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    3.69 @@ -273,7 +274,7 @@
    3.70  
    3.71  - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
    3.72  {
    3.73 -	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    3.74 +	if (!SDL_AppleTVRemoteOpenedAsJoystick) {
    3.75  		for (UIPress *press in presses) {
    3.76  			SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    3.77  			SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    3.78 @@ -294,25 +295,27 @@
    3.79  {
    3.80      /* Swipe gestures don't trigger begin states. */
    3.81      if (gesture.state == UIGestureRecognizerStateEnded) {
    3.82 -        /* Send arrow key presses for now, as we don't have an external API
    3.83 -         * which better maps to swipe gestures. */
    3.84 -        switch (gesture.direction) {
    3.85 -        case UISwipeGestureRecognizerDirectionUp:
    3.86 -            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_UP);
    3.87 -            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_UP);
    3.88 -            break;
    3.89 -        case UISwipeGestureRecognizerDirectionDown:
    3.90 -            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DOWN);
    3.91 -            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DOWN);
    3.92 -            break;
    3.93 -        case UISwipeGestureRecognizerDirectionLeft:
    3.94 -            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LEFT);
    3.95 -            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LEFT);
    3.96 -            break;
    3.97 -        case UISwipeGestureRecognizerDirectionRight:
    3.98 -            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RIGHT);
    3.99 -            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RIGHT);
   3.100 -            break;
   3.101 +        if (!SDL_AppleTVRemoteOpenedAsJoystick) {
   3.102 +            /* Send arrow key presses for now, as we don't have an external API
   3.103 +             * which better maps to swipe gestures. */
   3.104 +            switch (gesture.direction) {
   3.105 +            case UISwipeGestureRecognizerDirectionUp:
   3.106 +                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_UP);
   3.107 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_UP);
   3.108 +                break;
   3.109 +            case UISwipeGestureRecognizerDirectionDown:
   3.110 +                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DOWN);
   3.111 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DOWN);
   3.112 +                break;
   3.113 +            case UISwipeGestureRecognizerDirectionLeft:
   3.114 +                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LEFT);
   3.115 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LEFT);
   3.116 +                break;
   3.117 +            case UISwipeGestureRecognizerDirectionRight:
   3.118 +                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RIGHT);
   3.119 +                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RIGHT);
   3.120 +                break;
   3.121 +            }
   3.122          }
   3.123      }
   3.124  }