A Simple X Image Viewer
 
 
 
 
 
 

579 lines
13 KiB

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