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

Update code structure

This commit is contained in:
cathery 2019-11-22 00:40:42 +03:00
parent cbe4e5a39b
commit 17b205d8d0
3 changed files with 111 additions and 115 deletions

View File

@ -18,7 +18,7 @@
#define DUALSHOCK3CONFIG "config_dualshock3.ini" #define DUALSHOCK3CONFIG "config_dualshock3.ini"
#define DUALSHOCK4CONFIG "config_dualshock4.ini" #define DUALSHOCK4CONFIG "config_dualshock4.ini"
std::array<const char *, NUM_CONTROLLERBUTTONS> keyNames{ constexpr std::array<const char *, NUM_CONTROLLERBUTTONS> keyNames{
"FACE_UP", "FACE_UP",
"FACE_RIGHT", "FACE_RIGHT",
"FACE_DOWN", "FACE_DOWN",

View File

@ -2,10 +2,13 @@
#include "log.h" #include "log.h"
#include "mainLoop.h" #include "mainLoop.h"
//CHANGELOG
// Fixed an issue where unplugging one controller would unplug others
//ISSUES: //ISSUES:
// when exiting the applet, only one of the controllers is reset // Kosmos Toolbox doesn't free up the memory associated with the sysmodule due to hiddbgAttachHdlsWorkBuffer() memory not being freed up
// Rumble is currently missing on all controllers // After plugging and unplugging a controller for a while, sysmodule stops working
// Kosmos Toolbox doesn't allow this sysmodule to be turned on after turning it off, probably due to heap memory not being freed up // DS3 controller seems to send random inputs
//TODO: //TODO:
// Shrink unneessary heap memory/stack size used for the sysmodule // Shrink unneessary heap memory/stack size used for the sysmodule
@ -14,7 +17,7 @@
extern "C" extern "C"
{ {
// Adjust size as needed. // Adjust size as needed.
#define INNER_HEAP_SIZE 0x40000 #define INNER_HEAP_SIZE 0x40'000
#ifndef __APPLET__ #ifndef __APPLET__
u32 __nx_applet_type = AppletType_None; u32 __nx_applet_type = AppletType_None;
@ -75,6 +78,15 @@ extern "C"
hiddbgReleaseHdlsWorkBuffer(); hiddbgReleaseHdlsWorkBuffer();
hiddbgExit(); hiddbgExit();
} }
alignas(16) u8 __nx_exception_stack[0x1000];
u64 __nx_exception_stack_size = sizeof(__nx_exception_stack);
__attribute__((weak)) u32 __nx_exception_ignoredebug = 1;
void __libnx_exception_handler(ThreadExceptionDump *ctx)
{
WriteToLog("Sysmodule crashed with error 0x", std::hex, ctx->error_desc);
}
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -9,13 +9,84 @@
#include "SwitchAbstractedPadHandler.h" #include "SwitchAbstractedPadHandler.h"
#include "configFile.h" #include "configFile.h"
#define APP_VERSION "0.5.0" #define APP_VERSION "0.5.1"
struct VendorEvent static const bool useAbstractedPad = hosversionBetween(5, 7);
std::vector<std::unique_ptr<SwitchVirtualGamepadHandler>> controllerInterfaces;
static GlobalConfig _globalConfig{};
void LoadGlobalConfig(const GlobalConfig *config)
{ {
uint16_t vendor; _globalConfig = *config;
Event event; }
};
Result CallInitHandler(std::unique_ptr<IController> &controllerPtr)
{
if (controllerPtr)
{
std::unique_ptr<SwitchVirtualGamepadHandler> switchHandler;
if (useAbstractedPad)
switchHandler = std::make_unique<SwitchAbstractedPadHandler>(std::move(controllerPtr));
else
switchHandler = std::make_unique<SwitchHDLHandler>(std::move(controllerPtr));
Result rc = switchHandler->Initialize();
if (R_SUCCEEDED(rc))
{
controllerInterfaces.push_back(std::move(switchHandler));
WriteToLog("Interface created successfully");
return 0;
}
else
{
WriteToLog("Error creating interface with error ", rc);
return rc;
}
}
return 1;
}
Result CreateDualshck3AvailableEvent(Event &out)
{
UsbHsInterfaceFilter filter;
filter.Flags = UsbHsInterfaceFilterFlags_idVendor | UsbHsInterfaceFilterFlags_idProduct;
filter.idVendor = VENDOR_SONY;
filter.idProduct = PRODUCT_DUALSHOCK3;
Result rc = usbHsCreateInterfaceAvailableEvent(&out, true, 0, &filter);
if (R_FAILED(rc))
WriteToLog("Failed to open event for Dualshock 3");
else
WriteToLog("Successfully created event for Dualshock 3");
return rc;
}
Result CreateDualshock4AvailableEvent(Event &out)
{
UsbHsInterfaceFilter filter;
filter.Flags = UsbHsInterfaceFilterFlags_idVendor | UsbHsInterfaceFilterFlags_idProduct;
filter.idVendor = VENDOR_SONY;
filter.idProduct = _globalConfig.dualshock4_productID;
Result rc = usbHsCreateInterfaceAvailableEvent(&out, true, 1, &filter);
if (R_FAILED(rc))
WriteToLog("Failed to open event for Dualshock 4 0x", std::hex, _globalConfig.dualshock4_productID);
else
WriteToLog("Successfully created event for Dualshock 4 0x", std::hex, _globalConfig.dualshock4_productID);
return rc;
}
Result CreateAllAvailableEvent(Event &out)
{
UsbHsInterfaceFilter filter;
filter.Flags = UsbHsInterfaceFilterFlags_bcdDevice_Min;
filter.bcdDevice_Min = 0;
Result rc = usbHsCreateInterfaceAvailableEvent(&out, true, 2, &filter);
if (R_FAILED(rc))
WriteToLog("Failed to open catch-all event");
else
WriteToLog("Successfully created catch-all event");
return rc;
}
Result QueryInterfaces(UsbHsInterface *interfaces, size_t interfaces_size, s32 *total_entries, u8 infclass, u8 infsubclass, u8 infprotocol) Result QueryInterfaces(UsbHsInterface *interfaces, size_t interfaces_size, s32 *total_entries, u8 infclass, u8 infsubclass, u8 infprotocol)
{ {
@ -44,109 +115,28 @@ Result QueryVendorProduct(UsbHsInterface *interfaces, size_t interfaces_size, s3
return 1; return 1;
} }
std::unique_ptr<IUSBDevice> devicePtr;
std::unique_ptr<IController> controllerPtr;
bool useAbstractedPad = hosversionBetween(5, 7);
std::vector<std::unique_ptr<SwitchVirtualGamepadHandler>> controllerInterfaces;
static GlobalConfig _globalConfig{};
void LoadGlobalConfig(const GlobalConfig *config)
{
_globalConfig = *config;
}
Result CreateDualshock4AvailableEvent(Event &event)
{
UsbHsInterfaceFilter filter;
filter.Flags = UsbHsInterfaceFilterFlags_idVendor | UsbHsInterfaceFilterFlags_idProduct;
filter.idVendor = VENDOR_SONY;
filter.idProduct = _globalConfig.dualshock4_productID;
Result rc = usbHsCreateInterfaceAvailableEvent(&event, true, 2, &filter);
if (R_FAILED(rc))
WriteToLog("Failed to open event for Dualshock 4 0x", std::hex, _globalConfig.dualshock4_productID);
else
WriteToLog("Successfully created event for Dualshock 4 0x", std::hex, _globalConfig.dualshock4_productID);
return rc;
}
Result CallInitHandler()
{
if (controllerPtr)
{
Result rc;
std::unique_ptr<SwitchVirtualGamepadHandler> switchHandler;
if (useAbstractedPad)
switchHandler = std::make_unique<SwitchAbstractedPadHandler>(std::move(controllerPtr));
else
switchHandler = std::make_unique<SwitchHDLHandler>(std::move(controllerPtr));
rc = switchHandler->Initialize();
if (R_SUCCEEDED(rc))
{
controllerInterfaces.push_back(std::move(switchHandler));
WriteToLog("Interface created successfully");
return 0;
}
else
{
WriteToLog("Error creating interface with error ", rc);
return rc;
}
}
return 1;
}
Result mainLoop() Result mainLoop()
{ {
WriteToLog("\n\nNew sysmodule session started on version " APP_VERSION); WriteToLog("\n\nNew sysmodule session started on version " APP_VERSION);
Result rc = 0; Result rc = 0;
Event catchAllEvent; std::unique_ptr<IController> controllerPtr;
Event ds3Event;
Event ds4Event;
UTimer filecheckTimer; UTimer filecheckTimer;
Waiter filecheckTimerWaiter = waiterForUTimer(&filecheckTimer); Waiter filecheckTimerWaiter = waiterForUTimer(&filecheckTimer);
utimerCreate(&filecheckTimer, 1e+9L, TimerType_Repeating); utimerCreate(&filecheckTimer, 1e+9L, TimerType_Repeating);
utimerStart(&filecheckTimer); utimerStart(&filecheckTimer);
CheckForFileChanges(); CheckForFileChanges();
LoadAllConfigs(); LoadAllConfigs();
{ Event catchAllEvent;
UsbHsInterfaceFilter filter; Event ds3Event;
//filter.Flags = UsbHsInterfaceFilterFlags_bInterfaceClass | UsbHsInterfaceFilterFlags_bcdDevice_Min; Event ds4Event;
//filter.bInterfaceClass = USB_CLASS_HID;
//filter.bcdDevice_Min = 0;
filter.Flags = UsbHsInterfaceFilterFlags_idVendor | UsbHsInterfaceFilterFlags_idProduct;
filter.idVendor = VENDOR_SONY;
filter.idProduct = PRODUCT_DUALSHOCK3;
rc = usbHsCreateInterfaceAvailableEvent(&ds3Event, true, 0, &filter);
if (R_FAILED(rc))
WriteToLog("Failed to open event for Dualshock 3");
else
WriteToLog("Successfully created event for Dualshock 3");
filter.Flags = UsbHsInterfaceFilterFlags_bcdDevice_Min; CreateDualshck3AvailableEvent(ds3Event);
filter.bcdDevice_Min = 0; CreateAllAvailableEvent(catchAllEvent);
rc = usbHsCreateInterfaceAvailableEvent(&catchAllEvent, true, 1, &filter); CreateDualshock4AvailableEvent(ds4Event);
if (R_FAILED(rc))
WriteToLog("Failed to open catch-all event");
else
WriteToLog("Successfully created catch-all event");
/*
filter.Flags = UsbHsInterfaceFilterFlags_idVendor | UsbHsInterfaceFilterFlags_idProduct;
filter.idVendor = VENDOR_SONY;
filter.idProduct = PRODUCT_DUALSHOCK4_2X;
rc = usbHsCreateInterfaceAvailableEvent(&ds4Event, true, 2, &filter);
if (R_FAILED(rc))
WriteToLog("Failed to open event for Dualshock 4 2x");
else
WriteToLog("Successfully created event for Dualshock 4 2x");
*/
CreateDualshock4AvailableEvent(ds4Event);
}
controllerInterfaces.reserve(10); controllerInterfaces.reserve(10);
@ -193,37 +183,32 @@ Result mainLoop()
if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 93, 1))) if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 93, 1)))
{ {
WriteToLog("Registering Xbox 360 controller"); WriteToLog("Registering Xbox 360 controller");
devicePtr = std::make_unique<SwitchUSBDevice>(interfaces, total_entries); controllerPtr = std::make_unique<Xbox360Controller>(std::make_unique<SwitchUSBDevice>(interfaces, total_entries));
controllerPtr = std::make_unique<Xbox360Controller>(std::move(devicePtr));
} }
else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 93, 129))) else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 93, 129)))
{ {
WriteToLog("Registering Xbox 360 Wireless adapter"); WriteToLog("Registering Xbox 360 Wireless adapter");
for (int i = 0; i != total_entries; ++i) for (int i = 0; i != total_entries; ++i)
{ {
devicePtr = std::make_unique<SwitchUSBDevice>(interfaces + i, 1); controllerPtr = std::make_unique<Xbox360WirelessController>(std::make_unique<SwitchUSBDevice>(interfaces + i, 1));
controllerPtr = std::make_unique<Xbox360WirelessController>(std::move(devicePtr)); CallInitHandler(controllerPtr);
CallInitHandler();
} }
} }
else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, 0x58, 0x42, 0x00))) else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, 0x58, 0x42, 0x00)))
{ {
WriteToLog("Registering Xbox One controller"); WriteToLog("Registering Xbox One controller");
devicePtr = std::make_unique<SwitchUSBDevice>(interfaces, total_entries); controllerPtr = std::make_unique<XboxController>(std::make_unique<SwitchUSBDevice>(interfaces, total_entries));
controllerPtr = std::make_unique<XboxController>(std::move(devicePtr));
} }
else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 71, 208))) else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 71, 208)))
{ {
WriteToLog("Registering Xbox One controller"); WriteToLog("Registering Xbox One controller");
devicePtr = std::make_unique<SwitchUSBDevice>(interfaces, total_entries); controllerPtr = std::make_unique<XboxOneController>(std::make_unique<SwitchUSBDevice>(interfaces, total_entries));
controllerPtr = std::make_unique<XboxOneController>(std::move(devicePtr));
} }
/* /*
else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 255, 255))) else if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_VENDOR_SPEC, 255, 255)))
{ {
WriteToLog("Registering Xbox One adapter"); WriteToLog("Registering Xbox One adapter");
devicePtr = std::make_unique<SwitchUSBDevice>(interfaces, total_entries); controllerPtr = std::make_unique<XboxOneAdapter>(std::make_unique<SwitchUSBDevice>(interfaces, total_entries));
controllerPtr = std::make_unique<XboxOneAdapter>(std::move(devicePtr));
} }
*/ */
} }
@ -238,8 +223,7 @@ Result mainLoop()
if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_HID, 0, 0))) if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_HID, 0, 0)))
{ {
WriteToLog("Registering DS3 controller"); WriteToLog("Registering DS3 controller");
devicePtr = std::make_unique<SwitchUSBDevice>(interfaces, total_entries); controllerPtr = std::make_unique<Dualshock3Controller>(std::make_unique<SwitchUSBDevice>(interfaces, total_entries));
controllerPtr = std::make_unique<Dualshock3Controller>(std::move(devicePtr));
} }
} }
rc = eventWait(&ds4Event, 0); rc = eventWait(&ds4Event, 0);
@ -252,11 +236,10 @@ Result mainLoop()
if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_HID, 0, 0))) if (R_SUCCEEDED(QueryInterfaces(interfaces, sizeof(interfaces), &total_entries, USB_CLASS_HID, 0, 0)))
{ {
WriteToLog("Registering DS4 controller"); WriteToLog("Registering DS4 controller");
devicePtr = std::make_unique<SwitchUSBDevice>(interfaces, total_entries); controllerPtr = std::make_unique<Dualshock4Controller>(std::make_unique<SwitchUSBDevice>(interfaces, total_entries));
controllerPtr = std::make_unique<Dualshock4Controller>(std::move(devicePtr));
} }
} }
CallInitHandler(); CallInitHandler(controllerPtr);
//On interface change event, check if any devices were removed, and erase them from memory appropriately //On interface change event, check if any devices were removed, and erase them from memory appropriately
rc = eventWait(usbHsGetInterfaceStateChangeEvent(), 0); rc = eventWait(usbHsGetInterfaceStateChangeEvent(), 0);
@ -299,6 +282,7 @@ Result mainLoop()
} }
} }
//Checks every 1 second for any changes in config files, then reloads them
rc = waitSingle(filecheckTimerWaiter, 0); rc = waitSingle(filecheckTimerWaiter, 0);
if (R_SUCCEEDED(rc)) if (R_SUCCEEDED(rc))
{ {