A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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