diff --git a/Dockerfile b/Dockerfile index f96a7e2..978ec5a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ -FROM ghcr.io/wiiu-env/devkitppc:20230621 +FROM ghcr.io/wiiu-env/devkitppc:20231112 -COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:20230719 /artifacts $DEVKITPRO +COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:0.8.0-dev-20231221-ca17105 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libnotifications:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libmappedmemory:20230621 /artifacts $DEVKITPRO diff --git a/Makefile b/Makefile index 902a4d4..e6402f4 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ CFLAGS := -Wall -O2 -ffunction-sections \ CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__ -D__WUPS__ -CXXFLAGS := $(CFLAGS) -std=c++20 -fno-exceptions -fno-rtti +CXXFLAGS := $(CFLAGS) -std=c++20 ASFLAGS := -g $(ARCH) LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libmappedmemory.ld $(WUPSSPECS) diff --git a/src/main.cpp b/src/main.cpp index f383403..1e35468 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,6 @@ #include "utils/config.h" #include "utils/logger.h" #include -#include #include // Mandatory plugin information. @@ -46,25 +45,40 @@ INITIALIZE_PLUGIN() { DEBUG_FUNCTION_LINE_ERR("Failed to init notification lib"); } - // Open storage to read values - WUPSStorageError storageRes = WUPS_OpenStorage(); - if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to open storage %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); - } else { - LOAD_BOOL_FROM_STORAGE(ENABLED_CONFIG_STRING, gEnabled); - LOAD_BOOL_FROM_STORAGE(SWAP_SCREENS_CONFIG_STRING, gDoScreenSwap); - LOAD_BOOL_FROM_STORAGE(ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING, gSwapScreenButtonComboEnabled); - LOAD_BOOL_FROM_STORAGE(ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING, gChangeAudioModeButtonComboEnabled); - LOAD_BOOL_FROM_STORAGE(ENABLE_NOTIFICATIONS_CONFIG_STRING, gShowNotifications); - LOAD_INT_FROM_STORAGE(SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING, gSwapScreenButtonCombo); - LOAD_INT_FROM_STORAGE(CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING, gSwapAudioButtonCombo); - LOAD_INT_FROM_STORAGE(SCREEN_MODE_CONFIG_STRING, gCurAudioMode); - - // Close storage - if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } + if (WUPSStorageAPI::GetOrStoreDefault(ENABLED_CONFIG_STRING, gEnabled, DEFAULT_ENABLED_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", ENABLED_CONFIG_STRING); } + if (WUPSStorageAPI::GetOrStoreDefault(SWAP_SCREENS_CONFIG_STRING, gDoScreenSwap, DEFAULT_SWAP_SCREENS_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", SWAP_SCREENS_CONFIG_STRING); + } + if (WUPSStorageAPI::GetOrStoreDefault(ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING, gSwapScreenButtonComboEnabled, DEFAULT_ENABLED_SWAP_SCREENS_COMBO_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING); + } + if (WUPSStorageAPI::GetOrStoreDefault(ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING, gChangeAudioModeButtonComboEnabled, DEFAULT_ENABLED_CHANGE_AUDIO_COMBO_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING); + } + if (WUPSStorageAPI::GetOrStoreDefault(ENABLE_NOTIFICATIONS_CONFIG_STRING, gShowNotifications, DEFAULT_ENABLE_NOTIFICATIONS_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", ENABLE_NOTIFICATIONS_CONFIG_STRING); + } + if (WUPSStorageAPI::GetOrStoreDefault(SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING, gSwapScreenButtonCombo, (uint32_t) DEFAULT_SWAP_SCREEN_BUTTON_COMBO_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING); + } + if (WUPSStorageAPI::GetOrStoreDefault(CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING, gSwapAudioButtonCombo, (uint32_t) DEFAULT_CHANGE_AUDIO_BUTTON_COMBO_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING); + } + if (WUPSStorageAPI::GetOrStoreDefault(AUDIO_MODE_CONFIG_STRING, gCurAudioMode, DEFAULT_AUDIO_MODE_CONFIG_VALUE) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to get or create item \"%s\"", AUDIO_MODE_CONFIG_STRING); + } + + if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to save storage"); + } + + WUPSConfigAPIOptionsV1 configOptions = {.name = "Swip Swap Me"}; + if (WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to init config api"); + } + deinitLogging(); } @@ -81,18 +95,15 @@ ON_APPLICATION_START() { } ON_APPLICATION_REQUESTS_EXIT() { - // Open storage to write current config - WUPSStorageError storageRes = WUPS_OpenStorage(); - if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to open storage %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); - } else { - WUPS_StoreInt(nullptr, SCREEN_MODE_CONFIG_STRING, (int32_t) gCurAudioMode); - WUPS_StoreBool(nullptr, SWAP_SCREENS_CONFIG_STRING, (int32_t) gDoScreenSwap); - - // Close storage - if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } + if (WUPSStorageAPI::Store(AUDIO_MODE_CONFIG_STRING, gCurAudioMode) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to store screen mode to storage"); } + if (WUPSStorageAPI::Store(SWAP_SCREENS_CONFIG_STRING, gDoScreenSwap) != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to store screen mode to storage"); + } + if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to save storage"); + } + deinitLogging(); } diff --git a/src/retain_vars.cpp b/src/retain_vars.cpp index bcbedbb..d6dfa51 100644 --- a/src/retain_vars.cpp +++ b/src/retain_vars.cpp @@ -1,12 +1,13 @@ #include "retain_vars.hpp" -#include +#include "utils/config.h" -uint32_t gSwapScreenButtonCombo = VPAD_BUTTON_TV; -uint32_t gSwapAudioButtonCombo = VPAD_BUTTON_STICK_L; -bool gSwapScreenButtonComboEnabled = true; -bool gChangeAudioModeButtonComboEnabled = false; -bool gEnabled = true; -bool gDoScreenSwap = false; -bool gShowNotifications = true; -bool gNotificationModuleInitDone = true; -SwipSwapAudioMode gCurAudioMode = AUDIO_MODE_MATCH_SCREEN; \ No newline at end of file +bool gEnabled = DEFAULT_ENABLED_CONFIG_VALUE; +bool gDoScreenSwap = DEFAULT_SWAP_SCREENS_CONFIG_VALUE; +uint32_t gSwapScreenButtonCombo = DEFAULT_SWAP_SCREEN_BUTTON_COMBO_CONFIG_VALUE; +uint32_t gSwapAudioButtonCombo = DEFAULT_CHANGE_AUDIO_BUTTON_COMBO_CONFIG_VALUE; +bool gSwapScreenButtonComboEnabled = DEFAULT_ENABLED_SWAP_SCREENS_COMBO_CONFIG_VALUE; +bool gChangeAudioModeButtonComboEnabled = DEFAULT_ENABLED_CHANGE_AUDIO_COMBO_CONFIG_VALUE; +bool gShowNotifications = DEFAULT_ENABLE_NOTIFICATIONS_CONFIG_VALUE; +SwipSwapAudioMode gCurAudioMode = DEFAULT_AUDIO_MODE_CONFIG_VALUE; + +bool gNotificationModuleInitDone = true; \ No newline at end of file diff --git a/src/utils/WUPSConfigItemButtonCombo.cpp b/src/utils/WUPSConfigItemButtonCombo.cpp index a52cb63..f1f1ffc 100644 --- a/src/utils/WUPSConfigItemButtonCombo.cpp +++ b/src/utils/WUPSConfigItemButtonCombo.cpp @@ -10,7 +10,7 @@ #include #include -const char *getButtonChar(VPADButtons value) { +static const char *getButtonChar(VPADButtons value) { std::string combo; if (value & VPAD_BUTTON_A) { return "\ue000"; @@ -69,7 +69,7 @@ const char *getButtonChar(VPADButtons value) { return ""; } -std::string getComboAsString(uint32_t value) { +static std::string getComboAsString(uint32_t value) { char comboString[60]; memset(comboString, 0, sizeof(comboString)); @@ -90,22 +90,20 @@ std::string getComboAsString(uint32_t value) { return res; } -int32_t WUPSConfigItemButtonCombo_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { +static int32_t WUPSConfigItemButtonCombo_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { auto *item = (ConfigItemButtonCombo *) context; snprintf(out_buf, out_size, "%s", getComboAsString(item->value).c_str()); return 0; } -bool WUPSConfigItemButtonCombo_callCallback(void *context) { +static void WUPSConfigItemButtonCombo_onCloseCallback(void *context) { auto *item = (ConfigItemButtonCombo *) context; - if (item->callback != nullptr) { - ((ButtonComboValueChangedCallback) (item->callback))(item, item->value); - return true; + if (item->valueAtCreation != item->value && item->valueChangedCallback != nullptr) { + ((ButtonComboValueChangedCallback) (item->valueChangedCallback))(item, item->value); } - return false; } -void checkForHold(ConfigItemButtonCombo *item) { +static void checkForHold(ConfigItemButtonCombo *item) { uint32_t lastHold = 0; uint32_t holdFor = 0; uint32_t holdForTarget = item->holdDurationInMs >> 4; @@ -121,9 +119,8 @@ void checkForHold(ConfigItemButtonCombo *item) { while (true) { uint32_t buttonsHold = 0; VPADReadError vpad_error = VPAD_READ_UNINITIALIZED; - VPADStatus vpad_data; - VPADRead(VPAD_CHAN_0, &vpad_data, 1, &vpad_error); - if (vpad_error == VPAD_READ_SUCCESS) { + VPADStatus vpad_data = {}; + if (VPADRead(VPAD_CHAN_0, &vpad_data, 1, &vpad_error) > 0 && vpad_error == VPAD_READ_SUCCESS) { buttonsHold = vpad_data.hold; } @@ -165,20 +162,16 @@ void checkForHold(ConfigItemButtonCombo *item) { } } -void WUPSConfigItemButtonCombo_onButtonPressed(void *context, WUPSConfigButtons buttons) { +static void WUPSConfigItemButtonCombo_onInput(void *context, WUPSConfigSimplePadData input) { auto *item = (ConfigItemButtonCombo *) context; if (item->state == BUTTON_COMBO_STATE_NONE) { - if ((buttons & WUPS_CONFIG_BUTTON_A) == WUPS_CONFIG_BUTTON_A) { + if ((input.buttons_d & WUPS_CONFIG_BUTTON_A) == WUPS_CONFIG_BUTTON_A) { item->state = BUTTON_COMBO_STATE_PREPARE_FOR_HOLD; } } } -bool WUPSConfigItemButtonCombo_isMovementAllowed(void *context) { - return true; -} - -int32_t WUPSConfigItemButtonCombo_getCurrentValueSelectedDisplay(void *context, char *out_buf, int32_t out_size) { +static int32_t WUPSConfigItemButtonCombo_getCurrentValueSelectedDisplay(void *context, char *out_buf, int32_t out_size) { auto *item = (ConfigItemButtonCombo *) context; if (item->state == BUTTON_COMBO_STATE_PREPARE_FOR_HOLD || item->state == BUTTON_COMBO_STATE_WAIT_FOR_HOLD) { if (item->state == BUTTON_COMBO_STATE_PREPARE_FOR_HOLD) { @@ -194,72 +187,112 @@ int32_t WUPSConfigItemButtonCombo_getCurrentValueSelectedDisplay(void *context, return 0; } -void WUPSConfigItemButtonCombo_restoreDefault(void *context) { +static void WUPSConfigItemButtonCombo_restoreDefault(void *context) { auto *item = (ConfigItemButtonCombo *) context; item->value = item->defaultValue; } -void WUPSConfigItemButtonCombo_onSelected(void *context, bool isSelected) { -} - -void WUPSConfigItemButtonCombo_onDelete(void *context) { - auto *item = (ConfigItemButtonCombo *) context; - if (item->configId) { - free(item->configId); +static void WUPSConfigItemButtonCombo_Cleanup(ConfigItemButtonCombo *item) { + if (!item) { + return; } + free((void *) item->identifier); free(item); } -extern "C" bool -WUPSConfigItemButtonComboAddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName, uint32_t defaultComboInVPADButtons, uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, ButtonComboValueChangedCallback callback) { - if (cat == 0) { - return false; +static void WUPSConfigItemButtonCombo_onDelete(void *context) { + WUPSConfigItemButtonCombo_Cleanup((ConfigItemButtonCombo *) context); +} + +extern "C" WUPSConfigAPIStatus +WUPSConfigItemButtonCombo_CreateEx(const char *identifier, + const char *displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, + VPADButtons abortButton, + uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback, + WUPSConfigItemHandle *outHandle) { + if (outHandle == nullptr) { + return WUPSCONFIG_API_RESULT_INVALID_ARGUMENT; } auto *item = (ConfigItemButtonCombo *) malloc(sizeof(ConfigItemButtonCombo)); if (item == nullptr) { OSReport("WUPSConfigItemButtonComboAddToCategoryEx: Failed to allocate memory for item data.\n"); - return false; + return WUPSCONFIG_API_RESULT_INVALID_ARGUMENT; } - if (configID != nullptr) { - item->configId = strdup(configID); + if (identifier != nullptr) { + item->identifier = strdup(identifier); } else { - item->configId = nullptr; + item->identifier = nullptr; } item->abortButton = abortButton; item->abortButtonHoldDurationInMs = abortButtonHoldDurationInMs; item->holdDurationInMs = holdDurationInMs; item->defaultValue = defaultComboInVPADButtons; - item->value = defaultComboInVPADButtons; - item->callback = (void *) callback; + item->value = currentComboInVPADButtons; + item->valueAtCreation = currentComboInVPADButtons; + item->valueChangedCallback = (void *) callback; item->state = BUTTON_COMBO_STATE_NONE; - WUPSConfigCallbacks_t callbacks = { + WUPSConfigAPIItemCallbacksV2 callbacks = { .getCurrentValueDisplay = &WUPSConfigItemButtonCombo_getCurrentValueDisplay, .getCurrentValueSelectedDisplay = &WUPSConfigItemButtonCombo_getCurrentValueSelectedDisplay, - .onSelected = &WUPSConfigItemButtonCombo_onSelected, + .onSelected = nullptr, .restoreDefault = &WUPSConfigItemButtonCombo_restoreDefault, - .isMovementAllowed = &WUPSConfigItemButtonCombo_isMovementAllowed, - .callCallback = &WUPSConfigItemButtonCombo_callCallback, - .onButtonPressed = &WUPSConfigItemButtonCombo_onButtonPressed, - .onDelete = &WUPSConfigItemButtonCombo_onDelete}; + .isMovementAllowed = nullptr, + .onCloseCallback = &WUPSConfigItemButtonCombo_onCloseCallback, + .onInput = &WUPSConfigItemButtonCombo_onInput, + .onInputEx = nullptr, + .onDelete = &WUPSConfigItemButtonCombo_onDelete, + }; - if (WUPSConfigItem_Create(&item->handle, configID, displayName, callbacks, item) < 0) { + WUPSConfigAPIItemOptionsV2 options = { + .displayName = displayName, + .context = item, + .callbacks = callbacks, + }; + + WUPSConfigAPIStatus err; + if ((err = WUPSConfigAPI_Item_Create(options, &item->handle)) != WUPSCONFIG_API_RESULT_SUCCESS) { OSReport("WUPSConfigItemButtonComboAddToCategoryEx: Failed to create config item.\n"); - free(item); - return false; + WUPSConfigItemButtonCombo_Cleanup(item); + return err; } - if (WUPSConfigCategory_AddItem(cat, item->handle) < 0) { - OSReport("WUPSConfigItemButtonComboAddToCategoryEx: Failed to add item to category.\n"); - WUPSConfigItem_Destroy(item->handle); - return false; + *outHandle = item->handle; + return WUPSCONFIG_API_RESULT_SUCCESS; +} + + +extern "C" WUPSConfigAPIStatus +WUPSConfigItemButtonCombo_AddToCategoryEx(WUPSConfigCategoryHandle cat, + const char *identifier, + const char *displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback) { + WUPSConfigItemHandle itemHandle; + WUPSConfigAPIStatus res; + if ((res = WUPSConfigItemButtonCombo_CreateEx(identifier, + displayName, + defaultComboInVPADButtons, currentComboInVPADButtons, + holdDurationInMs, abortButton, abortButtonHoldDurationInMs, + callback, &itemHandle)) != WUPSCONFIG_API_RESULT_SUCCESS) { + return res; } - return true; + + if ((res = WUPSConfigAPI_Category_AddItem(cat, itemHandle)) != WUPSCONFIG_API_RESULT_SUCCESS) { + + WUPSConfigAPI_Item_Destroy(itemHandle); + return res; + } + return WUPSCONFIG_API_RESULT_SUCCESS; } extern "C" bool -WUPSConfigItemButtonComboAddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName, uint32_t defaultComboInVPADButtons, ButtonComboValueChangedCallback callback) { - return WUPSConfigItemButtonComboAddToCategoryEx(cat, configID, displayName, defaultComboInVPADButtons, 2000, VPAD_BUTTON_B, 250, callback); +WUPSConfigItemButtonComboAddToCategory(WUPSConfigCategoryHandle cat, const char *displayName, uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, ButtonComboValueChangedCallback callback) { + return WUPSConfigItemButtonComboAddToCategoryEx(cat, displayName, defaultComboInVPADButtons, currentComboInVPADButtons, 2000, VPAD_BUTTON_B, 250, callback); } \ No newline at end of file diff --git a/src/utils/WUPSConfigItemButtonCombo.h b/src/utils/WUPSConfigItemButtonCombo.h index 80c90fb..1d3ec23 100644 --- a/src/utils/WUPSConfigItemButtonCombo.h +++ b/src/utils/WUPSConfigItemButtonCombo.h @@ -13,31 +13,82 @@ typedef enum ButtonComboState { } ButtonComboState; typedef struct ConfigItemButtonCombo { - char *configId; + const char *identifier; WUPSConfigItemHandle handle; uint32_t defaultValue; uint32_t value; + uint32_t valueAtCreation; uint32_t holdDurationInMs; VPADButtons abortButton; uint32_t abortButtonHoldDurationInMs; - void *callback; ButtonComboState state; + void *valueChangedCallback; } ConfigItemButtonCombo; typedef void (*ButtonComboValueChangedCallback)(ConfigItemButtonCombo *item, uint32_t buttonComboInVPADButtons); -bool WUPSConfigItemButtonComboAddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, uint32_t defaultComboInVPADButtons, ButtonComboValueChangedCallback callback); +extern "C" WUPSConfigAPIStatus +WUPSConfigItemButtonCombo_CreateEx(const char *identifier, + const char *displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback, + WUPSConfigItemHandle *outHandle); -bool WUPSConfigItemButtonComboAddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, uint32_t defaultComboInVPADButtons, uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, ButtonComboValueChangedCallback callback); +bool WUPSConfigItemButtonComboAddToCategory(WUPSConfigCategoryHandle cat, + const char *displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + ButtonComboValueChangedCallback callback); -#define WUPSConfigItemButtonCombo_AddToCategoryHandled(__config__, __cat__, __configID__, __displayName__, __defaultComboInVPADButtons__, __callback__) \ - do { \ - if (!WUPSConfigItemButtonComboAddToCategory(__cat__, __configID__, __displayName__, __defaultComboInVPADButtons__, __callback__)) { \ - WUPSConfig_Destroy(__config__); \ - return 0; \ - } \ - } while (0) +bool WUPSConfigItemButtonComboAddToCategoryEx(WUPSConfigCategoryHandle cat, + const char *displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, + VPADButtons abortButton, + uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback); #ifdef __cplusplus } +#endif + + +#ifdef __cplusplus + +#include +#include +#include +#include +#include + +class WUPSConfigItemButtonCombo : public WUPSConfigItem { +public: + static std::optional CreateEx(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback, + WUPSConfigAPIStatus &err) noexcept; + + static WUPSConfigItemButtonCombo CreateEx(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback); + + static std::optional Create(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + ButtonComboValueChangedCallback callback, + WUPSConfigAPIStatus &err) noexcept; + + static WUPSConfigItemButtonCombo Create(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + ButtonComboValueChangedCallback callback); + +private: + explicit WUPSConfigItemButtonCombo(WUPSConfigItemHandle itemHandle) : WUPSConfigItem(itemHandle) { + } +}; #endif \ No newline at end of file diff --git a/src/utils/WUPSConfigItemButtonComboCpp.cpp b/src/utils/WUPSConfigItemButtonComboCpp.cpp new file mode 100644 index 0000000..35e1de1 --- /dev/null +++ b/src/utils/WUPSConfigItemButtonComboCpp.cpp @@ -0,0 +1,52 @@ +#include "WUPSConfigItemButtonCombo.h" + +std::optional WUPSConfigItemButtonCombo::CreateEx(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback, + WUPSConfigAPIStatus &err) noexcept { + WUPSConfigItemHandle itemHandle; + if ((err = WUPSConfigItemButtonCombo_CreateEx(identifier ? identifier->data() : nullptr, + displayName.data(), + defaultComboInVPADButtons, currentComboInVPADButtons, + holdDurationInMs, abortButton, abortButtonHoldDurationInMs, + callback, + &itemHandle)) != WUPSCONFIG_API_RESULT_SUCCESS) { + return std::nullopt; + } + return WUPSConfigItemButtonCombo(itemHandle); +} + +WUPSConfigItemButtonCombo WUPSConfigItemButtonCombo::CreateEx(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + uint32_t holdDurationInMs, VPADButtons abortButton, uint32_t abortButtonHoldDurationInMs, + ButtonComboValueChangedCallback callback) { + WUPSConfigAPIStatus err; + auto result = CreateEx(std::move(identifier), displayName, defaultComboInVPADButtons, currentComboInVPADButtons, holdDurationInMs, abortButton, abortButtonHoldDurationInMs, callback, err); + if (!result) { + throw std::runtime_error(std::string("Failed to create WUPSConfigItemButtonCombo: ").append(WUPSConfigAPI_GetStatusStr(err))); + } + return std::move(*result); +} + +std::optional WUPSConfigItemButtonCombo::Create(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + ButtonComboValueChangedCallback callback, + WUPSConfigAPIStatus &err) noexcept { + return CreateEx(std::move(identifier), displayName, defaultComboInVPADButtons, currentComboInVPADButtons, 2000, VPAD_BUTTON_B, 250, callback, err); +} + +WUPSConfigItemButtonCombo WUPSConfigItemButtonCombo::Create(std::optional identifier, + std::string_view displayName, + uint32_t defaultComboInVPADButtons, uint32_t currentComboInVPADButtons, + ButtonComboValueChangedCallback callback) { + WUPSConfigAPIStatus err = WUPSCONFIG_API_RESULT_UNKNOWN_ERROR; + auto res = Create(std::move(identifier), displayName, defaultComboInVPADButtons, currentComboInVPADButtons, callback, err); + if (!res) { + throw std::runtime_error(std::string("Failed to create WUPSConfigItemButtonCombo: ").append(WUPSConfigAPI_GetStatusStr(err))); + } + return std::move(*res); +} diff --git a/src/utils/config.cpp b/src/utils/config.cpp index a80e066..4b1c059 100644 --- a/src/utils/config.cpp +++ b/src/utils/config.cpp @@ -3,102 +3,126 @@ #include "retain_vars.hpp" #include #include + extern void UpdateAudioMode(); void boolItemChangedConfig(ConfigItemBoolean *item, bool newValue) { - if (std::string_view(item->configId) == ENABLED_CONFIG_STRING) { + if (std::string_view(ENABLED_CONFIG_STRING) == item->identifier) { DEBUG_FUNCTION_LINE("New value in %s: %d", ENABLED_CONFIG_STRING, newValue); gEnabled = newValue; - WUPS_StoreInt(nullptr, ENABLED_CONFIG_STRING, gEnabled); + WUPSStorageAPI::Store(ENABLED_CONFIG_STRING, gEnabled); UpdateAudioMode(); return; } - PROCESS_BOOL_ITEM_CHANGED(SWAP_SCREENS_CONFIG_STRING, gDoScreenSwap); - PROCESS_BOOL_ITEM_CHANGED(ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING, gSwapScreenButtonComboEnabled); - PROCESS_BOOL_ITEM_CHANGED(ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING, gChangeAudioModeButtonComboEnabled); - PROCESS_BOOL_ITEM_CHANGED(ENABLE_NOTIFICATIONS_CONFIG_STRING, gShowNotifications); + + if (std::string_view(SWAP_SCREENS_CONFIG_STRING) == item->identifier) { + gDoScreenSwap = newValue; + WUPSStorageAPI::Store(item->identifier, gDoScreenSwap); + } else if (std::string_view(ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING) == item->identifier) { + gSwapScreenButtonComboEnabled = newValue; + WUPSStorageAPI::Store(item->identifier, gSwapScreenButtonComboEnabled); + } else if (std::string_view(ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING) == item->identifier) { + gChangeAudioModeButtonComboEnabled = newValue; + WUPSStorageAPI::Store(item->identifier, gChangeAudioModeButtonComboEnabled); + } else if (std::string_view(ENABLE_NOTIFICATIONS_CONFIG_STRING) == item->identifier) { + gShowNotifications = newValue; + WUPSStorageAPI::Store(item->identifier, gShowNotifications); + } else { + DEBUG_FUNCTION_LINE_ERR("Unexpected boolean item: %s", item->identifier); + } } void buttonComboItemChanged(ConfigItemButtonCombo *item, uint32_t newValue) { - if (item && item->configId) { - if (std::string_view(item->configId) == SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING) { + if (item && item->identifier) { + if (std::string_view(SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING) == item->identifier) { gSwapScreenButtonCombo = newValue; - WUPS_StoreInt(nullptr, item->configId, (int32_t) gSwapScreenButtonCombo); - } else if (std::string_view(item->configId) == CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING) { + WUPSStorageAPI::Store(item->identifier, (int32_t) gSwapScreenButtonCombo); + } else if (std::string_view(CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING) == item->identifier) { gSwapAudioButtonCombo = newValue; - WUPS_StoreInt(nullptr, item->configId, (int32_t) gSwapAudioButtonCombo); + WUPSStorageAPI::Store(item->identifier, (int32_t) gSwapAudioButtonCombo); } } } -void getConfigInfoForMap(std::map &curMap, ConfigItemMultipleValuesPair *pair, uint32_t default_lang, uint32_t *default_index, uint32_t *len) { - uint32_t i = 0; - for (auto &curEntry : curMap) { - if (default_lang == curEntry.first) { - *default_index = i; - } - pair[i].value = curEntry.first; - pair[i].valueName = (char *) curEntry.second; - i++; - } - *len = i; -} +void default_audio_mode_changed(ConfigItemMultipleValues *item, uint32_t newValue) { + DEBUG_FUNCTION_LINE("New value in %s changed: %d", item->identifier, newValue); -void default_lang_changed(ConfigItemMultipleValues *item, uint32_t newValue) { - DEBUG_FUNCTION_LINE("New value in %s changed: %d", item->configId, newValue); - - if (strcmp(item->configId, SCREEN_MODE_CONFIG_STRING) == 0) { + if (std::string_view(AUDIO_MODE_CONFIG_STRING) == item->identifier) { gCurAudioMode = static_cast(newValue); - WUPS_StoreInt(nullptr, item->configId, (int32_t) gCurAudioMode); + WUPSStorageAPI::Store(item->identifier, (int32_t) gCurAudioMode); } } -WUPS_GET_CONFIG() { - // We open the storage, so we can persist the configuration the user did. - if (WUPS_OpenStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to open storage"); - return 0; + +WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle) { + try { + WUPSConfigCategory root = WUPSConfigCategory(rootHandle); + + auto settings = WUPSConfigCategory::Create("Settings"); + + + settings.add(WUPSConfigItemBoolean::Create(ENABLED_CONFIG_STRING, + "Plugin enabled", + DEFAULT_ENABLED_CONFIG_VALUE, gEnabled, + &boolItemChangedConfig)); + settings.add(WUPSConfigItemBoolean::Create(ENABLE_NOTIFICATIONS_CONFIG_STRING, + "Show notifications", + DEFAULT_ENABLE_NOTIFICATIONS_CONFIG_VALUE, gShowNotifications, + &boolItemChangedConfig)); + settings.add(WUPSConfigItemBoolean::Create(SWAP_SCREENS_CONFIG_STRING, + "Swap screens", + DEFAULT_SWAP_SCREENS_CONFIG_VALUE, gDoScreenSwap, + &boolItemChangedConfig)); + + constexpr WUPSConfigItemMultipleValues::ValuePair audioModeMap[] = { + {AUDIO_MODE_NONE, "Normal"}, + {AUDIO_MODE_SWAP, "Swap TV and GamePad sound"}, + {AUDIO_MODE_MATCH_SCREEN, "Sound matches screen"}, + {AUDIO_MODE_COMBINE, "Combine TV and GamePad sound"}, + {AUDIO_MODE_LEFT_TV_RIGHT_DRC, "Left: TV; Right: GamePad"}}; + + settings.add(WUPSConfigItemMultipleValues::CreateFromValue(AUDIO_MODE_CONFIG_STRING, + "Audio mode:", + DEFAULT_AUDIO_MODE_CONFIG_VALUE, gCurAudioMode, + audioModeMap, + &default_audio_mode_changed)); + + root.add(std::move(settings)); + + auto buttonCombos = WUPSConfigCategory::Create("Button Combos"); + + buttonCombos.add(WUPSConfigItemBoolean::Create(ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING, + "Enable swap screen button combo", + DEFAULT_ENABLED_SWAP_SCREENS_COMBO_CONFIG_VALUE, gSwapScreenButtonComboEnabled, + &boolItemChangedConfig)); + + buttonCombos.add(WUPSConfigItemButtonCombo::Create(SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING, + "Swap screen", + DEFAULT_SWAP_SCREEN_BUTTON_COMBO_CONFIG_VALUE, gSwapScreenButtonCombo, + &buttonComboItemChanged)); + + buttonCombos.add(WUPSConfigItemBoolean::Create(ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING, + "Enable change audio mode button combo", + DEFAULT_ENABLED_CHANGE_AUDIO_COMBO_CONFIG_VALUE, gChangeAudioModeButtonComboEnabled, + &boolItemChangedConfig)); + + buttonCombos.add(WUPSConfigItemButtonCombo::Create(CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING, + "Change audio", + DEFAULT_CHANGE_AUDIO_BUTTON_COMBO_CONFIG_VALUE, gSwapAudioButtonCombo, + &buttonComboItemChanged)); + root.add(std::move(buttonCombos)); + } catch (std::exception &e) { + OSReport("Exception T_T : %s\n", e.what()); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; } - WUPSConfigHandle config; - WUPSConfig_CreateHandled(&config, "SwipSwapMe"); - - WUPSConfigCategoryHandle setting; - WUPSConfig_AddCategoryByNameHandled(config, "Settings", &setting); - WUPSConfigCategoryHandle combos; - WUPSConfig_AddCategoryByNameHandled(config, "Button combos", &combos); - - std::map audioModeMap{ - {AUDIO_MODE_NONE, "Normal"}, - {AUDIO_MODE_SWAP, "Swap TV and GamePad sound"}, - {AUDIO_MODE_MATCH_SCREEN, "Sound matches screen"}, - {AUDIO_MODE_COMBINE, "Combine TV and GamePad sound"}, - {AUDIO_MODE_LEFT_TV_RIGHT_DRC, "Left: TV; Right: GamePad"}}; - - ConfigItemMultipleValuesPair audioModePairs[audioModeMap.size()]; - - uint32_t number_values = 0; - uint32_t default_index = 0; - getConfigInfoForMap(audioModeMap, audioModePairs, gCurAudioMode, &default_index, &number_values); - - WUPSConfigItemBoolean_AddToCategoryHandled(config, setting, ENABLED_CONFIG_STRING, "Plugin enabled", gEnabled, &boolItemChangedConfig); - WUPSConfigItemBoolean_AddToCategoryHandled(config, setting, ENABLE_NOTIFICATIONS_CONFIG_STRING, "Show notifications", gShowNotifications, &boolItemChangedConfig); - WUPSConfigItemBoolean_AddToCategoryHandled(config, setting, SWAP_SCREENS_CONFIG_STRING, "Swap screens", gDoScreenSwap, &boolItemChangedConfig); - WUPSConfigItemMultipleValues_AddToCategoryHandled(config, setting, SCREEN_MODE_CONFIG_STRING, "Audio mode:", default_index, audioModePairs, number_values, - &default_lang_changed); - - WUPSConfigItemBoolean_AddToCategoryHandled(config, combos, ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING, "Enable swap screen button combo", gSwapScreenButtonComboEnabled, &boolItemChangedConfig); - WUPSConfigItemButtonCombo_AddToCategoryHandled(config, combos, SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING, "Swap screen", gSwapScreenButtonCombo, &buttonComboItemChanged); - WUPSConfigItemBoolean_AddToCategoryHandled(config, combos, ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING, "Enable change audio mode button combo", gChangeAudioModeButtonComboEnabled, &boolItemChangedConfig); - WUPSConfigItemButtonCombo_AddToCategoryHandled(config, combos, CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING, "Change audio", gSwapAudioButtonCombo, &buttonComboItemChanged); - - return config; + return WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS; } -WUPS_CONFIG_CLOSED() { +void ConfigMenuClosedCallback() { // Save all changes - if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) { + if (WUPSStorageAPI::SaveStorage() != WUPS_STORAGE_ERROR_SUCCESS) { DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); } } \ No newline at end of file diff --git a/src/utils/config.h b/src/utils/config.h index 13851df..2eb6c17 100644 --- a/src/utils/config.h +++ b/src/utils/config.h @@ -1,53 +1,29 @@ #pragma once #include "logger.h" +#include "retain_vars.hpp" #include +#include #include #include -#define ENABLED_CONFIG_STRING "enabled" -#define SWAP_SCREENS_CONFIG_STRING "swapScreens" -#define ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING "swapScreensComboEnabled" -#define ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING "changeAudioComboEnabled" -#define ENABLE_NOTIFICATIONS_CONFIG_STRING "notificationsEnabled" -#define SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING "screenButtonCombo" -#define CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING "audioButtonCombo" -#define SCREEN_MODE_CONFIG_STRING "audioMode" +#define DEFAULT_ENABLED_CONFIG_VALUE true +#define DEFAULT_SWAP_SCREENS_CONFIG_VALUE false +#define DEFAULT_ENABLED_SWAP_SCREENS_COMBO_CONFIG_VALUE true +#define DEFAULT_ENABLED_CHANGE_AUDIO_COMBO_CONFIG_VALUE false +#define DEFAULT_ENABLE_NOTIFICATIONS_CONFIG_VALUE true +#define DEFAULT_SWAP_SCREEN_BUTTON_COMBO_CONFIG_VALUE VPAD_BUTTON_TV +#define DEFAULT_CHANGE_AUDIO_BUTTON_COMBO_CONFIG_VALUE VPAD_BUTTON_STICK_L +#define DEFAULT_AUDIO_MODE_CONFIG_VALUE AUDIO_MODE_MATCH_SCREEN -#define LOAD_BOOL_FROM_STORAGE(config_name, __variable__) \ - if ((storageRes = WUPS_GetBool(nullptr, config_name, &__variable__)) == WUPS_STORAGE_ERROR_NOT_FOUND) { \ - if (WUPS_StoreBool(nullptr, config_name, __variable__) != WUPS_STORAGE_ERROR_SUCCESS) { \ - DEBUG_FUNCTION_LINE_WARN("Failed to store bool"); \ - } \ - } else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { \ - DEBUG_FUNCTION_LINE_WARN("Failed to get bool %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); \ - } \ - while (0) +#define ENABLED_CONFIG_STRING "enabled" +#define SWAP_SCREENS_CONFIG_STRING "swapScreens" +#define ENABLED_SWAP_SCREENS_COMBO_CONFIG_STRING "swapScreensComboEnabled" +#define ENABLED_CHANGE_AUDIO_COMBO_CONFIG_STRING "changeAudioComboEnabled" +#define ENABLE_NOTIFICATIONS_CONFIG_STRING "notificationsEnabled" +#define SWAP_SCREEN_BUTTON_COMBO_CONFIG_STRING "screenButtonCombo" +#define CHANGE_AUDIO_BUTTON_COMBO_CONFIG_STRING "audioButtonCombo" +#define AUDIO_MODE_CONFIG_STRING "audioMode" -#define LOAD_STRING_FROM_STORAGE(config_name, __string, __string_length) \ - if ((storageRes = WUPS_GetString(nullptr, config_name, __string, __string_length)) == WUPS_STORAGE_ERROR_NOT_FOUND) { \ - if (WUPS_StoreString(nullptr, config_name, __string) != WUPS_STORAGE_ERROR_SUCCESS) { \ - DEBUG_FUNCTION_LINE_WARN("Failed to store string"); \ - } \ - } else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { \ - DEBUG_FUNCTION_LINE_WARN("Failed to get bool %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); \ - } \ - while (0) +WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle rootHandle); -#define LOAD_INT_FROM_STORAGE(config_name, __intValue) \ - if ((storageRes = WUPS_GetInt(nullptr, config_name, (int32_t *) &__intValue)) == WUPS_STORAGE_ERROR_NOT_FOUND) { \ - if (WUPS_StoreInt(nullptr, config_name, (int32_t) __intValue) != WUPS_STORAGE_ERROR_SUCCESS) { \ - DEBUG_FUNCTION_LINE_WARN("Failed to store int"); \ - } \ - } else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { \ - DEBUG_FUNCTION_LINE_WARN("Failed to get int %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); \ - } \ - while (0) - -#define PROCESS_BOOL_ITEM_CHANGED(__config__name, __variable__) \ - if (std::string_view(item->configId) == __config__name) { \ - DEBUG_FUNCTION_LINE_ERR("New value in %s: %d", __config__name, newValue); \ - __variable__ = newValue; \ - WUPS_StoreInt(nullptr, __config__name, __variable__); \ - return; \ - } \ - while (0) +void ConfigMenuClosedCallback(); \ No newline at end of file