From 011f16365e9920abd88baedb83ea60a2928b366b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 21 Aug 2004 03:45:58 +0000 Subject: [PATCH] Date: Tue, 27 Jul 2004 17:14:00 +0200 From: "Eckhard Stolberg" Subject: Controller names in SDL for Windows I'm working on an Atari 2600 emulator for different systems that uses the SDL. Some time ago someone created an adaptor that lets you use your old Atari controllers with your computer through the USB port. Some of the Atari controllers require special handling by the emulator, so it would be nice, if it would be possible to detect if any of the controllers connected to the computer is this adaptor. SDL would allow that with the SDL_JoystickName function, but unfortunately it doesn't work properly on Windows. On Linux and MacOSX this function returns the name of the controller, but on Windows you'll only get the name of the joystick driver. Most joysticks nowadays use the generic Microsoft driver, so they all return the same name. In an old MSDN article (http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarinput/html/msdn_extdirect.asp) Microsoft describes how to read out the OEM controller names from the registry. I have implemented this for the SDL controller handler on Windows, and now reading the joystick name works properly there too. --- src/joystick/win32/SDL_mmjoystick.c | 90 ++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/src/joystick/win32/SDL_mmjoystick.c b/src/joystick/win32/SDL_mmjoystick.c index 6a06a79b4..10dce6853 100644 --- a/src/joystick/win32/SDL_mmjoystick.c +++ b/src/joystick/win32/SDL_mmjoystick.c @@ -37,6 +37,7 @@ static char rcsid = #include #include +#include #define MAX_JOYSTICKS 16 #define MAX_AXES 6 /* each joystick can have up to 6 axes */ @@ -51,6 +52,7 @@ static char rcsid = /* array to hold joystick ID values */ static UINT SYS_JoystickID[MAX_JOYSTICKS]; static JOYCAPS SYS_Joystick[MAX_JOYSTICKS]; +static char *SYS_JoystickNames[MAX_JOYSTICKS]; /* The private structure used to keep track of a joystick */ struct joystick_hwdata @@ -70,6 +72,78 @@ struct joystick_hwdata static void SetMMerror(char *function, int code); +static char *GetJoystickName(const char *szRegKey) +{ + /* added 7/24/2004 by Eckhard Stolberg */ + /* + see if there is a joystick for the current + index (1-16) listed in the registry + */ + char *name = NULL; + HKEY hKey; + DWORD regsize; + LONG regresult; + unsigned char regkey[256]; + unsigned char regvalue[256]; + unsigned char regname[256]; + + sprintf(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); + sprintf(regvalue, + "Joystick%d%s", i+1, + REGSTR_VAL_JOYOEMNAME); + regresult = RegQueryValueExA(hKey, + regvalue, 0, 0, (LPBYTE) ®name, + (LPDWORD) ®size); + RegCloseKey(hKey); + if (regresult == ERROR_SUCCESS) + { + /* open that registry key */ + sprintf(regkey, "%s\\%s", + REGSTR_PATH_JOYOEM, regname); + regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE, + 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 *) malloc(regsize); + /* ... and read it from the registry */ + regresult = + RegQueryValueExA(hKey, + REGSTR_VAL_JOYOEMNAME, 0, 0, + (LPBYTE) name, + (LPDWORD) ®size); + RegCloseKey(hKey); + } + } + } + } + return(name); +} + /* Function to scan the system for joysticks. * This function should set SDL_numjoysticks to the number of available * joysticks. Joystick 0 should be the system default joystick. @@ -94,6 +168,7 @@ int SDL_SYS_JoystickInit(void) for ( i = 0; i < MAX_JOYSTICKS; i++ ) { SYS_JoystickID[i] = JOYSTICKID1 + i; + SYS_JoystickNames[i] = NULL; } @@ -110,6 +185,7 @@ int SDL_SYS_JoystickInit(void) if ( result == JOYERR_NOERROR ) { SYS_JoystickID[numdevs] = SYS_JoystickID[i]; SYS_Joystick[numdevs] = joycaps; + SYS_JoystickName[numdevs] = GetJoystickName(joycaps.szRegKey); numdevs++; } } @@ -120,8 +196,11 @@ int SDL_SYS_JoystickInit(void) /* Function to get the device-dependent name of a joystick */ const char *SDL_SYS_JoystickName(int index) { - /***-> test for invalid index ? */ - return(SYS_Joystick[index].szPname); + if ( SYS_JoystickNames[index] != NULL ) { + return(SYS_JoystickNames[index]); + } else { + return(SYS_Joystick[index].szPname); + } } /* Function to open a joystick for use. @@ -292,7 +371,12 @@ void SDL_SYS_JoystickClose(SDL_Joystick *joystick) /* Function to perform any system-specific joystick related cleanup */ void SDL_SYS_JoystickQuit(void) { - return; + int i; + for (i = 0; i < MAX_JOYSTICKS; i++) { + if ( SYS_JoystickNames[i] != NULL ) { + free(SYS_JoystickNames[i]); + } + } }