Skip to content

Commit 3a2d9ce

Browse files
committed
Added a new hint SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION.
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.
1 parent 5160ba4 commit 3a2d9ce

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

include/SDL_hints.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ extern "C" {
295295
*/
296296
#define SDL_HINT_APPLE_TV_CONTROLLER_UI_EVENTS "SDL_APPLE_TV_CONTROLLER_UI_EVENTS"
297297

298+
/**
299+
* \brief A variable controlling whether the Apple TV remote's joystick axes
300+
* will automatically match the rotation of the remote.
301+
*
302+
* This variable can be set to the following values:
303+
* "0" - Remote orientation does not affect joystick axes (the default).
304+
* "1" - Joystick axes are based on the orientation of the remote.
305+
*/
306+
#define SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION "SDL_APPLE_TV_REMOTE_ALLOW_ROTATION"
307+
298308
/**
299309
* \brief A variable controlling whether the Android / iOS built-in
300310
* accelerometer should be listed as a joystick device, rather than listing

src/joystick/iphoneos/SDL_sysjoystick.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@
127127
}
128128
#if TARGET_OS_TV
129129
else if (controller.microGamepad) {
130+
const char *hint = SDL_GetHint(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION);
131+
130132
device->naxes = 2; /* treat the touch surface as two axes */
131133
device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
132134
device->nbuttons = 3; /* AX, pause button */
135+
136+
controller.microGamepad.allowsRotation = (hint != NULL && *hint != '0');
133137
}
134138
#endif /* TARGET_OS_TV */
135139

@@ -248,6 +252,22 @@
248252
return next;
249253
}
250254

255+
#if TARGET_OS_TV
256+
static void
257+
SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
258+
{
259+
BOOL allowRotation = newValue != NULL && *newValue != '0';
260+
261+
@autoreleasepool {
262+
for (GCController *controller in [GCController controllers]) {
263+
if (controller.microGamepad) {
264+
controller.microGamepad.allowsRotation = allowRotation;
265+
}
266+
}
267+
}
268+
}
269+
#endif /* TARGET_OS_TV */
270+
251271
/* Function to scan the system for joysticks.
252272
* Joystick 0 should be the system default joystick.
253273
* It should return 0, or -1 on an unrecoverable fatal error.
@@ -276,6 +296,11 @@
276296
SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
277297
}
278298

299+
#if TARGET_OS_TV
300+
SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
301+
SDL_AppleTVRemoteRotationHintChanged, NULL);
302+
#endif /* TARGET_OS_TV */
303+
279304
connectObserver = [center addObserverForName:GCControllerDidConnectNotification
280305
object:nil
281306
queue:nil
@@ -656,6 +681,11 @@ SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
656681
[center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
657682
disconnectObserver = nil;
658683
}
684+
685+
#if TARGET_OS_TV
686+
SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
687+
SDL_AppleTVRemoteRotationHintChanged, NULL);
688+
#endif /* TARGET_OS_TV */
659689
#endif /* SDL_JOYSTICK_MFI */
660690

661691
while (deviceList != NULL) {

0 commit comments

Comments
 (0)