diff options
author | axtloss <axtlos@getcryst.al> | 2024-05-28 19:12:49 +0200 |
---|---|---|
committer | axtloss <axtlos@getcryst.al> | 2024-05-28 19:12:49 +0200 |
commit | e2d8b96945423c14adbf7a54693f5064d5b157c1 (patch) | |
tree | 0052bad56749bcdf9dd99e822bd9c2ebd54999d4 /src/configuration.c | |
parent | ad8ba2db29c0f3abecb292b1059dad554bf7f3b5 (diff) | |
download | autodarkmode-e2d8b96945423c14adbf7a54693f5064d5b157c1.tar.gz autodarkmode-e2d8b96945423c14adbf7a54693f5064d5b157c1.tar.bz2 |
add configuration system
Diffstat (limited to 'src/configuration.c')
-rw-r--r-- | src/configuration.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/configuration.c b/src/configuration.c new file mode 100644 index 0000000..81a5bbb --- /dev/null +++ b/src/configuration.c @@ -0,0 +1,77 @@ +#include "configuration.h" + +#include "iniparser/src/iniparser.h" +#include "iniparser/src/dictionary.h" +#include "extString.h" +#include <string.h> +#include <glib.h> +#include <stdlib.h> +#include <unistd.h> + +dictionary * +load_config(void) +{ + char *xdg_config = getenv ("XDG_CONFIG_HOME"); + char *home = getenv ("HOME"); + if (home == NULL) + return NULL; + if (xdg_config == NULL) { + xdg_config = malloc (strlen(home)*sizeof(char)+strlen("/.config")*sizeof(char)+1); + sprintf (xdg_config, "%s/.config", home); + } + char *config_path = malloc(strlen(xdg_config)*sizeof(char)+strlen("/autodarkmode/config.ini")*sizeof(char)+1); + sprintf (config_path, "%s/autodarkmode/config.ini", xdg_config); + + free (xdg_config); + + if (access(config_path, F_OK) != 0) { + g_printerr ("Config file not found. Using default values.\n"); + free (config_path); + return NULL; + } + + g_print ("Loading config file %s\n", config_path); + + dictionary *dict = iniparser_load (config_path); + + free (config_path); + return dict; +} + +enum LocationType +config_get_location_type(dictionary *d) +{ + if (d == NULL) + return GCLUE; + + char *loctype = iniparser_getstring (d, "main:locationtype", "gclue"); + + if (strcmp(strlwr(loctype), "gclue") == 0) { + return GCLUE; + } else if (strcmp(strlwr(loctype), "manual") == 0) { + return MANUAL; + } else { + g_printerr("Invalid Location type %s. Defaulting to GeoClue\n", loctype); + return GCLUE; + } + return GCLUE; +} + +float +config_get_latitude (dictionary *d) +{ + if (d == NULL) + return 0; + + return iniparser_getdouble (d, "manual:latitude", 0); +} + +float +config_get_longitude (dictionary *d) +{ + if (d == NULL) + return 0; + + return iniparser_getdouble (d, "manual:longitude", 0); +} + |