summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick McDermott <pj@pehjota.net>2017-10-04 17:41:12 (EDT)
committer Patrick McDermott <pj@pehjota.net>2017-10-04 17:41:12 (EDT)
commit891435f53bf3b8f52e1161fc986f127d3343bf62 (patch)
treeedf96be7337a12688a3b3da48aa847b9d6bec3b1
parentf392135bc44e66558fe19c3418964b7e3c0820b2 (diff)
downloadmarquee-891435f53bf3b8f52e1161fc986f127d3343bf62.zip
marquee-891435f53bf3b8f52e1161fc986f127d3343bf62.tar.gz
marquee-891435f53bf3b8f52e1161fc986f127d3343bf62.tar.bz2
src/config.c: Run callbacks on value setting
And just use "enum", "union", and "struct" keywords and namespaces instead of defining types. Otherwise, at least one of these types would have to be forward declared.
-rw-r--r--src/config.c43
1 files changed, 33 insertions, 10 deletions
diff --git a/src/config.c b/src/config.c
index 701dc35..97f8429 100644
--- a/src/config.c
+++ b/src/config.c
@@ -26,14 +26,24 @@
#include "config.h"
-typedef enum {
+enum type {
TYPE_BOOLEAN,
-} Type;
+};
-typedef struct {
- Type type;
- GList *callbacks;
-} ConfigItem;
+union callback {
+ void (*boolean_cb)(const gchar *, gboolean, gpointer);
+};
+
+struct callbacks {
+ union callback cb;
+ gpointer user_data;
+ struct callbacks *next;
+};
+
+struct item {
+ enum type type;
+ struct callbacks *callbacks;
+};
static void
split_name(const gchar *name, gchar **group, gchar **key)
@@ -45,9 +55,11 @@ split_name(const gchar *name, gchar **group, gchar **key)
}
static void
-add_type_and_callbacks(MqConfig *config, const gchar *name, Type type)
+set_type_or_run_callbacks(MqConfig *config, const gchar *name, gpointer value,
+ enum type type)
{
- ConfigItem *item;
+ struct item *item;
+ struct callbacks *cbs;
if (!config->types_and_cbs_set) {
item = g_malloc(sizeof(*item));
@@ -55,6 +67,17 @@ add_type_and_callbacks(MqConfig *config, const gchar *name, Type type)
item->callbacks = NULL;
g_hash_table_insert(config->types_and_cbs, g_strdup(name),
item);
+ } else {
+ item = g_hash_table_lookup(config->types_and_cbs, name);
+ for (cbs = item->callbacks; cbs; cbs = cbs->next) {
+ switch (item->type) {
+ case TYPE_BOOLEAN:
+ cbs->cb.boolean_cb(name,
+ *((gboolean *) value),
+ cbs->user_data);
+ break;
+ }
+ }
}
}
@@ -68,7 +91,7 @@ set_boolean(MqConfig *config, const gchar *name, gboolean value)
g_key_file_set_boolean(config->key_file, group, name, value);
- add_type_and_callbacks(config, name, TYPE_BOOLEAN);
+ set_type_or_run_callbacks(config, name, &value, TYPE_BOOLEAN);
}
static void
@@ -115,7 +138,7 @@ mq_config_save(MqConfig *config)
void
mq_config_set(MqConfig *config, const gchar *name, const gchar *value)
{
- ConfigItem *item;
+ struct item *item;
item = g_hash_table_lookup(config->types_and_cbs, name);
g_assert(item);