rofi 2.1.2
rofi-icon-fetcher.c
Go to the documentation of this file.
1/*
2 * rofi
3 *
4 * MIT/X11 License
5 * Copyright © 2013-2023 Qball Cow <qball@gmpclient.org>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
29#include "glib.h"
30#define G_LOG_DOMAIN "Helpers.IconFetcher"
31
32#include "config.h"
33#include <stdlib.h>
34
35#include "helper.h"
36#include "rofi-icon-fetcher.h"
37#include "rofi-types.h"
38#include "settings.h"
39#include <cairo.h>
40#include <pango/pangocairo.h>
41
42#include "display.h"
43#include "keyb.h"
44#include "view.h"
45
46#include "nkutils-enum.h"
47#include "nkutils-xdg-theme.h"
48
49#include <stdint.h>
50
51#include "helper.h"
52#include <gdk-pixbuf/gdk-pixbuf.h>
53
55#define THUMBNAILER_ENTRY_GROUP "Thumbnailer Entry"
57#define THUMBNAILER_EXTENSION ".thumbnailer"
58
59typedef struct {
60 // Context for icon-themes.
61 NkXdgThemeContext *xdg_context;
62
63 // On name.
64 GHashTable *icon_cache;
65 // On uid.
66 GHashTable *icon_cache_uid;
67
68 // list extensions
70 uint32_t last_uid;
71
72 // thumbnailers per mime-types hashmap
73 GHashTable *thumbnailers;
75
76typedef struct {
77 char *name;
78 GList *sizes;
80
81typedef struct {
83
84 GCond *cond;
85 GMutex *mutex;
86 unsigned int *acount;
87
88 uint32_t uid;
89 int wsize;
90 int hsize;
91 guint scale;
92 cairo_surface_t *surface;
93 gboolean query_done;
94 gboolean query_started;
95
98
99// Free method.
100static void rofi_icon_fetch_entry_free(gpointer data);
106
107static void rofi_icon_fetcher_load_thumbnailers(const gchar *path) {
108 gchar *thumb_path = g_build_filename(path, "thumbnailers", NULL);
109
110 GDir *dir = g_dir_open(thumb_path, 0, NULL);
111
112 if (!dir) {
113 g_free(thumb_path);
114 return;
115 }
116
117 const gchar *dirent;
118
119 while ((dirent = g_dir_read_name(dir))) {
120 if (!g_str_has_suffix(dirent, THUMBNAILER_EXTENSION))
121 continue;
122
123 gchar *filename = g_build_filename(thumb_path, dirent, NULL);
124 GKeyFile *key_file = g_key_file_new();
125 GError *error = NULL;
126
127 if (!g_key_file_load_from_file(key_file, filename, 0, &error)) {
128 g_warning("Error loading thumbnailer %s: %s", filename, error->message);
129 g_error_free(error);
130 } else {
131 gchar *command = g_key_file_get_string(key_file, THUMBNAILER_ENTRY_GROUP,
132 "Exec", NULL);
133 gchar **mime_types = g_key_file_get_string_list(
134 key_file, THUMBNAILER_ENTRY_GROUP, "MimeType", NULL, NULL);
135
136 if (mime_types && command) {
137 guint i;
138 for (i = 0; mime_types[i] != NULL; i++) {
139 if (!g_hash_table_lookup(rofi_icon_fetcher_data->thumbnailers,
140 mime_types[i])) {
141 g_info("Loading thumbnailer %s for mimetype %s", filename,
142 mime_types[i]);
143 g_hash_table_insert(rofi_icon_fetcher_data->thumbnailers,
144 g_strdup(mime_types[i]), g_strdup(command));
145 }
146 }
147 }
148
149 if (mime_types)
150 g_strfreev(mime_types);
151 if (command)
152 g_free(command);
153 }
154
155 g_key_file_free(key_file);
156 g_free(filename);
157 }
158
159 g_dir_close(dir);
160 g_free(thumb_path);
161}
162
163static gchar **setup_thumbnailer_command(const gchar *command,
164 const gchar *filename,
165 const gchar *encoded_uri,
166 const gchar *output_path, int size) {
167 gchar **command_parts = g_strsplit(command, " ", 0);
168 guint command_parts_count = g_strv_length(command_parts);
169
170 gchar **command_args = NULL;
171
172 if (command_parts) {
173 command_args = g_malloc0(sizeof(gchar *) * (command_parts_count + 3 + 1));
174
175 // set process niceness value to 19 (low priority)
176 guint current_index = 0;
177
178 command_args[current_index++] = g_strdup("nice");
179 command_args[current_index++] = g_strdup("-n");
180 command_args[current_index++] = g_strdup("19");
181
182 // add executable and arguments of the thumbnailer to the list
183 guint i;
184 for (i = 0; command_parts[i] != NULL; i++) {
185 if (strcmp(command_parts[i], "%i") == 0) {
186 command_args[current_index++] = g_strdup(filename);
187 } else if (strcmp(command_parts[i], "%u") == 0) {
188 command_args[current_index++] = g_strdup(encoded_uri);
189 } else if (strcmp(command_parts[i], "%o") == 0) {
190 command_args[current_index++] = g_strdup(output_path);
191 } else if (strcmp(command_parts[i], "%s") == 0) {
192 command_args[current_index++] = g_strdup_printf("%d", size);
193 } else {
194 command_args[current_index++] = g_strdup(command_parts[i]);
195 }
196 }
197
198 command_args[current_index++] = NULL;
199
200 g_strfreev(command_parts);
201 }
202
203 return command_args;
204}
205
206static gboolean exec_thumbnailer_command(gchar **command_args) {
207 // launch and wait thumbnailers process
208 gint wait_status;
209 GError *error = NULL;
210
211 gboolean spawned = g_spawn_sync(NULL, command_args, NULL,
212 G_SPAWN_DEFAULT | G_SPAWN_SEARCH_PATH, NULL,
213 NULL, NULL, NULL, &wait_status, &error);
214
215 if (spawned) {
216 return g_spawn_check_wait_status(wait_status, NULL);
217 } else {
218 g_warning("Error calling thumbnailer: %s", error->message);
219 g_error_free(error);
220
221 return FALSE;
222 }
223}
224
225static gboolean rofi_icon_fetcher_create_thumbnail(const gchar *mime_type,
226 const gchar *filename,
227 const gchar *encoded_uri,
228 const gchar *output_path,
229 int size) {
230 gboolean thumbnail_created = FALSE;
231
232 gchar *command =
233 g_hash_table_lookup(rofi_icon_fetcher_data->thumbnailers, mime_type);
234
235 if (!command) {
236 return thumbnail_created;
237 }
238
239 // split command string to isolate arguments and expand them in a list
240 gchar **command_args = setup_thumbnailer_command(
241 command, filename, encoded_uri, output_path, size);
242
243 if (command_args) {
244 thumbnail_created = exec_thumbnailer_command(command_args);
245 g_strfreev(command_args);
246 }
247
248 return thumbnail_created;
249}
250
252 IconFetcherEntry *entry = (IconFetcherEntry *)data;
253 // Mark it in a way it should be re-fetched on next query?
254 g_atomic_int_set(&entry->query_started, FALSE);
255}
256
257static void rofi_icon_fetch_entry_free(gpointer data) {
259
260 // Free name/key.
261 g_free(entry->name);
262
263 for (GList *iter = g_list_first(entry->sizes); iter;
264 iter = g_list_next(iter)) {
265 IconFetcherEntry *sentry = (IconFetcherEntry *)(iter->data);
266
267 cairo_surface_destroy(sentry->surface);
268 g_free(sentry);
269 }
270
271 g_list_free(entry->sizes);
272 g_free(entry);
273}
274
276 g_assert(rofi_icon_fetcher_data == NULL);
277
279
280 const char *themes[2] = {config.icon_theme, NULL};
281
282 rofi_icon_fetcher_data = g_malloc0(sizeof(IconFetcher));
283
284 rofi_icon_fetcher_data->xdg_context = nk_xdg_theme_context_new(
285 (const gchar *const *)icon_fallback_themes, NULL);
286 nk_xdg_theme_preload_themes_icon(rofi_icon_fetcher_data->xdg_context, themes);
287
289 g_hash_table_new(g_direct_hash, g_direct_equal);
290 rofi_icon_fetcher_data->icon_cache = g_hash_table_new_full(
291 g_str_hash, g_str_equal, NULL, rofi_icon_fetch_entry_free);
292
293 GSList *l = gdk_pixbuf_get_formats();
294 for (GSList *li = l; li != NULL; li = g_slist_next(li)) {
295 gchar **exts =
296 gdk_pixbuf_format_get_extensions((GdkPixbufFormat *)li->data);
297
298 for (unsigned int i = 0; exts && exts[i]; i++) {
300 g_list_append(rofi_icon_fetcher_data->supported_extensions, exts[i]);
301 g_info("Add image extension: %s", exts[i]);
302 exts[i] = NULL;
303 }
304
305 g_free(exts);
306 }
307 g_slist_free(l);
308
309 // load available thumbnailers from system dirs and user dir
310 rofi_icon_fetcher_data->thumbnailers = g_hash_table_new_full(
311 g_str_hash, g_str_equal, (GDestroyNotify)g_free, (GDestroyNotify)g_free);
312
313 const gchar *const *system_data_dirs = g_get_system_data_dirs();
314 const gchar *user_data_dir = g_get_user_data_dir();
315
317
318 guint i;
319 for (i = 0; system_data_dirs[i] != NULL; i++) {
320 rofi_icon_fetcher_load_thumbnailers(system_data_dirs[i]);
321 }
322}
323
324static void free_wrapper(gpointer data, G_GNUC_UNUSED gpointer user_data) {
325 g_free(data);
326}
327
329 if (rofi_icon_fetcher_data == NULL) {
330 return;
331 }
332
333 g_hash_table_unref(rofi_icon_fetcher_data->thumbnailers);
334
335 nk_xdg_theme_context_free(rofi_icon_fetcher_data->xdg_context);
336
337 g_hash_table_unref(rofi_icon_fetcher_data->icon_cache_uid);
338 g_hash_table_unref(rofi_icon_fetcher_data->icon_cache);
339
341 NULL);
344
345 // Context is free'ed above, so it should be save to deallocate.
346 g_strfreev(icon_fallback_themes);
347}
348
349/*
350 * _rofi_icon_fetcher_get_icon_surface and alpha_mult
351 * are inspired by gdk_cairo_set_source_pixbuf
352 * GDK is:
353 * Copyright (C) 2011-2018 Red Hat, Inc.
354 */
355#if G_BYTE_ORDER == G_LITTLE_ENDIAN
357#define RED_BYTE 2
359#define GREEN_BYTE 1
361#define BLUE_BYTE 0
363#define ALPHA_BYTE 3
364#else
366#define RED_BYTE 1
368#define GREEN_BYTE 2
370#define BLUE_BYTE 3
372#define ALPHA_BYTE 0
373#endif
374
375static inline guchar alpha_mult(guchar c, guchar a) {
376 guint16 t;
377 switch (a) {
378 case 0xff:
379 return c;
380 case 0x00:
381 return 0x00;
382 default:
383 t = c * a + 0x7f;
384 return ((t >> 8) + t) >> 8;
385 }
386}
387
388static cairo_surface_t *
390 gint width, height;
391 const guchar *pixels;
392 gint stride;
393 gboolean alpha;
394
395 if (pixbuf == NULL) {
396 return NULL;
397 }
398
399 width = gdk_pixbuf_get_width(pixbuf);
400 height = gdk_pixbuf_get_height(pixbuf);
401 pixels = gdk_pixbuf_read_pixels(pixbuf);
402 stride = gdk_pixbuf_get_rowstride(pixbuf);
403 alpha = gdk_pixbuf_get_has_alpha(pixbuf);
404
405 cairo_surface_t *surface = NULL;
406
407 gint cstride;
408 guint lo, o;
409 guchar a = 0xff;
410 const guchar *pixels_end, *line;
411 guchar *cpixels;
412
413 pixels_end = pixels + height * stride;
414 o = alpha ? 4 : 3;
415 lo = o * width;
416
417 surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
418 cpixels = cairo_image_surface_get_data(surface);
419 cstride = cairo_image_surface_get_stride(surface);
420
421 cairo_surface_flush(surface);
422 while (pixels < pixels_end) {
423 line = pixels;
424 const guchar *line_end = line + lo;
425 guchar *cline = cpixels;
426
427 while (line < line_end) {
428 if (alpha) {
429 a = line[3];
430 }
431 cline[RED_BYTE] = alpha_mult(line[0], a);
432 cline[GREEN_BYTE] = alpha_mult(line[1], a);
433 cline[BLUE_BYTE] = alpha_mult(line[2], a);
434 cline[ALPHA_BYTE] = a;
435
436 line += o;
437 cline += 4;
438 }
439
440 pixels += stride;
441 cpixels += cstride;
442 }
443 cairo_surface_mark_dirty(surface);
444 cairo_surface_flush(surface);
445
446 return surface;
447}
448
449gboolean rofi_icon_fetcher_file_is_image(const char *const path) {
450 if (path == NULL) {
451 return FALSE;
452 }
453 const char *suf = strrchr(path, '.');
454 if (suf == NULL) {
455 return FALSE;
456 }
457 suf++;
458
459 for (GList *iter = rofi_icon_fetcher_data->supported_extensions; iter != NULL;
460 iter = g_list_next(iter)) {
461 if (g_ascii_strcasecmp(iter->data, suf) == 0) {
462 return TRUE;
463 }
464 }
465 return FALSE;
466}
467
468// build thumbnail's path using md5 hash of an entry name
469static gchar *rofi_icon_fetcher_get_thumbnail(gchar *name, int requested_size,
470 int *thumb_size) {
471 // calc entry_name md5 hash
472 GChecksum *checksum = g_checksum_new(G_CHECKSUM_MD5);
473 g_checksum_update(checksum, (guchar *)name, -1);
474 const gchar *md5_hex = g_checksum_get_string(checksum);
475
476 // determine thumbnail folder based on the request size
477 const gchar *cache_dir = g_get_user_cache_dir();
478 gchar *thumb_dir;
479 gchar *thumb_path;
480
481 if (requested_size <= 128) {
482 *thumb_size = 128;
483 thumb_dir = g_strconcat(cache_dir, "/thumbnails/normal/", NULL);
484 thumb_path =
485 g_strconcat(cache_dir, "/thumbnails/normal/", md5_hex, ".png", NULL);
486 } else if (requested_size <= 256) {
487 *thumb_size = 256;
488 thumb_dir = g_strconcat(cache_dir, "/thumbnails/large/", NULL);
489 thumb_path =
490 g_strconcat(cache_dir, "/thumbnails/large/", md5_hex, ".png", NULL);
491 } else if (requested_size <= 512) {
492 *thumb_size = 512;
493 thumb_dir = g_strconcat(cache_dir, "/thumbnails/x-large/", NULL);
494 thumb_path =
495 g_strconcat(cache_dir, "/thumbnails/x-large/", md5_hex, ".png", NULL);
496 } else {
497 *thumb_size = 1024;
498 thumb_dir = g_strconcat(cache_dir, "/thumbnails/xx-large/", NULL);
499 thumb_path =
500 g_strconcat(cache_dir, "/thumbnails/xx-large/", md5_hex, ".png", NULL);
501 }
502
503 // create thumbnail directory if it does not exist
504 g_mkdir_with_parents(thumb_dir, 0700);
505
506 g_free(thumb_dir);
507 g_checksum_free(checksum);
508
509 return thumb_path;
510}
511
512// retrieves icon key from a .desktop file
513static gchar *rofi_icon_fetcher_get_desktop_icon(const gchar *file_path) {
514 GKeyFile *kf = g_key_file_new();
515 GError *key_error = NULL;
516 gchar *icon_key = NULL;
517
518 gboolean res = g_key_file_load_from_file(kf, file_path, 0, &key_error);
519
520 if (res) {
521 icon_key = g_key_file_get_string(kf, "Desktop Entry", "Icon", NULL);
522 } else {
523 g_debug("Failed to parse desktop file %s because: %s.", file_path,
524 key_error->message);
525
526 g_error_free(key_error);
527 }
528
529 g_key_file_free(kf);
530
531 return icon_key;
532}
533
535 G_GNUC_UNUSED gpointer user_data) {
536 g_debug("starting up icon fetching thread.");
537 // as long as dr->icon is updated atomicly.. (is a pointer write atomic?)
538 // this should be fine running in another thread.
539 IconFetcherEntry *sentry = (IconFetcherEntry *)sdata;
540 const gchar *themes[] = {config.icon_theme, NULL};
541
542 const gchar *icon_path;
543 gchar *icon_path_ = NULL;
544
545 if (g_str_has_prefix(sentry->entry->name, "thumbnail://")) {
546 // remove uri thumbnail prefix from entry name
547 gchar *entry_name = &sentry->entry->name[12];
548
549 if (strcmp(entry_name, "") == 0) {
550 g_atomic_int_set(&sentry->query_done, TRUE);
552 return;
553 }
554
555 // use custom user command to generate the thumbnail
556 if (config.preview_cmd != NULL) {
557 int requested_size = MAX(sentry->wsize, sentry->hsize);
558 int thumb_size;
559
560 icon_path = icon_path_ = rofi_icon_fetcher_get_thumbnail(
561 entry_name, requested_size, &thumb_size);
562
563 if (!g_file_test(icon_path, G_FILE_TEST_EXISTS)) {
564 char **command_args = NULL;
565 int argsv = 0;
566 gchar *size_str = g_strdup_printf("%d", thumb_size);
567
568 helper_parse_setup(config.preview_cmd, &command_args, &argsv, "{input}",
569 entry_name, "{output}", icon_path_, "{size}",
570 size_str, NULL);
571
572 g_free(size_str);
573
574 if (command_args) {
575 exec_thumbnailer_command(command_args);
576 g_strfreev(command_args);
577 }
578 }
579 } else if (g_path_is_absolute(entry_name)) {
580 // if the entry name is an absolute path try to fetch its thumbnail
581 if (g_str_has_suffix(entry_name, ".desktop")) {
582 // if the entry is a .desktop file try to read its icon key
583 gchar *icon_key = rofi_icon_fetcher_get_desktop_icon(entry_name);
584
585 if (icon_key == NULL || strlen(icon_key) == 0) {
586 // no icon in .desktop file, fallback on mimetype icon (text/plain)
587 icon_path = icon_path_ = nk_xdg_theme_get_icon(
588 rofi_icon_fetcher_data->xdg_context, themes, NULL, "text-plain",
589 MIN(sentry->wsize, sentry->hsize), 1, TRUE);
590
591 g_free(icon_key);
592 } else if (g_path_is_absolute(icon_key)) {
593 // icon in .desktop file is an absolute path to an image
594 icon_path = icon_path_ = icon_key;
595 } else {
596 // icon in .desktop file is a standard icon name
597 icon_path = icon_path_ = nk_xdg_theme_get_icon(
598 rofi_icon_fetcher_data->xdg_context, themes, NULL, icon_key,
599 MIN(sentry->wsize, sentry->hsize), 1, TRUE);
600
601 g_free(icon_key);
602 }
603 } else {
604 // build encoded uri string from absolute file path
605 gchar *encoded_uri = g_filename_to_uri(entry_name, NULL, NULL);
606 int requested_size = MAX(sentry->wsize, sentry->hsize);
607 int thumb_size;
608
609 // look for file thumbnail in appropriate folder based on requested size
610 icon_path = icon_path_ = rofi_icon_fetcher_get_thumbnail(
611 encoded_uri, requested_size, &thumb_size);
612
613 if (!g_file_test(icon_path, G_FILE_TEST_EXISTS)) {
614 // try to generate thumbnail
615 char *content_type = g_content_type_guess(entry_name, NULL, 0, NULL);
616 char *mime_type = g_content_type_get_mime_type(content_type);
617
618 if (mime_type) {
619 gboolean created = rofi_icon_fetcher_create_thumbnail(
620 mime_type, entry_name, encoded_uri, icon_path_, thumb_size);
621
622 if (!created) {
623 // replace forward slashes with minus sign to get the icon's name
624 int index = 0;
625
626 while (mime_type[index]) {
627 if (mime_type[index] == '/')
628 mime_type[index] = '-';
629 index++;
630 }
631
632 g_free(icon_path_);
633
634 // try to fetch the mime-type icon
635 icon_path = icon_path_ = nk_xdg_theme_get_icon(
636 rofi_icon_fetcher_data->xdg_context, themes, NULL, mime_type,
637 MIN(sentry->wsize, sentry->hsize), 1, TRUE);
638 }
639
640 g_free(mime_type);
641 g_free(content_type);
642 }
643 }
644
645 g_free(encoded_uri);
646 }
647 }
648
649 // no suitable icon or thumbnail was found
650 if (icon_path_ == NULL || !g_file_test(icon_path, G_FILE_TEST_EXISTS)) {
651 sentry->query_done = TRUE;
653 return;
654 }
655 } else if (g_path_is_absolute(sentry->entry->name)) {
656 icon_path = sentry->entry->name;
657 } else if (g_str_has_prefix(sentry->entry->name, "<span")) {
658 cairo_surface_t *surface = cairo_image_surface_create(
659 CAIRO_FORMAT_ARGB32, sentry->wsize, sentry->hsize);
660 cairo_t *cr = cairo_create(surface);
661 PangoLayout *layout = pango_cairo_create_layout(cr);
662 pango_layout_set_markup(layout, sentry->entry->name, -1);
663
664 int width, height;
665 pango_layout_get_size(layout, &width, &height);
666 double ws = sentry->wsize / ((double)width / PANGO_SCALE);
667 double wh = sentry->hsize / ((double)height / PANGO_SCALE);
668 double scale = MIN(ws, wh);
669
670 cairo_move_to(
671 cr, (sentry->wsize - ((double)width / PANGO_SCALE) * scale) / 2.0,
672 (sentry->hsize - ((double)height / PANGO_SCALE) * scale) / 2.0);
673 cairo_scale(cr, scale, scale);
674 pango_cairo_update_layout(cr, layout);
675 pango_layout_get_size(layout, &width, &height);
676 pango_cairo_show_layout(cr, layout);
677 g_object_unref(layout);
678 cairo_destroy(cr);
679 // force ordering.
680 g_atomic_pointer_set(&sentry->surface, surface);
681 g_atomic_int_set(&sentry->query_done, TRUE);
683 return;
684
685 } else {
686 icon_path = icon_path_ = nk_xdg_theme_get_icon(
687 rofi_icon_fetcher_data->xdg_context, themes, NULL, sentry->entry->name,
688 MIN(sentry->wsize, sentry->hsize), sentry->scale, TRUE);
689 if (icon_path_ == NULL) {
690 g_debug("failed to get icon %s(%dx%d): n/a", sentry->entry->name,
691 sentry->wsize, sentry->hsize);
692
693 const char *ext = g_strrstr(sentry->entry->name, ".");
694 if (ext) {
695 const char *exts2[2] = {ext, NULL};
696 icon_path = icon_path_ =
697 helper_get_theme_path(sentry->entry->name, exts2, NULL);
698 }
699 if (icon_path_ == NULL) {
700 g_atomic_int_set(&sentry->query_done, TRUE);
702 return;
703 }
704 } else {
705 g_debug("found icon %s(%dx%d): %s", sentry->entry->name, sentry->wsize,
706 sentry->hsize, icon_path);
707 }
708 }
709 cairo_surface_t *icon_surf = NULL;
710
711#if 0 // unsure why added in past?
712 const char *suf = strrchr(icon_path, '.');
713 if (suf == NULL) {
714 sentry->query_done = TRUE;
715 g_free(icon_path_);
717 return;
718 }
719#endif
720
721 int width = sentry->wsize, height = sentry->hsize;
722 if (width > 0)
723 width *= sentry->scale;
724 if (height > 0)
725 height *= sentry->scale;
726
727 GError *error = NULL;
728 GdkPixbuf *pb =
729 gdk_pixbuf_new_from_file_at_scale(icon_path, width, height, TRUE, &error);
730
731 /*
732 * The GIF codec throws GDK_PIXBUF_ERROR_INCOMPLETE_ANIMATION if it's closed
733 * without decoding all the frames. Since gdk_pixbuf_new_from_file_at_scale
734 * only decodes the first frame, this specific error needs to be ignored.
735 */
736 if (error != NULL && g_error_matches(error, GDK_PIXBUF_ERROR,
737 GDK_PIXBUF_ERROR_INCOMPLETE_ANIMATION)) {
738 g_clear_error(&error);
739 }
740
741 if (error != NULL) {
742 g_warning("Failed to load image: |%s| %d %d %s (%p)", icon_path,
743 sentry->wsize, sentry->hsize, error->message, (void *)pb);
744 g_error_free(error);
745 if (pb) {
746 g_object_unref(pb);
747 }
748 } else {
750 g_object_unref(pb);
751 }
752
753 // Force ordering.
754 g_atomic_pointer_set(&sentry->surface, icon_surf);
755 g_atomic_int_set(&sentry->query_done, TRUE);
756 g_free(icon_path_);
758}
759
760uint32_t rofi_icon_fetcher_query_advanced(const char *name, const int wsize,
761 const int hsize) {
762 g_debug("Query: %s(%dx%d)", name, wsize, hsize);
763 IconFetcherNameEntry *entry =
764 g_hash_table_lookup(rofi_icon_fetcher_data->icon_cache, name);
765 if (entry == NULL) {
766 entry = g_new0(IconFetcherNameEntry, 1);
767 entry->name = g_strdup(name);
768 g_hash_table_insert(rofi_icon_fetcher_data->icon_cache, entry->name, entry);
769 }
770 IconFetcherEntry *sentry;
771 const guint scale = display_scale();
772 for (GList *iter = g_list_first(entry->sizes); iter;
773 iter = g_list_next(iter)) {
774 sentry = iter->data;
775 if (sentry->wsize == wsize && sentry->hsize == hsize &&
776 sentry->scale == scale) {
777 if (!g_atomic_int_get(&sentry->query_started)) {
778 g_atomic_int_set(&sentry->query_started, TRUE);
779 g_thread_pool_push(tpool, sentry, NULL);
780 }
781 return sentry->uid;
782 }
783 }
784
785 // Not found.
786 sentry = g_new0(IconFetcherEntry, 1);
787 sentry->uid = ++(rofi_icon_fetcher_data->last_uid);
788 sentry->wsize = wsize;
789 sentry->hsize = hsize;
790 sentry->scale = scale;
791 sentry->entry = entry;
792 sentry->query_done = FALSE;
793 g_atomic_int_set(&sentry->query_started, TRUE);
794 sentry->surface = NULL;
795
796 entry->sizes = g_list_prepend(entry->sizes, sentry);
797 g_hash_table_insert(rofi_icon_fetcher_data->icon_cache_uid,
798 GINT_TO_POINTER(sentry->uid), sentry);
799
800 // Push into fetching queue.
803 sentry->state.priority = G_PRIORITY_LOW;
804 g_thread_pool_push(tpool, sentry, NULL);
805
806 return sentry->uid;
807}
808uint32_t rofi_icon_fetcher_query(const char *name, const int size) {
809 g_debug("Query: %s(%d)", name, size);
810 IconFetcherNameEntry *entry =
811 g_hash_table_lookup(rofi_icon_fetcher_data->icon_cache, name);
812 if (entry == NULL) {
813 entry = g_new0(IconFetcherNameEntry, 1);
814 entry->name = g_strdup(name);
815 g_hash_table_insert(rofi_icon_fetcher_data->icon_cache, entry->name, entry);
816 }
817 IconFetcherEntry *sentry;
818 const guint scale = display_scale();
819 for (GList *iter = g_list_first(entry->sizes); iter;
820 iter = g_list_next(iter)) {
821 sentry = iter->data;
822 if (sentry->wsize == size && sentry->hsize == size &&
823 sentry->scale == scale) {
824 if (!g_atomic_int_get(&sentry->query_started)) {
825 g_atomic_int_set(&sentry->query_started, TRUE);
826 g_thread_pool_push(tpool, sentry, NULL);
827 }
828 return sentry->uid;
829 }
830 }
831
832 // Not found.
833 sentry = g_new0(IconFetcherEntry, 1);
834 sentry->uid = ++(rofi_icon_fetcher_data->last_uid);
835 sentry->wsize = size;
836 sentry->hsize = size;
837 sentry->scale = scale;
838 sentry->entry = entry;
839 sentry->query_done = FALSE;
840 g_atomic_int_set(&sentry->query_started, TRUE);
841 sentry->surface = NULL;
842
843 entry->sizes = g_list_prepend(entry->sizes, sentry);
844 g_hash_table_insert(rofi_icon_fetcher_data->icon_cache_uid,
845 GINT_TO_POINTER(sentry->uid), sentry);
846
847 // Push into fetching queue.
850 sentry->state.priority = G_PRIORITY_LOW;
851 g_thread_pool_push(tpool, sentry, NULL);
852
853 return sentry->uid;
854}
855
856cairo_surface_t *rofi_icon_fetcher_get(const uint32_t uid) {
857 IconFetcherEntry *sentry = g_hash_table_lookup(
858 rofi_icon_fetcher_data->icon_cache_uid, GINT_TO_POINTER(uid));
859 if (sentry) {
860 return sentry->surface;
861 }
862 g_warning("Querying an non-existing uid");
863 return NULL;
864}
865
866gboolean rofi_icon_fetcher_get_ex(const uint32_t uid,
867 cairo_surface_t **surface) {
868 IconFetcherEntry *sentry = g_hash_table_lookup(
869 rofi_icon_fetcher_data->icon_cache_uid, GINT_TO_POINTER(uid));
870 *surface = NULL;
871 if (sentry) {
872 if (g_atomic_int_get(&sentry->query_done)) {
873 *surface = g_atomic_pointer_get(&sentry->surface);
874 return TRUE;
875 }
876 // If query not started, re-start it.
877 if (!g_atomic_int_get(&sentry->query_started)) {
878 g_info("Restart query: %u", uid);
879 g_atomic_int_set(&sentry->query_started, TRUE);
880 g_thread_pool_push(tpool, sentry, NULL);
881 }
882 *surface = g_atomic_pointer_get(&sentry->surface);
883 return FALSE;
884 }
885 g_warning("Querying an non-existing uid");
886 return FALSE;
887}
guint display_scale(void)
Definition display.c:78
int helper_parse_setup(char *string, char ***output, int *length,...)
Definition helper.c:102
uint32_t rofi_icon_fetcher_query_advanced(const char *name, const int wsize, const int hsize)
gboolean rofi_icon_fetcher_file_is_image(const char *const path)
cairo_surface_t * rofi_icon_fetcher_get(const uint32_t uid)
void rofi_icon_fetcher_destroy(void)
gboolean rofi_icon_fetcher_get_ex(const uint32_t uid, cairo_surface_t **surface)
uint32_t rofi_icon_fetcher_query(const char *name, const int size)
void rofi_icon_fetcher_init(void)
const char * cache_dir
Definition rofi.c:86
void rofi_view_reload(void)
Definition view.c:2221
char * helper_get_theme_path(const char *file, const char **ext, const char *parent_file)
Definition helper.c:1590
static void rofi_icon_fetcher_load_thumbnailers(const gchar *path)
static void rofi_icon_fetch_entry_free(gpointer data)
static void free_wrapper(gpointer data, G_GNUC_UNUSED gpointer user_data)
IconFetcher * rofi_icon_fetcher_data
static gboolean exec_thumbnailer_command(gchar **command_args)
#define ALPHA_BYTE
#define BLUE_BYTE
static guchar alpha_mult(guchar c, guchar a)
#define GREEN_BYTE
static gboolean rofi_icon_fetcher_create_thumbnail(const gchar *mime_type, const gchar *filename, const gchar *encoded_uri, const gchar *output_path, int size)
static gchar ** setup_thumbnailer_command(const gchar *command, const gchar *filename, const gchar *encoded_uri, const gchar *output_path, int size)
static cairo_surface_t * rofi_icon_fetcher_get_surface_from_pixbuf(GdkPixbuf *pixbuf)
static gchar * rofi_icon_fetcher_get_desktop_icon(const gchar *file_path)
static void rofi_icon_fetch_thread_pool_entry_remove(gpointer data)
static void rofi_icon_fetcher_worker(thread_state *sdata, G_GNUC_UNUSED gpointer user_data)
static gchar * rofi_icon_fetcher_get_thumbnail(gchar *name, int requested_size, int *thumb_size)
char ** icon_fallback_themes
#define RED_BYTE
#define THUMBNAILER_EXTENSION
#define THUMBNAILER_ENTRY_GROUP
struct _thread_state thread_state
Settings config
IconFetcherNameEntry * entry
unsigned int * acount
cairo_surface_t * surface
NkXdgThemeContext * xdg_context
GHashTable * icon_cache_uid
GList * supported_extensions
GHashTable * thumbnailers
GHashTable * icon_cache
char * preview_cmd
Definition settings.h:71
char * fallback_icon_themes
Definition settings.h:227
char * icon_theme
Definition settings.h:102
void(*) callback(struct _thread_state *t, gpointer data)
Definition rofi-types.h:368
void(*) free(void *)
Definition rofi-types.h:369
GThreadPool * tpool
Definition view.c:70