From 4d9f28c1ae27453ea335e54fb6393a1bd288c42e Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 2 Dec 2023 21:20:23 +0100 Subject: [PATCH] example_plugin: Update to use the new config api --- plugins/example_plugin/Makefile | 2 +- .../example_plugin/src/{main.cpp => main.c} | 154 +++++++++++++----- 2 files changed, 117 insertions(+), 39 deletions(-) rename plugins/example_plugin/src/{main.cpp => main.c} (52%) diff --git a/plugins/example_plugin/Makefile b/plugins/example_plugin/Makefile index 565db8d..c84c3ef 100644 --- a/plugins/example_plugin/Makefile +++ b/plugins/example_plugin/Makefile @@ -96,7 +96,7 @@ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) all: $(BUILD) $(BUILD): - @[ -d $@ ] || mkdir -p $@ + @$(shell [ ! -d $(BUILD) ] && mkdir -p $(BUILD)) @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile #------------------------------------------------------------------------------- diff --git a/plugins/example_plugin/src/main.cpp b/plugins/example_plugin/src/main.c similarity index 52% rename from plugins/example_plugin/src/main.cpp rename to plugins/example_plugin/src/main.c index 62c7c12..2a62259 100644 --- a/plugins/example_plugin/src/main.cpp +++ b/plugins/example_plugin/src/main.c @@ -1,9 +1,10 @@ #include "utils/logger.h" #include #include -#include +#include #include #include +#include /** Mandatory plugin information. @@ -28,6 +29,113 @@ WUPS_USE_STORAGE("example_plugin"); // Unique id for the storage api bool logFSOpen = true; +/** + * Callback that will be called if the config has been changed + */ +void logFSOpenChanged(ConfigItemBoolean *item, bool newValue) { + DEBUG_FUNCTION_LINE_INFO("New value in logFSOpenChanged: %d", newValue); + logFSOpen = newValue; + // If the value has changed, we store it in the storage. + WUPS_StoreInt(NULL, LOG_FS_OPEN_CONFIG_ID, logFSOpen); +} + +WUPSConfigAPICallbackStatus ConfigMenuOpenedCallback(WUPSConfigCategoryHandle root) { + // We open the storage, so we can persist the configuration the user did. + if (WUPS_OpenStorage() != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE("Failed to open storage"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + + { + // Let's create a new category called "Settings" + WUPSConfigCategoryHandle settingsCategory; + WUPSConfigAPICreateCategoryOptionsV1 settingsCategoryOptions = {.name = "Settings"}; + if (WUPSConfigAPI_Category_Create(settingsCategoryOptions, &settingsCategory) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to create settings category"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + + // Add a new item to this settings category + if (!WUPSConfigItemBoolean_AddToCategory(settingsCategory, LOG_FS_OPEN_CONFIG_ID, "Log FSOpen calls", logFSOpen, &logFSOpenChanged)) { + DEBUG_FUNCTION_LINE_ERR("Failed to add item to category"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + + // Add the category to the root. + if (WUPSConfigAPI_Category_AddCategory(root, settingsCategory) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to add category to root item"); + } + } + + { + // We can also have categories inside categories! + WUPSConfigCategoryHandle categoryLevel1; + WUPSConfigAPICreateCategoryOptionsV1 catLev1Options = {.name = "Category with subcategory"}; + if (WUPSConfigAPI_Category_Create(catLev1Options, &categoryLevel1) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to create categoryLevel1"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + WUPSConfigCategoryHandle categoryLevel2; + WUPSConfigAPICreateCategoryOptionsV1 catLev2Options = {.name = "Category inside category"}; + if (WUPSConfigAPI_Category_Create(catLev2Options, &categoryLevel2) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to create categoryLevel1"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + if (!WUPSConfigItemBoolean_AddToCategory(categoryLevel2, "stubInsideCategory", "This is stub item inside a nested category", false, NULL)) { + DEBUG_FUNCTION_LINE_ERR("Failed to add stub item to root category"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + + // add categoryLevel2 to categoryLevel1 + if (WUPSConfigAPI_Category_AddCategory(categoryLevel1, categoryLevel2) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to add category to root item"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + + // add categoryLevel2 to categoryLevel1 + if (WUPSConfigAPI_Category_AddCategory(root, categoryLevel1) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to add category to root item"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + } + + { + // We can also directly add items to the root category + if (!WUPSConfigItemBoolean_AddToCategory(root, "stub0", "This is stub item without category", false, NULL)) { + DEBUG_FUNCTION_LINE_ERR("Failed to add stub item to root category"); + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + ConfigItemMultipleValuesPair values[10]; + int numOfElements = sizeof(values) / sizeof(values[0]); + for (int i = 0; i < numOfElements; i++) { +#define STR_SIZE 10 + char *str = (char *) malloc(STR_SIZE); + if (!str) { + OSFatal("Failed to allocate memory"); + } + snprintf(str, STR_SIZE, "%d", i); + values[i].value = i; + values[i].valueName = str; + } + bool multValuesRes = WUPSConfigItemMultipleValues_AddToCategory(root, "multival", "Multiple values", 0, values, numOfElements, NULL); + for (int i = 0; i < sizeof(values) / sizeof(values[0]); i++) { + free(values[i].valueName); + } + if (!multValuesRes) { + return WUPSCONFIG_API_CALLBACK_RESULT_ERROR; + } + } + + return WUPSCONFIG_API_CALLBACK_RESULT_SUCCESS; +} + +void ConfigMenuClosedCallback() { + // Save all changes + if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); + } +} + /** Gets called ONCE when the plugin was loaded. **/ @@ -36,15 +144,20 @@ INITIALIZE_PLUGIN() { initLogging(); DEBUG_FUNCTION_LINE("INITIALIZE_PLUGIN of example_plugin!"); + WUPSConfigAPIOptionsV1 configOptions = {.name = "example_plugin"}; + if (WUPSConfigAPI_Init(configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) != WUPSCONFIG_API_RESULT_SUCCESS) { + DEBUG_FUNCTION_LINE_ERR("Failed to init config api"); + } + // Open storage to read values WUPSStorageError storageRes = WUPS_OpenStorage(); if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { DEBUG_FUNCTION_LINE("Failed to open storage %s (%d)", WUPS_GetStorageStatusStr(storageRes), storageRes); } else { // Try to get value from storage - if ((storageRes = WUPS_GetBool(nullptr, LOG_FS_OPEN_CONFIG_ID, &logFSOpen)) == WUPS_STORAGE_ERROR_NOT_FOUND) { + if ((storageRes = WUPS_GetBool(NULL, LOG_FS_OPEN_CONFIG_ID, &logFSOpen)) == WUPS_STORAGE_ERROR_NOT_FOUND) { // Add the value to the storage if it's missing. - if (WUPS_StoreBool(nullptr, LOG_FS_OPEN_CONFIG_ID, logFSOpen) != WUPS_STORAGE_ERROR_SUCCESS) { + if (WUPS_StoreBool(NULL, LOG_FS_OPEN_CONFIG_ID, logFSOpen) != WUPS_STORAGE_ERROR_SUCCESS) { DEBUG_FUNCTION_LINE("Failed to store bool"); } } else if (storageRes != WUPS_STORAGE_ERROR_SUCCESS) { @@ -89,41 +202,6 @@ ON_APPLICATION_REQUESTS_EXIT() { DEBUG_FUNCTION_LINE_INFO("ON_APPLICATION_REQUESTS_EXIT of example_plugin!"); } -/** - * Callback that will be called if the config has been changed - */ -void logFSOpenChanged(ConfigItemBoolean *item, bool newValue) { - DEBUG_FUNCTION_LINE_INFO("New value in logFSOpenChanged: %d", newValue); - logFSOpen = newValue; - // If the value has changed, we store it in the storage. - WUPS_StoreInt(nullptr, LOG_FS_OPEN_CONFIG_ID, logFSOpen); -} - -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("Failed to open storage"); - return 0; - } - - WUPSConfigHandle config; - WUPSConfig_CreateHandled(&config, "Example Plugin"); - - WUPSConfigCategoryHandle cat; - WUPSConfig_AddCategoryByNameHandled(config, "Logging", &cat); - - WUPSConfigItemBoolean_AddToCategoryHandled(config, cat, LOG_FS_OPEN_CONFIG_ID, "Log FSOpen calls", logFSOpen, &logFSOpenChanged); - - return config; -} - -WUPS_CONFIG_CLOSED() { - // Save all changes - if (WUPS_CloseStorage() != WUPS_STORAGE_ERROR_SUCCESS) { - DEBUG_FUNCTION_LINE_ERR("Failed to close storage"); - } -} - /** This defines a function replacement. It allows to replace the system function with an own function.