Date: Tue, 27 Jul 2004 17:14:00 +0200
authorSam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 03:45:58 +0000
changeset 9371e6366bde299
parent 936 84f930aebaeb
child 938 fa2ce068b0b6
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
     1.1 --- a/src/joystick/win32/SDL_mmjoystick.c	Sat Aug 21 03:21:44 2004 +0000
     1.2 +++ b/src/joystick/win32/SDL_mmjoystick.c	Sat Aug 21 03:45:58 2004 +0000
     1.3 @@ -37,6 +37,7 @@
     1.4  
     1.5  #include <windows.h>
     1.6  #include <mmsystem.h>
     1.7 +#include <regstr.h>
     1.8  
     1.9  #define MAX_JOYSTICKS	16
    1.10  #define MAX_AXES	6	/* each joystick can have up to 6 axes */
    1.11 @@ -51,6 +52,7 @@
    1.12  /* array to hold joystick ID values */
    1.13  static UINT	SYS_JoystickID[MAX_JOYSTICKS];
    1.14  static JOYCAPS	SYS_Joystick[MAX_JOYSTICKS];
    1.15 +static char	*SYS_JoystickNames[MAX_JOYSTICKS];
    1.16  
    1.17  /* The private structure used to keep track of a joystick */
    1.18  struct joystick_hwdata
    1.19 @@ -70,6 +72,78 @@
    1.20  static void SetMMerror(char *function, int code);
    1.21  
    1.22  
    1.23 +static char *GetJoystickName(const char *szRegKey)
    1.24 +{
    1.25 +	/* added 7/24/2004 by Eckhard Stolberg */
    1.26 +	/*
    1.27 +		see if there is a joystick for the current
    1.28 +		index (1-16) listed in the registry
    1.29 +	*/
    1.30 +	char *name = NULL;
    1.31 +	HKEY hKey;
    1.32 +	DWORD regsize;
    1.33 +	LONG regresult;
    1.34 +	unsigned char regkey[256];
    1.35 +	unsigned char regvalue[256];
    1.36 +	unsigned char regname[256];
    1.37 +
    1.38 +	sprintf(regkey, "%s\\%s\\%s",
    1.39 +		REGSTR_PATH_JOYCONFIG,
    1.40 +		szRegKey,
    1.41 +		REGSTR_KEY_JOYCURR);
    1.42 +	regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
    1.43 +		(LPTSTR) &regkey, 0, KEY_READ, &hKey);
    1.44 +	if (regresult == ERROR_SUCCESS)
    1.45 +	{
    1.46 +		/*
    1.47 +			find the registry key name for the
    1.48 +			joystick's properties
    1.49 +		*/
    1.50 +		regsize = sizeof(regname);
    1.51 +		sprintf(regvalue,
    1.52 +			"Joystick%d%s", i+1,
    1.53 +			REGSTR_VAL_JOYOEMNAME);
    1.54 +		regresult = RegQueryValueExA(hKey,
    1.55 +			regvalue, 0, 0, (LPBYTE) &regname,
    1.56 +			(LPDWORD) &regsize);
    1.57 +		RegCloseKey(hKey);
    1.58 +		if (regresult == ERROR_SUCCESS)
    1.59 +		{
    1.60 +			/* open that registry key */
    1.61 +			sprintf(regkey, "%s\\%s",
    1.62 +				REGSTR_PATH_JOYOEM, regname);
    1.63 +			regresult = RegOpenKeyExA(HKEY_LOCAL_MACHINE,
    1.64 +				regkey, 0, KEY_READ, &hKey);
    1.65 +			if (regresult == ERROR_SUCCESS)
    1.66 +			{
    1.67 +				/* find the size for the OEM name text */
    1.68 +				regsize = sizeof(regvalue);
    1.69 +				regresult =
    1.70 +					RegQueryValueExA(hKey,
    1.71 +					REGSTR_VAL_JOYOEMNAME,
    1.72 +					0, 0, NULL,
    1.73 +					(LPDWORD) &regsize);
    1.74 +				if (regresult == ERROR_SUCCESS)
    1.75 +				{
    1.76 +					/*
    1.77 +						allocate enough memory
    1.78 +						for the OEM name text ...
    1.79 +					*/
    1.80 +					name = (char *) malloc(regsize);
    1.81 +					/* ... and read it from the registry */
    1.82 +					regresult =
    1.83 +						RegQueryValueExA(hKey,
    1.84 +						REGSTR_VAL_JOYOEMNAME, 0, 0,
    1.85 +						(LPBYTE) name,
    1.86 +						(LPDWORD) &regsize);
    1.87 +					RegCloseKey(hKey);
    1.88 +				}
    1.89 +			}
    1.90 +		}
    1.91 +	}
    1.92 +	return(name);
    1.93 +}
    1.94 +
    1.95  /* Function to scan the system for joysticks.
    1.96   * This function should set SDL_numjoysticks to the number of available
    1.97   * joysticks.  Joystick 0 should be the system default joystick.
    1.98 @@ -94,6 +168,7 @@
    1.99  
   1.100  	for ( i = 0; i < MAX_JOYSTICKS; i++ ) {
   1.101  		SYS_JoystickID[i] = JOYSTICKID1 + i;
   1.102 +		SYS_JoystickNames[i] = NULL;
   1.103  	}
   1.104  
   1.105  
   1.106 @@ -110,6 +185,7 @@
   1.107  			if ( result == JOYERR_NOERROR ) {
   1.108  				SYS_JoystickID[numdevs] = SYS_JoystickID[i];
   1.109  				SYS_Joystick[numdevs] = joycaps;
   1.110 +				SYS_JoystickName[numdevs] = GetJoystickName(joycaps.szRegKey);
   1.111  				numdevs++;
   1.112  			}
   1.113  		}
   1.114 @@ -120,8 +196,11 @@
   1.115  /* Function to get the device-dependent name of a joystick */
   1.116  const char *SDL_SYS_JoystickName(int index)
   1.117  {
   1.118 -	/***-> test for invalid index ? */
   1.119 -	return(SYS_Joystick[index].szPname);
   1.120 +	if ( SYS_JoystickNames[index] != NULL ) {
   1.121 +		return(SYS_JoystickNames[index]);
   1.122 +	} else {
   1.123 +		return(SYS_Joystick[index].szPname);
   1.124 +	}
   1.125  }
   1.126  
   1.127  /* Function to open a joystick for use.
   1.128 @@ -292,7 +371,12 @@
   1.129  /* Function to perform any system-specific joystick related cleanup */
   1.130  void SDL_SYS_JoystickQuit(void)
   1.131  {
   1.132 -	return;
   1.133 +	int i;
   1.134 +	for (i = 0; i < MAX_JOYSTICKS; i++) {
   1.135 +		if ( SYS_JoystickNames[i] != NULL ) {
   1.136 +			free(SYS_JoystickNames[i]);
   1.137 +		}
   1.138 +	}
   1.139  }
   1.140  
   1.141