A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

483 líneas
10 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. #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 "exif.h"
  27. #include "thumbs.h"
  28. #include "util.h"
  29. #include "config.h"
  30. static const int thumb_dim = THUMB_SIZE + 10;
  31. static const char * const CACHE_DIR = ".sxiv/cache";
  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. char *homedir;
  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 = true;
  152. tns->dirty = false;
  153. if ((homedir = getenv("HOME")) != NULL) {
  154. if (cache_dir != NULL)
  155. free(cache_dir);
  156. len = strlen(homedir) + strlen(CACHE_DIR) + 2;
  157. cache_dir = (char*) s_malloc(len);
  158. snprintf(cache_dir, len, "%s/%s", homedir, CACHE_DIR);
  159. } else {
  160. warn("could not locate thumbnail cache directory");
  161. }
  162. }
  163. void tns_free(tns_t *tns)
  164. {
  165. int i;
  166. if (tns == NULL)
  167. return;
  168. if (tns->thumbs != NULL) {
  169. for (i = 0; i < tns->cnt; i++) {
  170. if (tns->thumbs[i].im != NULL) {
  171. imlib_context_set_image(tns->thumbs[i].im);
  172. imlib_free_image();
  173. }
  174. }
  175. free(tns->thumbs);
  176. tns->thumbs = NULL;
  177. }
  178. if (cache_dir != NULL) {
  179. free(cache_dir);
  180. cache_dir = NULL;
  181. }
  182. }
  183. bool tns_load(tns_t *tns, int n, const fileinfo_t *file,
  184. bool force, bool silent)
  185. {
  186. int w, h;
  187. bool use_cache, cache_hit = false;
  188. float z, zw, zh;
  189. thumb_t *t;
  190. Imlib_Image *im;
  191. const char *fmt;
  192. if (tns == NULL || tns->thumbs == NULL)
  193. return false;
  194. if (file == NULL || file->name == NULL || file->path == NULL)
  195. return false;
  196. if (n < 0 || n >= tns->cap)
  197. return false;
  198. t = &tns->thumbs[n];
  199. t->file = file;
  200. if (t->im != NULL) {
  201. imlib_context_set_image(t->im);
  202. imlib_free_image();
  203. }
  204. if ((use_cache = tns_cache_enabled())) {
  205. if (!force && (im = tns_cache_load(file->path)) != NULL)
  206. cache_hit = true;
  207. }
  208. if (!cache_hit) {
  209. if (access(file->path, R_OK) < 0 ||
  210. (im = imlib_load_image(file->path)) == NULL)
  211. {
  212. if (!silent)
  213. warn("could not open image: %s", file->name);
  214. return false;
  215. }
  216. }
  217. imlib_context_set_image(im);
  218. imlib_context_set_anti_alias(1);
  219. if ((fmt = imlib_image_format()) == NULL) {
  220. if (!silent)
  221. warn("could not open image: %s", file->name);
  222. imlib_free_image_and_decache();
  223. return false;
  224. }
  225. if (STREQ(fmt, "jpeg"))
  226. exif_auto_orientate(file);
  227. w = imlib_image_get_width();
  228. h = imlib_image_get_height();
  229. zw = (float) THUMB_SIZE / (float) w;
  230. zh = (float) THUMB_SIZE / (float) h;
  231. z = MIN(zw, zh);
  232. z = MIN(z, 1.0);
  233. t->w = z * w;
  234. t->h = z * h;
  235. t->im = imlib_create_cropped_scaled_image(0, 0, w, h, t->w, t->h);
  236. if (t->im == NULL)
  237. die("could not allocate memory");
  238. imlib_free_image_and_decache();
  239. if (use_cache && !cache_hit)
  240. tns_cache_write(t, true);
  241. tns->dirty = true;
  242. return true;
  243. }
  244. void tns_check_view(tns_t *tns, bool scrolled)
  245. {
  246. int r;
  247. if (tns == NULL)
  248. return;
  249. tns->first -= tns->first % tns->cols;
  250. r = tns->sel % tns->cols;
  251. if (scrolled) {
  252. /* move selection into visible area */
  253. if (tns->sel >= tns->first + tns->cols * tns->rows)
  254. tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
  255. else if (tns->sel < tns->first)
  256. tns->sel = tns->first + r;
  257. } else {
  258. /* scroll to selection */
  259. if (tns->first + tns->cols * tns->rows <= tns->sel) {
  260. tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
  261. tns->dirty = true;
  262. } else if (tns->first > tns->sel) {
  263. tns->first = tns->sel - r;
  264. tns->dirty = true;
  265. }
  266. }
  267. }
  268. void tns_render(tns_t *tns)
  269. {
  270. thumb_t *t;
  271. win_t *win;
  272. int i, cnt, r, x, y;
  273. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  274. return;
  275. if (!tns->dirty)
  276. return;
  277. win = tns->win;
  278. win_clear(win);
  279. imlib_context_set_drawable(win->pm);
  280. tns->cols = MAX(1, win->w / thumb_dim);
  281. tns->rows = MAX(1, win->h / thumb_dim);
  282. if (tns->cnt < tns->cols * tns->rows) {
  283. tns->first = 0;
  284. cnt = tns->cnt;
  285. } else {
  286. tns_check_view(tns, false);
  287. cnt = tns->cols * tns->rows;
  288. if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
  289. tns->first -= r - r % tns->cols;
  290. if (r > 0)
  291. cnt -= r % tns->cols;
  292. }
  293. r = cnt % tns->cols ? 1 : 0;
  294. tns->x = x = (win->w - MIN(cnt, tns->cols) * thumb_dim) / 2 + 5;
  295. tns->y = y = (win->h - (cnt / tns->cols + r) * thumb_dim) / 2 + 5;
  296. for (i = 0; i < cnt; i++) {
  297. t = &tns->thumbs[tns->first + i];
  298. t->x = x + (THUMB_SIZE - t->w) / 2;
  299. t->y = y + (THUMB_SIZE - t->h) / 2;
  300. imlib_context_set_image(t->im);
  301. if (!tns->alpha && imlib_image_has_alpha())
  302. win_draw_rect(win, win->pm, t->x, t->y, t->w, t->h, true, 0, win->white);
  303. imlib_render_image_part_on_drawable_at_size(0, 0, t->w, t->h,
  304. t->x, t->y, t->w, t->h);
  305. if ((i + 1) % tns->cols == 0) {
  306. x = tns->x;
  307. y += thumb_dim;
  308. } else {
  309. x += thumb_dim;
  310. }
  311. }
  312. tns->dirty = false;
  313. tns_highlight(tns, tns->sel, true);
  314. }
  315. void tns_highlight(tns_t *tns, int n, bool hl)
  316. {
  317. thumb_t *t;
  318. win_t *win;
  319. int x, y;
  320. unsigned long col;
  321. if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
  322. return;
  323. win = tns->win;
  324. if (n >= 0 && n < tns->cnt) {
  325. t = &tns->thumbs[n];
  326. if (hl)
  327. col = win->selcol;
  328. else if (win->fullscreen)
  329. col = win->fscol;
  330. else
  331. col = win->bgcol;
  332. x = t->x - (THUMB_SIZE - t->w) / 2;
  333. y = t->y - (THUMB_SIZE - t->h) / 2;
  334. win_draw_rect(win, win->pm, x - 3, y - 3, THUMB_SIZE + 6, THUMB_SIZE + 6,
  335. false, 2, col);
  336. }
  337. }
  338. bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
  339. {
  340. int old, max;
  341. if (tns == NULL || tns->thumbs == NULL)
  342. return false;
  343. old = tns->sel;
  344. cnt = cnt > 1 ? cnt : 1;
  345. switch (dir) {
  346. case DIR_UP:
  347. tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
  348. break;
  349. case DIR_DOWN:
  350. max = tns->cols * ((tns->cnt - 1) / tns->cols) +
  351. MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
  352. tns->sel = MIN(tns->sel + cnt * tns->cols, max);
  353. break;
  354. case DIR_LEFT:
  355. tns->sel = MAX(tns->sel - cnt, 0);
  356. break;
  357. case DIR_RIGHT:
  358. tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
  359. break;
  360. }
  361. if (tns->sel != old) {
  362. tns_highlight(tns, old, false);
  363. tns_check_view(tns, false);
  364. if (!tns->dirty)
  365. tns_highlight(tns, tns->sel, true);
  366. }
  367. return tns->sel != old;
  368. }
  369. bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
  370. {
  371. int d, max, old;
  372. if (tns == NULL)
  373. return false;
  374. old = tns->first;
  375. d = tns->cols * (screen ? tns->rows : 1);
  376. if (dir == DIR_DOWN) {
  377. max = tns->cnt - tns->cols * tns->rows;
  378. if (tns->cnt % tns->cols != 0)
  379. max += tns->cols - tns->cnt % tns->cols;
  380. tns->first = MIN(tns->first + d, max);
  381. } else if (dir == DIR_UP) {
  382. tns->first = MAX(tns->first - d, 0);
  383. }
  384. if (tns->first != old) {
  385. tns_check_view(tns, true);
  386. tns->dirty = true;
  387. }
  388. return tns->first != old;
  389. }
  390. int tns_translate(tns_t *tns, int x, int y)
  391. {
  392. int n;
  393. if (tns == NULL || tns->thumbs == NULL)
  394. return -1;
  395. if (x < tns->x || y < tns->y)
  396. return -1;
  397. n = tns->first + (y - tns->y) / thumb_dim * tns->cols +
  398. (x - tns->x) / thumb_dim;
  399. if (n >= tns->cnt)
  400. n = -1;
  401. return n;
  402. }