labwc/src/theme/theme.c

150 lines
3.7 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"
2020-10-09 19:46:59 +01:00
#include "common/string-helpers.h"
2021-02-21 22:03:14 +00:00
#include "common/zfree.h"
#include "theme/theme.h"
#include "xbm/xbm.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);
}
2021-02-21 21:54:40 +00:00
static void entry(struct theme *theme, const char *key, const char *value)
2020-06-11 21:20:43 +01:00
{
if (!key || !value) {
2020-06-11 21:20:43 +01:00
return;
}
if (match(key, "window.active.title.bg.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->window_active_title_bg_color);
} else if (match(key, "window.active.handle.bg.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->window_active_handle_bg_color);
} else if (match(key, "window.inactive.title.bg.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->window_inactive_title_bg_color);
} else if (match(key, "window.active.button.unpressed.image.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->window_active_button_unpressed_image_color);
} else if (match(key, "window.inactive.button.unpressed.image.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->window_inactive_button_unpressed_image_color);
2021-02-16 20:43:20 +00:00
} else if (match(key, "menu.items.bg.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->menu_items_bg_color);
2021-02-16 20:43:20 +00:00
} else if (match(key, "menu.items.text.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->menu_items_text_color);
2021-02-16 20:43:20 +00:00
} else if (match(key, "menu.items.active.bg.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->menu_items_active_bg_color);
2021-02-16 20:43:20 +00:00
} else if (match(key, "menu.items.active.text.color")) {
2021-02-21 21:54:40 +00:00
parse_hexstr(value, theme->menu_items_active_text_color);
}
2020-06-11 21:20:43 +01:00
}
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';
2020-10-09 19:46:59 +01:00
*key = string_strip(line);
*value = string_strip(++p);
2020-06-11 21:20:43 +01:00
}
static void
2021-02-21 21:54:40 +00:00
process_line(struct theme *theme, 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);
2021-02-21 21:54:40 +00:00
entry(theme, key, value);
2020-06-11 21:20:43 +01:00
}
static void
2021-02-21 21:54:40 +00:00
theme_read(struct theme *theme, 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) {
info("cannot find theme (%s), using built-in", theme_name);
2021-02-21 21:54:40 +00:00
theme_builtin(theme);
2020-06-11 21:20:43 +01:00
return;
}
2020-10-13 19:41:55 +01:00
info("read 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';
}
2021-02-21 21:54:40 +00:00
process_line(theme, line);
2020-06-11 21:20:43 +01:00
}
free(line);
fclose(stream);
}
void
2021-02-21 21:54:40 +00:00
theme_init(struct theme *theme, struct wlr_renderer *renderer,
const char *theme_name)
{
2021-02-21 21:54:40 +00:00
theme_read(theme, theme_name);
xbm_load(theme, renderer);
}
2021-02-21 22:03:14 +00:00
void
theme_finish(struct theme *theme)
{
zfree(theme->xbm_close_active_unpressed);
zfree(theme->xbm_maximize_active_unpressed);
zfree(theme->xbm_iconify_active_unpressed);
zfree(theme->xbm_close_inactive_unpressed);
zfree(theme->xbm_maximize_inactive_unpressed);
zfree(theme->xbm_iconify_inactive_unpressed);
}