src/joystick/linux/SDL_sysjoystick.c
changeset 13611 bcbfec15e25f
parent 13480 6a144eb5e1f1
child 13670 63387e8920f5
     1.1 --- a/src/joystick/linux/SDL_sysjoystick.c	Thu Mar 12 19:47:28 2020 -0700
     1.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Thu Mar 12 19:47:30 2020 -0700
     1.3 @@ -88,20 +88,6 @@
     1.4      (((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
     1.5  #define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
     1.6  
     1.7 -static int
     1.8 -PrefixMatch(const char *a, const char *b)
     1.9 -{
    1.10 -    int matchlen = 0;
    1.11 -    while (*a && *b) {
    1.12 -        if (*a++ == *b++) {
    1.13 -            ++matchlen;
    1.14 -        } else {
    1.15 -            break;
    1.16 -        }
    1.17 -    }
    1.18 -    return matchlen;
    1.19 -}
    1.20 -
    1.21  static void
    1.22  FixupDeviceInfoForMapping(int fd, struct input_id *inpid)
    1.23  {
    1.24 @@ -120,12 +106,12 @@
    1.25  
    1.26  
    1.27  static int
    1.28 -IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *guid)
    1.29 +IsJoystick(int fd, char **name_return, SDL_JoystickGUID *guid)
    1.30  {
    1.31      struct input_id inpid;
    1.32      Uint16 *guid16 = (Uint16 *)guid->data;
    1.33 -    const char *name;
    1.34 -    const char *spot;
    1.35 +    char *name;
    1.36 +    char product_string[128];
    1.37  
    1.38  #if !SDL_USE_LIBUDEV
    1.39      /* When udev is enabled we only get joystick devices here, so there's no need to test them */
    1.40 @@ -149,27 +135,19 @@
    1.41          return 0;
    1.42      }
    1.43  
    1.44 -    name = SDL_GetCustomJoystickName(inpid.vendor, inpid.product);
    1.45 -    if (name) {
    1.46 -        SDL_strlcpy(namebuf, name, namebuflen);
    1.47 -    } else {
    1.48 -        if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
    1.49 -            return 0;
    1.50 -        }
    1.51 +    if (ioctl(fd, EVIOCGNAME(sizeof(product_string)), product_string) < 0) {
    1.52 +        return 0;
    1.53 +    }
    1.54  
    1.55 -        /* Remove duplicate manufacturer in the name */
    1.56 -        for (spot = namebuf + 1; *spot; ++spot) {
    1.57 -            int matchlen = PrefixMatch(namebuf, spot);
    1.58 -            if (matchlen > 0 && spot[matchlen - 1] == ' ') {
    1.59 -                SDL_memmove(namebuf, spot, SDL_strlen(spot)+1);
    1.60 -                break;
    1.61 -            }
    1.62 -        }
    1.63 +    name = SDL_CreateJoystickName(inpid.vendor, inpid.product, NULL, product_string);
    1.64 +    if (!name) {
    1.65 +        return 0;
    1.66      }
    1.67  
    1.68  #ifdef SDL_JOYSTICK_HIDAPI
    1.69 -    if (HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, namebuf)) {
    1.70 +    if (HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, name)) {
    1.71          /* The HIDAPI driver is taking care of this device */
    1.72 +        SDL_free(name);
    1.73          return 0;
    1.74      }
    1.75  #endif
    1.76 @@ -177,7 +155,7 @@
    1.77      FixupDeviceInfoForMapping(fd, &inpid);
    1.78  
    1.79  #ifdef DEBUG_JOYSTICK
    1.80 -    printf("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", namebuf, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
    1.81 +    printf("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", name, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
    1.82  #endif
    1.83  
    1.84      SDL_memset(guid->data, 0, sizeof(guid->data));
    1.85 @@ -195,12 +173,14 @@
    1.86          *guid16++ = SDL_SwapLE16(inpid.version);
    1.87          *guid16++ = 0;
    1.88      } else {
    1.89 -        SDL_strlcpy((char*)guid16, namebuf, sizeof(guid->data) - 4);
    1.90 +        SDL_strlcpy((char*)guid16, name, sizeof(guid->data) - 4);
    1.91      }
    1.92  
    1.93 -    if (SDL_ShouldIgnoreJoystick(namebuf, *guid)) {
    1.94 +    if (SDL_ShouldIgnoreJoystick(name, *guid)) {
    1.95 +        SDL_free(name);
    1.96          return 0;
    1.97      }
    1.98 +    *name_return = name;
    1.99      return 1;
   1.100  }
   1.101  
   1.102 @@ -236,7 +216,7 @@
   1.103      struct stat sb;
   1.104      int fd = -1;
   1.105      int isstick = 0;
   1.106 -    char namebuf[128];
   1.107 +    char *name = NULL;
   1.108      SDL_JoystickGUID guid;
   1.109      SDL_joylist_item *item;
   1.110  
   1.111 @@ -264,7 +244,7 @@
   1.112      printf("Checking %s\n", path);
   1.113  #endif
   1.114  
   1.115 -    isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid);
   1.116 +    isstick = IsJoystick(fd, &name, &guid);
   1.117      close(fd);
   1.118      if (!isstick) {
   1.119          return -1;
   1.120 @@ -278,7 +258,7 @@
   1.121      SDL_zerop(item);
   1.122      item->devnum = sb.st_rdev;
   1.123      item->path = SDL_strdup(path);
   1.124 -    item->name = SDL_strdup(namebuf);
   1.125 +    item->name = name;
   1.126      item->guid = guid;
   1.127  
   1.128      if ((item->path == NULL) || (item->name == NULL)) {