1
0
mirror of https://codeberg.org/vyivel/dulcepan/ synced 2025-03-12 18:59:15 +02:00
dulcepan/src/config.c

205 lines
5.4 KiB
C
Raw Normal View History

2024-06-19 18:40:22 +03:00
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <sfdo-basedir.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dulcepan.h"
#define CONFIG_SUBPATH "dulcepan.cfg"
static inline void bytes_to_color(uint8_t bytes[static 4], float out[static 4]) {
for (size_t i = 0; i < 4; i++) {
out[i] = (float)bytes[i] / UINT8_MAX;
2024-06-23 20:59:39 +03:00
}
2024-06-19 18:40:22 +03:00
}
2024-06-23 20:59:39 +03:00
static void load_color(const char *value, int line_idx, float out[static 4]) {
2024-06-19 18:40:22 +03:00
size_t len = strlen(value);
2024-06-23 20:59:39 +03:00
uint8_t bytes[4] = {0, 0, 0, 0};
if (len == 6) {
2024-06-19 18:40:22 +03:00
bytes[3] = UINT8_MAX;
} else if (len != 8) {
goto bad;
}
for (size_t i = 0; i < len; i++) {
2024-06-23 20:59:39 +03:00
uint8_t digit;
2024-06-19 18:40:22 +03:00
if (value[i] >= '0' && value[i] <= '9') {
2024-06-23 20:59:39 +03:00
digit = (uint8_t)(value[i] - '0');
2024-06-19 18:40:22 +03:00
} else if (value[i] >= 'A' && value[i] <= 'F') {
2024-06-23 20:59:39 +03:00
digit = (uint8_t)(value[i] - 'A' + 10);
2024-06-19 18:40:22 +03:00
} else if (value[i] >= 'a' && value[i] <= 'f') {
2024-06-23 20:59:39 +03:00
digit = (uint8_t)(value[i] - 'a' + 10);
2024-06-19 18:40:22 +03:00
} else {
goto bad;
}
2024-06-23 20:59:39 +03:00
bytes[i / 2] = (uint8_t)(bytes[i / 2] * 16 + digit);
2024-06-19 18:40:22 +03:00
}
bytes_to_color(bytes, out);
return;
bad:
dp_log_fatal("Config: invalid color %s on line %d", value, line_idx);
}
2024-06-24 13:38:56 +03:00
static void load_int(const char *value, int line_idx, int min, int max, int *out) {
const char *p = value;
int mul = 1;
if (*p == '-') {
++p;
mul = -1;
}
2024-06-19 18:40:22 +03:00
*out = 0;
2024-06-24 13:38:56 +03:00
for (; *p != '\0'; p++) {
if ((*p < '0' && *p > 9) || *out >= INT_MAX / 10) {
2024-06-19 18:40:22 +03:00
dp_log_fatal("Config: invalid number %s on line %d", value, line_idx);
}
2024-06-24 13:38:56 +03:00
*out = *out * 10 - '0' + *p;
}
*out *= mul;
if (*out < min || *out > max) {
dp_log_fatal("Config: number %s on line %d is out of range", value, line_idx);
2024-06-19 18:40:22 +03:00
}
}
static void load_bool(const char *value, int line_idx, bool *out) {
if (strcmp(value, "true") == 0) {
*out = true;
} else if (strcmp(value, "false") == 0) {
*out = false;
} else {
dp_log_fatal("Config: invalid boolean value %s on line %d", value, line_idx);
}
}
2024-06-20 19:26:38 +03:00
static void load_key(const char *value, int line_idx, xkb_keysym_t *out) {
*out = xkb_keysym_from_name(value, XKB_KEYSYM_CASE_INSENSITIVE);
if (*out == XKB_KEY_NoSymbol) {
dp_log_fatal("Config: unknown key %s on line %d", value, line_idx);
}
}
2024-06-19 18:40:22 +03:00
void dp_config_load(struct dp_config *config, const char *user_path) {
FILE *fp = NULL;
*config = (struct dp_config){
2024-06-20 19:26:38 +03:00
.quit_key = XKB_KEY_Escape,
.save_key = XKB_KEY_space,
2024-06-19 18:40:22 +03:00
.border_size = 2,
2024-06-19 20:07:06 +02:00
.png_compression = 6,
2024-06-19 18:40:22 +03:00
.quick_select = false,
};
2024-06-23 20:59:39 +03:00
bytes_to_color((uint8_t[]){0xff, 0xff, 0xff, 0x40}, config->unselected_color);
bytes_to_color((uint8_t[]){0x00, 0x00, 0x00, 0x00}, config->selected_color);
bytes_to_color((uint8_t[]){0xff, 0xff, 0xff, 0xff}, config->border_color);
2024-06-19 18:40:22 +03:00
if (user_path != NULL) {
fp = fopen(user_path, "r");
if (fp == NULL) {
dp_log_fatal("Failed to open %s: %s", user_path, strerror(errno));
}
} else {
struct sfdo_basedir_ctx *basedir_ctx = sfdo_basedir_ctx_create();
size_t n_dirs;
const struct sfdo_string *dirs = sfdo_basedir_get_config_dirs(basedir_ctx, &n_dirs);
for (size_t i = 0; i < n_dirs && fp == NULL; i++) {
const struct sfdo_string *dir = &dirs[i];
size_t size = dir->len + sizeof(CONFIG_SUBPATH);
char *path = dp_zalloc(size);
memcpy(path, dir->data, dir->len);
memcpy(path + dir->len, CONFIG_SUBPATH, sizeof(CONFIG_SUBPATH));
fp = fopen(path, "r");
if (fp == NULL) {
if (errno != ENOENT) {
dp_log_fatal("Failed to open %s: %s", path, strerror(errno));
}
}
free(path);
}
sfdo_basedir_ctx_destroy(basedir_ctx);
}
if (fp == NULL) {
return;
}
int line_idx = 0;
char *line = NULL;
size_t line_cap = 0;
ssize_t line_len;
while ((line_len = getline(&line, &line_cap, fp)) != -1) {
++line_idx;
if (line[line_len - 1] == '\n') {
line[--line_len] = '\0';
}
// One of:
// [whitespace] [# comment]
// [whitespace] <key> [whitespace] = [whitespace] <value> [whitespace] [# comment]
ssize_t i = 0;
while (isspace(line[line_len]) && i++ < line_len) {
// Skip whitespace
}
if (line[i] == '\0' || line[i] == '#') {
continue;
}
char *key = &line[i];
while (!isspace(line[i]) && line[i] != '=' && line[i] != '#' && i++ < line_len) {
// Read key
}
char *key_end = &line[i];
while (isspace(line[i]) && i++ < line_len) {
// Skip whitespace
}
if (line[i++] != '=') {
dp_log_fatal("Config: invalid syntax on line %d", line_idx);
}
*key_end = '\0';
while (isspace(line[i]) && i++ < line_len) {
// Skip whitespace
}
char *value = &line[i];
while (!isspace(line[i]) && line[i] != '#' && i++ < line_len) {
// Read value
}
line[i] = '\0';
if (strcmp(key, "unselected-color") == 0) {
2024-06-23 20:59:39 +03:00
load_color(value, line_idx, config->unselected_color);
2024-06-19 18:40:22 +03:00
} else if (strcmp(key, "selected-color") == 0) {
2024-06-23 20:59:39 +03:00
load_color(value, line_idx, config->selected_color);
2024-06-19 18:40:22 +03:00
} else if (strcmp(key, "border-color") == 0) {
2024-06-23 20:59:39 +03:00
load_color(value, line_idx, config->border_color);
2024-06-19 18:40:22 +03:00
} else if (strcmp(key, "border-size") == 0) {
2024-06-24 13:38:56 +03:00
load_int(value, line_idx, 0, INT_MAX, &config->border_size);
2024-06-19 20:07:06 +02:00
} else if (strcmp(key, "png-compression") == 0) {
2024-06-24 13:38:56 +03:00
load_int(value, line_idx, 0, 9, &config->png_compression);
2024-06-19 18:40:22 +03:00
} else if (strcmp(key, "quick-select") == 0) {
load_bool(value, line_idx, &config->quick_select);
2024-06-20 19:26:38 +03:00
} else if (strcmp(key, "quit-key") == 0) {
load_key(value, line_idx, &config->quit_key);
} else if (strcmp(key, "save-key") == 0) {
load_key(value, line_idx, &config->save_key);
2024-06-19 18:40:22 +03:00
} else {
2024-06-24 16:06:42 +03:00
dp_log_error("Config: unknown key %s on line %d", key, line_idx);
2024-06-19 18:40:22 +03:00
}
}
free(line);
fclose(fp);
}