From e9c9c67a122dcec31091d71d37c491a0588ba3e6 Mon Sep 17 00:00:00 2001 From: cathery Date: Wed, 4 Dec 2019 21:44:01 +0300 Subject: [PATCH] Reimplement SwitchVirtualGamepadHandler threads using SwitchThread --- .../include/SwitchVirtualGamepadHandler.h | 17 ++----- .../source/SwitchVirtualGamepadHandler.cpp | 50 ++++++------------- 2 files changed, 21 insertions(+), 46 deletions(-) diff --git a/SwitchUSB/include/SwitchVirtualGamepadHandler.h b/SwitchUSB/include/SwitchVirtualGamepadHandler.h index d29e5f2..7d20f59 100644 --- a/SwitchUSB/include/SwitchVirtualGamepadHandler.h +++ b/SwitchUSB/include/SwitchVirtualGamepadHandler.h @@ -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 &&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; } diff --git a/SwitchUSB/source/SwitchVirtualGamepadHandler.cpp b/SwitchUSB/source/SwitchVirtualGamepadHandler.cpp index 1ef34bc..bd64fe4 100644 --- a/SwitchUSB/source/SwitchVirtualGamepadHandler.cpp +++ b/SwitchUSB/source/SwitchVirtualGamepadHandler.cpp @@ -18,58 +18,40 @@ void SwitchVirtualGamepadHandler::Exit() { } -void inputThreadLoop(void *handler) +void SwitchVirtualGamepadHandler::InputThreadLoop(void *handler) { - svcSleepThread(1e+7L); - SwitchVirtualGamepadHandler *switchHandler = static_cast(handler); - bool *keepThreadRunning = switchHandler->GetInputThreadBool(); - - while (*keepThreadRunning) - { - switchHandler->UpdateInput(); - } + static_cast(handler)->UpdateInput(); } -void outputThreadLoop(void *handler) +void SwitchVirtualGamepadHandler::OutputThreadLoop(void *handler) { - svcSleepThread(1e+7L); - SwitchVirtualGamepadHandler *switchHandler = static_cast(handler); - bool *keepThreadRunning = switchHandler->GetInputThreadBool(); - - while (*keepThreadRunning) - { - switchHandler->UpdateOutput(); - } + static_cast(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(); } \ No newline at end of file