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.
 
 
 
 
 
 

556 líneas
12 KiB

  1. /* sxiv: main.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 _MAPPINGS_CONFIG
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <libgen.h>
  24. #include <unistd.h>
  25. #include <sys/stat.h>
  26. #include <sys/time.h>
  27. #include <X11/Xutil.h>
  28. #include <X11/keysym.h>
  29. #include "commands.h"
  30. #include "image.h"
  31. #include "options.h"
  32. #include "thumbs.h"
  33. #include "types.h"
  34. #include "util.h"
  35. #include "window.h"
  36. #include "config.h"
  37. enum {
  38. INFO_STR_LEN = 256,
  39. FILENAME_CNT = 1024
  40. };
  41. typedef struct {
  42. struct timeval when;
  43. bool active;
  44. timeout_f handler;
  45. } timeout_t;
  46. /* timeout handler functions: */
  47. void redraw(void);
  48. void reset_cursor(void);
  49. void animate(void);
  50. appmode_t mode;
  51. img_t img;
  52. tns_t tns;
  53. win_t win;
  54. fileinfo_t *files;
  55. int filecnt, fileidx;
  56. size_t filesize;
  57. int prefix;
  58. char win_bar_l[INFO_STR_LEN];
  59. char win_bar_r[INFO_STR_LEN];
  60. char win_title[INFO_STR_LEN];
  61. timeout_t timeouts[] = {
  62. { { 0, 0 }, false, redraw },
  63. { { 0, 0 }, false, reset_cursor },
  64. { { 0, 0 }, false, animate },
  65. };
  66. void cleanup(void) {
  67. static bool in = false;
  68. if (!in) {
  69. in = true;
  70. img_close(&img, false);
  71. tns_free(&tns);
  72. win_close(&win);
  73. }
  74. }
  75. void check_add_file(char *filename) {
  76. if (filename == NULL || *filename == '\0')
  77. return;
  78. if (access(filename, R_OK) < 0) {
  79. warn("could not open file: %s", filename);
  80. return;
  81. }
  82. if (fileidx == filecnt) {
  83. filecnt *= 2;
  84. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  85. }
  86. if (*filename != '/') {
  87. files[fileidx].path = absolute_path(filename);
  88. if (files[fileidx].path == NULL) {
  89. warn("could not get absolute path of file: %s\n", filename);
  90. return;
  91. }
  92. }
  93. files[fileidx].loaded = false;
  94. files[fileidx].name = s_strdup(filename);
  95. files[fileidx].base = s_strdup(basename(filename));
  96. if (*filename == '/')
  97. files[fileidx].path = files[fileidx].name;
  98. fileidx++;
  99. }
  100. void remove_file(int n, bool manual) {
  101. if (n < 0 || n >= filecnt)
  102. return;
  103. if (filecnt == 1) {
  104. if (!manual)
  105. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  106. cleanup();
  107. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  108. }
  109. if (files[n].path != files[n].name)
  110. free((void*) files[n].path);
  111. free((void*) files[n].name);
  112. if (n + 1 < filecnt)
  113. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  114. if (n + 1 < tns.cnt) {
  115. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  116. sizeof(thumb_t));
  117. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  118. }
  119. filecnt--;
  120. if (n < tns.cnt)
  121. tns.cnt--;
  122. }
  123. void set_timeout(timeout_f handler, int time, bool overwrite) {
  124. int i;
  125. for (i = 0; i < ARRLEN(timeouts); i++) {
  126. if (timeouts[i].handler == handler) {
  127. if (!timeouts[i].active || overwrite) {
  128. gettimeofday(&timeouts[i].when, 0);
  129. TV_ADD_MSEC(&timeouts[i].when, time);
  130. timeouts[i].active = true;
  131. }
  132. return;
  133. }
  134. }
  135. }
  136. void reset_timeout(timeout_f handler) {
  137. int i;
  138. for (i = 0; i < ARRLEN(timeouts); i++) {
  139. if (timeouts[i].handler == handler) {
  140. timeouts[i].active = false;
  141. return;
  142. }
  143. }
  144. }
  145. bool check_timeouts(struct timeval *t) {
  146. int i = 0, tdiff, tmin = -1;
  147. struct timeval now;
  148. gettimeofday(&now, 0);
  149. while (i < ARRLEN(timeouts)) {
  150. if (timeouts[i].active) {
  151. tdiff = TV_DIFF(&timeouts[i].when, &now);
  152. if (tdiff <= 0) {
  153. timeouts[i].active = false;
  154. if (timeouts[i].handler != NULL)
  155. timeouts[i].handler();
  156. i = tmin = -1;
  157. } else if (tmin < 0 || tdiff < tmin) {
  158. tmin = tdiff;
  159. }
  160. }
  161. i++;
  162. }
  163. if (tmin > 0 && t != NULL)
  164. TV_SET_MSEC(t, tmin);
  165. return tmin > 0;
  166. }
  167. void load_image(int new) {
  168. struct stat fstats;
  169. if (new < 0 || new >= filecnt)
  170. return;
  171. win_set_cursor(&win, CURSOR_WATCH);
  172. img_close(&img, false);
  173. while (!img_load(&img, &files[new])) {
  174. remove_file(new, false);
  175. if (new >= filecnt)
  176. new = filecnt - 1;
  177. }
  178. files[new].loaded = true;
  179. fileidx = new;
  180. if (stat(files[new].path, &fstats) == 0)
  181. filesize = fstats.st_size;
  182. else
  183. filesize = 0;
  184. if (img.multi.cnt > 0 && img.multi.animate)
  185. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  186. else
  187. reset_timeout(animate);
  188. }
  189. void update_info(void) {
  190. unsigned int i, fw, pw;
  191. char frame_info[16];
  192. const char *size_unit;
  193. float size = filesize;
  194. pw = 0;
  195. for (i = filecnt; i > 0; i /= 10)
  196. pw++;
  197. if (mode == MODE_THUMB) {
  198. snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d %s",
  199. pw, tns.cnt > 0 ? tns.sel + 1 : 0, filecnt,
  200. tns.cnt > 0 ? files[tns.sel].base : "");
  201. win_bar_r[0] = '\0';
  202. snprintf(win_title, sizeof win_title, "sxiv");
  203. } else {
  204. size_readable(&size, &size_unit);
  205. if (img.multi.cnt > 0) {
  206. fw = 0;
  207. for (i = img.multi.cnt; i > 0; i /= 10)
  208. fw++;
  209. snprintf(frame_info, sizeof frame_info, " %0*d/%d",
  210. fw, img.multi.sel + 1, img.multi.cnt);
  211. } else {
  212. frame_info[0] = '\0';
  213. }
  214. snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d %s",
  215. pw, fileidx + 1, filecnt, files[fileidx].base);
  216. snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s %dx%d %3d%%%s",
  217. size, size_unit, img.w, img.h, (int) (img.zoom * 100.0),
  218. frame_info);
  219. snprintf(win_title, sizeof win_title, "sxiv - %s",
  220. files[fileidx].name);
  221. }
  222. win_set_title(&win, win_title);
  223. win_set_bar_info(&win, win_bar_l, win_bar_r);
  224. }
  225. void redraw(void) {
  226. if (mode == MODE_IMAGE)
  227. img_render(&img);
  228. else
  229. tns_render(&tns);
  230. update_info();
  231. win_draw(&win);
  232. reset_timeout(redraw);
  233. reset_cursor();
  234. }
  235. void reset_cursor(void) {
  236. int i;
  237. cursor_t cursor = CURSOR_NONE;
  238. if (mode == MODE_IMAGE) {
  239. for (i = 0; i < ARRLEN(timeouts); i++) {
  240. if (timeouts[i].handler == reset_cursor) {
  241. if (timeouts[i].active)
  242. cursor = CURSOR_ARROW;
  243. break;
  244. }
  245. }
  246. } else {
  247. if (tns.cnt != filecnt)
  248. cursor = CURSOR_WATCH;
  249. else
  250. cursor = CURSOR_ARROW;
  251. }
  252. win_set_cursor(&win, cursor);
  253. }
  254. void animate(void) {
  255. if (img_frame_animate(&img, false)) {
  256. redraw();
  257. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  258. }
  259. }
  260. bool keymask(const keymap_t *k, unsigned int state) {
  261. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  262. }
  263. bool buttonmask(const button_t *b, unsigned int state) {
  264. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  265. (state & (ControlMask | ShiftMask));
  266. }
  267. void on_keypress(XKeyEvent *kev) {
  268. int i;
  269. KeySym ksym;
  270. char key;
  271. if (kev == NULL)
  272. return;
  273. XLookupString(kev, &key, 1, &ksym, NULL);
  274. if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
  275. (kev->state & ControlMask) == 0)
  276. {
  277. /* number prefix for commands */
  278. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  279. return;
  280. }
  281. for (i = 0; i < ARRLEN(keys); i++) {
  282. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  283. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  284. redraw();
  285. prefix = 0;
  286. return;
  287. }
  288. }
  289. }
  290. void on_buttonpress(XButtonEvent *bev) {
  291. int i, sel;
  292. if (bev == NULL)
  293. return;
  294. if (mode == MODE_IMAGE) {
  295. win_set_cursor(&win, CURSOR_ARROW);
  296. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  297. for (i = 0; i < ARRLEN(buttons); i++) {
  298. if (buttons[i].button == bev->button &&
  299. buttonmask(&buttons[i], bev->state))
  300. {
  301. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  302. redraw();
  303. return;
  304. }
  305. }
  306. } else {
  307. /* thumbnail mode (hard-coded) */
  308. switch (bev->button) {
  309. case Button1:
  310. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  311. if (sel == tns.sel) {
  312. mode = MODE_IMAGE;
  313. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  314. load_image(tns.sel);
  315. } else {
  316. tns_highlight(&tns, tns.sel, false);
  317. tns_highlight(&tns, sel, true);
  318. tns.sel = sel;
  319. }
  320. redraw();
  321. break;
  322. }
  323. break;
  324. case Button4:
  325. case Button5:
  326. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  327. (bev->state & ControlMask) != 0))
  328. redraw();
  329. break;
  330. }
  331. }
  332. }
  333. void run(void) {
  334. int xfd;
  335. fd_set fds;
  336. struct timeval timeout;
  337. XEvent ev;
  338. redraw();
  339. while (true) {
  340. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  341. XPending(win.env.dpy) == 0)
  342. {
  343. /* load thumbnails */
  344. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  345. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  346. tns.cnt++;
  347. } else {
  348. remove_file(tns.cnt, false);
  349. if (tns.sel >= tns.cnt)
  350. tns.sel--;
  351. }
  352. if (tns.cnt == filecnt)
  353. redraw();
  354. else
  355. check_timeouts(NULL);
  356. }
  357. while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
  358. /* wait for timeouts */
  359. xfd = ConnectionNumber(win.env.dpy);
  360. FD_ZERO(&fds);
  361. FD_SET(xfd, &fds);
  362. select(xfd + 1, &fds, 0, 0, &timeout);
  363. }
  364. XNextEvent(win.env.dpy, &ev);
  365. switch (ev.type) {
  366. /* handle events */
  367. case ButtonPress:
  368. on_buttonpress(&ev.xbutton);
  369. break;
  370. case ClientMessage:
  371. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  372. return;
  373. break;
  374. case ConfigureNotify:
  375. if (win_configure(&win, &ev.xconfigure)) {
  376. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  377. if (mode == MODE_IMAGE)
  378. img.checkpan = true;
  379. else
  380. tns.dirty = true;
  381. }
  382. break;
  383. case KeyPress:
  384. on_keypress(&ev.xkey);
  385. break;
  386. case MotionNotify:
  387. if (mode == MODE_IMAGE) {
  388. win_set_cursor(&win, CURSOR_ARROW);
  389. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  390. }
  391. break;
  392. }
  393. }
  394. }
  395. int fncmp(const void *a, const void *b) {
  396. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  397. }
  398. int main(int argc, char **argv) {
  399. int i, start;
  400. size_t n;
  401. ssize_t len;
  402. char *filename;
  403. struct stat fstats;
  404. r_dir_t dir;
  405. parse_options(argc, argv);
  406. if (options->clean_cache) {
  407. tns_init(&tns, 0, NULL);
  408. tns_clean_cache(&tns);
  409. exit(EXIT_SUCCESS);
  410. }
  411. if (options->filecnt == 0) {
  412. print_usage();
  413. exit(EXIT_FAILURE);
  414. }
  415. if (options->recursive || options->from_stdin)
  416. filecnt = FILENAME_CNT;
  417. else
  418. filecnt = options->filecnt;
  419. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  420. fileidx = 0;
  421. /* build file list: */
  422. if (options->from_stdin) {
  423. filename = NULL;
  424. while ((len = get_line(&filename, &n, stdin)) > 0) {
  425. if (filename[len-1] == '\n')
  426. filename[len-1] = '\0';
  427. check_add_file(filename);
  428. }
  429. if (filename != NULL)
  430. free(filename);
  431. } else {
  432. for (i = 0; i < options->filecnt; i++) {
  433. filename = options->filenames[i];
  434. if (stat(filename, &fstats) < 0) {
  435. warn("could not stat file: %s", filename);
  436. continue;
  437. }
  438. if (!S_ISDIR(fstats.st_mode)) {
  439. check_add_file(filename);
  440. } else {
  441. if (!options->recursive) {
  442. warn("ignoring directory: %s", filename);
  443. continue;
  444. }
  445. if (r_opendir(&dir, filename) < 0) {
  446. warn("could not open directory: %s", filename);
  447. continue;
  448. }
  449. start = fileidx;
  450. while ((filename = r_readdir(&dir)) != NULL) {
  451. check_add_file(filename);
  452. free((void*) filename);
  453. }
  454. r_closedir(&dir);
  455. if (fileidx - start > 1)
  456. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  457. }
  458. }
  459. }
  460. if (fileidx == 0) {
  461. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  462. exit(EXIT_FAILURE);
  463. }
  464. filecnt = fileidx;
  465. fileidx = options->startnum < filecnt ? options->startnum : 0;
  466. win_init(&win);
  467. img_init(&img, &win);
  468. if (options->thumb_mode) {
  469. mode = MODE_THUMB;
  470. tns_init(&tns, filecnt, &win);
  471. while (!tns_load(&tns, 0, &files[0], false, false))
  472. remove_file(0, false);
  473. tns.cnt = 1;
  474. } else {
  475. mode = MODE_IMAGE;
  476. tns.thumbs = NULL;
  477. load_image(fileidx);
  478. }
  479. win_open(&win);
  480. run();
  481. cleanup();
  482. return 0;
  483. }