1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-07-08 03:58:43 +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 "switch.h"
#include "IController.h" #include "IController.h"
#include "SwitchControllerHandler.h" #include "SwitchControllerHandler.h"
#include "SwitchThread.h"
//This class is a base class for SwitchHDLHandler and SwitchAbstractedPaadHandler. //This class is a base class for SwitchHDLHandler and SwitchAbstractedPaadHandler.
class SwitchVirtualGamepadHandler class SwitchVirtualGamepadHandler
@ -10,22 +11,16 @@ protected:
u32 m_vibrationDeviceHandle; u32 m_vibrationDeviceHandle;
SwitchControllerHandler m_controllerHandler; SwitchControllerHandler m_controllerHandler;
bool m_keepInputThreadRunning; SwitchThread m_inputThread;
Thread m_inputThread; SwitchThread m_outputThread;
bool m_keepOutputThreadRunning; static void InputThreadLoop(void *argument);
Thread m_outputThread; static void OutputThreadLoop(void *argument);
public: public:
SwitchVirtualGamepadHandler(std::unique_ptr<IController> &&controller); SwitchVirtualGamepadHandler(std::unique_ptr<IController> &&controller);
virtual ~SwitchVirtualGamepadHandler(); 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 //Override this if you want a custom init procedure
virtual Result Initialize(); virtual Result Initialize();
//Override this if you want a custom exit procedure //Override this if you want a custom exit procedure
@ -47,8 +42,6 @@ public:
virtual void UpdateOutput() = 0; virtual void UpdateOutput() = 0;
//Get the raw controller handler pointer //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 SwitchControllerHandler *GetControllerHandler() { return &m_controllerHandler; }
inline IController *GetController() { return m_controllerHandler.GetController(); } inline IController *GetController() { return m_controllerHandler.GetController(); }
inline u32 *GetVibrationHandle() { return &m_vibrationDeviceHandle; } 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); static_cast<SwitchVirtualGamepadHandler *>(handler)->UpdateInput();
SwitchVirtualGamepadHandler *switchHandler = static_cast<SwitchVirtualGamepadHandler *>(handler);
bool *keepThreadRunning = switchHandler->GetInputThreadBool();
while (*keepThreadRunning)
{
switchHandler->UpdateInput();
}
} }
void outputThreadLoop(void *handler) void SwitchVirtualGamepadHandler::OutputThreadLoop(void *handler)
{ {
svcSleepThread(1e+7L); static_cast<SwitchVirtualGamepadHandler *>(handler)->UpdateOutput();
SwitchVirtualGamepadHandler *switchHandler = static_cast<SwitchVirtualGamepadHandler *>(handler);
bool *keepThreadRunning = switchHandler->GetInputThreadBool();
while (*keepThreadRunning)
{
switchHandler->UpdateOutput();
}
} }
Result SwitchVirtualGamepadHandler::InitInputThread() Result SwitchVirtualGamepadHandler::InitInputThread()
{ {
m_keepInputThreadRunning = true; Result rc = m_inputThread.Initialize(0x400, 0x3B);
Result rc = threadCreate(&m_inputThread, &inputThreadLoop, this, NULL, 0x400, 0x3B, -2); if (R_SUCCEEDED(rc))
if (R_FAILED(rc)) rc = m_inputThread.Start(&SwitchVirtualGamepadHandler::InputThreadLoop, this);
return rc;
return threadStart(&m_inputThread); return rc;
} }
void SwitchVirtualGamepadHandler::ExitInputThread() void SwitchVirtualGamepadHandler::ExitInputThread()
{ {
m_keepInputThreadRunning = false; m_inputThread.Exit();
threadWaitForExit(&m_inputThread);
threadClose(&m_inputThread);
} }
Result SwitchVirtualGamepadHandler::InitOutputThread() Result SwitchVirtualGamepadHandler::InitOutputThread()
{ {
m_keepOutputThreadRunning = true; Result rc = m_outputThread.Initialize(0x400, 0x3B);
Result rc = threadCreate(&m_outputThread, &outputThreadLoop, this, NULL, 0x400, 0x3B, -2); if (R_SUCCEEDED(rc))
if (R_FAILED(rc)) rc = m_outputThread.Start(&SwitchVirtualGamepadHandler::OutputThreadLoop, this);
return rc;
return threadStart(&m_outputThread); return rc;
} }
void SwitchVirtualGamepadHandler::ExitOutputThread() void SwitchVirtualGamepadHandler::ExitOutputThread()
{ {
m_keepOutputThreadRunning = false; m_outputThread.Exit();
threadWaitForExit(&m_outputThread);
threadClose(&m_outputThread);
} }