diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2026-06-10 20:55:46 +0200 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2026-06-10 20:57:40 +0200 |
| commit | 4d03f4370b3052949acfe7cc589d82adc427b8fb (patch) | |
| tree | 9df3898b10830472fff79da98c8717771ded41f1 /src/c/config.c | |
| parent | 338df4088001869f83de45cf628b4ec43824a499 (diff) | |
make shown extensions configurable via phone app
Diffstat (limited to 'src/c/config.c')
| -rw-r--r-- | src/c/config.c | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/src/c/config.c b/src/c/config.c new file mode 100644 index 0000000..1d3f3ac --- /dev/null +++ b/src/c/config.c @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2026 Reiner Herrmann + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "event.h" + +struct Configuration { + uint8_t version; + uint32_t enabled_extensions; +} __attribute__((__packed__)); + +#define SETTINGS_KEY 1 +#define CONFIG_VERSION 1 +#define NUM_EXTENSIONS 12 + +static struct Configuration configuration; + +extern void reload_data(); + +static void config_persist() { + persist_write_data(SETTINGS_KEY, &configuration, sizeof(configuration)); + + /* recalculate events based on updated configuration */ + init_events(); + reload_data(); +} + +static void config_load_defaults() { + configuration.version = CONFIG_VERSION; + configuration.enabled_extensions = 0xffffffff; +} + +static void config_load() { + config_load_defaults(); + + /* buffer remains unchanged if no data is persisted */ + persist_read_data(SETTINGS_KEY, &configuration, sizeof(configuration)); + + if (configuration.version != CONFIG_VERSION) { + /* unknown persisted configuration format */ + config_load_defaults(); + } +} + +static void config_inbox_received_handler(DictionaryIterator *iter, void *context) { + uint32_t old_enabled_extensions = configuration.enabled_extensions; + + for (int i=0; i<NUM_EXTENSIONS; i++) { + Tuple *enabled_extension = dict_find(iter, MESSAGE_KEY_enabled_extensions + i); + if (enabled_extension) { + bool val = enabled_extension->value->uint8; + /* clear bit */ + configuration.enabled_extensions &= ~(1 << i); + /* set value into bit */ + configuration.enabled_extensions |= (val << i); + } + } + + if (old_enabled_extensions != configuration.enabled_extensions) { + /* only persist if something changed */ + config_persist(); + } +} + +void config_init() { + config_load(); + + app_message_register_inbox_received(config_inbox_received_handler); + app_message_open(128, 128); +} + +void config_deinit() { + app_message_deregister_callbacks(); +} + +uint32_t config_get_enabled_extensions() { + return configuration.enabled_extensions; +} |
