Fixed compiling HIDAPI Steam Controller support with C89 compiler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
diff --git a/src/joystick/hidapi/SDL_hidapi_steam.c b/src/joystick/hidapi/SDL_hidapi_steam.c
index cf8463f..b4ba97c 100644
--- a/src/joystick/hidapi/SDL_hidapi_steam.c
+++ b/src/joystick/hidapi/SDL_hidapi_steam.c
@@ -239,6 +239,9 @@ static int WriteSegmentToSteamControllerPacketAssembler( SteamControllerPacketAs
{
if ( pAssembler->bIsBle )
{
+ uint8_t uSegmentHeader = pSegment[ 1 ];
+ int nSegmentNumber = uSegmentHeader & 0x07;
+
HEXDUMP( pSegment, nSegmentLength );
if ( pSegment[ 0 ] != BLE_REPORT_NUMBER )
@@ -255,7 +258,6 @@ static int WriteSegmentToSteamControllerPacketAssembler( SteamControllerPacketAs
return -1;
}
- uint8_t uSegmentHeader = pSegment[ 1 ];
DPRINTF("GOT PACKET HEADER = 0x%x\n", uSegmentHeader);
if ( ( uSegmentHeader & REPORT_SEGMENT_DATA_FLAG ) == 0 )
@@ -264,7 +266,6 @@ static int WriteSegmentToSteamControllerPacketAssembler( SteamControllerPacketAs
return 0;
}
- int nSegmentNumber = uSegmentHeader & 0x07;
if ( nSegmentNumber != pAssembler->nExpectedSegmentNumber )
{
ResetSteamControllerPacketAssembler( pAssembler );
@@ -306,20 +307,21 @@ static int WriteSegmentToSteamControllerPacketAssembler( SteamControllerPacketAs
static int SetFeatureReport( hid_device *dev, unsigned char uBuffer[65], int nActualDataLen )
{
- DPRINTF("SetFeatureReport %p %p %d\n", dev, uBuffer, nActualDataLen);
int nRet = -1;
bool bBle = true; // only wireless/BLE for now, though macOS could do wired in the future
+ DPRINTF("SetFeatureReport %p %p %d\n", dev, uBuffer, nActualDataLen);
+
if ( bBle )
{
- if ( nActualDataLen < 1 )
- return -1;
-
int nSegmentNumber = 0;
uint8_t uPacketBuffer[ MAX_REPORT_SEGMENT_SIZE ];
+ unsigned char *pBufferPtr = uBuffer + 1;
+
+ if ( nActualDataLen < 1 )
+ return -1;
// Skip report number in data
- unsigned char *pBufferPtr = uBuffer + 1;
nActualDataLen--;
while ( nActualDataLen > 0 )
@@ -347,17 +349,19 @@ static int SetFeatureReport( hid_device *dev, unsigned char uBuffer[65], int nAc
static int GetFeatureReport( hid_device *dev, unsigned char uBuffer[65] )
{
- DPRINTF("GetFeatureReport( %p %p )\n", dev, uBuffer );
int nRet = -1;
bool bBle = true;
+ DPRINTF("GetFeatureReport( %p %p )\n", dev, uBuffer );
+
if ( bBle )
{
+ int nRetries = 0;
+ uint8_t uSegmentBuffer[ MAX_REPORT_SEGMENT_SIZE ];
+
SteamControllerPacketAssembler assembler;
InitializeSteamControllerPacketAssembler( &assembler );
- int nRetries = 0;
- uint8_t uSegmentBuffer[ MAX_REPORT_SEGMENT_SIZE ];
while( nRetries < BLE_MAX_READ_RETRIES )
{
memset( uSegmentBuffer, 0, sizeof( uSegmentBuffer ) );
@@ -398,9 +402,10 @@ static int GetFeatureReport( hid_device *dev, unsigned char uBuffer[65] )
static int ReadResponse( hid_device *dev, uint8_t uBuffer[65], int nExpectedResponse )
{
- DPRINTF("ReadResponse( %p %p %d )\n", dev, uBuffer, nExpectedResponse );
int nRet = GetFeatureReport( dev, uBuffer );
+ DPRINTF("ReadResponse( %p %p %d )\n", dev, uBuffer, nExpectedResponse );
+
if ( nRet < 0 )
return nRet;
@@ -418,11 +423,14 @@ static int ReadResponse( hid_device *dev, uint8_t uBuffer[65], int nExpectedResp
//---------------------------------------------------------------------------
static bool ResetSteamController( hid_device *dev, bool bSuppressErrorSpew )
{
- DPRINTF( "ResetSteamController hid=%p\n", dev );
// Firmware quirk: Set Feature and Get Feature requests always require a 65-byte buffer.
unsigned char buf[65];
int res = -1;
+ int nSettings = 0;
+ int nAttributesLength;
+ DPRINTF( "ResetSteamController hid=%p\n", dev );
+
buf[0] = 0;
buf[1] = ID_GET_ATTRIBUTES_VALUES;
res = SetFeatureReport( dev, buf, 2 );
@@ -444,7 +452,7 @@ static bool ResetSteamController( hid_device *dev, bool bSuppressErrorSpew )
return false;
}
- int nAttributesLength = buf[ 2 ];
+ nAttributesLength = buf[ 2 ];
if ( nAttributesLength > res )
{
if ( !bSuppressErrorSpew )
@@ -476,7 +484,6 @@ static bool ResetSteamController( hid_device *dev, bool bSuppressErrorSpew )
}
// Apply custom settings - clear trackpad modes (cancel mouse emulation), etc
- int nSettings = 0;
#define ADD_SETTING(SETTING, VALUE) \
buf[3+nSettings*3] = SETTING; \
buf[3+nSettings*3+1] = ((uint16_t)VALUE)&0xFF; \
@@ -651,6 +658,15 @@ static void RotatePadShort( short *pX, short *pY, float flAngleInRad )
//---------------------------------------------------------------------------
static void FormatStatePacketUntilGyro( SteamControllerStateInternal_t *pState, ValveControllerStatePacket_t *pStatePacket )
{
+ int nLeftPadX;
+ int nLeftPadY;
+ int nRightPadX;
+ int nRightPadY;
+ int nPadOffset;
+
+ // 15 degrees in rad
+ const float flRotationAngle = 0.261799f;
+
memset(pState, 0, offsetof(SteamControllerStateInternal_t, sBatteryLevel));
//pState->eControllerType = m_eControllerType;
@@ -735,18 +751,14 @@ static void FormatStatePacketUntilGyro( SteamControllerStateInternal_t *pState,
pState->sRightPadX = pStatePacket->sRightPadX;
pState->sRightPadY = pStatePacket->sRightPadY;
- int nLeftPadX = pState->sLeftPadX;
- int nLeftPadY = pState->sLeftPadY;
- int nRightPadX = pState->sRightPadX;
- int nRightPadY = pState->sRightPadY;
-
- // 15 degrees in rad
- const float flRotationAngle = 0.261799f;
+ nLeftPadX = pState->sLeftPadX;
+ nLeftPadY = pState->sLeftPadY;
+ nRightPadX = pState->sRightPadX;
+ nRightPadY = pState->sRightPadY;
RotatePad(&nLeftPadX, &nLeftPadY, -flRotationAngle);
RotatePad(&nRightPadX, &nRightPadY, flRotationAngle);
- int nPadOffset;
if (pState->ulButtons & STEAM_LEFTPAD_FINGERDOWN_MASK)
nPadOffset = 1000;
else
@@ -1002,6 +1014,7 @@ HIDAPI_DriverSteam_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
SDL_SetError("Couldn't open %s", device->path);
goto error;
}
+ hid_set_nonblocking(device->dev, 1);
if (!ResetSteamController(device->dev, false)) {
goto error;