Added a new hint SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION.
authorAlex Szpakowski <slime73@gmail.com>
Sat, 17 Sep 2016 01:31:07 -0300
changeset 1035112f90eb6b52b
parent 10350 130fc0b68ca8
child 10352 d11894522a9a
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.
include/SDL_hints.h
src/joystick/iphoneos/SDL_sysjoystick.m
     1.1 --- a/include/SDL_hints.h	Thu Sep 15 21:49:29 2016 -0300
     1.2 +++ b/include/SDL_hints.h	Sat Sep 17 01:31:07 2016 -0300
     1.3 @@ -296,6 +296,16 @@
     1.4  #define SDL_HINT_APPLE_TV_CONTROLLER_UI_EVENTS "SDL_APPLE_TV_CONTROLLER_UI_EVENTS"
     1.5  
     1.6  /**
     1.7 + * \brief  A variable controlling whether the Apple TV remote's joystick axes
     1.8 + *         will automatically match the rotation of the remote.
     1.9 + *
    1.10 + *  This variable can be set to the following values:
    1.11 + *    "0"       - Remote orientation does not affect joystick axes (the default).
    1.12 + *    "1"       - Joystick axes are based on the orientation of the remote.
    1.13 + */
    1.14 +#define SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION "SDL_APPLE_TV_REMOTE_ALLOW_ROTATION"
    1.15 +
    1.16 +/**
    1.17   *  \brief  A variable controlling whether the Android / iOS built-in
    1.18   *  accelerometer should be listed as a joystick device, rather than listing
    1.19   *  actual joysticks only.
     2.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Thu Sep 15 21:49:29 2016 -0300
     2.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Sat Sep 17 01:31:07 2016 -0300
     2.3 @@ -127,9 +127,13 @@
     2.4      }
     2.5  #if TARGET_OS_TV
     2.6      else if (controller.microGamepad) {
     2.7 +        const char *hint = SDL_GetHint(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION);
     2.8 +
     2.9          device->naxes = 2; /* treat the touch surface as two axes */
    2.10          device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
    2.11          device->nbuttons = 3; /* AX, pause button */
    2.12 +
    2.13 +        controller.microGamepad.allowsRotation = (hint != NULL && *hint != '0');
    2.14      }
    2.15  #endif /* TARGET_OS_TV */
    2.16  
    2.17 @@ -248,6 +252,22 @@
    2.18      return next;
    2.19  }
    2.20  
    2.21 +#if TARGET_OS_TV
    2.22 +static void
    2.23 +SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
    2.24 +{
    2.25 +    BOOL allowRotation = newValue != NULL && *newValue != '0';
    2.26 +
    2.27 +    @autoreleasepool {
    2.28 +        for (GCController *controller in [GCController controllers]) {
    2.29 +            if (controller.microGamepad) {
    2.30 +                controller.microGamepad.allowsRotation = allowRotation;
    2.31 +            }
    2.32 +        }
    2.33 +    }
    2.34 +}
    2.35 +#endif /* TARGET_OS_TV */
    2.36 +
    2.37  /* Function to scan the system for joysticks.
    2.38   * Joystick 0 should be the system default joystick.
    2.39   * It should return 0, or -1 on an unrecoverable fatal error.
    2.40 @@ -276,6 +296,11 @@
    2.41              SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
    2.42          }
    2.43  
    2.44 +#if TARGET_OS_TV
    2.45 +        SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
    2.46 +                            SDL_AppleTVRemoteRotationHintChanged, NULL);
    2.47 +#endif /* TARGET_OS_TV */
    2.48 +
    2.49          connectObserver = [center addObserverForName:GCControllerDidConnectNotification
    2.50                                                object:nil
    2.51                                                 queue:nil
    2.52 @@ -656,6 +681,11 @@
    2.53              [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
    2.54              disconnectObserver = nil;
    2.55          }
    2.56 +
    2.57 +#if TARGET_OS_TV
    2.58 +        SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
    2.59 +                            SDL_AppleTVRemoteRotationHintChanged, NULL);
    2.60 +#endif /* TARGET_OS_TV */
    2.61  #endif /* SDL_JOYSTICK_MFI */
    2.62  
    2.63          while (deviceList != NULL) {