A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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