1
0
mirror of https://github.com/cathery/sys-con.git synced 2024-07-08 03:58:43 +00:00

Reimplement psc and event threads using SwitchThread

This commit is contained in:
cathery 2019-12-04 20:40:34 +03:00
parent cd64016cbd
commit b4dfe8a548

View File

@ -8,6 +8,7 @@
#include "SwitchHDLHandler.h" #include "SwitchHDLHandler.h"
#include "SwitchAbstractedPadHandler.h" #include "SwitchAbstractedPadHandler.h"
#include "configFile.h" #include "configFile.h"
#include "SwitchThread.h"
#define APP_VERSION "0.6.0" #define APP_VERSION "0.6.0"
@ -15,6 +16,12 @@
#define DS4EVENT_INDEX 1 #define DS4EVENT_INDEX 1
#define ALLEVENT_INDEX 2 #define ALLEVENT_INDEX 2
#ifdef __APPLET__
#define APPLET_STACKSIZE 0x1'000
#else
#define APPLET_STACKSIZE 0x0
#endif
static const bool useAbstractedPad = hosversionBetween(5, 7); static const bool useAbstractedPad = hosversionBetween(5, 7);
std::vector<std::unique_ptr<SwitchVirtualGamepadHandler>> controllerInterfaces; std::vector<std::unique_ptr<SwitchVirtualGamepadHandler>> controllerInterfaces;
std::unique_ptr<IController> controllerPtr; std::unique_ptr<IController> controllerPtr;
@ -140,44 +147,39 @@ void inline CloseEvents()
struct PSCLoopBuffer struct PSCLoopBuffer
{ {
PscPmModule &pscModule; PscPmModule &pscModule;
bool &pscLoopRunning;
bool &shouldSleep; bool &shouldSleep;
}; };
void pscLoop(void *buffer) void pscLoop(void *buffer)
{ {
Waiter pscModuleWaiter = waiterForEvent(&static_cast<PSCLoopBuffer *>(buffer)->pscModule.event);
PscPmState pscState; PscPmState pscState;
u32 out_flags; u32 out_flags;
while (static_cast<PSCLoopBuffer *>(buffer)->pscLoopRunning) Result rc = waitSingle(waiterForEvent(&static_cast<PSCLoopBuffer *>(buffer)->pscModule.event), U64_MAX);
if (R_SUCCEEDED(rc))
{ {
Result rc = waitSingle(pscModuleWaiter, U64_MAX); rc = pscPmModuleGetRequest(&static_cast<PSCLoopBuffer *>(buffer)->pscModule, &pscState, &out_flags);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
{ {
rc = pscPmModuleGetRequest(&static_cast<PSCLoopBuffer *>(buffer)->pscModule, &pscState, &out_flags); switch (pscState)
if (R_SUCCEEDED(rc))
{ {
switch (pscState) case PscPmState_ReadyAwaken:
{ OpenEvents();
case PscPmState_ReadyAwaken: static_cast<PSCLoopBuffer *>(buffer)->shouldSleep = false;
OpenEvents(); break;
static_cast<PSCLoopBuffer *>(buffer)->shouldSleep = false; case PscPmState_ReadySleep:
break; case PscPmState_ReadyShutdown:
case PscPmState_ReadySleep: CloseEvents();
case PscPmState_ReadyShutdown: static_cast<PSCLoopBuffer *>(buffer)->shouldSleep = true;
CloseEvents(); //controllerInterfaces.clear();
static_cast<PSCLoopBuffer *>(buffer)->shouldSleep = true; break;
//controllerInterfaces.clear(); default:
break; break;
default:
break;
}
pscPmModuleAcknowledge(&static_cast<PSCLoopBuffer *>(buffer)->pscModule, pscState);
} }
pscPmModuleAcknowledge(&static_cast<PSCLoopBuffer *>(buffer)->pscModule, pscState);
} }
} }
}
void CheckForInterfaces() void CheckForInterfaces()
{ {
@ -224,6 +226,30 @@ void CheckForInterfaces()
} }
} }
void eventLoop(void *buffer)
{
if (R_SUCCEEDED(eventWait(static_cast<Event *>(buffer), U64_MAX)))
{
CheckForInterfaces();
WriteToLog("Catch-all event went off");
}
}
Result errorLoop()
{
#ifdef __APPLET__
WriteToLog("Press B to exit...");
while (appletMainLoop())
{
hidScanInput();
if (hidKeysDown(CONTROLLER_P1_AUTO) & KEY_B)
break;
LockedUpdateConsole();
}
#endif
return 0;
}
Result mainLoop() Result mainLoop()
{ {
@ -240,25 +266,30 @@ Result mainLoop()
rc = pscmGetPmModule(&pscModule, static_cast<PscPmModuleId>(126), dependencies, sizeof(dependencies) / sizeof(uint16_t), true); rc = pscmGetPmModule(&pscModule, static_cast<PscPmModuleId>(126), dependencies, sizeof(dependencies) / sizeof(uint16_t), true);
WriteToLog("Get module result: 0x%x", rc); WriteToLog("Get module result: 0x%x", rc);
if (R_FAILED(rc))
return errorLoop();
bool pscLoopRunning = true;
bool shouldSleep = false; bool shouldSleep = false;
Thread pscThread; PSCLoopBuffer loopBuffer{pscModule, shouldSleep};
PSCLoopBuffer loopBuffer{pscModule, pscLoopRunning, shouldSleep};
threadCreate(&pscThread, pscLoop, &loopBuffer, NULL, 0x300, 0x3B, -2); SwitchThread pscThread = SwitchThread(pscLoop, &loopBuffer, 0x300, 0x3B);
WriteToLog("Is psc thread running: %i", pscThread.IsRunning());
rc = threadStart(&pscThread);
WriteToLog("PSC thread start: 0x", std::hex, rc);
CheckForFileChanges(); CheckForFileChanges();
LoadAllConfigs(); LoadAllConfigs();
rc = OpenEvents(); rc = OpenEvents();
if (R_FAILED(rc)) if (R_FAILED(rc))
{
WriteToLog("Failed to open events: 0x%x", rc); WriteToLog("Failed to open events: 0x%x", rc);
return errorLoop();
}
controllerInterfaces.reserve(10); controllerInterfaces.reserve(10);
constexpr size_t eventThreadStack = 0x1'500 + APPLET_STACKSIZE;
SwitchThread eventThread(&eventLoop, &catchAllEvent, eventThreadStack, 0x20);
WriteToLog("Is event thread running: %i", eventThread.IsRunning());
while (appletMainLoop()) while (appletMainLoop())
{ {
if (!shouldSleep) if (!shouldSleep)
@ -340,17 +371,17 @@ Result mainLoop()
#endif #endif
} }
//After we break out of the loop, close all events and exit WriteToLog("Closing PSC module");
WriteToLog("Destroying events");
CloseEvents();
pscPmModuleFinalize(&pscModule); pscPmModuleFinalize(&pscModule);
pscPmModuleClose(&pscModule); pscPmModuleClose(&pscModule);
eventClose(&pscModule.event);
pscThread.Close();
pscLoopRunning = false; WriteToLog("Destroying events");
threadWaitForExit(&pscThread); CloseEvents();
threadClose(&pscThread); eventThread.Close();
WriteToLog("Clearing interfaces");
controllerInterfaces.clear(); controllerInterfaces.clear();
return rc; return rc;
} }