-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.c
269 lines (224 loc) · 7.92 KB
/
config.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include "config.h"
#include "util.h"
#include <ctype.h>
#include <iniparser.h>
#include <math.h>
#ifdef SNDIO
#include <sndio.h>
#endif
#include <stdarg.h>
#include <stdbool.h>
#include <sys/stat.h>
enum input_method default_methods[] = {
INPUT_FIFO,
INPUT_ALSA,
INPUT_PULSE,
};
const char *input_method_names[] = {
"fifo", "alsa", "pulse", "sndio", "shmem",
};
const bool has_input_method[] = {
true, /** Always have at least FIFO and shmem input. */
HAS_ALSA, HAS_PULSE, HAS_SNDIO, true,
};
enum input_method input_method_by_name(const char *str) {
for (int i = 0; i < INPUT_MAX; i++) {
if (!strcmp(str, input_method_names[i])) {
return (enum input_method)i;
}
}
return INPUT_MAX;
}
void write_errorf(void *err, const char *fmt, ...) {
struct error_s *error = (struct error_s *)err;
va_list args;
va_start(args, fmt);
error->length +=
vsnprintf((char *)error->message + error->length, MAX_ERROR_LEN - error->length, fmt, args);
va_end(args);
}
int validate_color(char *checkColor) {
int validColor = 0;
if (checkColor[0] == '#' && strlen(checkColor) == 7) {
// 0 to 9 and a to f
for (int i = 1; checkColor[i]; ++i) {
if (!isdigit(checkColor[i])) {
if (tolower(checkColor[i]) >= 'a' && tolower(checkColor[i]) <= 'f') {
validColor = 1;
} else {
validColor = 0;
break;
}
} else {
validColor = 1;
}
}
}
return validColor;
}
bool validate_colors(void *params, void *err) {
struct config_params *p = (struct config_params *)params;
struct error_s *error = (struct error_s *)err;
// validate: color
if (
(!validate_color(p->plot_l_col)) ||
(!validate_color(p->plot_r_col)) ||
(!validate_color(p->ax_col)) ||
(!validate_color(p->ax_2_col)) ||
(!validate_color(p->text_col)) ||
(!validate_color(p->audio_col)) ||
(!validate_color(p->osc_col))
) {
write_errorf(error, "The value for some color is invalid. It can be one of the 7 "
"named colors.\n");
return false;
}
return true;
}
bool validate_config(struct config_params *p, struct error_s *error) {
// validate: colors
if (!validate_colors(p, error)) {
return false;
}
// validate: persistence
if (p->persistence < 0) {
p->persistence = 0;
} else if (p->persistence >= 1.0) {
p->persistence = 0.99999;
}
return true;
}
bool load_colors(struct config_params *p, dictionary *ini) {
free(p->plot_l_col);
p->plot_l_col = strdup(iniparser_getstring(ini, "color:plot_l", "#FF5100"));
free(p->plot_r_col);
p->plot_r_col = strdup(iniparser_getstring(ini, "color:plot_r", "#00FF61"));
free(p->ax_col);
p->ax_col = strdup(iniparser_getstring(ini, "color:ax", "#92FF00"));
free(p->ax_2_col);
p->ax_2_col = strdup(iniparser_getstring(ini, "color:ax_2", "#FF5110"));
free(p->text_col);
p->text_col = strdup(iniparser_getstring(ini, "color:text", "#FF5000"));
free(p->audio_col);
p->audio_col = strdup(iniparser_getstring(ini, "color:audio", "#FF5000"));
free(p->osc_col);
p->osc_col = strdup(iniparser_getstring(ini, "color:oscilloscope", "#FF5000"));
return true;
}
bool load_config(char configPath[PATH_MAX], struct config_params *p, struct error_s *error) {
FILE *fp;
// config: creating path to default config file
if (configPath[0] == '\0') {
char *configFile = "config";
char *configHome = getenv("XDG_CONFIG_HOME");
if (configHome != NULL) {
sprintf(configPath, "%s/%s/", configHome, PACKAGE);
} else {
configHome = getenv("HOME");
if (configHome != NULL) {
sprintf(configPath, "%s/%s/", configHome, ".config");
mkdir(configPath, 0777);
sprintf(configPath, "%s/%s/%s/", configHome, ".config", PACKAGE);
} else {
write_errorf(error, "No HOME found (ERR_HOMELESS), exiting...");
return false;
}
}
// config: create directory
mkdir(configPath, 0777);
// config: adding default filename file
strcat(configPath, configFile);
fp = fopen(configPath, "ab+");
if (fp) {
fclose(fp);
} else {
write_errorf(error, "Unable to access config '%s', exiting...\n", configPath);
return false;
}
} else { // opening specified file
fp = fopen(configPath, "rb+");
if (fp) {
fclose(fp);
} else {
write_errorf(error, "Unable to open file '%s', exiting...\n", configPath);
return false;
}
}
// config: parse ini
dictionary *ini;
ini = iniparser_load(configPath);
// config: general
p->persistence = iniparser_getdouble(ini, "general:persistence", 0.95);
if (!load_colors(p, ini)) {
return false;
}
p->noise_floor = iniparser_getint(ini, "general:noise_floor", -100);
p->height = iniparser_getint(ini, "output:height", 480);
p->width = iniparser_getint(ini, "output:width", 800);
p->rotate = iniparser_getint(ini, "output:rotate", 0);
p->fullscreen = !strcmp(iniparser_getstring(ini, "output:fullscreen", "false"), "true");
free(p->text_font);
p->text_font = strdup(iniparser_getstring(ini, "general:text_font", "/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf"));
free(p->audio_font);
p->audio_font = strdup(iniparser_getstring(ini, "general:audio_font", "/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf"));
free(p->vis);
p->vis = strdup(iniparser_getstring(ini, "general:vis", "fft"));
// config: output
free(p->audio_source);
// config: input
char *input_method_name;
for (size_t i = 0; i < ARRAY_SIZE(default_methods); i++) {
enum input_method method = default_methods[i];
if (has_input_method[method]) {
input_method_name =
(char *)iniparser_getstring(ini, "input:method", input_method_names[method]);
}
}
p->im = input_method_by_name(input_method_name);
switch (p->im) {
#ifdef ALSA
case INPUT_ALSA:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "hw:Loopback,1"));
break;
#endif
case INPUT_FIFO:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "/tmp/mpd.fifo"));
p->fifoSample = iniparser_getint(ini, "input:sample_rate", 44100);
p->fifoSampleBits = iniparser_getint(ini, "input:sample_bits", 16);
break;
#ifdef PULSE
case INPUT_PULSE:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", "auto"));
break;
#endif
#ifdef SNDIO
case INPUT_SNDIO:
p->audio_source = strdup(iniparser_getstring(ini, "input:source", SIO_DEVANY));
break;
#endif
case INPUT_SHMEM:
p->audio_source =
strdup(iniparser_getstring(ini, "input:source", "/squeezelite-00:00:00:00:00:00"));
break;
case INPUT_MAX: {
char supported_methods[255] = "";
for (int i = 0; i < INPUT_MAX; i++) {
if (has_input_method[i]) {
strcat(supported_methods, "'");
strcat(supported_methods, input_method_names[i]);
strcat(supported_methods, "' ");
}
}
write_errorf(error, "input method '%s' is not supported, supported methods are: %s\n",
input_method_name, supported_methods);
return false;
}
default:
write_errorf(error, "bellini was built without '%s' input support\n",
input_method_names[p->im]);
return false;
}
bool result = validate_config(p, error);
iniparser_freedict(ini);
return result;
}