[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 "FourPlay.h"
#include "Joystick.h"
#include "Memory.h"
#include "YamlHelper.h"
@ -67,15 +68,13 @@ BYTE __stdcall FourPlayCard::IORead(WORD pc, WORD addr, BYTE bWrite, BYTE value,
UINT yAxis = 0;
JOYINFOEX infoEx;
MMRESULT result = 0;
infoEx.dwSize = sizeof(infoEx);
infoEx.dwFlags = JOY_RETURNPOV | JOY_RETURNBUTTONS;
switch (addr & 0xF)
{
case 0: // Joystick 1
result = joyGetPosEx(JOYSTICKID1, &infoEx);
if (result == JOYERR_NOERROR)
if (GetJoystick1() >= 0 && joyGetPosEx(GetJoystick1(), &infoEx) == JOYERR_NOERROR)
{
xAxis = (infoEx.dwXpos >> 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);
break;
case 1: // Joystick 2
result = joyGetPosEx(JOYSTICKID2, &infoEx);
if (result == JOYERR_NOERROR)
if (GetJoystick2() >= 0 && joyGetPosEx(GetJoystick2(), &infoEx) == JOYERR_NOERROR)
{
xAxis = (infoEx.dwXpos >> 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 int JOYSTICK_1 = -1;
static int JOYSTICK_2 = -1;
//===========================================================================
void JoySetHookAltKeys(bool hook)
@ -117,16 +120,29 @@ void JoySetHookAltKeys(bool hook)
g_bHookAltKeys = hook;
}
//===========================================================================
void CheckJoystick0()
int GetJoystick1(void)
{
return JOYSTICK_1;
}
int GetJoystick2(void)
{
return JOYSTICK_2;
}
//===========================================================================
static void CheckJoystick0()
{
if (JOYSTICK_1 < 0)
return;
static DWORD lastcheck = 0;
DWORD currtime = GetTickCount();
if ((currtime-lastcheck >= 10) || joybutton[0] || joybutton[1])
{
lastcheck = currtime;
JOYINFO info;
if (joyGetPos(JOYSTICKID1,&info) == JOYERR_NOERROR)
if (joyGetPos(JOYSTICK_1,&info) == JOYERR_NOERROR)
{
joybutton[0] = ((info.wButtons & JOY_BUTTON1) != 0);
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;
DWORD currtime = GetTickCount();
@ -150,14 +166,14 @@ void CheckJoystick1()
{
lastcheck = currtime;
JOYINFO info;
MMRESULT result = 0;
MMRESULT result = JOYERR_NOERROR;
if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2)
{
// Use results of joystick 1 thumbstick 2 and button 2 for joystick 1 and button 1
JOYINFOEX infoEx;
infoEx.dwSize = sizeof(infoEx);
infoEx.dwFlags = JOY_RETURNBUTTONS | JOY_RETURNZ | JOY_RETURNR;
result = joyGetPosEx(JOYSTICKID1, &infoEx);
result = joyGetPosEx(JOYSTICK_1, &infoEx);
if (result == JOYERR_NOERROR)
{
info.wButtons = (infoEx.dwButtons & JOY_BUTTON2) ? JOY_BUTTON1 : 0;
@ -165,9 +181,12 @@ void CheckJoystick1()
info.wYpos = infoEx.dwRpos;
}
}
else
result = joyGetPos(JOYSTICKID2, &info);
if (result == JOYERR_NOERROR)
else
{
result = joyGetPos(JOYSTICK_2, &info); // NB. joyGetPos(-1, &info) returns JOYERR_PARMS (bad parameters)
}
if (result == JOYERR_NOERROR)
{
joybutton[2] = ((info.wButtons & JOY_BUTTON1) != 0);
if(joyinfo[joytype[1]] != DEVICE_NONE)
@ -190,99 +209,128 @@ void CheckJoystick1()
//===========================================================================
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.
//
//
// Init for emulated joystick #0:
//
JOYSTICK_1 = -1;
JOYSTICK_2 = -1;
if (joyinfo[joytype[0]] == DEVICE_JOYSTICK)
{
JOYCAPS caps;
if (joyGetDevCaps(JOYSTICKID1,&caps,sizeof(JOYCAPS)) == JOYERR_NOERROR)
bool firstFound = false;
const UINT numDevs = joyGetNumDevs();
for (UINT i = 0; i < numDevs; i++)
{
joyshrx[0] = 0;
joyshry[0] = 0;
joysubx[0] = (int)caps.wXmin;
joysuby[0] = (int)caps.wYmin;
UINT xrange = caps.wXmax-caps.wXmin;
UINT yrange = caps.wYmax-caps.wYmin;
while (xrange > 256)
{
xrange >>= 1;
++joyshrx[0];
}
while (yrange > 256)
{
yrange >>= 1;
++joyshry[0];
}
}
else
{
joytype[0] = J0C_KEYBD_NUMPAD;
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 #1:
//
//
// Init for emulated joystick #0:
//
if (joyinfo[joytype[1]] == DEVICE_JOYSTICK)
{
JOYCAPS caps;
if (joyGetDevCaps(JOYSTICKID2,&caps,sizeof(JOYCAPS)) == JOYERR_NOERROR)
if (joyinfo[joytype[0]] == DEVICE_JOYSTICK)
{
joyshrx[1] = 0;
joyshry[1] = 0;
joysubx[1] = (int)caps.wXmin;
joysuby[1] = (int)caps.wYmin;
UINT xrange = caps.wXmax-caps.wXmin;
UINT yrange = caps.wYmax-caps.wYmin;
while (xrange > 256)
{
xrange >>= 1;
++joyshrx[1];
}
while (yrange > 256)
{
yrange >>= 1;
++joyshry[1];
}
}
else
{
joytype[1] = J1C_DISABLED;
JOYCAPS caps;
if (JOYSTICK_1 >= 0 && joyGetDevCaps(JOYSTICK_1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR)
{
joyshrx[0] = 0;
joyshry[0] = 0;
joysubx[0] = (int)caps.wXmin;
joysuby[0] = (int)caps.wYmin;
UINT xrange = caps.wXmax - caps.wXmin;
UINT yrange = caps.wYmax - caps.wYmin;
while (xrange > 256)
{
xrange >>= 1;
++joyshrx[0];
}
while (yrange > 256)
{
yrange >>= 1;
++joyshry[0];
}
}
else
{
joytype[0] = J0C_KEYBD_NUMPAD;
}
}
//
// Init for emulated joystick #1:
//
if (joyinfo[joytype[1]] == DEVICE_JOYSTICK)
{
JOYCAPS caps;
if (JOYSTICK_2 >= 0 && joyGetDevCaps(JOYSTICK_2, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR)
{
joyshrx[1] = 0;
joyshry[1] = 0;
joysubx[1] = (int)caps.wXmin;
joysuby[1] = (int)caps.wYmin;
UINT xrange = caps.wXmax - caps.wXmin;
UINT yrange = caps.wYmax - caps.wYmin;
while (xrange > 256)
{
xrange >>= 1;
++joyshrx[1];
}
while (yrange > 256)
{
yrange >>= 1;
++joyshry[1];
}
}
else
{
joytype[1] = J1C_DISABLED;
}
}
else if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2)
{
JOYCAPS caps;
if (JOYSTICK_1 >= 0 && joyGetDevCaps(JOYSTICK_1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR)
{
joyshrx[1] = 0;
joyshry[1] = 0;
joysubx[1] = (int)caps.wZmin;
joysuby[1] = (int)caps.wRmin;
UINT xrange = caps.wZmax - caps.wZmin;
UINT yrange = caps.wRmax - caps.wRmin;
while (xrange > 256)
{
xrange >>= 1;
++joyshrx[1];
}
while (yrange > 256)
{
yrange >>= 1;
++joyshry[1];
}
}
else
{
joytype[1] = J1C_DISABLED;
}
}
}
else if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2)
{
JOYCAPS caps;
if (joyGetDevCaps(JOYSTICKID1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR)
{
joyshrx[1] = 0;
joyshry[1] = 0;
joysubx[1] = (int)caps.wZmin;
joysuby[1] = (int)caps.wRmin;
UINT xrange = caps.wZmax - caps.wZmin;
UINT yrange = caps.wRmax - caps.wRmin;
while (xrange > 256)
{
xrange >>= 1;
++joyshrx[1];
}
while (yrange > 256)
{
yrange >>= 1;
++joyshry[1];
}
}
else
{
joytype[1] = J1C_DISABLED;
}
}
}
//===========================================================================
@ -707,9 +755,9 @@ BOOL JoySetEmulationType(HWND window, DWORD newtype, int nJoystickNumber, const
if (joyinfo[newtype] == DEVICE_JOYSTICK || joyinfo[newtype] == DEVICE_JOYSTICK_THUMBSTICK2)
{
JOYCAPS caps;
unsigned int nJoy2ID = joyinfo[newtype] == DEVICE_JOYSTICK_THUMBSTICK2 ? JOYSTICKID1 : JOYSTICKID2;
unsigned int nJoyID = nJoystickNumber == JN_JOYSTICK0 ? JOYSTICKID1 : nJoy2ID;
if (joyGetDevCaps(nJoyID, &caps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
int nJoy2ID = joyinfo[newtype] == DEVICE_JOYSTICK_THUMBSTICK2 ? JOYSTICK_1 : JOYSTICK_2;
int nJoyID = nJoystickNumber == JN_JOYSTICK0 ? JOYSTICK_1 : nJoy2ID;
if (nJoyID < 0 || joyGetDevCaps(nJoyID, &caps, sizeof(JOYCAPS)) != JOYERR_NOERROR)
{
MessageBox(window,
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 JoySetHookAltKeys(bool hook);
void JoySetButtonVirtualKey(UINT button, UINT virtKey);
int GetJoystick1(void);
int GetJoystick2(void);
void JoySaveSnapshot(class YamlSaveHelper& yamlSaveHelper);
void JoyLoadSnapshot(class YamlLoadHelper& yamlLoadHelper, UINT version);

View File

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