Delay delivery of the pause button release on MFI controllers so it doesn't happen in the same frame as the button press
authorSam Lantinga <slouken@libsdl.org>
Thu, 08 Mar 2018 16:32:22 -0800
changeset 11925ddb8dcdaa8d0
parent 11924 6aade58208aa
child 11926 32b708d2f684
Delay delivery of the pause button release on MFI controllers so it doesn't happen in the same frame as the button press
src/joystick/iphoneos/SDL_sysjoystick.m
src/joystick/iphoneos/SDL_sysjoystick_c.h
     1.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Wed Mar 07 18:10:01 2018 -0800
     1.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Thu Mar 08 16:32:22 2018 -0800
     1.3 @@ -31,6 +31,7 @@
     1.4  #include "SDL_joystick.h"
     1.5  #include "SDL_hints.h"
     1.6  #include "SDL_stdinc.h"
     1.7 +#include "SDL_timer.h"
     1.8  #include "../SDL_sysjoystick.h"
     1.9  #include "../SDL_joystick_c.h"
    1.10  #include "../steam/SDL_steamcontroller.h"
    1.11 @@ -84,12 +85,12 @@
    1.12  {
    1.13  #ifdef SDL_JOYSTICK_MFI
    1.14      const Uint16 BUS_BLUETOOTH = 0x05;
    1.15 -	const Uint16 VENDOR_APPLE = 0x05AC;
    1.16 +    const Uint16 VENDOR_APPLE = 0x05AC;
    1.17      Uint16 *guid16 = (Uint16 *)device->guid.data;
    1.18 -	Uint16 vendor = 0;
    1.19 -	Uint16 product = 0;
    1.20 -	Uint16 version = 0;
    1.21 -	Uint8 subtype = 0;
    1.22 +    Uint16 vendor = 0;
    1.23 +    Uint16 product = 0;
    1.24 +    Uint16 version = 0;
    1.25 +    Uint8 subtype = 0;
    1.26  
    1.27      const char *name = NULL;
    1.28      /* Explicitly retain the controller because SDL_JoystickDeviceItem is a
    1.29 @@ -107,25 +108,25 @@
    1.30      device->name = SDL_strdup(name);
    1.31  
    1.32      if (controller.extendedGamepad) {
    1.33 -		vendor = VENDOR_APPLE;
    1.34 -		product = 1;
    1.35 -		subtype = 1;
    1.36 +        vendor = VENDOR_APPLE;
    1.37 +        product = 1;
    1.38 +        subtype = 1;
    1.39          device->naxes = 6; /* 2 thumbsticks and 2 triggers */
    1.40          device->nhats = 1; /* d-pad */
    1.41          device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
    1.42      } else if (controller.gamepad) {
    1.43 -		vendor = VENDOR_APPLE;
    1.44 -		product = 2;
    1.45 -		subtype = 2;
    1.46 +        vendor = VENDOR_APPLE;
    1.47 +        product = 2;
    1.48 +        subtype = 2;
    1.49          device->naxes = 0; /* no traditional analog inputs */
    1.50          device->nhats = 1; /* d-pad */
    1.51          device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
    1.52      }
    1.53  #if TARGET_OS_TV
    1.54      else if (controller.microGamepad) {
    1.55 -		vendor = VENDOR_APPLE;
    1.56 -		product = 3;
    1.57 -		subtype = 3;
    1.58 +        vendor = VENDOR_APPLE;
    1.59 +        product = 3;
    1.60 +        subtype = 3;
    1.61          device->naxes = 2; /* treat the touch surface as two axes */
    1.62          device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
    1.63          device->nbuttons = 3; /* AX, pause button */
    1.64 @@ -138,16 +139,16 @@
    1.65      /* Byteswap so devices get same GUID on little/big endian platforms. */
    1.66      *guid16++ = SDL_SwapLE16(BUS_BLUETOOTH);
    1.67      *guid16++ = 0;
    1.68 -	*guid16++ = SDL_SwapLE16(vendor);
    1.69 -	*guid16++ = 0;
    1.70 -	*guid16++ = SDL_SwapLE16(product);
    1.71 -	*guid16++ = 0;
    1.72 -	*guid16++ = SDL_SwapLE16(version);
    1.73 -	*guid16++ = 0;
    1.74 +    *guid16++ = SDL_SwapLE16(vendor);
    1.75 +    *guid16++ = 0;
    1.76 +    *guid16++ = SDL_SwapLE16(product);
    1.77 +    *guid16++ = 0;
    1.78 +    *guid16++ = SDL_SwapLE16(version);
    1.79 +    *guid16++ = 0;
    1.80  
    1.81 -	/* Note that this is an MFI controller and what subtype it is */
    1.82 -	device->guid.data[14] = 'm';
    1.83 -	device->guid.data[15] = subtype;
    1.84 +    /* Note that this is an MFI controller and what subtype it is */
    1.85 +    device->guid.data[14] = 'm';
    1.86 +    device->guid.data[15] = subtype;
    1.87  
    1.88      /* This will be set when the first button press of the controller is
    1.89       * detected. */
    1.90 @@ -559,6 +560,8 @@
    1.91          Uint8 hatstate = SDL_HAT_CENTERED;
    1.92          int i;
    1.93          int updateplayerindex = 0;
    1.94 +        const Uint8 pausebutton = joystick->nbuttons - 1; /* The pause button is always last. */
    1.95 +        const Uint32 PAUSE_RELEASE_DELAY_MS = 100;
    1.96  
    1.97          if (controller.extendedGamepad) {
    1.98              GCExtendedGamepad *gamepad = controller.extendedGamepad;
    1.99 @@ -647,16 +650,20 @@
   1.100          }
   1.101  
   1.102          for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
   1.103 -            /* The pause button is always last. */
   1.104 -            Uint8 pausebutton = joystick->nbuttons - 1;
   1.105 -
   1.106              SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
   1.107 -            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
   1.108 -
   1.109 +            joystick->hwdata->pause_button_down_time = SDL_GetTicks();
   1.110 +            if (!joystick->hwdata->pause_button_down_time) {
   1.111 +                joystick->hwdata->pause_button_down_time = 1;
   1.112 +            }
   1.113              updateplayerindex = YES;
   1.114          }
   1.115 +        joystick->hwdata->num_pause_presses = 0;
   1.116  
   1.117 -        joystick->hwdata->num_pause_presses = 0;
   1.118 +        if (joystick->hwdata->pause_button_down_time &&
   1.119 +            SDL_TICKS_PASSED(SDL_GetTicks(), joystick->hwdata->pause_button_down_time + PAUSE_RELEASE_DELAY_MS)) {
   1.120 +            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
   1.121 +            joystick->hwdata->pause_button_down_time = 0;
   1.122 +        }
   1.123  
   1.124          if (updateplayerindex && controller.playerIndex == -1) {
   1.125              BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
     2.1 --- a/src/joystick/iphoneos/SDL_sysjoystick_c.h	Wed Mar 07 18:10:01 2018 -0800
     2.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick_c.h	Thu Mar 08 16:32:22 2018 -0800
     2.3 @@ -35,6 +35,7 @@
     2.4  
     2.5      GCController __unsafe_unretained *controller;
     2.6      int num_pause_presses;
     2.7 +    Uint32 pause_button_down_time;
     2.8  
     2.9      char *name;
    2.10      SDL_Joystick *joystick;