Fixed setting the controller name for the RAWINPUT driver
authorSam Lantinga
Tue, 07 Apr 2020 11:17:52 -0700
changeset 1370097f6cba13e49
parent 13699 fd93b560e3b0
child 13701 3cc182fa6110
Fixed setting the controller name for the RAWINPUT driver
src/joystick/windows/SDL_rawinputjoystick.c
     1.1 --- a/src/joystick/windows/SDL_rawinputjoystick.c	Tue Apr 07 11:17:22 2020 -0700
     1.2 +++ b/src/joystick/windows/SDL_rawinputjoystick.c	Tue Apr 07 11:17:52 2020 -0700
     1.3 @@ -270,8 +270,8 @@
     1.4      RID_DEVICE_INFO rdi;
     1.5      UINT rdi_size = sizeof(rdi);
     1.6      char dev_name[128];
     1.7 +    UINT name_size = SDL_arraysize(dev_name);
     1.8      const char *name;
     1.9 -    UINT name_size = SDL_arraysize(dev_name);
    1.10      SDL_RAWINPUT_Device *curr, *last;
    1.11  
    1.12      SDL_assert(!RAWINPUT_DeviceFromHandle(hDevice));
    1.13 @@ -313,18 +313,46 @@
    1.14          device->guid.data[15] = 0;
    1.15      }
    1.16  
    1.17 -    if (!device->name) {
    1.18 -        size_t name_size = (6 + 1 + 6 + 1);
    1.19 -        CHECK(device->name = SDL_callocStructs(char, name_size));
    1.20 -        SDL_snprintf(device->name, name_size, "0x%.4x/0x%.4x", device->vendor_id, device->product_id);
    1.21 -    }
    1.22 -
    1.23      CHECK(device->driver = RAWINPUT_GetDeviceDriver(device));
    1.24  
    1.25      name = device->driver->GetDeviceName(device->vendor_id, device->product_id);
    1.26      if (name) {
    1.27 -        SDL_free(device->name);
    1.28          device->name = SDL_strdup(name);
    1.29 +    } else {
    1.30 +        char *manufacturer_string = NULL;
    1.31 +        char *product_string = NULL;
    1.32 +        HMODULE hHID;
    1.33 +
    1.34 +        hHID = LoadLibrary( TEXT( "hid.dll" ) );
    1.35 +        if (hHID) {
    1.36 +            typedef BOOLEAN (WINAPI * HidD_GetStringFunc)(HANDLE HidDeviceObject, PVOID Buffer, ULONG BufferLength);
    1.37 +            HidD_GetStringFunc GetManufacturerString = (HidD_GetStringFunc)GetProcAddress(hHID, "HidD_GetManufacturerString");
    1.38 +            HidD_GetStringFunc GetProductString = (HidD_GetStringFunc)GetProcAddress(hHID, "HidD_GetProductString");
    1.39 +            if (GetManufacturerString && GetProductString) {
    1.40 +                HANDLE hFile = CreateFileA(dev_name, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    1.41 +                if (hFile != INVALID_HANDLE_VALUE) {
    1.42 +                    WCHAR string[128];
    1.43 +
    1.44 +                    if (GetManufacturerString(hFile, string, sizeof(string))) {
    1.45 +                        manufacturer_string = WIN_StringToUTF8(string);
    1.46 +                    }
    1.47 +                    if (GetProductString(hFile, string, sizeof(string))) {
    1.48 +                        product_string = WIN_StringToUTF8(string);
    1.49 +                    }
    1.50 +                    CloseHandle(hFile);
    1.51 +                }
    1.52 +            }
    1.53 +            FreeLibrary(hHID);
    1.54 +        }
    1.55 +
    1.56 +        device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string);
    1.57 +
    1.58 +        if (manufacturer_string) {
    1.59 +            SDL_free(manufacturer_string);
    1.60 +        }
    1.61 +        if (product_string) {
    1.62 +            SDL_free(product_string);
    1.63 +        }
    1.64      }
    1.65  
    1.66  #ifdef DEBUG_RAWINPUT