diff src/joystick/win32/SDL_mmjoystick.c @ 2176:cbe06fa842cd

Fixed joystick name detection - merged from revision 3226 on SDL 1.2 branch
author Sam Lantinga <slouken@libsdl.org>
date Thu, 12 Jul 2007 05:31:08 +0000
parents 6051761b5934
children 99210400e8b9
line wrap: on
line diff
--- a/src/joystick/win32/SDL_mmjoystick.c	Thu Jul 12 03:14:49 2007 +0000
+++ b/src/joystick/win32/SDL_mmjoystick.c	Thu Jul 12 05:31:08 2007 +0000
@@ -78,59 +78,62 @@
        index (1-16) listed in the registry
      */
     char *name = NULL;
+    HKEY hTopKey;
     HKEY hKey;
     DWORD regsize;
     LONG regresult;
-    unsigned char regkey[256];
-    unsigned char regvalue[256];
-    unsigned char regname[256];
+    char regkey[256];
+    char regvalue[256];
+    char regname[256];
+
+    SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
+                 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
+    hTopKey = HKEY_LOCAL_MACHINE;
+    regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
+    if (regresult != ERROR_SUCCESS) {
+        hTopKey = HKEY_CURRENT_USER;
+        regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
+    }
+    if (regresult != ERROR_SUCCESS) {
+        return NULL;
+    }
 
-    SDL_snprintf((char *) regkey, SDL_arraysize(regkey), "%s\\%s\\%s",
-                 REGSTR_PATH_JOYCONFIG, szRegKey, REGSTR_KEY_JOYCURR);
-    regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
-                              (LPTSTR) & regkey, 0, KEY_READ, &hKey);
+    /* find the registry key name for the joystick's properties */
+    regsize = sizeof(regname);
+    SDL_snprintf(regvalue, SDL_arraysize(regvalue), "Joystick%d%s", index + 1,
+                 REGSTR_VAL_JOYOEMNAME);
+    regresult =
+        RegQueryValueExA(hKey, regvalue, 0, 0, (LPBYTE) regname, &regsize);
+    RegCloseKey(hKey);
+
+    if (regresult != ERROR_SUCCESS) {
+        return NULL;
+    }
+
+    /* open that registry key */
+    SDL_snprintf(regkey, SDL_arraysize(regkey), "%s\\%s", REGSTR_PATH_JOYOEM,
+                 regname);
+    regresult = RegOpenKeyExA(hTopKey, regkey, 0, KEY_READ, &hKey);
+    if (regresult != ERROR_SUCCESS) {
+        return NULL;
+    }
+
+    /* find the size for the OEM name text */
+    regsize = sizeof(regvalue);
+    regresult =
+        RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0, NULL, &regsize);
     if (regresult == ERROR_SUCCESS) {
-        /*
-           find the registry key name for the
-           joystick's properties
-         */
-        regsize = sizeof(regname);
-        SDL_snprintf((char *) regvalue, SDL_arraysize(regvalue),
-                     "Joystick%d%s", index + 1, REGSTR_VAL_JOYOEMNAME);
-        regresult = RegQueryValueExA(hKey,
-                                     (char *) regvalue, 0, 0,
-                                     (LPBYTE) & regname, (LPDWORD) & regsize);
-        RegCloseKey(hKey);
-        if (regresult == ERROR_SUCCESS) {
-            /* open that registry key */
-            SDL_snprintf((char *) regkey, SDL_arraysize(regkey),
-                         "%s\\%s", REGSTR_PATH_JOYOEM, regname);
-            regresult =
-                RegOpenKeyExA(HKEY_LOCAL_MACHINE, (char *) regkey, 0,
-                              KEY_READ, &hKey);
-            if (regresult == ERROR_SUCCESS) {
-                /* find the size for the OEM name text */
-                regsize = sizeof(regvalue);
-                regresult =
-                    RegQueryValueExA(hKey,
-                                     REGSTR_VAL_JOYOEMNAME,
-                                     0, 0, NULL, (LPDWORD) & regsize);
-                if (regresult == ERROR_SUCCESS) {
-                    /*
-                       allocate enough memory
-                       for the OEM name text ...
-                     */
-                    name = (char *) SDL_malloc(regsize);
-                    /* ... and read it from the registry */
-                    regresult =
-                        RegQueryValueExA(hKey,
+        /* allocate enough memory for the OEM name text ... */
+        name = (char *) SDL_malloc(regsize);
+        if (name) {
+            /* ... and read it from the registry */
+            regresult = RegQueryValueExA(hKey,
                                          REGSTR_VAL_JOYOEMNAME, 0, 0,
-                                         (LPBYTE) name, (LPDWORD) & regsize);
-                    RegCloseKey(hKey);
-                }
-            }
+                                         (LPBYTE) name, &regsize);
         }
     }
+    RegCloseKey(hKey);
+
     return (name);
 }