2023-02-26 05:13:40 +01:00
|
|
|
void
|
|
|
|
createifnexist(const char *dir)
|
|
|
|
{
|
|
|
|
if(access(dir, F_OK) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(errno == EACCES)
|
|
|
|
fprintf(stderr, "spmenu: no access to create directory: %s\n", dir);
|
|
|
|
|
|
|
|
if(mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1)
|
|
|
|
fprintf(stderr, "spmenu: failed to create directory: %s\n", dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
createifnexist_rec(const char *dir)
|
|
|
|
{
|
|
|
|
char *buf, *s = (char*)dir, *bs;
|
|
|
|
|
|
|
|
if(!(buf = malloc(strlen(s)+1)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
memset(buf, 0, strlen(s)+1);
|
|
|
|
|
|
|
|
for(bs = buf; *s; ++s, ++bs) {
|
|
|
|
if(*s == '/' && *buf)
|
|
|
|
createifnexist(buf);
|
|
|
|
|
|
|
|
*bs = *s;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
loadimage(const char *file, int *width, int *height)
|
|
|
|
{
|
|
|
|
image = imlib_load_image(file);
|
|
|
|
if (!image)
|
|
|
|
return;
|
|
|
|
|
|
|
|
imlib_context_set_image(image);
|
|
|
|
|
|
|
|
*width = imlib_image_get_width();
|
|
|
|
*height = imlib_image_get_height();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-02-28 22:36:33 +01:00
|
|
|
scaleimage(int *width, int *height)
|
2023-02-26 05:13:40 +01:00
|
|
|
{
|
|
|
|
int nwidth, nheight;
|
|
|
|
float aspect = 1.0f;
|
|
|
|
|
2023-02-28 22:36:33 +01:00
|
|
|
if (imagewidth > *width)
|
|
|
|
aspect = (float)(*width)/imagewidth;
|
2023-02-26 05:13:40 +01:00
|
|
|
else
|
2023-02-28 22:36:33 +01:00
|
|
|
aspect = (float)imagewidth/(*width);
|
2023-02-26 05:13:40 +01:00
|
|
|
|
2023-02-28 22:36:33 +01:00
|
|
|
nwidth = *width * aspect;
|
|
|
|
nheight = *height * aspect;
|
2023-02-26 05:13:40 +01:00
|
|
|
|
2023-02-28 22:36:33 +01:00
|
|
|
if(nwidth == *width && nheight == *height)
|
2023-02-26 05:13:40 +01:00
|
|
|
return;
|
|
|
|
|
2023-02-28 22:36:33 +01:00
|
|
|
image = imlib_create_cropped_scaled_image(0,0,*width,*height,nwidth,nheight);
|
|
|
|
|
2023-02-26 05:13:40 +01:00
|
|
|
imlib_free_image();
|
|
|
|
|
2023-02-28 22:36:33 +01:00
|
|
|
if(!image)
|
2023-02-26 05:13:40 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
imlib_context_set_image(image);
|
2023-02-28 22:36:33 +01:00
|
|
|
|
|
|
|
*width = nwidth;
|
|
|
|
*height = nheight;
|
|
|
|
|
|
|
|
return;
|
2023-02-26 05:13:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
loadimagecache(const char *file, int *width, int *height)
|
|
|
|
{
|
|
|
|
int slen = 0, i;
|
|
|
|
unsigned char digest[MD5_DIGEST_LENGTH];
|
|
|
|
char md5[MD5_DIGEST_LENGTH*2+1];
|
|
|
|
char *xdg_cache, *home = NULL, *dsize, *buf;
|
|
|
|
struct passwd *pw = NULL;
|
|
|
|
|
|
|
|
/* just load and don't store or try cache */
|
2023-02-28 22:36:33 +01:00
|
|
|
if (longestedge > 256) {
|
2023-02-26 05:13:40 +01:00
|
|
|
loadimage(file, width, height);
|
2023-02-28 22:36:33 +01:00
|
|
|
if (image)
|
|
|
|
scaleimage(width, height);
|
2023-02-26 05:13:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-02-28 22:36:33 +01:00
|
|
|
if (longestedge > 128)
|
2023-02-26 05:13:40 +01: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);
|
|
|
|
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);
|
|
|
|
|
2023-02-28 22:36:33 +01:00
|
|
|
if (image && *width < imagewidth && *height < imageheight) {
|
2023-02-26 05:13:40 +01:00
|
|
|
imlib_free_image();
|
|
|
|
image = NULL;
|
2023-02-28 22:36:33 +01:00
|
|
|
} else if(image && (*width > imagewidth || *height > imageheight)) {
|
|
|
|
scaleimage(width, height);
|
2023-02-26 05:13:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* we are done */
|
|
|
|
if (image) {
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we din't find anything from cache, or it was just wrong */
|
|
|
|
loadimage(file, width, height);
|
|
|
|
if (!image) {
|
|
|
|
free(buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-28 22:36:33 +01:00
|
|
|
scaleimage(width, height);
|
2023-02-26 05:13:40 +01:00
|
|
|
imlib_image_set_format("png");
|
|
|
|
createifnexist_rec(buf);
|
|
|
|
imlib_save_image(buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
2023-02-28 23:03:35 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
jumptoindex(unsigned int index) {
|
|
|
|
unsigned int i;
|
|
|
|
sel = curr = matches;
|
|
|
|
calcoffsets();
|
|
|
|
for(i = 1; i < index; ++i) {
|
|
|
|
if(sel && sel->right && (sel = sel->right) == next) {
|
|
|
|
curr = next;
|
|
|
|
calcoffsets();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
resizetoimageheight(int imageheight)
|
|
|
|
{
|
|
|
|
int omh = mh, olines = lines;
|
|
|
|
lines = reallines;
|
|
|
|
|
|
|
|
if(lines * bh < imageheight + imagegaps * 2)
|
|
|
|
lines = (imageheight+imagegaps*2)/bh;
|
|
|
|
|
|
|
|
mh = (lines + 1) * bh;
|
|
|
|
|
|
|
|
if(mh - bh < imageheight + imagegaps * 2)
|
|
|
|
mh = imageheight+imagegaps*2+bh;
|
|
|
|
|
|
|
|
if(!win || omh == mh)
|
|
|
|
return;
|
|
|
|
|
|
|
|
XResizeWindow(dpy, win, mw, mh);
|
|
|
|
drw_resize(drw, mw, mh);
|
|
|
|
|
|
|
|
if (olines != lines) {
|
|
|
|
struct item *item;
|
|
|
|
unsigned int i = 1;
|
|
|
|
for (item = matches; item && item != sel; item = item->right)
|
|
|
|
++i;
|
|
|
|
|
|
|
|
jumptoindex(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
drawmenu();
|
|
|
|
}
|