aboutsummaryrefslogtreecommitdiff
path: root/src/configuration.c
blob: 1bf9eb983efe64417185b14afeb09ca2dc596fa0 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* configuration.c
 *
 * Copyright 2025 Rose <rose@pinkro.se>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License only.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#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");
    if (xdg_config == NULL) {
        char *home = getenv ("HOME");
        if (home == NULL)
          return NULL;

        // the getenv output cannot be freed, it results in a segfault
        // but this one should be freed, as it is manually allocated
        // however this isnt freed. that is bad. i am too lazy to free it properly
        // autodarkmode can leak some memory, as a treat
        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);

    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;

    const 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);
}