Skip to content
Permalink
Browse files

Added custom names for some controllers

  • Loading branch information
slouken committed Dec 12, 2019
1 parent e029fdb commit 0f52916027f68ffd6a20f319e485a328e8db7731
@@ -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_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 */
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 */
extern SDL_GameControllerType SDL_GetJoystickGameControllerTypeFromGUID(SDL_JoystickGUID guid, const char *name);
extern SDL_GameControllerType SDL_GetJoystickGameControllerType(Uint16 vendor, Uint16 product, const char *name);

Large diffs are not rendered by default.

@@ -399,6 +399,7 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
Sint32 vendor = 0;
Sint32 product = 0;
Sint32 version = 0;
const char *name;
char manufacturer_string[256];
char product_string[256];
CFTypeRef refCF = NULL;
@@ -427,21 +428,6 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)

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));
if (refCF) {
CFNumberGetValue(refCF, kCFNumberSInt32Type, &vendor);
@@ -457,6 +443,26 @@ GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
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
if (HIDAPI_IsDevicePresent(vendor, product, version, pDevice->product)) {
/* 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 */
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) {
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));
@@ -107,6 +107,7 @@ IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *gui
{
struct input_id inpid;
Uint16 *guid16 = (Uint16 *)guid->data;
const char *name;
const char *spot;

#if !SDL_USE_LIBUDEV
@@ -127,21 +128,26 @@ IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *gui
}
#endif

if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
if (ioctl(fd, EVIOCGID, &inpid) < 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;
name = SDL_GetCustomJoystickName(inpid.vendor, inpid.product);
if (name) {
SDL_strlcpy(namebuf, name, namebuflen);
} else {
if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
return 0;
}
}

if (ioctl(fd, EVIOCGID, &inpid) < 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
@@ -521,6 +521,7 @@ EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
Uint16 product = 0;
Uint16 version = 0;
WCHAR hidPath[MAX_PATH];
const char *name;

if (devtype == DI8DEVTYPE_SUPPLEMENTAL) {
/* Add any supplemental devices that should be ignored here */
@@ -605,14 +606,7 @@ EnumJoysticksCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)

SDL_zerop(pNewJoystick);
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_memset(pNewJoystick->guid.data, 0, sizeof(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);
}

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) {
/* This is a duplicate interface for a controller that will show up with XInput,
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 product = 0;
Uint16 version = 0;
const char *name;
JoyStick_DeviceData *pPrevJoystick = NULL;
JoyStick_DeviceData *pNewJoystick = *pContext;

@@ -247,22 +248,13 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
pNewJoystick = pNewJoystick->pNext;
}

pNewJoystick = (JoyStick_DeviceData *)SDL_malloc(sizeof(JoyStick_DeviceData));
pNewJoystick = (JoyStick_DeviceData *)SDL_calloc(1, sizeof(JoyStick_DeviceData));
if (!pNewJoystick) {
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;
if (SDL_XInputUseOldJoystickMapping()) {
SDL_zero(pNewJoystick->guid);
} else {
if (!SDL_XInputUseOldJoystickMapping()) {
Uint16 *guid16 = (Uint16 *)pNewJoystick->guid.data;

GuessXInputDevice(userid, &vendor, &product, &version);
@@ -283,6 +275,17 @@ AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
pNewJoystick->SubType = SubType;
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)) {
SDL_free(pNewJoystick);
return;

0 comments on commit 0f52916

Please sign in to comment.