Skip to content
Permalink
Browse files
Refactored HIDAPI controller code to support dongles and hubs that dy…
…namically attach controllers
  • Loading branch information
slouken committed Dec 19, 2019
1 parent 5d5a567 commit e7f7e3f40f2b2e603cb9d1f7629c87b865278bd1
@@ -193,10 +193,16 @@ static SDL_bool HIDAPI_DriverPS4_CanRumble(Uint16 vendor_id, Uint16 product_id)
return SDL_TRUE;
}

static int HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
static SDL_bool
HIDAPI_DriverPS4_InitDevice(SDL_HIDAPI_Device *device)
{
return HIDAPI_JoystickConnected(device, NULL);
}

static int HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);

static SDL_bool
HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_DriverPS4_Context *ctx;

@@ -205,14 +211,21 @@ HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id,
SDL_OutOfMemory();
return SDL_FALSE;
}
*context = ctx;

device->dev = hid_open_path(device->path, 0);
if (!device->dev) {
SDL_free(ctx);
SDL_SetError("Couldn't open %s", device->path);
return SDL_FALSE;
}
device->context = ctx;

/* Check for type of connection */
ctx->is_dongle = (vendor_id == SONY_USB_VID && product_id == SONY_DS4_DONGLE_PID);
ctx->is_dongle = (device->vendor_id == SONY_USB_VID && device->product_id == SONY_DS4_DONGLE_PID);
if (ctx->is_dongle) {
ctx->is_bluetooth = SDL_FALSE;
} else if (vendor_id == SONY_USB_VID) {
ctx->is_bluetooth = !CheckUSBConnected(dev);
} else if (device->vendor_id == SONY_USB_VID) {
ctx->is_bluetooth = !CheckUSBConnected(device->dev);
} else {
/* Third party controllers appear to all be wired */
ctx->is_bluetooth = SDL_FALSE;
@@ -222,12 +235,12 @@ HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id,
#endif

/* Check to see if audio is supported */
if (vendor_id == SONY_USB_VID &&
(product_id == SONY_DS4_SLIM_PID || product_id == SONY_DS4_DONGLE_PID )) {
if (device->vendor_id == SONY_USB_VID &&
(device->product_id == SONY_DS4_SLIM_PID || device->product_id == SONY_DS4_DONGLE_PID )) {
ctx->audio_supported = SDL_TRUE;
}

if (HIDAPI_DriverPS4_CanRumble(vendor_id, product_id)) {
if (HIDAPI_DriverPS4_CanRumble(device->vendor_id, device->product_id)) {
if (ctx->is_bluetooth) {
ctx->rumble_supported = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_FALSE);
} else {
@@ -236,7 +249,7 @@ HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id,
}

/* Initialize LED and effect state */
HIDAPI_DriverPS4_Rumble(joystick, dev, ctx, 0, 0, 0);
HIDAPI_DriverPS4_RumbleJoystick(device, joystick, 0, 0, 0);

/* Initialize the joystick capabilities */
joystick->nbuttons = 16;
@@ -247,9 +260,9 @@ HIDAPI_DriverPS4_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id,
}

static int
HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
HIDAPI_DriverPS4_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
{
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context;
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
DS4EffectsState_t *effects;
Uint8 data[78];
int report_size, offset;
@@ -293,7 +306,7 @@ HIDAPI_DriverPS4_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context,
SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
}

if (hid_write(dev, data, report_size) != report_size) {
if (hid_write(device->dev, data, report_size) != report_size) {
return SDL_SetError("Couldn't send rumble packet");
}

@@ -416,20 +429,28 @@ HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_
}

static SDL_bool
HIDAPI_DriverPS4_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
{
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)context;
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
SDL_Joystick *joystick = NULL;
Uint8 data[USB_PACKET_LENGTH];
int size;

while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
if (device->num_joysticks > 0) {
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
}
if (!joystick) {
return SDL_FALSE;
}

while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
switch (data[0]) {
case k_EPS4ReportIdUsbState:
HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[1]);
HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t *)&data[1]);
break;
case k_EPS4ReportIdBluetoothState:
/* Bluetooth state packets have two additional bytes at the beginning */
HIDAPI_DriverPS4_HandleStatePacket(joystick, dev, ctx, (PS4StatePacket_t *)&data[3]);
HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t *)&data[3]);
break;
default:
#ifdef DEBUG_JOYSTICK
@@ -442,17 +463,30 @@ HIDAPI_DriverPS4_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
if (ctx->rumble_expiration) {
Uint32 now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
HIDAPI_DriverPS4_Rumble(joystick, dev, context, 0, 0, 0);
HIDAPI_DriverPS4_RumbleJoystick(device, joystick, 0, 0, 0);
}
}

if (size < 0) {
/* Read error, device is disconnected */
HIDAPI_JoystickDisconnected(device, joystick->instance_id);
}
return (size >= 0);
}

static void
HIDAPI_DriverPS4_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
HIDAPI_DriverPS4_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
hid_close(device->dev);
device->dev = NULL;

SDL_free(device->context);
device->context = NULL;
}

static void
HIDAPI_DriverPS4_FreeDevice(SDL_HIDAPI_Device *device)
{
SDL_free(context);
}

SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4 =
@@ -461,10 +495,12 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4 =
SDL_TRUE,
HIDAPI_DriverPS4_IsSupportedDevice,
HIDAPI_DriverPS4_GetDeviceName,
HIDAPI_DriverPS4_Init,
HIDAPI_DriverPS4_Rumble,
HIDAPI_DriverPS4_Update,
HIDAPI_DriverPS4_Quit
HIDAPI_DriverPS4_InitDevice,
HIDAPI_DriverPS4_UpdateDevice,
HIDAPI_DriverPS4_OpenJoystick,
HIDAPI_DriverPS4_RumbleJoystick,
HIDAPI_DriverPS4_CloseJoystick,
HIDAPI_DriverPS4_FreeDevice
};

#endif /* SDL_JOYSTICK_HIDAPI_PS4 */
@@ -223,6 +223,23 @@ typedef struct {
} SDL_DriverSwitch_Context;


static SDL_bool IsGameCubeFormFactor(int vendor_id, int product_id)
{
static Uint32 gamecube_formfactor[] = {
MAKE_VIDPID(0x0e6f, 0x0185), /* PDP Wired Fight Pad Pro for Nintendo Switch */
MAKE_VIDPID(0x20d6, 0xa711), /* Core (Plus) Wired Controller */
};
Uint32 id = MAKE_VIDPID(vendor_id, product_id);
int i;

for (i = 0; i < SDL_arraysize(gamecube_formfactor); ++i) {
if (id == gamecube_formfactor[i]) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}

static SDL_bool
HIDAPI_DriverSwitch_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, const char *name)
{
@@ -604,9 +621,15 @@ static Uint8 RemapButton(SDL_DriverSwitch_Context *ctx, Uint8 button)
}
return button;
}


static SDL_bool
HIDAPI_DriverSwitch_InitDevice(SDL_HIDAPI_Device *device)
{
return HIDAPI_JoystickConnected(device, NULL);
}

static SDL_bool
HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_DriverSwitch_Context *ctx;
Uint8 input_mode;
@@ -616,15 +639,19 @@ HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_
SDL_OutOfMemory();
return SDL_FALSE;
}
ctx->dev = dev;

*context = ctx;
device->dev = ctx->dev = hid_open_path(device->path, 0);
if (!device->dev) {
SDL_SetError("Couldn't open %s", device->path);
goto error;
}
device->context = ctx;

/* Find out whether or not we can send output reports */
ctx->m_bInputOnly = SDL_IsJoystickNintendoSwitchProInputOnly(vendor_id, product_id);
ctx->m_bInputOnly = SDL_IsJoystickNintendoSwitchProInputOnly(device->vendor_id, device->product_id);
if (!ctx->m_bInputOnly) {
/* The Power A Nintendo Switch Pro controllers don't have a Home LED */
ctx->m_bHasHomeLED = (vendor_id != 0 && product_id != 0) ? SDL_TRUE : SDL_FALSE;
ctx->m_bHasHomeLED = (device->vendor_id != 0 && device->product_id != 0) ? SDL_TRUE : SDL_FALSE;

/* Initialize rumble data */
SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]);
@@ -637,14 +664,12 @@ HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_

if (!LoadStickCalibration(ctx)) {
SDL_SetError("Couldn't load stick calibration");
SDL_free(ctx);
return SDL_FALSE;
goto error;
}

if (!SetVibrationEnabled(ctx, 1)) {
SDL_SetError("Couldn't enable vibration");
SDL_free(ctx);
return SDL_FALSE;
goto error;
}

/* Set the desired input mode */
@@ -655,17 +680,15 @@ HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_
}
if (!SetInputMode(ctx, input_mode)) {
SDL_SetError("Couldn't set input mode");
SDL_free(ctx);
return SDL_FALSE;
goto error;
}

/* Start sending USB reports */
if (!ctx->m_bUsingBluetooth) {
/* ForceUSB doesn't generate an ACK, so don't wait for a reply */
if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, SDL_FALSE)) {
SDL_SetError("Couldn't start USB reports");
SDL_free(ctx);
return SDL_FALSE;
goto error;
}
}

@@ -676,7 +699,7 @@ HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_
SetSlotLED(ctx, (joystick->instance_id % 4));
}

if (vendor_id == 0x0e6f && product_id == 0x0185) {
if (IsGameCubeFormFactor(device->vendor_id, device->product_id)) {
/* This is a controller shaped like a GameCube controller, with a large central A button */
ctx->m_bUseButtonLabels = SDL_TRUE;
} else {
@@ -690,12 +713,23 @@ HIDAPI_DriverSwitch_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;

return SDL_TRUE;

error:
if (device->dev) {
hid_close(device->dev);
device->dev = NULL;
}
if (device->context) {
SDL_free(device->context);
device->context = NULL;
}
return SDL_FALSE;
}

static int
HIDAPI_DriverSwitch_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
HIDAPI_DriverSwitch_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
{
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context;
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;

/* Experimentally determined rumble values. These will only matter on some controllers as tested ones
* seem to disregard these and just use any non-zero rumble values as a binary flag for constant rumble
@@ -996,11 +1030,19 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
}

static SDL_bool
HIDAPI_DriverSwitch_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
{
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context;
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
SDL_Joystick *joystick = NULL;
int size;

if (device->num_joysticks > 0) {
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
}
if (!joystick) {
return SDL_FALSE;
}

while ((size = ReadInput(ctx)) > 0) {
if (ctx->m_bInputOnly) {
HandleInputOnlyControllerState(joystick, ctx, (SwitchInputOnlyControllerStatePacket_t *)&ctx->m_rgucReadBuffer[0]);
@@ -1021,17 +1063,21 @@ HIDAPI_DriverSwitch_Update(SDL_Joystick *joystick, hid_device *dev, void *contex
if (ctx->m_nRumbleExpiration) {
Uint32 now = SDL_GetTicks();
if (SDL_TICKS_PASSED(now, ctx->m_nRumbleExpiration)) {
HIDAPI_DriverSwitch_Rumble(joystick, dev, context, 0, 0, 0);
HIDAPI_DriverSwitch_RumbleJoystick(device, joystick, 0, 0, 0);
}
}

if (size < 0) {
/* Read error, device is disconnected */
HIDAPI_JoystickDisconnected(device, joystick->instance_id);
}
return (size >= 0);
}

static void
HIDAPI_DriverSwitch_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
HIDAPI_DriverSwitch_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)context;
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;

if (!ctx->m_bInputOnly) {
/* Restore simple input mode for other applications */
@@ -1041,7 +1087,16 @@ HIDAPI_DriverSwitch_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
SDL_GameControllerButtonReportingHintChanged, ctx);

SDL_free(context);
hid_close(device->dev);
device->dev = NULL;

SDL_free(device->context);
device->context = NULL;
}

static void
HIDAPI_DriverSwitch_FreeDevice(SDL_HIDAPI_Device *device)
{
}

SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch =
@@ -1050,10 +1105,12 @@ SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch =
SDL_TRUE,
HIDAPI_DriverSwitch_IsSupportedDevice,
HIDAPI_DriverSwitch_GetDeviceName,
HIDAPI_DriverSwitch_Init,
HIDAPI_DriverSwitch_Rumble,
HIDAPI_DriverSwitch_Update,
HIDAPI_DriverSwitch_Quit
HIDAPI_DriverSwitch_InitDevice,
HIDAPI_DriverSwitch_UpdateDevice,
HIDAPI_DriverSwitch_OpenJoystick,
HIDAPI_DriverSwitch_RumbleJoystick,
HIDAPI_DriverSwitch_CloseJoystick,
HIDAPI_DriverSwitch_FreeDevice
};

#endif /* SDL_JOYSTICK_HIDAPI_SWITCH */

0 comments on commit e7f7e3f

Please sign in to comment.