# HG changeset patch # User Sam Lantinga # Date 1184217595 0 # Node ID 209a3ef8a328abcaa463efa7a0f0e71d49ba0f34 # Parent ef815c44c662b9d454b71c9657e729afc80ae055 Fixed joystick name detection diff -r ef815c44c662 -r 209a3ef8a328 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, ®size); + 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) ®key, 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) ®name, - (LPDWORD) ®size); - 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) ®size); - 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) ®size); - 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, ®size); + 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, ®size); } } + RegCloseKey(hKey); + return(name); }