diff options
Diffstat (limited to 'src/c')
| -rw-r--r-- | src/c/config.c | 79 | ||||
| -rw-r--r-- | src/c/config.h | 15 | ||||
| -rw-r--r-- | src/c/event.c | 6 | ||||
| -rw-r--r-- | src/c/gw2et.c | 10 |
4 files changed, 106 insertions, 4 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; +} diff --git a/src/c/config.h b/src/c/config.h new file mode 100644 index 0000000..fcd21fb --- /dev/null +++ b/src/c/config.h @@ -0,0 +1,15 @@ +/* + * Copyright (C) 2026 Reiner Herrmann + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include <pebble.h> + +void config_init(); +void config_deinit(); +uint32_t config_get_enabled_extensions(); + +#endif diff --git a/src/c/event.c b/src/c/event.c index 36b0529..59fdb98 100644 --- a/src/c/event.c +++ b/src/c/event.c @@ -4,6 +4,7 @@ */ #include "event.h" +#include "config.h" #define TIME2MIN(hh,mm) (hh * 60 + mm) @@ -589,9 +590,8 @@ static struct Gw2Event *all_events = NULL; static int all_events_count = 0; static int event_index = 0; -static inline uint16_t enabled_categories() { - /* TODO: make this mask configurable */ - return 0xffff; +static inline uint32_t enabled_categories() { + return config_get_enabled_extensions(); } static inline bool is_category_enabled(enum Category category) { diff --git a/src/c/gw2et.c b/src/c/gw2et.c index e591868..42e67df 100644 --- a/src/c/gw2et.c +++ b/src/c/gw2et.c @@ -4,6 +4,7 @@ */ #include <pebble.h> +#include "config.h" #include "event.h" #define DEFAULT_NUM_EVENTS 30 @@ -101,12 +102,18 @@ static void main_window_unload(Window *window) { menu_layer_destroy(main_menu); } +void reload_data() { + menu_layer_reload_data(main_menu); +} + static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { update_event_index(); - menu_layer_reload_data(main_menu); + reload_data(); } static void init() { + config_init(); + main_window = window_create(); window_set_window_handlers(main_window, (WindowHandlers) { .load = main_window_load, @@ -119,6 +126,7 @@ static void init() { } static void deinit() { + config_deinit(); tick_timer_service_unsubscribe(); window_destroy(main_window); cleanup_events(); |
