-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.h
157 lines (133 loc) · 4.11 KB
/
config.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef CONFIG_H
#define CONFIG_H
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <toml.h>
#define CONFIG_FILE_PATH \
({ \
const char* home = getenv("HOME"); \
home ? (snprintf(_config_path, sizeof(_config_path), "%s/.config/anvilock/config.toml", home), \
_config_path) \
: (log_message(LOG_LEVEL_ERROR, "HOME environment variable not set."), NULL); \
})
// Buffer to hold config file path
static char _config_path[256];
static char* font_path = NULL;
static char* bg_name = NULL; // New variable for bg name
static char* bg_path = NULL; // New variable for bg path
static char* debug_log_enable = NULL;
static char errbuf[200];
// 1 - Success, 0 - Failure
static int load_config()
{
const char* config_path = CONFIG_FILE_PATH;
if (!config_path)
{
return 0;
}
FILE* config_file = fopen(config_path, "r");
if (config_file == NULL)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to open config file: %s", config_path);
return 0;
}
toml_table_t* config = toml_parse_file(config_file, errbuf, sizeof(errbuf));
fclose(config_file);
if (!config)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to parse config file: %s", errbuf);
return 0;
}
// Parse font table
toml_table_t* font_table = toml_table_in(config, "font");
if (!font_table)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to find font table in config file: %s",
config_path);
toml_free(config);
return 0;
}
// Temporary string to store the font path
char* font_path_str;
if (toml_rtos(toml_raw_in(font_table, "path"), &font_path_str) == -1)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to find font path in config file: %s", config_path);
toml_free(config);
return 0;
}
font_path = strdup(font_path_str);
free(font_path_str);
// Parse bg table
toml_table_t* bg_table = toml_table_in(config, "bg");
if (!bg_table)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to find bg table in config file: %s", config_path);
toml_free(config);
return 0;
}
// Parse the bg name
char* bg_name_str;
if (toml_rtos(toml_raw_in(bg_table, "name"), &bg_name_str) == -1)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to find bg name in config file: %s", config_path);
toml_free(config);
return 0;
}
bg_name = strdup(bg_name_str);
free(bg_name_str);
// Parse the bg path
char* bg_path_str;
if (toml_rtos(toml_raw_in(bg_table, "path"), &bg_path_str) == -1)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to find bg path in config file: %s", config_path);
toml_free(config);
return 0;
}
bg_path = strdup(bg_path_str);
free(bg_path_str);
toml_table_t* debug_table = toml_table_in(config, "debug");
if (!debug_table)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to find debug table in config file: %s",
config_path);
toml_free(config);
return 0;
}
char* debug_option_str;
if (toml_rtos(toml_raw_in(debug_table, "debug_log_enable"), &debug_option_str) == -1)
{
log_message(LOG_LEVEL_ERROR, "[TOML] Failed to find debug_log_enable in config file: %s",
config_path);
toml_free(config);
return 0;
}
debug_log_enable = strdup(debug_option_str);
free(debug_option_str);
toml_free(config);
return 1;
}
static const char* get_font_path() { return font_path; }
static const char* get_bg_name() { return bg_name; }
static const char* get_bg_path() { return bg_path; }
static const char* get_debug_log_option() { return debug_log_enable; }
static void free_config()
{
if (font_path != NULL)
{
free(font_path);
font_path = NULL;
}
if (bg_name != NULL)
{
free(bg_name);
bg_name = NULL;
}
if (bg_path != NULL)
{
free(bg_path);
bg_path = NULL;
}
}
#endif