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.
 
 
 
 
 
 

724 lines
15 KiB

  1. /* Copyright 2011-2013 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  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 <fcntl.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <signal.h>
  27. #include <sys/select.h>
  28. #include <sys/stat.h>
  29. #include <sys/time.h>
  30. #include <sys/wait.h>
  31. #include <X11/keysym.h>
  32. #include "types.h"
  33. #include "commands.h"
  34. #include "image.h"
  35. #include "options.h"
  36. #include "thumbs.h"
  37. #include "util.h"
  38. #include "window.h"
  39. #include "config.h"
  40. enum {
  41. FILENAME_CNT = 1024,
  42. TITLE_LEN = 256
  43. };
  44. typedef struct {
  45. struct timeval when;
  46. bool active;
  47. timeout_f handler;
  48. } timeout_t;
  49. /* timeout handler functions: */
  50. void redraw(void);
  51. void reset_cursor(void);
  52. void animate(void);
  53. void clear_resize(void);
  54. appmode_t mode;
  55. img_t img;
  56. tns_t tns;
  57. win_t win;
  58. fileinfo_t *files;
  59. int filecnt, fileidx;
  60. int markcnt;
  61. int alternate;
  62. int prefix;
  63. bool resized = false;
  64. const char * const INFO_SCRIPT = ".sxiv/exec/image-info";
  65. struct {
  66. char *script;
  67. int fd;
  68. unsigned int i, lastsep;
  69. bool open;
  70. } info;
  71. timeout_t timeouts[] = {
  72. { { 0, 0 }, false, redraw },
  73. { { 0, 0 }, false, reset_cursor },
  74. { { 0, 0 }, false, animate },
  75. { { 0, 0 }, false, clear_resize },
  76. };
  77. void cleanup(void)
  78. {
  79. static bool in = false;
  80. if (!in) {
  81. in = true;
  82. img_close(&img, false);
  83. tns_free(&tns);
  84. win_close(&win);
  85. }
  86. }
  87. void check_add_file(char *filename)
  88. {
  89. const char *bn;
  90. if (filename == NULL || *filename == '\0')
  91. return;
  92. if (access(filename, R_OK) < 0) {
  93. warn("could not open file: %s", filename);
  94. return;
  95. }
  96. if (fileidx == filecnt) {
  97. filecnt *= 2;
  98. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  99. }
  100. if (*filename != '/') {
  101. files[fileidx].path = absolute_path(filename);
  102. if (files[fileidx].path == NULL) {
  103. warn("could not get absolute path of file: %s\n", filename);
  104. return;
  105. }
  106. }
  107. files[fileidx].loaded = false;
  108. files[fileidx].name = s_strdup(filename);
  109. if (*filename == '/')
  110. files[fileidx].path = files[fileidx].name;
  111. if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
  112. files[fileidx].base = ++bn;
  113. else
  114. files[fileidx].base = files[fileidx].name;
  115. fileidx++;
  116. }
  117. void remove_file(int n, bool manual)
  118. {
  119. if (n < 0 || n >= filecnt)
  120. return;
  121. if (filecnt == 1) {
  122. if (!manual)
  123. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  124. cleanup();
  125. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  126. }
  127. if (files[n].path != files[n].name)
  128. free((void*) files[n].path);
  129. free((void*) files[n].name);
  130. if (n + 1 < filecnt)
  131. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  132. if (n + 1 < tns.cnt) {
  133. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  134. sizeof(thumb_t));
  135. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  136. }
  137. filecnt--;
  138. if (n < tns.cnt)
  139. tns.cnt--;
  140. }
  141. void set_timeout(timeout_f handler, int time, bool overwrite)
  142. {
  143. int i;
  144. for (i = 0; i < ARRLEN(timeouts); i++) {
  145. if (timeouts[i].handler == handler) {
  146. if (!timeouts[i].active || overwrite) {
  147. gettimeofday(&timeouts[i].when, 0);
  148. TV_ADD_MSEC(&timeouts[i].when, time);
  149. timeouts[i].active = true;
  150. }
  151. return;
  152. }
  153. }
  154. }
  155. void reset_timeout(timeout_f handler)
  156. {
  157. int i;
  158. for (i = 0; i < ARRLEN(timeouts); i++) {
  159. if (timeouts[i].handler == handler) {
  160. timeouts[i].active = false;
  161. return;
  162. }
  163. }
  164. }
  165. bool check_timeouts(struct timeval *t)
  166. {
  167. int i = 0, tdiff, tmin = -1;
  168. struct timeval now;
  169. while (i < ARRLEN(timeouts)) {
  170. if (timeouts[i].active) {
  171. gettimeofday(&now, 0);
  172. tdiff = TV_DIFF(&timeouts[i].when, &now);
  173. if (tdiff <= 0) {
  174. timeouts[i].active = false;
  175. if (timeouts[i].handler != NULL)
  176. timeouts[i].handler();
  177. i = tmin = -1;
  178. } else if (tmin < 0 || tdiff < tmin) {
  179. tmin = tdiff;
  180. }
  181. }
  182. i++;
  183. }
  184. if (tmin > 0 && t != NULL)
  185. TV_SET_MSEC(t, tmin);
  186. return tmin > 0;
  187. }
  188. void open_info(void)
  189. {
  190. static pid_t pid;
  191. int pfd[2];
  192. if (info.script == NULL || info.open || win.bar.h == 0)
  193. return;
  194. if (info.fd != -1) {
  195. close(info.fd);
  196. kill(pid, SIGTERM);
  197. info.fd = -1;
  198. }
  199. win.bar.l[0] = '\0';
  200. if (pipe(pfd) < 0)
  201. return;
  202. pid = fork();
  203. if (pid > 0) {
  204. close(pfd[1]);
  205. fcntl(pfd[0], F_SETFL, O_NONBLOCK);
  206. info.fd = pfd[0];
  207. info.i = info.lastsep = 0;
  208. info.open = true;
  209. } else if (pid == 0) {
  210. close(pfd[0]);
  211. dup2(pfd[1], 1);
  212. execl(info.script, info.script, files[fileidx].name, NULL);
  213. warn("could not exec: %s", info.script);
  214. exit(EXIT_FAILURE);
  215. }
  216. }
  217. void read_info(void)
  218. {
  219. ssize_t i, n;
  220. char buf[BAR_L_LEN];
  221. while (true) {
  222. n = read(info.fd, buf, sizeof(buf));
  223. if (n < 0 && errno == EAGAIN)
  224. return;
  225. else if (n == 0)
  226. goto end;
  227. for (i = 0; i < n; i++) {
  228. if (buf[i] == '\n') {
  229. if (info.lastsep == 0) {
  230. win.bar.l[info.i++] = ' ';
  231. info.lastsep = 1;
  232. }
  233. } else {
  234. win.bar.l[info.i++] = buf[i];
  235. info.lastsep = 0;
  236. }
  237. if (info.i + 1 == sizeof(win.bar.l))
  238. goto end;
  239. }
  240. }
  241. end:
  242. info.i -= info.lastsep;
  243. win.bar.l[info.i] = '\0';
  244. win_update_bar(&win);
  245. info.fd = -1;
  246. while (waitpid(-1, NULL, WNOHANG) > 0);
  247. }
  248. void load_image(int new)
  249. {
  250. if (new < 0 || new >= filecnt)
  251. return;
  252. win_set_cursor(&win, CURSOR_WATCH);
  253. img_close(&img, false);
  254. while (!img_load(&img, &files[new])) {
  255. remove_file(new, false);
  256. if (new >= filecnt)
  257. new = filecnt - 1;
  258. }
  259. files[new].loaded = true;
  260. alternate = fileidx;
  261. fileidx = new;
  262. info.open = false;
  263. open_info();
  264. if (img.multi.cnt > 0 && img.multi.animate)
  265. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  266. else
  267. reset_timeout(animate);
  268. }
  269. void update_info(void)
  270. {
  271. int sel;
  272. unsigned int i, fn, fw, n;
  273. unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
  274. char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
  275. const char * mark;
  276. bool ow_info;
  277. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  278. sel = mode == MODE_IMAGE ? fileidx : tns.sel;
  279. /* update window title */
  280. if (mode == MODE_THUMB) {
  281. win_set_title(&win, "sxiv");
  282. } else {
  283. snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
  284. win_set_title(&win, title);
  285. }
  286. /* update bar contents */
  287. if (win.bar.h == 0)
  288. return;
  289. mark = files[sel].marked ? "* " : "";
  290. if (mode == MODE_THUMB) {
  291. if (tns.cnt == filecnt) {
  292. n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, sel + 1, filecnt);
  293. ow_info = true;
  294. } else {
  295. snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.cnt, filecnt);
  296. rt[0] = '\0';
  297. ow_info = false;
  298. }
  299. } else {
  300. n = snprintf(rt, rlen, "%s%3d%% | ", mark, (int) (img.zoom * 100.0));
  301. if (img.multi.cnt > 0) {
  302. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  303. n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
  304. fn, img.multi.sel + 1, img.multi.cnt);
  305. }
  306. n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
  307. ow_info = info.script == NULL;
  308. }
  309. if (ow_info) {
  310. fn = strlen(files[sel].name);
  311. if (fn < llen &&
  312. win_textwidth(files[sel].name, fn, true) +
  313. win_textwidth(rt, n, true) < win.w)
  314. {
  315. strncpy(lt, files[sel].name, llen);
  316. } else {
  317. strncpy(lt, files[sel].base, llen);
  318. }
  319. }
  320. }
  321. void redraw(void)
  322. {
  323. if (mode == MODE_IMAGE)
  324. img_render(&img);
  325. else
  326. tns_render(&tns);
  327. update_info();
  328. win_draw(&win);
  329. reset_timeout(redraw);
  330. reset_cursor();
  331. }
  332. void reset_cursor(void)
  333. {
  334. int i;
  335. cursor_t cursor = CURSOR_NONE;
  336. if (mode == MODE_IMAGE) {
  337. for (i = 0; i < ARRLEN(timeouts); i++) {
  338. if (timeouts[i].handler == reset_cursor) {
  339. if (timeouts[i].active)
  340. cursor = CURSOR_ARROW;
  341. break;
  342. }
  343. }
  344. } else {
  345. if (tns.cnt != filecnt)
  346. cursor = CURSOR_WATCH;
  347. else
  348. cursor = CURSOR_ARROW;
  349. }
  350. win_set_cursor(&win, cursor);
  351. }
  352. void animate(void)
  353. {
  354. if (img_frame_animate(&img, false)) {
  355. redraw();
  356. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  357. }
  358. }
  359. void clear_resize(void)
  360. {
  361. resized = false;
  362. }
  363. bool keymask(const keymap_t *k, unsigned int state)
  364. {
  365. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  366. }
  367. bool buttonmask(const button_t *b, unsigned int state)
  368. {
  369. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  370. (state & (ControlMask | ShiftMask));
  371. }
  372. void on_keypress(XKeyEvent *kev)
  373. {
  374. int i;
  375. KeySym ksym;
  376. char key;
  377. if (kev == NULL)
  378. return;
  379. XLookupString(kev, &key, 1, &ksym, NULL);
  380. if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
  381. (kev->state & ControlMask) == 0)
  382. {
  383. /* number prefix for commands */
  384. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  385. return;
  386. }
  387. for (i = 0; i < ARRLEN(keys); i++) {
  388. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  389. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  390. redraw();
  391. prefix = 0;
  392. return;
  393. }
  394. }
  395. }
  396. void on_buttonpress(XButtonEvent *bev)
  397. {
  398. int i, sel;
  399. if (bev == NULL)
  400. return;
  401. if (mode == MODE_IMAGE) {
  402. win_set_cursor(&win, CURSOR_ARROW);
  403. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  404. for (i = 0; i < ARRLEN(buttons); i++) {
  405. if (buttons[i].button == bev->button &&
  406. buttonmask(&buttons[i], bev->state))
  407. {
  408. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  409. redraw();
  410. return;
  411. }
  412. }
  413. } else {
  414. /* thumbnail mode (hard-coded) */
  415. switch (bev->button) {
  416. case Button1:
  417. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  418. if (sel == tns.sel) {
  419. mode = MODE_IMAGE;
  420. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  421. load_image(tns.sel);
  422. } else {
  423. tns_highlight(&tns, tns.sel, false);
  424. tns_highlight(&tns, sel, true);
  425. tns.sel = sel;
  426. }
  427. redraw();
  428. break;
  429. }
  430. break;
  431. case Button4:
  432. case Button5:
  433. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  434. (bev->state & ControlMask) != 0))
  435. redraw();
  436. break;
  437. }
  438. }
  439. }
  440. void run(void)
  441. {
  442. int xfd;
  443. fd_set fds;
  444. struct timeval timeout;
  445. bool discard, to_set;
  446. XEvent ev, nextev;
  447. redraw();
  448. while (true) {
  449. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  450. XPending(win.env.dpy) == 0)
  451. {
  452. /* load thumbnails */
  453. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  454. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  455. tns.cnt++;
  456. } else {
  457. remove_file(tns.cnt, false);
  458. if (tns.sel >= tns.cnt)
  459. tns.sel--;
  460. }
  461. if (tns.cnt == filecnt)
  462. redraw();
  463. else
  464. check_timeouts(NULL);
  465. }
  466. while (XPending(win.env.dpy) == 0
  467. && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
  468. {
  469. /* check for timeouts & input */
  470. xfd = ConnectionNumber(win.env.dpy);
  471. FD_ZERO(&fds);
  472. FD_SET(xfd, &fds);
  473. if (info.fd != -1) {
  474. FD_SET(info.fd, &fds);
  475. xfd = MAX(xfd, info.fd);
  476. }
  477. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  478. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  479. read_info();
  480. }
  481. do {
  482. XNextEvent(win.env.dpy, &ev);
  483. discard = false;
  484. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  485. XPeekEvent(win.env.dpy, &nextev);
  486. switch (ev.type) {
  487. case ConfigureNotify:
  488. discard = ev.type == nextev.type;
  489. break;
  490. case KeyPress:
  491. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  492. && ev.xkey.keycode == nextev.xkey.keycode;
  493. break;
  494. }
  495. }
  496. } while (discard);
  497. switch (ev.type) {
  498. /* handle events */
  499. case ButtonPress:
  500. on_buttonpress(&ev.xbutton);
  501. break;
  502. case ClientMessage:
  503. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  504. return;
  505. break;
  506. case ConfigureNotify:
  507. if (win_configure(&win, &ev.xconfigure)) {
  508. if (mode == MODE_IMAGE) {
  509. img.dirty = true;
  510. img.checkpan = true;
  511. } else {
  512. tns.dirty = true;
  513. }
  514. if (!resized || win.fullscreen) {
  515. redraw();
  516. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  517. resized = true;
  518. } else {
  519. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  520. }
  521. }
  522. break;
  523. case Expose:
  524. win_expose(&win, &ev.xexpose);
  525. break;
  526. case KeyPress:
  527. on_keypress(&ev.xkey);
  528. break;
  529. case MotionNotify:
  530. if (mode == MODE_IMAGE) {
  531. win_set_cursor(&win, CURSOR_ARROW);
  532. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  533. }
  534. break;
  535. }
  536. }
  537. }
  538. int fncmp(const void *a, const void *b)
  539. {
  540. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  541. }
  542. int main(int argc, char **argv)
  543. {
  544. int i, start;
  545. size_t n;
  546. ssize_t len;
  547. char *filename;
  548. const char *homedir;
  549. struct stat fstats;
  550. r_dir_t dir;
  551. parse_options(argc, argv);
  552. if (options->clean_cache) {
  553. tns_init(&tns, 0, NULL);
  554. tns_clean_cache(&tns);
  555. exit(EXIT_SUCCESS);
  556. }
  557. if (options->filecnt == 0 && !options->from_stdin) {
  558. print_usage();
  559. exit(EXIT_FAILURE);
  560. }
  561. if (options->recursive || options->from_stdin)
  562. filecnt = FILENAME_CNT;
  563. else
  564. filecnt = options->filecnt;
  565. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  566. fileidx = 0;
  567. if (options->from_stdin) {
  568. filename = NULL;
  569. while ((len = get_line(&filename, &n, stdin)) > 0) {
  570. if (filename[len-1] == '\n')
  571. filename[len-1] = '\0';
  572. check_add_file(filename);
  573. }
  574. if (filename != NULL)
  575. free(filename);
  576. }
  577. for (i = 0; i < options->filecnt; i++) {
  578. filename = options->filenames[i];
  579. if (stat(filename, &fstats) < 0) {
  580. warn("could not stat file: %s", filename);
  581. continue;
  582. }
  583. if (!S_ISDIR(fstats.st_mode)) {
  584. check_add_file(filename);
  585. } else {
  586. if (!options->recursive) {
  587. warn("ignoring directory: %s", filename);
  588. continue;
  589. }
  590. if (r_opendir(&dir, filename) < 0) {
  591. warn("could not open directory: %s", filename);
  592. continue;
  593. }
  594. start = fileidx;
  595. while ((filename = r_readdir(&dir)) != NULL) {
  596. check_add_file(filename);
  597. free((void*) filename);
  598. }
  599. r_closedir(&dir);
  600. if (fileidx - start > 1)
  601. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  602. }
  603. }
  604. if (fileidx == 0) {
  605. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  606. exit(EXIT_FAILURE);
  607. }
  608. filecnt = fileidx;
  609. fileidx = options->startnum < filecnt ? options->startnum : 0;
  610. win_init(&win);
  611. img_init(&img, &win);
  612. if ((homedir = getenv("HOME")) == NULL) {
  613. warn("could not locate home directory");
  614. } else {
  615. len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
  616. info.script = (char*) s_malloc(len);
  617. snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT);
  618. if (access(info.script, X_OK) != 0) {
  619. free(info.script);
  620. info.script = NULL;
  621. }
  622. }
  623. info.fd = -1;
  624. if (options->thumb_mode) {
  625. mode = MODE_THUMB;
  626. tns_init(&tns, filecnt, &win);
  627. while (!tns_load(&tns, 0, &files[0], false, false))
  628. remove_file(0, false);
  629. tns.cnt = 1;
  630. } else {
  631. mode = MODE_IMAGE;
  632. tns.thumbs = NULL;
  633. load_image(fileidx);
  634. }
  635. win_open(&win);
  636. run();
  637. cleanup();
  638. return 0;
  639. }