A Simple X Image Viewer
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

466 行
10 KiB

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