From dacdcf2516ef4520b0bb32fa34837381f65faf30 Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Mon, 12 Dec 2011 21:11:19 +0100 Subject: Implement a priority based sink order assignment This is handsome when using udev based device discovery. --- src/config.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/config.c (limited to 'src/config.c') diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..e24ece0 --- /dev/null +++ b/src/config.c @@ -0,0 +1,93 @@ +#include +#include + +#include "pa-sink-ctl.h" +#include "config.h" + +static int +parse_priorities(struct config *cfg) +{ + gchar **groups; + struct priority p; + int i; + gsize length; + GError *error = NULL; + + groups = g_key_file_get_groups(cfg->keyfile, &length); + + for (i = 0; i < length; ++i) { + if (strncmp(groups[i], "priority", 8) != 0) + continue; + + memset(&p, 0, sizeof p); + + p.match = g_key_file_get_value(cfg->keyfile, groups[i], + "match", &error); + if (error) + goto error; + p.value = g_key_file_get_value(cfg->keyfile, groups[i], + "value", &error); + if (error) + goto error; + p.priority = g_key_file_get_integer(cfg->keyfile, groups[i], + "priority", &error); + if (error) + goto error; + + list_append_struct(cfg->priorities, p); + } + + return 0; + +error: + if (p.value) + g_free(p.value); + if (p.match) + g_free(p.match); + + g_printerr("Failed to read property in prioritiy group '%s': %s\n", + groups[i], error->message); + + return -1; +} + +static void +destroy_priority(gpointer data) +{ + struct priority *p = data; + + g_free(p->value); + g_free(p->match); + g_free(p); +} + +int +config_init(struct config *cfg) +{ + GError *error = NULL; + + memset(cfg, 0, sizeof *cfg); + cfg->keyfile = g_key_file_new(); + cfg->priorities = NULL; + + if (!g_key_file_load_from_data_dirs(cfg->keyfile, + "pa-sink-ctl/config.ini", + NULL, G_KEY_FILE_NONE, &error) + && error) { + g_printerr("Failed to open config file: %s\n", error->message); + return -1; + } + + if (parse_priorities(cfg) < 0) + return -1; + + return 0; +} + +void +config_uninit(struct config *cfg) +{ + g_list_free_full(cfg->priorities, destroy_priority); + + g_key_file_free(cfg->keyfile); +} -- cgit