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.
 
 
 
 
 
 

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