Replaced SDL_HINT_APPLE_TV_REMOTE_SWIPES_AS_ARROW_KEYS with SDL_HINT_TV_REMOTE_AS_JOYSTICK which controls whether remotes on iOS and Android are interpreted as joysticks (the default) or as return/escape/arrow keys.
authorSam Lantinga <slouken@libsdl.org>
Tue, 06 Feb 2018 15:03:38 -0800
changeset 11845cf80a56f6d8d
parent 11844 8e2ca40e07f3
child 11846 a1b2d62d5d73
Replaced SDL_HINT_APPLE_TV_REMOTE_SWIPES_AS_ARROW_KEYS with SDL_HINT_TV_REMOTE_AS_JOYSTICK which controls whether remotes on iOS and Android are interpreted as joysticks (the default) or as return/escape/arrow keys.
include/SDL_hints.h
src/joystick/android/SDL_sysjoystick.c
src/joystick/iphoneos/SDL_sysjoystick.m
src/video/uikit/SDL_uikitview.m
     1.1 --- a/include/SDL_hints.h	Tue Feb 06 15:03:35 2018 -0800
     1.2 +++ b/include/SDL_hints.h	Tue Feb 06 15:03:38 2018 -0800
     1.3 @@ -356,16 +356,6 @@
     1.4  #define SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION "SDL_APPLE_TV_REMOTE_ALLOW_ROTATION"
     1.5  
     1.6  /**
     1.7 - * \brief  A variable controlling whether the Apple TV remote swipes are
     1.8 - *         translated into arrow key events
     1.9 - *
    1.10 - *  This variable can be set to the following values:
    1.11 - *    "0"       - Swipes are not translated into arrow key events
    1.12 - *    "1"       - Swipes are translated into arrow key events (the default)
    1.13 - */
    1.14 -#define SDL_HINT_APPLE_TV_REMOTE_SWIPES_AS_ARROW_KEYS "SDL_APPLE_TV_REMOTE_SWIPES_AS_ARROW_KEYS"
    1.15 -
    1.16 -/**
    1.17   * \brief  A variable controlling whether the home indicator bar on iPhone X
    1.18   *         should be hidden.
    1.19   *
    1.20 @@ -378,16 +368,25 @@
    1.21  
    1.22  /**
    1.23   *  \brief  A variable controlling whether the Android / iOS built-in
    1.24 - *  accelerometer should be listed as a joystick device, rather than listing
    1.25 - *  actual joysticks only.
    1.26 + *  accelerometer should be listed as a joystick device.
    1.27   *
    1.28   *  This variable can be set to the following values:
    1.29 - *    "0"       - List only real joysticks and accept input from them
    1.30 - *    "1"       - List real joysticks along with the accelerometer as if it were a 3 axis joystick (the default).
    1.31 + *    "0"       - The accelerometer is not listed as a joystick
    1.32 + *    "1"       - The accelerometer is available as a 3 axis joystick (the default).
    1.33   */
    1.34  #define SDL_HINT_ACCELEROMETER_AS_JOYSTICK "SDL_ACCELEROMETER_AS_JOYSTICK"
    1.35  
    1.36  /**
    1.37 + *  \brief  A variable controlling whether the Android / iOS remotes
    1.38 + *  should be listed as joystick devices, instead of sending keyboard events.
    1.39 + *
    1.40 + *  This variable can be set to the following values:
    1.41 + *    "0"       - Remotes send enter/escape/arrow key events
    1.42 + *    "1"       - Remotes are available as 2 axis, 2 button joysticks (the default).
    1.43 + */
    1.44 +#define SDL_HINT_TV_REMOTE_AS_JOYSTICK "SDL_TV_REMOTE_AS_JOYSTICK"
    1.45 +
    1.46 +/**
    1.47   *  \brief  A variable that lets you disable the detection and use of Xinput gamepad devices
    1.48   *
    1.49   *  The variable can be set to the following values:
     2.1 --- a/src/joystick/android/SDL_sysjoystick.c	Tue Feb 06 15:03:35 2018 -0800
     2.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Tue Feb 06 15:03:38 2018 -0800
     2.3 @@ -80,8 +80,7 @@
     2.4  {
     2.5      /* FIXME: If this function gets too unwieldy in the future, replace with a lookup table */
     2.6      int button = 0;
     2.7 -    switch(keycode) 
     2.8 -    {
     2.9 +    switch (keycode) {
    2.10          /* Some gamepad buttons (API 9) */
    2.11          case AKEYCODE_BUTTON_A:
    2.12              button = SDL_CONTROLLER_BUTTON_A;
    2.13 @@ -179,7 +178,30 @@
    2.14       */
    2.15      SDL_assert(button < ANDROID_MAX_NBUTTONS);
    2.16      return button;
    2.17 -    
    2.18 +}
    2.19 +
    2.20 +static SDL_Scancode
    2.21 +button_to_scancode(int button)
    2.22 +{
    2.23 +    switch (button) {
    2.24 +    case SDL_CONTROLLER_BUTTON_A:
    2.25 +        return SDL_SCANCODE_RETURN;
    2.26 +    case SDL_CONTROLLER_BUTTON_B:
    2.27 +        return SDL_SCANCODE_ESCAPE;
    2.28 +    case SDL_CONTROLLER_BUTTON_BACK:
    2.29 +        return SDL_SCANCODE_ESCAPE;
    2.30 +    case SDL_CONTROLLER_BUTTON_DPAD_UP:
    2.31 +        return SDL_SCANCODE_UP;
    2.32 +    case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
    2.33 +        return SDL_SCANCODE_DOWN;
    2.34 +    case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
    2.35 +        return SDL_SCANCODE_LEFT;
    2.36 +    case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
    2.37 +        return SDL_SCANCODE_RIGHT;
    2.38 +    }
    2.39 +
    2.40 +    /* Unsupported button */
    2.41 +    return SDL_SCANCODE_UNKNOWN;
    2.42  }
    2.43  
    2.44  int
    2.45 @@ -191,6 +213,8 @@
    2.46          item = JoystickByDeviceId(device_id);
    2.47          if (item && item->joystick) {
    2.48              SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
    2.49 +        } else {
    2.50 +            SDL_SendKeyboardKey(SDL_PRESSED, button_to_scancode(button));
    2.51          }
    2.52          return 0;
    2.53      }
    2.54 @@ -207,6 +231,8 @@
    2.55          item = JoystickByDeviceId(device_id);
    2.56          if (item && item->joystick) {
    2.57              SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
    2.58 +        } else {
    2.59 +            SDL_SendKeyboardKey(SDL_RELEASED, button_to_scancode(button));
    2.60          }
    2.61          return 0;
    2.62      }
    2.63 @@ -252,8 +278,15 @@
    2.64  {
    2.65      SDL_JoystickGUID guid;
    2.66      SDL_joylist_item *item;
    2.67 +
    2.68 +    if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    2.69 +        /* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
    2.70 +        if (naxes < 2 && nhats < 1) {
    2.71 +            return -1;
    2.72 +        }
    2.73 +    }
    2.74      
    2.75 -    if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
    2.76 +    if (JoystickByDeviceId(device_id) != NULL || name == NULL) {
    2.77          return -1;
    2.78      }
    2.79      
     3.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Feb 06 15:03:35 2018 -0800
     3.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Feb 06 15:03:38 2018 -0800
     3.3 @@ -150,6 +150,15 @@
     3.4  {
     3.5      SDL_JoystickDeviceItem *device = deviceList;
     3.6  
     3.7 +#if TARGET_OS_TV
     3.8 +    if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
     3.9 +        /* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
    3.10 +        if (controller && !controller.extendedGamepad && !controller.gamepad && controller.microGamepad) {
    3.11 +            return;
    3.12 +        }
    3.13 +    }
    3.14 +#endif
    3.15 +
    3.16      while (device != NULL) {
    3.17          if (device->controller == controller) {
    3.18              return;
    3.19 @@ -157,13 +166,11 @@
    3.20          device = device->next;
    3.21      }
    3.22  
    3.23 -    device = (SDL_JoystickDeviceItem *) SDL_malloc(sizeof(SDL_JoystickDeviceItem));
    3.24 +    device = (SDL_JoystickDeviceItem *) SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
    3.25      if (device == NULL) {
    3.26          return;
    3.27      }
    3.28  
    3.29 -    SDL_zerop(device);
    3.30 -
    3.31      device->accelerometer = accelerometer;
    3.32      device->instance_id = instancecounter++;
    3.33  
    3.34 @@ -277,11 +284,11 @@
    3.35      }
    3.36  
    3.37      *device_instance = device->instance_id = instancecounter++;
    3.38 -	device->name = SDL_strdup(name);
    3.39 -	device->guid = guid;
    3.40 -	SDL_GetSteamControllerInputs(&device->nbuttons,
    3.41 -								 &device->naxes,
    3.42 -								 &device->nhats);
    3.43 +    device->name = SDL_strdup(name);
    3.44 +    device->guid = guid;
    3.45 +    SDL_GetSteamControllerInputs(&device->nbuttons,
    3.46 +                                 &device->naxes,
    3.47 +                                 &device->nhats);
    3.48      device->m_bSteamController = SDL_TRUE;
    3.49  
    3.50      if (deviceList == NULL) {
    3.51 @@ -305,10 +312,10 @@
    3.52  {
    3.53      SDL_JoystickDeviceItem *item;
    3.54  
    3.55 -	for (item = deviceList; item; item = item->next) {
    3.56 +    for (item = deviceList; item; item = item->next) {
    3.57          if (item->instance_id == device_instance) {
    3.58 -			SDL_SYS_RemoveJoystickDevice(item);
    3.59 -			break;
    3.60 +            SDL_SYS_RemoveJoystickDevice(item);
    3.61 +            break;
    3.62          }
    3.63      }
    3.64  }
    3.65 @@ -608,9 +615,6 @@
    3.66                  SDL_PrivateJoystickAxis(joystick, i, axes[i]);
    3.67              }
    3.68  
    3.69 -            /* Apparently the dpad values are not accurate enough to be useful. */
    3.70 -            /* hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad); */
    3.71 -
    3.72              Uint8 buttons[] = {
    3.73                  gamepad.buttonA.isPressed,
    3.74                  gamepad.buttonX.isPressed,
    3.75 @@ -620,8 +624,6 @@
    3.76                  updateplayerindex |= (joystick->buttons[i] != buttons[i]);
    3.77                  SDL_PrivateJoystickButton(joystick, i, buttons[i]);
    3.78              }
    3.79 -
    3.80 -            /* TODO: Figure out what to do with reportsAbsoluteDpadValues */
    3.81          }
    3.82  #endif /* TARGET_OS_TV */
    3.83  
     4.1 --- a/src/video/uikit/SDL_uikitview.m	Tue Feb 06 15:03:35 2018 -0800
     4.2 +++ b/src/video/uikit/SDL_uikitview.m	Tue Feb 06 15:03:38 2018 -0800
     4.3 @@ -44,7 +44,7 @@
     4.4  {
     4.5      if ((self = [super initWithFrame:frame])) {
     4.6  #if TARGET_OS_TV
     4.7 -        if (SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_SWIPES_AS_ARROW_KEYS, SDL_TRUE)) {
     4.8 +        if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
     4.9              /* Apple TV Remote touchpad swipe gestures. */
    4.10              UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
    4.11              swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    4.12 @@ -237,10 +237,10 @@
    4.13          return SDL_SCANCODE_RIGHT;
    4.14      case UIPressTypeSelect:
    4.15          /* HIG says: "primary button behavior" */
    4.16 -        return SDL_SCANCODE_SELECT;
    4.17 +        return SDL_SCANCODE_RETURN;
    4.18      case UIPressTypeMenu:
    4.19          /* HIG says: "returns to previous screen" */
    4.20 -        return SDL_SCANCODE_MENU;
    4.21 +        return SDL_SCANCODE_ESCAPE;
    4.22      case UIPressTypePlayPause:
    4.23          /* HIG says: "secondary button behavior" */
    4.24          return SDL_SCANCODE_PAUSE;
    4.25 @@ -251,31 +251,34 @@
    4.26  
    4.27  - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
    4.28  {
    4.29 -    for (UIPress *press in presses) {
    4.30 -        SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    4.31 -        SDL_SendKeyboardKey(SDL_PRESSED, scancode);
    4.32 -    }
    4.33 -
    4.34 +	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    4.35 +    	for (UIPress *press in presses) {
    4.36 +        	SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    4.37 +        	SDL_SendKeyboardKey(SDL_PRESSED, scancode);
    4.38 +    	}
    4.39 +	}
    4.40      [super pressesBegan:presses withEvent:event];
    4.41  }
    4.42  
    4.43  - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
    4.44  {
    4.45 -    for (UIPress *press in presses) {
    4.46 -        SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    4.47 -        SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    4.48 -    }
    4.49 -
    4.50 +	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    4.51 +		for (UIPress *press in presses) {
    4.52 +			SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    4.53 +			SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    4.54 +		}
    4.55 +	}
    4.56      [super pressesEnded:presses withEvent:event];
    4.57  }
    4.58  
    4.59  - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(UIPressesEvent *)event
    4.60  {
    4.61 -    for (UIPress *press in presses) {
    4.62 -        SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    4.63 -        SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    4.64 -    }
    4.65 -
    4.66 +	if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
    4.67 +		for (UIPress *press in presses) {
    4.68 +			SDL_Scancode scancode = [self scancodeFromPressType:press.type];
    4.69 +			SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    4.70 +		}
    4.71 +	}
    4.72      [super pressesCancelled:presses withEvent:event];
    4.73  }
    4.74