winmm: Try to get full device names from the Windows Registry.
authorRyan C. Gordon <icculus@icculus.org>
Sun, 07 Aug 2016 01:48:38 -0400
changeset 10268a3faa23abdbb
parent 10267 0abfef2f7fc5
child 10269 6b259657cfd1
winmm: Try to get full device names from the Windows Registry.
src/audio/winmm/SDL_winmm.c
     1.1 --- a/src/audio/winmm/SDL_winmm.c	Sat Aug 06 23:05:02 2016 -0400
     1.2 +++ b/src/audio/winmm/SDL_winmm.c	Sun Aug 07 01:48:38 2016 -0400
     1.3 @@ -37,8 +37,7 @@
     1.4  #define WAVE_FORMAT_IEEE_FLOAT 0x0003
     1.5  #endif
     1.6  
     1.7 -/* !!! FIXME:
     1.8 -
     1.9 +/*
    1.10  WAVExxxCAPS gives you 31 bytes for the device name, and just truncates if it's
    1.11  longer. However, since WinXP, you can use the WAVExxxCAPS2 structure, which
    1.12  will give you a name GUID. The full name is in the Windows Registry under
    1.13 @@ -50,17 +49,74 @@
    1.14  
    1.15  http://web.archive.org/web/20131027093034/http://msdn.microsoft.com/en-us/library/windows/hardware/ff536382(v=vs.85).aspx
    1.16  
    1.17 +Always look this up in the registry if possible, because the strings are
    1.18 +different! At least on Win10, I see "Yeti Stereo Microphone" in the
    1.19 +Registry, and a unhelpful "Microphone(Yeti Stereo Microph" in winmm. Sigh.
    1.20  */
    1.21 +static char *
    1.22 +LookupDeviceName(const WCHAR *name, const GUID *guid)
    1.23 +{
    1.24 +    static const GUID nullguid = { 0 };
    1.25 +    const unsigned char *ptr;
    1.26 +    char keystr[128];
    1.27 +    WCHAR *strw = NULL;
    1.28 +    SDL_bool rc;
    1.29 +    HKEY hkey;
    1.30 +    DWORD len = 0;
    1.31 +    char *retval = NULL;
    1.32 +
    1.33 +    if (SDL_memcmp(guid, &nullguid, sizeof (*guid)) == 0) {
    1.34 +        return WIN_StringToUTF8(name);  /* No GUID, go with what we've got. */
    1.35 +    }
    1.36 +
    1.37 +    ptr = (const char *) guid;
    1.38 +    SDL_snprintf(keystr, sizeof (keystr),
    1.39 +        "System\\CurrentControlSet\\Control\\MediaCategories\\{%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
    1.40 +        ptr[3], ptr[2], ptr[1], ptr[0], ptr[5], ptr[4], ptr[7], ptr[6],
    1.41 +        ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15]);
    1.42 +
    1.43 +    strw = WIN_UTF8ToString(keystr);
    1.44 +    rc = (RegOpenKeyExW(HKEY_LOCAL_MACHINE, strw, 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS);
    1.45 +    SDL_free(strw);
    1.46 +    if (!rc) {
    1.47 +        return WIN_StringToUTF8(name);  /* oh well. */
    1.48 +    }
    1.49 +
    1.50 +    rc = (RegQueryValueExW(hkey, L"Name", NULL, NULL, NULL, &len) == ERROR_SUCCESS);
    1.51 +    if (!rc) {
    1.52 +        RegCloseKey(hkey);
    1.53 +        return WIN_StringToUTF8(name);  /* oh well. */
    1.54 +    }
    1.55 +
    1.56 +    strw = (WCHAR *) SDL_malloc(len + sizeof (WCHAR));
    1.57 +    if (!strw) {
    1.58 +        RegCloseKey(hkey);
    1.59 +        return WIN_StringToUTF8(name);  /* oh well. */
    1.60 +    }
    1.61 +
    1.62 +    rc = (RegQueryValueExW(hkey, L"Name", NULL, NULL, (LPBYTE) strw, &len) == ERROR_SUCCESS);
    1.63 +    RegCloseKey(hkey);
    1.64 +    if (!rc) {
    1.65 +        SDL_free(strw);
    1.66 +        return WIN_StringToUTF8(name);  /* oh well. */
    1.67 +    }
    1.68 +
    1.69 +    strw[len / 2] = 0;  /* make sure it's null-terminated. */
    1.70 +
    1.71 +    retval = WIN_StringToUTF8(strw);
    1.72 +    SDL_free(strw);
    1.73 +    return retval ? retval : WIN_StringToUTF8(name);
    1.74 +}
    1.75  
    1.76  #define DETECT_DEV_IMPL(iscap, typ, capstyp) \
    1.77  static void DetectWave##typ##Devs(void) { \
    1.78      const UINT iscapture = iscap ? 1 : 0; \
    1.79      const UINT devcount = wave##typ##GetNumDevs(); \
    1.80 -    capstyp caps; \
    1.81 +    capstyp##2W caps; \
    1.82      UINT i; \
    1.83      for (i = 0; i < devcount; i++) { \
    1.84 -        if (wave##typ##GetDevCaps(i,&caps,sizeof(caps))==MMSYSERR_NOERROR) { \
    1.85 -            char *name = WIN_StringToUTF8(caps.szPname); \
    1.86 +	if (wave##typ##GetDevCaps(i,(LP##capstyp##W)&caps,sizeof(caps))==MMSYSERR_NOERROR) { \
    1.87 +            char *name = LookupDeviceName(caps.szPname,&caps.NameGuid); \
    1.88              if (name != NULL) { \
    1.89                  SDL_AddAudioDevice((int) iscapture, name, (void *) ((size_t) i+1)); \
    1.90                  SDL_free(name); \