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.
 
 
 
 
 
 

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