Fixed joystick name detection SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Thu, 12 Jul 2007 05:19:55 +0000
branchSDL-1.2
changeset 4051 209a3ef8a328
parent 4050 ef815c44c662
child 4052 b004c6c24a98
Fixed joystick name detection
src/joystick/win32/SDL_mmjoystick.c
--- a/src/joystick/win32/SDL_mmjoystick.c	Thu Jul 12 02:51:58 2007 +0000
+++ b/src/joystick/win32/SDL_mmjoystick.c	Thu Jul 12 05:19:55 2007 +0000
@@ -76,67 +76,58 @@
 		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;
+	}
+
+	/* 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);
 
-	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);
-	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,
-						REGSTR_VAL_JOYOEMNAME, 0, 0,
-						(LPBYTE) name,
-						(LPDWORD) &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) {
+		/* 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, &regsize);
 		}
 	}
+	RegCloseKey(hKey);
+
 	return(name);
 }