A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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