mirror of
https://github.com/encounter/SDL.git
synced 2025-12-12 06:45:10 +00:00
Added SDL_JoystickGetSerial() and SDL_GameControllerGetSerial()
This commit is contained in:
@@ -135,34 +135,11 @@ HIDAPI_DriverPS4_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static SDL_bool ReadFeatureReport(hid_device *dev, Uint8 report_id, Uint8 *data, size_t size)
|
||||
static int ReadFeatureReport(hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
|
||||
{
|
||||
Uint8 report[USB_PACKET_LENGTH + 1];
|
||||
|
||||
SDL_memset(report, 0, sizeof(report));
|
||||
SDL_memset(report, 0, length);
|
||||
report[0] = report_id;
|
||||
if (hid_get_feature_report(dev, report, sizeof(report)) < 0) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
SDL_memcpy(data, report, SDL_min(size, sizeof(report)));
|
||||
return SDL_TRUE;
|
||||
}
|
||||
|
||||
static SDL_bool CheckUSBConnected(hid_device *dev)
|
||||
{
|
||||
int i;
|
||||
Uint8 data[16];
|
||||
|
||||
/* This will fail if we're on Bluetooth */
|
||||
if (ReadFeatureReport(dev, k_ePS4FeatureReportIdSerialNumber, data, sizeof(data))) {
|
||||
for (i = 0; i < sizeof(data); ++i) {
|
||||
if (data[i] != 0x00) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
}
|
||||
/* Maybe the dongle without a connected controller? */
|
||||
}
|
||||
return SDL_FALSE;
|
||||
return hid_get_feature_report(dev, report, length);
|
||||
}
|
||||
|
||||
static SDL_bool HIDAPI_DriverPS4_CanRumble(Uint16 vendor_id, Uint16 product_id)
|
||||
@@ -310,7 +287,36 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
if (ctx->is_dongle) {
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
} else if (device->vendor_id == USB_VENDOR_SONY) {
|
||||
ctx->is_bluetooth = !CheckUSBConnected(device->dev);
|
||||
int i;
|
||||
Uint8 data[USB_PACKET_LENGTH];
|
||||
int length;
|
||||
|
||||
/* This will fail if we're on Bluetooth */
|
||||
length = ReadFeatureReport(device->dev, k_ePS4FeatureReportIdSerialNumber, data, sizeof(data));
|
||||
if (length >= 7) {
|
||||
SDL_bool had_data = SDL_FALSE;
|
||||
|
||||
for (i = 0; i < length; ++i) {
|
||||
if (data[i] != 0x00) {
|
||||
had_data = SDL_TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (had_data) {
|
||||
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);
|
||||
} else {
|
||||
/* Maybe the dongle without a connected controller? */
|
||||
}
|
||||
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
} else {
|
||||
ctx->is_bluetooth = SDL_TRUE;
|
||||
}
|
||||
} else {
|
||||
/* Third party controllers appear to all be wired */
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
|
||||
@@ -140,16 +140,11 @@ HIDAPI_DriverPS5_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static SDL_bool ReadFeatureReport(hid_device *dev, Uint8 report_id)
|
||||
static int ReadFeatureReport(hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
|
||||
{
|
||||
Uint8 report[USB_PACKET_LENGTH + 1];
|
||||
|
||||
SDL_memset(report, 0, sizeof(report));
|
||||
SDL_memset(report, 0, length);
|
||||
report[0] = report_id;
|
||||
if (hid_get_feature_report(dev, report, sizeof(report)) < 0) {
|
||||
return SDL_FALSE;
|
||||
}
|
||||
return SDL_TRUE;
|
||||
return hid_get_feature_report(dev, report, length);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -277,6 +272,7 @@ static SDL_bool
|
||||
HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
{
|
||||
SDL_DriverPS5_Context *ctx;
|
||||
Uint8 data[USB_PACKET_LENGTH];
|
||||
|
||||
ctx = (SDL_DriverPS5_Context *)SDL_calloc(1, sizeof(*ctx));
|
||||
if (!ctx) {
|
||||
@@ -295,7 +291,13 @@ HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
/* Read the serial number (Bluetooth address in reverse byte order)
|
||||
This will also enable enhanced reports over Bluetooth
|
||||
*/
|
||||
ReadFeatureReport(device->dev, k_EPS5FeatureReportIdSerialNumber);
|
||||
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);
|
||||
}
|
||||
|
||||
/* Initialize player index (needed for setting LEDs) */
|
||||
ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
|
||||
|
||||
@@ -690,6 +690,24 @@ HIDAPI_JoystickGetCount(void)
|
||||
return SDL_HIDAPI_numjoysticks;
|
||||
}
|
||||
|
||||
static char *
|
||||
HIDAPI_ConvertString(const wchar_t *wide_string)
|
||||
{
|
||||
char *string = NULL;
|
||||
|
||||
if (wide_string) {
|
||||
string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
|
||||
if (!string) {
|
||||
if (sizeof(wchar_t) == sizeof(Uint16)) {
|
||||
string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
|
||||
} else if (sizeof(wchar_t) == sizeof(Uint32)) {
|
||||
string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)wide_string, (SDL_wcslen(wide_string)+1)*sizeof(wchar_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
static void
|
||||
HIDAPI_AddDevice(struct hid_device_info *info)
|
||||
{
|
||||
@@ -743,29 +761,9 @@ HIDAPI_AddDevice(struct hid_device_info *info)
|
||||
|
||||
/* Need the device name before getting the driver to know whether to ignore this device */
|
||||
{
|
||||
char *manufacturer_string = NULL;
|
||||
char *product_string = NULL;
|
||||
|
||||
if (info->manufacturer_string) {
|
||||
manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
|
||||
if (!manufacturer_string) {
|
||||
if (sizeof(wchar_t) == sizeof(Uint16)) {
|
||||
manufacturer_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
|
||||
} else if (sizeof(wchar_t) == sizeof(Uint32)) {
|
||||
manufacturer_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (info->product_string) {
|
||||
product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
|
||||
if (!product_string) {
|
||||
if (sizeof(wchar_t) == sizeof(Uint16)) {
|
||||
product_string = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
|
||||
} else if (sizeof(wchar_t) == sizeof(Uint32)) {
|
||||
product_string = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
char *manufacturer_string = HIDAPI_ConvertString(info->manufacturer_string);
|
||||
char *product_string = HIDAPI_ConvertString(info->product_string);
|
||||
char *serial_number = HIDAPI_ConvertString(info->serial_number);
|
||||
|
||||
device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string);
|
||||
|
||||
@@ -776,7 +774,14 @@ HIDAPI_AddDevice(struct hid_device_info *info)
|
||||
SDL_free(product_string);
|
||||
}
|
||||
|
||||
if (serial_number && *serial_number) {
|
||||
device->serial = serial_number;
|
||||
} else {
|
||||
SDL_free(serial_number);
|
||||
}
|
||||
|
||||
if (!device->name) {
|
||||
SDL_free(device->serial);
|
||||
SDL_free(device->path);
|
||||
SDL_free(device);
|
||||
return;
|
||||
@@ -793,7 +798,7 @@ HIDAPI_AddDevice(struct hid_device_info *info)
|
||||
HIDAPI_SetupDeviceDriver(device);
|
||||
|
||||
#ifdef DEBUG_HIDAPI
|
||||
SDL_Log("Added HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->hint : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
|
||||
SDL_Log("Added HIDAPI device '%s' VID 0x%.4x, PID 0x%.4x, version %d, serial %s, interface %d, interface_class %d, interface_subclass %d, interface_protocol %d, usage page 0x%.4x, usage 0x%.4x, path = %s, driver = %s (%s)\n", device->name, device->vendor_id, device->product_id, device->version, device->serial ? device->serial : "NONE", device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol, device->usage_page, device->usage, device->path, device->driver ? device->driver->hint : "NONE", device->driver && device->driver->enabled ? "ENABLED" : "DISABLED");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -813,6 +818,7 @@ HIDAPI_DelDevice(SDL_HIDAPI_Device *device)
|
||||
HIDAPI_CleanupDeviceDriver(device);
|
||||
|
||||
SDL_DestroyMutex(device->dev_lock);
|
||||
SDL_free(device->serial);
|
||||
SDL_free(device->name);
|
||||
SDL_free(device->path);
|
||||
SDL_free(device);
|
||||
@@ -1051,6 +1057,10 @@ HIDAPI_JoystickOpen(SDL_Joystick * joystick, int device_index)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!joystick->serial && device->serial) {
|
||||
joystick->serial = SDL_strdup(device->serial);
|
||||
}
|
||||
|
||||
joystick->hwdata = hwdata;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -61,6 +61,7 @@ typedef struct _SDL_HIDAPI_Device
|
||||
Uint16 vendor_id;
|
||||
Uint16 product_id;
|
||||
Uint16 version;
|
||||
char *serial;
|
||||
SDL_JoystickGUID guid;
|
||||
int interface_number; /* Available on Windows and Linux */
|
||||
int interface_class;
|
||||
|
||||
Reference in New Issue
Block a user