[Commits] SDL: Send Apple TV remote input as key events unless it's opened...

libsdl.org revision control commits-owner at libsdl.org
Tue Feb 6 16:43:34 PST 2018


details:   https://hg.libsdl.org/SDL/rev/a1b2d62d5d73
changeset: 11846:a1b2d62d5d73
user:      Sam Lantinga <slouken at libsdl.org>
date:      Tue Feb 06 16:43:31 2018 -0800
description:
Send Apple TV remote input as key events unless it's opened as a joystick, to match Android behavior.

diffstat:

 src/joystick/iphoneos/SDL_sysjoystick.m   |   8 +++
 src/joystick/iphoneos/SDL_sysjoystick_c.h |   1 +
 src/video/uikit/SDL_uikitview.m           |  77 ++++++++++++++++--------------
 3 files changed, 49 insertions(+), 37 deletions(-)

diffs (175 lines):

diff -r cf80a56f6d8d -r a1b2d62d5d73 src/joystick/iphoneos/SDL_sysjoystick.m
--- a/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Feb 06 15:03:38 2018 -0800
+++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Feb 06 16:43:31 2018 -0800
@@ -60,6 +60,7 @@
 
 static int numjoysticks = 0;
 static SDL_JoystickID instancecounter = 0;
+int SDL_AppleTVRemoteOpenedAsJoystick = 0;
 
 static SDL_JoystickDeviceItem *
 GetDeviceForIndex(int device_index)
@@ -116,6 +117,7 @@
 #if TARGET_OS_TV
     else if (controller.microGamepad) {
         device->guid.data[10] = 3;
+        device->remote = SDL_TRUE;
     }
 #endif /* TARGET_OS_TV */
 
@@ -455,6 +457,9 @@
 #endif /* SDL_JOYSTICK_MFI */
         }
     }
+    if (device->remote) {
+        ++SDL_AppleTVRemoteOpenedAsJoystick;
+    }
 
     return 0;
 }
@@ -719,6 +724,9 @@
 #endif
         }
     }
+    if (device->remote) {
+        --SDL_AppleTVRemoteOpenedAsJoystick;
+    }
 }
 
 /* Function to perform any system-specific joystick related cleanup */
diff -r cf80a56f6d8d -r a1b2d62d5d73 src/joystick/iphoneos/SDL_sysjoystick_c.h
--- a/src/joystick/iphoneos/SDL_sysjoystick_c.h	Tue Feb 06 15:03:38 2018 -0800
+++ b/src/joystick/iphoneos/SDL_sysjoystick_c.h	Tue Feb 06 16:43:31 2018 -0800
@@ -31,6 +31,7 @@
 typedef struct joystick_hwdata
 {
     SDL_bool accelerometer;
+    SDL_bool remote;
 
     GCController __unsafe_unretained *controller;
     int num_pause_presses;
diff -r cf80a56f6d8d -r a1b2d62d5d73 src/video/uikit/SDL_uikitview.m
--- a/src/video/uikit/SDL_uikitview.m	Tue Feb 06 15:03:38 2018 -0800
+++ b/src/video/uikit/SDL_uikitview.m	Tue Feb 06 16:43:31 2018 -0800
@@ -33,6 +33,9 @@
 #import "SDL_uikitmodes.h"
 #import "SDL_uikitwindow.h"
 
+/* This is defined in SDL_sysjoystick.m */
+extern int SDL_AppleTVRemoteOpenedAsJoystick;
+
 @implementation SDL_uikitview {
     SDL_Window *sdlwindow;
 
@@ -44,24 +47,22 @@
 {
     if ((self = [super initWithFrame:frame])) {
 #if TARGET_OS_TV
-        if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
-            /* Apple TV Remote touchpad swipe gestures. */
-            UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
-            swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
-            [self addGestureRecognizer:swipeUp];
+        /* Apple TV Remote touchpad swipe gestures. */
+        UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
+        [self addGestureRecognizer:swipeUp];
 
-            UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
-            swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
-            [self addGestureRecognizer:swipeDown];
+        UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
+        [self addGestureRecognizer:swipeDown];
 
-            UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
-            swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
-            [self addGestureRecognizer:swipeLeft];
+        UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
+        [self addGestureRecognizer:swipeLeft];
 
-            UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
-            swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
-            [self addGestureRecognizer:swipeRight];
-        }
+        UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
+        swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
+        [self addGestureRecognizer:swipeRight];
 #endif
 
         self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
@@ -251,7 +252,7 @@
 
 - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
 {
-	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
+	if (!SDL_AppleTVRemoteOpenedAsJoystick) {
     	for (UIPress *press in presses) {
         	SDL_Scancode scancode = [self scancodeFromPressType:press.type];
         	SDL_SendKeyboardKey(SDL_PRESSED, scancode);
@@ -262,7 +263,7 @@
 
 - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
 {
-	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
+	if (!SDL_AppleTVRemoteOpenedAsJoystick) {
 		for (UIPress *press in presses) {
 			SDL_Scancode scancode = [self scancodeFromPressType:press.type];
 			SDL_SendKeyboardKey(SDL_RELEASED, scancode);
@@ -273,7 +274,7 @@
 
 - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
 {
-	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
+	if (!SDL_AppleTVRemoteOpenedAsJoystick) {
 		for (UIPress *press in presses) {
 			SDL_Scancode scancode = [self scancodeFromPressType:press.type];
 			SDL_SendKeyboardKey(SDL_RELEASED, scancode);
@@ -294,25 +295,27 @@
 {
     /* Swipe gestures don't trigger begin states. */
     if (gesture.state == UIGestureRecognizerStateEnded) {
-        /* Send arrow key presses for now, as we don't have an external API
-         * which better maps to swipe gestures. */
-        switch (gesture.direction) {
-        case UISwipeGestureRecognizerDirectionUp:
-            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_UP);
-            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_UP);
-            break;
-        case UISwipeGestureRecognizerDirectionDown:
-            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DOWN);
-            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DOWN);
-            break;
-        case UISwipeGestureRecognizerDirectionLeft:
-            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LEFT);
-            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LEFT);
-            break;
-        case UISwipeGestureRecognizerDirectionRight:
-            SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RIGHT);
-            SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RIGHT);
-            break;
+        if (!SDL_AppleTVRemoteOpenedAsJoystick) {
+            /* Send arrow key presses for now, as we don't have an external API
+             * which better maps to swipe gestures. */
+            switch (gesture.direction) {
+            case UISwipeGestureRecognizerDirectionUp:
+                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_UP);
+                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_UP);
+                break;
+            case UISwipeGestureRecognizerDirectionDown:
+                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_DOWN);
+                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_DOWN);
+                break;
+            case UISwipeGestureRecognizerDirectionLeft:
+                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_LEFT);
+                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LEFT);
+                break;
+            case UISwipeGestureRecognizerDirectionRight:
+                SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_RIGHT);
+                SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RIGHT);
+                break;
+            }
         }
     }
 }


More information about the commits mailing list