A Simple X Image Viewer
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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