1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-07-05 10:48:46 +00:00

Reimplement SwitchVirtualGamepadHandler threads using SwitchThread

This commit is contained in:
cathery 2019-12-04 21:44:01 +03:00
parent 4f1247fe0c
commit e9c9c67a12
2 changed files with 21 additions and 46 deletions

View File

@ -2,6 +2,7 @@
#include "switch.h"
#include "IController.h"
#include "SwitchControllerHandler.h"
#include "SwitchThread.h"
//This class is a base class for SwitchHDLHandler and SwitchAbstractedPaadHandler.
class SwitchVirtualGamepadHandler
@ -10,22 +11,16 @@ protected:
u32 m_vibrationDeviceHandle;
SwitchControllerHandler m_controllerHandler;
bool m_keepInputThreadRunning;
Thread m_inputThread;
SwitchThread m_inputThread;
SwitchThread m_outputThread;
bool m_keepOutputThreadRunning;
Thread m_outputThread;
static void InputThreadLoop(void *argument);
static void OutputThreadLoop(void *argument);
public:
SwitchVirtualGamepadHandler(std::unique_ptr<IController> &&controller);
virtual ~SwitchVirtualGamepadHandler();
//Don't allow to move this class in any way because it holds a thread member that reads from the instance's address
SwitchVirtualGamepadHandler(SwitchVirtualGamepadHandler &&other) = delete;
SwitchVirtualGamepadHandler(SwitchVirtualGamepadHandler &other) = delete;
SwitchVirtualGamepadHandler &operator=(SwitchVirtualGamepadHandler &&other) = delete;
SwitchVirtualGamepadHandler &operator=(SwitchVirtualGamepadHandler &other) = delete;
//Override this if you want a custom init procedure
virtual Result Initialize();
//Override this if you want a custom exit procedure
@ -47,8 +42,6 @@ public:
virtual void UpdateOutput() = 0;
//Get the raw controller handler pointer
inline bool *GetInputThreadBool() { return &m_keepInputThreadRunning; }
inline bool *GetOutputThreadBool() { return &m_keepOutputThreadRunning; }
inline SwitchControllerHandler *GetControllerHandler() { return &m_controllerHandler; }
inline IController *GetController() { return m_controllerHandler.GetController(); }
inline u32 *GetVibrationHandle() { return &m_vibrationDeviceHandle; }

View File

@ -18,58 +18,40 @@ void SwitchVirtualGamepadHandler::Exit()
{
}
void inputThreadLoop(void *handler)
void SwitchVirtualGamepadHandler::InputThreadLoop(void *handler)
{
svcSleepThread(1e+7L);
SwitchVirtualGamepadHandler *switchHandler = static_cast<SwitchVirtualGamepadHandler *>(handler);
bool *keepThreadRunning = switchHandler->GetInputThreadBool();
while (*keepThreadRunning)
{
switchHandler->UpdateInput();
}
static_cast<SwitchVirtualGamepadHandler *>(handler)->UpdateInput();
}
void outputThreadLoop(void *handler)
void SwitchVirtualGamepadHandler::OutputThreadLoop(void *handler)
{
svcSleepThread(1e+7L);
SwitchVirtualGamepadHandler *switchHandler = static_cast<SwitchVirtualGamepadHandler *>(handler);
bool *keepThreadRunning = switchHandler->GetInputThreadBool();
while (*keepThreadRunning)
{
switchHandler->UpdateOutput();
}
static_cast<SwitchVirtualGamepadHandler *>(handler)->UpdateOutput();
}
Result SwitchVirtualGamepadHandler::InitInputThread()
{
m_keepInputThreadRunning = true;
Result rc = threadCreate(&m_inputThread, &inputThreadLoop, this, NULL, 0x400, 0x3B, -2);
if (R_FAILED(rc))
return rc;
return threadStart(&m_inputThread);
Result rc = m_inputThread.Initialize(0x400, 0x3B);
if (R_SUCCEEDED(rc))
rc = m_inputThread.Start(&SwitchVirtualGamepadHandler::InputThreadLoop, this);
return rc;
}
void SwitchVirtualGamepadHandler::ExitInputThread()
{
m_keepInputThreadRunning = false;
threadWaitForExit(&m_inputThread);
threadClose(&m_inputThread);
m_inputThread.Exit();
}
Result SwitchVirtualGamepadHandler::InitOutputThread()
{
m_keepOutputThreadRunning = true;
Result rc = threadCreate(&m_outputThread, &outputThreadLoop, this, NULL, 0x400, 0x3B, -2);
if (R_FAILED(rc))
return rc;
return threadStart(&m_outputThread);
Result rc = m_outputThread.Initialize(0x400, 0x3B);
if (R_SUCCEEDED(rc))
rc = m_outputThread.Start(&SwitchVirtualGamepadHandler::OutputThreadLoop, this);
return rc;
}
void SwitchVirtualGamepadHandler::ExitOutputThread()
{
m_keepOutputThreadRunning = false;
threadWaitForExit(&m_outputThread);
threadClose(&m_outputThread);
m_outputThread.Exit();
}