diff --git a/source/FourPlay.cpp b/source/FourPlay.cpp index de80cb94..e537cd33 100644 --- a/source/FourPlay.cpp +++ b/source/FourPlay.cpp @@ -49,12 +49,10 @@ #include "StdAfx.h" #include "FourPlay.h" +#include "Joystick.h" #include "Memory.h" #include "YamlHelper.h" -extern int JOYSTICK_1; // declared in joystick.cpp -extern int JOYSTICK_2; - BYTE __stdcall FourPlayCard::IORead(WORD pc, WORD addr, BYTE bWrite, BYTE value, ULONG nExecutedCycles) { BYTE nOutput = MemReadFloatingBus(nExecutedCycles); @@ -76,7 +74,7 @@ BYTE __stdcall FourPlayCard::IORead(WORD pc, WORD addr, BYTE bWrite, BYTE value, switch (addr & 0xF) { case 0: // Joystick 1 - if (JOYSTICK_1 >= 0 && joyGetPosEx(JOYSTICK_1, &infoEx) == JOYERR_NOERROR) + if (GetJoystick1() >= 0 && joyGetPosEx(GetJoystick1(), &infoEx) == JOYERR_NOERROR) { xAxis = (infoEx.dwXpos >> 8) & 0xFF; yAxis = (infoEx.dwYpos >> 8) & 0xFF; @@ -90,7 +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 - if (JOYSTICK_2 >= 0 && joyGetPosEx(JOYSTICK_2, &infoEx) == JOYERR_NOERROR) + if (GetJoystick2() >= 0 && joyGetPosEx(GetJoystick2(), &infoEx) == JOYERR_NOERROR) { xAxis = (infoEx.dwXpos >> 8) & 0xFF; yAxis = (infoEx.dwYpos >> 8) & 0xFF; diff --git a/source/Joystick.cpp b/source/Joystick.cpp index ef179782..87f2091d 100644 --- a/source/Joystick.cpp +++ b/source/Joystick.cpp @@ -48,9 +48,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA enum {DEVICE_NONE=0, DEVICE_JOYSTICK, DEVICE_KEYBOARD, DEVICE_MOUSE, DEVICE_JOYSTICK_THUMBSTICK2}; -int JOYSTICK_1 = -1; -int JOYSTICK_2 = -1; - // Indexed by joytype[n] static const DWORD joyinfo[6] = { DEVICE_NONE, DEVICE_JOYSTICK, @@ -113,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) @@ -120,8 +120,18 @@ void JoySetHookAltKeys(bool hook) g_bHookAltKeys = hook; } +int GetJoystick1(void) +{ + return JOYSTICK_1; +} + +int GetJoystick2(void) +{ + return JOYSTICK_2; +} + //=========================================================================== -void CheckJoystick0() +static void CheckJoystick0() { if (JOYSTICK_1 < 0) return; @@ -148,7 +158,7 @@ void CheckJoystick0() } } -void CheckJoystick1() +static void CheckJoystick1() { static DWORD lastcheck = 0; DWORD currtime = GetTickCount(); @@ -156,7 +166,7 @@ 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 @@ -171,9 +181,12 @@ void CheckJoystick1() info.wYpos = infoEx.dwRpos; } } - else - result = joyGetPos(JOYSTICK_2, &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) @@ -197,7 +210,7 @@ void CheckJoystick1() void JoyInitialize() { // - // Detect First and Second connected JOYSTICK in WinMM apis. JOYSTICKID1 is 0 and is not always anymore the first connected joystick + // Detect First and Second connected JOYSTICK in WinMM API. JOYSTICKID1 == 0 but is not always the first connected joystick. // JOYSTICK_1 = -1; @@ -205,8 +218,8 @@ void JoyInitialize() bool firstFound = false; - UINT joys = joyGetNumDevs(); - for (int i = 0; i < joys; i++) + const UINT numDevs = joyGetNumDevs(); + for (UINT i = 0; i < numDevs; i++) { JOYCAPS caps; int ret = joyGetDevCaps(i, &caps, sizeof(JOYCAPS)); @@ -264,10 +277,10 @@ void JoyInitialize() // Init for emulated joystick #1: // - if (JOYSTICK_2 >= 0 && joyinfo[joytype[1]] == DEVICE_JOYSTICK) + if (joyinfo[joytype[1]] == DEVICE_JOYSTICK) { JOYCAPS caps; - if (joyGetDevCaps(JOYSTICK_2, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) + if (JOYSTICK_2 >= 0 && joyGetDevCaps(JOYSTICK_2, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) { joyshrx[1] = 0; joyshry[1] = 0; @@ -291,10 +304,10 @@ void JoyInitialize() joytype[1] = J1C_DISABLED; } } - else if (JOYSTICK_1 >= 0 && joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2) + else if (joyinfo[joytype[1]] == DEVICE_JOYSTICK_THUMBSTICK2) { JOYCAPS caps; - if (joyGetDevCaps(JOYSTICK_1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) + if (JOYSTICK_1 >= 0 && joyGetDevCaps(JOYSTICK_1, &caps, sizeof(JOYCAPS)) == JOYERR_NOERROR) { joyshrx[1] = 0; joyshry[1] = 0; @@ -742,8 +755,8 @@ 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 ? JOYSTICK_1 : JOYSTICK_2; - unsigned int nJoyID = nJoystickNumber == JN_JOYSTICK0 ? JOYSTICK_1 : nJoy2ID; + 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, diff --git a/source/Joystick.h b/source/Joystick.h index 1a8ded42..18285825 100644 --- a/source/Joystick.h +++ b/source/Joystick.h @@ -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); diff --git a/source/SNESMAX.cpp b/source/SNESMAX.cpp index 83460b83..e7a6db06 100644 --- a/source/SNESMAX.cpp +++ b/source/SNESMAX.cpp @@ -53,13 +53,11 @@ */ #include "StdAfx.h" +#include "Joystick.h" #include "SNESMAX.h" #include "Memory.h" #include "YamlHelper.h" -extern int JOYSTICK_1; // declared in joystick.cpp -extern int JOYSTICK_2; - // Default to Sony DS4 / DualSense: // b11,..,b0: St,Sl / -,-,R,L,X,A,B,Y // @@ -119,12 +117,12 @@ BYTE __stdcall SNESMAXCard::IOWrite(WORD pc, WORD addr, BYTE bWrite, BYTE value, controller1Buttons = 0; controller2Buttons = 0; - if (JOYSTICK_1 >= 0 && joyGetPosEx(JOYSTICK_1, &infoEx) == JOYERR_NOERROR) + if (GetJoystick1() >= 0 && joyGetPosEx(GetJoystick1(), &infoEx) == JOYERR_NOERROR) controller1Buttons = pCard->GetControllerButtons(JOYSTICKID1, infoEx, pCard->m_altControllerType[0]); controller1Buttons = ~controller1Buttons; - if (JOYSTICK_2 >= 0 && joyGetPosEx(JOYSTICK_2, &infoEx) == JOYERR_NOERROR) + if (GetJoystick2() >= 0 && joyGetPosEx(GetJoystick2(), &infoEx) == JOYERR_NOERROR) controller2Buttons = pCard->GetControllerButtons(JOYSTICKID2, infoEx, pCard->m_altControllerType[1]); controller2Buttons = ~controller2Buttons;