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.
 
 
 
 
 
 

458 líneas
9.6 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. 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->alpha = 1;
  144. tns->dirty = 0;
  145. if ((homedir = getenv("HOME"))) {
  146. if (cache_dir)
  147. free(cache_dir);
  148. len = strlen(homedir) + 10;
  149. cache_dir = (char*) s_malloc(len * sizeof(char));
  150. snprintf(cache_dir, len, "%s/.sxiv", homedir);
  151. } else {
  152. warn("could not locate thumbnail cache directory");
  153. }
  154. }
  155. void tns_free(tns_t *tns) {
  156. int i;
  157. if (!tns)
  158. return;
  159. if (tns->thumbs) {
  160. for (i = 0; i < tns->cnt; i++) {
  161. if (tns->thumbs[i].im) {
  162. imlib_context_set_image(tns->thumbs[i].im);
  163. imlib_free_image();
  164. }
  165. }
  166. free(tns->thumbs);
  167. tns->thumbs = NULL;
  168. }
  169. if (cache_dir) {
  170. free(cache_dir);
  171. cache_dir = NULL;
  172. }
  173. }
  174. int tns_load(tns_t *tns, int n, const fileinfo_t *file,
  175. Bool force, Bool silent)
  176. {
  177. int w, h;
  178. int use_cache, cache_hit = 0;
  179. float z, zw, zh;
  180. thumb_t *t;
  181. Imlib_Image *im;
  182. if (!tns || !tns->thumbs || !file || !file->name || !file->path)
  183. return 0;
  184. if (n < 0 || n >= tns->cap)
  185. return 0;
  186. t = &tns->thumbs[n];
  187. t->file = file;
  188. if (t->im) {
  189. imlib_context_set_image(t->im);
  190. imlib_free_image();
  191. }
  192. if ((use_cache = tns_cache_enabled())) {
  193. if (!force && (im = tns_cache_load(file->path)))
  194. cache_hit = 1;
  195. }
  196. if (!cache_hit &&
  197. (access(file->path, R_OK) || !(im = imlib_load_image(file->path))))
  198. {
  199. if (!silent)
  200. warn("could not open image: %s", file->name);
  201. return 0;
  202. }
  203. imlib_context_set_image(im);
  204. imlib_context_set_anti_alias(1);
  205. w = imlib_image_get_width();
  206. h = imlib_image_get_height();
  207. zw = (float) THUMB_SIZE / (float) w;
  208. zh = (float) THUMB_SIZE / (float) h;
  209. z = MIN(zw, zh);
  210. t->w = z * w;
  211. t->h = z * h;
  212. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  213. die("could not allocate memory");
  214. imlib_free_image_and_decache();
  215. if (use_cache && !cache_hit)
  216. tns_cache_write(t, False);
  217. tns->dirty = 1;
  218. return 1;
  219. }
  220. void tns_check_view(tns_t *tns, Bool scrolled) {
  221. int r;
  222. if (!tns)
  223. return;
  224. tns->first -= tns->first % tns->cols;
  225. r = tns->sel % tns->cols;
  226. if (scrolled) {
  227. /* move selection into visible area */
  228. if (tns->sel >= tns->first + tns->cols * tns->rows)
  229. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  230. else if (tns->sel < tns->first)
  231. tns->sel = tns->first + r;
  232. } else {
  233. /* scroll to selection */
  234. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  235. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  236. tns->dirty = 1;
  237. } else if (tns->first > tns->sel) {
  238. tns->first = tns->sel - r;
  239. tns->dirty = 1;
  240. }
  241. }
  242. }
  243. void tns_render(tns_t *tns, win_t *win) {
  244. int i, cnt, r, x, y;
  245. thumb_t *t;
  246. if (!tns || !tns->thumbs || !win)
  247. return;
  248. if (!tns->dirty)
  249. return;
  250. win_clear(win);
  251. imlib_context_set_drawable(win->pm);
  252. tns->cols = MAX(1, win->w / thumb_dim);
  253. tns->rows = MAX(1, win->h / thumb_dim);
  254. if (tns->cnt < tns->cols * tns->rows) {
  255. tns->first = 0;
  256. cnt = tns->cnt;
  257. } else {
  258. tns_check_view(tns, False);
  259. cnt = tns->cols * tns->rows;
  260. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  261. tns->first -= r - r % tns->cols;
  262. if (r > 0)
  263. cnt -= r % tns->cols;
  264. }
  265. r = cnt % tns->cols ? 1 : 0;
  266. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  267. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  268. for (i = 0; i < cnt; i++) {
  269. t = &tns->thumbs[tns->first + i];
  270. t->x = x + (THUMB_SIZE - t->w) / 2;
  271. t->y = y + (THUMB_SIZE - t->h) / 2;
  272. imlib_context_set_image(t->im);
  273. if (imlib_image_has_alpha() && !tns->alpha)
  274. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, True, 0, win->white);
  275. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  276. t->x, t->y, t->w, t->h);
  277. if ((i + 1) % tns->cols == 0) {
  278. x = tns->x;
  279. y += thumb_dim;
  280. } else {
  281. x += thumb_dim;
  282. }
  283. }
  284. tns->dirty = 0;
  285. tns_highlight(tns, win, tns->sel, True);
  286. }
  287. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  288. thumb_t *t;
  289. int x, y;
  290. unsigned long col;
  291. if (!tns || !tns->thumbs || !win)
  292. return;
  293. if (n >= 0 && n < tns->cnt) {
  294. t = &tns->thumbs[n];
  295. if (hl)
  296. col = win->selcol;
  297. else if (win->fullscreen)
  298. col = win->black;
  299. else
  300. col = win->bgcol;
  301. x = t->x - (THUMB_SIZE - t->w) / 2;
  302. y = t->y - (THUMB_SIZE - t->h) / 2;
  303. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  304. False, 2, col);
  305. }
  306. win_draw(win);
  307. }
  308. int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
  309. int old;
  310. if (!tns || !tns->thumbs || !win)
  311. return 0;
  312. old = tns->sel;
  313. switch (dir) {
  314. case DIR_LEFT:
  315. if (tns->sel > 0)
  316. tns->sel--;
  317. break;
  318. case DIR_RIGHT:
  319. if (tns->sel < tns->cnt - 1)
  320. tns->sel++;
  321. break;
  322. case DIR_UP:
  323. if (tns->sel >= tns->cols)
  324. tns->sel -= tns->cols;
  325. break;
  326. case DIR_DOWN:
  327. if (tns->sel + tns->cols < tns->cnt)
  328. tns->sel += tns->cols;
  329. break;
  330. }
  331. if (tns->sel != old) {
  332. tns_highlight(tns, win, old, False);
  333. tns_check_view(tns, False);
  334. if (!tns->dirty)
  335. tns_highlight(tns, win, tns->sel, True);
  336. }
  337. return tns->sel != old;
  338. }
  339. int tns_scroll(tns_t *tns, direction_t dir) {
  340. int old;
  341. if (!tns)
  342. return 0;
  343. old = tns->first;
  344. if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  345. tns->first += tns->cols;
  346. tns_check_view(tns, True);
  347. tns->dirty = 1;
  348. } else if (dir == DIR_UP && tns->first >= tns->cols) {
  349. tns->first -= tns->cols;
  350. tns_check_view(tns, True);
  351. tns->dirty = 1;
  352. }
  353. return tns->first != old;
  354. }
  355. int tns_translate(tns_t *tns, int x, int y) {
  356. int n;
  357. if (!tns || !tns->thumbs)
  358. return -1;
  359. if (x < tns->x || y < tns->y)
  360. return -1;
  361. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  362. (x - tns->x) / thumb_dim;
  363. if (n >= tns->cnt)
  364. n = -1;
  365. return n;
  366. }