Cache icons too, not just normal images

This commit is contained in:
speedie 2023-07-10 20:38:25 +02:00
parent 2d445f860d
commit c6397915f4
2 changed files with 96 additions and 18 deletions

View file

@ -5,9 +5,85 @@ int draw_icon(struct item *item, int x, int y, int w, int h) {
int ich = 0; int ich = 0;
int icw = 0; int icw = 0;
if (hideimage) return x; // cache
int slen = 0, i;
unsigned int digest_len = EVP_MD_size(EVP_md5());
unsigned char *digest = (unsigned char *)OPENSSL_malloc(digest_len);
char *xdg_cache, *home = NULL, *dsize, *buf = NULL;
struct passwd *pw = NULL;
if (hideimage) {
return x;
}
if (item->image) { if (item->image) {
if (generatecache) {
if(!(xdg_cache = getenv("XDG_CACHE_HOME"))) {
if(!(home = getenv("HOME")) && (pw = getpwuid(getuid())))
home = pw->pw_dir;
if(!home) {
fprintf(stderr, "spmenu: could not find home directory");
return x;
}
}
dsize = "icon";
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);
char md5[digest_len*2+1];
for (i = 0; i < digest_len; ++i)
sprintf(&md5[i*2], "%02x", (unsigned int)digest[i]);
if (!cachedir || !strcmp(cachedir, "default")) {
if (xdg_cache || !strcmp(cachedir, "xdg"))
slen = snprintf(NULL, 0, "%s/thumbnails/%s/%s.png", xdg_cache, dsize, md5)+1;
else
slen = snprintf(NULL, 0, "%s/.cache/thumbnails/%s/%s.png", home, dsize, md5)+1;
} else {
slen = snprintf(NULL, 0, "%s/%s/%s.png", cachedir, dsize, md5)+1;
}
if(!(buf = malloc(slen))) {
return x;
}
if (!cachedir || !strcmp(cachedir, "default")) {
if (xdg_cache)
sprintf(buf, "%s/thumbnails/%s/%s.png", xdg_cache, dsize, md5);
else
sprintf(buf, "%s/.cache/thumbnails/%s/%s.png", home, dsize, md5);
} else {
sprintf(buf, "%s/%s/%s.png", cachedir, dsize, md5);
}
image = imlib_load_image(buf);
if (image) {
imlib_context_set_image(image);
icw = imlib_image_get_width();
ich = imlib_image_get_height();
}
}
if (!image || !generatecache) {
image = imlib_load_image(item->image); image = imlib_load_image(item->image);
if (!image) { if (!image) {
@ -19,20 +95,26 @@ int draw_icon(struct item *item, int x, int y, int w, int h) {
icw = imlib_image_get_width(); icw = imlib_image_get_width();
ich = imlib_image_get_height(); ich = imlib_image_get_height();
image = imlib_create_cropped_scaled_image(0, 0, icw, ich, w, h); // w = h = bh - lrpad / 2
icw = imlib_image_get_width();
ich = imlib_image_get_height();
imlib_free_image();
imlib_context_set_image(image);
if (!image) { if (!image) {
return x; return x;
} }
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);
icw = imlib_image_get_width();
ich = imlib_image_get_height();
}
imlib_image_set_format("png"); imlib_image_set_format("png");
if (buf && generatecache && image) {
createifnexist_rec(buf);
imlib_save_image(buf);
free(buf);
}
// Draw the image // Draw the image
draw_set_img(draw, imlib_image_get_data(), w, h); draw_set_img(draw, imlib_image_get_data(), w, h);
draw_img(draw, x, y); draw_img(draw, x, y);

View file

@ -220,7 +220,6 @@ void loadimagecache(const char *file, int *width, int *height) {
slen = snprintf(NULL, 0, "file://%s", file)+1; slen = snprintf(NULL, 0, "file://%s", file)+1;
if(!(buf = malloc(slen))) { if(!(buf = malloc(slen))) {
fprintf(stderr, "spmenu: out of memory");
return; return;
} }
@ -252,7 +251,6 @@ void loadimagecache(const char *file, int *width, int *height) {
} }
if(!(buf = malloc(slen))) { if(!(buf = malloc(slen))) {
fprintf(stderr, "spmenu: out of memory");
return; return;
} }
@ -285,8 +283,6 @@ void loadimagecache(const char *file, int *width, int *height) {
loadimage(file, width, height); loadimage(file, width, height);
scaleimage(width, height); scaleimage(width, height);
if (!generatecache) return;
if (image) imlib_image_set_format("png"); if (image) imlib_image_set_format("png");
if (buf && generatecache && image) { if (buf && generatecache && image) {