From a546273b4651494801edc50c2c60293ecffe6ade Mon Sep 17 00:00:00 2001 From: cathery Date: Mon, 20 Apr 2020 14:31:55 +0300 Subject: [PATCH] Merge dualshock events into one vendor event (thanks @p-sam) --- source/Sysmodule/source/config_handler.cpp | 116 +++++++++------------ source/Sysmodule/source/config_handler.h | 2 - source/Sysmodule/source/usb_module.cpp | 78 +++----------- source/Sysmodule/source/usb_module.h | 2 - 4 files changed, 68 insertions(+), 130 deletions(-) diff --git a/source/Sysmodule/source/config_handler.cpp b/source/Sysmodule/source/config_handler.cpp index 4eb4ea1..d99611c 100644 --- a/source/Sysmodule/source/config_handler.cpp +++ b/source/Sysmodule/source/config_handler.cpp @@ -82,87 +82,76 @@ namespace syscon::config int ParseConfigLine(void *dummy, const char *section, const char *name, const char *value) { - if (strcmp(section, "global") == 0) + if (strncmp(name, "key_", 4) == 0) { - if (strcmp(name, "use_dualshock_2nd_generation") == 0) - { - tempGlobalConfig.dualshock4_productID = (strcmp(value, "true") ? PRODUCT_DUALSHOCK4_1X : PRODUCT_DUALSHOCK4_2X); - return 1; - } + ControllerButton button = StringToKey(name + 4); + ControllerButton buttonValue = StringToKey(value); + tempConfig.buttons[button] = buttonValue; + tempConfig.buttons[buttonValue] = button; + return 1; } - else + else if (strcmp(name, "left_stick_deadzone") == 0) { - if (strncmp(name, "key_", 4) == 0) + tempConfig.leftStickDeadzonePercent = atoi(value); + return 1; + } + else if (strcmp(name, "right_stick_deadzone") == 0) + { + tempConfig.rightStickDeadzonePercent = atoi(value); + return 1; + } + else if (strcmp(name, "left_stick_rotation") == 0) + { + tempConfig.leftStickRotationDegrees = atoi(value); + return 1; + } + else if (strcmp(name, "right_stick_rotation") == 0) + { + tempConfig.rightStickRotationDegrees = atoi(value); + return 1; + } + else if (strcmp(name, "trigger_deadzone") == 0) + { + tempConfig.triggerDeadzonePercent = atoi(value); + return 1; + } + else if (strcmp(name, "swap_dpad_and_lstick") == 0) + { + tempConfig.swapDPADandLSTICK = (strcmp(value, "true") ? false : true); + return 1; + } + else if (strcmp(name, "firmware_path") == 0) + { + strcpy(firmwarePath, value); + return 1; + } + else if (strncmp(name, "color_", 6) == 0) + { + if (strcmp(name + 6, "body") == 0) { - ControllerButton button = StringToKey(name + 4); - ControllerButton buttonValue = StringToKey(value); - tempConfig.buttons[button] = buttonValue; - tempConfig.buttons[buttonValue] = button; + tempConfig.bodyColor = DecodeColorValue(value); return 1; } - else if (strcmp(name, "left_stick_deadzone") == 0) + else if (strcmp(name + 6, "buttons") == 0) { - tempConfig.leftStickDeadzonePercent = atoi(value); + tempConfig.buttonsColor = DecodeColorValue(value); return 1; } - else if (strcmp(name, "right_stick_deadzone") == 0) + else if (strcmp(name + 6, "leftGrip") == 0) { - tempConfig.rightStickDeadzonePercent = atoi(value); + tempConfig.leftGripColor = DecodeColorValue(value); return 1; } - else if (strcmp(name, "left_stick_rotation") == 0) + else if (strcmp(name + 6, "rightGrip") == 0) { - tempConfig.leftStickRotationDegrees = atoi(value); + tempConfig.rightGripColor = DecodeColorValue(value); return 1; } - else if (strcmp(name, "right_stick_rotation") == 0) + else if (strcmp(name + 6, "led") == 0) { - tempConfig.rightStickRotationDegrees = atoi(value); + tempColor = DecodeColorValue(value); return 1; } - else if (strcmp(name, "trigger_deadzone") == 0) - { - tempConfig.triggerDeadzonePercent = atoi(value); - return 1; - } - else if (strcmp(name, "swap_dpad_and_lstick") == 0) - { - tempConfig.swapDPADandLSTICK = (strcmp(value, "true") ? false : true); - return 1; - } - else if (strcmp(name, "firmware_path") == 0) - { - strcpy(firmwarePath, value); - return 1; - } - else if (strncmp(name, "color_", 6) == 0) - { - if (strcmp(name + 6, "body") == 0) - { - tempConfig.bodyColor = DecodeColorValue(value); - return 1; - } - else if (strcmp(name + 6, "buttons") == 0) - { - tempConfig.buttonsColor = DecodeColorValue(value); - return 1; - } - else if (strcmp(name + 6, "leftGrip") == 0) - { - tempConfig.leftGripColor = DecodeColorValue(value); - return 1; - } - else if (strcmp(name + 6, "rightGrip") == 0) - { - tempConfig.rightGripColor = DecodeColorValue(value); - return 1; - } - else if (strcmp(name + 6, "led") == 0) - { - tempColor = DecodeColorValue(value); - return 1; - } - } } return 0; @@ -189,7 +178,6 @@ namespace syscon::config { WriteToLog("File check succeeded! Loading configs..."); config::LoadAllConfigs(); - usb::ReloadDualshock4Event(); } } } while (is_config_changed_check_thread_running); diff --git a/source/Sysmodule/source/config_handler.h b/source/Sysmodule/source/config_handler.h index 0e43792..de4c3f8 100644 --- a/source/Sysmodule/source/config_handler.h +++ b/source/Sysmodule/source/config_handler.h @@ -14,12 +14,10 @@ namespace syscon::config { struct GlobalConfig { - uint16_t dualshock4_productID; }; inline GlobalConfig globalConfig { - .dualshock4_productID = PRODUCT_DUALSHOCK4_1X, }; void LoadGlobalConfig(const GlobalConfig &config); diff --git a/source/Sysmodule/source/usb_module.cpp b/source/Sysmodule/source/usb_module.cpp index c27f5e6..ab0366c 100644 --- a/source/Sysmodule/source/usb_module.cpp +++ b/source/Sysmodule/source/usb_module.cpp @@ -15,8 +15,7 @@ namespace syscon::usb namespace { constexpr u8 CatchAllEventIndex = 2; - constexpr u8 Dualshock3EventIndex = 0; - constexpr u8 Dualshock4EventIndex = 1; + constexpr u8 SonyEventIndex = 0; constexpr size_t MaxUsbHsInterfacesSize = 16; @@ -24,21 +23,18 @@ namespace syscon::usb ams::os::Mutex usbMutex; void UsbEventThreadFunc(void *arg); - void UsbDs3EventThreadFunc(void *arg); - void UsbDs4EventThreadFunc(void *arg); + void UsbSonyEventThreadFunc(void *arg); void UsbInterfaceChangeThreadFunc(void *arg); ams::os::StaticThread<0x2'000> g_usb_event_thread(&UsbEventThreadFunc, nullptr, 0x3A); - ams::os::StaticThread<0x2'000> g_ds3_event_thread(&UsbDs3EventThreadFunc, nullptr, 0x3B); - ams::os::StaticThread<0x2'000> g_ds4_event_thread(&UsbDs4EventThreadFunc, nullptr, 0x3C); + 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); bool is_usb_event_thread_running = false; bool is_usb_interface_change_thread_running = false; Event g_usbCatchAllEvent{}; - Event g_usbDualshock3Event{}; - Event g_usbDualshock4Event{}; + Event g_usbSonyEvent{}; UsbHsInterface interfaces[MaxUsbHsInterfacesSize]; s32 QueryInterfaces(u8 iclass, u8 isubclass, u8 iprotocol); @@ -72,12 +68,12 @@ namespace syscon::usb } while (is_usb_event_thread_running); } - void UsbDs3EventThreadFunc(void *arg) + void UsbSonyEventThreadFunc(void *arg) { do { - if (R_SUCCEEDED(eventWait(&g_usbDualshock3Event, UINT64_MAX))) + if (R_SUCCEEDED(eventWait(&g_usbSonyEvent, UINT64_MAX))) { - WriteToLog("Dualshock 3 event went off"); + WriteToLog("Sony event went off"); std::scoped_lock usbLock(usbMutex); if (!controllers::IsAtControllerLimit()) @@ -86,23 +82,8 @@ namespace syscon::usb if ((QueryVendorProduct(VENDOR_SONY, PRODUCT_DUALSHOCK3) != 0) && (total_entries = QueryInterfaces(USB_CLASS_HID, 0, 0)) != 0) WriteToLog("Initializing Dualshock 3 controller: 0x%x", controllers::Insert(std::make_unique(std::make_unique(interfaces, total_entries)))); - } - } - } while (is_usb_event_thread_running); - } - void UsbDs4EventThreadFunc(void *arg) - { - do { - if (R_SUCCEEDED(eventWait(&g_usbDualshock4Event, UINT64_MAX))) - { - WriteToLog("Dualshock 4 event went off"); - - std::scoped_lock usbLock(usbMutex); - if (!controllers::IsAtControllerLimit()) - { - s32 total_entries; - if ((QueryVendorProduct(VENDOR_SONY, config::globalConfig.dualshock4_productID) != 0) + else if ((QueryVendorProduct(VENDOR_SONY, PRODUCT_DUALSHOCK4_1X) != 0 || QueryVendorProduct(VENDOR_SONY, PRODUCT_DUALSHOCK4_2X) != 0) && (total_entries = QueryInterfaces(USB_CLASS_HID, 0, 0)) != 0) WriteToLog("Initializing Dualshock 4 controller: 0x%x", controllers::Insert(std::make_unique(std::make_unique(interfaces, total_entries)))); } @@ -190,24 +171,13 @@ namespace syscon::usb return usbHsCreateInterfaceAvailableEvent(&g_usbCatchAllEvent, true, CatchAllEventIndex, &filter); } - inline Result CreateDualshock3AvailableEvent() + inline Result CreateSonyAvailableEvent() { constexpr UsbHsInterfaceFilter filter { - .Flags = UsbHsInterfaceFilterFlags_idVendor | UsbHsInterfaceFilterFlags_idProduct, + .Flags = UsbHsInterfaceFilterFlags_idVendor, .idVendor = VENDOR_SONY, - .idProduct = PRODUCT_DUALSHOCK3, }; - return usbHsCreateInterfaceAvailableEvent(&g_usbDualshock3Event, true, Dualshock3EventIndex, &filter); - } - - inline Result CreateDualshock4AvailableEvent() - { - const UsbHsInterfaceFilter filter{ - .Flags = UsbHsInterfaceFilterFlags_idVendor | UsbHsInterfaceFilterFlags_idProduct, - .idVendor = VENDOR_SONY, - .idProduct = config::globalConfig.dualshock4_productID, - }; - return usbHsCreateInterfaceAvailableEvent(&g_usbDualshock4Event, true, Dualshock4EventIndex, &filter); + return usbHsCreateInterfaceAvailableEvent(&g_usbSonyEvent, true, SonyEventIndex, &filter); } } @@ -228,8 +198,7 @@ namespace syscon::usb is_usb_event_thread_running = true; R_TRY(g_usb_event_thread.Start().GetValue()); - R_TRY(g_ds3_event_thread.Start().GetValue()); - R_TRY(g_ds4_event_thread.Start().GetValue()); + R_TRY(g_sony_event_thread.Start().GetValue()); is_usb_interface_change_thread_running = true; R_TRY(g_usb_interface_change_thread.Start().GetValue()); return 0; @@ -244,13 +213,11 @@ namespace syscon::usb //TODO: test this without the cancel g_usb_event_thread.CancelSynchronization(); - g_ds3_event_thread.CancelSynchronization(); - g_ds4_event_thread.CancelSynchronization(); + g_sony_event_thread.CancelSynchronization(); g_usb_interface_change_thread.CancelSynchronization(); g_usb_event_thread.Join(); - g_ds3_event_thread.Join(); - g_ds4_event_thread.Join(); + g_sony_event_thread.Join(); g_usb_interface_change_thread.Join(); controllers::Reset(); @@ -261,26 +228,13 @@ namespace syscon::usb if (g_usbCatchAllEvent.revent != INVALID_HANDLE) return 0x99; R_TRY(CreateCatchAllAvailableEvent()); - R_TRY(CreateDualshock3AvailableEvent()); - R_TRY(CreateDualshock4AvailableEvent()); - /* - R_TRY(g_usb_event_thread.CancelSynchronization().GetValue()); - R_TRY(g_ds3_event_thread.CancelSynchronization().GetValue()); - R_TRY(g_ds4_event_thread.CancelSynchronization().GetValue()); - */ + R_TRY(CreateSonyAvailableEvent()); return 0; } void DestroyUsbEvents() { usbHsDestroyInterfaceAvailableEvent(&g_usbCatchAllEvent, CatchAllEventIndex); - usbHsDestroyInterfaceAvailableEvent(&g_usbDualshock3Event, Dualshock3EventIndex); - usbHsDestroyInterfaceAvailableEvent(&g_usbDualshock4Event, Dualshock4EventIndex); - } - - Result ReloadDualshock4Event() - { - usbHsDestroyInterfaceAvailableEvent(&g_usbDualshock4Event, Dualshock4EventIndex); - return CreateDualshock4AvailableEvent(); + usbHsDestroyInterfaceAvailableEvent(&g_usbSonyEvent, SonyEventIndex); } } \ No newline at end of file diff --git a/source/Sysmodule/source/usb_module.h b/source/Sysmodule/source/usb_module.h index 8f8329c..dc0e73a 100644 --- a/source/Sysmodule/source/usb_module.h +++ b/source/Sysmodule/source/usb_module.h @@ -10,6 +10,4 @@ namespace syscon::usb { Result CreateUsbEvents(); void DestroyUsbEvents(); - - Result ReloadDualshock4Event(); } \ No newline at end of file