Merge branch 'icons'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Eklöf · committed by Daniel Eklöf · parents 7478a5d, b50af49 · Browse files · 11 files·+435 added−70 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+435 added−70 removed
- other:+435 added−70 removed
Provenance
- Commit
c347f47- Tree
aeb62a6- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent 1
7478a5d- Parent 2
b50af49
11 files changed+435 added−70 removed
modified:application.h+8 added
1 #pragma once2 3+#include <cairo.h>4+5+struct cairo_icon {6+ int size;7+ cairo_surface_t *surface;8+};9+10 struct application {11 char *exec;12 char *title;13 char *comment;14+ struct cairo_icon icon;15 };modified:CMakeLists.txt+1 added
69 unmodified lines70 71 add_executable(f00sel72 main.c73 font.c font.h74+ icon.c icon.h75 render.c render.h76 shm.c shm.h77 log.c log.h78 xdg.c xdg.h11 unmodified linesmodified:font.c+4 added-3 removed
19 unmodified lines20 {21 FcFini();22 }23 cairo_scaled_font_t *24-font_from_name(const char *name, double *size)24+font_from_name(const char *name)25 {26 FcPattern *pattern = FcNameParse((const unsigned char *)name);27 if (pattern == NULL) {28 LOG_ERR("%s: failed to lookup font", name);16 unmodified lines45 LOG_ERR("%s: failed to match font", name);46 return NULL;47 }48 49+ double size;49- if (FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, size)) {50+ if (FcPatternGetDouble(final_pattern, FC_PIXEL_SIZE, 0, &size)) {51 LOG_ERR("%s: failed to get size", name);52 FcPatternDestroy(final_pattern);53 return NULL;54 }10 unmodified lines65 }66 67 cairo_matrix_t matrix, ctm;68 cairo_matrix_init_identity(&ctm);68- cairo_matrix_init_scale(&matrix, *size, *size);69+ cairo_matrix_init_scale(&matrix, size, size);70 71 cairo_font_options_t *options = cairo_font_options_create();72 cairo_scaled_font_t *scaled_font = cairo_scaled_font_create(73 face, &matrix, &ctm, options);12 unmodified linesmodified:font.h+1 added-1 removed
1 #pragma once2 3 #include <cairo.h>4 5-cairo_scaled_font_t *font_from_name(const char *name, double *size);5+cairo_scaled_font_t *font_from_name(const char *name);added:icon.c+203 added
1+#include "icon.h"2+3+#include <stdlib.h>4+#include <stdio.h>5+#include <string.h>6+#include <stdbool.h>7+#include <assert.h>8+#include <unistd.h>9+10+#include <sys/stat.h>11+#include <fcntl.h>12+13+#define LOG_MODULE "icon"14+#include "log.h"15+#include "tllist.h"16+#include "xdg.h"17+18+static bool19+dir_is_usable(const char *path, const char *context, const char *type)20+{21+ return (path != NULL &&22+ context != NULL && strcasecmp(context, "applications") == 0 &&23+ type != NULL && strcasecmp(type, "scalable") != 024+ );25+}26+27+static void28+parse_theme(FILE *index, struct icon_theme *theme)29+{30+ char *section = NULL;31+ int size = -1;32+ int scale = 1;33+ char *context = NULL;34+ char *type = NULL;35+36+ while (true) {37+ char *line = NULL;38+ size_t sz = 0;39+ ssize_t len = getline(&line, &sz, index);40+41+ if (len == -1) {42+ free(line);43+ break;44+ }45+46+ if (len == 0) {47+ free(line);48+ continue;49+ }50+51+ if (line[len - 1] == '\n') {52+ line[len - 1] = '\0';53+ len--;54+ }55+56+ if (len == 0) {57+ free(line);58+ continue;59+ }60+61+ if (line[0] == '[' && line[len - 1] == ']') {62+63+ if (dir_is_usable(section, context, type)) {64+ struct icon_dir dir = {.path = section, .size = size, .scale = scale};65+ tll_push_back(theme->dirs, dir);66+ } else67+ free(section);68+69+ free(context);70+ free(type);71+72+ size = -1;73+ scale = 1;74+ section = NULL;75+ context = NULL;76+ type = NULL;77+78+ section = malloc(len - 2 + 1);79+ memcpy(section, &line[1], len - 2);80+ section[len - 2] = '\0';81+ free(line);82+ continue;83+ }84+85+ char *tok_ctx = NULL;86+87+ const char *key = strtok_r(line, "=", &tok_ctx);88+ char *value = strtok_r(NULL, "=", &tok_ctx);89+90+ if (strcasecmp(key, "inherits") == 0) {91+ char *ctx = NULL;92+ for (const char *theme_name = strtok_r(value, ",", &ctx);93+ theme_name != NULL; theme_name = strtok_r(NULL, ",", &ctx))94+ {95+ struct icon_theme *sub_theme = icon_load_theme(theme_name);96+ if (sub_theme != NULL) {97+ LOG_DBG("%s inherits %s", theme->path, sub_theme->path);98+ tll_push_back(theme->inherits, sub_theme);99+ }100+ }101+ }102+103+ if (strcasecmp(key, "size") == 0)104+ sscanf(value, "%d", &size);105+106+ else if (strcasecmp(key, "scale") == 0)107+ sscanf(value, "%d", &scale);108+109+ else if (strcasecmp(key, "context") == 0)110+ context = strdup(value);111+112+ else if (strcasecmp(key, "type") == 0)113+ type = strdup(value);114+115+ free(line);116+ }117+118+ if (dir_is_usable(section, context, type)) {119+ struct icon_dir dir = {.path = section, .size = size, .scale = scale};120+ tll_push_back(theme->dirs, dir);121+ } else122+ free(section);123+124+ free(context);125+ free(type);126+}127+128+static struct icon_theme *129+load_theme_in(const char *dir)130+{131+ int theme_dir_fd = -1;132+ int index_fd = -1;133+ FILE *index = NULL;134+ struct icon_theme *theme = NULL;135+136+ theme_dir_fd = open(dir, O_RDONLY);137+ if (theme_dir_fd == -1) {138+ //LOG_WARN("%s: failed to open", dir);139+ goto out;140+ }141+142+ index_fd = openat(theme_dir_fd, "index.theme", O_RDONLY);143+ if (index_fd == -1) {144+ LOG_ERRNO("%s/index.theme: failed to open", dir);145+ goto out;146+ }147+148+ index = fdopen(index_fd, "r");149+ assert(index != NULL);150+151+ theme = calloc(1, sizeof(*theme));152+ theme->path = strdup(dir);153+ parse_theme(index, theme);154+155+ out:156+ if (index != NULL)157+ fclose(index);158+ if (index_fd != -1)159+ close(index_fd);160+ if (theme_dir_fd != -1)161+ close(theme_dir_fd);162+ return theme;163+}164+165+struct icon_theme *166+icon_load_theme(const char *name)167+{168+ struct icon_theme *theme = NULL;169+170+ xdg_data_dirs_t dirs = xdg_data_dirs();171+ tll_foreach(dirs, it) {172+ char path[strlen(it->item) + 1 + strlen("icons") + 1 + strlen(name) + 1];173+ sprintf(path, "%s/icons/%s", it->item, name);174+175+ theme = load_theme_in(path);176+ if (theme != NULL)177+ break;178+ }179+180+ xdg_data_dirs_destroy(dirs);181+ return theme;182+}183+184+void185+icon_theme_destroy(struct icon_theme *theme)186+{187+ if (theme == NULL)188+ return;189+190+ free(theme->path);191+192+ tll_foreach(theme->dirs, it) {193+ free(it->item.path);194+ tll_remove(theme->dirs, it);195+ }196+197+ tll_foreach(theme->inherits, it) {198+ icon_theme_destroy(it->item);199+ tll_remove(theme->inherits, it);200+ }201+202+ free(theme);203+}added:icon.h+18 added
1+#pragma once2+3+#include "tllist.h"4+5+struct icon_dir {6+ char *path; /* Relative to theme's base path */7+ int size;8+ int scale;9+};10+11+struct icon_theme {12+ char *path;13+ tll(struct icon_dir) dirs;14+ tll(struct icon_theme *) inherits;15+};16+17+struct icon_theme *icon_load_theme(const char *name);18+void icon_theme_destroy(struct icon_theme *theme);modified:main.c+18 added-3 removed
35 unmodified lines36 #include "render.h"37 #include "application.h"38 #include "match.h"39 #include "xdg.h"40+#include "font.h"41 42 struct monitor {43 struct wl_output *output;44 struct zxdg_output_v1 *xdg;154 unmodified lines199 /* TODO: initialize in enter? */200 c->wl.xkb_state = xkb_state_new(c->wl.xkb_keymap);201 202 munmap(map_str, size);202- /* TODO: should we close(fd)? */203+ close(fd);204 }205 206 static void207 keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,707 unmodified lines915 916 thrd_t keyboard_repeater_id;917 thrd_create(&keyboard_repeater_id, &keyboard_repeater, &c);918 919+ cairo_scaled_font_t *font = font_from_name(font_name);920+ cairo_surface_t *surf = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);921+ cairo_t *cr = cairo_create(surf);922+ cairo_font_extents_t fextents;923+ cairo_set_scaled_font(cr, font);924+ cairo_font_extents(cr, &fextents);925+ cairo_destroy(cr);926+ cairo_surface_destroy(surf);927+928+ LOG_DBG("height: %f, ascent: %f, descent: %f",929+ fextents.height, fextents.ascent, fextents.descent);930+931 //find_programs();919- xdg_find_programs(&c.applications);932+ xdg_find_programs(fextents.height, &c.applications);933 c.matches = malloc(tll_length(c.applications) * sizeof(c.matches[0]));934 update_matches(&c);935 936 c.wl.display = wl_display_connect(NULL);92 unmodified lines1029 /* Trigger a 'configure' event, after which we'll have the width */1030 wl_surface_commit(c.wl.surface);1031 wl_display_roundtrip(c.wl.display);1032 1020- c.render = render_init(font_name);1033+ c.render = render_init(font);1034 1035 refresh(&c);1036 1037 wl_display_dispatch_pending(c.wl.display);42 unmodified lines1080 tll_foreach(c.applications, it) {1081 free(it->item.exec);1082 free(it->item.title);1083 free(it->item.comment);1084+ cairo_surface_destroy(it->item.icon.surface);1085 tll_remove(c.applications, it);1086 }1087 1088 free(c.matches);38 unmodified lines1127 thrd_join(keyboard_repeater_id, NULL);1128 cnd_destroy(&c.repeat.cond);1129 mtx_destroy(&c.repeat.mutex);1130 close(c.repeat.trigger);1131+ cairo_debug_reset_static_data();1132 1133 return ret;1134 }modified:render.c+44 added-9 removed
8 unmodified lines9 #include "font.h"10 11 struct render {12 cairo_scaled_font_t *regular_font;13- double font_size;13 };14 15 static const double x_margin = 20;16 static const double y_margin = 4;159 unmodified lines176 }177 178 render_glyphs(buf, regular_color, &glyphs[0], glyph_start);179 render_glyphs(buf, match_color, &glyphs[start], glyph_match_count);181- render_glyphs(buf, regular_color, &glyphs[start + length], num_glyphs - glyph_start - glyph_match_count);180+ render_glyphs(buf, regular_color, &glyphs[start + length],181+ num_glyphs - glyph_start - glyph_match_count);182 } else183 render_glyphs(buf, regular_color, glyphs, num_glyphs);184 185 cairo_glyph_free(glyphs);15 unmodified lines201 cairo_font_extents_t fextents;202 cairo_font_extents(buf->cairo, &fextents);203 204 const double row_height = 2 * y_margin + fextents.height;205- const double first_row = 2 * border_size + 2 * y_margin + fextents.height;205+ const double first_row = 2 * border_size + row_height;206 const double sel_margin = x_margin / 3;207 208+ /*209+ * LOG_DBG("height=%f, ascent=%f, descent=%f", fextents.height, fextents.ascent,210+ * fextents.descent);211+ */208- double y = first_row + y_margin + fextents.height - fextents.descent;212+ double y = first_row + (row_height + fextents.height) / 2 - fextents.descent;213 214 for (size_t i = 0; i < match_count; i++) {215 const struct match *match = &matches[i];216 4 unmodified lines221 cairo_rectangle(buf->cairo,222 x_margin - sel_margin,223 first_row + i * row_height,224 buf->width - 2 * (x_margin - sel_margin),221- fextents.height + 2 * y_margin);225+ row_height);226 cairo_fill(buf->cairo);227 }228 225- /* Application title */229 double cur_x = border_size + x_margin;230+231+ if (match->application->icon.surface != NULL) {232+ cairo_surface_t *surf = match->application->icon.surface;233+ double size = match->application->icon.size;234+ double scale = 1.0;235+236+ if (size > row_height) {237+ scale = (row_height - 2 * y_margin) / size;238+ LOG_DBG("%s: scaling: %f (row-height: %f, size=%f",239+ match->application->title, scale, row_height, size);240+241+ size *= scale;242+ }243+244+ cairo_save(buf->cairo);245+ cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);246+247+ /* Translate/scale - order matters! */248+ cairo_translate(249+ buf->cairo, cur_x,250+ first_row + i * row_height + (row_height - size) / 2);251+ cairo_scale(buf->cairo, scale, scale);252+253+ cairo_set_source_surface(buf->cairo, surf, 0, 0);254+ cairo_paint(buf->cairo);255+ cairo_restore(buf->cairo);256+ }257+258+ /* TODO: use theme */259+ cur_x += row_height;260+261+ /* Application title */262 render_match_text(263 buf, &cur_x, y,264 match->application->title, match->start_title, match_length,265 render->regular_font, 0xffffffff, 0xcc9393ff);266 232- y += 2 * y_margin + fextents.height;267+ y += row_height;268 }269 }270 271 struct render *237-render_init(const char *font_name)272+render_init(cairo_scaled_font_t *font)273 {274 struct render *render = calloc(1, sizeof(*render));240- render->regular_font = font_from_name(font_name, &render->font_size);275+ render->regular_font = font;276 return render;277 }278 279 void8 unmodified linesmodified:render.h+1 added-1 removed
10 unmodified lines11 size_t cursor;12 };13 14 struct render;15-struct render *render_init(const char *font_name);15+struct render *render_init(cairo_scaled_font_t *font);16 void render_destroy(struct render *render);17 18 void render_prompt(19 const struct render *render, struct buffer *buf,5 unmodified linesmodified:xdg.c+132 added-52 removed
8 unmodified lines9 #include <sys/types.h>10 #include <sys/stat.h>11 #include <dirent.h>12 #include <fcntl.h>13+#include <pwd.h>14 15 #define LOG_MODULE "xdg"16 #define LOG_ENABLE_DBG 017 #include "log.h"18+#include "icon.h"19 20+static struct cairo_icon21+load_icon(const char *name, int icon_size, const struct icon_theme *theme)22+{23+ if (name == NULL || theme == NULL)24+ return (struct cairo_icon){0};25+26+ LOG_DBG("looking for %s in %s", name, theme->path);27+28+ int min_diff = 10000;29+30+ /* Assume sorted */31+ for (size_t i = 0; i < 3; i++) {32+ tll_foreach(theme->dirs, it) {33+ if (it->item.scale != 1)34+ continue;35+36+ const int diff = abs(it->item.size - icon_size);37+ if (i == 0 && diff != 0){38+ /* Looking for *exactly* our wanted size */39+ if (diff < min_diff)40+ min_diff = diff;41+ continue;42+ } else if (i == 1 && diff != min_diff) {43+ /* Try the one which matches most closely */44+ continue;45+ } else {46+ /* Use anyone available */47+ }48+49+ const size_t len = strlen(theme->path) + 1 +50+ strlen(it->item.path) + 1 +51+ strlen(name) + strlen(".png") + 1;52+53+ char *full_path = malloc(len);54+ sprintf(full_path, "%s/%s/%s.png", theme->path, it->item.path, name);55+56+ cairo_surface_t *surf = cairo_image_surface_create_from_png(full_path);57+58+ if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS) {59+ LOG_DBG("%s: %s", name, full_path);60+ free(full_path);61+ return (struct cairo_icon){.size = it->item.size, .surface = surf};62+ }63+64+ free(full_path);65+ cairo_surface_destroy(surf);66+ }67+ }68+69+ tll_foreach(theme->inherits, it) {70+ struct cairo_icon icon = load_icon(name, icon_size, it->item);71+ if (icon.size > 0)72+ return icon;73+ }74+75+ return (struct cairo_icon){0};76+}77+78 static void19-parse_desktop_file(int fd, application_list_t *applications)79+parse_desktop_file(int fd, const struct icon_theme *theme, int icon_size,80+ application_list_t *applications)81 {82 FILE *f = fdopen(fd, "r");83 if (f == NULL)84 return;1 unmodified line86 bool is_desktop_entry = false;87 char *name = NULL;88 char *exec = NULL;89 char *generic_name = NULL;90+ char *icon = NULL;91 92 while (true) {93 char *line = NULL;94 size_t sz = 0;41 unmodified lines136 137 else if (strcasecmp(key, "genericname") == 0)138 generic_name = strdup(value);139 140+ else if (strcasecmp(key, "icon") == 0)141+ icon = strdup(value);142+143 free(line);144 }145 146 fclose(f);10 unmodified lines157 if (!already_added) {158 tll_push_back(159 *applications,160 ((struct application){96- .exec = exec, .title = name, .comment = generic_name}));161+ .exec = exec, .title = name, .comment = generic_name,162+ .icon = load_icon(icon, icon_size, theme)}));163+ free(icon);164 return;165 }166 }167 168 free(name);169 free(exec);170 free(generic_name);171+ free(icon);172 }173 174 static void107-scan_dir(int base_fd, application_list_t *applications)175+scan_dir(int base_fd, const struct icon_theme *theme, int icon_size,176+ application_list_t *applications)177 {178 DIR *d = fdopendir(base_fd);179 if (d == NULL) {180 LOG_ERRNO("failed to open directory");16 unmodified lines197 LOG_ERRNO("%s: failed to open", e->d_name);198 continue;199 }200 132- scan_dir(dir_fd, applications);201+ scan_dir(dir_fd, theme, icon_size, applications);202 close(dir_fd);203 } else if (S_ISREG(st.st_mode)) {204 /* Skip files not ending with ".desktop" */205 const size_t desktop_len = strlen(".desktop");8 unmodified lines214 int fd = openat(base_fd, e->d_name, O_RDONLY);215 if (fd == -1)216 LOG_WARN("%s: failed to open", e->d_name);217 else {149- parse_desktop_file(fd, applications);218+ parse_desktop_file(fd, theme, icon_size, applications);219 close(fd);220 }221 }222+223 }224 225 closedir(d);226 }227 228 void159-xdg_find_programs(application_list_t *applications)229+xdg_find_programs(int icon_size, application_list_t *applications)230 {161- const char *xdg_data_home = getenv("XDG_DATA_HOME");162- if (xdg_data_home != NULL) {163- int fd_base = open(xdg_data_home, O_RDONLY);164- int fd = fd_base != -1 ? openat(fd_base, "applications", O_RDONLY) : -1;231+ struct icon_theme *theme = icon_load_theme("Arc");232+ if (theme == NULL)233+ LOG_WARN("icon theme not found");234+ else235+ LOG_INFO("theme: %s", theme->path);236 166- if (fd_base == -1 || fd == -1)167- LOG_WARN("%s: failed to open", xdg_data_home);168- else169- scan_dir(fd, applications);237+ xdg_data_dirs_t dirs = xdg_data_dirs();238+ tll_foreach(dirs, it) {239+ char path[strlen(it->item) + 1 + strlen("applications") + 1];240+ sprintf(path, "%s/applications", it->item);241 171- close(fd);172- close(fd_base);173- } else {174- const char *home = getenv("HOME");175- char buf[strlen(home) + 1 + strlen(".local/share/applications") + 1];176- sprintf(buf, "%s/%s", home, ".local/share/applications");242+ int fd = open(path, O_RDONLY);243+ if (fd != -1) {244+ scan_dir(fd, theme, icon_size, applications);245+ close(fd);246+ }247+ }248 178- int fd = open(buf, O_RDONLY);179- if (fd == -1)180- LOG_WARN("%s: failed to open", buf);181- else182- scan_dir(fd, applications);183- close(fd);249+ xdg_data_dirs_destroy(dirs);250+ icon_theme_destroy(theme);251+}252+253+xdg_data_dirs_t254+xdg_data_dirs(void)255+{256+ xdg_data_dirs_t ret = tll_init();257+258+ const char *xdg_data_home = getenv("XDG_DATA_HOME");259+ if (xdg_data_home != NULL)260+ tll_push_back(ret, strdup(xdg_data_home));261+ else {262+ static const char *const local = ".local/share";263+ const struct passwd *pw = getpwuid(getuid());264+265+ char *path = malloc(strlen(pw->pw_dir) + 1 + strlen(local) + 1);266+ sprintf(path, "%s/%s", pw->pw_dir, local);267+ tll_push_back(ret, path);268 }269 270 const char *_xdg_data_dirs = getenv("XDG_DATA_DIRS");271+272 if (_xdg_data_dirs != NULL) {188- char *xdg_data_dirs = strdup(_xdg_data_dirs);189- for (const char *data_dir = strtok(xdg_data_dirs, ":");190- data_dir != NULL;191- data_dir = strtok(NULL, ":"))192- {193- int fd_base = open(data_dir, O_RDONLY);194- int fd = fd_base != -1 ? openat(fd_base, "applications", O_RDONLY) : -1;195- if (fd_base == -1 || fd == -1)196- LOG_WARN("%s: failed to open", data_dir);197- else198- scan_dir(fd, applications);273 200- close(fd);201- close(fd_base);274+ char *ctx = NULL;275+ char *copy = strdup(_xdg_data_dirs);276+277+ for (const char *tok = strtok_r(copy, ":", &ctx);278+ tok != NULL;279+ tok = strtok_r(NULL, ":", &ctx))280+ {281+ tll_push_back(ret, strdup(tok));282 }283 204- free(xdg_data_dirs);284+ free(copy);285 } else {206- static const char *const default_paths[] = {207- "/usr/local/share/applications",208- "/usr/share/applications",209- };286+ tll_push_back(ret, strdup("/usr/local/share"));287+ tll_push_back(ret, strdup("/usr/share"));288+ }289 211- for (size_t i = 0; i < sizeof(default_paths) / sizeof(default_paths[0]); i++) {212- int fd = open(default_paths[i], O_RDONLY);213- if (fd == -1)214- LOG_WARN("%s: failed to open", default_paths[i]);215- else216- scan_dir(fd, applications);217- close(fd);218- }290+ return ret;291+}292+293+void294+xdg_data_dirs_destroy(xdg_data_dirs_t dirs)295+{296+ tll_foreach(dirs, it) {297+ free(it->item);298+ tll_remove(dirs, it);299 }300 }modified:xdg.h+5 added-1 removed
1 unmodified line2 3 #include "application.h"4 #include "tllist.h"5 6+typedef tll(char *) xdg_data_dirs_t;7 typedef tll(struct application) application_list_t;8 9+xdg_data_dirs_t xdg_data_dirs(void);10+void xdg_data_dirs_destroy(xdg_data_dirs_t dirs);11+8-void xdg_find_programs(application_list_t *applications);12+void xdg_find_programs(int icon_size, application_list_t *applications);