particles/icon: init

Introduce a new icon particle. It follows the icon
spec (https://specifications.freedesktop.org/icon-theme-spec/latest/index.html).
Rendering logic is taken from fuzzel (using nanosvg + libpng), while loading
logic is taken from sway. Standard usage is with `use-tag = false` which expands
the provided string template and then loads the string as the icon name. There
are settings to manually override the base paths, themes, etc. The second usage
which is required for tray support is a special icon tag that transfers raw
pixmaps. With `use-tag = true` it first expands the string, and then uses that
output to find an icon pixmap tag. To reduce memory usage, themes are reference
counted so they can be passed down the configuration stack without having to
load them in multiple times.

For programmability, a fallback particle can be specified if no icon/tag is
found `fallback: ...`. And the new icon pixmap tag can be existence checked in
map conditions using `+{tag_name}`.

Future work to be done in follow up diffs:
1. Icon caching. Currently performs an icon lookup on each instantiation & a
   render on each refresh.
2. Theme caching. Changing theme directories results in a new "theme collection"
   being created resulting in the possibility of duplicated theme loading.
This commit is contained in:
Jordan Isaacs 2023-11-13 23:25:05 -08:00
parent 1a323c6d21
commit 6113f9b94e
No known key found for this signature in database
GPG key ID: 98983D44651F8116
51 changed files with 8473 additions and 40 deletions

88
tag.c
View file

@ -9,7 +9,8 @@
#include <string.h>
#define LOG_MODULE "tag"
#define LOG_ENABLE_DBG 1
#define LOG_ENABLE_DBG 0
#include "icon.h"
#include "log.h"
#include "module.h"
@ -29,6 +30,11 @@ struct private
};
};
struct icon_private {
char *name;
struct icon_pixmaps *pixmaps;
};
static const char *
tag_name(const struct tag *tag)
{
@ -375,6 +381,50 @@ tag_new_string(struct module *owner, const char *name, const char *value)
return tag;
}
static const char *
icon_tag_name(const struct icon_tag *icon_tag)
{
const struct icon_private *priv = icon_tag->private;
return priv->name;
}
static struct icon_pixmaps *
pixmaps_as_pixmaps(const struct icon_tag *icon_tag)
{
const struct icon_private *priv = icon_tag->private;
return priv->pixmaps;
}
void
pixmap_destroy(struct icon_tag *icon_tag)
{
struct icon_private *priv = icon_tag->private;
icon_pixmaps_dec(priv->pixmaps);
free(priv->name);
free(priv);
free(icon_tag);
}
struct icon_tag *
icon_tag_new_pixmap(struct module *owner, const char *name, struct icon_pixmaps *pixmaps)
{
if (pixmaps == NULL) {
return NULL;
}
struct icon_private *priv = malloc(sizeof(*priv));
priv->name = strdup(name);
priv->pixmaps = icon_pixmaps_inc(pixmaps);
struct icon_tag *icon_tag = malloc(sizeof(*icon_tag));
icon_tag->private = priv;
icon_tag->owner = owner;
icon_tag->name = &icon_tag_name;
icon_tag->pixmaps = &pixmaps_as_pixmaps;
icon_tag->destroy = &pixmap_destroy;
return icon_tag;
}
const struct tag *
tag_for_name(const struct tag_set *set, const char *name)
{
@ -390,14 +440,22 @@ tag_for_name(const struct tag_set *set, const char *name)
return NULL;
}
void
tag_set_destroy(struct tag_set *set)
const struct icon_tag *
icon_tag_for_name(const struct tag_set *set, const char *name)
{
for (size_t i = 0; i < set->count; i++)
set->tags[i]->destroy(set->tags[i]);
if (set == NULL)
return NULL;
set->tags = NULL;
set->count = 0;
for (size_t i = 0; i < set->icon_count; i++) {
const struct icon_tag *tag = set->icon_tags[i];
if (!tag)
continue;
if (strcmp(tag->name(tag), name) == 0)
return tag;
}
return NULL;
}
struct sbuf {
@ -744,3 +802,19 @@ tags_expand_templates(char *expanded[], const char *template[], size_t nmemb, co
for (size_t i = 0; i < nmemb; i++)
expanded[i] = tags_expand_template(template[i], tags);
}
void
tag_set_destroy(struct tag_set *set)
{
for (size_t i = 0; i < set->count; i++)
set->tags[i]->destroy(set->tags[i]);
for (size_t i = 0; i < set->icon_count; i++) {
if (set->icon_tags[i] != NULL) {
set->icon_tags[i]->destroy(set->icon_tags[i]);
}
}
set->tags = NULL;
set->count = 0;
}