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.
 
 
 
 
 
 

470 lines
10 KiB

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