spmenu/libs/img.c

431 lines
12 KiB
C
Raw Normal View History

2023-03-13 21:21:40 +01:00
#if USEIMAGE
void setimagesize(int width, int height) {
2023-04-05 20:44:09 +02:00
int oih = 0;
int oiw = 0;
// this makes sure we cannot scale the image up or down too much
2023-04-06 02:39:00 +02:00
if ((!image && height < imageheight) || (!image && width < imagewidth) || (width < 0) || width > mw || hideimage) return;
2023-04-05 20:44:09 +02:00
// original width/height
oih = imageheight;
oiw = imagewidth;
imageheight = height;
imagewidth = width;
drawimage();
needredraw = 0;
if (!image) {
imageheight = oih;
imagewidth = oiw;
return;
}
drawmenu();
}
void flipimage(void) {
2023-04-05 20:44:09 +02:00
// flip image
switch (flip) {
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:
flip = flip ? 1 : 0;
return;
}
}
void rotateimage(void) {
2023-04-05 20:44:09 +02:00
rotation %= 4;
imlib_image_orientate(rotation);
}
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;
if (!lines || !columns || hideimage) return;
// to prevent the image from being drawn multiple times wasting resources
if (!needredraw) {
needredraw = 1;
return;
}
// load image cache
if (sel && sel->image && strcmp(sel->image, limg ? limg : "")) {
if (longestedge)
loadimagecache(sel->image, &width, &height);
} else if ((!sel || !sel->image) && image) { // free image
cleanupimage();
2023-04-06 02:39:00 +02:00
} if (image && longestedge && width && height) { // render the image
2023-05-08 23:00:45 +02:00
// rotate and flip, will return if we don't need to
2023-04-05 20:44:09 +02:00
rotateimage();
flipimage();
int leftmargin = 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-04-05 20:44:09 +02:00
wtr = bh;
} else {
wta = bh;
2023-04-05 20:44:09 +02:00
}
// margin
xta += menumarginh;
wta += menumarginv;
2023-05-08 23:00:45 +02:00
if (mh != bh + height + imagegaps * 2 - wtr) { // menu height cannot be smaller than image height
resizetoimageheight(height);
}
2023-04-05 20:44:09 +02:00
// render image
2023-04-21 11:52:01 +02:00
if (!imageposition && image) { // top mode = 0
2023-04-05 20:44:09 +02:00
if (height > width)
width = height;
imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, wta+imagegaps);
2023-04-21 11:52:01 +02:00
} else if (imageposition == 1 && image) { // bottom mode = 1
2023-04-05 20:44:09 +02:00
if (height > width)
width = height;
imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, mh-height-imagegaps);
2023-04-21 11:52:01 +02:00
} else if (imageposition == 2 && image) { // center mode = 2
imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, (mh-wta-height)/2+wta);
2023-04-21 11:52:01 +02:00
} else if (image) { // top center
2023-04-05 20:44:09 +02:00
int minh = MIN(height, mh-bh-imagegaps*2);
imlib_render_image_on_drawable(leftmargin+(imagewidth-width)/2+xta, (minh-height)/2+wta+imagegaps);
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_context_set_blend(1);
imlib_context_set_dither(1);
imlib_set_color_usage(128);
imlib_context_set_display(dpy);
imlib_context_set_visual(visual);
imlib_context_set_colormap(cmap);
imlib_context_set_drawable(win);
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
2023-05-08 23:00:45 +02:00
if (imagewidth > *width)
aspect = (float)(*width)/imagewidth;
else
aspect = (float)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 char digest[MD5_DIGEST_LENGTH];
char md5[MD5_DIGEST_LENGTH*2+1];
char *xdg_cache, *home = NULL, *dsize, *buf = NULL;
struct passwd *pw = NULL;
// just load and don't store or try cache
if (longestedge > maxcache) {
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";
if (longestedge > 128)
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);
MD5((unsigned char*)buf, slen, digest);
free(buf);
for(i = 0; i < MD5_DIGEST_LENGTH; ++i)
sprintf(&md5[i*2], "%02x", (unsigned int)digest[i]);
// path for cached thumbnail
if (xdg_cache)
slen = snprintf(NULL, 0, "%s/thumbnails/%s/%s.png", xdg_cache, dsize, md5)+1;
else
slen = snprintf(NULL, 0, "%s/.thumbnails/%s/%s.png", home, dsize, md5)+1;
if(!(buf = malloc(slen))) {
fprintf(stderr, "out of memory");
return;
}
if (xdg_cache)
sprintf(buf, "%s/thumbnails/%s/%s.png", xdg_cache, dsize, md5);
else
sprintf(buf, "%s/.thumbnails/%s/%s.png", home, dsize, md5);
loadimage(buf, width, height);
if (image && *width < imagewidth && *height < imageheight) {
imlib_free_image();
image = NULL;
} else if(image && (*width > imagewidth || *height > imageheight)) {
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-05-08 23:00:45 +02:00
int omh = mh, olines = lines;
lines = reallines;
2023-04-05 20:44:09 +02:00
int wtr = 0;
2023-04-17 19:00:59 +02:00
int x, y;
2023-05-08 23:00:45 +02:00
#if USEXINERAMA
2023-04-17 19:00:59 +02:00
int j, di, a, n, area = 0;
XineramaScreenInfo *info;
2023-05-08 23:00:45 +02:00
Window pw;
2023-04-17 19:00:59 +02:00
unsigned int du;
Window w, dw, *dws;
2023-05-08 23:00:45 +02:00
#endif
2023-04-17 19:00:59 +02:00
XWindowAttributes wa;
2023-05-08 23:00:45 +02:00
if (lines * bh < imageheight + imagegaps * 2) {
2023-04-05 20:44:09 +02:00
lines = (imageheight + imagegaps * 2) / bh;
}
if (hideprompt && hideinput && hidemode && hidematchcount) {
wtr = bh;
}
2023-05-08 23:00:45 +02:00
mh = MAX((lines + 1) * bh + 2 * menumarginv, ((lines + 1) * bh) - wtr + 2 * menumarginv);
2023-04-05 20:44:09 +02:00
2023-05-08 23:00:45 +02:00
if (mh - bh < imageheight + imagegaps * 2) {
mh = (imageheight + imagegaps * 2 + bh) - wtr + 2 * menumarginv;
2023-04-05 20:44:09 +02:00
}
2023-04-17 19:00:59 +02:00
// init xinerama screens
2023-05-08 23:00:45 +02:00
#if USEXINERAMA
int i = 0;
if (parentwin == root && (info = XineramaQueryScreens(dpy, &n))) {
XGetInputFocus(dpy, &w, &di);
if (mon >= 0 && mon < n) {
i = mon;
2023-04-17 20:03:02 +02:00
} else if (w != root && w != PointerRoot && w != None) {
do {
2023-05-08 23:00:45 +02:00
if (XQueryTree(dpy, (pw = w), &dw, &w, &dws, &du) && dws)
XFree(dws);
2023-04-17 20:03:02 +02:00
} while (w != root && w != pw);
2023-05-08 23:00:45 +02:00
if (XGetWindowAttributes(dpy, pw, &wa)) {
for (j = 0; j < n; j++) {
if ((a = INTERSECT(wa.x, wa.y, wa.width, wa.height, info[j])) > area) {
area = a;
i = j;
2023-04-17 20:03:02 +02:00
}
}
2023-05-08 23:00:45 +02:00
}
2023-04-17 20:03:02 +02:00
if (mon < 0 && !area && XQueryPointer(dpy, root, &dw, &dw, &x, &y, &di, &di, &du)) {
for (i = 0; i < n; i++) {
if (INTERSECT(x, y, 1, 1, info[i])) {
break;
}
}
}
}
2023-04-17 19:00:59 +02:00
// calculate x/y position
2023-05-08 23:00:45 +02:00
if (menuposition == 2) { // centered
mw = MIN(MAX(max_textw() + promptw, minwidth), info[i].width);
x = info[i].x_org + ((info[i].width - mw) / 2);
y = info[i].y_org + ((info[i].height - mh) / 2);
} else { // top or bottom
x = info[i].x_org + xpos;
y = info[i].y_org + (menuposition ? 0 : info[i].height - mh - ypos);
mw = (menuwidth>0 ? menuwidth : info[i].width);
}
XFree(info);
} else
#endif
{
2023-04-22 21:39:22 +02:00
if (!XGetWindowAttributes(dpy, parentwin, &wa))
2023-05-08 23:00:45 +02:00
die("spmenu: could not get embedding window attributes: 0x%lx",
parentwin); // die because unable to get attributes for the parent window
if (menuposition == 2) { // centered
mw = MIN(MAX(max_textw() + promptw, minwidth), wa.width);
x = (wa.width - mw) / 2;
y = (wa.height - mh) / 2;
} else { // top or bottom
x = 0;
y = menuposition ? 0 : wa.height - mh - ypos;
mw = wa.width;
}
}
if (!win || omh == mh) {
2023-04-05 20:44:09 +02:00
return;
}
2023-04-22 00:50:26 +02:00
XMoveResizeWindow(dpy, win, x + sp, y + vp, mw - 2 * sp - borderwidth * 2, mh);
2023-05-08 23:00:45 +02:00
drw_resize(drw, mw - 2 * sp - borderwidth, 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
2023-05-08 23:00:45 +02:00
drawmenu();
2023-04-05 20:44:09 +02:00
}
2023-03-13 21:21:40 +01:00
#endif