From 77256ef2d77f3a5d239b4987d8a9477f178cca46 Mon Sep 17 00:00:00 2001 From: cathery Date: Wed, 13 May 2020 15:00:25 +0300 Subject: [PATCH] Update for libnx 3.2.0, gcc 10 and libstratosphere --- .vscode/c_cpp_properties.json | 6 ++- .vscode/tasks.json | 2 +- README.md | 8 +++- .../SwitchVirtualGamepadHandler.cpp | 20 +++++---- .../SwitchVirtualGamepadHandler.h | 7 +++- source/Sysmodule/source/config_handler.cpp | 13 ++++-- .../Sysmodule/source/controller_handler.cpp | 2 +- source/Sysmodule/source/log.cpp | 2 +- source/Sysmodule/source/psc_module.cpp | 14 +++++-- source/Sysmodule/source/usb_module.cpp | 41 +++++++++++++------ source/libstratosphere | 2 +- 11 files changed, 78 insertions(+), 39 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 4ab5f2f..cdeba24 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -17,11 +17,13 @@ "SWITCH", "__SWITCH__", "ATMOSPHERE_BOARD_NINTENDO_SWITCH", - "ATMOSPHERE_IS_STRATOSPHERE" + "ATMOSPHERE_IS_STRATOSPHERE", + "ATMOSPHERE_ARCH_ARM64", + "ATMOSPHERE_OS_HORIZON" ], "compilerPath": "${DEVKITPRO}/devkitA64/bin/aarch64-none-elf-g++", "cStandard": "c11", - "cppStandard": "c++17", + "cppStandard": "c++20", "intelliSenseMode": "gcc-x64" } ], diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 81e7c57..902daeb 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -5,7 +5,7 @@ "label": "Build Release", "type": "shell", "promptOnClose": true, - "command": "make -j8", + "command": "make -j3", "presentation": { "reveal": "always", "panel": "shared" diff --git a/README.md b/README.md index 1ac877a..ce03c3d 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,13 @@ sys-con comes with a config folder located at `sdmc:/config/sys-con/`. It contai ## Building (For developers) -Like all other switch projects, you need to have [devkitPro](https://switchbrew.org/wiki/Setting_up_Development_Environment) set up on your system. +Don't download the project as ZIP. It doesn't copy the submodules correctly and you will be left with empty folders. -This project uses libnx version 3.1.0 or later. +Instead, clone the repository **recursively** using any git client you have. (Git Bash, Git GUI, Github Desktop, etc.) + +Like all other switch projects, you need to have [devkitA64](https://switchbrew.org/wiki/Setting_up_Development_Environment) set up on your system. + +This project uses libnx version **3.2.0**. If you have **Visual Studio Code**, you can open the project as a folder and run the build tasks from inside the program. It also has Intellisense configured for switch development, if you have DEVKITPRO correctly defined in your environment variables. Handy! diff --git a/source/ControllerSwitch/SwitchVirtualGamepadHandler.cpp b/source/ControllerSwitch/SwitchVirtualGamepadHandler.cpp index cb16ff3..ff2dff1 100644 --- a/source/ControllerSwitch/SwitchVirtualGamepadHandler.cpp +++ b/source/ControllerSwitch/SwitchVirtualGamepadHandler.cpp @@ -37,30 +37,34 @@ void SwitchVirtualGamepadHandler::OutputThreadLoop(void *handler) Result SwitchVirtualGamepadHandler::InitInputThread() { - R_TRY(m_inputThread.Initialize(&SwitchVirtualGamepadHandler::InputThreadLoop, this, 0x30).GetValue()); m_inputThreadIsRunning = true; - return m_inputThread.Start().GetValue(); + R_ABORT_UNLESS(threadCreate(&m_inputThread, &SwitchVirtualGamepadHandler::InputThreadLoop, this, input_thread_stack, sizeof(input_thread_stack), 0x30, -2)); + R_ABORT_UNLESS(threadStart(&m_inputThread)); + return 0; } void SwitchVirtualGamepadHandler::ExitInputThread() { m_inputThreadIsRunning = false; - m_inputThread.CancelSynchronization(); - m_inputThread.Join(); + svcCancelSynchronization(m_inputThread.handle); + threadWaitForExit(&m_inputThread); + threadClose(&m_inputThread); } Result SwitchVirtualGamepadHandler::InitOutputThread() { - R_TRY(m_outputThread.Initialize(&SwitchVirtualGamepadHandler::OutputThreadLoop, this, 0x30).GetValue()); m_outputThreadIsRunning = true; - return m_outputThread.Start().GetValue(); + R_ABORT_UNLESS(threadCreate(&m_outputThread, &SwitchVirtualGamepadHandler::OutputThreadLoop, this, output_thread_stack, sizeof(output_thread_stack), 0x30, -2)); + R_ABORT_UNLESS(threadStart(&m_outputThread)); + return 0; } void SwitchVirtualGamepadHandler::ExitOutputThread() { m_outputThreadIsRunning = false; - m_outputThread.CancelSynchronization(); - m_outputThread.Join(); + svcCancelSynchronization(m_outputThread.handle); + threadWaitForExit(&m_outputThread); + threadClose(&m_outputThread); } static_assert(JOYSTICK_MAX == 32767 && JOYSTICK_MIN == -32767, diff --git a/source/ControllerSwitch/SwitchVirtualGamepadHandler.h b/source/ControllerSwitch/SwitchVirtualGamepadHandler.h index 4905c02..9400edd 100644 --- a/source/ControllerSwitch/SwitchVirtualGamepadHandler.h +++ b/source/ControllerSwitch/SwitchVirtualGamepadHandler.h @@ -10,8 +10,11 @@ protected: u32 m_vibrationDeviceHandle; std::unique_ptr m_controller; - ams::os::StaticThread<0x1'000> m_inputThread; - ams::os::StaticThread<0x1'000> m_outputThread; + alignas(ams::os::ThreadStackAlignment) u8 input_thread_stack[0x1000]; + alignas(ams::os::ThreadStackAlignment) u8 output_thread_stack[0x1000]; + + Thread m_inputThread; + Thread m_outputThread; bool m_inputThreadIsRunning = false; bool m_outputThreadIsRunning = false; diff --git a/source/Sysmodule/source/config_handler.cpp b/source/Sysmodule/source/config_handler.cpp index 3b66d6b..b02a287 100644 --- a/source/Sysmodule/source/config_handler.cpp +++ b/source/Sysmodule/source/config_handler.cpp @@ -20,9 +20,11 @@ namespace syscon::config UTimer filecheckTimer; Waiter filecheckTimerWaiter = waiterForUTimer(&filecheckTimer); + // Thread to check for any config changes void ConfigChangedCheckThreadFunc(void *arg); - ams::os::StaticThread<0x2'000> g_config_changed_check_thread(&ConfigChangedCheckThreadFunc, nullptr, 0x3E); + alignas(ams::os::ThreadStackAlignment) u8 config_thread_stack[0x2000]; + Thread g_config_changed_check_thread; bool is_config_changed_check_thread_running = false; @@ -317,14 +319,17 @@ namespace syscon::config return 1; utimerStart(&filecheckTimer); is_config_changed_check_thread_running = true; - return g_config_changed_check_thread.Start().GetValue(); + R_ABORT_UNLESS(threadCreate(&g_config_changed_check_thread, &ConfigChangedCheckThreadFunc, nullptr, config_thread_stack, sizeof(config_thread_stack), 0x3E, -2)); + R_ABORT_UNLESS(threadStart(&g_config_changed_check_thread)); + return 0; } void Disable() { is_config_changed_check_thread_running = false; utimerStop(&filecheckTimer); - g_config_changed_check_thread.CancelSynchronization(); - g_config_changed_check_thread.Join(); + svcCancelSynchronization(g_config_changed_check_thread.handle); + threadWaitForExit(&g_config_changed_check_thread); + threadClose(&g_config_changed_check_thread); } } // namespace syscon::config \ No newline at end of file diff --git a/source/Sysmodule/source/controller_handler.cpp b/source/Sysmodule/source/controller_handler.cpp index b377e89..f275f59 100644 --- a/source/Sysmodule/source/controller_handler.cpp +++ b/source/Sysmodule/source/controller_handler.cpp @@ -14,7 +14,7 @@ namespace syscon::controllers constexpr size_t MaxControllerHandlersSize = 10; std::vector> controllerHandlers; bool UseAbstractedPad; - ams::os::Mutex controllerMutex; + ams::os::Mutex controllerMutex(false); } // namespace bool IsAtControllerLimit() diff --git a/source/Sysmodule/source/log.cpp b/source/Sysmodule/source/log.cpp index 7634ffd..e52b088 100644 --- a/source/Sysmodule/source/log.cpp +++ b/source/Sysmodule/source/log.cpp @@ -3,7 +3,7 @@ #include "config_handler.h" #include -static ams::os::Mutex printMutex; +static ams::os::Mutex printMutex(false); void DiscardOldLogs() { diff --git a/source/Sysmodule/source/psc_module.cpp b/source/Sysmodule/source/psc_module.cpp index 7b176f7..798d0f4 100644 --- a/source/Sysmodule/source/psc_module.cpp +++ b/source/Sysmodule/source/psc_module.cpp @@ -13,9 +13,11 @@ namespace syscon::psc Waiter pscModuleWaiter; const uint16_t dependencies[] = {PscPmModuleId_Usb}; + //Thread to check for psc:pm state change (console waking up/going to sleep) void PscThreadFunc(void *arg); - ams::os::StaticThread<0x1'000> g_psc_thread(&PscThreadFunc, nullptr, 0x2C); + alignas(ams::os::ThreadStackAlignment) u8 psc_thread_stack[0x1000]; + Thread g_psc_thread; bool is_psc_thread_running = false; @@ -31,6 +33,7 @@ namespace syscon::psc { switch (pscState) { + case PscPmState_Awake: case PscPmState_ReadyAwaken: //usb::CreateUsbEvents(); break; @@ -53,7 +56,9 @@ namespace syscon::psc R_TRY(pscmGetPmModule(&pscModule, PscPmModuleId(126), dependencies, sizeof(dependencies) / sizeof(uint16_t), true)); pscModuleWaiter = waiterForEvent(&pscModule.event); is_psc_thread_running = true; - return g_psc_thread.Start().GetValue(); + R_ABORT_UNLESS(threadCreate(&g_psc_thread, &PscThreadFunc, nullptr, psc_thread_stack, sizeof(psc_thread_stack), 0x2C, -2)); + R_ABORT_UNLESS(threadStart(&g_psc_thread)); + return 0; } void Exit() @@ -64,7 +69,8 @@ namespace syscon::psc pscPmModuleClose(&pscModule); eventClose(&pscModule.event); - g_psc_thread.CancelSynchronization(); - g_psc_thread.Join(); + svcCancelSynchronization(g_psc_thread.handle); + threadWaitForExit(&g_psc_thread); + threadClose(&g_psc_thread); } }; // namespace syscon::psc \ No newline at end of file diff --git a/source/Sysmodule/source/usb_module.cpp b/source/Sysmodule/source/usb_module.cpp index 0a89fda..5450684 100644 --- a/source/Sysmodule/source/usb_module.cpp +++ b/source/Sysmodule/source/usb_module.cpp @@ -19,15 +19,22 @@ namespace syscon::usb constexpr size_t MaxUsbHsInterfacesSize = 16; - ams::os::Mutex usbMutex; + ams::os::Mutex usbMutex(false); + //Thread that waits on generic usb event void UsbEventThreadFunc(void *arg); + //Thread that waits on sony vendor usb event void UsbSonyEventThreadFunc(void *arg); + //Thread that waits on any disconnected usb devices void UsbInterfaceChangeThreadFunc(void *arg); - ams::os::StaticThread<0x2'000> g_usb_event_thread(&UsbEventThreadFunc, nullptr, 0x3A); - ams::os::StaticThread<0x2'000> g_sony_event_thread(&UsbSonyEventThreadFunc, nullptr, 0x3B); - ams::os::StaticThread<0x2'000> g_usb_interface_change_thread(&UsbInterfaceChangeThreadFunc, nullptr, 0x2C); + alignas(ams::os::ThreadStackAlignment) u8 usb_event_thread_stack[0x2000]; + alignas(ams::os::ThreadStackAlignment) u8 sony_event_thread_stack[0x2000]; + alignas(ams::os::ThreadStackAlignment) u8 usb_interface_change_thread_stack[0x2000]; + + Thread g_usb_event_thread; + Thread g_sony_event_thread; + Thread g_usb_interface_change_thread; bool is_usb_event_thread_running = false; bool is_usb_interface_change_thread_running = false; @@ -198,9 +205,13 @@ namespace syscon::usb is_usb_event_thread_running = true; is_usb_interface_change_thread_running = true; - R_TRY(g_usb_event_thread.Start().GetValue()); - R_TRY(g_sony_event_thread.Start().GetValue()); - R_TRY(g_usb_interface_change_thread.Start().GetValue()); + R_ABORT_UNLESS(threadCreate(&g_usb_event_thread, &UsbEventThreadFunc, nullptr, usb_event_thread_stack, sizeof(usb_event_thread_stack), 0x3A, -2)); + R_ABORT_UNLESS(threadCreate(&g_sony_event_thread, &UsbSonyEventThreadFunc, nullptr, sony_event_thread_stack, sizeof(sony_event_thread_stack), 0x3B, -2)); + R_ABORT_UNLESS(threadCreate(&g_usb_interface_change_thread, &UsbInterfaceChangeThreadFunc, nullptr, usb_interface_change_thread_stack, sizeof(usb_interface_change_thread_stack), 0x2C, -2)); + + R_ABORT_UNLESS(threadStart(&g_usb_event_thread)); + R_ABORT_UNLESS(threadStart(&g_sony_event_thread)); + R_ABORT_UNLESS(threadStart(&g_usb_interface_change_thread)); return 0; } @@ -209,13 +220,17 @@ namespace syscon::usb is_usb_event_thread_running = false; is_usb_interface_change_thread_running = false; - g_usb_event_thread.CancelSynchronization(); - g_sony_event_thread.CancelSynchronization(); - g_usb_interface_change_thread.CancelSynchronization(); + svcCancelSynchronization(g_usb_event_thread.handle); + threadWaitForExit(&g_usb_event_thread); + threadClose(&g_usb_event_thread); - g_usb_event_thread.Join(); - g_sony_event_thread.Join(); - g_usb_interface_change_thread.Join(); + svcCancelSynchronization(g_sony_event_thread.handle); + threadWaitForExit(&g_sony_event_thread); + threadClose(&g_sony_event_thread); + + svcCancelSynchronization(g_usb_interface_change_thread.handle); + threadWaitForExit(&g_usb_interface_change_thread); + threadClose(&g_usb_interface_change_thread); DestroyUsbEvents(); controllers::Reset(); diff --git a/source/libstratosphere b/source/libstratosphere index 62f5667..b38939a 160000 --- a/source/libstratosphere +++ b/source/libstratosphere @@ -1 +1 @@ -Subproject commit 62f5667b5f020f4a871ccd8f0362f2625d92e616 +Subproject commit b38939adb58c8a28eb9556aa7abbcbed4d823c43