2022-02-03 16:24:36 +01:00
|
|
|
#include "wups/config/WUPSConfigItemBoolean.h"
|
2023-12-14 17:20:38 +01:00
|
|
|
#include "wups/config_api.h"
|
2021-09-24 19:57:15 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2022-09-24 17:41:20 +02:00
|
|
|
#include <cstring>
|
2023-12-16 17:10:45 +01:00
|
|
|
#include <wups/config.h>
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemBoolean_onCloseCallback(void *context) {
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
2023-12-16 17:10:45 +01:00
|
|
|
if (item->valueAtCreation != item->value && item->valueChangedCallback != nullptr) {
|
|
|
|
((BooleanValueChangedCallback) (item->valueChangedCallback))(item, item->value);
|
|
|
|
}
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static inline void toggleValue(ConfigItemBoolean *item) {
|
2021-09-24 19:57:15 +02:00
|
|
|
item->value = !item->value;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemBoolean_onInput(void *context, WUPSConfigSimplePadData input) {
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
2023-12-16 17:10:45 +01:00
|
|
|
if ((input.buttons_d & WUPS_CONFIG_BUTTON_A) == WUPS_CONFIG_BUTTON_A) {
|
2021-09-24 19:57:15 +02:00
|
|
|
toggleValue(item);
|
2023-12-16 17:10:45 +01:00
|
|
|
} else if (input.buttons_d & WUPS_CONFIG_BUTTON_LEFT && !item->value) {
|
2021-09-24 19:57:15 +02:00
|
|
|
toggleValue(item);
|
2023-12-16 17:10:45 +01:00
|
|
|
} else if ((input.buttons_d & WUPS_CONFIG_BUTTON_RIGHT) && item->value) {
|
2021-09-24 19:57:15 +02:00
|
|
|
toggleValue(item);
|
|
|
|
}
|
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
static int32_t WUPSConfigItemBoolean_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
|
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
|
|
|
snprintf(out_buf, out_size, " %s", item->value ? item->trueValue : item->falseValue);
|
|
|
|
return 0;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static int32_t WUPSConfigItemBoolean_getCurrentValueSelectedDisplay(void *context, char *out_buf, int32_t out_size) {
|
2021-09-24 19:57:15 +02:00
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
|
|
|
if (item->value) {
|
|
|
|
snprintf(out_buf, out_size, " %s >", item->trueValue);
|
|
|
|
} else {
|
|
|
|
snprintf(out_buf, out_size, "< %s ", item->falseValue);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemBoolean_restoreDefault(void *context) {
|
2022-02-03 16:24:36 +01:00
|
|
|
auto *item = (ConfigItemBoolean *) context;
|
2021-09-24 19:57:15 +02:00
|
|
|
item->value = item->defaultValue;
|
|
|
|
}
|
|
|
|
|
2023-11-17 13:50:27 +01:00
|
|
|
static void WUPSConfigItemBoolean_Cleanup(ConfigItemBoolean *item) {
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
free((void *) item->identifier);
|
2023-11-17 13:50:27 +01:00
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
static void WUPSConfigItemBoolean_onDelete(void *context) {
|
2023-11-17 13:50:27 +01:00
|
|
|
WUPSConfigItemBoolean_Cleanup((ConfigItemBoolean *) context);
|
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
extern "C" WUPSConfigAPIStatus
|
|
|
|
WUPSConfigItemBoolean_CreateEx(const char *identifier,
|
|
|
|
const char *displayName,
|
|
|
|
bool defaultValue,
|
|
|
|
bool currentValue,
|
|
|
|
BooleanValueChangedCallback callback,
|
|
|
|
const char *trueValue,
|
|
|
|
const char *falseValue,
|
|
|
|
WUPSConfigItemHandle *outHandle) {
|
|
|
|
if (outHandle == nullptr) {
|
|
|
|
return WUPSCONFIG_API_RESULT_INVALID_ARGUMENT;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
auto *item = (ConfigItemBoolean *) malloc(sizeof(ConfigItemBoolean));
|
|
|
|
if (item == nullptr) {
|
2023-12-16 17:10:45 +01:00
|
|
|
return WUPSCONFIG_API_RESULT_OUT_OF_MEMORY;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
if (identifier != nullptr) {
|
|
|
|
item->identifier = strdup(identifier);
|
2022-09-24 17:41:20 +02:00
|
|
|
} else {
|
2023-12-16 17:10:45 +01:00
|
|
|
item->identifier = nullptr;
|
2022-09-24 17:41:20 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
item->defaultValue = defaultValue;
|
|
|
|
item->value = currentValue;
|
|
|
|
item->valueAtCreation = currentValue;
|
|
|
|
item->valueChangedCallback = (void *) callback;
|
2021-09-24 19:57:15 +02:00
|
|
|
snprintf(item->trueValue, sizeof(item->trueValue), "%s", trueValue);
|
|
|
|
snprintf(item->falseValue, sizeof(item->falseValue), "%s", falseValue);
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
WUPSConfigAPIItemCallbacksV2 callbacks = {
|
2022-02-03 16:24:36 +01:00
|
|
|
.getCurrentValueDisplay = &WUPSConfigItemBoolean_getCurrentValueDisplay,
|
2021-09-24 19:57:15 +02:00
|
|
|
.getCurrentValueSelectedDisplay = &WUPSConfigItemBoolean_getCurrentValueSelectedDisplay,
|
2023-12-16 17:10:45 +01:00
|
|
|
.onSelected = nullptr,
|
2022-02-03 16:24:36 +01:00
|
|
|
.restoreDefault = &WUPSConfigItemBoolean_restoreDefault,
|
2023-12-16 17:10:45 +01:00
|
|
|
.isMovementAllowed = nullptr,
|
|
|
|
.onCloseCallback = &WUPSConfigItemBoolean_onCloseCallback,
|
|
|
|
.onInput = &WUPSConfigItemBoolean_onInput,
|
|
|
|
.onInputEx = nullptr,
|
2022-02-03 16:24:36 +01:00
|
|
|
.onDelete = &WUPSConfigItemBoolean_onDelete};
|
2021-09-24 19:57:15 +02:00
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
WUPSConfigAPIItemOptionsV2 options = {
|
2023-10-01 13:00:55 +02:00
|
|
|
.displayName = displayName,
|
|
|
|
.context = item,
|
|
|
|
.callbacks = callbacks,
|
|
|
|
};
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
WUPSConfigAPIStatus err;
|
|
|
|
if ((err = WUPSConfigAPI_Item_Create(options, &item->handle)) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
2023-11-17 13:50:27 +01:00
|
|
|
WUPSConfigItemBoolean_Cleanup(item);
|
2023-12-16 17:10:45 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outHandle = item->handle;
|
|
|
|
return WUPSCONFIG_API_RESULT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" WUPSConfigAPIStatus
|
|
|
|
WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat,
|
|
|
|
const char *identifier,
|
|
|
|
const char *displayName,
|
|
|
|
bool defaultValue,
|
|
|
|
bool currentValue,
|
|
|
|
BooleanValueChangedCallback callback,
|
|
|
|
const char *trueValue,
|
|
|
|
const char *falseValue) {
|
|
|
|
WUPSConfigItemHandle itemHandle;
|
|
|
|
WUPSConfigAPIStatus res;
|
|
|
|
if ((res = WUPSConfigItemBoolean_CreateEx(identifier,
|
|
|
|
displayName,
|
|
|
|
defaultValue, currentValue,
|
|
|
|
callback,
|
|
|
|
trueValue, falseValue,
|
|
|
|
&itemHandle)) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
|
|
|
return res;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
if ((res = WUPSConfigAPI_Category_AddItem(cat, itemHandle)) != WUPSCONFIG_API_RESULT_SUCCESS) {
|
|
|
|
|
|
|
|
WUPSConfigAPI_Item_Destroy(itemHandle);
|
|
|
|
return res;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
2023-12-16 17:10:45 +01:00
|
|
|
return WUPSCONFIG_API_RESULT_SUCCESS;
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|
|
|
|
|
2023-12-16 17:10:45 +01:00
|
|
|
extern "C" WUPSConfigAPIStatus
|
|
|
|
WUPSConfigItemBoolean_AddToCategory(WUPSConfigCategoryHandle cat,
|
|
|
|
const char *identifier,
|
|
|
|
const char *displayName,
|
|
|
|
bool defaultValue,
|
|
|
|
bool currentValue,
|
|
|
|
BooleanValueChangedCallback callback) {
|
|
|
|
return WUPSConfigItemBoolean_AddToCategoryEx(cat, identifier, displayName, defaultValue, currentValue, callback, "true", "false");
|
2021-09-24 19:57:15 +02:00
|
|
|
}
|