labwc/src/theme/theme.c

146 lines
2.9 KiB
C
Raw Normal View History

2020-06-11 21:20:43 +01:00
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <glib.h>
#include <stdbool.h>
2020-06-11 21:20:43 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/dir.h"
2020-08-12 19:37:44 +01:00
#include "common/log.h"
#include "theme/theme.h"
2020-06-11 21:20:43 +01:00
static int
hex_to_dec(char c)
2020-06-11 21:20:43 +01:00
{
if (c >= '0' && c <= '9') {
2020-06-11 21:20:43 +01:00
return c - '0';
}
if (c >= 'a' && c <= 'f') {
2020-06-11 21:20:43 +01:00
return c - 'a' + 10;
}
if (c >= 'A' && c <= 'F') {
2020-06-11 21:20:43 +01:00
return c - 'A' + 10;
}
2020-06-11 21:20:43 +01:00
return 0;
}
void
parse_hexstr(const char *hex, float *rgba)
2020-06-11 21:20:43 +01:00
{
if (!hex || hex[0] != '#' || strlen(hex) < 7) {
2020-06-11 21:20:43 +01:00
return;
}
2020-06-11 21:20:43 +01:00
rgba[0] = (hex_to_dec(hex[1]) * 16 + hex_to_dec(hex[2])) / 255.0;
rgba[1] = (hex_to_dec(hex[3]) * 16 + hex_to_dec(hex[4])) / 255.0;
rgba[2] = (hex_to_dec(hex[5]) * 16 + hex_to_dec(hex[6])) / 255.0;
if (strlen(hex) > 7) {
2020-06-11 21:20:43 +01:00
rgba[3] = atoi(hex + 7) / 100.0;
} else {
2020-06-11 21:20:43 +01:00
rgba[3] = 1.0;
}
2020-06-11 21:20:43 +01:00
}
static bool
match(const gchar *pattern, const gchar *string)
2020-07-27 20:54:00 +01:00
{
return (bool)g_pattern_match_simple(pattern, string);
}
2020-08-21 20:35:06 +01:00
/* clang-format off */
2020-06-11 21:20:43 +01:00
static void entry(const char *key, const char *value)
{
if (!key || !value) {
2020-06-11 21:20:43 +01:00
return;
}
if (match(key, "window.active.title.bg.color")) {
2020-06-11 21:20:43 +01:00
parse_hexstr(value, theme.window_active_title_bg_color);
} else if (match(key, "window.active.handle.bg.color")) {
parse_hexstr(value, theme.window_active_handle_bg_color);
} else if (match(key, "window.inactive.title.bg.color")) {
parse_hexstr(value, theme.window_inactive_title_bg_color);
} else if (match(key, "window.active.button.unpressed.image.color")) {
parse_hexstr(value, theme.window_active_button_unpressed_image_color);
} else if (match(key, "window.inactive.button.unpressed.image.color")) {
2020-08-21 20:35:06 +01:00
parse_hexstr(value, theme.window_inactive_button_unpressed_image_color);
}
2020-06-11 21:20:43 +01:00
}
2020-08-21 20:35:06 +01:00
/* clang-format on */
2020-06-11 21:20:43 +01:00
static void
rtrim(char **s)
2020-06-11 21:20:43 +01:00
{
size_t len = strlen(*s);
if (!len) {
2020-06-11 21:20:43 +01:00
return;
}
2020-06-11 21:20:43 +01:00
char *end = *s + len - 1;
while (end >= *s && isspace(*end)) {
2020-06-11 21:20:43 +01:00
end--;
}
2020-06-11 21:20:43 +01:00
*(end + 1) = '\0';
}
static char *
strstrip(char *s)
2020-06-11 21:20:43 +01:00
{
rtrim(&s);
while (isspace(*s)) {
2020-06-11 21:20:43 +01:00
s++;
}
2020-06-11 21:20:43 +01:00
return s;
}
static void
parse_config_line(char *line, char **key, char **value)
2020-06-11 21:20:43 +01:00
{
char *p = strchr(line, ':');
if (!p) {
2020-06-11 21:20:43 +01:00
return;
}
2020-06-11 21:20:43 +01:00
*p = '\0';
*key = strstrip(line);
*value = strstrip(++p);
}
static void
process_line(char *line)
2020-06-11 21:20:43 +01:00
{
if (line[0] == '\0' || line[0] == '#') {
2020-06-11 21:20:43 +01:00
return;
}
2020-06-11 21:20:43 +01:00
char *key = NULL, *value = NULL;
parse_config_line(line, &key, &value);
entry(key, value);
}
void
theme_read(const char *theme_name)
2020-06-11 21:20:43 +01:00
{
FILE *stream = NULL;
2020-06-11 21:20:43 +01:00
char *line = NULL;
size_t len = 0;
char themerc[4096];
2020-06-11 21:20:43 +01:00
if (strlen(theme_dir(theme_name))) {
snprintf(themerc, sizeof(themerc), "%s/themerc",
theme_dir(theme_name));
stream = fopen(themerc, "r");
}
2020-06-11 21:20:43 +01:00
if (!stream) {
warn("cannot find theme (%s), using built-in", theme_name);
theme_builtin();
2020-06-11 21:20:43 +01:00
return;
}
info("reading themerc (%s)", themerc);
while (getline(&line, &len, stream) != -1) {
2020-06-11 21:20:43 +01:00
char *p = strrchr(line, '\n');
if (p) {
2020-06-11 21:20:43 +01:00
*p = '\0';
}
2020-06-11 21:20:43 +01:00
process_line(line);
}
free(line);
fclose(stream);
}