A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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