xbm.c: refactor and load builtin icons as fallback

This commit is contained in:
Johan Malm 2020-07-13 20:09:34 +01:00
parent 1330071e0c
commit 8a42bc9184
6 changed files with 86 additions and 63 deletions

View file

@ -6,28 +6,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "theme/xbm/xbm.h"
#include "theme/xbm/parse.h"
#include "theme/theme-dir.h"
#include "rcxml.h"
struct dir {
const char *prefix;
const char *path;
};
static struct dir theme_dirs[] = {
{ "XDG_DATA_HOME", "themes" },
{ "HOME", ".local/share/themes" },
{ "HOME", ".themes" },
{ "XDG_DATA_HOME", "themes" },
{ NULL, "/usr/share/themes" },
{ NULL, "/usr/local/share/themes" },
{ NULL, "opt/share/themes" },
{ NULL, NULL }
};
/* built-in 6x6 buttons */
char close_button_normal[] = { 0x33, 0x3f, 0x1e, 0x1e, 0x3f, 0x33 };
char iconify_button_normal[] = { 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f };
@ -44,7 +28,7 @@ static struct wlr_texture *texture_from_pixmap(struct wlr_renderer *renderer,
pixmap->height, pixmap->data);
}
static struct wlr_texture *builtin(struct wlr_renderer *renderer,
static struct wlr_texture *texture_from_builtin(struct wlr_renderer *renderer,
const char *button)
{
struct pixmap pixmap = xbm_create_pixmap_builtin(button);
@ -54,61 +38,37 @@ static struct wlr_texture *builtin(struct wlr_renderer *renderer,
return texture;
}
static char *theme_dir(void)
{
static char buffer[4096] = { 0 };
if (buffer[0] != '\0')
return buffer;
struct stat st;
for (int i = 0; theme_dirs[i].path; i++) {
char *prefix = NULL;
struct dir d = theme_dirs[i];
if (d.prefix) {
prefix = getenv(d.prefix);
if (!prefix)
continue;
snprintf(buffer, sizeof(buffer), "%s/%s/%s/openbox-3",
prefix, d.path, rc.theme_name);
} else {
snprintf(buffer, sizeof(buffer), "%s/%s/openbox-3",
d.path, rc.theme_name);
}
if (!stat(buffer, &st) && S_ISDIR(st.st_mode))
return buffer;
}
buffer[0] = '\0';
return buffer;
}
static char *xbm_path(const char *button)
{
static char buffer[4096] = { 0 };
snprintf(buffer, sizeof(buffer), "%s/%s", theme_dir(), button);
snprintf(buffer, sizeof(buffer), "%s/%s", theme_dir(rc.theme_name),
button);
return buffer;
}
void xbm_load(struct wlr_renderer *renderer)
static void load_button(struct wlr_renderer *renderer, const char *filename,
struct wlr_texture **texture, char *button)
{
struct token *tokens;
char *buffer = xbm_read_file(xbm_path("close.xbm"));
if (!buffer) {
fprintf(stderr, "no buffer\n");
char *buffer = xbm_read_file(xbm_path(filename));
if (!buffer)
goto out;
}
tokens = xbm_tokenize(buffer);
fprintf(stderr, "loading %s\n", filename);
struct token *tokens = xbm_tokenize(buffer);
free(buffer);
struct pixmap pixmap = xbm_create_pixmap(tokens);
theme.xbm_close = texture_from_pixmap(renderer, &pixmap);
*texture = texture_from_pixmap(renderer, &pixmap);
if (tokens)
free(tokens);
if (pixmap.data)
free(pixmap.data);
out:
if (!theme.xbm_close)
theme.xbm_close = builtin(renderer, close_button_normal);
theme.xbm_maximize = builtin(renderer, max_button_normal);
theme.xbm_iconify = builtin(renderer, iconify_button_normal);
if (!(*texture))
*texture = texture_from_builtin(renderer, button);
}
void xbm_load(struct wlr_renderer *r)
{
load_button(r, "close.xbm", &theme.xbm_close, close_button_normal);
load_button(r, "max.xbm", &theme.xbm_maximize, max_button_normal);
load_button(r, "iconify.xbm", &theme.xbm_iconify, iconify_button_normal);
}