A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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