spmenu/libs/img.c

434 lines
11 KiB
C
Raw Normal View History

2023-05-14 00:21:16 +02:00
/* See LICENSE file for copyright and license details. */
2023-03-13 21:21:40 +01:00
#if USEIMAGE
void setimagesize(int width, int height) {
if (!image || hideimage || height < 5 || width < 5 || width > sp.mw) {
2023-04-05 20:44:09 +02:00
return;
}
img.imageheight = height;
img.imagewidth = width;
2023-04-05 20:44:09 +02:00
}
void flipimage(void) {
2023-06-23 03:38:21 +02:00
switch (img.flip) {
2023-04-05 20:44:09 +02:00
case 1: // horizontal
imlib_image_flip_horizontal();
break;
case 2: // vertical
imlib_image_flip_vertical();
break;
case 3: // diagonal
imlib_image_flip_diagonal();
break;
default:
2023-06-23 03:38:21 +02:00
img.flip = img.flip ? 1 : 0;
2023-04-05 20:44:09 +02:00
return;
}
}
void cleanupimage(void) {
2023-04-05 20:44:09 +02:00
if (image) { // free image using imlib2
imlib_free_image();
image = NULL;
}
}
void drawimage(void) {
2023-04-05 20:44:09 +02:00
int width = 0, height = 0;
char *limg = NULL;
2023-07-03 18:48:38 +02:00
if (!lines || !columns || hideimage || !imagetype) return;
2023-04-05 20:44:09 +02:00
// load image cache
if (sel && sel->image && strcmp(sel->image, limg ? limg : "")) {
2023-06-23 03:38:21 +02:00
if (img.longestedge)
2023-04-05 20:44:09 +02:00
loadimagecache(sel->image, &width, &height);
} else if ((!sel || !sel->image) && image) { // free image
cleanupimage();
2023-06-24 04:43:05 +02:00
}
if (!image) {
return;
}
// render the image
if (img.longestedge && width && height) {
2023-04-05 20:44:09 +02:00
flipimage();
int leftmargin = img.imagegaps; // gaps between image and menu
int wtr = 0; // remove from w
int wta = 0; // add to w
int xta = 0; // add to x
2023-04-05 20:44:09 +02:00
if (hideprompt && hideinput && hidemode && hidematchcount && hidecaps) {
2023-06-23 03:38:21 +02:00
wtr = sp.bh;
2023-04-05 20:44:09 +02:00
} else {
2023-06-23 03:38:21 +02:00
wta = sp.bh;
2023-04-05 20:44:09 +02:00
}
// margin
xta += menumarginh;
wta += menumarginv;
2023-06-23 03:38:21 +02:00
if (sp.mh != sp.bh + height + leftmargin * 2 - wtr && imageresize) { // menu height cannot be smaller than image height
resizetoimageheight(imlib_image_get_height());
}
draw_set_img(draw, imlib_image_get_data(), width, height);
// render image on X11
2023-06-24 04:43:05 +02:00
if (!imageposition) { // top mode = 0
2023-04-05 20:44:09 +02:00
if (height > width)
width = height;
draw_img(draw, leftmargin + (img.imagewidth - width) / 2 + xta, wta + leftmargin);
2023-06-24 04:43:05 +02:00
} else if (imageposition == 1) { // bottom mode = 1
2023-04-05 20:44:09 +02:00
if (height > width)
width = height;
draw_img(draw, leftmargin + (img.imagewidth - width) / 2 + xta, sp.mh - height - leftmargin);
2023-06-24 04:43:05 +02:00
} else if (imageposition == 2) { // center mode = 2
draw_img(draw, leftmargin + (img.imagewidth - width) / 2 + xta, (sp.mh - wta - height) / 2 + wta);
2023-06-24 04:43:05 +02:00
} else { // top center
2023-06-23 03:38:21 +02:00
int minh = MIN(height, sp.mh - sp.bh - leftmargin * 2);
draw_img(draw, leftmargin + (img.imagewidth - width) / 2 + xta, (minh - height) / 2 + wta + leftmargin);
2023-04-05 20:44:09 +02:00
}
}
if (sel) {
limg = sel->image;
} else {
limg = NULL;
}
}
void setimageopts(void) {
2023-04-05 20:44:09 +02:00
imlib_set_cache_size(8192 * 1024);
2023-05-08 23:00:45 +02:00
imlib_set_color_usage(128);
2023-04-05 20:44:09 +02:00
}
void createifnexist(const char *dir) {
2023-04-05 20:44:09 +02:00
// exists, so return
2023-05-08 23:00:45 +02:00
if (access(dir, F_OK) == 0)
2023-04-05 20:44:09 +02:00
return;
// fatal: permission denied
2023-05-08 23:00:45 +02:00
if (errno == EACCES)
2023-04-05 20:44:09 +02:00
fprintf(stderr, "spmenu: no access to create directory: %s\n", dir);
// mkdir() failure
2023-05-08 23:00:45 +02:00
if (mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1)
fprintf(stderr, "spmenu: failed to create directory: %s\n", dir);
2023-04-05 20:44:09 +02:00
}
void createifnexist_rec(const char *dir) {
2023-05-08 23:00:45 +02:00
char *buf, *s = (char*)dir, *bs;
2023-04-05 20:44:09 +02:00
if(!(buf = malloc(strlen(s)+1)))
2023-05-08 23:00:45 +02:00
return;
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
memset(buf, 0, strlen(s)+1);
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
for(bs = buf; *s; ++s, ++bs) {
if(*s == '/' && *buf)
2023-04-05 20:44:09 +02:00
createifnexist(buf);
2023-05-08 23:00:45 +02:00
*bs = *s;
}
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
free(buf);
2023-04-05 20:44:09 +02:00
}
void loadimage(const char *file, int *width, int *height) {
2023-04-28 18:50:33 +02:00
if (!file) return;
2023-05-08 23:00:45 +02:00
image = imlib_load_image(file);
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
if (!image)
2023-04-05 20:44:09 +02:00
return;
2023-05-08 23:00:45 +02:00
imlib_context_set_image(image);
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
*width = imlib_image_get_width();
*height = imlib_image_get_height();
2023-04-05 20:44:09 +02:00
}
void scaleimage(int *width, int *height) {
2023-05-08 23:00:45 +02:00
int new_width, new_height;
float aspect = 1.0f;
2023-04-05 20:44:09 +02:00
// depending on what size, we determine aspect ratio
if (img.imagewidth > *width) {
aspect = (float)(*width)/img.imagewidth;
} else {
aspect = (float)img.imagewidth/(*width);
}
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
new_width = *width * aspect;
new_height = *height * aspect;
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
if ((new_width == *width && new_height == *height) || (!image))
2023-04-05 20:44:09 +02:00
return;
2023-05-08 23:00:45 +02:00
image = imlib_create_cropped_scaled_image(0,0,*width,*height,new_width,new_height);
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
imlib_free_image();
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
if(!image)
2023-04-05 20:44:09 +02:00
return;
2023-05-08 23:00:45 +02:00
imlib_context_set_image(image);
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
*width = new_width;
*height = new_height;
2023-04-05 20:44:09 +02:00
return;
}
void loadimagecache(const char *file, int *width, int *height) {
2023-05-08 23:00:45 +02:00
int slen = 0, i;
unsigned int digest_len = EVP_MD_size(EVP_md5());
unsigned char *digest = (unsigned char *)OPENSSL_malloc(digest_len);
2023-05-08 23:00:45 +02:00
char *xdg_cache, *home = NULL, *dsize, *buf = NULL;
struct passwd *pw = NULL;
// just load and don't store or try cache
2023-06-23 03:38:21 +02:00
if (img.longestedge > maxcache) {
2023-05-08 23:00:45 +02:00
loadimage(file, width, height);
2023-04-05 20:44:09 +02:00
if (image)
scaleimage(width, height);
2023-05-08 23:00:45 +02:00
return;
}
2023-04-05 20:44:09 +02:00
if (generatecache) {
// try find image from cache first
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;
}
}
// which cache do we try?
dsize = "normal";
2023-06-23 03:38:21 +02:00
if (img.longestedge > 128)
2023-04-05 20:44:09 +02:00
dsize = "large";
slen = snprintf(NULL, 0, "file://%s", file)+1;
if(!(buf = malloc(slen))) {
fprintf(stderr, "spmenu: out of memory");
return;
}
// calculate md5 from path
sprintf(buf, "file://%s", file);
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);
2023-04-05 20:44:09 +02:00
free(buf);
char md5[digest_len*2+1];
for (i = 0; i < digest_len; ++i)
2023-04-05 20:44:09 +02:00
sprintf(&md5[i*2], "%02x", (unsigned int)digest[i]);
// path for cached thumbnail
2023-05-12 23:23:15 +02:00
if (!cachedir || !strcmp(cachedir, "default")) {
if (xdg_cache || !strcmp(cachedir, "xdg"))
2023-05-12 23:23:15 +02:00
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;
2023-05-12 23:23:15 +02:00
} else {
slen = snprintf(NULL, 0, "%s/%s/%s.png", cachedir, dsize, md5)+1;
}
2023-04-05 20:44:09 +02:00
if(!(buf = malloc(slen))) {
fprintf(stderr, "spmenu: out of memory");
2023-04-05 20:44:09 +02:00
return;
}
2023-05-12 23:23:15 +02:00
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);
2023-05-12 23:23:15 +02:00
} else {
sprintf(buf, "%s/%s/%s.png", cachedir, dsize, md5);
}
2023-04-05 20:44:09 +02:00
loadimage(buf, width, height);
if (image && *width < img.imagewidth && *height < img.imageheight) {
2023-04-05 20:44:09 +02:00
imlib_free_image();
image = NULL;
} else if(image && (*width > img.imagewidth || *height > img.imageheight)) {
2023-04-05 20:44:09 +02:00
scaleimage(width, height);
}
// we are done
if (image) {
free(buf);
return;
}
}
// we din't find anything from cache, or it was just wrong
2023-05-08 23:00:45 +02:00
loadimage(file, width, height);
scaleimage(width, height);
2023-04-05 20:44:09 +02:00
if (!generatecache) return;
2023-04-21 11:52:01 +02:00
if (image) imlib_image_set_format("png");
2023-04-05 20:44:09 +02:00
2023-04-21 11:52:01 +02:00
if (buf && generatecache && image) {
2023-04-05 20:44:09 +02:00
createifnexist_rec(buf);
imlib_save_image(buf);
free(buf);
}
}
void jumptoindex(unsigned int index) {
2023-05-08 23:00:45 +02:00
unsigned int i;
sel = curr = matches;
2023-04-21 09:51:51 +02:00
2023-05-08 23:00:45 +02:00
calcoffsets();
2023-04-21 09:51:51 +02:00
2023-05-08 23:00:45 +02:00
for (i = 1; i < index; ++i) {
if(sel && sel->right && (sel = sel->right) == next) {
curr = next;
calcoffsets();
}
}
2023-04-05 20:44:09 +02:00
}
void resizetoimageheight(int imageheight) {
2023-07-03 18:48:38 +02:00
if (!imagetype) return;
#if USEX
if (!protocol) {
resizetoimageheight_x11(imageheight);
} else {
#if USEWAYLAND
resizetoimageheight_wl(imageheight);
#endif
}
#elif USEWAYLAND
resizetoimageheight_wl(imageheight);
#endif
}
#if USEX
void resizetoimageheight_x11(int imageheight) {
2023-06-23 03:38:21 +02:00
int mh = sp.mh, olines = lines;
lines = img.setlines;
2023-04-05 20:44:09 +02:00
2023-04-17 19:00:59 +02:00
int x, y;
if (lines * sp.bh < imageheight + img.imagegaps * 2) {
lines = (imageheight + img.imagegaps * 2) / sp.bh;
2023-04-05 20:44:09 +02:00
}
2023-06-11 16:46:36 +02:00
get_mh();
2023-04-05 20:44:09 +02:00
2023-06-24 17:34:03 +02:00
if (menuposition == 2) { // centered
sp.mw = MIN(MAX(max_textw() + sp.promptw, minwidth), mo.output_width);
x = (mo.output_width - sp.mw) / 2 + xpos;
y = (mo.output_height - sp.mh) / 2 - ypos;
} else { // top or bottom
x = 0;
y = menuposition ? 0 : mo.output_width - sp.mh - ypos;
sp.mw = (menuwidth > 0 ? menuwidth : mo.output_width);
2023-05-08 23:00:45 +02:00
}
if (
!win ||
2023-06-23 03:38:21 +02:00
mh == sp.mh) {
2023-04-05 20:44:09 +02:00
return;
}
2023-06-23 03:38:21 +02:00
XMoveResizeWindow(dpy, win, x + sp.sp, y + sp.vp, sp.mw - 2 * sp.sp - borderwidth * 2, sp.mh);
draw_resize(draw, sp.mw - 2 * sp.sp - borderwidth, sp.mh);
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
if (olines != lines) {
struct item *item;
unsigned int i = 1;
2023-04-05 20:44:09 +02:00
// walk through all matches
2023-05-08 23:00:45 +02:00
for (item = matches; item && item != sel; item = item->right)
2023-04-05 20:44:09 +02:00
++i;
2023-05-08 23:00:45 +02:00
jumptoindex(i);
}
2023-04-05 20:44:09 +02:00
drawmenu_layer();
}
#endif
#if USEWAYLAND
void resizetoimageheight_wl(int imageheight) {
2023-06-23 03:38:21 +02:00
int mh = sp.mh, olines = lines;
lines = img.setlines;
if (lines * sp.bh < imageheight + img.imagegaps * 2) {
lines = (imageheight + img.imagegaps * 2) / sp.bh;
}
2023-06-11 16:46:36 +02:00
get_mh();
2023-06-23 03:38:21 +02:00
if (mh == sp.mh) {
return;
}
if (olines != lines) {
struct item *item;
unsigned int i = 1;
// walk through all matches
for (item = matches; item && item != sel; item = item->right)
++i;
jumptoindex(i);
}
2023-06-23 03:38:21 +02:00
state.width = sp.mw;
state.height = sp.mh;
state.buffer = create_buffer(&state);
if (draw == NULL) {
die("spmenu: draw == NULL");
}
if (state.buffer == NULL) {
die("state.buffer == null");
}
set_layer_size(&state, state.width, state.height);
draw_create_surface_wl(draw, state.data, state.width, state.height);
drawmenu();
wl_surface_set_buffer_scale(state.surface, 1);
wl_surface_attach(state.surface, state.buffer, 0, 0);
wl_surface_damage(state.surface, 0, 0, state.width, state.height);
wl_surface_commit(state.surface);
}
#endif
void store_image_vars(void) {
img.imagewidth = imagewidth;
img.imageheight = imageheight;
img.imagegaps = imagegaps;
img.longestedge = MAX(img.imagewidth, img.imageheight);
2023-04-05 20:44:09 +02:00
}
2023-03-13 21:21:40 +01:00
#endif