[WinMM] Fix for when joysticks not detected: (PR #1162)

. first & second devices are not always at index 0 & 1
This commit is contained in:
tomcw 2023-05-05 21:38:33 +01:00
commit a53fbf212e
4 changed files with 156 additions and 108 deletions

View File

@ -49,6 +49,7 @@
#include "StdAfx.h" #include "StdAfx.h"
#include "FourPlay.h" #include "FourPlay.h"
#include "Joystick.h"
#include "Memory.h" #include "Memory.h"
#include "YamlHelper.h" #include "YamlHelper.h"
@ -67,15 +68,13 @@ BYTE __stdcall FourPlayCard::IORead(WORD pc, WORD addr, BYTE bWrite, BYTE value,
UINT yAxis = 0; UINT yAxis = 0;
JOYINFOEX infoEx; JOYINFOEX infoEx;
MMRESULT result = 0;
infoEx.dwSize = sizeof(infoEx); infoEx.dwSize = sizeof(infoEx);
infoEx.dwFlags = JOY_RETURNPOV | JOY_RETURNBUTTONS; infoEx.dwFlags = JOY_RETURNPOV | JOY_RETURNBUTTONS;
switch (addr & 0xF) switch (addr & 0xF)
{ {
case 0: // Joystick 1 case 0: // Joystick 1
result = joyGetPosEx(JOYSTICKID1, &infoEx); if (GetJoystick1() >= 0 && joyGetPosEx(GetJoystick1(), &infoEx) == JOYERR_NOERROR)
if (result == JOYERR_NOERROR)
{ {
xAxis = (infoEx.dwXpos >> 8) & 0xFF; xAxis = (infoEx.dwXpos >> 8) & 0xFF;
yAxis = (infoEx.dwYpos >> 8) & 0xFF; yAxis = (infoEx.dwYpos >> 8) & 0xFF;
@ -89,8 +88,7 @@ BYTE __stdcall FourPlayCard::IORead(WORD pc, WORD addr, BYTE bWrite, BYTE value,
nOutput = up | (down << 1) | (left << 2) | (right << 3) | (alwaysHigh << 5) | (trigger2 << 6) | (trigger1 << 7); nOutput = up | (down << 1) | (left << 2) | (right << 3) | (alwaysHigh << 5) | (trigger2 << 6) | (trigger1 << 7);
break; break;
case 1: // Joystick 2 case 1: // Joystick 2
result = joyGetPosEx(JOYSTICKID2, &infoEx); if (GetJoystick2() >= 0 && joyGetPosEx(GetJoystick2(), &infoEx) == JOYERR_NOERROR)
if (result == JOYERR_NOERROR)
{ {
xAxis = (infoEx.dwXpos >> 8) & 0xFF; xAxis = (infoEx.dwXpos >> 8) & 0xFF;
yAxis = (infoEx.dwYpos >> 8) & 0xFF; yAxis = (infoEx.dwYpos >> 8) & 0xFF;

View File

@ -110,6 +110,9 @@ static UINT g_uJoyportReadMode = JOYPORT_LEFTRIGHT;
static bool g_bHookAltKeys = true; static bool g_bHookAltKeys = true;
static int JOYSTICK_1 = -1;
static int JOYSTICK_2 = -1;
//=========================================================================== //===========================================================================
void JoySetHookAltKeys(bool hook) void JoySetHookAltKeys(bool hook)
@ -117,16 +120,29 @@ void JoySetHookAltKeys(bool hook)
g_bHookAltKeys = hook; g_bHookAltKeys = hook;
} }
//=========================================================================== int GetJoystick1(void)
void CheckJoystick0()
{ {
return JOYSTICK_1;
}
int GetJoystick2(void)
{
return JOYSTICK_2;
}
//===========================================================================
static void CheckJoystick0()
{
if (JOYSTICK_1 < 0)
return;
static DWORD lastcheck = 0; static DWORD lastcheck = 0;
DWORD currtime = GetTickCount(); DWORD currtime = GetTickCount();
if ((currtime-lastcheck >= 10) || joybutton[0] || joybutton[1]) if ((currtime-lastcheck >= 10) || joybutton[0] || joybutton[1])
{ {
lastcheck = currtime; lastcheck = currtime;
JOYINFO info; JOYINFO info;
if (joyGetPos(JOYSTICKID1,&info) == JOYERR_NOERROR) if (joyGetPos(JOYSTICK_1,&info) == JOYERR_NOERROR)
{ {
joybutton[0] = ((info.wButtons & JOY_BUTTON1) != 0); joybutton[0] = ((info.wButtons & JOY_BUTTON1) != 0);
if (joyinfo[joytype[1]] == DEVICE_NONE) // Only consider 2nd button if NOT emulating a 2nd Apple joystick if (joyinfo[joytype[1]] == DEVICE_NONE) // Only consider 2nd button if NOT emulating a 2nd Apple joystick
@ -142,7 +158,7 @@ void CheckJoystick0()
} }
} }
void CheckJoystick1() static void CheckJoystick1()
{ {
static DWORD lastcheck = 0; static DWORD lastcheck = 0;
DWORD currtime = GetTickCount(); DWORD currtime = GetTickCount();
@ -150,14 +166,14 @@ void CheckJoystick1()
{ {
lastcheck = currtime; lastcheck = currtime;
JOYINFO info; JOYINFO info;
MMRESULT result = 0; MMRESULT result = JOYERR_NOERROR;
if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2) if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2)
{ {
// Use results of joystick 1 thumbstick 2 and button 2 for joystick 1 and button 1 // Use results of joystick 1 thumbstick 2 and button 2 for joystick 1 and button 1
JOYINFOEX infoEx; JOYINFOEX infoEx;
infoEx.dwSize = sizeof(infoEx); infoEx.dwSize = sizeof(infoEx);
infoEx.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNZ | JOY_RETURNR; infoEx.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNZ | JOY_RETURNR;
result = joyGetPosEx(JOYSTICKID1, &infoEx); result = joyGetPosEx(JOYSTICK_1, &infoEx);
if (result == JOYERR_NOERROR) if (result == JOYERR_NOERROR)
{ {
info.wButtons = (infoEx.dwButtons & JOY_BUTTON2) ? JOY_BUTTON1 : 0; info.wButtons = (infoEx.dwButtons & JOY_BUTTON2) ? JOY_BUTTON1 : 0;
@ -166,7 +182,10 @@ void CheckJoystick1()
} }
} }
else else
result = joyGetPos(JOYSTICKID2, &info); {
result = joyGetPos(JOYSTICK_2, &info); // NB. joyGetPos(-1, &info) returns JOYERR_PARMS (bad parameters)
}
if (result == JOYERR_NOERROR) if (result == JOYERR_NOERROR)
{ {
joybutton[2] = ((info.wButtons & JOY_BUTTON1) != 0); joybutton[2] = ((info.wButtons & JOY_BUTTON1) != 0);
@ -190,8 +209,37 @@ void CheckJoystick1()
//=========================================================================== //===========================================================================
void JoyInitialize() void JoyInitialize()
{ {
// Emulated joystick #0 can only use JOYSTICKID1 (if no joystick, then use keyboard) //
// Emulated joystick #1 can only use JOYSTICKID2 (if no joystick, then disable) // Detect First and Second connected JOYSTICK in WinMM API. JOYSTICKID1 == 0 but is not always the first connected joystick.
//
JOYSTICK_1 = -1;
JOYSTICK_2 = -1;
bool firstFound = false;
const UINT numDevs = joyGetNumDevs();
for (UINT i = 0; i < numDevs; i++)
{
JOYCAPS caps;
int ret = joyGetDevCaps(i, &caps, sizeof(JOYCAPS));
if (ret != JOYERR_NOERROR)
continue;
JOYINFO info;
ret = joyGetPos(i, &info);
if (ret != JOYERR_NOERROR)
continue;
if (firstFound)
{
JOYSTICK_2 = i;
break;
}
JOYSTICK_1 = i;
firstFound = true;
}
// //
// Init for emulated joystick #0: // Init for emulated joystick #0:
@ -200,14 +248,14 @@ void JoyInitialize()
if (joyinfo[joytype[0]] == DEVICE_JOYSTICK) if (joyinfo[joytype[0]] == DEVICE_JOYSTICK)
{ {
JOYCAPS caps; JOYCAPS caps;
if (joyGetDevCaps(JOYSTICKID1,&caps,sizeof(JOYCAPS)) == JOYERR_NOERROR) if (JOYSTICK_1 >= 0 && joyGetDevCaps(JOYSTICK_1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR)
{ {
joyshrx[0] = 0; joyshrx[0] = 0;
joyshry[0] = 0; joyshry[0] = 0;
joysubx[0] = (int)caps.wXmin; joysubx[0] = (int)caps.wXmin;
joysuby[0] = (int)caps.wYmin; joysuby[0] = (int)caps.wYmin;
UINT xrange = caps.wXmax-caps.wXmin; UINT xrange = caps.wXmax - caps.wXmin;
UINT yrange = caps.wYmax-caps.wYmin; UINT yrange = caps.wYmax - caps.wYmin;
while (xrange > 256) while (xrange > 256)
{ {
xrange >>= 1; xrange >>= 1;
@ -232,14 +280,14 @@ void JoyInitialize()
if (joyinfo[joytype[1]] == DEVICE_JOYSTICK) if (joyinfo[joytype[1]] == DEVICE_JOYSTICK)
{ {
JOYCAPS caps; JOYCAPS caps;
if (joyGetDevCaps(JOYSTICKID2,&caps,sizeof(JOYCAPS)) == JOYERR_NOERROR) if (JOYSTICK_2 >= 0 && joyGetDevCaps(JOYSTICK_2, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR)
{ {
joyshrx[1] = 0; joyshrx[1] = 0;
joyshry[1] = 0; joyshry[1] = 0;
joysubx[1] = (int)caps.wXmin; joysubx[1] = (int)caps.wXmin;
joysuby[1] = (int)caps.wYmin; joysuby[1] = (int)caps.wYmin;
UINT xrange = caps.wXmax-caps.wXmin; UINT xrange = caps.wXmax - caps.wXmin;
UINT yrange = caps.wYmax-caps.wYmin; UINT yrange = caps.wYmax - caps.wYmin;
while (xrange > 256) while (xrange > 256)
{ {
xrange >>= 1; xrange >>= 1;
@ -259,7 +307,7 @@ void JoyInitialize()
else if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2) else if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2)
{ {
JOYCAPS caps; JOYCAPS caps;
if (joyGetDevCaps(JOYSTICKID1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) if (JOYSTICK_1 >= 0 && joyGetDevCaps(JOYSTICK_1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR)
{ {
joyshrx[1] = 0; joyshrx[1] = 0;
joyshry[1] = 0; joyshry[1] = 0;
@ -707,9 +755,9 @@ BOOL JoySetEmulationType(HWND window, DWORD newtype, int nJoystickNumber, const
if (joyinfo[newtype] == DEVICE_JOYSTICK || joyinfo[newtype] == DEVICE_JOYSTICK_THUMBSTICK2) if (joyinfo[newtype] == DEVICE_JOYSTICK || joyinfo[newtype] == DEVICE_JOYSTICK_THUMBSTICK2)
{ {
JOYCAPS caps; JOYCAPS caps;
unsigned int nJoy2ID = joyinfo[newtype] == DEVICE_JOYSTICK_THUMBSTICK2 ? JOYSTICKID1 : JOYSTICKID2; int nJoy2ID = joyinfo[newtype] == DEVICE_JOYSTICK_THUMBSTICK2 ? JOYSTICK_1 : JOYSTICK_2;
unsigned int nJoyID = nJoystickNumber == JN_JOYSTICK0 ? JOYSTICKID1 : nJoy2ID; int nJoyID = nJoystickNumber == JN_JOYSTICK0 ? JOYSTICK_1 : nJoy2ID;
if (joyGetDevCaps(nJoyID, &caps, sizeof(JOYCAPS)) != JOYERR_NOERROR) if (nJoyID < 0 || joyGetDevCaps(nJoyID, &caps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
{ {
MessageBox(window, MessageBox(window,
TEXT("The emulator is unable to read your PC joystick. ") TEXT("The emulator is unable to read your PC joystick. ")

View File

@ -26,6 +26,8 @@ short JoyGetTrim(bool bAxisX);
void JoyportControl(const UINT uControl); void JoyportControl(const UINT uControl);
void JoySetHookAltKeys(bool hook); void JoySetHookAltKeys(bool hook);
void JoySetButtonVirtualKey(UINT button, UINT virtKey); void JoySetButtonVirtualKey(UINT button, UINT virtKey);
int GetJoystick1(void);
int GetJoystick2(void);
void JoySaveSnapshot(class YamlSaveHelper& yamlSaveHelper); void JoySaveSnapshot(class YamlSaveHelper& yamlSaveHelper);
void JoyLoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT version); void JoyLoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT version);

View File

@ -53,6 +53,7 @@
*/ */
#include "StdAfx.h" #include "StdAfx.h"
#include "Joystick.h"
#include "SNESMAX.h" #include "SNESMAX.h"
#include "Memory.h" #include "Memory.h"
#include "YamlHelper.h" #include "YamlHelper.h"
@ -103,7 +104,6 @@ BYTE __stdcall SNESMAXCard::IOWrite(WORD pc, WORD addr, BYTE bWrite, BYTE value,
UINT yAxis = 0; UINT yAxis = 0;
JOYINFOEX infoEx; JOYINFOEX infoEx;
MMRESULT result = 0;
infoEx.dwSize = sizeof(infoEx); infoEx.dwSize = sizeof(infoEx);
infoEx.dwFlags = JOY_RETURNPOV | JOY_RETURNBUTTONS; infoEx.dwFlags = JOY_RETURNPOV | JOY_RETURNBUTTONS;
@ -117,14 +117,14 @@ BYTE __stdcall SNESMAXCard::IOWrite(WORD pc, WORD addr, BYTE bWrite, BYTE value,
controller1Buttons = 0; controller1Buttons = 0;
controller2Buttons = 0; controller2Buttons = 0;
result = joyGetPosEx(JOYSTICKID1, &infoEx); if (GetJoystick1() >= 0 && joyGetPosEx(GetJoystick1(), &infoEx) == JOYERR_NOERROR)
if (result == JOYERR_NOERROR)
controller1Buttons = pCard->GetControllerButtons(JOYSTICKID1, infoEx, pCard->m_altControllerType[0]); controller1Buttons = pCard->GetControllerButtons(JOYSTICKID1, infoEx, pCard->m_altControllerType[0]);
controller1Buttons = ~controller1Buttons; controller1Buttons = ~controller1Buttons;
result = joyGetPosEx(JOYSTICKID2, &infoEx); if (GetJoystick2() >= 0 && joyGetPosEx(GetJoystick2(), &infoEx) == JOYERR_NOERROR)
if (result == JOYERR_NOERROR)
controller2Buttons = pCard->GetControllerButtons(JOYSTICKID2, infoEx, pCard->m_altControllerType[1]); controller2Buttons = pCard->GetControllerButtons(JOYSTICKID2, infoEx, pCard->m_altControllerType[1]);
controller2Buttons = ~controller2Buttons; controller2Buttons = ~controller2Buttons;
break; break;