A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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