A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

453 lines
9.4 KiB

  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
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (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., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, 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,
  174. Bool force, Bool silent)
  175. {
  176. int w, h;
  177. int use_cache, cache_hit = 0;
  178. float z, zw, zh;
  179. thumb_t *t;
  180. Imlib_Image *im;
  181. if (!tns || !tns->thumbs || !file || !file->name || !file->path)
  182. return 0;
  183. if (n < 0 || n >= tns->cap)
  184. return 0;
  185. t = &tns->thumbs[n];
  186. t->file = file;
  187. if (t->im) {
  188. imlib_context_set_image(t->im);
  189. imlib_free_image();
  190. }
  191. if ((use_cache = tns_cache_enabled())) {
  192. if (!force && (im = tns_cache_load(file->path)))
  193. cache_hit = 1;
  194. }
  195. if (!cache_hit &&
  196. (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
  197. {
  198. if (!silent)
  199. warn("could not open image: %s", file->name);
  200. return 0;
  201. }
  202. imlib_context_set_image(im);
  203. imlib_context_set_anti_alias(1);
  204. w = imlib_image_get_width();
  205. h = imlib_image_get_height();
  206. zw = (float) THUMB_SIZE / (float) w;
  207. zh = (float) THUMB_SIZE / (float) h;
  208. z = MIN(zw, zh);
  209. t->w = z * w;
  210. t->h = z * h;
  211. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  212. die("could not allocate memory");
  213. imlib_free_image_and_decache();
  214. if (use_cache && !cache_hit)
  215. tns_cache_write(t, False);
  216. tns->dirty = 1;
  217. return 1;
  218. }
  219. void tns_check_view(tns_t *tns, Bool scrolled) {
  220. int r;
  221. if (!tns)
  222. return;
  223. tns->first -= tns->first % tns->cols;
  224. r = tns->sel % tns->cols;
  225. if (scrolled) {
  226. /* move selection into visible area */
  227. if (tns->sel >= tns->first + tns->cols * tns->rows)
  228. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  229. else if (tns->sel < tns->first)
  230. tns->sel = tns->first + r;
  231. } else {
  232. /* scroll to selection */
  233. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  234. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  235. tns->dirty = 1;
  236. } else if (tns->first > tns->sel) {
  237. tns->first = tns->sel - r;
  238. tns->dirty = 1;
  239. }
  240. }
  241. }
  242. void tns_render(tns_t *tns, win_t *win) {
  243. int i, cnt, r, x, y;
  244. thumb_t *t;
  245. if (!tns || !tns->thumbs || !win)
  246. return;
  247. if (!tns->dirty)
  248. return;
  249. win_clear(win);
  250. imlib_context_set_drawable(win->pm);
  251. tns->cols = MAX(1, win->w / thumb_dim);
  252. tns->rows = MAX(1, win->h / thumb_dim);
  253. if (tns->cnt < tns->cols * tns->rows) {
  254. tns->first = 0;
  255. cnt = tns->cnt;
  256. } else {
  257. tns_check_view(tns, False);
  258. cnt = tns->cols * tns->rows;
  259. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  260. tns->first -= r - r % tns->cols;
  261. if (r > 0)
  262. cnt -= r % tns->cols;
  263. }
  264. r = cnt % tns->cols ? 1 : 0;
  265. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  266. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  267. for (i = 0; i < cnt; i++) {
  268. t = &tns->thumbs[tns->first + i];
  269. t->x = x + (THUMB_SIZE - t->w) / 2;
  270. t->y = y + (THUMB_SIZE - t->h) / 2;
  271. imlib_context_set_image(t->im);
  272. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  273. t->x, t->y, t->w, t->h);
  274. if ((i + 1) % tns->cols == 0) {
  275. x = tns->x;
  276. y += thumb_dim;
  277. } else {
  278. x += thumb_dim;
  279. }
  280. }
  281. tns->dirty = 0;
  282. tns_highlight(tns, win, tns->sel, True);
  283. }
  284. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  285. thumb_t *t;
  286. int x, y;
  287. unsigned long col;
  288. if (!tns || !tns->thumbs || !win)
  289. return;
  290. if (n >= 0 && n < tns->cnt) {
  291. t = &tns->thumbs[n];
  292. if (hl)
  293. col = win->selcol;
  294. else if (win->fullscreen)
  295. col = win->black;
  296. else
  297. col = win->bgcol;
  298. x = t->x - (THUMB_SIZE - t->w) / 2;
  299. y = t->y - (THUMB_SIZE - t->h) / 2;
  300. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  301. False, 2, col);
  302. }
  303. win_draw(win);
  304. }
  305. int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
  306. int old;
  307. if (!tns || !tns->thumbs || !win)
  308. return 0;
  309. old = tns->sel;
  310. switch (dir) {
  311. case DIR_LEFT:
  312. if (tns->sel > 0)
  313. tns->sel--;
  314. break;
  315. case DIR_RIGHT:
  316. if (tns->sel < tns->cnt - 1)
  317. tns->sel++;
  318. break;
  319. case DIR_UP:
  320. if (tns->sel >= tns->cols)
  321. tns->sel -= tns->cols;
  322. break;
  323. case DIR_DOWN:
  324. if (tns->sel + tns->cols < tns->cnt)
  325. tns->sel += tns->cols;
  326. break;
  327. }
  328. if (tns->sel != old) {
  329. tns_highlight(tns, win, old, False);
  330. tns_check_view(tns, False);
  331. if (!tns->dirty)
  332. tns_highlight(tns, win, tns->sel, True);
  333. }
  334. return tns->sel != old;
  335. }
  336. int tns_scroll(tns_t *tns, direction_t dir) {
  337. int old;
  338. if (!tns)
  339. return 0;
  340. old = tns->first;
  341. if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  342. tns->first += tns->cols;
  343. tns_check_view(tns, True);
  344. tns->dirty = 1;
  345. } else if (dir == DIR_UP && tns->first >= tns->cols) {
  346. tns->first -= tns->cols;
  347. tns_check_view(tns, True);
  348. tns->dirty = 1;
  349. }
  350. return tns->first != old;
  351. }
  352. int tns_translate(tns_t *tns, int x, int y) {
  353. int n;
  354. if (!tns || !tns->thumbs)
  355. return -1;
  356. if (x < tns->x || y < tns->y)
  357. return -1;
  358. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  359. (x - tns->x) / thumb_dim;
  360. if (n >= tns->cnt)
  361. n = -1;
  362. return n;
  363. }