Skip to content

Commit

Permalink
Added custom names for some controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Dec 12, 2019
1 parent e029fdb commit 0f52916
Show file tree
Hide file tree
Showing 8 changed files with 538 additions and 486 deletions.
6 changes: 6 additions & 0 deletions src/joystick/SDL_joystick.c
Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions src/joystick/SDL_joystick_c.h
Expand Up @@ -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);
Expand Down
903 changes: 460 additions & 443 deletions src/joystick/controller_type.h

Large diffs are not rendered by default.

36 changes: 21 additions & 15 deletions src/joystick/darwin/SDL_sysjoystick.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 */
Expand Down
6 changes: 6 additions & 0 deletions src/joystick/hidapi/SDL_hidapijoystick.c
Expand Up @@ -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));
Expand Down
26 changes: 16 additions & 10 deletions src/joystick/linux/SDL_sysjoystick.c
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 12 additions & 7 deletions src/joystick/windows/SDL_dinputjoystick.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
25 changes: 14 additions & 11 deletions src/joystick/windows/SDL_xinputjoystick.c
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 0f52916

Please sign in to comment.