A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

468 linhas
10 KiB

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