A Simple X Image Viewer
 
 
 
 
 
 

559 lines
12 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 <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. if (tns.cnt != filecnt) {
  199. snprintf(win_bar_l, sizeof win_bar_l, "Loading... %0*d/%d",
  200. pw, tns.cnt, filecnt);
  201. } else {
  202. snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
  203. pw, tns.sel + 1, filecnt, BAR_SEPARATOR, files[tns.sel].base);
  204. }
  205. win_set_title(&win, "sxiv");
  206. win_set_bar_info(&win, win_bar_l, NULL);
  207. } else {
  208. size_readable(&size, &size_unit);
  209. if (img.multi.cnt > 0) {
  210. fw = 0;
  211. for (i = img.multi.cnt; i > 0; i /= 10)
  212. fw++;
  213. snprintf(frame_info, sizeof frame_info, "%s%0*d/%d",
  214. BAR_SEPARATOR, fw, img.multi.sel + 1, img.multi.cnt);
  215. } else {
  216. frame_info[0] = '\0';
  217. }
  218. snprintf(win_bar_l, sizeof win_bar_l, "%0*d/%d%s%s",
  219. pw, fileidx + 1, filecnt, BAR_SEPARATOR, files[fileidx].base);
  220. snprintf(win_bar_r, sizeof win_bar_r, "%.2f%s%s%dx%d%s%3d%%%s",
  221. size, size_unit, BAR_SEPARATOR, img.w, img.h, BAR_SEPARATOR,
  222. (int) (img.zoom * 100.0), frame_info);
  223. snprintf(win_title, sizeof win_title, "sxiv - %s", files[fileidx].name);
  224. win_set_title(&win, win_title);
  225. win_set_bar_info(&win, win_bar_l, win_bar_r);
  226. }
  227. }
  228. void redraw(void) {
  229. if (mode == MODE_IMAGE)
  230. img_render(&img);
  231. else
  232. tns_render(&tns);
  233. update_info();
  234. win_draw(&win);
  235. reset_timeout(redraw);
  236. reset_cursor();
  237. }
  238. void reset_cursor(void) {
  239. int i;
  240. cursor_t cursor = CURSOR_NONE;
  241. if (mode == MODE_IMAGE) {
  242. for (i = 0; i < ARRLEN(timeouts); i++) {
  243. if (timeouts[i].handler == reset_cursor) {
  244. if (timeouts[i].active)
  245. cursor = CURSOR_ARROW;
  246. break;
  247. }
  248. }
  249. } else {
  250. if (tns.cnt != filecnt)
  251. cursor = CURSOR_WATCH;
  252. else
  253. cursor = CURSOR_ARROW;
  254. }
  255. win_set_cursor(&win, cursor);
  256. }
  257. void animate(void) {
  258. if (img_frame_animate(&img, false)) {
  259. redraw();
  260. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  261. }
  262. }
  263. bool keymask(const keymap_t *k, unsigned int state) {
  264. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  265. }
  266. bool buttonmask(const button_t *b, unsigned int state) {
  267. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  268. (state & (ControlMask | ShiftMask));
  269. }
  270. void on_keypress(XKeyEvent *kev) {
  271. int i;
  272. KeySym ksym;
  273. char key;
  274. if (kev == NULL)
  275. return;
  276. XLookupString(kev, &key, 1, &ksym, NULL);
  277. if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
  278. (kev->state & ControlMask) == 0)
  279. {
  280. /* number prefix for commands */
  281. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  282. return;
  283. }
  284. for (i = 0; i < ARRLEN(keys); i++) {
  285. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  286. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  287. redraw();
  288. prefix = 0;
  289. return;
  290. }
  291. }
  292. }
  293. void on_buttonpress(XButtonEvent *bev) {
  294. int i, sel;
  295. if (bev == NULL)
  296. return;
  297. if (mode == MODE_IMAGE) {
  298. win_set_cursor(&win, CURSOR_ARROW);
  299. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  300. for (i = 0; i < ARRLEN(buttons); i++) {
  301. if (buttons[i].button == bev->button &&
  302. buttonmask(&buttons[i], bev->state))
  303. {
  304. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  305. redraw();
  306. return;
  307. }
  308. }
  309. } else {
  310. /* thumbnail mode (hard-coded) */
  311. switch (bev->button) {
  312. case Button1:
  313. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  314. if (sel == tns.sel) {
  315. mode = MODE_IMAGE;
  316. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  317. load_image(tns.sel);
  318. } else {
  319. tns_highlight(&tns, tns.sel, false);
  320. tns_highlight(&tns, sel, true);
  321. tns.sel = sel;
  322. }
  323. redraw();
  324. break;
  325. }
  326. break;
  327. case Button4:
  328. case Button5:
  329. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  330. (bev->state & ControlMask) != 0))
  331. redraw();
  332. break;
  333. }
  334. }
  335. }
  336. void run(void) {
  337. int xfd;
  338. fd_set fds;
  339. struct timeval timeout;
  340. XEvent ev;
  341. redraw();
  342. while (true) {
  343. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  344. XPending(win.env.dpy) == 0)
  345. {
  346. /* load thumbnails */
  347. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  348. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  349. tns.cnt++;
  350. } else {
  351. remove_file(tns.cnt, false);
  352. if (tns.sel >= tns.cnt)
  353. tns.sel--;
  354. }
  355. if (tns.cnt == filecnt)
  356. redraw();
  357. else
  358. check_timeouts(NULL);
  359. }
  360. while (XPending(win.env.dpy) == 0 && check_timeouts(&timeout)) {
  361. /* wait for timeouts */
  362. xfd = ConnectionNumber(win.env.dpy);
  363. FD_ZERO(&fds);
  364. FD_SET(xfd, &fds);
  365. select(xfd + 1, &fds, 0, 0, &timeout);
  366. }
  367. XNextEvent(win.env.dpy, &ev);
  368. switch (ev.type) {
  369. /* handle events */
  370. case ButtonPress:
  371. on_buttonpress(&ev.xbutton);
  372. break;
  373. case ClientMessage:
  374. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  375. return;
  376. break;
  377. case ConfigureNotify:
  378. if (win_configure(&win, &ev.xconfigure)) {
  379. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  380. if (mode == MODE_IMAGE)
  381. img.checkpan = true;
  382. else
  383. tns.dirty = true;
  384. }
  385. break;
  386. case KeyPress:
  387. on_keypress(&ev.xkey);
  388. break;
  389. case MotionNotify:
  390. if (mode == MODE_IMAGE) {
  391. win_set_cursor(&win, CURSOR_ARROW);
  392. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  393. }
  394. break;
  395. }
  396. }
  397. }
  398. int fncmp(const void *a, const void *b) {
  399. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  400. }
  401. int main(int argc, char **argv) {
  402. int i, start;
  403. size_t n;
  404. ssize_t len;
  405. char *filename;
  406. struct stat fstats;
  407. r_dir_t dir;
  408. parse_options(argc, argv);
  409. if (options->clean_cache) {
  410. tns_init(&tns, 0, NULL);
  411. tns_clean_cache(&tns);
  412. exit(EXIT_SUCCESS);
  413. }
  414. if (options->filecnt == 0) {
  415. print_usage();
  416. exit(EXIT_FAILURE);
  417. }
  418. if (options->recursive || options->from_stdin)
  419. filecnt = FILENAME_CNT;
  420. else
  421. filecnt = options->filecnt;
  422. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  423. fileidx = 0;
  424. /* build file list: */
  425. if (options->from_stdin) {
  426. filename = NULL;
  427. while ((len = get_line(&filename, &n, stdin)) > 0) {
  428. if (filename[len-1] == '\n')
  429. filename[len-1] = '\0';
  430. check_add_file(filename);
  431. }
  432. if (filename != NULL)
  433. free(filename);
  434. } else {
  435. for (i = 0; i < options->filecnt; i++) {
  436. filename = options->filenames[i];
  437. if (stat(filename, &fstats) < 0) {
  438. warn("could not stat file: %s", filename);
  439. continue;
  440. }
  441. if (!S_ISDIR(fstats.st_mode)) {
  442. check_add_file(filename);
  443. } else {
  444. if (!options->recursive) {
  445. warn("ignoring directory: %s", filename);
  446. continue;
  447. }
  448. if (r_opendir(&dir, filename) < 0) {
  449. warn("could not open directory: %s", filename);
  450. continue;
  451. }
  452. start = fileidx;
  453. while ((filename = r_readdir(&dir)) != NULL) {
  454. check_add_file(filename);
  455. free((void*) filename);
  456. }
  457. r_closedir(&dir);
  458. if (fileidx - start > 1)
  459. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  460. }
  461. }
  462. }
  463. if (fileidx == 0) {
  464. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  465. exit(EXIT_FAILURE);
  466. }
  467. filecnt = fileidx;
  468. fileidx = options->startnum < filecnt ? options->startnum : 0;
  469. win_init(&win);
  470. img_init(&img, &win);
  471. if (options->thumb_mode) {
  472. mode = MODE_THUMB;
  473. tns_init(&tns, filecnt, &win);
  474. while (!tns_load(&tns, 0, &files[0], false, false))
  475. remove_file(0, false);
  476. tns.cnt = 1;
  477. } else {
  478. mode = MODE_IMAGE;
  479. tns.thumbs = NULL;
  480. load_image(fileidx);
  481. }
  482. win_open(&win);
  483. run();
  484. cleanup();
  485. return 0;
  486. }