Skip to content

Commit 7ec8e4a

Browse files
committed
sinput: centralize feature initialization
Pull all processing on the data vector to the beginning of ProcessSDLFeaturesResponse to allow for reworking the feature descriptor and return a boolean to allow bailing initting the controller if the device is incompatible.
1 parent ad67b4d commit 7ec8e4a

File tree

1 file changed

+20
-33
lines changed

1 file changed

+20
-33
lines changed

src/joystick/hidapi/SDL_hidapi_sinput.c

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ static inline float CalculateAccelScale(uint16_t g_range)
233233
return SDL_STANDARD_GRAVITY / (32768.0f / (float)g_range);
234234
}
235235

236-
static void ProcessSDLFeaturesResponse(SDL_HIDAPI_Device *device, Uint8 *data)
236+
static bool ProcessSDLFeaturesResponse(SDL_HIDAPI_Device *device, Uint8 *data)
237237
{
238238
SDL_DriverSInput_Context *ctx = (SDL_DriverSInput_Context *)device->context;
239239

240240
// Obtain protocol version
241241
ctx->protocol_version = EXTRACTUINT16(data, 0);
242-
Uint8 *fflags = data + 2;
243-
Uint8 *buttons = data + 12;
244-
242+
245243
//
246244
// Unpack feature flags into context
247245
//
246+
Uint8 *fflags = data + 2;
247+
Uint8 *buttons = data + 12;
248248
ctx->rumble_supported = (fflags[0] & 0x01) != 0;
249249
ctx->player_leds_supported = (fflags[0] & 0x02) != 0;
250250
ctx->accelerometer_supported = (fflags[0] & 0x04) != 0;
@@ -272,17 +272,25 @@ static void ProcessSDLFeaturesResponse(SDL_HIDAPI_Device *device, Uint8 *data)
272272
device->guid.data[15] = data[5];
273273
ctx->sub_type = (data[5] & 0x1F);
274274

275-
#if defined(DEBUG_SINPUT_INIT)
276-
SDL_Log("SInput Face Style: %d", (data[5] & 0xE0) >> 5);
277-
SDL_Log("SInput Sub-type: %d", (data[5] & 0x1F));
278-
#endif
275+
// Get and validate touchpad parameters
276+
ctx->touchpad_count = data[16];
277+
ctx->touchpad_finger_count = data[17];
279278

280279
//
281280
// IMU Info
282281
//
283282
ctx->polling_rate_ms = data[6];
284283
ctx->accelRange = EXTRACTUINT16(data, 8);
285284
ctx->gyroRange = EXTRACTUINT16(data, 10);
285+
ctx->accelScale = CalculateAccelScale(ctx->accelRange);
286+
ctx->gyroScale = CalculateGyroScale(ctx->gyroRange);
287+
288+
// Get device Serial - MAC address
289+
Uint8 *serial = data + 18;
290+
char serial_str[18];
291+
(void)SDL_snprintf(serial_str, sizeof(serial_str), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
292+
serial[0], serial[1], serial[2], data[3], data[4], data[5]);
293+
HIDAPI_SetDeviceSerial(device, serial_str);
286294

287295
//
288296
// Get mappings based on SDL subtype and assert that they match.
@@ -418,32 +426,15 @@ static void ProcessSDLFeaturesResponse(SDL_HIDAPI_Device *device, Uint8 *data)
418426
}
419427

420428
#if defined(DEBUG_SINPUT_INIT)
429+
SDL_Log("SInput Face Style: %d", (device->guid.data[15] & 0xE0) >> 5);
430+
SDL_Log("SInput Sub-type: %d", (device->guid.data[15] & 0x1F));
421431
SDL_Log("Buttons count: %d", ctx->buttons_count);
422-
#endif
423-
424-
// Get and validate touchpad parameters
425-
ctx->touchpad_count = data[16];
426-
ctx->touchpad_finger_count = data[17];
427-
428-
// Get device Serial - MAC address
429-
char serial[18];
430-
(void)SDL_snprintf(serial, sizeof(serial), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
431-
data[18], data[19], data[20], data[21], data[22], data[23]);
432-
#if defined(DEBUG_SINPUT_INIT)
433432
SDL_Log("Serial num: %s", serial);
434-
#endif
435-
HIDAPI_SetDeviceSerial(device, serial);
436-
437-
#if defined(DEBUG_SINPUT_INIT)
438433
SDL_Log("Accelerometer Range: %d", ctx->accelRange);
439-
#endif
440-
441-
#if defined(DEBUG_SINPUT_INIT)
442434
SDL_Log("Gyro Range: %d", ctx->gyroRange);
443435
#endif
444436

445-
ctx->accelScale = CalculateAccelScale(ctx->accelRange);
446-
ctx->gyroScale = CalculateGyroScale(ctx->gyroRange);
437+
return true;
447438
}
448439

449440
static bool RetrieveSDLFeatures(SDL_HIDAPI_Device *device)
@@ -487,11 +478,7 @@ static bool RetrieveSDLFeatures(SDL_HIDAPI_Device *device)
487478
#endif
488479

489480
if ((read == USB_PACKET_LENGTH) && (data[0] == SINPUT_DEVICE_REPORT_ID_INPUT_CMDDAT) && (data[1] == SINPUT_DEVICE_COMMAND_FEATURES)) {
490-
ProcessSDLFeaturesResponse(device, &(data[SINPUT_REPORT_IDX_COMMAND_RESPONSE_BULK]));
491-
#if defined(DEBUG_SINPUT_INIT)
492-
SDL_Log("Received SInput SDL Features command response");
493-
#endif
494-
return true;
481+
return ProcessSDLFeaturesResponse(device, &(data[SINPUT_REPORT_IDX_COMMAND_RESPONSE_BULK]));
495482
}
496483
}
497484

0 commit comments

Comments
 (0)