Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
authorSam Lantinga <slouken@libsdl.org>
Thu, 12 Jul 2007 05:31:08 +0000
changeset 2176cbe06fa842cd
parent 2175 2590d4e5fb89
child 2177 4f896c20caf6
Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
src/joystick/win32/SDL_mmjoystick.c
     1.1 --- a/src/joystick/win32/SDL_mmjoystick.c	Thu Jul 12 03:14:49 2007 +0000
     1.2 +++ b/src/joystick/win32/SDL_mmjoystick.c	Thu Jul 12 05:31:08 2007 +0000
     1.3 @@ -78,59 +78,62 @@
     1.4         index (1-16) listed in the registry
     1.5       */
     1.6      char *name = NULL;
     1.7 +    HKEY hTopKey;
     1.8      HKEY hKey;
     1.9      DWORD regsize;
    1.10      LONG regresult;
    1.11 -    unsigned char regkey[256];
    1.12 -    unsigned char regvalue[256];
    1.13 -    unsigned char regname[256];
    1.14 +    char regkey[256];
    1.15 +    char regvalue[256];
    1.16 +    char regname[256];
    1.17  
    1.18 -    SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
    1.19 +    SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
    1.20                   REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
    1.21 -    regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
    1.22 -                              (LPTSTR) & regkey, 0, KEY_READ, &hKey);
    1.23 +    hTopKey = HKEY_LOCAL_MACHINE;
    1.24 +    regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
    1.25 +    if (regresult != ERROR_SUCCESS) {
    1.26 +        hTopKey = HKEY_CURRENT_USER;
    1.27 +        regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
    1.28 +    }
    1.29 +    if (regresult != ERROR_SUCCESS) {
    1.30 +        return NULL;
    1.31 +    }
    1.32 +
    1.33 +    /* find the registry key name for the joystick's properties */
    1.34 +    regsize = sizeof(regname);
    1.35 +    SDL_snprintf(regvalue, SDL_arraysize(regvalue), "Joystick%d%s", index + 1,
    1.36 +                 REGSTR_VAL_JOYOEMNAME);
    1.37 +    regresult =
    1.38 +        RegQueryValueExA(hKey, regvalue, 0, 0, (LPBYTE) regname, &regsize);
    1.39 +    RegCloseKey(hKey);
    1.40 +
    1.41 +    if (regresult != ERROR_SUCCESS) {
    1.42 +        return NULL;
    1.43 +    }
    1.44 +
    1.45 +    /* open that registry key */
    1.46 +    SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s", REGSTR_PATH_JOYOEM,
    1.47 +                 regname);
    1.48 +    regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
    1.49 +    if (regresult != ERROR_SUCCESS) {
    1.50 +        return NULL;
    1.51 +    }
    1.52 +
    1.53 +    /* find the size for the OEM name text */
    1.54 +    regsize = sizeof(regvalue);
    1.55 +    regresult =
    1.56 +        RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0, NULL, &regsize);
    1.57      if (regresult == ERROR_SUCCESS) {
    1.58 -        /*
    1.59 -           find the registry key name for the
    1.60 -           joystick's properties
    1.61 -         */
    1.62 -        regsize = sizeof(regname);
    1.63 -        SDL_snprintf((char *) regvalue, SDL_arraysize(regvalue),
    1.64 -                     "Joystick%d%s", index + 1, REGSTR_VAL_JOYOEMNAME);
    1.65 -        regresult = RegQueryValueExA(hKey,
    1.66 -                                     (char *) regvalue, 0, 0,
    1.67 -                                     (LPBYTE) & regname, (LPDWORD) & regsize);
    1.68 -        RegCloseKey(hKey);
    1.69 -        if (regresult == ERROR_SUCCESS) {
    1.70 -            /* open that registry key */
    1.71 -            SDL_snprintf((char *) regkey, SDL_arraysize(regkey),
    1.72 -                         "%s\\%s", REGSTR_PATH_JOYOEM, regname);
    1.73 -            regresult =
    1.74 -                RegOpenKeyExA(HKEY_LOCAL_MACHINE, (char *) regkey, 0,
    1.75 -                              KEY_READ, &hKey);
    1.76 -            if (regresult == ERROR_SUCCESS) {
    1.77 -                /* find the size for the OEM name text */
    1.78 -                regsize = sizeof(regvalue);
    1.79 -                regresult =
    1.80 -                    RegQueryValueExA(hKey,
    1.81 -                                     REGSTR_VAL_JOYOEMNAME,
    1.82 -                                     0, 0, NULL, (LPDWORD) & regsize);
    1.83 -                if (regresult == ERROR_SUCCESS) {
    1.84 -                    /*
    1.85 -                       allocate enough memory
    1.86 -                       for the OEM name text ...
    1.87 -                     */
    1.88 -                    name = (char *) SDL_malloc(regsize);
    1.89 -                    /* ... and read it from the registry */
    1.90 -                    regresult =
    1.91 -                        RegQueryValueExA(hKey,
    1.92 +        /* allocate enough memory for the OEM name text ... */
    1.93 +        name = (char *) SDL_malloc(regsize);
    1.94 +        if (name) {
    1.95 +            /* ... and read it from the registry */
    1.96 +            regresult = RegQueryValueExA(hKey,
    1.97                                           REGSTR_VAL_JOYOEMNAME, 0, 0,
    1.98 -                                         (LPBYTE) name, (LPDWORD) & regsize);
    1.99 -                    RegCloseKey(hKey);
   1.100 -                }
   1.101 -            }
   1.102 +                                         (LPBYTE) name, &regsize);
   1.103          }
   1.104      }
   1.105 +    RegCloseKey(hKey);
   1.106 +
   1.107      return (name);
   1.108  }
   1.109