A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

thumbs.c 9.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /* sxiv: thumbs.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #define _THUMBS_CONFIG
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include "thumbs.h"
  26. #include "util.h"
  27. #include "config.h"
  28. #ifdef __NetBSD__
  29. #define st_mtim st_mtimespec
  30. #define st_atim st_atimespec
  31. #endif
  32. const int thumb_dim = THUMB_SIZE + 10;
  33. char *cache_dir = NULL;
  34. int tns_cache_enabled() {
  35. struct stat stats;
  36. return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
  37. !access(cache_dir, W_OK);
  38. }
  39. char* tns_cache_filepath(const char *filepath) {
  40. size_t len;
  41. char *cfile = NULL;
  42. if (!cache_dir || !filepath || *filepath != '/')
  43. return NULL;
  44. if (strncmp(filepath, cache_dir, strlen(cache_dir))) {
  45. len = strlen(cache_dir) + strlen(filepath) + 6;
  46. cfile = (char*) s_malloc(len);
  47. snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
  48. }
  49. return cfile;
  50. }
  51. Imlib_Image* tns_cache_load(const char *filepath) {
  52. char *cfile;
  53. struct stat cstats, fstats;
  54. Imlib_Image *im = NULL;
  55. if (!filepath)
  56. return NULL;
  57. if (stat(filepath, &fstats))
  58. return NULL;
  59. if ((cfile = tns_cache_filepath(filepath))) {
  60. if (!stat(cfile, &cstats) &&
  61. cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
  62. cstats.st_mtim.tv_nsec / 1000 == fstats.st_mtim.tv_nsec / 1000)
  63. {
  64. im = imlib_load_image(cfile);
  65. }
  66. free(cfile);
  67. }
  68. return im;
  69. }
  70. void tns_cache_write(thumb_t *t, Bool force) {
  71. char *cfile, *dirend;
  72. struct stat cstats, fstats;
  73. struct timeval times[2];
  74. Imlib_Load_Error err = 0;
  75. if (!t || !t->im || !t->file || !t->file->name || !t->file->path)
  76. return;
  77. if (stat(t->file->path, &fstats))
  78. return;
  79. if ((cfile = tns_cache_filepath(t->file->path))) {
  80. if (force || stat(cfile, &cstats) ||
  81. cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
  82. cstats.st_mtim.tv_nsec / 1000 != fstats.st_mtim.tv_nsec / 1000)
  83. {
  84. if ((dirend = strrchr(cfile, '/'))) {
  85. *dirend = '\0';
  86. err = r_mkdir(cfile);
  87. *dirend = '/';
  88. }
  89. if (!err) {
  90. imlib_context_set_image(t->im);
  91. imlib_image_set_format("png");
  92. imlib_save_image_with_error_return(cfile, &err);
  93. }
  94. if (err) {
  95. warn("could not cache thumbnail: %s", t->file->name);
  96. } else {
  97. TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
  98. TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
  99. utimes(cfile, times);
  100. }
  101. }
  102. free(cfile);
  103. }
  104. }
  105. void tns_clean_cache(tns_t *tns) {
  106. int dirlen, delete;
  107. char *cfile, *filename, *tpos;
  108. r_dir_t dir;
  109. if (!cache_dir)
  110. return;
  111. if (r_opendir(&dir, cache_dir)) {
  112. warn("could not open thumbnail cache directory: %s", cache_dir);
  113. return;
  114. }
  115. dirlen = strlen(cache_dir);
  116. while ((cfile = r_readdir(&dir))) {
  117. filename = cfile + dirlen;
  118. delete = 0;
  119. if ((tpos = strrchr(filename, '.'))) {
  120. *tpos = '\0';
  121. delete = access(filename, F_OK);
  122. *tpos = '.';
  123. }
  124. if (delete && unlink(cfile))
  125. warn("could not delete cache file: %s", cfile);
  126. free(cfile);
  127. }
  128. r_closedir(&dir);
  129. }
  130. void tns_init(tns_t *tns, int cnt) {
  131. int len;
  132. char *homedir;
  133. if (!tns)
  134. return;
  135. if (cnt) {
  136. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  137. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  138. } else {
  139. tns->thumbs = NULL;
  140. }
  141. tns->cnt = tns->first = tns->sel = 0;
  142. tns->cap = cnt;
  143. tns->dirty = 0;
  144. if ((homedir = getenv("HOME"))) {
  145. if (cache_dir)
  146. free(cache_dir);
  147. len = strlen(homedir) + 10;
  148. cache_dir = (char*) s_malloc(len * sizeof(char));
  149. snprintf(cache_dir, len, "%s/.sxiv", homedir);
  150. } else {
  151. warn("could not locate thumbnail cache directory");
  152. }
  153. }
  154. void tns_free(tns_t *tns) {
  155. int i;
  156. if (!tns)
  157. return;
  158. if (tns->thumbs) {
  159. for (i = 0; i < tns->cnt; i++) {
  160. if (tns->thumbs[i].im) {
  161. imlib_context_set_image(tns->thumbs[i].im);
  162. imlib_free_image();
  163. }
  164. }
  165. free(tns->thumbs);
  166. tns->thumbs = NULL;
  167. }
  168. if (cache_dir) {
  169. free(cache_dir);
  170. cache_dir = NULL;
  171. }
  172. }
  173. int tns_load(tns_t *tns, int n, const fileinfo_t *file, unsigned char silent) {
  174. int w, h;
  175. int use_cache, cached = 0;
  176. float z, zw, zh;
  177. thumb_t *t;
  178. Imlib_Image *im;
  179. if (!tns || !tns->thumbs || !file || !file->name || !file->path)
  180. return 0;
  181. if (n < 0 || n >= tns->cap)
  182. return 0;
  183. t = &tns->thumbs[n];
  184. t->file = file;
  185. if (t->im) {
  186. imlib_context_set_image(t->im);
  187. imlib_free_image();
  188. }
  189. if ((use_cache = tns_cache_enabled())) {
  190. if ((im = tns_cache_load(file->path)))
  191. cached = 1;
  192. }
  193. if (!cached &&
  194. (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
  195. {
  196. if (!silent)
  197. warn("could not open image: %s", file->name);
  198. return 0;
  199. }
  200. imlib_context_set_image(im);
  201. imlib_context_set_anti_alias(1);
  202. w = imlib_image_get_width();
  203. h = imlib_image_get_height();
  204. zw = (float) THUMB_SIZE / (float) w;
  205. zh = (float) THUMB_SIZE / (float) h;
  206. z = MIN(zw, zh);
  207. t->w = z * w;
  208. t->h = z * h;
  209. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  210. die("could not allocate memory");
  211. imlib_free_image_and_decache();
  212. if (use_cache && !cached)
  213. tns_cache_write(t, False);
  214. tns->dirty = 1;
  215. return 1;
  216. }
  217. void tns_check_view(tns_t *tns, Bool scrolled) {
  218. int r;
  219. if (!tns)
  220. return;
  221. tns->first -= tns->first % tns->cols;
  222. r = tns->sel % tns->cols;
  223. if (scrolled) {
  224. /* move selection into visible area */
  225. if (tns->sel >= tns->first + tns->cols * tns->rows)
  226. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  227. else if (tns->sel < tns->first)
  228. tns->sel = tns->first + r;
  229. } else {
  230. /* scroll to selection */
  231. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  232. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  233. tns->dirty = 1;
  234. } else if (tns->first > tns->sel) {
  235. tns->first = tns->sel - r;
  236. tns->dirty = 1;
  237. }
  238. }
  239. }
  240. void tns_render(tns_t *tns, win_t *win) {
  241. int i, cnt, r, x, y;
  242. thumb_t *t;
  243. if (!tns || !tns->thumbs || !win)
  244. return;
  245. if (!tns->dirty)
  246. return;
  247. win_clear(win);
  248. imlib_context_set_drawable(win->pm);
  249. tns->cols = MAX(1, win->w / thumb_dim);
  250. tns->rows = MAX(1, win->h / thumb_dim);
  251. if (tns->cnt < tns->cols * tns->rows) {
  252. tns->first = 0;
  253. cnt = tns->cnt;
  254. } else {
  255. tns_check_view(tns, False);
  256. cnt = tns->cols * tns->rows;
  257. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  258. tns->first -= r - r % tns->cols;
  259. if (r > 0)
  260. cnt -= r % tns->cols;
  261. }
  262. r = cnt % tns->cols ? 1 : 0;
  263. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  264. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  265. for (i = 0; i < cnt; i++) {
  266. t = &tns->thumbs[tns->first + i];
  267. t->x = x + (THUMB_SIZE - t->w) / 2;
  268. t->y = y + (THUMB_SIZE - t->h) / 2;
  269. imlib_context_set_image(t->im);
  270. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  271. t->x, t->y, t->w, t->h);
  272. if ((i + 1) % tns->cols == 0) {
  273. x = tns->x;
  274. y += thumb_dim;
  275. } else {
  276. x += thumb_dim;
  277. }
  278. }
  279. tns->dirty = 0;
  280. tns_highlight(tns, win, tns->sel, True);
  281. }
  282. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  283. thumb_t *t;
  284. unsigned long col;
  285. if (!tns || !tns->thumbs || !win)
  286. return;
  287. if (n >= 0 && n < tns->cnt) {
  288. t = &tns->thumbs[n];
  289. if (hl)
  290. col = win->selcol;
  291. else if (win->fullscreen)
  292. col = win->black;
  293. else
  294. col = win->bgcol;
  295. win_draw_rect(win, win->pm, t->x - 4, t->y - 4, t->w + 8, t->h + 8,
  296. False, 2, col);
  297. }
  298. win_draw(win);
  299. }
  300. int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
  301. int old;
  302. if (!tns || !tns->thumbs || !win)
  303. return 0;
  304. old = tns->sel;
  305. switch (dir) {
  306. case DIR_LEFT:
  307. if (tns->sel > 0)
  308. tns->sel--;
  309. break;
  310. case DIR_RIGHT:
  311. if (tns->sel < tns->cnt - 1)
  312. tns->sel++;
  313. break;
  314. case DIR_UP:
  315. if (tns->sel >= tns->cols)
  316. tns->sel -= tns->cols;
  317. break;
  318. case DIR_DOWN:
  319. if (tns->sel + tns->cols < tns->cnt)
  320. tns->sel += tns->cols;
  321. break;
  322. }
  323. if (tns->sel != old) {
  324. tns_highlight(tns, win, old, False);
  325. tns_check_view(tns, False);
  326. if (!tns->dirty)
  327. tns_highlight(tns, win, tns->sel, True);
  328. }
  329. return tns->sel != old;
  330. }
  331. int tns_scroll(tns_t *tns, direction_t dir) {
  332. int old;
  333. if (!tns)
  334. return 0;
  335. old = tns->first;
  336. if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  337. tns->first += tns->cols;
  338. tns_check_view(tns, True);
  339. tns->dirty = 1;
  340. } else if (dir == DIR_UP && tns->first >= tns->cols) {
  341. tns->first -= tns->cols;
  342. tns_check_view(tns, True);
  343. tns->dirty = 1;
  344. }
  345. return tns->first != old;
  346. }
  347. int tns_translate(tns_t *tns, int x, int y) {
  348. int n;
  349. thumb_t *t;
  350. if (!tns || !tns->thumbs)
  351. return -1;
  352. if (x < tns->x || y < tns->y)
  353. return -1;
  354. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  355. (x - tns->x) / thumb_dim;
  356. if (n < tns->cnt) {
  357. t = &tns->thumbs[n];
  358. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  359. return n;
  360. }
  361. return -1;
  362. }