Skip to content

Commit

Permalink
Added a new hint SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION.
Browse files Browse the repository at this point in the history
When set to "1", the orientation of the Apple TV remote affects the axes of the corresponding SDL joystick. It is "0" (disabled) by default.
  • Loading branch information
slime73 committed Sep 17, 2016
1 parent 4bcce33 commit a96b6f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -295,6 +295,16 @@ extern "C" {
*/
#define SDL_HINT_APPLE_TV_CONTROLLER_UI_EVENTS "SDL_APPLE_TV_CONTROLLER_UI_EVENTS"

/**
* \brief A variable controlling whether the Apple TV remote's joystick axes
* will automatically match the rotation of the remote.
*
* This variable can be set to the following values:
* "0" - Remote orientation does not affect joystick axes (the default).
* "1" - Joystick axes are based on the orientation of the remote.
*/
#define SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION "SDL_APPLE_TV_REMOTE_ALLOW_ROTATION"

/**
* \brief A variable controlling whether the Android / iOS built-in
* accelerometer should be listed as a joystick device, rather than listing
Expand Down
30 changes: 30 additions & 0 deletions src/joystick/iphoneos/SDL_sysjoystick.m
Expand Up @@ -127,9 +127,13 @@
}
#if TARGET_OS_TV
else if (controller.microGamepad) {
const char *hint = SDL_GetHint(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION);

device->naxes = 2; /* treat the touch surface as two axes */
device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
device->nbuttons = 3; /* AX, pause button */

controller.microGamepad.allowsRotation = (hint != NULL && *hint != '0');
}
#endif /* TARGET_OS_TV */

Expand Down Expand Up @@ -248,6 +252,22 @@
return next;
}

#if TARGET_OS_TV
static void
SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
{
BOOL allowRotation = newValue != NULL && *newValue != '0';

@autoreleasepool {
for (GCController *controller in [GCController controllers]) {
if (controller.microGamepad) {
controller.microGamepad.allowsRotation = allowRotation;
}
}
}
}
#endif /* TARGET_OS_TV */

/* Function to scan the system for joysticks.
* Joystick 0 should be the system default joystick.
* It should return 0, or -1 on an unrecoverable fatal error.
Expand Down Expand Up @@ -276,6 +296,11 @@
SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
}

#if TARGET_OS_TV
SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */

connectObserver = [center addObserverForName:GCControllerDidConnectNotification
object:nil
queue:nil
Expand Down Expand Up @@ -656,6 +681,11 @@ SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
[center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
disconnectObserver = nil;
}

#if TARGET_OS_TV
SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
SDL_AppleTVRemoteRotationHintChanged, NULL);
#endif /* TARGET_OS_TV */
#endif /* SDL_JOYSTICK_MFI */

while (deviceList != NULL) {
Expand Down

0 comments on commit a96b6f2

Please sign in to comment.