A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

383 lines
8.0 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
  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. #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 "config.h"
  25. #include "thumbs.h"
  26. #include "util.h"
  27. extern Imlib_Image *im_invalid;
  28. const int thumb_dim = THUMB_SIZE + 10;
  29. int tns_cache_enabled();
  30. void tns_cache_write(thumb_t*, Bool);
  31. void tns_init(tns_t *tns, int cnt) {
  32. if (!tns)
  33. return;
  34. tns->cnt = tns->first = tns->sel = 0;
  35. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  36. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  37. tns->cap = cnt;
  38. tns->dirty = 0;
  39. }
  40. void tns_free(tns_t *tns, win_t *win) {
  41. int i;
  42. Bool cache;
  43. if (!tns || !tns->thumbs)
  44. return;
  45. cache = tns_cache_enabled();
  46. for (i = 0; i < tns->cnt; ++i) {
  47. if (tns->thumbs[i].im) {
  48. if (cache)
  49. tns_cache_write(&tns->thumbs[i], False);
  50. imlib_context_set_image(tns->thumbs[i].im);
  51. imlib_free_image();
  52. }
  53. }
  54. free(tns->thumbs);
  55. tns->thumbs = NULL;
  56. }
  57. void tns_load(tns_t *tns, win_t *win, int n, const char *filename) {
  58. int w, h;
  59. float z, zw, zh;
  60. thumb_t *t;
  61. Imlib_Image *im;
  62. if (!tns || !win || !filename)
  63. return;
  64. if (n >= tns->cap)
  65. return;
  66. else if (n >= tns->cnt)
  67. tns->cnt = n + 1;
  68. t = &tns->thumbs[n];
  69. if (t->im) {
  70. imlib_context_set_image(t->im);
  71. imlib_free_image();
  72. }
  73. if ((im = imlib_load_image(filename)))
  74. imlib_context_set_image(im);
  75. else
  76. imlib_context_set_image(im_invalid);
  77. w = imlib_image_get_width();
  78. h = imlib_image_get_height();
  79. if (im) {
  80. t->filename = filename;
  81. zw = (float) THUMB_SIZE / (float) w;
  82. zh = (float) THUMB_SIZE / (float) h;
  83. z = MIN(zw, zh);
  84. } else {
  85. t->filename = NULL;
  86. z = 1.0;
  87. }
  88. t->w = z * w;
  89. t->h = z * h;
  90. imlib_context_set_anti_alias(1);
  91. if (!(t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h)))
  92. die("could not allocate memory");
  93. if (im)
  94. imlib_free_image_and_decache();
  95. tns->dirty = 1;
  96. }
  97. void tns_check_view(tns_t *tns, Bool scrolled) {
  98. int r;
  99. if (!tns)
  100. return;
  101. tns->first -= tns->first % tns->cols;
  102. r = tns->sel % tns->cols;
  103. if (scrolled) {
  104. /* move selection into visible area */
  105. if (tns->sel >= tns->first + tns->cols * tns->rows)
  106. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  107. else if (tns->sel < tns->first)
  108. tns->sel = tns->first + r;
  109. } else {
  110. /* scroll to selection */
  111. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  112. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  113. tns->dirty = 1;
  114. } else if (tns->first > tns->sel) {
  115. tns->first = tns->sel - r;
  116. tns->dirty = 1;
  117. }
  118. }
  119. }
  120. void tns_render(tns_t *tns, win_t *win) {
  121. int i, cnt, r, x, y;
  122. thumb_t *t;
  123. if (!tns || !tns->dirty || !win)
  124. return;
  125. win_clear(win);
  126. imlib_context_set_drawable(win->pm);
  127. tns->cols = MAX(1, win->w / thumb_dim);
  128. tns->rows = MAX(1, win->h / thumb_dim);
  129. if (tns->cnt < tns->cols * tns->rows) {
  130. tns->first = 0;
  131. cnt = tns->cnt;
  132. } else {
  133. tns_check_view(tns, False);
  134. cnt = tns->cols * tns->rows;
  135. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  136. tns->first -= r - r % tns->cols;
  137. if (r > 0)
  138. cnt -= r % tns->cols;
  139. }
  140. r = cnt % tns->cols ? 1 : 0;
  141. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  142. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  143. for (i = 0; i < cnt; ++i) {
  144. t = &tns->thumbs[tns->first + i];
  145. t->x = x + (THUMB_SIZE - t->w) / 2;
  146. t->y = y + (THUMB_SIZE - t->h) / 2;
  147. imlib_context_set_image(t->im);
  148. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  149. t->x, t->y, t->w, t->h);
  150. if ((i + 1) % tns->cols == 0) {
  151. x = tns->x;
  152. y += thumb_dim;
  153. } else {
  154. x += thumb_dim;
  155. }
  156. }
  157. tns->dirty = 0;
  158. tns_highlight(tns, win, tns->sel, True);
  159. }
  160. void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
  161. thumb_t *t;
  162. unsigned long col;
  163. if (!tns || !win)
  164. return;
  165. if (n >= 0 && n < tns->cnt) {
  166. t = &tns->thumbs[n];
  167. if (hl)
  168. col = win->selcol;
  169. else if (win->fullscreen)
  170. col = win->black;
  171. else
  172. col = win->bgcol;
  173. win_draw_rect(win, win->pm, t->x - 2, t->y - 2, t->w + 4, t->h + 4,
  174. False, 2, col);
  175. }
  176. win_draw(win);
  177. }
  178. int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
  179. int old;
  180. if (!tns || !win)
  181. return 0;
  182. old = tns->sel;
  183. switch (dir) {
  184. case TNS_LEFT:
  185. if (tns->sel > 0)
  186. --tns->sel;
  187. break;
  188. case TNS_RIGHT:
  189. if (tns->sel < tns->cnt - 1)
  190. ++tns->sel;
  191. break;
  192. case TNS_UP:
  193. if (tns->sel >= tns->cols)
  194. tns->sel -= tns->cols;
  195. break;
  196. case TNS_DOWN:
  197. if (tns->sel + tns->cols < tns->cnt)
  198. tns->sel += tns->cols;
  199. break;
  200. }
  201. if (tns->sel != old) {
  202. tns_highlight(tns, win, old, False);
  203. tns_check_view(tns, False);
  204. if (!tns->dirty)
  205. tns_highlight(tns, win, tns->sel, True);
  206. }
  207. return tns->sel != old;
  208. }
  209. int tns_scroll(tns_t *tns, tnsdir_t dir) {
  210. int old;
  211. if (!tns)
  212. return 0;
  213. old = tns->first;
  214. if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
  215. tns->first += tns->cols;
  216. tns_check_view(tns, True);
  217. tns->dirty = 1;
  218. } else if (dir == TNS_UP && tns->first >= tns->cols) {
  219. tns->first -= tns->cols;
  220. tns_check_view(tns, True);
  221. tns->dirty = 1;
  222. }
  223. return tns->first != old;
  224. }
  225. int tns_translate(tns_t *tns, int x, int y) {
  226. int n;
  227. thumb_t *t;
  228. if (!tns || x < tns->x || y < tns->y)
  229. return -1;
  230. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  231. (x - tns->x) / thumb_dim;
  232. if (n < tns->cnt) {
  233. t = &tns->thumbs[n];
  234. if (x >= t->x && x <= t->x + t->w && y >= t->y && y <= t->y + t->h)
  235. return n;
  236. }
  237. return -1;
  238. }
  239. /* thumbnail caching */
  240. int tns_cache_enabled() {
  241. int len, ret = 0;
  242. char *cpath, *homedir;
  243. struct stat stats;
  244. if ((homedir = getenv("HOME"))) {
  245. len = strlen(homedir) + 10;
  246. cpath = (char*) s_malloc(len * sizeof(char));
  247. snprintf(cpath, len, "%s/.sxiv", homedir);
  248. ret = !stat(cpath, &stats) && S_ISDIR(stats.st_mode) &&
  249. !access(cpath, W_OK);
  250. free(cpath);
  251. }
  252. return ret;
  253. }
  254. char* tns_cache_filename(const char *filename) {
  255. size_t len;
  256. int i;
  257. char *cfile, *abspath, *homedir;
  258. if (!filename)
  259. return NULL;
  260. if (!(homedir = getenv("HOME")))
  261. return NULL;
  262. if (*filename != '/') {
  263. if (!(abspath = absolute_path(filename)))
  264. return NULL;
  265. } else {
  266. abspath = (char*) s_malloc(strlen(filename) + 1);
  267. strcpy(abspath, filename);
  268. }
  269. len = strlen(abspath);
  270. for (i = 1; i < len; ++i) {
  271. if (abspath[i] == '/')
  272. abspath[i] = '%';
  273. }
  274. len += strlen(homedir) + 15;
  275. cfile = (char*) s_malloc(len);
  276. snprintf(cfile, len, "%s/.sxiv/%s.png", homedir, abspath + 1);
  277. free(abspath);
  278. return cfile;
  279. }
  280. void tns_cache_write(thumb_t *t, Bool force) {
  281. char *cfile;
  282. struct stat cstats, fstats;
  283. struct timeval times[2];
  284. Imlib_Load_Error err;
  285. if (!t || !t->im || !t->filename)
  286. return;
  287. if ((cfile = tns_cache_filename(t->filename))) {
  288. if (stat(t->filename, &fstats))
  289. goto end;
  290. if (force || stat(cfile, &cstats) ||
  291. cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
  292. cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
  293. {
  294. imlib_context_set_image(t->im);
  295. imlib_image_set_format("png");
  296. imlib_save_image_with_error_return(cfile, &err);
  297. if (err) {
  298. warn("could not cache thumbnail:", t->filename);
  299. } else {
  300. TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
  301. TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
  302. utimes(cfile, times);
  303. }
  304. }
  305. }
  306. end:
  307. free(cfile);
  308. }