aboutsummaryrefslogtreecommitdiff
path: root/src/c/config.c
blob: 1d3f3acaf72932daefc97100f30c4cd9f067d1c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
}