early-access version 1667
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -32,13 +32,18 @@
|
||||
#include "../SDL_sysjoystick.h"
|
||||
#include "SDL_hidapijoystick_c.h"
|
||||
#include "SDL_hidapi_rumble.h"
|
||||
#include "../../hidapi/SDL_hidapi.h"
|
||||
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
|
||||
|
||||
/* Define this if you want to log all packets from the controller */
|
||||
/*#define DEBUG_GAMECUBE_PROTOCOL*/
|
||||
|
||||
#define MAX_CONTROLLERS 4
|
||||
|
||||
typedef struct {
|
||||
SDL_bool pc_mode;
|
||||
SDL_JoystickID joysticks[MAX_CONTROLLERS];
|
||||
Uint8 wireless[MAX_CONTROLLERS];
|
||||
Uint8 min_axis[MAX_CONTROLLERS*SDL_CONTROLLER_AXIS_MAX];
|
||||
@@ -57,6 +62,10 @@ HIDAPI_DriverGameCube_IsSupportedDevice(const char *name, SDL_GameControllerType
|
||||
/* Nintendo Co., Ltd. Wii U GameCube Controller Adapter */
|
||||
return SDL_TRUE;
|
||||
}
|
||||
if (vendor_id == USB_VENDOR_SHENZHEN && product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER) {
|
||||
/* EVORETRO GameCube Controller Adapter */
|
||||
return SDL_TRUE;
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
@@ -77,25 +86,6 @@ ResetAxisRange(SDL_DriverGameCube_Context *ctx, int joystick_index)
|
||||
ctx->min_axis[joystick_index*SDL_CONTROLLER_AXIS_MAX+SDL_CONTROLLER_AXIS_TRIGGERRIGHT] = 40;
|
||||
}
|
||||
|
||||
static float fsel(float fComparand, float fValGE, float fLT)
|
||||
{
|
||||
return fComparand >= 0 ? fValGE : fLT;
|
||||
}
|
||||
|
||||
static float RemapVal(float val, float A, float B, float C, float D)
|
||||
{
|
||||
if (A == B) {
|
||||
return fsel(val - B , D , C);
|
||||
}
|
||||
if (val < A) {
|
||||
val = A;
|
||||
}
|
||||
if (val > B) {
|
||||
val = B;
|
||||
}
|
||||
return C + (D - C) * (val - A) / (B - A);
|
||||
}
|
||||
|
||||
static void SDLCALL SDL_GameControllerButtonReportingHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
|
||||
{
|
||||
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)userdata;
|
||||
@@ -129,6 +119,10 @@ HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
|
||||
Uint8 initMagic = 0x13;
|
||||
Uint8 rumbleMagic = 0x11;
|
||||
|
||||
#ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
|
||||
SDL_EnableGameCubeAdaptors();
|
||||
#endif
|
||||
|
||||
ctx = (SDL_DriverGameCube_Context *)SDL_calloc(1, sizeof(*ctx));
|
||||
if (!ctx) {
|
||||
SDL_OutOfMemory();
|
||||
@@ -149,40 +143,54 @@ HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
|
||||
ctx->joysticks[3] = -1;
|
||||
ctx->rumble[0] = rumbleMagic;
|
||||
|
||||
/* This is all that's needed to initialize the device. Really! */
|
||||
if (hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {
|
||||
SDL_SetError("Couldn't initialize WUP-028");
|
||||
goto error;
|
||||
if (device->vendor_id != USB_VENDOR_NINTENDO) {
|
||||
ctx->pc_mode = SDL_TRUE;
|
||||
}
|
||||
|
||||
/* Wait for the adapter to initialize */
|
||||
SDL_Delay(10);
|
||||
|
||||
/* Add all the applicable joysticks */
|
||||
while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
|
||||
if (size < 37 || packet[0] != 0x21) {
|
||||
continue; /* Nothing to do yet...? */
|
||||
if (ctx->pc_mode) {
|
||||
for (i = 0; i < MAX_CONTROLLERS; ++i) {
|
||||
ResetAxisRange(ctx, i);
|
||||
HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
|
||||
}
|
||||
} else {
|
||||
/* This is all that's needed to initialize the device. Really! */
|
||||
if (hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {
|
||||
SDL_SetError("Couldn't initialize WUP-028");
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* Go through all 4 slots */
|
||||
curSlot = packet + 1;
|
||||
for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
|
||||
ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
|
||||
/* Wait for the adapter to initialize */
|
||||
SDL_Delay(10);
|
||||
|
||||
/* Only allow rumble if the adapter's second USB cable is connected */
|
||||
ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
|
||||
/* Add all the applicable joysticks */
|
||||
while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
|
||||
#ifdef DEBUG_GAMECUBE_PROTOCOL
|
||||
HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
|
||||
#endif
|
||||
if (size < 37 || packet[0] != 0x21) {
|
||||
continue; /* Nothing to do yet...? */
|
||||
}
|
||||
|
||||
if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
|
||||
if (ctx->joysticks[i] == -1) {
|
||||
ResetAxisRange(ctx, i);
|
||||
HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
|
||||
/* Go through all 4 slots */
|
||||
curSlot = packet + 1;
|
||||
for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
|
||||
ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
|
||||
|
||||
/* Only allow rumble if the adapter's second USB cable is connected */
|
||||
ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
|
||||
|
||||
if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
|
||||
if (ctx->joysticks[i] == -1) {
|
||||
ResetAxisRange(ctx, i);
|
||||
HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
|
||||
}
|
||||
} else {
|
||||
if (ctx->joysticks[i] != -1) {
|
||||
HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
|
||||
ctx->joysticks[i] = -1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (ctx->joysticks[i] != -1) {
|
||||
HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
|
||||
ctx->joysticks[i] = -1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -193,14 +201,19 @@ HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
|
||||
return SDL_TRUE;
|
||||
|
||||
error:
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
if (device->context) {
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
if (device->context) {
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
@@ -223,90 +236,167 @@ HIDAPI_DriverGameCube_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_Joysti
|
||||
{
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverGameCube_HandleJoystickPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)
|
||||
{
|
||||
SDL_Joystick *joystick;
|
||||
Uint8 i, v;
|
||||
Sint16 axis_value;
|
||||
|
||||
if (size != 10) {
|
||||
return; /* How do we handle this packet? */
|
||||
}
|
||||
|
||||
i = packet[0] - 1;
|
||||
if (i >= MAX_CONTROLLERS) {
|
||||
return; /* How do we handle this packet? */
|
||||
}
|
||||
|
||||
joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
|
||||
if (!joystick) {
|
||||
/* Hasn't been opened yet, skip */
|
||||
return;
|
||||
}
|
||||
|
||||
#define READ_BUTTON(off, flag, button) \
|
||||
SDL_PrivateJoystickButton( \
|
||||
joystick, \
|
||||
RemapButton(ctx, button), \
|
||||
(packet[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
|
||||
);
|
||||
READ_BUTTON(1, 0x02, 0) /* A */
|
||||
READ_BUTTON(1, 0x04, 1) /* B */
|
||||
READ_BUTTON(1, 0x01, 2) /* X */
|
||||
READ_BUTTON(1, 0x08, 3) /* Y */
|
||||
READ_BUTTON(2, 0x80, 4) /* DPAD_LEFT */
|
||||
READ_BUTTON(2, 0x20, 5) /* DPAD_RIGHT */
|
||||
READ_BUTTON(2, 0x40, 6) /* DPAD_DOWN */
|
||||
READ_BUTTON(2, 0x10, 7) /* DPAD_UP */
|
||||
READ_BUTTON(2, 0x02, 8) /* START */
|
||||
READ_BUTTON(1, 0x80, 9) /* RIGHTSHOULDER */
|
||||
/* These two buttons are for the bottoms of the analog triggers.
|
||||
* More than likely, you're going to want to read the axes instead!
|
||||
* -flibit
|
||||
*/
|
||||
READ_BUTTON(1, 0x20, 10) /* TRIGGERRIGHT */
|
||||
READ_BUTTON(1, 0x10, 11) /* TRIGGERLEFT */
|
||||
#undef READ_BUTTON
|
||||
|
||||
#define READ_AXIS(off, axis, invert) \
|
||||
v = invert ? (0xff - packet[off]) : packet[off]; \
|
||||
if (v < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = v; \
|
||||
if (v > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = v; \
|
||||
axis_value = (Sint16)HIDAPI_RemapVal(v, ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \
|
||||
SDL_PrivateJoystickAxis( \
|
||||
joystick, \
|
||||
axis, axis_value \
|
||||
);
|
||||
READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX, 0)
|
||||
READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY, 0)
|
||||
READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTX, 1)
|
||||
READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTY, 1)
|
||||
READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0)
|
||||
READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0)
|
||||
#undef READ_AXIS
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)
|
||||
{
|
||||
SDL_Joystick *joystick;
|
||||
Uint8 *curSlot;
|
||||
Uint8 i;
|
||||
Sint16 axis_value;
|
||||
|
||||
if (size < 37 || packet[0] != 0x21) {
|
||||
return; /* Nothing to do right now...? */
|
||||
}
|
||||
|
||||
/* Go through all 4 slots */
|
||||
curSlot = packet + 1;
|
||||
for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
|
||||
ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
|
||||
|
||||
/* Only allow rumble if the adapter's second USB cable is connected */
|
||||
ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
|
||||
|
||||
if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
|
||||
if (ctx->joysticks[i] == -1) {
|
||||
ResetAxisRange(ctx, i);
|
||||
HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
|
||||
}
|
||||
joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
|
||||
|
||||
/* Hasn't been opened yet, skip */
|
||||
if (joystick == NULL) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (ctx->joysticks[i] != -1) {
|
||||
HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
|
||||
ctx->joysticks[i] = -1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
#define READ_BUTTON(off, flag, button) \
|
||||
SDL_PrivateJoystickButton( \
|
||||
joystick, \
|
||||
RemapButton(ctx, button), \
|
||||
(curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
|
||||
);
|
||||
READ_BUTTON(1, 0x01, 0) /* A */
|
||||
READ_BUTTON(1, 0x04, 1) /* B */
|
||||
READ_BUTTON(1, 0x02, 2) /* X */
|
||||
READ_BUTTON(1, 0x08, 3) /* Y */
|
||||
READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */
|
||||
READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */
|
||||
READ_BUTTON(1, 0x40, 6) /* DPAD_DOWN */
|
||||
READ_BUTTON(1, 0x80, 7) /* DPAD_UP */
|
||||
READ_BUTTON(2, 0x01, 8) /* START */
|
||||
READ_BUTTON(2, 0x02, 9) /* RIGHTSHOULDER */
|
||||
/* These two buttons are for the bottoms of the analog triggers.
|
||||
* More than likely, you're going to want to read the axes instead!
|
||||
* -flibit
|
||||
*/
|
||||
READ_BUTTON(2, 0x04, 10) /* TRIGGERRIGHT */
|
||||
READ_BUTTON(2, 0x08, 11) /* TRIGGERLEFT */
|
||||
#undef READ_BUTTON
|
||||
|
||||
#define READ_AXIS(off, axis) \
|
||||
if (curSlot[off] < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
|
||||
if (curSlot[off] > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
|
||||
axis_value = (Sint16)HIDAPI_RemapVal(curSlot[off], ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \
|
||||
SDL_PrivateJoystickAxis( \
|
||||
joystick, \
|
||||
axis, axis_value \
|
||||
);
|
||||
READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX)
|
||||
READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY)
|
||||
READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTX)
|
||||
READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTY)
|
||||
READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT)
|
||||
READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
|
||||
#undef READ_AXIS
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
|
||||
SDL_Joystick *joystick;
|
||||
Uint8 packet[37];
|
||||
Uint8 *curSlot;
|
||||
Uint8 i;
|
||||
Sint16 axis_value;
|
||||
Uint8 packet[USB_PACKET_LENGTH];
|
||||
int size;
|
||||
|
||||
/* Read input packet */
|
||||
while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
|
||||
if (size < 37 || packet[0] != 0x21) {
|
||||
continue; /* Nothing to do right now...? */
|
||||
}
|
||||
|
||||
/* Go through all 4 slots */
|
||||
curSlot = packet + 1;
|
||||
for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
|
||||
ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
|
||||
|
||||
/* Only allow rumble if the adapter's second USB cable is connected */
|
||||
ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
|
||||
|
||||
if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
|
||||
if (ctx->joysticks[i] == -1) {
|
||||
ResetAxisRange(ctx, i);
|
||||
HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
|
||||
}
|
||||
joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
|
||||
|
||||
/* Hasn't been opened yet, skip */
|
||||
if (joystick == NULL) {
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (ctx->joysticks[i] != -1) {
|
||||
HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
|
||||
ctx->joysticks[i] = -1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
#define READ_BUTTON(off, flag, button) \
|
||||
SDL_PrivateJoystickButton( \
|
||||
joystick, \
|
||||
RemapButton(ctx, button), \
|
||||
(curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
|
||||
);
|
||||
READ_BUTTON(1, 0x01, 0) /* A */
|
||||
READ_BUTTON(1, 0x04, 1) /* B */
|
||||
READ_BUTTON(1, 0x02, 2) /* X */
|
||||
READ_BUTTON(1, 0x08, 3) /* Y */
|
||||
READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */
|
||||
READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */
|
||||
READ_BUTTON(1, 0x40, 6) /* DPAD_DOWN */
|
||||
READ_BUTTON(1, 0x80, 7) /* DPAD_UP */
|
||||
READ_BUTTON(2, 0x01, 8) /* START */
|
||||
READ_BUTTON(2, 0x02, 9) /* RIGHTSHOULDER */
|
||||
/* These two buttons are for the bottoms of the analog triggers.
|
||||
* More than likely, you're going to want to read the axes instead!
|
||||
* -flibit
|
||||
*/
|
||||
READ_BUTTON(2, 0x04, 10) /* TRIGGERRIGHT */
|
||||
READ_BUTTON(2, 0x08, 11) /* TRIGGERLEFT */
|
||||
#undef READ_BUTTON
|
||||
|
||||
#define READ_AXIS(off, axis) \
|
||||
if (axis < SDL_CONTROLLER_AXIS_TRIGGERLEFT) \
|
||||
if (curSlot[off] < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
|
||||
if (curSlot[off] > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
|
||||
axis_value = (Sint16)(RemapVal(curSlot[off], ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16)); \
|
||||
SDL_PrivateJoystickAxis( \
|
||||
joystick, \
|
||||
axis, axis_value \
|
||||
);
|
||||
READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX)
|
||||
READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY)
|
||||
READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTX)
|
||||
READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTY)
|
||||
READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT)
|
||||
READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
|
||||
#undef READ_AXIS
|
||||
#ifdef DEBUG_GAMECUBE_PROTOCOL
|
||||
//HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
|
||||
#endif
|
||||
if (ctx->pc_mode) {
|
||||
HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, packet, size);
|
||||
} else {
|
||||
HIDAPI_DriverGameCube_HandleNintendoPacket(device, ctx, packet, size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -341,6 +431,11 @@ HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *jo
|
||||
{
|
||||
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
|
||||
Uint8 i, val;
|
||||
|
||||
if (ctx->pc_mode) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
for (i = 0; i < MAX_CONTROLLERS; i += 1) {
|
||||
if (joystick->instance_id == ctx->joysticks[i]) {
|
||||
if (ctx->wireless[i]) {
|
||||
@@ -404,14 +499,18 @@ HIDAPI_DriverGameCube_FreeDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
|
||||
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
|
||||
SDL_GameControllerButtonReportingHintChanged, ctx);
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube =
|
||||
|
153
externals/SDL/src/joystick/hidapi/SDL_hidapi_ps4.c
vendored
153
externals/SDL/src/joystick/hidapi/SDL_hidapi_ps4.c
vendored
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "SDL_timer.h"
|
||||
#include "SDL_joystick.h"
|
||||
#include "SDL_gamecontroller.h"
|
||||
#include "../../SDL_hints_c.h"
|
||||
#include "../SDL_sysjoystick.h"
|
||||
#include "SDL_hidapijoystick_c.h"
|
||||
#include "SDL_hidapi_rumble.h"
|
||||
@@ -45,6 +46,7 @@
|
||||
|
||||
#define GYRO_RES_PER_DEGREE 1024.0f
|
||||
#define ACCEL_RES_PER_G 8192.0f
|
||||
#define BLUETOOTH_DISCONNECT_TIMEOUT_MS 500
|
||||
|
||||
#define LOAD16(A, B) (Sint16)((Uint16)(A) | (((Uint16)(B)) << 8))
|
||||
|
||||
@@ -119,14 +121,18 @@ typedef struct {
|
||||
} IMUCalibrationData;
|
||||
|
||||
typedef struct {
|
||||
SDL_HIDAPI_Device *device;
|
||||
SDL_Joystick *joystick;
|
||||
SDL_bool is_dongle;
|
||||
SDL_bool is_bluetooth;
|
||||
SDL_bool official_controller;
|
||||
SDL_bool audio_supported;
|
||||
SDL_bool effects_supported;
|
||||
SDL_bool enhanced_mode;
|
||||
SDL_bool report_sensors;
|
||||
SDL_bool hardware_calibration;
|
||||
IMUCalibrationData calibration[6];
|
||||
Uint32 last_packet;
|
||||
int player_index;
|
||||
Uint8 rumble_left;
|
||||
Uint8 rumble_right;
|
||||
@@ -387,6 +393,10 @@ HIDAPI_DriverPS4_UpdateEffects(SDL_HIDAPI_Device *device)
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
if (!ctx->enhanced_mode) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
SDL_zero(data);
|
||||
|
||||
if (ctx->is_bluetooth) {
|
||||
@@ -432,6 +442,46 @@ HIDAPI_DriverPS4_UpdateEffects(SDL_HIDAPI_Device *device)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverPS4_TickleBluetooth(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
/* This is just a dummy packet that should have no effect, since we don't set the CRC */
|
||||
Uint8 data[78];
|
||||
|
||||
SDL_zero(data);
|
||||
|
||||
data[0] = k_EPS4ReportIdBluetoothEffects;
|
||||
data[1] = 0xC0; /* Magic value HID + CRC */
|
||||
|
||||
SDL_HIDAPI_SendRumble(device, data, sizeof(data));
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverPS4_SetEnhancedMode(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
|
||||
|
||||
if (!ctx->enhanced_mode) {
|
||||
ctx->enhanced_mode = SDL_TRUE;
|
||||
|
||||
SDL_PrivateJoystickAddTouchpad(joystick, 2);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL);
|
||||
|
||||
HIDAPI_DriverPS4_UpdateEffects(device);
|
||||
}
|
||||
}
|
||||
|
||||
static void SDLCALL SDL_PS4RumbleHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
|
||||
{
|
||||
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)userdata;
|
||||
|
||||
/* This is a one-way trip, you can't switch the controller back to simple report mode */
|
||||
if (SDL_GetStringBoolean(hint, SDL_FALSE)) {
|
||||
HIDAPI_DriverPS4_SetEnhancedMode(ctx->device, ctx->joystick);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverPS4_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
|
||||
{
|
||||
@@ -451,12 +501,16 @@ static SDL_bool
|
||||
HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_DriverPS4_Context *ctx;
|
||||
SDL_bool enhanced_mode = SDL_FALSE;
|
||||
|
||||
ctx = (SDL_DriverPS4_Context *)SDL_calloc(1, sizeof(*ctx));
|
||||
if (!ctx) {
|
||||
SDL_OutOfMemory();
|
||||
return SDL_FALSE;
|
||||
}
|
||||
ctx->device = device;
|
||||
ctx->joystick = joystick;
|
||||
ctx->last_packet = SDL_GetTicks();
|
||||
|
||||
device->dev = hid_open_path(device->path, 0);
|
||||
if (!device->dev) {
|
||||
@@ -471,6 +525,7 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
if (ctx->is_dongle) {
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
ctx->official_controller = SDL_TRUE;
|
||||
enhanced_mode = SDL_TRUE;
|
||||
} else if (device->vendor_id == USB_VENDOR_SONY) {
|
||||
Uint8 data[USB_PACKET_LENGTH];
|
||||
int size;
|
||||
@@ -484,13 +539,30 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
data[6], data[5], data[4], data[3], data[2], data[1]);
|
||||
joystick->serial = SDL_strdup(serial);
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
enhanced_mode = SDL_TRUE;
|
||||
} else {
|
||||
ctx->is_bluetooth = SDL_TRUE;
|
||||
|
||||
/* Read a report to see if we're in enhanced mode */
|
||||
size = hid_read_timeout(device->dev, data, sizeof(data), 16);
|
||||
#ifdef DEBUG_PS4_PROTOCOL
|
||||
if (size > 0) {
|
||||
HIDAPI_DumpPacket("PS4 first packet: size = %d", data, size);
|
||||
} else {
|
||||
SDL_Log("PS4 first packet: size = %d\n", size);
|
||||
}
|
||||
#endif
|
||||
if (size > 0 &&
|
||||
data[0] >= k_EPS4ReportIdBluetoothState1 &&
|
||||
data[0] <= k_EPS4ReportIdBluetoothState9) {
|
||||
enhanced_mode = SDL_TRUE;
|
||||
}
|
||||
}
|
||||
ctx->official_controller = SDL_TRUE;
|
||||
} else {
|
||||
/* Third party controllers appear to all be wired */
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
enhanced_mode = SDL_TRUE;
|
||||
}
|
||||
#ifdef DEBUG_PS4
|
||||
SDL_Log("PS4 dongle = %s, bluetooth = %s\n", ctx->is_dongle ? "TRUE" : "FALSE", ctx->is_bluetooth ? "TRUE" : "FALSE");
|
||||
@@ -503,28 +575,42 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
}
|
||||
|
||||
if (HIDAPI_DriverPS4_CanRumble(device->vendor_id, device->product_id)) {
|
||||
if (ctx->is_bluetooth) {
|
||||
ctx->effects_supported = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_FALSE);
|
||||
} else {
|
||||
ctx->effects_supported = SDL_TRUE;
|
||||
ctx->effects_supported = SDL_TRUE;
|
||||
}
|
||||
|
||||
if (!joystick->serial && device->serial && SDL_strlen(device->serial) == 12) {
|
||||
int i, j;
|
||||
char serial[18];
|
||||
|
||||
j = -1;
|
||||
for (i = 0; i < 12; i += 2) {
|
||||
j += 1;
|
||||
SDL_memcpy(&serial[j], &device->serial[i], 2);
|
||||
j += 2;
|
||||
serial[j] = '-';
|
||||
}
|
||||
serial[j] = '\0';
|
||||
|
||||
joystick->serial = SDL_strdup(serial);
|
||||
}
|
||||
|
||||
/* Initialize player index (needed for setting LEDs) */
|
||||
ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
|
||||
|
||||
/* Initialize LED and effect state */
|
||||
HIDAPI_DriverPS4_UpdateEffects(device);
|
||||
|
||||
/* Initialize the joystick capabilities */
|
||||
/* Initialize the joystick capabilities
|
||||
*
|
||||
* We can't dynamically add the touchpad button, so always report it here
|
||||
*/
|
||||
joystick->nbuttons = 16;
|
||||
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
|
||||
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
|
||||
|
||||
SDL_PrivateJoystickAddTouchpad(joystick, 2);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL);
|
||||
joystick->epowerlevel = ctx->is_bluetooth ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
|
||||
|
||||
if (enhanced_mode) {
|
||||
HIDAPI_DriverPS4_SetEnhancedMode(device, joystick);
|
||||
} else {
|
||||
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE,
|
||||
SDL_PS4RumbleHintChanged, ctx);
|
||||
}
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
@@ -569,6 +655,10 @@ HIDAPI_DriverPS4_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joysti
|
||||
{
|
||||
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
|
||||
|
||||
if (!ctx->enhanced_mode) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
HIDAPI_DriverPS4_LoadCalibrationData(device);
|
||||
}
|
||||
@@ -729,8 +819,9 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
|
||||
SDL_Joystick *joystick = NULL;
|
||||
Uint8 data[USB_PACKET_LENGTH];
|
||||
Uint8 data[USB_PACKET_LENGTH*2];
|
||||
int size;
|
||||
int packet_count = 0;
|
||||
|
||||
if (device->num_joysticks > 0) {
|
||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||
@@ -743,6 +834,9 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
#ifdef DEBUG_PS4_PROTOCOL
|
||||
HIDAPI_DumpPacket("PS4 packet: size = %d", data, size);
|
||||
#endif
|
||||
++packet_count;
|
||||
ctx->last_packet = SDL_GetTicks();
|
||||
|
||||
switch (data[0]) {
|
||||
case k_EPS4ReportIdUsbState:
|
||||
HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t *)&data[1]);
|
||||
@@ -756,6 +850,10 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
case k_EPS4ReportIdBluetoothState7:
|
||||
case k_EPS4ReportIdBluetoothState8:
|
||||
case k_EPS4ReportIdBluetoothState9:
|
||||
if (!ctx->enhanced_mode) {
|
||||
/* This is the extended report, we can enable effects now */
|
||||
HIDAPI_DriverPS4_SetEnhancedMode(device, joystick);
|
||||
}
|
||||
/* Bluetooth state packets have two additional bytes at the beginning, the first notes if HID is present */
|
||||
if (data[1] & 0x80) {
|
||||
HIDAPI_DriverPS4_HandleStatePacket(joystick, device->dev, ctx, (PS4StatePacket_t*)&data[3]);
|
||||
@@ -769,6 +867,14 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx->is_bluetooth && packet_count == 0) {
|
||||
/* Check to see if it looks like the device disconnected */
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
/* Send an empty output report to tickle the Bluetooth stack */
|
||||
HIDAPI_DriverPS4_TickleBluetooth(device);
|
||||
}
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
/* Read error, device is disconnected */
|
||||
HIDAPI_JoystickDisconnected(device, joystick->instance_id);
|
||||
@@ -779,11 +885,20 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
static void
|
||||
HIDAPI_DriverPS4_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
SDL_DriverPS4_Context *ctx = (SDL_DriverPS4_Context *)device->context;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE,
|
||||
SDL_PS4RumbleHintChanged, ctx);
|
||||
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
|
287
externals/SDL/src/joystick/hidapi/SDL_hidapi_ps5.c
vendored
287
externals/SDL/src/joystick/hidapi/SDL_hidapi_ps5.c
vendored
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "SDL_timer.h"
|
||||
#include "SDL_joystick.h"
|
||||
#include "SDL_gamecontroller.h"
|
||||
#include "../../SDL_hints_c.h"
|
||||
#include "../SDL_sysjoystick.h"
|
||||
#include "SDL_hidapijoystick_c.h"
|
||||
#include "SDL_hidapi_rumble.h"
|
||||
@@ -129,13 +130,12 @@ typedef struct
|
||||
} DS5EffectsState_t;
|
||||
|
||||
typedef enum {
|
||||
k_EDS5EffectNone,
|
||||
k_EDS5EffectRumbleStart,
|
||||
k_EDS5EffectRumble,
|
||||
k_EDS5EffectLEDReset,
|
||||
k_EDS5EffectLED,
|
||||
k_EDS5EffectPadLights,
|
||||
k_EDS5EffectMicLight,
|
||||
k_EDS5EffectRumbleStart = (1 << 0),
|
||||
k_EDS5EffectRumble = (1 << 1),
|
||||
k_EDS5EffectLEDReset = (1 << 2),
|
||||
k_EDS5EffectLED = (1 << 3),
|
||||
k_EDS5EffectPadLights = (1 << 4),
|
||||
k_EDS5EffectMicLight = (1 << 5)
|
||||
} EDS5Effect;
|
||||
|
||||
typedef enum {
|
||||
@@ -150,12 +150,16 @@ typedef struct {
|
||||
} IMUCalibrationData;
|
||||
|
||||
typedef struct {
|
||||
SDL_HIDAPI_Device *device;
|
||||
SDL_Joystick *joystick;
|
||||
SDL_bool is_bluetooth;
|
||||
SDL_bool enhanced_mode;
|
||||
SDL_bool report_sensors;
|
||||
SDL_bool hardware_calibration;
|
||||
IMUCalibrationData calibration[6];
|
||||
Uint32 last_packet;
|
||||
int player_index;
|
||||
SDL_bool player_lights;
|
||||
Uint8 rumble_left;
|
||||
Uint8 rumble_right;
|
||||
SDL_bool color_set;
|
||||
@@ -204,9 +208,9 @@ SetLedsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
|
||||
{ 0x40, 0x00, 0x00 }, /* Red */
|
||||
{ 0x00, 0x40, 0x00 }, /* Green */
|
||||
{ 0x20, 0x00, 0x20 }, /* Pink */
|
||||
{ 0x02, 0x01, 0x00 }, /* Orange */
|
||||
{ 0x00, 0x01, 0x01 }, /* Teal */
|
||||
{ 0x01, 0x01, 0x01 } /* White */
|
||||
{ 0x20, 0x10, 0x00 }, /* Orange */
|
||||
{ 0x00, 0x10, 0x10 }, /* Teal */
|
||||
{ 0x10, 0x10, 0x10 } /* White */
|
||||
};
|
||||
|
||||
if (player_index >= 0) {
|
||||
@@ -220,6 +224,24 @@ SetLedsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
|
||||
effects->ucLedBlue = colors[player_index][2];
|
||||
}
|
||||
|
||||
static void
|
||||
SetLightsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
|
||||
{
|
||||
static const Uint8 lights[] = {
|
||||
0x04,
|
||||
0x0A,
|
||||
0x15,
|
||||
0x1B
|
||||
};
|
||||
|
||||
if (player_index >= 0 && player_index < SDL_arraysize(lights)) {
|
||||
/* Bitmask, 0x1F enables all lights, 0x20 changes instantly instead of fade */
|
||||
effects->ucPadLights = lights[player_index] | 0x20;
|
||||
} else {
|
||||
effects->ucPadLights = 0x00;
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
@@ -350,7 +372,7 @@ HIDAPI_DriverPS5_ApplyCalibrationData(SDL_DriverPS5_Context *ctx, int index, Sin
|
||||
}
|
||||
|
||||
static int
|
||||
HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
|
||||
HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, int effect_mask)
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
|
||||
DS5EffectsState_t *effects;
|
||||
@@ -360,6 +382,9 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
|
||||
int *pending_size;
|
||||
int maximum_size;
|
||||
|
||||
if (!ctx->enhanced_mode) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
SDL_zero(data);
|
||||
|
||||
@@ -378,7 +403,8 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
|
||||
effects = (DS5EffectsState_t *)&data[offset];
|
||||
|
||||
/* Make sure the Bluetooth connection sequence has completed before sending LED color change */
|
||||
if (effect == k_EDS5EffectLED && ctx->is_bluetooth) {
|
||||
if (ctx->is_bluetooth &&
|
||||
(effect_mask & (k_EDS5EffectLED | k_EDS5EffectPadLights)) != 0) {
|
||||
if (ctx->led_reset_state != k_EDS5LEDResetStateComplete) {
|
||||
ctx->led_reset_state = k_EDS5LEDResetStatePending;
|
||||
return 0;
|
||||
@@ -396,17 +422,16 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
|
||||
/* Leaving emulated rumble bits off will restore audio haptics */
|
||||
}
|
||||
|
||||
switch (effect) {
|
||||
case k_EDS5EffectRumbleStart:
|
||||
if ((effect_mask & k_EDS5EffectRumbleStart) != 0) {
|
||||
effects->ucEnableBits1 |= 0x02; /* Disable audio haptics */
|
||||
break;
|
||||
case k_EDS5EffectRumble:
|
||||
}
|
||||
if ((effect_mask & k_EDS5EffectRumble) != 0) {
|
||||
/* Already handled above */
|
||||
break;
|
||||
case k_EDS5EffectLEDReset:
|
||||
}
|
||||
if ((effect_mask & k_EDS5EffectLEDReset) != 0) {
|
||||
effects->ucEnableBits2 |= 0x08; /* Reset LED state */
|
||||
break;
|
||||
case k_EDS5EffectLED:
|
||||
}
|
||||
if ((effect_mask & k_EDS5EffectLED) != 0) {
|
||||
effects->ucEnableBits2 |= 0x04; /* Enable LED color */
|
||||
|
||||
/* Populate the LED state with the appropriate color from our lookup table */
|
||||
@@ -417,19 +442,20 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
|
||||
} else {
|
||||
SetLedsForPlayerIndex(effects, ctx->player_index);
|
||||
}
|
||||
break;
|
||||
case k_EDS5EffectPadLights:
|
||||
}
|
||||
if ((effect_mask & k_EDS5EffectPadLights) != 0) {
|
||||
effects->ucEnableBits2 |= 0x10; /* Enable touchpad lights */
|
||||
|
||||
effects->ucPadLights = 0x00; /* Bitmask, 0x1F enables all lights, 0x20 changes instantly instead of fade */
|
||||
break;
|
||||
case k_EDS5EffectMicLight:
|
||||
if (ctx->player_lights) {
|
||||
SetLightsForPlayerIndex(effects, ctx->player_index);
|
||||
} else {
|
||||
effects->ucPadLights = 0x00;
|
||||
}
|
||||
}
|
||||
if ((effect_mask & k_EDS5EffectMicLight) != 0) {
|
||||
effects->ucEnableBits2 |= 0x01; /* Enable microphone light */
|
||||
|
||||
effects->ucMicLightMode = 0; /* Bitmask, 0x00 = off, 0x01 = solid, 0x02 = pulse */
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if (ctx->is_bluetooth) {
|
||||
@@ -461,17 +487,6 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, EDS5Effect effect)
|
||||
return SDL_HIDAPI_SendRumbleAndUnlock(device, data, report_size);
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverPS5_SetBluetooth(SDL_HIDAPI_Device *device, SDL_bool is_bluetooth)
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
|
||||
|
||||
if (ctx->is_bluetooth != is_bluetooth) {
|
||||
ctx->is_bluetooth = is_bluetooth;
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverPS5_CheckPendingLEDReset(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
@@ -479,17 +494,73 @@ HIDAPI_DriverPS5_CheckPendingLEDReset(SDL_HIDAPI_Device *device)
|
||||
const PS5StatePacket_t *packet = &ctx->last_state.state;
|
||||
|
||||
/* Check the timer to make sure the Bluetooth connection LED animation is complete */
|
||||
const Uint32 connection_complete = 10000000;
|
||||
const Uint32 connection_complete = 10200000;
|
||||
Uint32 timer = ((Uint32)packet->rgucTimer1[0] << 0) |
|
||||
((Uint32)packet->rgucTimer1[1] << 8) |
|
||||
((Uint32)packet->rgucTimer1[2] << 16) |
|
||||
((Uint32)packet->rgucTimer1[3] << 24);
|
||||
if (timer >= connection_complete) {
|
||||
if (SDL_TICKS_PASSED(timer, connection_complete)) {
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLEDReset);
|
||||
|
||||
ctx->led_reset_state = k_EDS5LEDResetStateComplete;
|
||||
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, (k_EDS5EffectLED | k_EDS5EffectPadLights));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverPS5_TickleBluetooth(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
/* This is just a dummy packet that should have no effect, since we don't set the CRC */
|
||||
Uint8 data[78];
|
||||
|
||||
SDL_zero(data);
|
||||
|
||||
data[0] = k_EPS5ReportIdBluetoothEffects;
|
||||
data[1] = 0x02; /* Magic value */
|
||||
|
||||
SDL_HIDAPI_SendRumble(device, data, sizeof(data));
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverPS5_SetEnhancedMode(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
|
||||
|
||||
if (!ctx->enhanced_mode) {
|
||||
ctx->enhanced_mode = SDL_TRUE;
|
||||
|
||||
SDL_PrivateJoystickAddTouchpad(joystick, 2);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL);
|
||||
|
||||
/* Switch into enhanced report mode */
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, 0);
|
||||
|
||||
/* Update the light effects */
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, (k_EDS5EffectLED | k_EDS5EffectPadLights));
|
||||
}
|
||||
}
|
||||
|
||||
static void SDLCALL SDL_PS5RumbleHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)userdata;
|
||||
|
||||
/* This is a one-way trip, you can't switch the controller back to simple report mode */
|
||||
if (SDL_GetStringBoolean(hint, SDL_FALSE)) {
|
||||
HIDAPI_DriverPS5_SetEnhancedMode(ctx->device, ctx->joystick);
|
||||
}
|
||||
}
|
||||
|
||||
static void SDLCALL SDL_PS5PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)userdata;
|
||||
SDL_bool player_lights = SDL_GetStringBoolean(hint, SDL_TRUE);
|
||||
|
||||
if (player_lights != ctx->player_lights) {
|
||||
ctx->player_lights = player_lights;
|
||||
|
||||
HIDAPI_DriverPS5_UpdateEffects(ctx->device, k_EDS5EffectPadLights);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -505,20 +576,24 @@ HIDAPI_DriverPS5_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID
|
||||
ctx->player_index = player_index;
|
||||
|
||||
/* This will set the new LED state based on the new player index */
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, (k_EDS5EffectLED | k_EDS5EffectPadLights));
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx;
|
||||
Uint8 data[USB_PACKET_LENGTH];
|
||||
Uint8 data[USB_PACKET_LENGTH*2];
|
||||
int size;
|
||||
SDL_bool enhanced_mode = SDL_FALSE;
|
||||
|
||||
ctx = (SDL_DriverPS5_Context *)SDL_calloc(1, sizeof(*ctx));
|
||||
if (!ctx) {
|
||||
SDL_OutOfMemory();
|
||||
return SDL_FALSE;
|
||||
}
|
||||
ctx->device = device;
|
||||
ctx->joystick = joystick;
|
||||
ctx->last_packet = SDL_GetTicks();
|
||||
|
||||
device->dev = hid_open_path(device->path, 0);
|
||||
@@ -529,31 +604,84 @@ HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
}
|
||||
device->context = ctx;
|
||||
|
||||
/* Read the serial number (Bluetooth address in reverse byte order)
|
||||
This will also enable enhanced reports over Bluetooth
|
||||
*/
|
||||
if (ReadFeatureReport(device->dev, k_EPS5FeatureReportIdSerialNumber, data, sizeof(data)) >= 7) {
|
||||
/* Read a report to see what mode we're in */
|
||||
size = hid_read_timeout(device->dev, data, sizeof(data), 16);
|
||||
#ifdef DEBUG_PS5_PROTOCOL
|
||||
if (size > 0) {
|
||||
HIDAPI_DumpPacket("PS5 first packet: size = %d", data, size);
|
||||
} else {
|
||||
SDL_Log("PS5 first packet: size = %d\n", size);
|
||||
}
|
||||
#endif
|
||||
if (size == 64) {
|
||||
/* Connected over USB */
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
enhanced_mode = SDL_TRUE;
|
||||
} else if (size > 0 && data[0] == k_EPS5ReportIdBluetoothEffects) {
|
||||
/* Connected over Bluetooth, using enhanced reports */
|
||||
ctx->is_bluetooth = SDL_TRUE;
|
||||
enhanced_mode = SDL_TRUE;
|
||||
} else {
|
||||
/* Connected over Bluetooth, using simple reports (DirectInput enabled) */
|
||||
ctx->is_bluetooth = SDL_TRUE;
|
||||
|
||||
/* Games written prior the introduction of PS5 controller support in SDL will not be aware of
|
||||
SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, but they did know SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE.
|
||||
To support apps that only knew about the PS4 hint, we'll use the PS4 hint as the default.
|
||||
*/
|
||||
enhanced_mode = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
|
||||
SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_FALSE));
|
||||
}
|
||||
|
||||
if (enhanced_mode) {
|
||||
/* Read the serial number (Bluetooth address in reverse byte order)
|
||||
This will also enable enhanced reports over Bluetooth
|
||||
*/
|
||||
if (ReadFeatureReport(device->dev, k_EPS5FeatureReportIdSerialNumber, data, sizeof(data)) >= 7) {
|
||||
char serial[18];
|
||||
|
||||
SDL_snprintf(serial, sizeof(serial), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
|
||||
data[6], data[5], data[4], data[3], data[2], data[1]);
|
||||
joystick->serial = SDL_strdup(serial);
|
||||
}
|
||||
}
|
||||
|
||||
if (!joystick->serial && device->serial && SDL_strlen(device->serial) == 12) {
|
||||
int i, j;
|
||||
char serial[18];
|
||||
|
||||
SDL_snprintf(serial, sizeof(serial), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
|
||||
data[6], data[5], data[4], data[3], data[2], data[1]);
|
||||
j = -1;
|
||||
for (i = 0; i < 12; i += 2) {
|
||||
j += 1;
|
||||
SDL_memcpy(&serial[j], &device->serial[i], 2);
|
||||
j += 2;
|
||||
serial[j] = '-';
|
||||
}
|
||||
serial[j] = '\0';
|
||||
|
||||
joystick->serial = SDL_strdup(serial);
|
||||
}
|
||||
|
||||
/* Initialize player index (needed for setting LEDs) */
|
||||
ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
|
||||
ctx->player_lights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, SDL_TRUE);
|
||||
|
||||
/* Initialize LED and effect state */
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
|
||||
|
||||
/* Initialize the joystick capabilities */
|
||||
/* Initialize the joystick capabilities
|
||||
*
|
||||
* We can't dynamically add the touchpad button, so always report it here
|
||||
*/
|
||||
joystick->nbuttons = 17;
|
||||
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
|
||||
joystick->epowerlevel = ctx->is_bluetooth ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
|
||||
|
||||
SDL_PrivateJoystickAddTouchpad(joystick, 2);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL);
|
||||
|
||||
if (enhanced_mode) {
|
||||
HIDAPI_DriverPS5_SetEnhancedMode(device, joystick);
|
||||
} else {
|
||||
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
|
||||
SDL_PS5RumbleHintChanged, ctx);
|
||||
}
|
||||
SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
|
||||
SDL_PS5PlayerLEDHintChanged, ctx);
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
@@ -602,6 +730,10 @@ HIDAPI_DriverPS5_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joysti
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
|
||||
|
||||
if (!ctx->enhanced_mode) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
HIDAPI_DriverPS5_LoadCalibrationData(device);
|
||||
}
|
||||
@@ -783,8 +915,8 @@ HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_
|
||||
Uint8 data = packet->rgucButtonsAndHat[2];
|
||||
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, 15, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, 16, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, 16, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
|
||||
axis = ((int)packet->ucTriggerLeft * 257) - 32768;
|
||||
@@ -869,20 +1001,21 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
|
||||
switch (data[0]) {
|
||||
case k_EPS5ReportIdState:
|
||||
if (size == 10) {
|
||||
HIDAPI_DriverPS5_SetBluetooth(device, SDL_TRUE); /* Simple state packet over Bluetooth */
|
||||
if (size == 10 || size == 78) {
|
||||
HIDAPI_DriverPS5_HandleSimpleStatePacket(joystick, device->dev, ctx, (PS5SimpleStatePacket_t *)&data[1]);
|
||||
} else {
|
||||
HIDAPI_DriverPS5_SetBluetooth(device, SDL_FALSE);
|
||||
HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[1]);
|
||||
}
|
||||
break;
|
||||
case k_EPS5ReportIdBluetoothState:
|
||||
HIDAPI_DriverPS5_SetBluetooth(device, SDL_TRUE);
|
||||
HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[2]);
|
||||
if (!ctx->enhanced_mode) {
|
||||
/* This is the extended report, we can enable effects now */
|
||||
HIDAPI_DriverPS5_SetEnhancedMode(device, joystick);
|
||||
}
|
||||
if (ctx->led_reset_state == k_EDS5LEDResetStatePending) {
|
||||
HIDAPI_DriverPS5_CheckPendingLEDReset(device);
|
||||
}
|
||||
HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[2]);
|
||||
break;
|
||||
default:
|
||||
#ifdef DEBUG_JOYSTICK
|
||||
@@ -896,7 +1029,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
/* Check to see if it looks like the device disconnected */
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
/* Send an empty output report to tickle the Bluetooth stack */
|
||||
HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectNone);
|
||||
HIDAPI_DriverPS5_TickleBluetooth(device);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -910,11 +1043,23 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
static void
|
||||
HIDAPI_DriverPS5_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
|
||||
SDL_PS5RumbleHintChanged, ctx);
|
||||
|
||||
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
|
||||
SDL_PS5PlayerLEDHintChanged, ctx);
|
||||
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -25,6 +25,7 @@
|
||||
/* Handle rumble on a separate thread so it doesn't block the application */
|
||||
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_timer.h"
|
||||
#include "SDL_hidapijoystick_c.h"
|
||||
#include "SDL_hidapi_rumble.h"
|
||||
#include "../../thread/SDL_systhread.h"
|
||||
@@ -76,11 +77,17 @@ static int SDL_HIDAPI_RumbleThread(void *data)
|
||||
if (request) {
|
||||
SDL_LockMutex(request->device->dev_lock);
|
||||
if (request->device->dev) {
|
||||
hid_write( request->device->dev, request->data, request->size );
|
||||
#ifdef DEBUG_RUMBLE
|
||||
HIDAPI_DumpPacket("Rumble packet: size = %d", request->data, request->size);
|
||||
#endif
|
||||
hid_write(request->device->dev, request->data, request->size);
|
||||
}
|
||||
SDL_UnlockMutex(request->device->dev_lock);
|
||||
(void)SDL_AtomicDecRef(&request->device->rumble_pending);
|
||||
SDL_free(request);
|
||||
|
||||
/* Make sure we're not starving report reads when there's lots of rumble */
|
||||
SDL_Delay(10);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
326
externals/SDL/src/joystick/hidapi/SDL_hidapi_stadia.c
vendored
Executable file
326
externals/SDL/src/joystick/hidapi/SDL_hidapi_stadia.c
vendored
Executable file
@@ -0,0 +1,326 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
#include "../../SDL_internal.h"
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI
|
||||
|
||||
#include "SDL_hints.h"
|
||||
#include "SDL_events.h"
|
||||
#include "SDL_joystick.h"
|
||||
#include "SDL_gamecontroller.h"
|
||||
#include "../SDL_sysjoystick.h"
|
||||
#include "SDL_hidapijoystick_c.h"
|
||||
#include "SDL_hidapi_rumble.h"
|
||||
|
||||
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_STADIA
|
||||
|
||||
/* Define this if you want to log all packets from the controller */
|
||||
/*#define DEBUG_STADIA_PROTOCOL*/
|
||||
|
||||
enum
|
||||
{
|
||||
SDL_CONTROLLER_BUTTON_STADIA_SHARE = 15,
|
||||
SDL_CONTROLLER_BUTTON_STADIA_GOOGLE_ASSISTANT,
|
||||
SDL_CONTROLLER_NUM_STADIA_BUTTONS,
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
Uint8 last_state[USB_PACKET_LENGTH];
|
||||
} SDL_DriverStadia_Context;
|
||||
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverStadia_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
|
||||
{
|
||||
if (vendor_id == USB_VENDOR_GOOGLE && product_id == USB_PRODUCT_GOOGLE_STADIA_CONTROLLER) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
static const char *
|
||||
HIDAPI_DriverStadia_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
|
||||
{
|
||||
return "Google Stadia Controller";
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverStadia_InitDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
return HIDAPI_JoystickConnected(device, NULL);
|
||||
}
|
||||
|
||||
static int
|
||||
HIDAPI_DriverStadia_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverStadia_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
|
||||
{
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverStadia_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_DriverStadia_Context *ctx;
|
||||
|
||||
ctx = (SDL_DriverStadia_Context *)SDL_calloc(1, sizeof(*ctx));
|
||||
if (!ctx) {
|
||||
SDL_OutOfMemory();
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
device->dev = hid_open_path(device->path, 0);
|
||||
if (!device->dev) {
|
||||
SDL_SetError("Couldn't open %s", device->path);
|
||||
SDL_free(ctx);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
device->context = ctx;
|
||||
|
||||
/* Initialize the joystick capabilities */
|
||||
joystick->nbuttons = SDL_CONTROLLER_NUM_STADIA_BUTTONS;
|
||||
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
|
||||
joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
|
||||
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
static int
|
||||
HIDAPI_DriverStadia_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
|
||||
{
|
||||
Uint8 rumble_packet[] = { 0x05, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
rumble_packet[1] = (low_frequency_rumble & 0xFF);
|
||||
rumble_packet[2] = (low_frequency_rumble >> 8);
|
||||
rumble_packet[3] = (high_frequency_rumble & 0xFF);
|
||||
rumble_packet[4] = (high_frequency_rumble >> 8);
|
||||
|
||||
if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
|
||||
return SDL_SetError("Couldn't send rumble packet");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
HIDAPI_DriverStadia_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
|
||||
{
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverStadia_HasJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
static int
|
||||
HIDAPI_DriverStadia_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
|
||||
{
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
static int
|
||||
HIDAPI_DriverStadia_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
|
||||
{
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverStadia_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverStadia_Context *ctx, Uint8 *data, int size)
|
||||
{
|
||||
Sint16 axis;
|
||||
|
||||
// The format is the same but the original FW will send 10 bytes and January '21 FW update will send 11
|
||||
if (size < 10 || data[0] != 0x03) {
|
||||
/* We don't know how to handle this report */
|
||||
return;
|
||||
}
|
||||
|
||||
if (ctx->last_state[1] != data[1]) {
|
||||
SDL_bool dpad_up = SDL_FALSE;
|
||||
SDL_bool dpad_down = SDL_FALSE;
|
||||
SDL_bool dpad_left = SDL_FALSE;
|
||||
SDL_bool dpad_right = SDL_FALSE;
|
||||
|
||||
switch (data[1]) {
|
||||
case 0:
|
||||
dpad_up = SDL_TRUE;
|
||||
break;
|
||||
case 1:
|
||||
dpad_up = SDL_TRUE;
|
||||
dpad_right = SDL_TRUE;
|
||||
break;
|
||||
case 2:
|
||||
dpad_right = SDL_TRUE;
|
||||
break;
|
||||
case 3:
|
||||
dpad_right = SDL_TRUE;
|
||||
dpad_down = SDL_TRUE;
|
||||
break;
|
||||
case 4:
|
||||
dpad_down = SDL_TRUE;
|
||||
break;
|
||||
case 5:
|
||||
dpad_left = SDL_TRUE;
|
||||
dpad_down = SDL_TRUE;
|
||||
break;
|
||||
case 6:
|
||||
dpad_left = SDL_TRUE;
|
||||
break;
|
||||
case 7:
|
||||
dpad_up = SDL_TRUE;
|
||||
dpad_left = SDL_TRUE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
|
||||
}
|
||||
|
||||
if (ctx->last_state[2] != data[2]) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_STADIA_SHARE, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_STADIA_GOOGLE_ASSISTANT, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
|
||||
if (ctx->last_state[3] != data[3]) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
|
||||
#define READ_STICK_AXIS(offset) \
|
||||
(data[offset] == 0x80 ? 0 : \
|
||||
(Sint16)HIDAPI_RemapVal((float)((int)data[offset] - 0x80), 0x01 - 0x80, 0xff - 0x80, SDL_MIN_SINT16, SDL_MAX_SINT16))
|
||||
{
|
||||
axis = READ_STICK_AXIS(4);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
|
||||
axis = READ_STICK_AXIS(5);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
|
||||
axis = READ_STICK_AXIS(6);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
|
||||
axis = READ_STICK_AXIS(7);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
|
||||
}
|
||||
#undef READ_STICK_AXIS
|
||||
|
||||
#define READ_TRIGGER_AXIS(offset) \
|
||||
(Sint16)(((int)data[offset] * 257) - 32768)
|
||||
{
|
||||
axis = READ_TRIGGER_AXIS(8);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
|
||||
axis = READ_TRIGGER_AXIS(9);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
|
||||
}
|
||||
#undef READ_TRIGGER_AXIS
|
||||
|
||||
SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverStadia_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
SDL_DriverStadia_Context *ctx = (SDL_DriverStadia_Context *)device->context;
|
||||
SDL_Joystick *joystick = NULL;
|
||||
Uint8 data[USB_PACKET_LENGTH];
|
||||
int size = 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) {
|
||||
#ifdef DEBUG_STADIA_PROTOCOL
|
||||
HIDAPI_DumpPacket("Google Stadia packet: size = %d", data, size);
|
||||
#endif
|
||||
HIDAPI_DriverStadia_HandleStatePacket(joystick, ctx, data, size);
|
||||
}
|
||||
|
||||
if (size < 0) {
|
||||
/* Read error, device is disconnected */
|
||||
HIDAPI_JoystickDisconnected(device, joystick->instance_id);
|
||||
}
|
||||
return (size >= 0);
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverStadia_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_DriverStadia_FreeDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
}
|
||||
|
||||
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia =
|
||||
{
|
||||
SDL_HINT_JOYSTICK_HIDAPI_STADIA,
|
||||
SDL_TRUE,
|
||||
HIDAPI_DriverStadia_IsSupportedDevice,
|
||||
HIDAPI_DriverStadia_GetDeviceName,
|
||||
HIDAPI_DriverStadia_InitDevice,
|
||||
HIDAPI_DriverStadia_GetDevicePlayerIndex,
|
||||
HIDAPI_DriverStadia_SetDevicePlayerIndex,
|
||||
HIDAPI_DriverStadia_UpdateDevice,
|
||||
HIDAPI_DriverStadia_OpenJoystick,
|
||||
HIDAPI_DriverStadia_RumbleJoystick,
|
||||
HIDAPI_DriverStadia_RumbleJoystickTriggers,
|
||||
HIDAPI_DriverStadia_HasJoystickLED,
|
||||
HIDAPI_DriverStadia_SetJoystickLED,
|
||||
HIDAPI_DriverStadia_SetJoystickSensorsEnabled,
|
||||
HIDAPI_DriverStadia_CloseJoystick,
|
||||
HIDAPI_DriverStadia_FreeDevice,
|
||||
};
|
||||
|
||||
#endif /* SDL_JOYSTICK_HIDAPI_STADIA */
|
||||
|
||||
#endif /* SDL_JOYSTICK_HIDAPI */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -1016,14 +1016,18 @@ HIDAPI_DriverSteam_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
|
||||
return SDL_TRUE;
|
||||
|
||||
error:
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
if (device->context) {
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
if (device->context) {
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
@@ -1170,12 +1174,17 @@ HIDAPI_DriverSteam_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
static void
|
||||
HIDAPI_DriverSteam_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
CloseSteamController(device->dev);
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
CloseSteamController(device->dev);
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -44,13 +44,24 @@
|
||||
/* Define this to get log output for rumble logic */
|
||||
/*#define DEBUG_RUMBLE*/
|
||||
|
||||
/* How often you can write rumble commands to the controller in Bluetooth mode
|
||||
If you send commands more frequently than this, you can turn off the controller.
|
||||
/* The initialization sequence doesn't appear to work correctly on Windows unless
|
||||
the reads and writes are on the same thread.
|
||||
|
||||
... and now I can't reproduce this, so I'm leaving it in, but disabled for now.
|
||||
*/
|
||||
#define RUMBLE_WRITE_FREQUENCY_MS 25
|
||||
/*#define SWITCH_SYNCHRONOUS_WRITES*/
|
||||
|
||||
/* How often you can write rumble commands to the controller.
|
||||
If you send commands more frequently than this, you can turn off the controller
|
||||
in Bluetooth mode, or the motors can miss the command in USB mode.
|
||||
*/
|
||||
#define RUMBLE_WRITE_FREQUENCY_MS 30
|
||||
|
||||
/* How often you have to refresh a long duration rumble to keep the motors running */
|
||||
#define RUMBLE_REFRESH_FREQUENCY_MS 40
|
||||
#define RUMBLE_REFRESH_FREQUENCY_MS 50
|
||||
|
||||
#define SWITCH_GYRO_SCALE 14.2842f
|
||||
#define SWITCH_ACCEL_SCALE 4096.f
|
||||
|
||||
typedef enum {
|
||||
k_eSwitchInputReportIDs_SubcommandReply = 0x21,
|
||||
@@ -79,6 +90,7 @@ typedef enum {
|
||||
} ESwitchSubcommandIDs;
|
||||
|
||||
typedef enum {
|
||||
k_eSwitchProprietaryCommandIDs_Status = 0x01,
|
||||
k_eSwitchProprietaryCommandIDs_Handshake = 0x02,
|
||||
k_eSwitchProprietaryCommandIDs_HighSpeed = 0x03,
|
||||
k_eSwitchProprietaryCommandIDs_ForceUSB = 0x04,
|
||||
@@ -87,6 +99,7 @@ typedef enum {
|
||||
} ESwitchProprietaryCommandIDs;
|
||||
|
||||
typedef enum {
|
||||
k_eSwitchDeviceInfoControllerType_Unknown = 0x0,
|
||||
k_eSwitchDeviceInfoControllerType_JoyConLeft = 0x1,
|
||||
k_eSwitchDeviceInfoControllerType_JoyConRight = 0x2,
|
||||
k_eSwitchDeviceInfoControllerType_ProController = 0x3,
|
||||
@@ -176,6 +189,16 @@ typedef struct
|
||||
};
|
||||
} SwitchSubcommandInputPacket_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Uint8 ucPacketType;
|
||||
Uint8 ucCommandID;
|
||||
Uint8 ucFiller;
|
||||
|
||||
Uint8 ucDeviceType;
|
||||
Uint8 rgucMACAddress[6];
|
||||
} SwitchProprietaryStatusPacket_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Uint8 rgucData[4];
|
||||
@@ -212,6 +235,8 @@ typedef struct {
|
||||
SDL_bool m_bUsingBluetooth;
|
||||
SDL_bool m_bIsGameCube;
|
||||
SDL_bool m_bUseButtonLabels;
|
||||
ESwitchDeviceInfoControllerType m_eControllerType;
|
||||
Uint8 m_rgucMACAddress[6];
|
||||
Uint8 m_nCommandNumber;
|
||||
SwitchCommonOutputPacket_t m_RumblePacket;
|
||||
Uint8 m_rgucReadBuffer[k_unSwitchMaxOutputPacketLength];
|
||||
@@ -220,6 +245,8 @@ typedef struct {
|
||||
SDL_bool m_bRumblePending;
|
||||
SDL_bool m_bRumbleZeroPending;
|
||||
Uint32 m_unRumblePending;
|
||||
SDL_bool m_bHasSensors;
|
||||
SDL_bool m_bReportSensors;
|
||||
|
||||
SwitchInputOnlyControllerStatePacket_t m_lastInputOnlyState;
|
||||
SwitchSimpleStatePacket_t m_lastSimpleState;
|
||||
@@ -285,7 +312,7 @@ HIDAPI_DriverSwitch_IsSupportedDevice(const char *name, SDL_GameControllerType t
|
||||
controller to continually attempt to reconnect is to filter it out by manufactuer/product string.
|
||||
Note that the controller does have a different product string when connected over Bluetooth.
|
||||
*/
|
||||
if (SDL_strcmp( name, "HORI Wireless Switch Pad" ) == 0) {
|
||||
if (SDL_strcmp(name, "HORI Wireless Switch Pad") == 0) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
return (type == SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO) ? SDL_TRUE : SDL_FALSE;
|
||||
@@ -295,6 +322,16 @@ static const char *
|
||||
HIDAPI_DriverSwitch_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
|
||||
{
|
||||
/* Give a user friendly name for this controller */
|
||||
if (vendor_id == USB_VENDOR_NINTENDO) {
|
||||
if (product_id == USB_PRODUCT_NINTENDO_SWITCH_JOY_CON_LEFT) {
|
||||
return "Nintendo Switch Joy-Con Left";
|
||||
}
|
||||
|
||||
if (product_id == USB_PRODUCT_NINTENDO_SWITCH_JOY_CON_RIGHT) {
|
||||
return "Nintendo Switch Joy-Con Right";
|
||||
}
|
||||
}
|
||||
|
||||
return "Nintendo Switch Pro Controller";
|
||||
}
|
||||
|
||||
@@ -310,11 +347,15 @@ static int ReadInput(SDL_DriverSwitch_Context *ctx)
|
||||
|
||||
static int WriteOutput(SDL_DriverSwitch_Context *ctx, const Uint8 *data, int size)
|
||||
{
|
||||
#ifdef SWITCH_SYNCHRONOUS_WRITES
|
||||
return hid_write(ctx->device->dev, data, size);
|
||||
#else
|
||||
/* Use the rumble thread for general asynchronous writes */
|
||||
if (SDL_HIDAPI_LockRumble() < 0) {
|
||||
return -1;
|
||||
}
|
||||
return SDL_HIDAPI_SendRumbleAndUnlock(ctx->device, data, size);
|
||||
#endif /* SWITCH_SYNCHRONOUS_WRITES */
|
||||
}
|
||||
|
||||
static SwitchSubcommandInputPacket_t *ReadSubcommandReply(SDL_DriverSwitch_Context *ctx, ESwitchSubcommandIDs expectedID)
|
||||
@@ -432,6 +473,7 @@ static SDL_bool WriteProprietary(SDL_DriverSwitch_Context *ctx, ESwitchProprieta
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
SDL_zero(packet);
|
||||
packet.ucPacketType = k_eSwitchOutputReportIDs_Proprietary;
|
||||
packet.ucProprietaryID = ucCommand;
|
||||
if (pBuf) {
|
||||
@@ -493,6 +535,40 @@ static SDL_bool WriteRumble(SDL_DriverSwitch_Context *ctx)
|
||||
return WritePacket(ctx, (Uint8 *)&ctx->m_RumblePacket, sizeof(ctx->m_RumblePacket));
|
||||
}
|
||||
|
||||
static SDL_bool BReadDeviceInfo(SDL_DriverSwitch_Context *ctx)
|
||||
{
|
||||
SwitchSubcommandInputPacket_t *reply = NULL;
|
||||
|
||||
ctx->m_bUsingBluetooth = SDL_FALSE;
|
||||
|
||||
if (WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Status, NULL, 0, SDL_TRUE)) {
|
||||
SwitchProprietaryStatusPacket_t *status = (SwitchProprietaryStatusPacket_t *)&ctx->m_rgucReadBuffer[0];
|
||||
size_t i;
|
||||
|
||||
ctx->m_eControllerType = (ESwitchDeviceInfoControllerType)status->ucDeviceType;
|
||||
for (i = 0; i < sizeof (ctx->m_rgucMACAddress); ++i)
|
||||
ctx->m_rgucMACAddress[i] = status->rgucMACAddress[ sizeof(ctx->m_rgucMACAddress) - i - 1 ];
|
||||
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
ctx->m_bUsingBluetooth = SDL_TRUE;
|
||||
|
||||
if (WriteSubcommand(ctx, k_eSwitchSubcommandIDs_RequestDeviceInfo, NULL, 0, &reply)) {
|
||||
// Byte 2: Controller ID (1=LJC, 2=RJC, 3=Pro)
|
||||
ctx->m_eControllerType = (ESwitchDeviceInfoControllerType)reply->deviceInfo.ucDeviceType;
|
||||
|
||||
// Bytes 4-9: MAC address (big-endian)
|
||||
memcpy(ctx->m_rgucMACAddress, reply->deviceInfo.rgucMACAddress, sizeof(ctx->m_rgucMACAddress));
|
||||
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
ctx->m_bUsingBluetooth = SDL_FALSE;
|
||||
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
static SDL_bool BTrySetupUSB(SDL_DriverSwitch_Context *ctx)
|
||||
{
|
||||
/* We have to send a connection handshake to the controller when communicating over USB
|
||||
@@ -510,6 +586,9 @@ static SDL_bool BTrySetupUSB(SDL_DriverSwitch_Context *ctx)
|
||||
if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Handshake, NULL, 0, SDL_TRUE)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
if (!WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, SDL_FALSE)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
@@ -550,6 +629,12 @@ static SDL_bool SetSlotLED(SDL_DriverSwitch_Context *ctx, Uint8 slot)
|
||||
return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetPlayerLights, &led_data, sizeof(led_data), NULL);
|
||||
}
|
||||
|
||||
static SDL_bool SetIMUEnabled(SDL_DriverSwitch_Context* ctx, SDL_bool enabled)
|
||||
{
|
||||
Uint8 imu_data = enabled ? 1 : 0;
|
||||
return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_EnableIMU, &imu_data, sizeof(imu_data), NULL);
|
||||
}
|
||||
|
||||
static SDL_bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx, Uint8 input_mode)
|
||||
{
|
||||
Uint8 *pStickCal;
|
||||
@@ -621,19 +706,6 @@ static SDL_bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx, Uint8 input_
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
static float fsel(float fComparand, float fValGE, float fLT)
|
||||
{
|
||||
return fComparand >= 0 ? fValGE : fLT;
|
||||
}
|
||||
|
||||
static float RemapVal(float val, float A, float B, float C, float D)
|
||||
{
|
||||
if (A == B) {
|
||||
return fsel(val - B , D , C);
|
||||
}
|
||||
return C + (D - C) * (val - A) / (B - A);
|
||||
}
|
||||
|
||||
static Sint16 ApplyStickCalibrationCentered(SDL_DriverSwitch_Context *ctx, int nStick, int nAxis, Sint16 sRawValue, Sint16 sCenter)
|
||||
{
|
||||
sRawValue -= sCenter;
|
||||
@@ -646,9 +718,9 @@ static Sint16 ApplyStickCalibrationCentered(SDL_DriverSwitch_Context *ctx, int n
|
||||
}
|
||||
|
||||
if (sRawValue > 0) {
|
||||
return (Sint16)(RemapVal(sRawValue, 0, ctx->m_StickExtents[nStick].axis[nAxis].sMax, 0, SDL_MAX_SINT16));
|
||||
return (Sint16)HIDAPI_RemapVal(sRawValue, 0, ctx->m_StickExtents[nStick].axis[nAxis].sMax, 0, SDL_MAX_SINT16);
|
||||
} else {
|
||||
return (Sint16)(RemapVal(sRawValue, ctx->m_StickExtents[nStick].axis[nAxis].sMin, 0, SDL_MIN_SINT16, 0));
|
||||
return (Sint16)HIDAPI_RemapVal(sRawValue, ctx->m_StickExtents[nStick].axis[nAxis].sMin, 0, SDL_MIN_SINT16, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -740,9 +812,16 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
|
||||
SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[0]);
|
||||
SetNeutralRumble(&ctx->m_RumblePacket.rumbleData[1]);
|
||||
|
||||
/* Try setting up USB mode, and if that fails we're using Bluetooth */
|
||||
if (!BTrySetupUSB(ctx)) {
|
||||
ctx->m_bUsingBluetooth = SDL_TRUE;
|
||||
if (!BReadDeviceInfo(ctx)) {
|
||||
SDL_SetError("Couldn't read device info");
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (!ctx->m_bUsingBluetooth) {
|
||||
if (!BTrySetupUSB(ctx)) {
|
||||
SDL_SetError("Couldn't setup USB mode");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
/* Determine the desired input mode (needed before loading stick calibration) */
|
||||
@@ -758,9 +837,17 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
|
||||
* level and we only care about battery level over bluetooth anyway.
|
||||
*/
|
||||
if (device->vendor_id == USB_VENDOR_NINTENDO &&
|
||||
device->product_id == USB_PRODUCT_NINTENDO_SWITCH_PRO) {
|
||||
(device->product_id == USB_PRODUCT_NINTENDO_SWITCH_PRO ||
|
||||
device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOY_CON_LEFT ||
|
||||
device->product_id == USB_PRODUCT_NINTENDO_SWITCH_JOY_CON_RIGHT)) {
|
||||
input_mode = k_eSwitchInputReportIDs_FullControllerState;
|
||||
}
|
||||
|
||||
if (input_mode == k_eSwitchInputReportIDs_FullControllerState) {
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO);
|
||||
SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL);
|
||||
ctx->m_bHasSensors = SDL_TRUE;
|
||||
}
|
||||
|
||||
if (!LoadStickCalibration(ctx, input_mode)) {
|
||||
SDL_SetError("Couldn't load stick calibration");
|
||||
@@ -789,9 +876,27 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
|
||||
|
||||
/* Set the LED state */
|
||||
if (ctx->m_bHasHomeLED) {
|
||||
SetHomeLED(ctx, 100);
|
||||
if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_SWITCH_HOME_LED, SDL_TRUE)) {
|
||||
SetHomeLED(ctx, 100);
|
||||
} else {
|
||||
SetHomeLED(ctx, 0);
|
||||
}
|
||||
}
|
||||
SetSlotLED(ctx, (joystick->instance_id % 4));
|
||||
|
||||
/* Set the serial number */
|
||||
{
|
||||
char serial[18];
|
||||
|
||||
SDL_snprintf(serial, sizeof(serial), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
|
||||
ctx->m_rgucMACAddress[0],
|
||||
ctx->m_rgucMACAddress[1],
|
||||
ctx->m_rgucMACAddress[2],
|
||||
ctx->m_rgucMACAddress[3],
|
||||
ctx->m_rgucMACAddress[4],
|
||||
ctx->m_rgucMACAddress[5]);
|
||||
joystick->serial = SDL_strdup(serial);
|
||||
}
|
||||
}
|
||||
|
||||
if (IsGameCubeFormFactor(device->vendor_id, device->product_id)) {
|
||||
@@ -803,21 +908,30 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
|
||||
SDL_GameControllerButtonReportingHintChanged, ctx);
|
||||
|
||||
/* Initialize the joystick capabilities */
|
||||
joystick->nbuttons = 16;
|
||||
if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft ||
|
||||
ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
|
||||
joystick->nbuttons = 20;
|
||||
} else {
|
||||
joystick->nbuttons = 16;
|
||||
}
|
||||
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
|
||||
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;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
if (device->context) {
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
@@ -859,7 +973,7 @@ HIDAPI_DriverSwitch_ActuallyRumbleJoystick(SDL_DriverSwitch_Context *ctx, Uint16
|
||||
static int
|
||||
HIDAPI_DriverSwitch_SendPendingRumble(SDL_DriverSwitch_Context *ctx)
|
||||
{
|
||||
if ((SDL_GetTicks() - ctx->m_unRumbleSent) < RUMBLE_WRITE_FREQUENCY_MS) {
|
||||
if (!SDL_TICKS_PASSED(SDL_GetTicks(), ctx->m_unRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -868,7 +982,7 @@ HIDAPI_DriverSwitch_SendPendingRumble(SDL_DriverSwitch_Context *ctx)
|
||||
Uint16 high_frequency_rumble = (Uint16)ctx->m_unRumblePending;
|
||||
|
||||
#ifdef DEBUG_RUMBLE
|
||||
SDL_Log("Sent pending rumble %d/%d\n", low_frequency_rumble, high_frequency_rumble);
|
||||
SDL_Log("Sent pending rumble %d/%d, %d ms after previous rumble\n", low_frequency_rumble, high_frequency_rumble, SDL_GetTicks() - ctx->m_unRumbleSent);
|
||||
#endif
|
||||
ctx->m_bRumblePending = SDL_FALSE;
|
||||
ctx->m_unRumblePending = 0;
|
||||
@@ -880,7 +994,7 @@ HIDAPI_DriverSwitch_SendPendingRumble(SDL_DriverSwitch_Context *ctx)
|
||||
ctx->m_bRumbleZeroPending = SDL_FALSE;
|
||||
|
||||
#ifdef DEBUG_RUMBLE
|
||||
SDL_Log("Sent pending zero rumble\n");
|
||||
SDL_Log("Sent pending zero rumble, %d ms after previous rumble\n", SDL_GetTicks() - ctx->m_unRumbleSent);
|
||||
#endif
|
||||
return HIDAPI_DriverSwitch_ActuallyRumbleJoystick(ctx, 0, 0);
|
||||
}
|
||||
@@ -899,7 +1013,7 @@ HIDAPI_DriverSwitch_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joys
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx->m_bUsingBluetooth && (SDL_GetTicks() - ctx->m_unRumbleSent) < RUMBLE_WRITE_FREQUENCY_MS) {
|
||||
if (!SDL_TICKS_PASSED(SDL_GetTicks(), ctx->m_unRumbleSent + RUMBLE_WRITE_FREQUENCY_MS)) {
|
||||
if (low_frequency_rumble || high_frequency_rumble) {
|
||||
Uint32 unRumblePending = ((Uint32)low_frequency_rumble << 16) | high_frequency_rumble;
|
||||
|
||||
@@ -945,7 +1059,30 @@ HIDAPI_DriverSwitch_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joys
|
||||
static int
|
||||
HIDAPI_DriverSwitch_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
|
||||
{
|
||||
return SDL_Unsupported();
|
||||
SDL_DriverSwitch_Context* ctx = (SDL_DriverSwitch_Context*)device->context;
|
||||
|
||||
if (!ctx->m_bHasSensors) {
|
||||
return SDL_Unsupported();
|
||||
}
|
||||
|
||||
SetIMUEnabled(ctx, enabled);
|
||||
ctx->m_bReportSensors = enabled;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static float
|
||||
HIDAPI_DriverSwitch_ScaleGyro(Sint16 value)
|
||||
{
|
||||
float result = (value / SWITCH_GYRO_SCALE) * (float)M_PI / 180.0f;
|
||||
return result;
|
||||
}
|
||||
|
||||
static float
|
||||
HIDAPI_DriverSwitch_ScaleAccel(Sint16 value)
|
||||
{
|
||||
float result = (value / SWITCH_ACCEL_SCALE) * SDL_STANDARD_GRAVITY;
|
||||
return result;
|
||||
}
|
||||
|
||||
static void HandleInputOnlyControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_Context *ctx, SwitchInputOnlyControllerStatePacket_t *packet)
|
||||
@@ -1023,22 +1160,22 @@ static void HandleInputOnlyControllerState(SDL_Joystick *joystick, SDL_DriverSwi
|
||||
}
|
||||
|
||||
if (packet->rgucJoystickLeft[0] != ctx->m_lastInputOnlyState.rgucJoystickLeft[0]) {
|
||||
axis = (Sint16)(RemapVal(packet->rgucJoystickLeft[0], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16));
|
||||
axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickLeft[0], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
|
||||
}
|
||||
|
||||
if (packet->rgucJoystickLeft[1] != ctx->m_lastInputOnlyState.rgucJoystickLeft[1]) {
|
||||
axis = (Sint16)(RemapVal(packet->rgucJoystickLeft[1], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16));
|
||||
axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickLeft[1], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
|
||||
}
|
||||
|
||||
if (packet->rgucJoystickRight[0] != ctx->m_lastInputOnlyState.rgucJoystickRight[0]) {
|
||||
axis = (Sint16)(RemapVal(packet->rgucJoystickRight[0], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16));
|
||||
axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickRight[0], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
|
||||
}
|
||||
|
||||
if (packet->rgucJoystickRight[1] != ctx->m_lastInputOnlyState.rgucJoystickRight[1]) {
|
||||
axis = (Sint16)(RemapVal(packet->rgucJoystickRight[1], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16));
|
||||
axis = (Sint16)HIDAPI_RemapVal(packet->rgucJoystickRight[1], SDL_MIN_UINT8, SDL_MAX_UINT8, SDL_MIN_SINT16, SDL_MAX_SINT16);
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
|
||||
}
|
||||
|
||||
@@ -1146,6 +1283,10 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
|
||||
SDL_PrivateJoystickButton(joystick, RemapButton(ctx, SDL_CONTROLLER_BUTTON_B), (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, RemapButton(ctx, SDL_CONTROLLER_BUTTON_X), (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, RemapButton(ctx, SDL_CONTROLLER_BUTTON_Y), (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_PADDLE1, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_PADDLE3, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
|
||||
axis = (data & 0x80) ? 32767 : -32768;
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
|
||||
@@ -1168,6 +1309,10 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
|
||||
if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConLeft) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_PADDLE4, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_PADDLE2, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
|
||||
axis = (data & 0x80) ? 32767 : -32768;
|
||||
SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
|
||||
@@ -1210,6 +1355,54 @@ static void HandleFullControllerState(SDL_Joystick *joystick, SDL_DriverSwitch_C
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx->m_bReportSensors) {
|
||||
float data[3];
|
||||
|
||||
/* Note the order of components has been shuffled to match PlayStation controllers,
|
||||
* since that's our de facto standard from already supporting those controllers, and
|
||||
* users will want consistent axis mappings across devices.
|
||||
*/
|
||||
data[0] = HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[0].sGyroY);
|
||||
data[1] = HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[0].sGyroZ);
|
||||
data[2] = HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[0].sGyroX);
|
||||
data[0] += HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[1].sGyroY);
|
||||
data[1] += HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[1].sGyroZ);
|
||||
data[2] += HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[1].sGyroX);
|
||||
data[0] += HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[2].sGyroY);
|
||||
data[1] += HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[2].sGyroZ);
|
||||
data[2] += HIDAPI_DriverSwitch_ScaleGyro(packet->imuState[2].sGyroX);
|
||||
data[0] /= -3.f;
|
||||
data[1] /= 3.f;
|
||||
data[2] /= -3.f;
|
||||
/* Right Joy-Con flips some axes, so let's flip them back for consistency */
|
||||
if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
|
||||
data[0] = -data[0];
|
||||
data[1] = -data[1];
|
||||
}
|
||||
|
||||
SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_GYRO, data, 3);
|
||||
|
||||
data[0] = HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[0].sAccelY);
|
||||
data[1] = HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[0].sAccelZ);
|
||||
data[2] = HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[0].sAccelX);
|
||||
data[0] += HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[1].sAccelY);
|
||||
data[1] += HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[1].sAccelZ);
|
||||
data[2] += HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[1].sAccelX);
|
||||
data[0] += HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[2].sAccelY);
|
||||
data[1] += HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[2].sAccelZ);
|
||||
data[2] += HIDAPI_DriverSwitch_ScaleAccel(packet->imuState[2].sAccelX);
|
||||
data[0] /= -3.f;
|
||||
data[1] /= 3.f;
|
||||
data[2] /= -3.f;
|
||||
/* Right Joy-Con flips some axes, so let's flip them back for consistency */
|
||||
if (ctx->m_eControllerType == k_eSwitchDeviceInfoControllerType_JoyConRight) {
|
||||
data[0] = -data[0];
|
||||
data[1] = -data[1];
|
||||
}
|
||||
|
||||
SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_ACCEL, data, 3);
|
||||
}
|
||||
|
||||
ctx->m_lastFullState = *packet;
|
||||
}
|
||||
|
||||
@@ -1252,7 +1445,7 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
} else if (ctx->m_bRumbleActive &&
|
||||
SDL_TICKS_PASSED(SDL_GetTicks(), ctx->m_unRumbleSent + RUMBLE_REFRESH_FREQUENCY_MS)) {
|
||||
#ifdef DEBUG_RUMBLE
|
||||
SDL_Log("Sent continuing rumble\n");
|
||||
SDL_Log("Sent continuing rumble, %d ms after previous rumble\n", SDL_GetTicks() - ctx->m_unRumbleSent);
|
||||
#endif
|
||||
WriteRumble(ctx);
|
||||
}
|
||||
@@ -1277,11 +1470,15 @@ HIDAPI_DriverSwitch_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joyst
|
||||
SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
|
||||
SDL_GameControllerButtonReportingHintChanged, ctx);
|
||||
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -42,22 +42,6 @@ typedef struct {
|
||||
Uint8 last_state[USB_PACKET_LENGTH];
|
||||
} SDL_DriverXbox360_Context;
|
||||
|
||||
#if defined(__MACOSX__)
|
||||
static SDL_bool
|
||||
IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
|
||||
{
|
||||
/* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
|
||||
if (vendor_id == USB_VENDOR_MICROSOFT) {
|
||||
if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
|
||||
product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
|
||||
product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
#endif
|
||||
|
||||
static SDL_bool
|
||||
HIDAPI_DriverXbox360_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
|
||||
{
|
||||
@@ -89,7 +73,7 @@ HIDAPI_DriverXbox360_IsSupportedDevice(const char *name, SDL_GameControllerType
|
||||
|
||||
Bluetooth Xbox One controllers are handled by the SDL Xbox One driver
|
||||
*/
|
||||
if (IsBluetoothXboxOneController(vendor_id, product_id)) {
|
||||
if (SDL_IsJoystickBluetoothXboxOne(vendor_id, product_id)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
return (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE) ? SDL_TRUE : SDL_FALSE;
|
||||
@@ -106,9 +90,11 @@ HIDAPI_DriverXbox360_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
|
||||
|
||||
static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
|
||||
{
|
||||
Uint8 mode = 0x02 + slot;
|
||||
const Uint8 led_packet[] = { 0x01, 0x03, mode };
|
||||
const SDL_bool blink = SDL_FALSE;
|
||||
Uint8 mode = (blink ? 0x02 : 0x06) + slot;
|
||||
Uint8 led_packet[] = { 0x01, 0x03, 0x00 };
|
||||
|
||||
led_packet[2] = mode;
|
||||
if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
@@ -133,7 +119,9 @@ HIDAPI_DriverXbox360_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_Joystic
|
||||
if (!device->dev) {
|
||||
return;
|
||||
}
|
||||
SetSlotLED(device->dev, (player_index % 4));
|
||||
if (player_index >= 0) {
|
||||
SetSlotLED(device->dev, (player_index % 4));
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
@@ -174,7 +162,7 @@ static int
|
||||
HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
|
||||
{
|
||||
#ifdef __MACOSX__
|
||||
if (IsBluetoothXboxOneController(device->vendor_id, device->product_id)) {
|
||||
if (SDL_IsJoystickBluetoothXboxOne(device->vendor_id, device->product_id)) {
|
||||
Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
|
||||
|
||||
rumble_packet[4] = (low_frequency_rumble >> 8);
|
||||
@@ -321,13 +309,17 @@ HIDAPI_DriverXbox360_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
static void
|
||||
HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
if (device->dev) {
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
}
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -49,7 +49,7 @@ HIDAPI_DriverXbox360W_IsSupportedDevice(const char *name, SDL_GameControllerType
|
||||
{
|
||||
const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
|
||||
|
||||
if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == 0x0291 || product_id == 0x02a9 || product_id == 0x0719)) ||
|
||||
if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == 0x0291 || product_id == 0x02a9 || product_id == 0x0719) && interface_protocol == 0) ||
|
||||
(type == SDL_CONTROLLER_TYPE_XBOX360 && interface_protocol == XB360W_IFACE_PROTOCOL)) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
@@ -64,9 +64,11 @@ HIDAPI_DriverXbox360W_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
|
||||
|
||||
static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
|
||||
{
|
||||
Uint8 mode = 0x02 + slot;
|
||||
const Uint8 led_packet[] = { 0x00, 0x00, 0x08, (0x40 + (mode % 0x0e)), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
const SDL_bool blink = SDL_FALSE;
|
||||
Uint8 mode = (blink ? 0x02 : 0x06) + slot;
|
||||
Uint8 led_packet[] = { 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
led_packet[3] = 0x40 + (mode % 0x0e);
|
||||
if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
@@ -131,7 +133,9 @@ HIDAPI_DriverXbox360W_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_Joysti
|
||||
if (!device->dev) {
|
||||
return;
|
||||
}
|
||||
SetSlotLED(device->dev, (player_index % 4));
|
||||
if (player_index >= 0) {
|
||||
SetSlotLED(device->dev, (player_index % 4));
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
@@ -309,11 +313,15 @@ HIDAPI_DriverXbox360W_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
|
||||
static void
|
||||
HIDAPI_DriverXbox360W_FreeDevice(SDL_HIDAPI_Device *device)
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W =
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -129,22 +129,6 @@ typedef struct {
|
||||
Uint8 right_trigger_rumble;
|
||||
} SDL_DriverXboxOne_Context;
|
||||
|
||||
|
||||
static SDL_bool
|
||||
IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
|
||||
{
|
||||
/* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
|
||||
if (vendor_id == USB_VENDOR_MICROSOFT) {
|
||||
if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
|
||||
product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
|
||||
product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH ||
|
||||
product_id == USB_PRODUCT_XBOX_ONE_SERIES_X_BLUETOOTH) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
}
|
||||
return SDL_FALSE;
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
ControllerHasPaddles(Uint16 vendor_id, Uint16 product_id)
|
||||
{
|
||||
@@ -154,7 +138,8 @@ ControllerHasPaddles(Uint16 vendor_id, Uint16 product_id)
|
||||
static SDL_bool
|
||||
ControllerHasTriggerRumble(Uint16 vendor_id, Uint16 product_id)
|
||||
{
|
||||
return SDL_IsJoystickXboxOneElite(vendor_id, product_id);
|
||||
// All the Microsoft Xbox One controllers have trigger rumble
|
||||
return (vendor_id == USB_VENDOR_MICROSOFT);
|
||||
}
|
||||
|
||||
static SDL_bool
|
||||
@@ -179,7 +164,11 @@ SendAckIfNeeded(SDL_HIDAPI_Device *device, Uint8 *data, int size)
|
||||
/* The Windows driver is taking care of acks */
|
||||
#else
|
||||
if ((data[1] & 0x30) == 0x30) {
|
||||
Uint8 ack_packet[] = { 0x01, 0x20, data[2], 0x09, 0x00, data[0], 0x20, data[3], 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
Uint8 ack_packet[] = { 0x01, 0x20, 0x00, 0x09, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
||||
|
||||
ack_packet[2] = data[2];
|
||||
ack_packet[5] = data[0];
|
||||
ack_packet[7] = data[3];
|
||||
|
||||
/* The initial ack needs 0x80 added to the response, for some reason */
|
||||
if (data[0] == 0x04 && data[1] == 0xF0) {
|
||||
@@ -289,7 +278,7 @@ HIDAPI_DriverXboxOne_IsSupportedDevice(const char *name, SDL_GameControllerType
|
||||
#endif
|
||||
#ifdef __MACOSX__
|
||||
/* Wired Xbox One controllers are handled by the 360Controller driver */
|
||||
if (!IsBluetoothXboxOneController(vendor_id, product_id)) {
|
||||
if (!SDL_IsJoystickBluetoothXboxOne(vendor_id, product_id)) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
#endif
|
||||
@@ -343,7 +332,7 @@ HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joyst
|
||||
|
||||
ctx->vendor_id = device->vendor_id;
|
||||
ctx->product_id = device->product_id;
|
||||
ctx->bluetooth = IsBluetoothXboxOneController(device->vendor_id, device->product_id);
|
||||
ctx->bluetooth = SDL_IsJoystickBluetoothXboxOne(device->vendor_id, device->product_id);
|
||||
ctx->start_time = SDL_GetTicks();
|
||||
ctx->sequence = 1;
|
||||
ctx->has_paddles = ControllerHasPaddles(ctx->vendor_id, ctx->product_id);
|
||||
@@ -488,14 +477,18 @@ HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne
|
||||
}
|
||||
|
||||
if (ctx->has_share_button) {
|
||||
/* Version 1 of the firmware for Xbox One Series X */
|
||||
if (ctx->last_state[18] != data[18]) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MISC1, (data[18] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
|
||||
/* Version 2 of the firmware for Xbox One Series X */
|
||||
if (ctx->last_state[22] != data[22]) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MISC1, (data[22] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
/* Xbox Series X firmware version 5.0, report is 36 bytes, share button is in byte 18
|
||||
* Xbox Series X firmware version 5.1, report is 44 bytes, share button is in byte 18
|
||||
* Xbox Series X firmware version 5.5, report is 48 bytes, share button is in byte 22
|
||||
*/
|
||||
if (size < 48) {
|
||||
if (ctx->last_state[18] != data[18]) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MISC1, (data[18] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
} else {
|
||||
if (ctx->last_state[22] != data[22]) {
|
||||
SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MISC1, (data[22] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,7 +519,7 @@ HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne
|
||||
button4_bit = 0x04;
|
||||
|
||||
/* The mapped controller state is at offset 4, the raw state is at offset 18, compare them to see if the paddles are mapped */
|
||||
paddles_mapped = (SDL_memcmp(&data[4], &data[18], 14) != 0);
|
||||
paddles_mapped = (SDL_memcmp(&data[4], &data[18], 2) != 0);
|
||||
|
||||
} else /* if (size == 38) */ {
|
||||
/* XBox One Elite Series 2 */
|
||||
@@ -1054,11 +1047,15 @@ HIDAPI_DriverXboxOne_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
static void
|
||||
HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
SDL_LockMutex(device->dev_lock);
|
||||
{
|
||||
hid_close(device->dev);
|
||||
device->dev = NULL;
|
||||
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
SDL_free(device->context);
|
||||
device->context = NULL;
|
||||
}
|
||||
SDL_UnlockMutex(device->dev_lock);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "SDL_thread.h"
|
||||
#include "SDL_timer.h"
|
||||
#include "SDL_joystick.h"
|
||||
#include "SDL_log.h"
|
||||
#include "../SDL_sysjoystick.h"
|
||||
#include "SDL_hidapijoystick_c.h"
|
||||
#include "SDL_hidapi_rumble.h"
|
||||
@@ -51,6 +52,24 @@
|
||||
#ifdef SDL_USE_LIBUDEV
|
||||
#include <poll.h>
|
||||
#endif
|
||||
#ifdef HAVE_INOTIFY
|
||||
#include <errno.h> /* errno, strerror */
|
||||
#include <fcntl.h>
|
||||
#include <limits.h> /* For the definition of NAME_MAX */
|
||||
#include <sys/inotify.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if defined(SDL_USE_LIBUDEV)
|
||||
typedef enum
|
||||
{
|
||||
ENUMERATION_UNSET,
|
||||
ENUMERATION_LIBUDEV,
|
||||
ENUMERATION_FALLBACK
|
||||
} LinuxEnumerationMethod;
|
||||
|
||||
static LinuxEnumerationMethod linux_enumeration_method = ENUMERATION_UNSET;
|
||||
#endif
|
||||
|
||||
struct joystick_hwdata
|
||||
@@ -59,12 +78,18 @@ struct joystick_hwdata
|
||||
};
|
||||
|
||||
static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
|
||||
&SDL_HIDAPI_DriverGameCube,
|
||||
#endif
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_PS4
|
||||
&SDL_HIDAPI_DriverPS4,
|
||||
#endif
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_PS5
|
||||
&SDL_HIDAPI_DriverPS5,
|
||||
#endif
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_STADIA
|
||||
&SDL_HIDAPI_DriverStadia,
|
||||
#endif
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_STEAM
|
||||
&SDL_HIDAPI_DriverSteam,
|
||||
#endif
|
||||
@@ -78,9 +103,6 @@ static SDL_HIDAPI_DeviceDriver *SDL_HIDAPI_drivers[] = {
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
|
||||
&SDL_HIDAPI_DriverXboxOne,
|
||||
#endif
|
||||
#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
|
||||
&SDL_HIDAPI_DriverGameCube,
|
||||
#endif
|
||||
};
|
||||
static int SDL_HIDAPI_numdrivers = 0;
|
||||
static SDL_SpinLock SDL_HIDAPI_spinlock;
|
||||
@@ -89,6 +111,10 @@ static int SDL_HIDAPI_numjoysticks = 0;
|
||||
static SDL_bool initialized = SDL_FALSE;
|
||||
static SDL_bool shutting_down = SDL_FALSE;
|
||||
|
||||
#if defined(HAVE_INOTIFY)
|
||||
static int inotify_fd = -1;
|
||||
#endif
|
||||
|
||||
#if defined(SDL_USE_LIBUDEV)
|
||||
static const SDL_UDEV_Symbols * usyms = NULL;
|
||||
#endif
|
||||
@@ -181,6 +207,46 @@ static void CallbackIOServiceFunc(void *context, io_iterator_t portIterator)
|
||||
}
|
||||
#endif /* __MACOSX__ */
|
||||
|
||||
#ifdef HAVE_INOTIFY
|
||||
#ifdef HAVE_INOTIFY_INIT1
|
||||
static int SDL_inotify_init1(void) {
|
||||
return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||
}
|
||||
#else
|
||||
static int SDL_inotify_init1(void) {
|
||||
int fd = inotify_init();
|
||||
if (fd < 0) return -1;
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
return fd;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int
|
||||
StrHasPrefix(const char *string, const char *prefix)
|
||||
{
|
||||
return (SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0);
|
||||
}
|
||||
|
||||
static int
|
||||
StrIsInteger(const char *string)
|
||||
{
|
||||
const char *p;
|
||||
|
||||
if (*string == '\0') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (p = string; *p != '\0'; p++) {
|
||||
if (*p < '0' || *p > '9') {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void
|
||||
HIDAPI_InitializeDiscovery()
|
||||
{
|
||||
@@ -270,24 +336,55 @@ HIDAPI_InitializeDiscovery()
|
||||
#endif // __MACOSX__
|
||||
|
||||
#if defined(SDL_USE_LIBUDEV)
|
||||
SDL_HIDAPI_discovery.m_pUdev = NULL;
|
||||
SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
|
||||
SDL_HIDAPI_discovery.m_nUdevFd = -1;
|
||||
if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
|
||||
SDL_HIDAPI_discovery.m_pUdev = NULL;
|
||||
SDL_HIDAPI_discovery.m_pUdevMonitor = NULL;
|
||||
SDL_HIDAPI_discovery.m_nUdevFd = -1;
|
||||
|
||||
usyms = SDL_UDEV_GetUdevSyms();
|
||||
if (usyms) {
|
||||
SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
|
||||
}
|
||||
if (SDL_HIDAPI_discovery.m_pUdev) {
|
||||
SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
|
||||
if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
|
||||
usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
|
||||
SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
|
||||
SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
|
||||
usyms = SDL_UDEV_GetUdevSyms();
|
||||
if (usyms) {
|
||||
SDL_HIDAPI_discovery.m_pUdev = usyms->udev_new();
|
||||
}
|
||||
if (SDL_HIDAPI_discovery.m_pUdev) {
|
||||
SDL_HIDAPI_discovery.m_pUdevMonitor = usyms->udev_monitor_new_from_netlink(SDL_HIDAPI_discovery.m_pUdev, "udev");
|
||||
if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
|
||||
usyms->udev_monitor_enable_receiving(SDL_HIDAPI_discovery.m_pUdevMonitor);
|
||||
SDL_HIDAPI_discovery.m_nUdevFd = usyms->udev_monitor_get_fd(SDL_HIDAPI_discovery.m_pUdevMonitor);
|
||||
SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
#endif /* SDL_USE_LIBUDEV */
|
||||
{
|
||||
#if defined(HAVE_INOTIFY)
|
||||
inotify_fd = SDL_inotify_init1();
|
||||
|
||||
if (inotify_fd < 0) {
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
|
||||
"Unable to initialize inotify, falling back to polling: %s",
|
||||
strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
/* We need to watch for attribute changes in addition to
|
||||
* creation, because when a device is first created, it has
|
||||
* permissions that we can't read. When udev chmods it to
|
||||
* something that we maybe *can* read, we'll get an
|
||||
* IN_ATTRIB event to tell us. */
|
||||
if (inotify_add_watch(inotify_fd, "/dev",
|
||||
IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
|
||||
close(inotify_fd);
|
||||
inotify_fd = -1;
|
||||
SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
|
||||
"Unable to add inotify watch, falling back to polling: %s",
|
||||
strerror (errno));
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_HIDAPI_discovery.m_bCanGetNotifications = SDL_TRUE;
|
||||
#endif /* HAVE_INOTIFY */
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -328,31 +425,78 @@ HIDAPI_UpdateDiscovery()
|
||||
#endif
|
||||
|
||||
#if defined(SDL_USE_LIBUDEV)
|
||||
if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
|
||||
/* Drain all notification events.
|
||||
* We don't expect a lot of device notifications so just
|
||||
* do a new discovery on any kind or number of notifications.
|
||||
* This could be made more restrictive if necessary.
|
||||
*/
|
||||
for (;;) {
|
||||
struct pollfd PollUdev;
|
||||
struct udev_device *pUdevDevice;
|
||||
if (linux_enumeration_method == ENUMERATION_LIBUDEV) {
|
||||
if (SDL_HIDAPI_discovery.m_nUdevFd >= 0) {
|
||||
/* Drain all notification events.
|
||||
* We don't expect a lot of device notifications so just
|
||||
* do a new discovery on any kind or number of notifications.
|
||||
* This could be made more restrictive if necessary.
|
||||
*/
|
||||
for (;;) {
|
||||
struct pollfd PollUdev;
|
||||
struct udev_device *pUdevDevice;
|
||||
|
||||
PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
|
||||
PollUdev.events = POLLIN;
|
||||
if (poll(&PollUdev, 1, 0) != 1) {
|
||||
break;
|
||||
}
|
||||
PollUdev.fd = SDL_HIDAPI_discovery.m_nUdevFd;
|
||||
PollUdev.events = POLLIN;
|
||||
if (poll(&PollUdev, 1, 0) != 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
|
||||
|
||||
pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
|
||||
if (pUdevDevice) {
|
||||
usyms->udev_device_unref(pUdevDevice);
|
||||
pUdevDevice = usyms->udev_monitor_receive_device(SDL_HIDAPI_discovery.m_pUdevMonitor);
|
||||
if (pUdevDevice) {
|
||||
const char *action = NULL;
|
||||
action = usyms->udev_device_get_action(pUdevDevice);
|
||||
if (!action || SDL_strcmp(action, "add") == 0 || SDL_strcmp(action, "remove") == 0) {
|
||||
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
|
||||
}
|
||||
usyms->udev_device_unref(pUdevDevice);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
else
|
||||
#endif /* SDL_USE_LIBUDEV */
|
||||
{
|
||||
#if defined(HAVE_INOTIFY)
|
||||
if (inotify_fd >= 0) {
|
||||
union
|
||||
{
|
||||
struct inotify_event event;
|
||||
char storage[4096];
|
||||
char enough_for_inotify[sizeof (struct inotify_event) + NAME_MAX + 1];
|
||||
} buf;
|
||||
ssize_t bytes;
|
||||
size_t remain = 0;
|
||||
size_t len;
|
||||
|
||||
bytes = read(inotify_fd, &buf, sizeof (buf));
|
||||
|
||||
if (bytes > 0) {
|
||||
remain = (size_t) bytes;
|
||||
}
|
||||
|
||||
while (remain > 0) {
|
||||
if (buf.event.len > 0 &&
|
||||
!SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
|
||||
if (StrHasPrefix(buf.event.name, "hidraw") &&
|
||||
StrIsInteger(buf.event.name + strlen ("hidraw"))) {
|
||||
SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_TRUE;
|
||||
/* We found an hidraw change. We still continue to
|
||||
* drain the inotify fd to avoid leaving old
|
||||
* notifications in the queue. */
|
||||
}
|
||||
}
|
||||
|
||||
len = sizeof (struct inotify_event) + buf.event.len;
|
||||
remain -= len;
|
||||
|
||||
if (remain != 0) {
|
||||
memmove(&buf.storage[0], &buf.storage[len], remain);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* HAVE_INOTIFY */
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -376,7 +520,8 @@ HIDAPI_ShutdownDiscovery()
|
||||
#endif
|
||||
|
||||
#if defined(SDL_USE_LIBUDEV)
|
||||
if (usyms) {
|
||||
if (linux_enumeration_method == ENUMERATION_LIBUDEV &&
|
||||
usyms) {
|
||||
if (SDL_HIDAPI_discovery.m_pUdevMonitor) {
|
||||
usyms->udev_monitor_unref(SDL_HIDAPI_discovery.m_pUdevMonitor);
|
||||
}
|
||||
@@ -410,6 +555,12 @@ HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size)
|
||||
SDL_free(buffer);
|
||||
}
|
||||
|
||||
float
|
||||
HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max)
|
||||
{
|
||||
return output_min + (output_max - output_min) * (val - val_min) / (val_max - val_min);
|
||||
}
|
||||
|
||||
static void HIDAPI_JoystickDetect(void);
|
||||
static void HIDAPI_JoystickClose(SDL_Joystick * joystick);
|
||||
|
||||
@@ -437,8 +588,14 @@ HIDAPI_GetDeviceDriver(SDL_HIDAPI_Device *device)
|
||||
const Uint16 USAGE_MULTIAXISCONTROLLER = 0x0008;
|
||||
int i;
|
||||
SDL_GameControllerType type;
|
||||
SDL_JoystickGUID check_guid;
|
||||
|
||||
if (SDL_ShouldIgnoreJoystick(device->name, device->guid)) {
|
||||
/* Make sure we have a generic GUID here, otherwise if we pass a HIDAPI
|
||||
guid, this call will create a game controller mapping for the device.
|
||||
*/
|
||||
check_guid = device->guid;
|
||||
check_guid.data[14] = 0;
|
||||
if (SDL_ShouldIgnoreJoystick(device->name, check_guid)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -585,6 +742,28 @@ HIDAPI_JoystickInit(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(SDL_USE_LIBUDEV)
|
||||
if (linux_enumeration_method == ENUMERATION_UNSET) {
|
||||
if (SDL_getenv("SDL_HIDAPI_JOYSTICK_DISABLE_UDEV") != NULL) {
|
||||
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||
"udev disabled by SDL_HIDAPI_JOYSTICK_DISABLE_UDEV");
|
||||
linux_enumeration_method = ENUMERATION_FALLBACK;
|
||||
} else if (access("/.flatpak-info", F_OK) == 0
|
||||
|| access("/run/host/container-manager", F_OK) == 0) {
|
||||
/* Explicitly check `/.flatpak-info` because, for old versions of
|
||||
* Flatpak, this was the only available way to tell if we were in
|
||||
* a Flatpak container. */
|
||||
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||
"Container detected, disabling HIDAPI udev integration");
|
||||
linux_enumeration_method = ENUMERATION_FALLBACK;
|
||||
} else {
|
||||
SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
|
||||
"Using udev for HIDAPI joystick device discovery");
|
||||
linux_enumeration_method = ENUMERATION_LIBUDEV;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__MACOSX__) || defined(__IPHONEOS__) || defined(__TVOS__)
|
||||
/* The hidapi framwork is weak-linked on Apple platforms */
|
||||
int HID_API_EXPORT HID_API_CALL hid_init(void) __attribute__((weak_import));
|
||||
@@ -883,12 +1062,13 @@ HIDAPI_UpdateDeviceList(void)
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove any devices that weren't seen */
|
||||
/* Remove any devices that weren't seen or have been disconnected due to read errors */
|
||||
device = SDL_HIDAPI_devices;
|
||||
while (device) {
|
||||
SDL_HIDAPI_Device *next = device->next;
|
||||
|
||||
if (!device->seen) {
|
||||
if (!device->seen ||
|
||||
(device->driver && device->num_joysticks == 0 && !device->dev)) {
|
||||
HIDAPI_DelDevice(device);
|
||||
}
|
||||
device = next;
|
||||
@@ -1239,6 +1419,13 @@ HIDAPI_JoystickQuit(void)
|
||||
|
||||
SDL_HIDAPI_QuitRumble();
|
||||
|
||||
#if defined(HAVE_INOTIFY)
|
||||
if (inotify_fd >= 0) {
|
||||
close(inotify_fd);
|
||||
inotify_fd = -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
while (SDL_HIDAPI_devices) {
|
||||
HIDAPI_DelDevice(SDL_HIDAPI_devices);
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
|
||||
Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
@@ -31,12 +31,13 @@
|
||||
#include "../usb_ids.h"
|
||||
|
||||
/* This is the full set of HIDAPI drivers available */
|
||||
#define SDL_JOYSTICK_HIDAPI_GAMECUBE
|
||||
#define SDL_JOYSTICK_HIDAPI_PS4
|
||||
#define SDL_JOYSTICK_HIDAPI_PS5
|
||||
#define SDL_JOYSTICK_HIDAPI_STADIA
|
||||
#define SDL_JOYSTICK_HIDAPI_SWITCH
|
||||
#define SDL_JOYSTICK_HIDAPI_XBOX360
|
||||
#define SDL_JOYSTICK_HIDAPI_XBOXONE
|
||||
#define SDL_JOYSTICK_HIDAPI_GAMECUBE
|
||||
|
||||
#if defined(__IPHONEOS__) || defined(__TVOS__) || defined(__ANDROID__)
|
||||
/* Very basic Steam Controller support on mobile devices */
|
||||
@@ -105,14 +106,15 @@ typedef struct _SDL_HIDAPI_DeviceDriver
|
||||
|
||||
|
||||
/* HIDAPI device support */
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS4;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSteam;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverSwitch;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
|
||||
extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube;
|
||||
|
||||
/* Return true if a HID device is present and supported as a joystick */
|
||||
extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
|
||||
@@ -123,6 +125,8 @@ extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickI
|
||||
|
||||
extern void HIDAPI_DumpPacket(const char *prefix, Uint8 *data, int size);
|
||||
|
||||
extern float HIDAPI_RemapVal(float val, float val_min, float val_max, float output_min, float output_max);
|
||||
|
||||
#endif /* SDL_JOYSTICK_HIDAPI_H */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
Simple DirectMedia Layer
|
||||
Copyright (C) 2020 Valve Corporation
|
||||
Copyright (C) 2021 Valve Corporation
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
|
Reference in New Issue
Block a user