A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

729 řádky
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 markcnt;
  61. int alternate;
  62. int prefix;
  63. bool resized = false;
  64. const char * const INFO_SCRIPT = ".sxiv/exec/image-info";
  65. struct {
  66. char *script;
  67. int fd;
  68. unsigned int i, lastsep;
  69. bool open;
  70. } info;
  71. timeout_t timeouts[] = {
  72. { { 0, 0 }, false, redraw },
  73. { { 0, 0 }, false, reset_cursor },
  74. { { 0, 0 }, false, animate },
  75. { { 0, 0 }, false, clear_resize },
  76. };
  77. void cleanup(void)
  78. {
  79. static bool in = false;
  80. if (!in) {
  81. in = true;
  82. img_close(&img, false);
  83. tns_free(&tns);
  84. win_close(&win);
  85. }
  86. }
  87. void check_add_file(char *filename)
  88. {
  89. const char *bn;
  90. if (filename == NULL || *filename == '\0')
  91. return;
  92. if (access(filename, R_OK) < 0) {
  93. warn("could not open file: %s", filename);
  94. return;
  95. }
  96. if (fileidx == filecnt) {
  97. filecnt *= 2;
  98. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  99. }
  100. if (*filename != '/') {
  101. files[fileidx].path = absolute_path(filename);
  102. if (files[fileidx].path == NULL) {
  103. warn("could not get absolute path of file: %s\n", filename);
  104. return;
  105. }
  106. }
  107. files[fileidx].loaded = false;
  108. files[fileidx].name = s_strdup(filename);
  109. if (*filename == '/')
  110. files[fileidx].path = files[fileidx].name;
  111. if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
  112. files[fileidx].base = ++bn;
  113. else
  114. files[fileidx].base = files[fileidx].name;
  115. fileidx++;
  116. }
  117. void remove_file(int n, bool manual)
  118. {
  119. if (n < 0 || n >= filecnt)
  120. return;
  121. if (filecnt == 1) {
  122. if (!manual)
  123. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  124. cleanup();
  125. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  126. }
  127. if (files[n].path != files[n].name)
  128. free((void*) files[n].path);
  129. free((void*) files[n].name);
  130. if (n + 1 < filecnt)
  131. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  132. if (n + 1 < tns.cnt) {
  133. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  134. sizeof(thumb_t));
  135. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  136. }
  137. filecnt--;
  138. if (n < tns.cnt)
  139. tns.cnt--;
  140. if (n < alternate)
  141. alternate--;
  142. }
  143. void set_timeout(timeout_f handler, int time, bool overwrite)
  144. {
  145. int i;
  146. for (i = 0; i < ARRLEN(timeouts); i++) {
  147. if (timeouts[i].handler == handler) {
  148. if (!timeouts[i].active || overwrite) {
  149. gettimeofday(&timeouts[i].when, 0);
  150. TV_ADD_MSEC(&timeouts[i].when, time);
  151. timeouts[i].active = true;
  152. }
  153. return;
  154. }
  155. }
  156. }
  157. void reset_timeout(timeout_f handler)
  158. {
  159. int i;
  160. for (i = 0; i < ARRLEN(timeouts); i++) {
  161. if (timeouts[i].handler == handler) {
  162. timeouts[i].active = false;
  163. return;
  164. }
  165. }
  166. }
  167. bool check_timeouts(struct timeval *t)
  168. {
  169. int i = 0, tdiff, tmin = -1;
  170. struct timeval now;
  171. while (i < ARRLEN(timeouts)) {
  172. if (timeouts[i].active) {
  173. gettimeofday(&now, 0);
  174. tdiff = TV_DIFF(&timeouts[i].when, &now);
  175. if (tdiff <= 0) {
  176. timeouts[i].active = false;
  177. if (timeouts[i].handler != NULL)
  178. timeouts[i].handler();
  179. i = tmin = -1;
  180. } else if (tmin < 0 || tdiff < tmin) {
  181. tmin = tdiff;
  182. }
  183. }
  184. i++;
  185. }
  186. if (tmin > 0 && t != NULL)
  187. TV_SET_MSEC(t, tmin);
  188. return tmin > 0;
  189. }
  190. void open_info(void)
  191. {
  192. static pid_t pid;
  193. int pfd[2];
  194. if (info.script == NULL || info.open || win.bar.h == 0)
  195. return;
  196. if (info.fd != -1) {
  197. close(info.fd);
  198. kill(pid, SIGTERM);
  199. info.fd = -1;
  200. }
  201. win.bar.l[0] = '\0';
  202. if (pipe(pfd) < 0)
  203. return;
  204. pid = fork();
  205. if (pid > 0) {
  206. close(pfd[1]);
  207. fcntl(pfd[0], F_SETFL, O_NONBLOCK);
  208. info.fd = pfd[0];
  209. info.i = info.lastsep = 0;
  210. info.open = true;
  211. } else if (pid == 0) {
  212. close(pfd[0]);
  213. dup2(pfd[1], 1);
  214. execl(info.script, info.script, files[fileidx].name, NULL);
  215. warn("could not exec: %s", info.script);
  216. exit(EXIT_FAILURE);
  217. }
  218. }
  219. void read_info(void)
  220. {
  221. ssize_t i, n;
  222. char buf[BAR_L_LEN];
  223. while (true) {
  224. n = read(info.fd, buf, sizeof(buf));
  225. if (n < 0 && errno == EAGAIN)
  226. return;
  227. else if (n == 0)
  228. goto end;
  229. for (i = 0; i < n; i++) {
  230. if (buf[i] == '\n') {
  231. if (info.lastsep == 0) {
  232. win.bar.l[info.i++] = ' ';
  233. info.lastsep = 1;
  234. }
  235. } else {
  236. win.bar.l[info.i++] = buf[i];
  237. info.lastsep = 0;
  238. }
  239. if (info.i + 1 == sizeof(win.bar.l))
  240. goto end;
  241. }
  242. }
  243. end:
  244. info.i -= info.lastsep;
  245. win.bar.l[info.i] = '\0';
  246. win_update_bar(&win);
  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 < 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%3d%% | ", mark, (int) (img.zoom * 100.0));
  306. if (img.multi.cnt > 0) {
  307. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  308. n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
  309. fn, img.multi.sel + 1, img.multi.cnt);
  310. }
  311. n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
  312. ow_info = info.script == NULL;
  313. }
  314. if (ow_info) {
  315. fn = strlen(files[sel].name);
  316. if (fn < llen &&
  317. win_textwidth(files[sel].name, fn, true) +
  318. win_textwidth(rt, n, true) < win.w)
  319. {
  320. strncpy(lt, files[sel].name, llen);
  321. } else {
  322. strncpy(lt, files[sel].base, llen);
  323. }
  324. }
  325. }
  326. void redraw(void)
  327. {
  328. if (mode == MODE_IMAGE)
  329. img_render(&img);
  330. else
  331. tns_render(&tns);
  332. update_info();
  333. win_draw(&win);
  334. reset_timeout(redraw);
  335. reset_cursor();
  336. }
  337. void reset_cursor(void)
  338. {
  339. int i;
  340. cursor_t cursor = CURSOR_NONE;
  341. if (mode == MODE_IMAGE) {
  342. for (i = 0; i < ARRLEN(timeouts); i++) {
  343. if (timeouts[i].handler == reset_cursor) {
  344. if (timeouts[i].active)
  345. cursor = CURSOR_ARROW;
  346. break;
  347. }
  348. }
  349. } else {
  350. if (tns.cnt != filecnt)
  351. cursor = CURSOR_WATCH;
  352. else
  353. cursor = CURSOR_ARROW;
  354. }
  355. win_set_cursor(&win, cursor);
  356. }
  357. void animate(void)
  358. {
  359. if (img_frame_animate(&img, false)) {
  360. redraw();
  361. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  362. }
  363. }
  364. void clear_resize(void)
  365. {
  366. resized = false;
  367. }
  368. bool keymask(const keymap_t *k, unsigned int state)
  369. {
  370. return (k->ctrl ? ControlMask : 0) == (state & ControlMask);
  371. }
  372. bool buttonmask(const button_t *b, unsigned int state)
  373. {
  374. return ((b->ctrl ? ControlMask : 0) | (b->shift ? ShiftMask : 0)) ==
  375. (state & (ControlMask | ShiftMask));
  376. }
  377. void on_keypress(XKeyEvent *kev)
  378. {
  379. int i;
  380. KeySym ksym;
  381. char key;
  382. if (kev == NULL)
  383. return;
  384. XLookupString(kev, &key, 1, &ksym, NULL);
  385. if ((ksym == XK_Escape || (key >= '0' && key <= '9')) &&
  386. (kev->state & ControlMask) == 0)
  387. {
  388. /* number prefix for commands */
  389. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  390. return;
  391. }
  392. for (i = 0; i < ARRLEN(keys); i++) {
  393. if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
  394. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  395. redraw();
  396. prefix = 0;
  397. return;
  398. }
  399. }
  400. }
  401. void on_buttonpress(XButtonEvent *bev)
  402. {
  403. int i, sel;
  404. if (bev == NULL)
  405. return;
  406. if (mode == MODE_IMAGE) {
  407. win_set_cursor(&win, CURSOR_ARROW);
  408. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  409. for (i = 0; i < ARRLEN(buttons); i++) {
  410. if (buttons[i].button == bev->button &&
  411. buttonmask(&buttons[i], bev->state))
  412. {
  413. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  414. redraw();
  415. return;
  416. }
  417. }
  418. } else {
  419. /* thumbnail mode (hard-coded) */
  420. switch (bev->button) {
  421. case Button1:
  422. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  423. if (sel == tns.sel) {
  424. mode = MODE_IMAGE;
  425. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  426. load_image(tns.sel);
  427. } else {
  428. tns_highlight(&tns, tns.sel, false);
  429. tns_highlight(&tns, sel, true);
  430. tns.sel = sel;
  431. }
  432. redraw();
  433. break;
  434. }
  435. break;
  436. case Button4:
  437. case Button5:
  438. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  439. (bev->state & ControlMask) != 0))
  440. redraw();
  441. break;
  442. }
  443. }
  444. }
  445. void run(void)
  446. {
  447. int xfd;
  448. fd_set fds;
  449. struct timeval timeout;
  450. bool discard, to_set;
  451. XEvent ev, nextev;
  452. redraw();
  453. while (true) {
  454. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  455. XPending(win.env.dpy) == 0)
  456. {
  457. /* load thumbnails */
  458. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  459. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  460. tns.cnt++;
  461. } else {
  462. remove_file(tns.cnt, false);
  463. if (tns.sel >= tns.cnt)
  464. tns.sel--;
  465. }
  466. if (tns.cnt == filecnt)
  467. redraw();
  468. else
  469. check_timeouts(NULL);
  470. }
  471. while (XPending(win.env.dpy) == 0
  472. && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
  473. {
  474. /* check for timeouts & input */
  475. xfd = ConnectionNumber(win.env.dpy);
  476. FD_ZERO(&fds);
  477. FD_SET(xfd, &fds);
  478. if (info.fd != -1) {
  479. FD_SET(info.fd, &fds);
  480. xfd = MAX(xfd, info.fd);
  481. }
  482. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  483. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  484. read_info();
  485. }
  486. do {
  487. XNextEvent(win.env.dpy, &ev);
  488. discard = false;
  489. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  490. XPeekEvent(win.env.dpy, &nextev);
  491. switch (ev.type) {
  492. case ConfigureNotify:
  493. discard = ev.type == nextev.type;
  494. break;
  495. case KeyPress:
  496. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  497. && ev.xkey.keycode == nextev.xkey.keycode;
  498. break;
  499. }
  500. }
  501. } while (discard);
  502. switch (ev.type) {
  503. /* handle events */
  504. case ButtonPress:
  505. on_buttonpress(&ev.xbutton);
  506. break;
  507. case ClientMessage:
  508. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  509. return;
  510. break;
  511. case ConfigureNotify:
  512. if (win_configure(&win, &ev.xconfigure)) {
  513. if (mode == MODE_IMAGE) {
  514. img.dirty = true;
  515. img.checkpan = true;
  516. } else {
  517. tns.dirty = true;
  518. }
  519. if (!resized || win.fullscreen) {
  520. redraw();
  521. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  522. resized = true;
  523. } else {
  524. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  525. }
  526. }
  527. break;
  528. case Expose:
  529. win_expose(&win, &ev.xexpose);
  530. break;
  531. case KeyPress:
  532. on_keypress(&ev.xkey);
  533. break;
  534. case MotionNotify:
  535. if (mode == MODE_IMAGE) {
  536. win_set_cursor(&win, CURSOR_ARROW);
  537. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  538. }
  539. break;
  540. }
  541. }
  542. }
  543. int fncmp(const void *a, const void *b)
  544. {
  545. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  546. }
  547. int main(int argc, char **argv)
  548. {
  549. int i, start;
  550. size_t n;
  551. ssize_t len;
  552. char *filename;
  553. const char *homedir;
  554. struct stat fstats;
  555. r_dir_t dir;
  556. parse_options(argc, argv);
  557. if (options->clean_cache) {
  558. tns_init(&tns, 0, NULL);
  559. tns_clean_cache(&tns);
  560. exit(EXIT_SUCCESS);
  561. }
  562. if (options->filecnt == 0 && !options->from_stdin) {
  563. print_usage();
  564. exit(EXIT_FAILURE);
  565. }
  566. if (options->recursive || options->from_stdin)
  567. filecnt = FILENAME_CNT;
  568. else
  569. filecnt = options->filecnt;
  570. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  571. fileidx = 0;
  572. if (options->from_stdin) {
  573. filename = NULL;
  574. while ((len = get_line(&filename, &n, stdin)) > 0) {
  575. if (filename[len-1] == '\n')
  576. filename[len-1] = '\0';
  577. check_add_file(filename);
  578. }
  579. if (filename != NULL)
  580. free(filename);
  581. }
  582. for (i = 0; i < options->filecnt; i++) {
  583. filename = options->filenames[i];
  584. if (stat(filename, &fstats) < 0) {
  585. warn("could not stat file: %s", filename);
  586. continue;
  587. }
  588. if (!S_ISDIR(fstats.st_mode)) {
  589. check_add_file(filename);
  590. } else {
  591. if (!options->recursive) {
  592. warn("ignoring directory: %s", filename);
  593. continue;
  594. }
  595. if (r_opendir(&dir, filename) < 0) {
  596. warn("could not open directory: %s", filename);
  597. continue;
  598. }
  599. start = fileidx;
  600. while ((filename = r_readdir(&dir)) != NULL) {
  601. check_add_file(filename);
  602. free((void*) filename);
  603. }
  604. r_closedir(&dir);
  605. if (fileidx - start > 1)
  606. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  607. }
  608. }
  609. if (fileidx == 0) {
  610. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  611. exit(EXIT_FAILURE);
  612. }
  613. filecnt = fileidx;
  614. fileidx = options->startnum < filecnt ? options->startnum : 0;
  615. win_init(&win);
  616. img_init(&img, &win);
  617. if ((homedir = getenv("HOME")) == NULL) {
  618. warn("could not locate home directory");
  619. } else {
  620. len = strlen(homedir) + strlen(INFO_SCRIPT) + 2;
  621. info.script = (char*) s_malloc(len);
  622. snprintf(info.script, len, "%s/%s", homedir, INFO_SCRIPT);
  623. if (access(info.script, X_OK) != 0) {
  624. free(info.script);
  625. info.script = NULL;
  626. }
  627. }
  628. info.fd = -1;
  629. if (options->thumb_mode) {
  630. mode = MODE_THUMB;
  631. tns_init(&tns, filecnt, &win);
  632. while (!tns_load(&tns, 0, &files[0], false, false))
  633. remove_file(0, false);
  634. tns.cnt = 1;
  635. } else {
  636. mode = MODE_IMAGE;
  637. tns.thumbs = NULL;
  638. load_image(fileidx);
  639. }
  640. win_open(&win);
  641. run();
  642. cleanup();
  643. return 0;
  644. }