Krolyxon
Commit 5cfae63

Branches

Refs →

nsxiv5cfae63

Create thumbnail cache dir automatically

Author
Bert Münnich
Date
2014-06-10 23:15 +0200
Parent
cd02f2d
Commit
5cfae636206df6f6f36d004f7dc66bcfb6d4a52c

+61 −91

3 files changed

  1. M sxiv.1 +4 −12
  2. M thumbs.c +55 −70
  3. M util.c +2 −9
M sxiv.1 View
@@ -27,9 +27,6 @@ sxiv has two modes of operation: image and thumbnail mode. The default is image
2727 mode, in which only the current image is shown. In thumbnail mode a grid of
2828 small previews is displayed, making it easy to choose an image to open.
2929 .P
30−sxiv can also cache its thumbnails. Please see the section THUMBNAIL CACHING
31−for information on how to enable this feature.
32−.P
3330 Please note, that the fullscreen mode requires an EWMH/NetWM compliant window
3431 manager.
3532 .SH OPTIONS
@@ -371,18 +368,13 @@ keysym as listed in /usr/include/X11/keysymdef.h without the "XK_" prefix.
371368 There is also an example script installed together with sxiv as
372369 .IR PREFIX/share/sxiv/exec/key-handler .
373370 .SH THUMBNAIL CACHING
374−To enable thumbnail caching, please make sure to create the directory
375−.I $XDG_CACHE_HOME/sxiv/
376−with write permissions. sxiv will then store all thumbnails inside this
377−directory, but it will not create this directory by itself. It rather uses the
378−existance of this directory as an affirmation, that the user wants thumbnails
379−to be cached.
371+sxiv stores all thumbnails under
372+.IR $XDG_CACHE_HOME/sxiv/ .
380373 .P
381374 Use the command line option
382375 .I \-c
383−to keep the cache directory clean by removing all orphaned cache files.
384−Additionally, run the following command afterwards inside the cache directory
385−to remove empty subdirectories:
376+to remove all orphaned cache files. Additionally, run the following command
377+afterwards inside the cache directory to remove empty subdirectories:
386378 .P
387379 .RS
388380 find . \-depth \-type d \-empty ! \-name '.' \-exec rmdir {} \\;
M thumbs.c View
@@ -36,18 +36,9 @@
3636 void exif_auto_orientate(const fileinfo_t*);
3737 #endif
3838
39+static char *cache_dir;
3940 static const int thumb_dim = THUMB_SIZE + 10;
4041
41−static char *cache_dir = NULL;
42−
43−bool tns_cache_enabled(void)
44−{
45− struct stat stats;
46−
47− return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
48− S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
49−}
50−
5142 char* tns_cache_filepath(const char *filepath)
5243 {
5344 size_t len;
@@ -107,19 +98,15 @@ void tns_cache_write(thumb_t *t, bool force)
10798 err = r_mkdir(cfile);
10899 *dirend = '/';
109100 }
110−
111101 if (err == 0) {
112102 imlib_context_set_image(t->im);
113103 imlib_image_set_format("png");
114104 imlib_save_image_with_error_return(cfile, &err);
115105 }
116−
117106 if (err == 0) {
118107 times.actime = fstats.st_atime;
119108 times.modtime = fstats.st_mtime;
120109 utime(cfile, &times);
121− } else {
122− warn("could not cache thumbnail: %s", t->file->name);
123110 }
124111 }
125112 free(cfile);
@@ -177,7 +164,6 @@ void tns_init(tns_t *tns, int cnt, win_t *win)
177164 } else {
178165 tns->thumbs = NULL;
179166 }
180−
181167 tns->cap = cnt;
182168 tns->cnt = tns->first = tns->sel = 0;
183169 tns->win = win;
@@ -226,7 +212,7 @@ bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
226212 bool force, bool silent)
227213 {
228214 int w, h;
229− bool use_cache, cache_hit = false;
215+ bool cache_hit = false;
230216 float z, zw, zh;
231217 thumb_t *t;
232218 Imlib_Image im = NULL;
@@ -246,64 +232,63 @@ bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
246232 imlib_free_image();
247233 }
248234
249− if ((use_cache = tns_cache_enabled())) {
250− if (!force && (im = tns_cache_load(file->path)) != NULL)
251− cache_hit = true;
252− }
253−
254− if (!cache_hit) {
235+ if (!force && (im = tns_cache_load(file->path)) != NULL) {
236+ cache_hit = true;
237+ } else {
255238 #if HAVE_LIBEXIF
256− int pw = 0, ph = 0, x = 0, y = 0;
257− bool err;
258− ExifData *ed;
259− ExifEntry *entry;
260− ExifContent *ifd;
261− ExifByteOrder byte_order;
262− int tmpfd;
263− char tmppath[] = "/tmp/sxiv-XXXXXX";
264− Imlib_Image tmpim;
265−
266− if ((ed = exif_data_new_from_file(file->path)) != NULL &&
267− ed->data != NULL && ed->size > 0)
268− {
269− if ((tmpfd = mkstemp(tmppath)) >= 0) {
270− err = write(tmpfd, ed->data, ed->size) != ed->size;
271− close(tmpfd);
272−
273− if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
274− byte_order = exif_data_get_byte_order(ed);
275− ifd = ed->ifd[EXIF_IFD_EXIF];
276− entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
277− if (entry != NULL)
278− pw = exif_get_long(entry->data, byte_order);
279− entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
280− if (entry != NULL)
281− ph = exif_get_long(entry->data, byte_order);
282−
283− imlib_context_set_image(tmpim);
284− w = imlib_image_get_width();
285− h = imlib_image_get_height();
286−
287− if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
288− zw = (float) pw / (float) w;
289− zh = (float) ph / (float) h;
290− if (zw < zh) {
291− pw /= zh;
292− x = (w - pw) / 2;
293− w = pw;
294− } else if (zw > zh) {
295− ph /= zw;
296− y = (h - ph) / 2;
297− h = ph;
239+ if (!force) {
240+ int pw = 0, ph = 0, x = 0, y = 0;
241+ bool err;
242+ ExifData *ed;
243+ ExifEntry *entry;
244+ ExifContent *ifd;
245+ ExifByteOrder byte_order;
246+ int tmpfd;
247+ char tmppath[] = "/tmp/sxiv-XXXXXX";
248+ Imlib_Image tmpim;
249+
250+ if ((ed = exif_data_new_from_file(file->path)) != NULL &&
251+ ed->data != NULL && ed->size > 0)
252+ {
253+ if ((tmpfd = mkstemp(tmppath)) >= 0) {
254+ err = write(tmpfd, ed->data, ed->size) != ed->size;
255+ close(tmpfd);
256+
257+ if (!err && (tmpim = imlib_load_image(tmppath)) != NULL) {
258+ byte_order = exif_data_get_byte_order(ed);
259+ ifd = ed->ifd[EXIF_IFD_EXIF];
260+ entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_X_DIMENSION);
261+ if (entry != NULL)
262+ pw = exif_get_long(entry->data, byte_order);
263+ entry = exif_content_get_entry(ifd, EXIF_TAG_PIXEL_Y_DIMENSION);
264+ if (entry != NULL)
265+ ph = exif_get_long(entry->data, byte_order);
266+
267+ imlib_context_set_image(tmpim);
268+ w = imlib_image_get_width();
269+ h = imlib_image_get_height();
270+
271+ if (pw > w && ph > h && (pw - ph >= 0) == (w - h >= 0)) {
272+ zw = (float) pw / (float) w;
273+ zh = (float) ph / (float) h;
274+ if (zw < zh) {
275+ pw /= zh;
276+ x = (w - pw) / 2;
277+ w = pw;
278+ } else if (zw > zh) {
279+ ph /= zw;
280+ y = (h - ph) / 2;
281+ h = ph;
282+ }
298283 }
284+ if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
285+ die("could not allocate memory");
286+ imlib_free_image_and_decache();
299287 }
300− if ((im = imlib_create_cropped_image(x, y, w, h)) == NULL)
301− die("could not allocate memory");
302− imlib_free_image_and_decache();
288+ unlink(tmppath);
303289 }
304− unlink(tmppath);
290+ exif_data_unref(ed);
305291 }
306− exif_data_unref(ed);
307292 }
308293 #endif
309294 if (im == NULL && (access(file->path, R_OK) < 0 ||
@@ -337,7 +322,7 @@ bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
337322
338323 imlib_free_image_and_decache();
339324
340− if (use_cache && !cache_hit)
325+ if (!cache_hit)
341326 tns_cache_write(t, true);
342327
343328 tns->dirty = true;
M util.c View
@@ -324,14 +324,8 @@ int r_mkdir(const char *path)
324324 if (path == NULL || *path == '\0')
325325 return -1;
326326
327− if (stat(path, &stats) == 0) {
328− if (S_ISDIR(stats.st_mode)) {
329− return 0;
330− } else {
331− warn("not a directory: %s", path);
332− return -1;
333− }
334− }
327+ if (stat(path, &stats) == 0)
328+ return S_ISDIR(stats.st_mode) ? 0 : -1;
335329
336330 d = dir = (char*) s_malloc(strlen(path) + 1);
337331 strcpy(dir, path);
@@ -346,7 +340,6 @@ int r_mkdir(const char *path)
346340 err = -1;
347341 }
348342 } else if (stat(dir, &stats) < 0 || !S_ISDIR(stats.st_mode)) {
349− warn("not a directory: %s", dir);
350343 err = -1;
351344 }
352345 if (d != NULL)