A Simple X Image Viewer
 
 
 
 
 
 

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