File: icon.c

package info (click to toggle)
fnott 1.7.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 688 kB
  • sloc: ansic: 7,803; xml: 369; sh: 27; makefile: 8
file content (497 lines) | stat: -rw-r--r-- 14,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "icon.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>

#include <sys/stat.h>
#include <fcntl.h>

#include <tllist.h>

#include <nanosvg/nanosvg.h>
#include <nanosvg/nanosvgrast.h>

#define LOG_MODULE "icon"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "png-fnott.h"
#include "svg.h"
#include "xdg.h"

enum icon_type { ICON_NONE, ICON_PNG, ICON_SVG };

typedef tll(char *) theme_names_t;

pixman_image_t *
icon_load(const char *name, int icon_size, const icon_theme_list_t *themes)
{
    pixman_image_t *pix = NULL;

    if (name[0] == '/') {
        if ((pix = svg_load(name, icon_size)) != NULL) {
            LOG_DBG("%s: absolute path SVG", name);
            return pix;
        }
        if ((pix = png_load(name)) != NULL) {
            LOG_DBG("%s: abslute path PNG", name);
            return pix;
        }
    }

    const size_t file_name_len = strlen(name) + 4;
    char file_name[file_name_len + 1];
    strcpy(file_name, name);
    strcat(file_name, ".xxx");

    struct {
        int diff;
        const struct xdg_data_dir *xdg_dir;
        const struct icon_theme *theme;
        const struct icon_dir *icon_dir;
        enum icon_type type;
    } min_diff = {.diff = INT_MAX};

    /* For details, see
     * https://specifications.freedesktop.org/icon-theme-spec/icon-theme-spec-latest.html#icon_lookup */

    xdg_data_dirs_t xdg_dirs = xdg_data_dirs();

    tll_foreach(*themes, theme_it) {
        const struct icon_theme *theme = &theme_it->item;

        /* Fallback icon to use if there aren’t any exact matches */
        /* Assume sorted */
        tll_foreach(theme->dirs, icon_dir_it) {
            const struct icon_dir *icon_dir = &icon_dir_it->item;

            char theme_relative_path[
                5 + 1 + /* “icons” */
                strlen(theme->name) + 1 +
                strlen(icon_dir->path) + 1];

            sprintf(theme_relative_path, "icons/%s/%s",
                    theme->name, icon_dir->path);

            tll_foreach(xdg_dirs, xdg_dir_it) {
                const struct xdg_data_dir *xdg_dir = &xdg_dir_it->item;

                const int scale = icon_dir->scale;
                const int size = icon_dir->size * scale;
                const int min_size = icon_dir->min_size * scale;
                const int max_size = icon_dir->max_size * scale;
                const int threshold = icon_dir->threshold * scale;
                const enum icon_dir_type type = icon_dir->type;

                bool is_exact_match = false;;
                int diff = INT_MAX;

                /* See if this directory is usable for the requested icon size */
                switch (type) {
                case ICON_DIR_FIXED:
                    is_exact_match = size == icon_size;
                    diff = abs(size - icon_size);
                    break;

                case ICON_DIR_THRESHOLD:
                    is_exact_match =
                        (size - threshold) <= icon_size &&
                        (size + threshold) >= icon_size;
                    diff = icon_size < (size - threshold)
                        ? (size - threshold) - icon_size
                        : icon_size - (size + threshold);
                    break;

                case ICON_DIR_SCALABLE:
                    is_exact_match =
                        min_size <= icon_size &&
                        max_size >= icon_size;
                    diff = icon_size < min_size
                        ? min_size - icon_size
                        : icon_size - max_size;
                    break;
                }

                int dir_fd = openat(
                    xdg_dir->fd, theme_relative_path, O_RDONLY | O_DIRECTORY);
                if (dir_fd < 0)
                    continue;

                if (!is_exact_match && min_diff.diff <= diff) {
                    close(dir_fd);
                    continue;
                }

                const size_t len = file_name_len;
                char *path = file_name;
                path[len - 4] = '.';
                path[len - 3] = 'p';
                path[len - 2] = 'n';
                path[len - 1] = 'g';

                if (faccessat(dir_fd, path, R_OK, 0) < 0) {
                    path[len - 3] = 's';
                    path[len - 2] = 'v';
                    path[len - 1] = 'g';
                    if (faccessat(dir_fd, path, R_OK, 0) < 0) {
                        close(dir_fd);
                        continue;
                    }
                }

                if (!is_exact_match) {
                    assert(diff < min_diff.diff);
                    min_diff.diff = diff;
                    min_diff.xdg_dir = xdg_dir;
                    min_diff.theme = theme;
                    min_diff.icon_dir = icon_dir;
                    min_diff.type = path[len - 3] == 's'
                        ? ICON_SVG : ICON_PNG;
                    close(dir_fd);
                    continue;
                }

                size_t path_len =
                    strlen(xdg_dir->path) + 1 +
                    5 + 1 + /* “icons” */
                    strlen(theme->name) + 1 +
                    strlen(icon_dir->path) + 1 +
                    len;
                char full_path[path_len + 1];

                sprintf(full_path, "%s/icons/%s/%s/%s",
                        xdg_dir->path, theme->name, icon_dir->path, path);

                if ((path[len - 3] == 's' &&
                     (pix = svg_load(full_path, icon_size)) != NULL) ||
                    (path[len - 3] == 'p' &&
                     (pix = png_load(full_path)) != NULL))
                {
                    LOG_DBG("%s: %s", icon.name, full_path);
                    close(dir_fd);
                    goto done;
                }

                close(dir_fd);
            }
        }

        /* Try loading fallbacks for those icons we didn’t find an
         * exact match */
        if (min_diff.type == ICON_NONE) {
            assert(min_diff.xdg_dir == NULL);
            continue;
        }

        size_t path_len =
            strlen(min_diff.xdg_dir->path) + 1 +
            5 + 1 + /* “icons” */
            strlen(min_diff.theme->name) + 1 +
            strlen(min_diff.icon_dir->path) + 1 +
            strlen(name) + 4;

        char full_path[path_len + 1];
        sprintf(full_path, "%s/icons/%s/%s/%s.%s",
                min_diff.xdg_dir->path,
                min_diff.theme->name,
                min_diff.icon_dir->path,
                name,
                min_diff.type == ICON_SVG ? "svg" : "png");

        if ((min_diff.type == ICON_SVG &&
             (pix = svg_load(full_path, icon_size)) != NULL) ||
            (min_diff.type == ICON_PNG &&
             (pix = png_load(full_path)) != NULL))
        {
            LOG_DBG("%s: %s (fallback)", icon.name, full_path);
            goto done;
        } else {
            /* Reset diff data, before checking the parent theme(s) */
            min_diff.diff = INT_MAX;
            min_diff.xdg_dir = NULL;
            min_diff.theme = NULL;
            min_diff.icon_dir = NULL;
            min_diff.type = ICON_NONE;
        }
    }

    /* Finally, look in XDG_DATA_DIRS/pixmaps */
    tll_foreach(xdg_dirs, it) {
        const size_t len =
            strlen(it->item.path) + 1 +
            strlen("pixmaps") + 1 +
            strlen(name) + strlen(".svg");
        char path[len + 1];

        /* Try SVG variant first */
        sprintf(path, "%s/pixmaps/%s.svg", it->item.path, name);
        if ((pix = svg_load(path, icon_size)) != NULL) {
            LOG_DBG("%s: %s (pixmaps)", icon.name, path);
            goto done;
        }

        /* No SVG, look for PNG instead */
        path[len - 3] = 'p';
        path[len - 2] = 'n';
        path[len - 1] = 'g';
        if ((pix = png_load(path)) != NULL) {
            LOG_DBG("%s: %s (pixmaps)", icon.name, path);
            goto done;
        }
    }

done:
    xdg_data_dirs_destroy(xdg_dirs);
    return pix;
}

static void
parse_theme(FILE *index, struct icon_theme *theme, theme_names_t *themes_to_load)
{
    char *section = NULL;
    int size = -1;
    int min_size = -1;
    int max_size = -1;
    int scale = 1;
    int threshold = 2;
    char *context = NULL;
    enum icon_dir_type type = ICON_DIR_THRESHOLD;

    while (true) {
        char *line = NULL;
        size_t sz = 0;
        ssize_t len = getline(&line, &sz, index);

        if (len == -1) {
            free(line);
            break;
        }

        if (len == 0) {
            free(line);
            continue;
        }

        if (line[len - 1] == '\n') {
            line[len - 1] = '\0';
            len--;
        }

        if (len == 0) {
            free(line);
            continue;
        }

        if (line[0] == '[' && line[len - 1] == ']') {

            tll_foreach(theme->dirs, it) {
                struct icon_dir *d = &it->item;

                if (section == NULL || strcmp(d->path, section) != 0)
                    continue;

                d->size = size;
                d->min_size = min_size >= 0 ? min_size : size;
                d->max_size = max_size >= 0 ? max_size : size;
                d->scale = scale;
                d->threshold = threshold;
                d->type = type;
            }

            free(section);
            free(context);

            size = min_size = max_size = -1;
            scale = 1;
            section = NULL;
            context = NULL;
            type = ICON_DIR_THRESHOLD;
            threshold = 2;

            section = malloc(len - 2 + 1);
            memcpy(section, &line[1], len - 2);
            section[len - 2] = '\0';
            free(line);
            continue;
        }

        char *tok_ctx = NULL;

        const char *key = strtok_r(line, "=", &tok_ctx);
        char *value = strtok_r(NULL, "=", &tok_ctx);

        if (strcasecmp(key, "inherits") == 0) {
            char *ctx = NULL;
            for (const char *theme_name = strtok_r(value, ", ", &ctx);
                 theme_name != NULL; theme_name = strtok_r(NULL, ", ", &ctx))
            {
                tll_push_back(*themes_to_load, strdup(theme_name));
            }
        }

        if (strcasecmp(key, "directories") == 0) {
            char *save = NULL;
            for (const char *d = strtok_r(value, ",", &save);
                 d != NULL;
                 d = strtok_r(NULL, ",", &save))
            {
                struct icon_dir dir = {.path = strdup(d)};
                tll_push_back(theme->dirs, dir);
            }
        }

        else if (strcasecmp(key, "size") == 0)
            sscanf(value, "%d", &size);

        else if (strcasecmp(key, "minsize") == 0)
            sscanf(value, "%d", &min_size);

        else if (strcasecmp(key, "maxsize") == 0)
            sscanf(value, "%d", &max_size);

        else if (strcasecmp(key, "scale") == 0)
            sscanf(value, "%d", &scale);

        else if (strcasecmp(key, "context") == 0)
            context = strdup(value);

        else if (strcasecmp(key, "threshold") == 0)
            sscanf(value, "%d", &threshold);

        else if (strcasecmp(key, "type") == 0) {
            if (strcasecmp(value, "fixed") == 0)
                type = ICON_DIR_FIXED;
            else if (strcasecmp(value, "scalable") == 0)
                type = ICON_DIR_SCALABLE;
            else if (strcasecmp(value, "threshold") == 0)
                type = ICON_DIR_THRESHOLD;
            else {
                LOG_WARN(
                    "ignoring unrecognized icon theme directory type: %s",
                    value);
            }
        }

        free(line);
    }

    tll_foreach(theme->dirs, it) {
        struct icon_dir *d = &it->item;

        if (section == NULL || strcmp(d->path, section) != 0)
            continue;

        d->size = size;
        d->min_size = min_size >= 0 ? min_size : size;
        d->max_size = max_size >= 0 ? max_size : size;
        d->scale = scale;
        d->threshold = threshold;
        d->type = type;
    }

    tll_foreach(theme->dirs, it) {
        if (it->item.size == 0) {
            free(it->item.path);
            tll_remove(theme->dirs, it);
        }
    }

    free(section);
    free(context);
}

static bool
load_theme_in(const char *dir, struct icon_theme *theme,
              theme_names_t *themes_to_load)
{
    char path[PATH_MAX];
    snprintf(path, sizeof(path), "%s/index.theme", dir);

    FILE *index = fopen(path, "r");
    if (index == NULL)
        return false;

    parse_theme(index, theme, themes_to_load);
    fclose(index);
    return true;
}

icon_theme_list_t
icon_load_theme(const char *name)
{
    /* List of themes; first item is the primary theme, subsequent
     * items are inherited items (i.e. fallback themes) */
    icon_theme_list_t themes = tll_init();

    /* List of themes to try to load. This list will be appended to as
     * we go, and find 'Inherits' values in the theme index files. */
    theme_names_t themes_to_load = tll_init();
    tll_push_back(themes_to_load, strdup(name));

    xdg_data_dirs_t dirs = xdg_data_dirs();

    while (tll_length(themes_to_load) > 0) {
        char *theme_name = tll_pop_front(themes_to_load);

        /*
         * Check if we've already loaded this theme. Example:
         * "Arc" inherits "Moka,Faba,elementary,Adwaita,ghome,hicolor
         * "Moka" inherits "Faba"
         * "Faba" inherits "elementary,gnome,hicolor"
         */
        bool theme_already_loaded = false;
        tll_foreach(themes, it) {
            if (strcasecmp(it->item.name, theme_name) == 0) {
                theme_already_loaded = true;
                break;
            }
        }

        if (theme_already_loaded) {
            free(theme_name);
            continue;
        }

        tll_foreach(dirs, dir_it) {
            char path[strlen(dir_it->item.path) + 1 +
                      strlen("icons") + 1 +
                      strlen(theme_name) + 1];
            sprintf(path, "%s/icons/%s", dir_it->item.path, theme_name);

            struct icon_theme theme = {0};
            if (load_theme_in(path, &theme, &themes_to_load)) {
                theme.name = strdup(theme_name);
                tll_push_back(themes, theme);
            }
        }

        free(theme_name);
    }

    xdg_data_dirs_destroy(dirs);
    return themes;
}

static void
theme_destroy(struct icon_theme theme)
{
    free(theme.name);

    tll_foreach(theme.dirs, it) {
        free(it->item.path);
        tll_remove(theme.dirs, it);
    }
}

void
icon_themes_destroy(icon_theme_list_t themes)
{
    tll_foreach(themes, it) {
        theme_destroy(it->item);
        tll_remove(themes, it);
    }
}