2023-07-03 18:48:38 +02:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
|
2023-08-07 06:37:38 +02:00
|
|
|
#if IMAGE
|
2023-08-07 05:54:59 +02:00
|
|
|
static int draw_icon(struct item *item, int x, int y, int w, int h);
|
|
|
|
|
2023-07-03 18:48:38 +02:00
|
|
|
int draw_icon(struct item *item, int x, int y, int w, int h) {
|
|
|
|
int ich = 0;
|
|
|
|
int icw = 0;
|
|
|
|
|
2023-07-10 20:38:25 +02:00
|
|
|
// cache
|
|
|
|
int slen = 0, i;
|
|
|
|
unsigned int digest_len = EVP_MD_size(EVP_md5());
|
|
|
|
unsigned char *digest = (unsigned char *)OPENSSL_malloc(digest_len);
|
2023-07-16 02:57:38 +02:00
|
|
|
char *xdg_cache, *home = NULL, *cachesize, *buf = NULL;
|
|
|
|
struct passwd *user_id = NULL;
|
2023-07-10 20:38:25 +02:00
|
|
|
|
|
|
|
if (hideimage) {
|
|
|
|
return x;
|
|
|
|
}
|
2023-07-03 18:48:38 +02:00
|
|
|
|
|
|
|
if (item->image) {
|
2023-07-10 20:38:25 +02:00
|
|
|
if (generatecache) {
|
|
|
|
if(!(xdg_cache = getenv("XDG_CACHE_HOME"))) {
|
2023-07-16 02:57:38 +02:00
|
|
|
if(!(home = getenv("HOME")) && (user_id = getpwuid(getuid()))) {
|
|
|
|
home = user_id->pw_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!home) { // no home directory.. somehow
|
2023-07-10 20:38:25 +02:00
|
|
|
fprintf(stderr, "spmenu: could not find home directory");
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
cachesize = "icon";
|
2023-07-10 20:38:25 +02:00
|
|
|
|
|
|
|
slen = snprintf(NULL, 0, "file://%s", item->image)+1;
|
|
|
|
|
|
|
|
if(!(buf = malloc(slen))) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(buf, "file://%s", item->image);
|
|
|
|
|
|
|
|
EVP_MD_CTX *mdcontext = EVP_MD_CTX_new();
|
|
|
|
EVP_DigestInit_ex(mdcontext, EVP_md5(), NULL);
|
|
|
|
EVP_DigestUpdate(mdcontext, buf, slen);
|
|
|
|
|
|
|
|
EVP_DigestFinal_ex(mdcontext, digest, &digest_len);
|
|
|
|
EVP_MD_CTX_free(mdcontext);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
char md5sum[digest_len*2+1];
|
2023-07-03 18:48:38 +02:00
|
|
|
|
2023-07-10 20:38:25 +02:00
|
|
|
for (i = 0; i < digest_len; ++i)
|
2023-07-16 02:57:38 +02:00
|
|
|
sprintf(&md5sum[i*2], "%02x", (unsigned int)digest[i]);
|
2023-07-10 20:38:25 +02:00
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
if (!cachedir || !strcmp(cachedir, "default")) { // "default" here is from the config file
|
2023-07-10 20:38:25 +02:00
|
|
|
if (xdg_cache || !strcmp(cachedir, "xdg"))
|
2023-07-16 02:57:38 +02:00
|
|
|
slen = snprintf(NULL, 0, "%s/thumbnails/%s/%s.png", xdg_cache, cachesize, md5sum)+1;
|
2023-07-10 20:38:25 +02:00
|
|
|
else
|
2023-07-16 02:57:38 +02:00
|
|
|
slen = snprintf(NULL, 0, "%s/.cache/thumbnails/%s/%s.png", home, cachesize, md5sum)+1;
|
2023-07-10 20:38:25 +02:00
|
|
|
} else {
|
2023-07-16 02:57:38 +02:00
|
|
|
slen = snprintf(NULL, 0, "%s/%s/%s.png", cachedir, cachesize, md5sum)+1;
|
2023-07-10 20:38:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!(buf = malloc(slen))) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
if (!cachedir || !strcmp(cachedir, "default")) { // "default" here is from the config file
|
2023-07-10 20:38:25 +02:00
|
|
|
if (xdg_cache)
|
2023-07-16 02:57:38 +02:00
|
|
|
sprintf(buf, "%s/thumbnails/%s/%s.png", xdg_cache, cachesize, md5sum);
|
2023-07-10 20:38:25 +02:00
|
|
|
else
|
2023-07-16 02:57:38 +02:00
|
|
|
sprintf(buf, "%s/.cache/thumbnails/%s/%s.png", home, cachesize, md5sum);
|
2023-07-10 20:38:25 +02:00
|
|
|
} else {
|
2023-07-16 02:57:38 +02:00
|
|
|
sprintf(buf, "%s/%s/%s.png", cachedir, cachesize, md5sum);
|
2023-07-10 20:38:25 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
image = imlib_load_image(buf); // attempt to load image from path
|
2023-07-10 20:38:25 +02:00
|
|
|
|
|
|
|
if (image) {
|
|
|
|
imlib_context_set_image(image);
|
|
|
|
|
|
|
|
icw = imlib_image_get_width();
|
|
|
|
ich = imlib_image_get_height();
|
|
|
|
}
|
2023-07-03 18:48:38 +02:00
|
|
|
}
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
/* If passed w or h has changed, cache is invalid and has to be regenerated. This prevents flickering.
|
|
|
|
* It could change if sp.bh has changed in some way, such as font size being changed after cache has
|
|
|
|
* been generated.
|
|
|
|
*/
|
2023-07-14 00:49:00 +02:00
|
|
|
if (ich != h || icw != w) {
|
|
|
|
image = NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
// load regular image, because an image was not loaded from cache
|
2023-07-10 20:38:25 +02:00
|
|
|
if (!image || !generatecache) {
|
|
|
|
image = imlib_load_image(item->image);
|
|
|
|
|
|
|
|
if (!image) {
|
|
|
|
return x;
|
|
|
|
}
|
2023-07-03 18:48:38 +02:00
|
|
|
|
2023-07-10 20:38:25 +02:00
|
|
|
imlib_context_set_image(image);
|
2023-07-03 18:48:38 +02:00
|
|
|
|
2023-07-10 20:38:25 +02:00
|
|
|
icw = imlib_image_get_width();
|
|
|
|
ich = imlib_image_get_height();
|
2023-07-03 18:48:38 +02:00
|
|
|
|
2023-07-10 20:38:25 +02:00
|
|
|
if (!image) {
|
|
|
|
return x;
|
|
|
|
}
|
2023-07-03 18:48:38 +02:00
|
|
|
|
2023-07-10 20:38:25 +02:00
|
|
|
image = imlib_create_cropped_scaled_image(0, 0, icw, ich, w, h); // w = h = bh - lrpad / 2
|
|
|
|
imlib_free_image();
|
|
|
|
imlib_context_set_image(image);
|
2023-07-03 18:48:38 +02:00
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
// get new width/height
|
2023-07-10 20:38:25 +02:00
|
|
|
icw = imlib_image_get_width();
|
|
|
|
ich = imlib_image_get_height();
|
2023-07-03 18:48:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
imlib_image_set_format("png");
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
// create directory and write cache file
|
2023-07-10 20:38:25 +02:00
|
|
|
if (buf && generatecache && image) {
|
|
|
|
createifnexist_rec(buf);
|
|
|
|
imlib_save_image(buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
2023-07-16 02:57:38 +02:00
|
|
|
// draw the image
|
2023-07-03 18:48:38 +02:00
|
|
|
draw_set_img(draw, imlib_image_get_data(), w, h);
|
|
|
|
draw_img(draw, x, y);
|
|
|
|
|
|
|
|
x += w;
|
|
|
|
}
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
#endif
|