Skip to content
Myr

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

11 files+435 added−70 removed

modified:application.h+8 added
1#pragma once23#include <cairo.h>45struct cairo_icon {6 int size;7 cairo_surface_t *surface;8};910struct application {11 char *exec;12 char *title;13 char *comment;14 struct cairo_icon icon;15};
modified:CMakeLists.txt+1 added
69 unmodified lines
7071add_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.h
11 unmodified lines
modified:font.c+4 added-3 removed
19 unmodified lines
20{21 FcFini();22}23cairo_scaled_font_t *24font_from_name(const char *name, double *size)24font_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 lines
45 LOG_ERR("%s: failed to match font", name);46 return NULL;47 }4849 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 lines
65 }6667 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);7071 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 lines
modified:font.h+1 added-1 removed
1#pragma once23#include <cairo.h>45cairo_scaled_font_t *font_from_name(const char *name, double *size);5cairo_scaled_font_t *font_from_name(const char *name);
added:icon.c+203 added
1#include "icon.h"23#include <stdlib.h>4#include <stdio.h>5#include <string.h>6#include <stdbool.h>7#include <assert.h>8#include <unistd.h>910#include <sys/stat.h>11#include <fcntl.h>1213#define LOG_MODULE "icon"14#include "log.h"15#include "tllist.h"16#include "xdg.h"1718static bool19dir_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}2627static void28parse_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;3536 while (true) {37 char *line = NULL;38 size_t sz = 0;39 ssize_t len = getline(&line, &sz, index);4041 if (len == -1) {42 free(line);43 break;44 }4546 if (len == 0) {47 free(line);48 continue;49 }5051 if (line[len - 1] == '\n') {52 line[len - 1] = '\0';53 len--;54 }5556 if (len == 0) {57 free(line);58 continue;59 }6061 if (line[0] == '[' && line[len - 1] == ']') {6263 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);6869 free(context);70 free(type);7172 size = -1;73 scale = 1;74 section = NULL;75 context = NULL;76 type = NULL;7778 section = malloc(len - 2 + 1);79 memcpy(section, &line[1], len - 2);80 section[len - 2] = '\0';81 free(line);82 continue;83 }8485 char *tok_ctx = NULL;8687 const char *key = strtok_r(line, "=", &tok_ctx);88 char *value = strtok_r(NULL, "=", &tok_ctx);8990 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 }102103 if (strcasecmp(key, "size") == 0)104 sscanf(value, "%d", &size);105106 else if (strcasecmp(key, "scale") == 0)107 sscanf(value, "%d", &scale);108109 else if (strcasecmp(key, "context") == 0)110 context = strdup(value);111112 else if (strcasecmp(key, "type") == 0)113 type = strdup(value);114115 free(line);116 }117118 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);123124 free(context);125 free(type);126}127128static struct icon_theme *129load_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;135136 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 }141142 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 }147148 index = fdopen(index_fd, "r");149 assert(index != NULL);150151 theme = calloc(1, sizeof(*theme));152 theme->path = strdup(dir);153 parse_theme(index, theme);154155 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}164165struct icon_theme *166icon_load_theme(const char *name)167{168 struct icon_theme *theme = NULL;169170 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);174175 theme = load_theme_in(path);176 if (theme != NULL)177 break;178 }179180 xdg_data_dirs_destroy(dirs);181 return theme;182}183184void185icon_theme_destroy(struct icon_theme *theme)186{187 if (theme == NULL)188 return;189190 free(theme->path);191192 tll_foreach(theme->dirs, it) {193 free(it->item.path);194 tll_remove(theme->dirs, it);195 }196197 tll_foreach(theme->inherits, it) {198 icon_theme_destroy(it->item);199 tll_remove(theme->inherits, it);200 }201202 free(theme);203}
added:icon.h+18 added
1#pragma once23#include "tllist.h"45struct icon_dir {6 char *path; /* Relative to theme's base path */7 int size;8 int scale;9};1011struct icon_theme {12 char *path;13 tll(struct icon_dir) dirs;14 tll(struct icon_theme *) inherits;15};1617struct icon_theme *icon_load_theme(const char *name);18void icon_theme_destroy(struct icon_theme *theme);
modified:main.c+18 added-3 removed
35 unmodified lines
36#include "render.h"37#include "application.h"38#include "match.h"39#include "xdg.h"40#include "font.h"4142struct monitor {43 struct wl_output *output;44 struct zxdg_output_v1 *xdg;
154 unmodified lines
199 /* TODO: initialize in enter? */200 c->wl.xkb_state = xkb_state_new(c->wl.xkb_keymap);201202 munmap(map_str, size);202 /* TODO: should we close(fd)? */203 close(fd);204}205206static void207keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,
707 unmodified lines
915916 thrd_t keyboard_repeater_id;917 thrd_create(&keyboard_repeater_id, &keyboard_repeater, &c);918919 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);927928 LOG_DBG("height: %f, ascent: %f, descent: %f",929 fextents.height, fextents.ascent, fextents.descent);930931 //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);935936 c.wl.display = wl_display_connect(NULL);
92 unmodified lines
1029 /* 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);10321020 c.render = render_init(font_name);1033 c.render = render_init(font);10341035 refresh(&c);10361037 wl_display_dispatch_pending(c.wl.display);
42 unmodified lines
1080 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 }10871088 free(c.matches);
38 unmodified lines
1127 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();11321133 return ret;1134}
modified:render.c+44 added-9 removed
8 unmodified lines
9#include "font.h"1011struct render {12 cairo_scaled_font_t *regular_font;13 double font_size;13};1415static const double x_margin = 20;16static const double y_margin = 4;
159 unmodified lines
176 }177178 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);184185 cairo_glyph_free(glyphs);
15 unmodified lines
201 cairo_font_extents_t fextents;202 cairo_font_extents(buf->cairo, &fextents);203204 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;207208 /*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;213214 for (size_t i = 0; i < match_count; i++) {215 const struct match *match = &matches[i];216
4 unmodified lines
221 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 }228225 /* Application title */229 double cur_x = border_size + x_margin;230231 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;235236 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);240241 size *= scale;242 }243244 cairo_save(buf->cairo);245 cairo_set_operator(buf->cairo, CAIRO_OPERATOR_OVER);246247 /* 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);252253 cairo_set_source_surface(buf->cairo, surf, 0, 0);254 cairo_paint(buf->cairo);255 cairo_restore(buf->cairo);256 }257258 /* TODO: use theme */259 cur_x += row_height;260261 /* 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);266232 y += 2 * y_margin + fextents.height;267 y += row_height;268 }269}270271struct render *237render_init(const char *font_name)272render_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}278279void
8 unmodified lines
modified:render.h+1 added-1 removed
10 unmodified lines
11 size_t cursor;12};1314struct render;15struct render *render_init(const char *font_name);15struct render *render_init(cairo_scaled_font_t *font);16void render_destroy(struct render *render);1718void render_prompt(19 const struct render *render, struct buffer *buf,
5 unmodified lines
modified:xdg.c+132 added-52 removed
8 unmodified lines
9#include <sys/types.h>10#include <sys/stat.h>11#include <dirent.h>12#include <fcntl.h>13#include <pwd.h>1415#define LOG_MODULE "xdg"16#define LOG_ENABLE_DBG 017#include "log.h"18#include "icon.h"1920static struct cairo_icon21load_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};2526 LOG_DBG("looking for %s in %s", name, theme->path);2728 int min_diff = 10000;2930 /* 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;3536 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 }4849 const size_t len = strlen(theme->path) + 1 +50 strlen(it->item.path) + 1 +51 strlen(name) + strlen(".png") + 1;5253 char *full_path = malloc(len);54 sprintf(full_path, "%s/%s/%s.png", theme->path, it->item.path, name);5556 cairo_surface_t *surf = cairo_image_surface_create_from_png(full_path);5758 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 }6364 free(full_path);65 cairo_surface_destroy(surf);66 }67 }6869 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 }7475 return (struct cairo_icon){0};76}7778static void19parse_desktop_file(int fd, application_list_t *applications)79parse_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 line
86 bool is_desktop_entry = false;87 char *name = NULL;88 char *exec = NULL;89 char *generic_name = NULL;90 char *icon = NULL;9192 while (true) {93 char *line = NULL;94 size_t sz = 0;
41 unmodified lines
136137 else if (strcasecmp(key, "genericname") == 0)138 generic_name = strdup(value);139140 else if (strcasecmp(key, "icon") == 0)141 icon = strdup(value);142143 free(line);144 }145146 fclose(f);
10 unmodified lines
157 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 }167168 free(name);169 free(exec);170 free(generic_name);171 free(icon);172}173174static void107scan_dir(int base_fd, application_list_t *applications)175scan_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 lines
197 LOG_ERRNO("%s: failed to open", e->d_name);198 continue;199 }200132 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 lines
214 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 }222223 }224225 closedir(d);226}227228void159xdg_find_programs(application_list_t *applications)229xdg_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);236166 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);241171 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 }248178 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}252253xdg_data_dirs_t254xdg_data_dirs(void)255{256 xdg_data_dirs_t ret = tll_init();257258 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());264265 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 }269270 const char *_xdg_data_dirs = getenv("XDG_DATA_DIRS");271272 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);273200 close(fd);201 close(fd_base);274 char *ctx = NULL;275 char *copy = strdup(_xdg_data_dirs);276277 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 }283204 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 }289211 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}292293void294xdg_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 line
23#include "application.h"4#include "tllist.h"56typedef tll(char *) xdg_data_dirs_t;7typedef tll(struct application) application_list_t;89xdg_data_dirs_t xdg_data_dirs(void);10void xdg_data_dirs_destroy(xdg_data_dirs_t dirs);118void xdg_find_programs(application_list_t *applications);12void xdg_find_programs(int icon_size, application_list_t *applications);