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