A Simple X Image Viewer
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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