Used SDL's joystick name detection on windows.

This commit is contained in:
hiker 2014-10-25 10:18:27 +11:00
parent 1cd5ffd484
commit 759182bb68

View File

@ -359,6 +359,69 @@ void pollJoysticks()
#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
}
#include <stdio.h>
#include <mmsystem.h>
#include <regstr.h>
/** This function is ported from SDL and released under zlip:
* Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
*/
void setJoystickName(int index, const JOYCAPS &caps, SJoystickInfo *joystick)
{
// As a default use the name given in the joystick structure
// - though that is always the same name, independent of joystick :(
joystick->Name = caps.szPname;
core::stringc key = core::stringc(REGSTR_PATH_JOYCONFIG)+"\\"+caps.szRegKey
+ "\\"+REGSTR_KEY_JOYCURR;
HKEY hTopKey = HKEY_LOCAL_MACHINE;
HKEY hKey;
long regresult = RegOpenKeyExA(hTopKey, key.c_str(), 0, KEY_READ, &hKey);
if (regresult != ERROR_SUCCESS)
{
hTopKey = HKEY_CURRENT_USER;
regresult = RegOpenKeyExA(hTopKey, key.c_str(), 0, KEY_READ, &hKey);
}
if (regresult != ERROR_SUCCESS) return;
/* find the registry key name for the joystick's properties */
char regname[256];
DWORD regsize = sizeof(regname);
core::stringc regvalue = core::stringc("Joystick")+core::stringc(index+1)
+ REGSTR_VAL_JOYOEMNAME;
regresult = RegQueryValueExA(hKey, regvalue.c_str(), 0, 0,
(LPBYTE)regname, &regsize);
RegCloseKey(hKey);
if (regresult != ERROR_SUCCESS) return;
/* open that registry key */
core::stringc regkey = core::stringc(REGSTR_PATH_JOYOEM)+"\\"+regname;
regresult = RegOpenKeyExA(hTopKey, regkey.c_str(), 0, KEY_READ, &hKey);
if (regresult != ERROR_SUCCESS) return;
/* 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)
{
char *name;
/* allocate enough memory for the OEM name text ... */
name = new char[regsize];
if (name)
{
/* ... and read it from the registry */
regresult = RegQueryValueExA(hKey, REGSTR_VAL_JOYOEMNAME, 0, 0,
(LPBYTE)name, &regsize );
joystick->Name = name;
} // if name
} // if SUCCESS
RegCloseKey(hKey);
}
bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
{
#if defined _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
@ -407,11 +470,10 @@ bool activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
{
activeJoystick.Index = joystick;
ActiveJoysticks.push_back(activeJoystick);
returnInfo.Joystick = (u8)joystick;
returnInfo.Axes = activeJoystick.Caps.wNumAxes;
returnInfo.Buttons = activeJoystick.Caps.wNumButtons;
returnInfo.Name = activeJoystick.Caps.szPname;
setJoystickName(joystick, activeJoystick.Caps, &returnInfo);
returnInfo.PovHat = ((activeJoystick.Caps.wCaps & JOYCAPS_HASPOV) == JOYCAPS_HASPOV)
? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;