mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-05-28 11:57:24 +08:00
Added custom names for some controllers
This commit is contained in:
@@ -1164,6 +1164,12 @@ void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *prod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *
|
||||||
|
SDL_GetCustomJoystickName(Uint16 vendor, Uint16 product)
|
||||||
|
{
|
||||||
|
return GuessControllerName(vendor, product);
|
||||||
|
}
|
||||||
|
|
||||||
SDL_bool
|
SDL_bool
|
||||||
SDL_IsJoystickNintendoSwitchPro(Uint16 vendor, Uint16 product)
|
SDL_IsJoystickNintendoSwitchPro(Uint16 vendor, Uint16 product)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ extern int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id);
|
|||||||
/* Function to extract information from an SDL joystick GUID */
|
/* Function to extract information from an SDL joystick GUID */
|
||||||
extern void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version);
|
extern void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version);
|
||||||
|
|
||||||
|
/* Function to get a custom name for a controller, if it's available */
|
||||||
|
extern const char *SDL_GetCustomJoystickName(Uint16 vendor, Uint16 product);
|
||||||
|
|
||||||
/* Function to return the type of a controller */
|
/* Function to return the type of a controller */
|
||||||
extern SDL_GameControllerType SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name);
|
extern SDL_GameControllerType SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name);
|
||||||
extern SDL_GameControllerType SDL_GetJoystickGameControllerType(Uint16 vendor, Uint16 product, const char *name);
|
extern SDL_GameControllerType SDL_GetJoystickGameControllerType(Uint16 vendor, Uint16 product, const char *name);
|
||||||
|
|||||||
+452
-435
File diff suppressed because it is too large
Load Diff
@@ -399,6 +399,7 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
|
|||||||
Sint32 vendor = 0;
|
Sint32 vendor = 0;
|
||||||
Sint32 product = 0;
|
Sint32 product = 0;
|
||||||
Sint32 version = 0;
|
Sint32 version = 0;
|
||||||
|
const char *name;
|
||||||
char manufacturer_string[256];
|
char manufacturer_string[256];
|
||||||
char product_string[256];
|
char product_string[256];
|
||||||
CFTypeRef refCF = NULL;
|
CFTypeRef refCF = NULL;
|
||||||
@@ -427,21 +428,6 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
|
|||||||
|
|
||||||
pDevice->deviceRef = hidDevice;
|
pDevice->deviceRef = hidDevice;
|
||||||
|
|
||||||
/* get device name */
|
|
||||||
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey));
|
|
||||||
if ((!refCF) || (!CFStringGetCString(refCF, manufacturer_string, sizeof(manufacturer_string), kCFStringEncodingUTF8))) {
|
|
||||||
manufacturer_string[0] = '\0';
|
|
||||||
}
|
|
||||||
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
|
|
||||||
if ((!refCF) || (!CFStringGetCString(refCF, product_string, sizeof(product_string), kCFStringEncodingUTF8))) {
|
|
||||||
SDL_strlcpy(product_string, "Unidentified joystick", sizeof(product_string));
|
|
||||||
}
|
|
||||||
if (SDL_strncasecmp(manufacturer_string, product_string, SDL_strlen(manufacturer_string)) == 0) {
|
|
||||||
SDL_strlcpy(pDevice->product, product_string, sizeof(pDevice->product));
|
|
||||||
} else {
|
|
||||||
SDL_snprintf(pDevice->product, sizeof(pDevice->product), "%s %s", manufacturer_string, product_string);
|
|
||||||
}
|
|
||||||
|
|
||||||
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
|
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
|
||||||
if (refCF) {
|
if (refCF) {
|
||||||
CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor);
|
CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor);
|
||||||
@@ -457,6 +443,26 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
|
|||||||
CFNumberGetValue(refCF, kCFNumberSInt32Type, &version);
|
CFNumberGetValue(refCF, kCFNumberSInt32Type, &version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* get device name */
|
||||||
|
name = SDL_GetCustomJoystickName(vendor, product);
|
||||||
|
if (name) {
|
||||||
|
SDL_strlcpy(pDevice->product, name, sizeof(pDevice->product));
|
||||||
|
} else {
|
||||||
|
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey));
|
||||||
|
if ((!refCF) || (!CFStringGetCString(refCF, manufacturer_string, sizeof(manufacturer_string), kCFStringEncodingUTF8))) {
|
||||||
|
manufacturer_string[0] = '\0';
|
||||||
|
}
|
||||||
|
refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
|
||||||
|
if ((!refCF) || (!CFStringGetCString(refCF, product_string, sizeof(product_string), kCFStringEncodingUTF8))) {
|
||||||
|
SDL_strlcpy(product_string, "Unidentified joystick", sizeof(product_string));
|
||||||
|
}
|
||||||
|
if (SDL_strncasecmp(manufacturer_string, product_string, SDL_strlen(manufacturer_string)) == 0) {
|
||||||
|
SDL_strlcpy(pDevice->product, product_string, sizeof(pDevice->product));
|
||||||
|
} else {
|
||||||
|
SDL_snprintf(pDevice->product, sizeof(pDevice->product), "%s %s", manufacturer_string, product_string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef SDL_JOYSTICK_HIDAPI
|
#ifdef SDL_JOYSTICK_HIDAPI
|
||||||
if (HIDAPI_IsDevicePresent(vendor, product, version, pDevice->product)) {
|
if (HIDAPI_IsDevicePresent(vendor, product, version, pDevice->product)) {
|
||||||
/* The HIDAPI driver is taking care of this device */
|
/* The HIDAPI driver is taking care of this device */
|
||||||
|
|||||||
@@ -603,6 +603,12 @@ HIDAPI_AddDevice(struct hid_device_info *info)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Need the device name before getting the driver to know whether to ignore this device */
|
/* Need the device name before getting the driver to know whether to ignore this device */
|
||||||
|
if (!device->name) {
|
||||||
|
const char *name = SDL_GetCustomJoystickName(device->vendor_id, device->product_id);
|
||||||
|
if (name) {
|
||||||
|
device->name = SDL_strdup(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!device->name && info->manufacturer_string && info->product_string) {
|
if (!device->name && info->manufacturer_string && info->product_string) {
|
||||||
char *manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
|
char *manufacturer_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->manufacturer_string, (SDL_wcslen(info->manufacturer_string)+1)*sizeof(wchar_t));
|
||||||
char *product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
|
char *product_string = SDL_iconv_string("UTF-8", "WCHAR_T", (char*)info->product_string, (SDL_wcslen(info->product_string)+1)*sizeof(wchar_t));
|
||||||
|
|||||||
@@ -107,6 +107,7 @@ IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *gui
|
|||||||
{
|
{
|
||||||
struct input_id inpid;
|
struct input_id inpid;
|
||||||
Uint16 *guid16 = (Uint16 *)guid->data;
|
Uint16 *guid16 = (Uint16 *)guid->data;
|
||||||
|
const char *name;
|
||||||
const char *spot;
|
const char *spot;
|
||||||
|
|
||||||
#if !SDL_USE_LIBUDEV
|
#if !SDL_USE_LIBUDEV
|
||||||
@@ -127,23 +128,28 @@ IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *gui
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remove duplicate manufacturer in the name */
|
|
||||||
for (spot = namebuf + 1; *spot; ++spot) {
|
|
||||||
int matchlen = PrefixMatch(namebuf, spot);
|
|
||||||
if (matchlen > 0 && spot[matchlen - 1] == ' ') {
|
|
||||||
SDL_memmove(namebuf, spot, SDL_strlen(spot)+1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ioctl(fd, EVIOCGID, &inpid) < 0) {
|
if (ioctl(fd, EVIOCGID, &inpid) < 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name = SDL_GetCustomJoystickName(inpid.vendor, inpid.product);
|
||||||
|
if (name) {
|
||||||
|
SDL_strlcpy(namebuf, name, namebuflen);
|
||||||
|
} else {
|
||||||
|
if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Remove duplicate manufacturer in the name */
|
||||||
|
for (spot = namebuf + 1; *spot; ++spot) {
|
||||||
|
int matchlen = PrefixMatch(namebuf, spot);
|
||||||
|
if (matchlen > 0 && spot[matchlen - 1] == ' ') {
|
||||||
|
SDL_memmove(namebuf, spot, SDL_strlen(spot)+1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef SDL_JOYSTICK_HIDAPI
|
#ifdef SDL_JOYSTICK_HIDAPI
|
||||||
if (HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, namebuf)) {
|
if (HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, namebuf)) {
|
||||||
/* The HIDAPI driver is taking care of this device */
|
/* The HIDAPI driver is taking care of this device */
|
||||||
|
|||||||
@@ -521,6 +521,7 @@ EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
|
|||||||
Uint16 product = 0;
|
Uint16 product = 0;
|
||||||
Uint16 version = 0;
|
Uint16 version = 0;
|
||||||
WCHAR hidPath[MAX_PATH];
|
WCHAR hidPath[MAX_PATH];
|
||||||
|
const char *name;
|
||||||
|
|
||||||
if (devtype == DI8DEVTYPE_SUPPLEMENTAL) {
|
if (devtype == DI8DEVTYPE_SUPPLEMENTAL) {
|
||||||
/* Add any supplemental devices that should be ignored here */
|
/* Add any supplemental devices that should be ignored here */
|
||||||
@@ -605,14 +606,7 @@ EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
|
|||||||
|
|
||||||
SDL_zerop(pNewJoystick);
|
SDL_zerop(pNewJoystick);
|
||||||
SDL_wcslcpy(pNewJoystick->hidPath, hidPath, SDL_arraysize(pNewJoystick->hidPath));
|
SDL_wcslcpy(pNewJoystick->hidPath, hidPath, SDL_arraysize(pNewJoystick->hidPath));
|
||||||
pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
|
|
||||||
if (!pNewJoystick->joystickname) {
|
|
||||||
SDL_free(pNewJoystick);
|
|
||||||
return DIENUM_CONTINUE; /* better luck next time? */
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_memcpy(&pNewJoystick->dxdevice, pdidInstance, sizeof(DIDEVICEINSTANCE));
|
SDL_memcpy(&pNewJoystick->dxdevice, pdidInstance, sizeof(DIDEVICEINSTANCE));
|
||||||
|
|
||||||
SDL_memset(pNewJoystick->guid.data, 0, sizeof(pNewJoystick->guid.data));
|
SDL_memset(pNewJoystick->guid.data, 0, sizeof(pNewJoystick->guid.data));
|
||||||
|
|
||||||
guid16 = (Uint16 *)pNewJoystick->guid.data;
|
guid16 = (Uint16 *)pNewJoystick->guid.data;
|
||||||
@@ -635,6 +629,17 @@ EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
|
|||||||
SDL_strlcpy((char*)guid16, pNewJoystick->joystickname, sizeof(pNewJoystick->guid.data) - 4);
|
SDL_strlcpy((char*)guid16, pNewJoystick->joystickname, sizeof(pNewJoystick->guid.data) - 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name = SDL_GetCustomJoystickName(vendor, product);
|
||||||
|
if (name) {
|
||||||
|
pNewJoystick->joystickname = SDL_strdup(name);
|
||||||
|
} else {
|
||||||
|
pNewJoystick->joystickname = WIN_StringToUTF8(pdidInstance->tszProductName);
|
||||||
|
}
|
||||||
|
if (!pNewJoystick->joystickname) {
|
||||||
|
SDL_free(pNewJoystick);
|
||||||
|
return DIENUM_CONTINUE; /* better luck next time? */
|
||||||
|
}
|
||||||
|
|
||||||
if (SDL_strstr(pNewJoystick->joystickname, " XINPUT ") != NULL) {
|
if (SDL_strstr(pNewJoystick->joystickname, " XINPUT ") != NULL) {
|
||||||
/* This is a duplicate interface for a controller that will show up with XInput,
|
/* This is a duplicate interface for a controller that will show up with XInput,
|
||||||
e.g. Xbox One Elite Series 2 in Bluetooth mode.
|
e.g. Xbox One Elite Series 2 in Bluetooth mode.
|
||||||
|
|||||||
@@ -220,6 +220,7 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
|
|||||||
Uint16 vendor = 0;
|
Uint16 vendor = 0;
|
||||||
Uint16 product = 0;
|
Uint16 product = 0;
|
||||||
Uint16 version = 0;
|
Uint16 version = 0;
|
||||||
|
const char *name;
|
||||||
JoyStick_DeviceData *pPrevJoystick = NULL;
|
JoyStick_DeviceData *pPrevJoystick = NULL;
|
||||||
JoyStick_DeviceData *pNewJoystick = *pContext;
|
JoyStick_DeviceData *pNewJoystick = *pContext;
|
||||||
|
|
||||||
@@ -247,22 +248,13 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
|
|||||||
pNewJoystick = pNewJoystick->pNext;
|
pNewJoystick = pNewJoystick->pNext;
|
||||||
}
|
}
|
||||||
|
|
||||||
pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData));
|
pNewJoystick = (JoyStick_DeviceData *)SDL_calloc(1, sizeof(JoyStick_DeviceData));
|
||||||
if (!pNewJoystick) {
|
if (!pNewJoystick) {
|
||||||
return; /* better luck next time? */
|
return; /* better luck next time? */
|
||||||
}
|
}
|
||||||
SDL_zerop(pNewJoystick);
|
|
||||||
|
|
||||||
pNewJoystick->joystickname = GetXInputName(userid, SubType);
|
|
||||||
if (!pNewJoystick->joystickname) {
|
|
||||||
SDL_free(pNewJoystick);
|
|
||||||
return; /* better luck next time? */
|
|
||||||
}
|
|
||||||
|
|
||||||
pNewJoystick->bXInputDevice = SDL_TRUE;
|
pNewJoystick->bXInputDevice = SDL_TRUE;
|
||||||
if (SDL_XInputUseOldJoystickMapping()) {
|
if (!SDL_XInputUseOldJoystickMapping()) {
|
||||||
SDL_zero(pNewJoystick->guid);
|
|
||||||
} else {
|
|
||||||
Uint16 *guid16 = (Uint16 *)pNewJoystick->guid.data;
|
Uint16 *guid16 = (Uint16 *)pNewJoystick->guid.data;
|
||||||
|
|
||||||
GuessXInputDevice(userid, &vendor, &product, &version);
|
GuessXInputDevice(userid, &vendor, &product, &version);
|
||||||
@@ -283,6 +275,17 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
|
|||||||
pNewJoystick->SubType = SubType;
|
pNewJoystick->SubType = SubType;
|
||||||
pNewJoystick->XInputUserId = userid;
|
pNewJoystick->XInputUserId = userid;
|
||||||
|
|
||||||
|
name = SDL_GetCustomJoystickName(vendor, product);
|
||||||
|
if (name) {
|
||||||
|
pNewJoystick->joystickname = SDL_strdup(name);
|
||||||
|
} else {
|
||||||
|
pNewJoystick->joystickname = GetXInputName(userid, SubType);
|
||||||
|
}
|
||||||
|
if (!pNewJoystick->joystickname) {
|
||||||
|
SDL_free(pNewJoystick);
|
||||||
|
return; /* better luck next time? */
|
||||||
|
}
|
||||||
|
|
||||||
if (SDL_ShouldIgnoreJoystick(pNewJoystick->joystickname, pNewJoystick->guid)) {
|
if (SDL_ShouldIgnoreJoystick(pNewJoystick->joystickname, pNewJoystick->guid)) {
|
||||||
SDL_free(pNewJoystick);
|
SDL_free(pNewJoystick);
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user