A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

514 lignes
12 KiB

  1. /* Copyright 2011 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _THUMBS_CONFIG
  20. #define _RENDER_CONFIG
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <utime.h>
  27. #include "exif.h"
  28. #include "thumbs.h"
  29. #include "util.h"
  30. #include "config.h"
  31. static const int thumb_dim = THUMB_SIZE + 10;
  32. static char *cache_dir = NULL;
  33. bool tns_cache_enabled(void)
  34. {
  35. struct stat stats;
  36. return cache_dir != NULL && stat(cache_dir, &stats) == 0 &&
  37. S_ISDIR(stats.st_mode) && access(cache_dir, W_OK) == 0;
  38. }
  39. char* tns_cache_filepath(const char *filepath)
  40. {
  41. size_t len;
  42. char *cfile = NULL;
  43. if (cache_dir == NULL || filepath == NULL || *filepath != '/')
  44. return NULL;
  45. if (strncmp(filepath, cache_dir, strlen(cache_dir)) != 0) {
  46. /* don't cache images inside the cache directory! */
  47. len = strlen(cache_dir) + strlen(filepath) + 6;
  48. cfile = (char*) s_malloc(len);
  49. snprintf(cfile, len, "%s/%s.png", cache_dir, filepath + 1);
  50. }
  51. return cfile;
  52. }
  53. Imlib_Image tns_cache_load(const char *filepath)
  54. {
  55. char *cfile;
  56. struct stat cstats, fstats;
  57. Imlib_Image im = NULL;
  58. if (filepath == NULL)
  59. return NULL;
  60. if (stat(filepath, &fstats) < 0)
  61. return NULL;
  62. if ((cfile = tns_cache_filepath(filepath)) != NULL) {
  63. if (stat(cfile, &cstats) == 0 && cstats.st_mtime == fstats.st_mtime)
  64. im = imlib_load_image(cfile);
  65. free(cfile);
  66. }
  67. return im;
  68. }
  69. void tns_cache_write(thumb_t *t, bool force)
  70. {
  71. char *cfile, *dirend;
  72. struct stat cstats, fstats;
  73. struct utimbuf times;
  74. Imlib_Load_Error err = 0;
  75. if (t == NULL || t->im == NULL)
  76. return;
  77. if (t->file == NULL || t->file->name == NULL || t->file->path == NULL)
  78. return;
  79. if (stat(t->file->path, &fstats) < 0)
  80. return;
  81. if ((cfile = tns_cache_filepath(t->file->path)) != NULL) {
  82. if (force || stat(cfile, &cstats) < 0 ||
  83. cstats.st_mtime != fstats.st_mtime)
  84. {
  85. if ((dirend = strrchr(cfile, '/')) != NULL) {
  86. *dirend = '\0';
  87. err = r_mkdir(cfile);
  88. *dirend = '/';
  89. }
  90. if (err == 0) {
  91. imlib_context_set_image(t->im);
  92. imlib_image_set_format("png");
  93. imlib_save_image_with_error_return(cfile, &err);
  94. }
  95. if (err == 0) {
  96. times.actime = fstats.st_atime;
  97. times.modtime = fstats.st_mtime;
  98. utime(cfile, &times);
  99. } else {
  100. warn("could not cache thumbnail: %s", t->file->name);
  101. }
  102. }
  103. free(cfile);
  104. }
  105. }
  106. void tns_clean_cache(tns_t *tns)
  107. {
  108. int dirlen;
  109. bool delete;
  110. char *cfile, *filename, *tpos;
  111. r_dir_t dir;
  112. if (cache_dir == NULL)
  113. return;
  114. if (r_opendir(&dir, cache_dir) < 0) {
  115. warn("could not open thumbnail cache directory: %s", cache_dir);
  116. return;
  117. }
  118. dirlen = strlen(cache_dir);
  119. while ((cfile = r_readdir(&dir)) != NULL) {
  120. filename = cfile + dirlen;
  121. delete = false;
  122. if ((tpos = strrchr(filename, '.')) != NULL) {
  123. *tpos = '\0';
  124. if (access(filename, F_OK) < 0)
  125. delete = true;
  126. *tpos = '.';
  127. }
  128. if (delete) {
  129. if (unlink(cfile) < 0)
  130. warn("could not delete cache file: %s", cfile);
  131. }
  132. free(cfile);
  133. }
  134. r_closedir(&dir);
  135. }
  136. void tns_init(tns_t *tns, int cnt, win_t *win)
  137. {
  138. int len;
  139. const char *homedir, *dsuffix = "";
  140. if (tns == NULL)
  141. return;
  142. if (cnt > 0) {
  143. tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
  144. memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
  145. } else {
  146. tns->thumbs = NULL;
  147. }
  148. tns->cap = cnt;
  149. tns->cnt = tns->first = tns->sel = 0;
  150. tns->win = win;
  151. tns->alpha = !RENDER_WHITE_ALPHA;
  152. tns->dirty = false;
  153. if ((homedir = getenv("XDG_CACHE_HOME")) == NULL || homedir[0] == '\0') {
  154. homedir = getenv("HOME");
  155. dsuffix = "/.cache";
  156. }
  157. if (homedir != NULL) {
  158. if (cache_dir != NULL)
  159. free(cache_dir);
  160. len = strlen(homedir) + strlen(dsuffix) + 6;
  161. cache_dir = (char*) s_malloc(len);
  162. snprintf(cache_dir, len, "%s%s/sxiv", homedir, dsuffix);
  163. } else {
  164. warn("could not locate thumbnail cache directory");
  165. }
  166. }
  167. void tns_free(tns_t *tns)
  168. {
  169. int i;
  170. if (tns == NULL)
  171. return;
  172. if (tns->thumbs != NULL) {
  173. for (i = 0; i < tns->cnt; i++) {
  174. if (tns->thumbs[i].im != NULL) {
  175. imlib_context_set_image(tns->thumbs[i].im);
  176. imlib_free_image();
  177. }
  178. }
  179. free(tns->thumbs);
  180. tns->thumbs = NULL;
  181. }
  182. if (cache_dir != NULL) {
  183. free(cache_dir);
  184. cache_dir = NULL;
  185. }
  186. }
  187. bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
  188. bool force, bool silent)
  189. {
  190. int w, h;
  191. bool use_cache, cache_hit = false;
  192. float z, zw, zh;
  193. thumb_t *t;
  194. Imlib_Image im;
  195. const char *fmt;
  196. if (tns == NULL || tns->thumbs == NULL)
  197. return false;
  198. if (file == NULL || file->name == NULL || file->path == NULL)
  199. return false;
  200. if (n < 0 || n >= tns->cap)
  201. return false;
  202. t = &tns->thumbs[n];
  203. t->file = file;
  204. if (t->im != NULL) {
  205. imlib_context_set_image(t->im);
  206. imlib_free_image();
  207. }
  208. if ((use_cache = tns_cache_enabled())) {
  209. if (!force && (im = tns_cache_load(file->path)) != NULL)
  210. cache_hit = true;
  211. }
  212. if (!cache_hit) {
  213. if (access(file->path, R_OK) < 0 ||
  214. (im = imlib_load_image(file->path)) == NULL)
  215. {
  216. if (!silent)
  217. warn("could not open image: %s", file->name);
  218. return false;
  219. }
  220. }
  221. imlib_context_set_image(im);
  222. imlib_context_set_anti_alias(1);
  223. if ((fmt = imlib_image_format()) == NULL) {
  224. if (!silent)
  225. warn("could not open image: %s", file->name);
  226. imlib_free_image_and_decache();
  227. return false;
  228. }
  229. if (STREQ(fmt, "jpeg"))
  230. exif_auto_orientate(file);
  231. w = imlib_image_get_width();
  232. h = imlib_image_get_height();
  233. zw = (float) THUMB_SIZE / (float) w;
  234. zh = (float) THUMB_SIZE / (float) h;
  235. z = MIN(zw, zh);
  236. z = MIN(z, 1.0);
  237. t->w = z * w;
  238. t->h = z * h;
  239. t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
  240. if (t->im == NULL)
  241. die("could not allocate memory");
  242. imlib_free_image_and_decache();
  243. if (use_cache && !cache_hit)
  244. tns_cache_write(t, true);
  245. tns->dirty = true;
  246. return true;
  247. }
  248. void tns_check_view(tns_t *tns, bool scrolled)
  249. {
  250. int r;
  251. if (tns == NULL)
  252. return;
  253. tns->first -= tns->first % tns->cols;
  254. r = tns->sel % tns->cols;
  255. if (scrolled) {
  256. /* move selection into visible area */
  257. if (tns->sel >= tns->first + tns->cols * tns->rows)
  258. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  259. else if (tns->sel < tns->first)
  260. tns->sel = tns->first + r;
  261. } else {
  262. /* scroll to selection */
  263. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  264. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  265. tns->dirty = true;
  266. } else if (tns->first > tns->sel) {
  267. tns->first = tns->sel - r;
  268. tns->dirty = true;
  269. }
  270. }
  271. }
  272. void tns_render(tns_t *tns)
  273. {
  274. thumb_t *t;
  275. win_t *win;
  276. int i, cnt, r, x, y;
  277. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  278. return;
  279. if (!tns->dirty)
  280. return;
  281. win = tns->win;
  282. win_clear(win);
  283. imlib_context_set_drawable(win->pm);
  284. tns->cols = MAX(1, win->w / thumb_dim);
  285. tns->rows = MAX(1, win->h / thumb_dim);
  286. if (tns->cnt < tns->cols * tns->rows) {
  287. tns->first = 0;
  288. cnt = tns->cnt;
  289. } else {
  290. tns_check_view(tns, false);
  291. cnt = tns->cols * tns->rows;
  292. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  293. tns->first -= r - r % tns->cols;
  294. if (r > 0)
  295. cnt -= r % tns->cols;
  296. }
  297. r = cnt % tns->cols ? 1 : 0;
  298. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  299. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  300. for (i = 0; i < cnt; i++) {
  301. t = &tns->thumbs[tns->first + i];
  302. t->x = x + (THUMB_SIZE - t->w) / 2;
  303. t->y = y + (THUMB_SIZE - t->h) / 2;
  304. imlib_context_set_image(t->im);
  305. if (!tns->alpha && imlib_image_has_alpha())
  306. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
  307. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  308. t->x, t->y, t->w, t->h);
  309. if (t->file->marked)
  310. tns_mark(tns, tns->first + i, true);
  311. if ((i + 1) % tns->cols == 0) {
  312. x = tns->x;
  313. y += thumb_dim;
  314. } else {
  315. x += thumb_dim;
  316. }
  317. }
  318. tns->dirty = false;
  319. tns_highlight(tns, tns->sel, true);
  320. }
  321. void tns_mark(tns_t *tns, int n, bool mark)
  322. {
  323. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  324. return;
  325. if (n >= 0 && n < tns->cnt) {
  326. unsigned long col;
  327. thumb_t *t = &tns->thumbs[n];
  328. win_t *win = tns->win;
  329. int x = t->x, y = t->y, w = t->w, h = t->h;
  330. if (mark || n == tns->sel)
  331. col = win->selcol;
  332. else if (win->fullscreen)
  333. col = win->fscol;
  334. else
  335. col = win->bgcol;
  336. win_draw_rect(win, win->pm, x - 4, y - 4, 8, 2, true, 0, col);
  337. win_draw_rect(win, win->pm, x - 4, y - 4, 2, 8, true, 0, col);
  338. win_draw_rect(win, win->pm, x + w - 4, y - 4, 8, 2, true, 0, col);
  339. win_draw_rect(win, win->pm, x + w + 2, y - 4, 2, 8, true, 0, col);
  340. win_draw_rect(win, win->pm, x - 4, y + h + 2, 8, 2, true, 0, col);
  341. win_draw_rect(win, win->pm, x - 4, y + h - 4, 2, 8, true, 0, col);
  342. win_draw_rect(win, win->pm, x + w - 4, y + h + 2, 8, 2, true, 0, col);
  343. win_draw_rect(win, win->pm, x + w + 2, y + h - 4, 2, 8, true, 0, col);
  344. }
  345. }
  346. void tns_highlight(tns_t *tns, int n, bool hl)
  347. {
  348. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  349. return;
  350. if (n >= 0 && n < tns->cnt) {
  351. unsigned long col;
  352. thumb_t *t = &tns->thumbs[n];
  353. win_t *win = tns->win;
  354. if (hl)
  355. col = win->selcol;
  356. else if (win->fullscreen)
  357. col = win->fscol;
  358. else
  359. col = win->bgcol;
  360. win_draw_rect(win, win->pm, t->x - 3, t->y - 3, t->w + 6, t->h + 6,
  361. false, 2, col);
  362. if (!hl && t->file->marked)
  363. tns_mark(tns, n, true);
  364. }
  365. }
  366. bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
  367. {
  368. int old, max;
  369. if (tns == NULL || tns->thumbs == NULL)
  370. return false;
  371. old = tns->sel;
  372. cnt = cnt > 1 ? cnt : 1;
  373. switch (dir) {
  374. case DIR_UP:
  375. tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
  376. break;
  377. case DIR_DOWN:
  378. max = tns->cols * ((tns->cnt - 1) / tns->cols) +
  379. MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
  380. tns->sel = MIN(tns->sel + cnt * tns->cols, max);
  381. break;
  382. case DIR_LEFT:
  383. tns->sel = MAX(tns->sel - cnt, 0);
  384. break;
  385. case DIR_RIGHT:
  386. tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
  387. break;
  388. }
  389. if (tns->sel != old) {
  390. tns_highlight(tns, old, false);
  391. tns_check_view(tns, false);
  392. if (!tns->dirty)
  393. tns_highlight(tns, tns->sel, true);
  394. }
  395. return tns->sel != old;
  396. }
  397. bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
  398. {
  399. int d, max, old;
  400. if (tns == NULL)
  401. return false;
  402. old = tns->first;
  403. d = tns->cols * (screen ? tns->rows : 1);
  404. if (dir == DIR_DOWN) {
  405. max = tns->cnt - tns->cols * tns->rows;
  406. if (tns->cnt % tns->cols != 0)
  407. max += tns->cols - tns->cnt % tns->cols;
  408. tns->first = MIN(tns->first + d, max);
  409. } else if (dir == DIR_UP) {
  410. tns->first = MAX(tns->first - d, 0);
  411. }
  412. if (tns->first != old) {
  413. tns_check_view(tns, true);
  414. tns->dirty = true;
  415. }
  416. return tns->first != old;
  417. }
  418. int tns_translate(tns_t *tns, int x, int y)
  419. {
  420. int n;
  421. if (tns == NULL || tns->thumbs == NULL)
  422. return -1;
  423. if (x < tns->x || y < tns->y)
  424. return -1;
  425. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  426. (x - tns->x) / thumb_dim;
  427. if (n >= tns->cnt)
  428. n = -1;
  429. return n;
  430. }