2018-06-09 17:13:57 +00:00
|
|
|
/*
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2023-01-14 12:02:35 +00:00
|
|
|
* Copyright © 2013-2023 Qball Cow <qball@gmpclient.org>
|
2018-06-09 17:13:57 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-12 09:45:42 +00:00
|
|
|
/** The log domain of this Helper. */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define G_LOG_DOMAIN "Helpers.IconFetcher"
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 23:16:45 +00:00
|
|
|
#include "config.h"
|
2021-08-17 22:32:04 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <xcb/xproto.h>
|
2020-10-18 19:37:05 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
#include "helper.h"
|
2018-06-09 17:13:57 +00:00
|
|
|
#include "rofi-icon-fetcher.h"
|
|
|
|
#include "rofi-types.h"
|
2018-06-09 20:17:32 +00:00
|
|
|
#include "settings.h"
|
2022-02-23 21:42:56 +00:00
|
|
|
#include <cairo.h>
|
|
|
|
#include <pango/pangocairo.h>
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2018-06-12 09:28:31 +00:00
|
|
|
#include "keyb.h"
|
|
|
|
#include "view.h"
|
2021-08-17 22:32:04 +00:00
|
|
|
#include "xcb.h"
|
2018-06-12 09:28:31 +00:00
|
|
|
|
2020-10-18 20:01:49 +00:00
|
|
|
#include "nkutils-enum.h"
|
2021-08-17 22:32:04 +00:00
|
|
|
#include "nkutils-xdg-theme.h"
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2020-02-14 13:27:03 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2021-08-17 09:05:03 +00:00
|
|
|
#include "helper.h"
|
2021-08-17 22:32:04 +00:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2020-10-17 19:59:12 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
typedef struct {
|
|
|
|
// Context for icon-themes.
|
|
|
|
NkXdgThemeContext *xdg_context;
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
// On name.
|
|
|
|
GHashTable *icon_cache;
|
|
|
|
// On uid.
|
|
|
|
GHashTable *icon_cache_uid;
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
// list extensions
|
|
|
|
GList *supported_extensions;
|
|
|
|
uint32_t last_uid;
|
2018-06-09 17:13:57 +00:00
|
|
|
} IconFetcher;
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
typedef struct {
|
|
|
|
char *name;
|
|
|
|
GList *sizes;
|
2018-06-09 17:13:57 +00:00
|
|
|
} IconFetcherNameEntry;
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
typedef struct {
|
|
|
|
thread_state state;
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
GCond *cond;
|
|
|
|
GMutex *mutex;
|
|
|
|
unsigned int *acount;
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
uint32_t uid;
|
|
|
|
int wsize;
|
|
|
|
int hsize;
|
|
|
|
cairo_surface_t *surface;
|
2022-10-22 09:03:11 +00:00
|
|
|
gboolean query_done;
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
IconFetcherNameEntry *entry;
|
2018-06-09 17:13:57 +00:00
|
|
|
} IconFetcherEntry;
|
|
|
|
|
2018-06-12 12:11:08 +00:00
|
|
|
/**
|
|
|
|
* The icon fetcher internal state.
|
|
|
|
*/
|
2018-06-09 17:13:57 +00:00
|
|
|
IconFetcher *rofi_icon_fetcher_data = NULL;
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
static void rofi_icon_fetch_entry_free(gpointer data) {
|
|
|
|
IconFetcherNameEntry *entry = (IconFetcherNameEntry *)data;
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
// Free name/key.
|
|
|
|
g_free(entry->name);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
for (GList *iter = g_list_first(entry->sizes); iter;
|
|
|
|
iter = g_list_next(iter)) {
|
|
|
|
IconFetcherEntry *sentry = (IconFetcherEntry *)(iter->data);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
cairo_surface_destroy(sentry->surface);
|
|
|
|
g_free(sentry);
|
|
|
|
}
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
g_list_free(entry->sizes);
|
|
|
|
g_free(entry);
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
void rofi_icon_fetcher_init(void) {
|
|
|
|
g_assert(rofi_icon_fetcher_data == NULL);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
static const gchar *const icon_fallback_themes[] = {"Adwaita", "gnome", NULL};
|
|
|
|
const char *themes[2] = {config.icon_theme, NULL};
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
rofi_icon_fetcher_data = g_malloc0(sizeof(IconFetcher));
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
rofi_icon_fetcher_data->xdg_context =
|
|
|
|
nk_xdg_theme_context_new(icon_fallback_themes, NULL);
|
|
|
|
nk_xdg_theme_preload_themes_icon(rofi_icon_fetcher_data->xdg_context, themes);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
rofi_icon_fetcher_data->icon_cache_uid =
|
|
|
|
g_hash_table_new(g_direct_hash, g_direct_equal);
|
|
|
|
rofi_icon_fetcher_data->icon_cache = g_hash_table_new_full(
|
|
|
|
g_str_hash, g_str_equal, NULL, rofi_icon_fetch_entry_free);
|
2020-10-20 18:38:13 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
GSList *l = gdk_pixbuf_get_formats();
|
|
|
|
for (GSList *li = l; li != NULL; li = g_slist_next(li)) {
|
|
|
|
gchar **exts =
|
|
|
|
gdk_pixbuf_format_get_extensions((GdkPixbufFormat *)li->data);
|
2020-10-20 18:38:13 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
for (unsigned int i = 0; exts && exts[i]; i++) {
|
|
|
|
rofi_icon_fetcher_data->supported_extensions =
|
|
|
|
g_list_append(rofi_icon_fetcher_data->supported_extensions, exts[i]);
|
|
|
|
g_info("Add image extension: %s", exts[i]);
|
|
|
|
exts[i] = NULL;
|
2020-10-20 18:38:13 +00:00
|
|
|
}
|
2021-08-17 22:32:04 +00:00
|
|
|
|
|
|
|
g_free(exts);
|
|
|
|
}
|
|
|
|
g_slist_free(l);
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
static void free_wrapper(gpointer data, G_GNUC_UNUSED gpointer user_data) {
|
|
|
|
g_free(data);
|
2021-06-08 20:33:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
void rofi_icon_fetcher_destroy(void) {
|
|
|
|
if (rofi_icon_fetcher_data == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
nk_xdg_theme_context_free(rofi_icon_fetcher_data->xdg_context);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
g_hash_table_unref(rofi_icon_fetcher_data->icon_cache_uid);
|
|
|
|
g_hash_table_unref(rofi_icon_fetcher_data->icon_cache);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
g_list_foreach(rofi_icon_fetcher_data->supported_extensions, free_wrapper,
|
|
|
|
NULL);
|
|
|
|
g_list_free(rofi_icon_fetcher_data->supported_extensions);
|
|
|
|
g_free(rofi_icon_fetcher_data);
|
2020-02-14 13:27:03 +00:00
|
|
|
}
|
2020-10-17 19:59:12 +00:00
|
|
|
|
2020-10-20 18:38:13 +00:00
|
|
|
/*
|
|
|
|
* _rofi_icon_fetcher_get_icon_surface and alpha_mult
|
|
|
|
* are inspired by gdk_cairo_set_source_pixbuf
|
|
|
|
* GDK is:
|
|
|
|
* Copyright (C) 2011-2018 Red Hat, Inc.
|
|
|
|
*/
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of red byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define RED_BYTE 2
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of green byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define GREEN_BYTE 1
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of blue byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define BLUE_BYTE 0
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of alpha byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define ALPHA_BYTE 3
|
2020-10-20 18:38:13 +00:00
|
|
|
#else
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of red byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define RED_BYTE 1
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of green byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define GREEN_BYTE 2
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of blue byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define BLUE_BYTE 3
|
2021-05-05 19:53:29 +00:00
|
|
|
/** Location of alpha byte */
|
2021-08-17 22:32:04 +00:00
|
|
|
#define ALPHA_BYTE 0
|
2020-10-20 18:38:13 +00:00
|
|
|
#endif
|
2020-02-14 13:27:03 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
static inline guchar alpha_mult(guchar c, guchar a) {
|
|
|
|
guint16 t;
|
|
|
|
switch (a) {
|
|
|
|
case 0xff:
|
|
|
|
return c;
|
|
|
|
case 0x00:
|
|
|
|
return 0x00;
|
|
|
|
default:
|
|
|
|
t = c * a + 0x7f;
|
|
|
|
return ((t >> 8) + t) >> 8;
|
|
|
|
}
|
2020-02-14 13:27:03 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
static cairo_surface_t *
|
|
|
|
rofi_icon_fetcher_get_surface_from_pixbuf(GdkPixbuf *pixbuf) {
|
|
|
|
gint width, height;
|
|
|
|
const guchar *pixels;
|
|
|
|
gint stride;
|
|
|
|
gboolean alpha;
|
2020-11-03 22:57:02 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
if (pixbuf == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
width = gdk_pixbuf_get_width(pixbuf);
|
|
|
|
height = gdk_pixbuf_get_height(pixbuf);
|
|
|
|
pixels = gdk_pixbuf_read_pixels(pixbuf);
|
|
|
|
stride = gdk_pixbuf_get_rowstride(pixbuf);
|
|
|
|
alpha = gdk_pixbuf_get_has_alpha(pixbuf);
|
|
|
|
|
|
|
|
cairo_surface_t *surface = NULL;
|
|
|
|
|
|
|
|
gint cstride;
|
|
|
|
guint lo, o;
|
|
|
|
guchar a = 0xff;
|
|
|
|
const guchar *pixels_end, *line;
|
|
|
|
guchar *cpixels;
|
|
|
|
|
|
|
|
pixels_end = pixels + height * stride;
|
|
|
|
o = alpha ? 4 : 3;
|
|
|
|
lo = o * width;
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
|
|
|
cpixels = cairo_image_surface_get_data(surface);
|
|
|
|
cstride = cairo_image_surface_get_stride(surface);
|
|
|
|
|
|
|
|
cairo_surface_flush(surface);
|
|
|
|
while (pixels < pixels_end) {
|
|
|
|
line = pixels;
|
|
|
|
const guchar *line_end = line + lo;
|
|
|
|
guchar *cline = cpixels;
|
|
|
|
|
|
|
|
while (line < line_end) {
|
|
|
|
if (alpha) {
|
|
|
|
a = line[3];
|
|
|
|
}
|
|
|
|
cline[RED_BYTE] = alpha_mult(line[0], a);
|
|
|
|
cline[GREEN_BYTE] = alpha_mult(line[1], a);
|
|
|
|
cline[BLUE_BYTE] = alpha_mult(line[2], a);
|
|
|
|
cline[ALPHA_BYTE] = a;
|
|
|
|
|
|
|
|
line += o;
|
|
|
|
cline += 4;
|
2020-10-18 19:37:05 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
pixels += stride;
|
|
|
|
cpixels += cstride;
|
|
|
|
}
|
|
|
|
cairo_surface_mark_dirty(surface);
|
|
|
|
cairo_surface_flush(surface);
|
2020-10-18 19:37:05 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
return surface;
|
2020-10-18 19:37:05 +00:00
|
|
|
}
|
2020-10-18 20:01:49 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
gboolean rofi_icon_fetcher_file_is_image(const char *const path) {
|
|
|
|
if (path == NULL) {
|
2020-10-20 18:38:13 +00:00
|
|
|
return FALSE;
|
2021-08-17 22:32:04 +00:00
|
|
|
}
|
|
|
|
const char *suf = strrchr(path, '.');
|
|
|
|
if (suf == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
suf++;
|
2020-10-18 20:01:49 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
for (GList *iter = rofi_icon_fetcher_data->supported_extensions; iter != NULL;
|
|
|
|
iter = g_list_next(iter)) {
|
|
|
|
if (g_ascii_strcasecmp(iter->data, suf) == 0) {
|
|
|
|
return TRUE;
|
2020-11-03 22:57:02 +00:00
|
|
|
}
|
2021-08-17 22:32:04 +00:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2020-11-03 22:57:02 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
static void rofi_icon_fetcher_worker(thread_state *sdata,
|
|
|
|
G_GNUC_UNUSED gpointer user_data) {
|
|
|
|
g_debug("starting up icon fetching thread.");
|
|
|
|
// as long as dr->icon is updated atomicly.. (is a pointer write atomic?)
|
|
|
|
// this should be fine running in another thread.
|
|
|
|
IconFetcherEntry *sentry = (IconFetcherEntry *)sdata;
|
|
|
|
const gchar *themes[] = {config.icon_theme, NULL};
|
|
|
|
|
|
|
|
const gchar *icon_path;
|
|
|
|
gchar *icon_path_ = NULL;
|
|
|
|
|
|
|
|
if (g_path_is_absolute(sentry->entry->name)) {
|
|
|
|
icon_path = sentry->entry->name;
|
2022-07-16 11:13:29 +00:00
|
|
|
} else if (g_str_has_prefix(sentry->entry->name, "<span")) {
|
2022-02-23 21:42:56 +00:00
|
|
|
cairo_surface_t *surface = cairo_image_surface_create(
|
|
|
|
CAIRO_FORMAT_ARGB32, sentry->wsize, sentry->hsize);
|
|
|
|
cairo_t *cr = cairo_create(surface);
|
|
|
|
PangoLayout *layout = pango_cairo_create_layout(cr);
|
2022-07-16 11:13:29 +00:00
|
|
|
pango_layout_set_markup(layout, sentry->entry->name, -1);
|
2022-02-23 21:42:56 +00:00
|
|
|
|
|
|
|
int width, height;
|
|
|
|
pango_layout_get_size(layout, &width, &height);
|
|
|
|
double ws = sentry->wsize / ((double)width / PANGO_SCALE);
|
|
|
|
double wh = sentry->hsize / ((double)height / PANGO_SCALE);
|
|
|
|
double scale = MIN(ws, wh);
|
|
|
|
|
|
|
|
cairo_move_to(
|
|
|
|
cr, (sentry->wsize - ((double)width / PANGO_SCALE) * scale) / 2.0,
|
|
|
|
(sentry->hsize - ((double)height / PANGO_SCALE) * scale) / 2.0);
|
|
|
|
cairo_scale(cr, scale, scale);
|
|
|
|
pango_cairo_update_layout(cr, layout);
|
|
|
|
pango_layout_get_size(layout, &width, &height);
|
|
|
|
pango_cairo_show_layout(cr, layout);
|
|
|
|
g_object_unref(layout);
|
|
|
|
cairo_destroy(cr);
|
|
|
|
sentry->surface = surface;
|
2022-10-22 09:03:11 +00:00
|
|
|
sentry->query_done = TRUE;
|
2022-02-23 21:42:56 +00:00
|
|
|
rofi_view_reload();
|
|
|
|
return;
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
} else {
|
|
|
|
icon_path = icon_path_ = nk_xdg_theme_get_icon(
|
|
|
|
rofi_icon_fetcher_data->xdg_context, themes, NULL, sentry->entry->name,
|
|
|
|
MIN(sentry->wsize, sentry->hsize), 1, TRUE);
|
|
|
|
if (icon_path_ == NULL) {
|
|
|
|
g_debug("failed to get icon %s(%dx%d): n/a", sentry->entry->name,
|
|
|
|
sentry->wsize, sentry->hsize);
|
|
|
|
|
|
|
|
const char *ext = g_strrstr(sentry->entry->name, ".");
|
|
|
|
if (ext) {
|
2022-12-30 14:01:17 +00:00
|
|
|
const char *exts2[2] = {ext, NULL};
|
2023-04-21 12:32:56 +00:00
|
|
|
icon_path = icon_path_ =
|
2023-08-15 17:12:14 +00:00
|
|
|
helper_get_theme_path(sentry->entry->name, exts2, NULL);
|
2021-08-17 22:32:04 +00:00
|
|
|
}
|
2023-04-21 12:32:56 +00:00
|
|
|
if (icon_path_ == NULL) {
|
2022-10-22 09:03:11 +00:00
|
|
|
sentry->query_done = TRUE;
|
|
|
|
rofi_view_reload();
|
2020-11-03 22:57:02 +00:00
|
|
|
return;
|
2021-08-17 22:32:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
g_debug("found icon %s(%dx%d): %s", sentry->entry->name, sentry->wsize,
|
|
|
|
sentry->hsize, icon_path);
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
2021-08-17 22:32:04 +00:00
|
|
|
}
|
|
|
|
cairo_surface_t *icon_surf = NULL;
|
|
|
|
|
|
|
|
const char *suf = strrchr(icon_path, '.');
|
|
|
|
if (suf == NULL) {
|
2022-10-22 09:03:11 +00:00
|
|
|
sentry->query_done = TRUE;
|
2023-04-21 10:50:55 +00:00
|
|
|
g_free(icon_path_);
|
2022-10-22 09:03:11 +00:00
|
|
|
rofi_view_reload();
|
2021-08-17 22:32:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GError *error = NULL;
|
|
|
|
GdkPixbuf *pb = gdk_pixbuf_new_from_file_at_scale(
|
|
|
|
icon_path, sentry->wsize, sentry->hsize, TRUE, &error);
|
|
|
|
if (error != NULL) {
|
2023-11-16 14:25:40 +00:00
|
|
|
g_warning("Failed to load image: |%s| %d %d %s (%p)", icon_path,
|
|
|
|
sentry->wsize, sentry->hsize, error->message, pb);
|
2021-08-17 22:32:04 +00:00
|
|
|
g_error_free(error);
|
|
|
|
if (pb) {
|
|
|
|
g_object_unref(pb);
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
2021-08-17 22:32:04 +00:00
|
|
|
} else {
|
|
|
|
icon_surf = rofi_icon_fetcher_get_surface_from_pixbuf(pb);
|
|
|
|
g_object_unref(pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
sentry->surface = icon_surf;
|
|
|
|
g_free(icon_path_);
|
2022-10-22 09:03:11 +00:00
|
|
|
sentry->query_done = TRUE;
|
2021-08-17 22:32:04 +00:00
|
|
|
rofi_view_reload();
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
uint32_t rofi_icon_fetcher_query_advanced(const char *name, const int wsize,
|
|
|
|
const int hsize) {
|
|
|
|
g_debug("Query: %s(%dx%d)", name, wsize, hsize);
|
|
|
|
IconFetcherNameEntry *entry =
|
|
|
|
g_hash_table_lookup(rofi_icon_fetcher_data->icon_cache, name);
|
|
|
|
if (entry == NULL) {
|
|
|
|
entry = g_new0(IconFetcherNameEntry, 1);
|
|
|
|
entry->name = g_strdup(name);
|
|
|
|
g_hash_table_insert(rofi_icon_fetcher_data->icon_cache, entry->name, entry);
|
|
|
|
}
|
|
|
|
IconFetcherEntry *sentry;
|
|
|
|
for (GList *iter = g_list_first(entry->sizes); iter;
|
|
|
|
iter = g_list_next(iter)) {
|
|
|
|
sentry = iter->data;
|
|
|
|
if (sentry->wsize == wsize && sentry->hsize == hsize) {
|
|
|
|
return sentry->uid;
|
2021-06-15 15:18:34 +00:00
|
|
|
}
|
2021-08-17 22:32:04 +00:00
|
|
|
}
|
2021-06-15 15:18:34 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
// Not found.
|
|
|
|
sentry = g_new0(IconFetcherEntry, 1);
|
|
|
|
sentry->uid = ++(rofi_icon_fetcher_data->last_uid);
|
|
|
|
sentry->wsize = wsize;
|
|
|
|
sentry->hsize = hsize;
|
|
|
|
sentry->entry = entry;
|
2022-10-22 09:03:11 +00:00
|
|
|
sentry->query_done = FALSE;
|
2021-08-17 22:32:04 +00:00
|
|
|
sentry->surface = NULL;
|
2021-06-15 15:18:34 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
entry->sizes = g_list_prepend(entry->sizes, sentry);
|
|
|
|
g_hash_table_insert(rofi_icon_fetcher_data->icon_cache_uid,
|
|
|
|
GINT_TO_POINTER(sentry->uid), sentry);
|
2021-06-15 15:18:34 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
// Push into fetching queue.
|
|
|
|
sentry->state.callback = rofi_icon_fetcher_worker;
|
|
|
|
g_thread_pool_push(tpool, sentry, NULL);
|
2021-06-15 15:18:34 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
return sentry->uid;
|
2021-06-15 15:18:34 +00:00
|
|
|
}
|
2021-08-17 22:32:04 +00:00
|
|
|
uint32_t rofi_icon_fetcher_query(const char *name, const int size) {
|
|
|
|
g_debug("Query: %s(%d)", name, size);
|
|
|
|
IconFetcherNameEntry *entry =
|
|
|
|
g_hash_table_lookup(rofi_icon_fetcher_data->icon_cache, name);
|
|
|
|
if (entry == NULL) {
|
|
|
|
entry = g_new0(IconFetcherNameEntry, 1);
|
|
|
|
entry->name = g_strdup(name);
|
|
|
|
g_hash_table_insert(rofi_icon_fetcher_data->icon_cache, entry->name, entry);
|
|
|
|
}
|
|
|
|
IconFetcherEntry *sentry;
|
|
|
|
for (GList *iter = g_list_first(entry->sizes); iter;
|
|
|
|
iter = g_list_next(iter)) {
|
|
|
|
sentry = iter->data;
|
|
|
|
if (sentry->wsize == size && sentry->hsize == size) {
|
|
|
|
return sentry->uid;
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
2021-08-17 22:32:04 +00:00
|
|
|
}
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
// Not found.
|
|
|
|
sentry = g_new0(IconFetcherEntry, 1);
|
|
|
|
sentry->uid = ++(rofi_icon_fetcher_data->last_uid);
|
|
|
|
sentry->wsize = size;
|
|
|
|
sentry->hsize = size;
|
|
|
|
sentry->entry = entry;
|
|
|
|
sentry->surface = NULL;
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
entry->sizes = g_list_prepend(entry->sizes, sentry);
|
|
|
|
g_hash_table_insert(rofi_icon_fetcher_data->icon_cache_uid,
|
|
|
|
GINT_TO_POINTER(sentry->uid), sentry);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
// Push into fetching queue.
|
|
|
|
sentry->state.callback = rofi_icon_fetcher_worker;
|
|
|
|
g_thread_pool_push(tpool, sentry, NULL);
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
return sentry->uid;
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 22:32:04 +00:00
|
|
|
cairo_surface_t *rofi_icon_fetcher_get(const uint32_t uid) {
|
|
|
|
IconFetcherEntry *sentry = g_hash_table_lookup(
|
|
|
|
rofi_icon_fetcher_data->icon_cache_uid, GINT_TO_POINTER(uid));
|
|
|
|
if (sentry) {
|
|
|
|
return sentry->surface;
|
|
|
|
}
|
2022-10-22 09:03:11 +00:00
|
|
|
g_warning("Querying an non-existing uid");
|
2021-08-17 22:32:04 +00:00
|
|
|
return NULL;
|
2018-06-09 17:13:57 +00:00
|
|
|
}
|
2022-10-22 09:03:11 +00:00
|
|
|
|
|
|
|
gboolean rofi_icon_fetcher_get_ex(const uint32_t uid,
|
|
|
|
cairo_surface_t **surface) {
|
|
|
|
IconFetcherEntry *sentry = g_hash_table_lookup(
|
|
|
|
rofi_icon_fetcher_data->icon_cache_uid, GINT_TO_POINTER(uid));
|
|
|
|
*surface = NULL;
|
|
|
|
if (sentry) {
|
|
|
|
*surface = sentry->surface;
|
|
|
|
return sentry->query_done;
|
|
|
|
}
|
|
|
|
g_warning("Querying an non-existing uid");
|
|
|
|
return FALSE;
|
|
|
|
}
|