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.
 
 
 
 
 
 

737 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 < 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%3d%% | ", mark, (int) (img.zoom * 100.0));
  305. if (img.multi.cnt > 0) {
  306. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  307. n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
  308. fn, img.multi.sel + 1, img.multi.cnt);
  309. }
  310. if (img.gamma != 0)
  311. n += snprintf(rt + n, rlen - n, "g%d | ", img.gamma);
  312. n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
  313. ow_info = info.script == NULL;
  314. }
  315. if (ow_info) {
  316. fn = strlen(files[sel].name);
  317. if (fn < llen &&
  318. win_textwidth(files[sel].name, fn, true) +
  319. win_textwidth(rt, n, true) < win.w)
  320. {
  321. strncpy(lt, files[sel].name, llen);
  322. } else {
  323. strncpy(lt, files[sel].base, llen);
  324. }
  325. }
  326. }
  327. void redraw(void)
  328. {
  329. if (mode == MODE_IMAGE)
  330. img_render(&img);
  331. else
  332. tns_render(&tns);
  333. update_info();
  334. win_draw(&win);
  335. reset_timeout(redraw);
  336. reset_cursor();
  337. }
  338. void reset_cursor(void)
  339. {
  340. int i;
  341. cursor_t cursor = CURSOR_NONE;
  342. if (mode == MODE_IMAGE) {
  343. for (i = 0; i < ARRLEN(timeouts); i++) {
  344. if (timeouts[i].handler == reset_cursor) {
  345. if (timeouts[i].active)
  346. cursor = CURSOR_ARROW;
  347. break;
  348. }
  349. }
  350. } else {
  351. if (tns.cnt != filecnt)
  352. cursor = CURSOR_WATCH;
  353. else
  354. cursor = CURSOR_ARROW;
  355. }
  356. win_set_cursor(&win, cursor);
  357. }
  358. void animate(void)
  359. {
  360. if (img_frame_animate(&img, false)) {
  361. redraw();
  362. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  363. }
  364. }
  365. void clear_resize(void)
  366. {
  367. resized = false;
  368. }
  369. bool keymask(const keymap_t *k, unsigned int state)
  370. {
  371. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  372. }
  373. bool buttonmask(const button_t *b, unsigned int state)
  374. {
  375. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  376. (state & (ControlMask | ShiftMask));
  377. }
  378. void on_keypress(XKeyEvent *kev)
  379. {
  380. int i;
  381. KeySym ksym;
  382. char key;
  383. if (kev == NULL)
  384. return;
  385. XLookupString(kev, &key, 1, &ksym, NULL);
  386. if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
  387. (kev->state & ControlMask) == 0)
  388. {
  389. /* number prefix for commands */
  390. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  391. return;
  392. }
  393. for (i = 0; i < ARRLEN(keys); i++) {
  394. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  395. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  396. redraw();
  397. prefix = 0;
  398. return;
  399. }
  400. }
  401. }
  402. void on_buttonpress(XButtonEvent *bev)
  403. {
  404. int i, sel;
  405. if (bev == NULL)
  406. return;
  407. if (mode == MODE_IMAGE) {
  408. win_set_cursor(&win, CURSOR_ARROW);
  409. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  410. for (i = 0; i < ARRLEN(buttons); i++) {
  411. if (buttons[i].button == bev->button &&
  412. buttonmask(&buttons[i], bev->state))
  413. {
  414. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  415. redraw();
  416. return;
  417. }
  418. }
  419. } else {
  420. /* thumbnail mode (hard-coded) */
  421. switch (bev->button) {
  422. case Button1:
  423. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  424. if (sel == tns.sel) {
  425. mode = MODE_IMAGE;
  426. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  427. load_image(tns.sel);
  428. } else {
  429. tns_highlight(&tns, tns.sel, false);
  430. tns_highlight(&tns, sel, true);
  431. tns.sel = sel;
  432. }
  433. redraw();
  434. break;
  435. }
  436. break;
  437. case Button3:
  438. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  439. files[sel].marked = !files[sel].marked;
  440. tns_mark(&tns, sel, files[sel].marked);
  441. redraw();
  442. }
  443. break;
  444. case Button4:
  445. case Button5:
  446. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  447. (bev->state & ControlMask) != 0))
  448. redraw();
  449. break;
  450. }
  451. }
  452. }
  453. void run(void)
  454. {
  455. int xfd;
  456. fd_set fds;
  457. struct timeval timeout;
  458. bool discard, to_set;
  459. XEvent ev, nextev;
  460. redraw();
  461. while (true) {
  462. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  463. XPending(win.env.dpy) == 0)
  464. {
  465. /* load thumbnails */
  466. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  467. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  468. tns.cnt++;
  469. } else {
  470. remove_file(tns.cnt, false);
  471. if (tns.sel >= tns.cnt)
  472. tns.sel--;
  473. }
  474. if (tns.cnt == filecnt)
  475. redraw();
  476. else
  477. check_timeouts(NULL);
  478. }
  479. while (XPending(win.env.dpy) == 0
  480. && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
  481. {
  482. /* check for timeouts & input */
  483. xfd = ConnectionNumber(win.env.dpy);
  484. FD_ZERO(&fds);
  485. FD_SET(xfd, &fds);
  486. if (info.fd != -1) {
  487. FD_SET(info.fd, &fds);
  488. xfd = MAX(xfd, info.fd);
  489. }
  490. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  491. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  492. read_info();
  493. }
  494. do {
  495. XNextEvent(win.env.dpy, &ev);
  496. discard = false;
  497. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  498. XPeekEvent(win.env.dpy, &nextev);
  499. switch (ev.type) {
  500. case ConfigureNotify:
  501. discard = ev.type == nextev.type;
  502. break;
  503. case KeyPress:
  504. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  505. && ev.xkey.keycode == nextev.xkey.keycode;
  506. break;
  507. }
  508. }
  509. } while (discard);
  510. switch (ev.type) {
  511. /* handle events */
  512. case ButtonPress:
  513. on_buttonpress(&ev.xbutton);
  514. break;
  515. case ClientMessage:
  516. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  517. return;
  518. break;
  519. case ConfigureNotify:
  520. if (win_configure(&win, &ev.xconfigure)) {
  521. if (mode == MODE_IMAGE) {
  522. img.dirty = true;
  523. img.checkpan = true;
  524. } else {
  525. tns.dirty = true;
  526. }
  527. if (!resized || win.fullscreen) {
  528. redraw();
  529. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  530. resized = true;
  531. } else {
  532. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  533. }
  534. }
  535. break;
  536. case Expose:
  537. win_expose(&win, &ev.xexpose);
  538. break;
  539. case KeyPress:
  540. on_keypress(&ev.xkey);
  541. break;
  542. case MotionNotify:
  543. if (mode == MODE_IMAGE) {
  544. win_set_cursor(&win, CURSOR_ARROW);
  545. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  546. }
  547. break;
  548. }
  549. }
  550. }
  551. int fncmp(const void *a, const void *b)
  552. {
  553. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  554. }
  555. int main(int argc, char **argv)
  556. {
  557. int i, start;
  558. size_t n;
  559. ssize_t len;
  560. char *filename;
  561. const char *homedir;
  562. struct stat fstats;
  563. r_dir_t dir;
  564. parse_options(argc, argv);
  565. if (options->clean_cache) {
  566. tns_init(&tns, 0, NULL);
  567. tns_clean_cache(&tns);
  568. exit(EXIT_SUCCESS);
  569. }
  570. if (options->filecnt == 0 && !options->from_stdin) {
  571. print_usage();
  572. exit(EXIT_FAILURE);
  573. }
  574. if (options->recursive || options->from_stdin)
  575. filecnt = FILENAME_CNT;
  576. else
  577. filecnt = options->filecnt;
  578. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  579. fileidx = 0;
  580. if (options->from_stdin) {
  581. filename = NULL;
  582. while ((len = get_line(&filename, &n, stdin)) > 0) {
  583. if (filename[len-1] == '\n')
  584. filename[len-1] = '\0';
  585. check_add_file(filename);
  586. }
  587. if (filename != NULL)
  588. free(filename);
  589. }
  590. for (i = 0; i < options->filecnt; i++) {
  591. filename = options->filenames[i];
  592. if (stat(filename, &fstats) < 0) {
  593. warn("could not stat file: %s", filename);
  594. continue;
  595. }
  596. if (!S_ISDIR(fstats.st_mode)) {
  597. check_add_file(filename);
  598. } else {
  599. if (!options->recursive) {
  600. warn("ignoring directory: %s", filename);
  601. continue;
  602. }
  603. if (r_opendir(&dir, filename) < 0) {
  604. warn("could not open directory: %s", filename);
  605. continue;
  606. }
  607. start = fileidx;
  608. while ((filename = r_readdir(&dir)) != NULL) {
  609. check_add_file(filename);
  610. free((void*) filename);
  611. }
  612. r_closedir(&dir);
  613. if (fileidx - start > 1)
  614. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  615. }
  616. }
  617. if (fileidx == 0) {
  618. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  619. exit(EXIT_FAILURE);
  620. }
  621. filecnt = fileidx;
  622. fileidx = options->startnum < filecnt ? options->startnum : 0;
  623. win_init(&win);
  624. img_init(&img, &win);
  625. if ((homedir = getenv("HOME")) == NULL) {
  626. warn("could not locate home directory");
  627. } else {
  628. len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
  629. info.script = (char*) s_malloc(len);
  630. snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT);
  631. if (access(info.script, X_OK) != 0) {
  632. free(info.script);
  633. info.script = NULL;
  634. }
  635. }
  636. info.fd = -1;
  637. if (options->thumb_mode) {
  638. mode = MODE_THUMB;
  639. tns_init(&tns, filecnt, &win);
  640. while (!tns_load(&tns, 0, &files[0], false, false))
  641. remove_file(0, false);
  642. tns.cnt = 1;
  643. } else {
  644. mode = MODE_IMAGE;
  645. tns.thumbs = NULL;
  646. load_image(fileidx);
  647. }
  648. win_open(&win);
  649. run();
  650. cleanup();
  651. return 0;
  652. }