A Simple X Image Viewer
 
 
 
 
 
 

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