Skip to content
Myr

icon.c

This file

Path

icon.c

946 lines · 27.6 KiB · C

Last changed by

icon: skip non-existing theme directories as early as possibleDaniel Eklöfbefore adoption1bc67d9

History of this file

Take it

1#include "icon.h"23#include <ctype.h>4#include <stdlib.h>5#include <stdio.h>6#include <string.h>7#include <stdbool.h>8#include <assert.h>9#include <unistd.h>10#include <limits.h>11#include <errno.h>1213#include <sys/stat.h>14#include <fcntl.h>1516#if defined(FUZZEL_ENABLE_PNG_LIBPNG)17 #include "png-fuzzel.h"18#endif1920#if defined(FUZZEL_ENABLE_SVG_LIBRSVG)21 #include <librsvg/rsvg.h>22#endif2324#if defined(FUZZEL_ENABLE_SVG_NANOSVG)25 #include <nanosvg/nanosvg.h>26#endif2728#if defined(FUZZEL_ENABLE_SVG_RESVG)29 #include <resvg.h>30#endif3132#include <tllist.h>3334#define LOG_MODULE "icon"35#define LOG_ENABLE_DBG 036#include "log.h"37#include "xdg.h"38#include "xmalloc.h"39#include "xsnprintf.h"4041typedef tll(char *) theme_names_t;4243static bool44dir_context_is_allowed(const char *context)45{46    static const char *const allowed_contexts[] = {"applications", "apps", "legacy"};4748    if (context == NULL)49        return false;5051    for (size_t i = 0; i < sizeof(allowed_contexts) / sizeof(allowed_contexts[0]); i++) {52        if (strcasecmp(context, allowed_contexts[i]) == 0)53            return true;54    }5556    return false;57}5859static void60parse_theme(FILE *index, struct icon_theme *theme, bool filter_context,61            theme_names_t *themes_to_load)62{63    char *section = NULL;64    int size = -1;65    int min_size = -1;66    int max_size = -1;67    int scale = 1;68    int threshold = 2;69    char *context = NULL;70    enum icon_dir_type type = ICON_DIR_THRESHOLD;7172    while (true) {73        char *line = NULL;74        size_t sz = 0;75        ssize_t len = getline(&line, &sz, index);7677        if (len == -1) {78            free(line);79            break;80        }8182        if (len == 0) {83            free(line);84            continue;85        }8687        if (line[len - 1] == '\n') {88            line[len - 1] = '\0';89            len--;90        }9192        if (len == 0) {93            free(line);94            continue;95        }9697        if (line[0] == '[' && line[len - 1] == ']') {98            if (!filter_context || dir_context_is_allowed(context)) {99                tll_foreach(theme->dirs, it) {100                    struct icon_dir *d = &it->item;101102                    if (section == NULL || strcmp(d->path, section) != 0)103                        continue;104105                    d->size = size;106                    d->min_size = min_size >= 0 ? min_size : size;107                    d->max_size = max_size >= 0 ? max_size : size;108                    d->scale = scale;109                    d->threshold = threshold;110                    d->type = type;111                }112            }113114            free(section);115            free(context);116117            size = min_size = max_size = -1;118            scale = 1;119            section = NULL;120            context = NULL;121            type = ICON_DIR_THRESHOLD;122            threshold = 2;123124            section = xmalloc(len - 2 + 1);125            memcpy(section, &line[1], len - 2);126            section[len - 2] = '\0';127            free(line);128            continue;129        }130131        char *p = line;132        while (isspace((unsigned char)*p))133            p++;134        /* skip over comments and blank lines comprised of only whitespace */135        if (*p == '#' || *p == '\0') {136            free(line);137            continue;138        }139140        const char *key_start = p;141        /* look for the first space or '=', i.e. the end of the key name */142        while (*p != '\0' && !isspace((unsigned char)*p) && *p != '=')143            p++;144        if (p == key_start) {145            /* empty key name */146            free(line);147            continue;148        }149150        char *key_end = p;151152        while (isspace((unsigned char)*p))153            p++;154155        /* keys may not contain whitespace */156        if (*p != '=') {157            free(line);158            continue;159        }160        /* consume the '=' */161        p++;162163        /*164         * While values themselves may contain whitespace, the spec tells us to165         * ignore any leading whitespace:166         * "Space before and after the equals sign should be ignored"167         */168        while (isspace((unsigned char)*p))169            p++;170171        *key_end = '\0';172        const char *key = key_start;173        char *value = p;174175        if (strcasecmp(key, "inherits") == 0) {176            char *ctx = NULL;177            for (const char *theme_name = strtok_r(value, ",", &ctx);178                 theme_name != NULL; theme_name = strtok_r(NULL, ",", &ctx))179            {180                tll_push_back(*themes_to_load, xstrdup(theme_name));181            }182        }183184        if (strcasecmp(key, "directories") == 0) {185            char *save = NULL;186            for (const char *d = strtok_r(value, ",", &save);187                 d != NULL;188                 d = strtok_r(NULL, ",", &save))189            {190                struct icon_dir dir = {.path = xstrdup(d)};191                tll_push_back(theme->dirs, dir);192            }193        }194195        else if (strcasecmp(key, "size") == 0)196            sscanf(value, "%d", &size);197198        else if (strcasecmp(key, "minsize") == 0)199            sscanf(value, "%d", &min_size);200201        else if (strcasecmp(key, "maxsize") == 0)202            sscanf(value, "%d", &max_size);203204        else if (strcasecmp(key, "scale") == 0)205            sscanf(value, "%d", &scale);206207        else if (strcasecmp(key, "context") == 0)208            context = xstrdup(value);209210        else if (strcasecmp(key, "threshold") == 0)211            sscanf(value, "%d", &threshold);212213        else if (strcasecmp(key, "type") == 0) {214            if (strcasecmp(value, "fixed") == 0)215                type = ICON_DIR_FIXED;216            else if (strcasecmp(value, "scalable") == 0)217                type = ICON_DIR_SCALABLE;218            else if (strcasecmp(value, "threshold") == 0)219                type = ICON_DIR_THRESHOLD;220            else {221                LOG_WARN(222                    "ignoring unrecognized icon theme directory type: %s",223                    value);224            }225        }226227        free(line);228    }229230    if (!filter_context || dir_context_is_allowed(context)) {231        tll_foreach(theme->dirs, it) {232            struct icon_dir *d = &it->item;233234            if (section == NULL || strcmp(d->path, section) != 0)235                continue;236237            d->size = size;238            d->min_size = min_size >= 0 ? min_size : size;239            d->max_size = max_size >= 0 ? max_size : size;240            d->scale = scale;241            d->threshold = threshold;242            d->type = type;243        }244    }245246    tll_foreach(theme->dirs, it) {247        if (it->item.size == 0) {248            free(it->item.path);249            tll_remove(theme->dirs, it);250        }251    }252253    free(section);254    free(context);255}256257static bool258load_theme_in(const char *dir, struct icon_theme *theme,259              bool filter_context, theme_names_t *themes_to_load)260{261    char path[PATH_MAX];262    xsnprintf(path, sizeof(path), "%s/index.theme", dir);263264    FILE *index = fopen(path, "re");265    if (index == NULL)266        return false;267268    parse_theme(index, theme, filter_context, themes_to_load);269    fclose(index);270    return true;271}272273static bool274already_loaded_theme(const char *theme_name, icon_theme_list_t themes)275{276    tll_foreach(themes, it) {277        if (strcasecmp(it->item.name, theme_name) == 0) {278            return true;279        }280    }281    return false;282}283284static void285discover_and_load_theme(const char *theme_name, xdg_data_dirs_t dirs,286                        theme_names_t *themes_to_load, bool filter_context,287                        icon_theme_list_t *themes)288{289    tll_foreach(dirs, dir_it) {290        char path[strlen(dir_it->item.path) + 1 +291                  strlen(theme_name) + 1];292        sprintf(path, "%s/%s", dir_it->item.path, theme_name);293294        struct icon_theme theme = {0};295        if (load_theme_in(path, &theme, filter_context, themes_to_load)) {296            theme.name = xstrdup(theme_name);297            tll_push_back(*themes, theme);298        }299    }300}301302static xdg_data_dirs_t303get_icon_dirs(void)304{305    /*306     * See https://specifications.freedesktop.org/icon-theme-spec/latest/#directory_layout307     */308309    xdg_data_dirs_t dirs = xdg_data_dirs();310311    tll_foreach(dirs, it) {312        struct xdg_data_dir *d = &it->item;313        int fd = openat(d->fd, "icons", O_RDONLY | O_DIRECTORY | O_CLOEXEC);314        if (fd >= 0) {315            char *p = xasprintf("%s/icons", d->path);316            free(d->path);317            close(d->fd);318319            d->path = p;320            d->fd = fd;321        } else {322            free(d->path);323            close(d->fd);324            tll_remove(dirs, it);325        }326    }327328    const char *home = getenv("HOME");329    if (home != NULL) {330        char *path = xasprintf("%s/%s", home, ".icons");331        int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);332333        if (fd >= 0) {334            struct xdg_data_dir home_icons = {335                .path = path,336                .fd = fd,337            };338339            tll_push_front(dirs, home_icons);340        } else341            free(path);342    }343344    {345        /* /usr/local/share/pixmaps and usr/share/pixmaps:346         * Even if the specification only mentions /usr/share347         * explicitly as a base dir to look for pixmaps (while icons348         * is searched in both), some distributions do use349         * /usr/local/share/pixmaps (e.g. firefox.png under FreeBSD).350         */351        const char **paths = (const char *[]) { "/usr/local/share/pixmaps", "/usr/share/pixmaps", NULL };352        const char *path;353        while ((path = *paths++) != NULL) {354            int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);355            if (fd >= 0) {356                struct xdg_data_dir home_icons = {357                    .path = xstrdup(path),358                    .fd = fd,359                };360361                tll_push_back(dirs, home_icons);362            }363        }364    }365366    return dirs;367}368369icon_theme_list_t370icon_load_theme(const char *name, bool filter_context)371{372    /* List of themes; first item is the primary theme, subsequent373     * items are inherited items (i.e. fallback themes) */374    icon_theme_list_t themes = tll_init();375376    /* List of themes to try to load. This list will be appended to as377     * we go, and find 'Inherits' values in the theme index files. */378    theme_names_t themes_to_load = tll_init();379    tll_push_back(themes_to_load, xstrdup(name));380381    xdg_data_dirs_t dirs = get_icon_dirs();382383    while (tll_length(themes_to_load) > 0) {384        char *theme_name = tll_pop_front(themes_to_load);385386        /*387         * Check if we've already loaded this theme. Example:388         * "Arc" inherits "Moka,Faba,elementary,Adwaita,gnome,hicolor389         * "Moka" inherits "Faba"390         * "Faba" inherits "elementary,gnome,hicolor"391         */392        if (already_loaded_theme(theme_name, themes)) {393            free(theme_name);394            continue;395        }396397        discover_and_load_theme(theme_name, dirs, &themes_to_load, filter_context, &themes);398        free(theme_name);399    }400401    /*402     * According to the freedesktop.org icon theme spec,403     * implementation are required to always fallback on hicolor, even404     * if it is not explicitly set in inheritance chain.  See405     * https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html406     *407     * Thus, we add it in the end of the list, if it has not been408     * already added as part of the theme inheritance.409     */410    if (!already_loaded_theme("hicolor", themes)) {411        /*412         * hicolor has no dependency, thus the themes_to_load here is413         * assumed to stay empty and will be disregarded.414         */415        discover_and_load_theme("hicolor", dirs, &themes_to_load, filter_context, &themes);416    }417418    xdg_data_dirs_destroy(dirs);419    return themes;420}421422static void423theme_destroy(struct icon_theme theme)424{425    free(theme.name);426427    tll_foreach(theme.dirs, it) {428        free(it->item.path);429        tll_remove(theme.dirs, it);430    }431}432433void434icon_themes_destroy(icon_theme_list_t themes)435{436    tll_foreach(themes, it) {437        theme_destroy(it->item);438        tll_remove(themes, it);439    }440}441442#if defined(FUZZEL_ENABLE_PNG_LIBPNG)443static bool444icon_from_png_libpng(struct icon *icon, const char *file_name, bool gamma_correct)445{446    pixman_image_t *png = png_load(file_name, gamma_correct);447    if (png == NULL)448        return false;449450    icon->type = ICON_PNG;451    icon->png = png;452    return true;453}454#endif455456bool457icon_from_png(struct icon *icon, const char *name, bool gamma_correct)458{459#if defined(FUZZEL_ENABLE_PNG_LIBPNG)460    return icon_from_png_libpng(icon, name, gamma_correct);461#else462    return false;463#endif464}465466#if defined(FUZZEL_ENABLE_SVG_LIBRSVG)467static bool468icon_from_svg_librsvg(struct icon *icon, const char *file_name)469{470    RsvgHandle *svg = rsvg_handle_new_from_file(file_name, NULL);471    if (svg == NULL)472        return false;473474    icon->type = ICON_SVG;475    icon->svg = svg;476    return true;477}478#endif479480#if defined(FUZZEL_ENABLE_SVG_NANOSVG)481static bool482icon_from_svg_nanosvg(struct icon *icon, const char *file_name)483{484    /* TODO: DPI */485    NSVGimage *svg = nsvgParseFromFile(file_name, "px", 96);486    if (svg == NULL)487        return false;488489    if (svg->width == 0 || svg->height == 0)  {490        nsvgDelete(svg);491        return false;492    }493494    icon->type = ICON_SVG;495    icon->svg = svg;496    return true;497}498#endif499500#if defined(FUZZEL_ENABLE_SVG_RESVG)501static bool502icon_from_svg_resvg(struct icon *icon, const char *file_name)503{504    resvg_options *opt = resvg_options_create();505    if (opt == NULL) {506        return false;507    }508509    /* Set DPI to 96, same as nanosvg */510    resvg_options_set_dpi(opt, 96);511512    resvg_render_tree *tree = NULL;513    int32_t result = resvg_parse_tree_from_file(file_name, opt, &tree);514    resvg_options_destroy(opt);515516    if (result != RESVG_OK || tree == NULL) {517        /* Only log actual parsing errors, not just files that aren't valid SVGs */518        if (result == RESVG_ERROR_PARSING_FAILED ||519            result == RESVG_ERROR_MALFORMED_GZIP ||520            result == RESVG_ERROR_INVALID_SIZE) {521            LOG_DBG("%s: resvg failed to parse SVG (code %d)", file_name, result);522        }523        return false;524    }525526    if (resvg_is_image_empty(tree)) {527        resvg_tree_destroy(tree);528        return false;529    }530531    icon->type = ICON_SVG;532    icon->svg = tree;533    return true;534}535#endif536537bool538icon_from_svg(struct icon *icon, const char *name)539{540#if defined(FUZZEL_ENABLE_SVG_LIBRSVG)541    return icon_from_svg_librsvg(icon, name);542#elif defined(FUZZEL_ENABLE_SVG_NANOSVG)543    return icon_from_svg_nanosvg(icon, name);544#elif defined(FUZZEL_ENABLE_SVG_RESVG)545    return icon_from_svg_resvg(icon, name);546#else547    return false;548#endif549}550551static bool552svg(struct icon *icon, const char *path)553{554    icon->path = xstrdup(path);555    icon->type = ICON_SVG;556    icon->svg = NULL;557    return true;558}559560static bool561png(struct icon *icon, const char *path)562{563    icon->path = xstrdup(path);564    icon->type = ICON_PNG;565    icon->png = NULL;566    return true;567}568569static void570icon_reset(struct icon *icon)571{572    free(icon->path);573    icon->path = NULL;574575    switch (icon->type) {576    case ICON_NONE:577        break;578579    case ICON_PNG:580        if (icon->png != NULL) {581#if defined(FUZZEL_ENABLE_PNG_LIBPNG)582            free(pixman_image_get_data(icon->png));583            pixman_image_unref(icon->png);584            icon->png = NULL;585#endif586        }587        break;588589    case ICON_SVG:590        if (icon->svg != NULL) {591#if defined(FUZZEL_ENABLE_SVG_LIBRSVG)592            g_object_unref(icon->svg);593#elif defined(FUZZEL_ENABLE_SVG_NANOSVG)594            nsvgDelete(icon->svg);595#elif defined(FUZZEL_ENABLE_SVG_RESVG)596            resvg_tree_destroy(icon->svg);597#endif598            icon->svg = NULL;599        }600        break;601    }602603    tll_foreach(icon->rasterized, it) {604        struct rasterized *rast = &it->item;605        free(pixman_image_get_data(rast->pix));606        pixman_image_unref(rast->pix);607        tll_remove(icon->rasterized, it);608    }609610    icon->type = ICON_NONE;611}612613/*614 * Path is expected to contain the icon’s basename. It doesn’t have to615 * have the extension filled in; it will be filled in by this616 * function.617 *618 * Note that only supported image types are searched for. That is, if619 * PNGs have been disabled, we only search for SVGs.620 *621 * Also note that we only check for the existence of a file; we don’t622 * validate it.623 *624 * Returns true if there exist an icon file (of the specified name) in625 * <dir_fd>. In this case, path has been updated with the extension626 * (.png or .svg).627 */628static bool629icon_file_exists(int dir_fd, char *path, size_t path_len)630{631#if defined(FUZZEL_ENABLE_PNG_LIBPNG)632    path[path_len - 3] = 'p';633    path[path_len - 2] = 'n';634    path[path_len - 1] = 'g';635636    if (faccessat(dir_fd, path, R_OK, 0) < 0) {637#if defined(FUZZEL_ENABLE_SVG_NANOSVG) || defined(FUZZEL_ENABLE_SVG_LIBRSVG) || defined(FUZZEL_ENABLE_SVG_RESVG)638        path[path_len - 3] = 's';639        path[path_len - 2] = 'v';640        path[path_len - 1] = 'g';641        return faccessat(dir_fd, path, R_OK, 0) == 0;642#else643        return false;644#endif645    }646647    return true;648649#elif defined(FUZZEL_ENABLE_SVG_NANOSVG) || defined(FUZZEL_ENABLE_SVG_LIBRSVG) || defined(FUZZEL_ENABLE_SVG_RESVG)650    path[path_len - 3] = 's';651    path[path_len - 2] = 'v';652    path[path_len - 1] = 'g';653654    return faccessat(dir_fd, path, R_OK, 0) == 0;655#else656    return false;657#endif658}659660static bool661lookup_icons(const icon_theme_list_t *themes, int icon_size,662             struct application_list *applications,663             const xdg_data_dirs_t *xdg_dirs)664{665    struct icon_data {666        const char *name;667        struct application *app;668669        char *file_name;670        size_t file_name_len;671672        struct {673            int diff;674            const struct xdg_data_dir *xdg_dir;675            const struct icon_theme *theme;676            const struct icon_dir *icon_dir;677            enum icon_type type;678        } min_diff;679    };680681    tll(struct icon_data) icons = tll_init();682683    for (size_t i = 0; i < applications->count; i++) {684        struct application *app = applications->v[i];685        icon_reset(&app->icon);686687        if (app->icon.name == NULL)688            continue;689690        if (app->icon.name[0] == '/') {691            const size_t name_len = strlen(app->icon.name);692            if (app->icon.name[name_len - 3] == 's' &&693                app->icon.name[name_len - 2] == 'v' &&694                app->icon.name[name_len - 1] == 'g')695            {696                if (svg(&app->icon, app->icon.name))697                    LOG_DBG("%s: absolute path SVG", app->icon.name);698            } else if (app->icon.name[name_len - 3] == 'p' &&699                       app->icon.name[name_len - 2] == 'n' &&700                       app->icon.name[name_len - 1] == 'g')701            {702                if (png(&app->icon, app->icon.name))703                    LOG_DBG("%s: abslute path PNG", app->icon.name);704            }705        } else {706            char *file_name = xstrjoin(app->icon.name, ".xxx");707            struct icon_data data = {708                .name = app->icon.name,709                .app = app,710                .file_name = file_name,711                .file_name_len = strlen(file_name),712                .min_diff = {.diff = INT_MAX},713            };714            tll_push_back(icons, data);715        }716    }717718    /* For details, see719     * https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html#icon_lookup */720721    tll_foreach(*themes, theme_it) {722        const struct icon_theme *theme = &theme_it->item;723724        /* Fallback icon to use if there aren’t any exact matches */725        /* Assume sorted */726        tll_foreach(theme->dirs, icon_dir_it) {727            const struct icon_dir *icon_dir = &icon_dir_it->item;728729            char theme_relative_path[730                strlen(theme->name) + 1 +731                strlen(icon_dir->path) + 1];732733            sprintf(theme_relative_path, "%s/%s",734                    theme->name, icon_dir->path);735736            tll_foreach(*xdg_dirs, xdg_dir_it) {737                const struct xdg_data_dir *xdg_dir = &xdg_dir_it->item;738739                if (icon_dir->scale > 1) {740                    /*741                     * Scaled dirs are physically bigger icons (more742                     * pixels), but with less details. See743                     *  - https://codeberg.org/dnkl/fuzzel/issues/459#issuecomment-2574718744                     *  - https://codeberg.org/dnkl/fuzzel/issues/459#issuecomment-2574720745                     * For details on why we're skipping these.746                     */747                    continue;748                }749750                if (faccessat(xdg_dir->fd, theme_relative_path, R_OK, 0) != 0) {751                    LOG_DBG("%s/%s (%s): %s, skipping", xdg_dir->path,752                            theme_relative_path, theme->name, strerror(errno));753                    continue;754                }755756                const int size = icon_dir->size;757                const int min_size = icon_dir->min_size;758                const int max_size = icon_dir->max_size;759                const int threshold = icon_dir->threshold;760                const enum icon_dir_type type = icon_dir->type;761762                bool is_exact_match = false;763                int diff = INT_MAX;764765                /* See if this directory is usable for the requested icon size */766                switch (type) {767                case ICON_DIR_FIXED:768                    is_exact_match = size == icon_size;769                    diff = abs(size - icon_size);770                    LOG_DBG(771                        "%s/%s (fixed): "772                        "icon-size=%d, size=%d, exact=%d, diff=%d",773                        xdg_dir->path, theme_relative_path, icon_size, size,774                        is_exact_match, diff);775                    break;776777                case ICON_DIR_THRESHOLD:778                    is_exact_match =779                        (size - threshold) <= icon_size &&780                        (size + threshold) >= icon_size;781                    diff = icon_size < (size - threshold)782                        ? min_size - icon_size783                        : (icon_size > (size + threshold)784                           ? icon_size - max_size785                           : 0);786                    LOG_DBG(787                        "%s/%s (threshold): "788                        "icon-size=%d, threshold=%d, exact=%d, diff=%d",789                        xdg_dir->path, theme_relative_path, icon_size, threshold,790                        is_exact_match, diff);791                    break;792793                case ICON_DIR_SCALABLE:794                    is_exact_match =795                        min_size <= icon_size &&796                        max_size >= icon_size;797                    diff = icon_size < min_size798                        ? min_size - icon_size799                        : (icon_size > max_size800                           ? icon_size - max_size801                           : 0);802                    LOG_DBG("%s/%s (scalable): "803                            "icon-size=%d, min=%d, max=%d, exact=%d, diff=%d",804                            xdg_dir->path, theme_relative_path, icon_size,805                            min_size, max_size, is_exact_match, diff);806                    break;807                }808809                int dir_fd = openat(810                    xdg_dir->fd, theme_relative_path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);811                if (dir_fd < 0)812                    continue;813814                tll_foreach(icons, icon_it) {815                    struct icon_data *icon = &icon_it->item;816817                    if (!is_exact_match && icon->min_diff.diff <= diff)818                        continue;819820                    size_t len = icon->file_name_len;821                    char *path = icon->file_name;822                    path[len - 4] = '.';823824                    if (!icon_file_exists(dir_fd, path, len))825                        continue;826827                    if (!is_exact_match) {828                        assert(diff < icon->min_diff.diff);829                        icon->min_diff.diff = diff;830                        icon->min_diff.xdg_dir = xdg_dir;831                        icon->min_diff.theme = theme;832                        icon->min_diff.icon_dir = icon_dir;833                        icon->min_diff.type = path[len - 3] == 's'834                            ? ICON_SVG : ICON_PNG;835                        continue;836                    }837838                    char *full_path = xasprintf(839                        "%s/%s/%s/%s",840                        xdg_dir->path, theme->name, icon_dir->path, path);841842                    if ((path[len - 3] == 's' &&843                         svg(&icon->app->icon, full_path)) ||844                        (path[len - 3] == 'p' &&845                         png(&icon->app->icon, full_path)))846                    {847                        LOG_DBG("%s: %s", icon->name, full_path);848                        free(icon->file_name);849                        tll_remove(icons, icon_it);850                    }851852                    free(full_path);853                }854855                close(dir_fd);856            }857        }858859        /* Try loading fallbacks for those icons we didn’t find an860         * exact match */861        tll_foreach(icons, icon_it) {862            struct icon_data *icon = &icon_it->item;863864            if (icon->min_diff.type == ICON_NONE) {865                assert(icon->min_diff.xdg_dir == NULL);866                continue;867            }868869            size_t path_len =870                strlen(icon->min_diff.xdg_dir->path) + 1 +871                strlen(icon->min_diff.theme->name) + 1 +872                strlen(icon->min_diff.icon_dir->path) + 1 +873                strlen(icon->name) + 4;874875            char full_path[path_len + 1];876            sprintf(full_path, "%s/%s/%s/%s.%s",877                    icon->min_diff.xdg_dir->path,878                    icon->min_diff.theme->name,879                    icon->min_diff.icon_dir->path,880                    icon->name,881                    icon->min_diff.type == ICON_SVG ? "svg" : "png");882883            if ((icon->min_diff.type == ICON_SVG &&884                 svg(&icon->app->icon, full_path)) ||885                (icon->min_diff.type == ICON_PNG &&886                 png(&icon->app->icon, full_path)))887            {888                LOG_DBG("%s: %s (fallback)", icon->name, full_path);889                free(icon->file_name);890                tll_remove(icons, icon_it);891            } else {892                /* Reset diff data, before checking the parent theme(s) */893                icon->min_diff.diff = INT_MAX;894                icon->min_diff.xdg_dir = NULL;895                icon->min_diff.theme = NULL;896                icon->min_diff.icon_dir = NULL;897                icon->min_diff.type = ICON_NONE;898            }899        }900    }901902    /* Finally, look in XDG_DATA_DIRS/pixmaps */903    tll_foreach(icons, icon_it) {904        const struct icon_data *icon = &icon_it->item;905906        tll_foreach(*xdg_dirs, it) {907            size_t len = icon->file_name_len;908            char *path = icon->file_name;909910            if (!icon_file_exists(it->item.fd, path, len))911                continue;912913            char full_path[strlen(it->item.path) + 1 + len + 1];914915            /* Try SVG variant first */916            sprintf(full_path, "%s/%s", it->item.path, path);917            if (path[len - 3] == 's' && svg(&icon->app->icon, full_path)) {918                LOG_DBG("%s: %s (standalone)", icon->name, full_path);919                break;920            }921922            /* No SVG, look for PNG instead */923            if (path[len - 3] == 'p' && png(&icon->app->icon, full_path)) {924                LOG_DBG("%s: %s (standalone)", icon->name, full_path);925                break;926            }927928        }929930        free(icon->file_name);931        tll_remove(icons, icon_it);932    }933934    return true;935}936937bool938icon_lookup_application_icons(icon_theme_list_t themes, int icon_size,939                              struct application_list *applications)940{941    xdg_data_dirs_t xdg_dirs = get_icon_dirs();942    lookup_icons(&themes, icon_size, applications, &xdg_dirs);943    xdg_data_dirs_destroy(xdg_dirs);944945    return true;946}