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.
 
 
 
 
 
 

961 line
21 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. #include "sxiv.h"
  19. #define _MAPPINGS_CONFIG
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <locale.h>
  27. #include <signal.h>
  28. #include <sys/select.h>
  29. #include <sys/stat.h>
  30. #include <sys/wait.h>
  31. #include <time.h>
  32. #include <X11/keysym.h>
  33. #include <X11/XF86keysym.h>
  34. typedef struct {
  35. struct timeval when;
  36. bool active;
  37. timeout_f handler;
  38. } timeout_t;
  39. /* timeout handler functions: */
  40. void redraw(void);
  41. void reset_cursor(void);
  42. void animate(void);
  43. void slideshow(void);
  44. void clear_resize(void);
  45. appmode_t mode;
  46. arl_t arl;
  47. img_t img;
  48. tns_t tns;
  49. win_t win;
  50. fileinfo_t *files;
  51. int filecnt, fileidx;
  52. int alternate;
  53. int markcnt;
  54. int markidx;
  55. int prefix;
  56. bool extprefix;
  57. bool resized = false;
  58. typedef struct {
  59. int err;
  60. char *cmd;
  61. } extcmd_t;
  62. struct {
  63. extcmd_t f;
  64. int fd;
  65. unsigned int i, lastsep;
  66. pid_t pid;
  67. } info;
  68. struct {
  69. extcmd_t f;
  70. bool warned;
  71. } keyhandler;
  72. timeout_t timeouts[] = {
  73. { { 0, 0 }, false, redraw },
  74. { { 0, 0 }, false, reset_cursor },
  75. { { 0, 0 }, false, animate },
  76. { { 0, 0 }, false, slideshow },
  77. { { 0, 0 }, false, clear_resize },
  78. };
  79. cursor_t imgcursor[3] = {
  80. CURSOR_ARROW, CURSOR_ARROW, CURSOR_ARROW
  81. };
  82. void cleanup(void)
  83. {
  84. img_close(&img, false);
  85. arl_cleanup(&arl);
  86. tns_free(&tns);
  87. win_close(&win);
  88. }
  89. void check_add_file(char *filename, bool given)
  90. {
  91. char *path;
  92. if (*filename == '\0')
  93. return;
  94. if (access(filename, R_OK) < 0 ||
  95. (path = realpath(filename, NULL)) == NULL)
  96. {
  97. if (given)
  98. error(0, errno, "%s", filename);
  99. return;
  100. }
  101. if (fileidx == filecnt) {
  102. filecnt *= 2;
  103. files = erealloc(files, filecnt * sizeof(*files));
  104. memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
  105. }
  106. files[fileidx].name = estrdup(filename);
  107. files[fileidx].path = path;
  108. if (given)
  109. files[fileidx].flags |= FF_WARN;
  110. fileidx++;
  111. }
  112. void remove_file(int n, bool manual)
  113. {
  114. if (n < 0 || n >= filecnt)
  115. return;
  116. if (filecnt == 1) {
  117. if (!manual)
  118. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  119. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  120. }
  121. if (files[n].flags & FF_MARK)
  122. markcnt--;
  123. if (files[n].path != files[n].name)
  124. free((void*) files[n].path);
  125. free((void*) files[n].name);
  126. if (n + 1 < filecnt) {
  127. if (tns.thumbs != NULL) {
  128. memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
  129. sizeof(*tns.thumbs));
  130. memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
  131. }
  132. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
  133. }
  134. filecnt--;
  135. if (n < fileidx)
  136. fileidx--;
  137. if (n < alternate)
  138. alternate--;
  139. if (n < markidx)
  140. markidx--;
  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 close_info(void)
  190. {
  191. if (info.fd != -1) {
  192. kill(info.pid, SIGTERM);
  193. close(info.fd);
  194. info.fd = -1;
  195. }
  196. }
  197. void open_info(void)
  198. {
  199. int pfd[2];
  200. char w[12], h[12];
  201. if (info.f.err != 0 || info.fd >= 0 || win.bar.h == 0)
  202. return;
  203. win.bar.l.buf[0] = '\0';
  204. if (pipe(pfd) < 0)
  205. return;
  206. if ((info.pid = fork()) == 0) {
  207. close(pfd[0]);
  208. dup2(pfd[1], 1);
  209. snprintf(w, sizeof(w), "%d", img.w);
  210. snprintf(h, sizeof(h), "%d", img.h);
  211. execl(info.f.cmd, info.f.cmd, files[fileidx].name, w, h, NULL);
  212. error(EXIT_FAILURE, errno, "exec: %s", info.f.cmd);
  213. }
  214. close(pfd[1]);
  215. if (info.pid < 0) {
  216. close(pfd[0]);
  217. } else {
  218. fcntl(pfd[0], F_SETFL, O_NONBLOCK);
  219. info.fd = pfd[0];
  220. info.i = info.lastsep = 0;
  221. }
  222. }
  223. void read_info(void)
  224. {
  225. ssize_t i, n;
  226. char buf[BAR_L_LEN];
  227. while (true) {
  228. n = read(info.fd, buf, sizeof(buf));
  229. if (n < 0 && errno == EAGAIN)
  230. return;
  231. else if (n == 0)
  232. goto end;
  233. for (i = 0; i < n; i++) {
  234. if (buf[i] == '\n') {
  235. if (info.lastsep == 0) {
  236. win.bar.l.buf[info.i++] = ' ';
  237. info.lastsep = 1;
  238. }
  239. } else {
  240. win.bar.l.buf[info.i++] = buf[i];
  241. info.lastsep = 0;
  242. }
  243. if (info.i + 1 == win.bar.l.size)
  244. goto end;
  245. }
  246. }
  247. end:
  248. info.i -= info.lastsep;
  249. win.bar.l.buf[info.i] = '\0';
  250. win_draw(&win);
  251. close_info();
  252. }
  253. void load_image(int new)
  254. {
  255. bool prev = new < fileidx;
  256. static int current;
  257. if (new < 0 || new >= filecnt)
  258. return;
  259. if (win.xwin != None)
  260. win_set_cursor(&win, CURSOR_WATCH);
  261. reset_timeout(slideshow);
  262. if (new != current)
  263. alternate = current;
  264. img_close(&img, false);
  265. while (!img_load(&img, &files[new])) {
  266. remove_file(new, false);
  267. if (new >= filecnt)
  268. new = filecnt - 1;
  269. else if (new > 0 && prev)
  270. new--;
  271. }
  272. files[new].flags &= ~FF_WARN;
  273. fileidx = current = new;
  274. close_info();
  275. open_info();
  276. arl_setup(&arl, files[fileidx].path);
  277. if (img.multi.cnt > 0 && img.multi.animate)
  278. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  279. else
  280. reset_timeout(animate);
  281. }
  282. bool mark_image(int n, bool on)
  283. {
  284. markidx = n;
  285. if (!!(files[n].flags & FF_MARK) != on) {
  286. files[n].flags ^= FF_MARK;
  287. markcnt += on ? 1 : -1;
  288. if (mode == MODE_THUMB)
  289. tns_mark(&tns, n, on);
  290. return true;
  291. }
  292. return false;
  293. }
  294. void bar_put(win_bar_t *bar, const char *fmt, ...)
  295. {
  296. size_t len = bar->size - (bar->p - bar->buf), n;
  297. va_list ap;
  298. va_start(ap, fmt);
  299. n = vsnprintf(bar->p, len, fmt, ap);
  300. bar->p += MIN(len, n);
  301. va_end(ap);
  302. }
  303. #define BAR_SEP " "
  304. void update_info(void)
  305. {
  306. unsigned int i, fn, fw;
  307. char title[256];
  308. const char * mark;
  309. win_bar_t *l = &win.bar.l, *r = &win.bar.r;
  310. /* update window title */
  311. if (mode == MODE_THUMB) {
  312. win_set_title(&win, "sxiv");
  313. } else {
  314. snprintf(title, sizeof(title), "sxiv - %s", files[fileidx].name);
  315. win_set_title(&win, title);
  316. }
  317. /* update bar contents */
  318. if (win.bar.h == 0)
  319. return;
  320. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  321. mark = files[fileidx].flags & FF_MARK ? "* " : "";
  322. l->p = l->buf;
  323. r->p = r->buf;
  324. if (mode == MODE_THUMB) {
  325. if (tns.loadnext < tns.end)
  326. bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
  327. else if (tns.initnext < filecnt)
  328. bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
  329. else
  330. strncpy(l->buf, files[fileidx].name, l->size);
  331. bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
  332. } else {
  333. bar_put(r, "%s", mark);
  334. if (img.ss.on) {
  335. if (img.ss.delay % 10 != 0)
  336. bar_put(r, "%2.1fs" BAR_SEP, (float)img.ss.delay / 10);
  337. else
  338. bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
  339. }
  340. if (img.gamma != 0)
  341. bar_put(r, "G%+d" BAR_SEP, img.gamma);
  342. bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0));
  343. if (img.multi.cnt > 0) {
  344. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  345. bar_put(r, "%0*d/%d" BAR_SEP, fn, img.multi.sel + 1, img.multi.cnt);
  346. }
  347. bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
  348. if (info.f.err)
  349. strncpy(l->buf, files[fileidx].name, l->size);
  350. }
  351. }
  352. int ptr_third_x(void)
  353. {
  354. int x, y;
  355. win_cursor_pos(&win, &x, &y);
  356. return MAX(0, MIN(2, (x / (win.w * 0.33))));
  357. }
  358. void redraw(void)
  359. {
  360. int t;
  361. if (mode == MODE_IMAGE) {
  362. img_render(&img);
  363. if (img.ss.on) {
  364. t = img.ss.delay * 100;
  365. if (img.multi.cnt > 0 && img.multi.animate)
  366. t = MAX(t, img.multi.length);
  367. set_timeout(slideshow, t, false);
  368. }
  369. } else {
  370. tns_render(&tns);
  371. }
  372. update_info();
  373. win_draw(&win);
  374. reset_timeout(redraw);
  375. reset_cursor();
  376. }
  377. void reset_cursor(void)
  378. {
  379. int c, i;
  380. cursor_t cursor = CURSOR_NONE;
  381. if (mode == MODE_IMAGE) {
  382. for (i = 0; i < ARRLEN(timeouts); i++) {
  383. if (timeouts[i].handler == reset_cursor) {
  384. if (timeouts[i].active) {
  385. c = ptr_third_x();
  386. c = MAX(fileidx > 0 ? 0 : 1, c);
  387. c = MIN(fileidx + 1 < filecnt ? 2 : 1, c);
  388. cursor = imgcursor[c];
  389. }
  390. break;
  391. }
  392. }
  393. } else {
  394. if (tns.loadnext < tns.end || tns.initnext < filecnt)
  395. cursor = CURSOR_WATCH;
  396. else
  397. cursor = CURSOR_ARROW;
  398. }
  399. win_set_cursor(&win, cursor);
  400. }
  401. void animate(void)
  402. {
  403. if (img_frame_animate(&img)) {
  404. redraw();
  405. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  406. }
  407. }
  408. void slideshow(void)
  409. {
  410. load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
  411. redraw();
  412. }
  413. void clear_resize(void)
  414. {
  415. resized = false;
  416. }
  417. Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
  418. {
  419. return ev->type == ButtonPress || ev->type == KeyPress;
  420. }
  421. void run_key_handler(const char *key, unsigned int mask)
  422. {
  423. pid_t pid;
  424. FILE *pfs;
  425. bool marked = mode == MODE_THUMB && markcnt > 0;
  426. bool changed = false;
  427. int f, i, pfd[2];
  428. int fcnt = marked ? markcnt : 1;
  429. char kstr[32];
  430. struct stat *oldst, st;
  431. XEvent dump;
  432. if (keyhandler.f.err != 0) {
  433. if (!keyhandler.warned) {
  434. error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
  435. keyhandler.warned = true;
  436. }
  437. return;
  438. }
  439. if (key == NULL)
  440. return;
  441. if (pipe(pfd) < 0) {
  442. error(0, errno, "pipe");
  443. return;
  444. }
  445. if ((pfs = fdopen(pfd[1], "w")) == NULL) {
  446. error(0, errno, "open pipe");
  447. close(pfd[0]), close(pfd[1]);
  448. return;
  449. }
  450. oldst = emalloc(fcnt * sizeof(*oldst));
  451. close_info();
  452. strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
  453. win_draw(&win);
  454. win_set_cursor(&win, CURSOR_WATCH);
  455. snprintf(kstr, sizeof(kstr), "%s%s%s%s",
  456. mask & ControlMask ? "C-" : "",
  457. mask & Mod1Mask ? "M-" : "",
  458. mask & ShiftMask ? "S-" : "", key);
  459. if ((pid = fork()) == 0) {
  460. close(pfd[1]);
  461. dup2(pfd[0], 0);
  462. execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
  463. error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
  464. }
  465. close(pfd[0]);
  466. if (pid < 0) {
  467. error(0, errno, "fork");
  468. fclose(pfs);
  469. goto end;
  470. }
  471. for (f = i = 0; f < fcnt; i++) {
  472. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  473. stat(files[i].path, &oldst[f]);
  474. fprintf(pfs, "%s\n", files[i].name);
  475. f++;
  476. }
  477. }
  478. fclose(pfs);
  479. while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
  480. for (f = i = 0; f < fcnt; i++) {
  481. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  482. if (stat(files[i].path, &st) != 0 ||
  483. memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
  484. {
  485. if (tns.thumbs != NULL) {
  486. tns_unload(&tns, i);
  487. tns.loadnext = MIN(tns.loadnext, i);
  488. }
  489. changed = true;
  490. }
  491. f++;
  492. }
  493. }
  494. /* drop user input events that occurred while running the key handler */
  495. while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
  496. end:
  497. if (mode == MODE_IMAGE) {
  498. if (changed) {
  499. img_close(&img, true);
  500. load_image(fileidx);
  501. } else {
  502. open_info();
  503. }
  504. }
  505. free(oldst);
  506. reset_cursor();
  507. redraw();
  508. }
  509. #define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
  510. void on_keypress(XKeyEvent *kev)
  511. {
  512. int i;
  513. unsigned int sh = 0;
  514. KeySym ksym, shksym;
  515. char dummy, key;
  516. bool dirty = false;
  517. XLookupString(kev, &key, 1, &ksym, NULL);
  518. if (kev->state & ShiftMask) {
  519. kev->state &= ~ShiftMask;
  520. XLookupString(kev, &dummy, 1, &shksym, NULL);
  521. kev->state |= ShiftMask;
  522. if (ksym != shksym)
  523. sh = ShiftMask;
  524. }
  525. if (IsModifierKey(ksym))
  526. return;
  527. if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
  528. extprefix = False;
  529. } else if (extprefix) {
  530. run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
  531. extprefix = False;
  532. } else if (key >= '0' && key <= '9') {
  533. /* number prefix for commands */
  534. prefix = prefix * 10 + (int) (key - '0');
  535. return;
  536. } else for (i = 0; i < ARRLEN(keys); i++) {
  537. if (keys[i].ksym == ksym &&
  538. MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
  539. keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
  540. (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
  541. {
  542. if (cmds[keys[i].cmd].func(keys[i].arg))
  543. dirty = true;
  544. }
  545. }
  546. if (dirty)
  547. redraw();
  548. prefix = 0;
  549. }
  550. void on_buttonpress(XButtonEvent *bev)
  551. {
  552. int i, sel;
  553. bool dirty = false;
  554. static Time firstclick;
  555. if (mode == MODE_IMAGE) {
  556. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  557. reset_cursor();
  558. for (i = 0; i < ARRLEN(buttons); i++) {
  559. if (buttons[i].button == bev->button &&
  560. MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
  561. buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
  562. (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
  563. {
  564. if (cmds[buttons[i].cmd].func(buttons[i].arg))
  565. dirty = true;
  566. }
  567. }
  568. if (dirty)
  569. redraw();
  570. } else {
  571. /* thumbnail mode (hard-coded) */
  572. switch (bev->button) {
  573. case Button1:
  574. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  575. if (sel != fileidx) {
  576. tns_highlight(&tns, fileidx, false);
  577. tns_highlight(&tns, sel, true);
  578. fileidx = sel;
  579. firstclick = bev->time;
  580. redraw();
  581. } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
  582. mode = MODE_IMAGE;
  583. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  584. load_image(fileidx);
  585. redraw();
  586. } else {
  587. firstclick = bev->time;
  588. }
  589. }
  590. break;
  591. case Button3:
  592. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  593. bool on = !(files[sel].flags & FF_MARK);
  594. XEvent e;
  595. for (;;) {
  596. if (sel >= 0 && mark_image(sel, on))
  597. redraw();
  598. XMaskEvent(win.env.dpy,
  599. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  600. if (e.type == ButtonPress || e.type == ButtonRelease)
  601. break;
  602. while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e));
  603. sel = tns_translate(&tns, e.xbutton.x, e.xbutton.y);
  604. }
  605. }
  606. break;
  607. case Button4:
  608. case Button5:
  609. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  610. (bev->state & ControlMask) != 0))
  611. redraw();
  612. break;
  613. }
  614. }
  615. prefix = 0;
  616. }
  617. const struct timespec ten_ms = {0, 10000000};
  618. void run(void)
  619. {
  620. int xfd;
  621. fd_set fds;
  622. struct timeval timeout;
  623. bool discard, init_thumb, load_thumb, to_set;
  624. XEvent ev, nextev;
  625. while (true) {
  626. to_set = check_timeouts(&timeout);
  627. init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
  628. load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
  629. if ((init_thumb || load_thumb || to_set || info.fd != -1 ||
  630. arl.fd != -1) && XPending(win.env.dpy) == 0)
  631. {
  632. if (load_thumb) {
  633. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  634. if (!tns_load(&tns, tns.loadnext, false, false)) {
  635. remove_file(tns.loadnext, false);
  636. tns.dirty = true;
  637. }
  638. if (tns.loadnext >= tns.end)
  639. redraw();
  640. } else if (init_thumb) {
  641. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  642. if (!tns_load(&tns, tns.initnext, false, true))
  643. remove_file(tns.initnext, false);
  644. } else {
  645. xfd = ConnectionNumber(win.env.dpy);
  646. FD_ZERO(&fds);
  647. FD_SET(xfd, &fds);
  648. if (info.fd != -1) {
  649. FD_SET(info.fd, &fds);
  650. xfd = MAX(xfd, info.fd);
  651. }
  652. if (arl.fd != -1) {
  653. FD_SET(arl.fd, &fds);
  654. xfd = MAX(xfd, arl.fd);
  655. }
  656. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  657. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  658. read_info();
  659. if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
  660. if (arl_handle(&arl)) {
  661. /* when too fast, imlib2 can't load the image */
  662. nanosleep(&ten_ms, NULL);
  663. img_close(&img, true);
  664. load_image(fileidx);
  665. redraw();
  666. }
  667. }
  668. }
  669. continue;
  670. }
  671. do {
  672. XNextEvent(win.env.dpy, &ev);
  673. discard = false;
  674. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  675. XPeekEvent(win.env.dpy, &nextev);
  676. switch (ev.type) {
  677. case ConfigureNotify:
  678. case MotionNotify:
  679. discard = ev.type == nextev.type;
  680. break;
  681. case KeyPress:
  682. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  683. && ev.xkey.keycode == nextev.xkey.keycode;
  684. break;
  685. }
  686. }
  687. } while (discard);
  688. switch (ev.type) {
  689. /* handle events */
  690. case ButtonPress:
  691. on_buttonpress(&ev.xbutton);
  692. break;
  693. case ClientMessage:
  694. if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
  695. cmds[g_quit].func(0);
  696. break;
  697. case ConfigureNotify:
  698. if (win_configure(&win, &ev.xconfigure)) {
  699. if (mode == MODE_IMAGE) {
  700. img.dirty = true;
  701. img.checkpan = true;
  702. } else {
  703. tns.dirty = true;
  704. }
  705. if (!resized || win.fullscreen) {
  706. redraw();
  707. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  708. resized = true;
  709. } else {
  710. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  711. }
  712. }
  713. break;
  714. case KeyPress:
  715. on_keypress(&ev.xkey);
  716. break;
  717. case MotionNotify:
  718. if (mode == MODE_IMAGE) {
  719. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  720. reset_cursor();
  721. }
  722. break;
  723. }
  724. }
  725. }
  726. int fncmp(const void *a, const void *b)
  727. {
  728. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  729. }
  730. void sigchld(int sig)
  731. {
  732. while (waitpid(-1, NULL, WNOHANG) > 0);
  733. }
  734. void setup_signal(int sig, void (*handler)(int sig))
  735. {
  736. struct sigaction sa;
  737. sa.sa_handler = handler;
  738. sigemptyset(&sa.sa_mask);
  739. sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
  740. if (sigaction(sig, &sa, 0) == -1)
  741. error(EXIT_FAILURE, errno, "signal %d", sig);
  742. }
  743. int main(int argc, char **argv)
  744. {
  745. int i, start;
  746. size_t n;
  747. ssize_t len;
  748. char *filename;
  749. const char *homedir, *dsuffix = "";
  750. struct stat fstats;
  751. r_dir_t dir;
  752. setup_signal(SIGCHLD, sigchld);
  753. setup_signal(SIGPIPE, SIG_IGN);
  754. setlocale(LC_COLLATE, "");
  755. parse_options(argc, argv);
  756. if (options->clean_cache) {
  757. tns_init(&tns, NULL, NULL, NULL, NULL);
  758. tns_clean_cache(&tns);
  759. exit(EXIT_SUCCESS);
  760. }
  761. if (options->filecnt == 0 && !options->from_stdin) {
  762. print_usage();
  763. exit(EXIT_FAILURE);
  764. }
  765. if (options->recursive || options->from_stdin)
  766. filecnt = 1024;
  767. else
  768. filecnt = options->filecnt;
  769. files = emalloc(filecnt * sizeof(*files));
  770. memset(files, 0, filecnt * sizeof(*files));
  771. fileidx = 0;
  772. if (options->from_stdin) {
  773. n = 0;
  774. filename = NULL;
  775. while ((len = getline(&filename, &n, stdin)) > 0) {
  776. if (filename[len-1] == '\n')
  777. filename[len-1] = '\0';
  778. check_add_file(filename, true);
  779. }
  780. free(filename);
  781. }
  782. for (i = 0; i < options->filecnt; i++) {
  783. filename = options->filenames[i];
  784. if (stat(filename, &fstats) < 0) {
  785. error(0, errno, "%s", filename);
  786. continue;
  787. }
  788. if (!S_ISDIR(fstats.st_mode)) {
  789. check_add_file(filename, true);
  790. } else {
  791. if (r_opendir(&dir, filename, options->recursive) < 0) {
  792. error(0, errno, "%s", filename);
  793. continue;
  794. }
  795. start = fileidx;
  796. while ((filename = r_readdir(&dir, true)) != NULL) {
  797. check_add_file(filename, false);
  798. free((void*) filename);
  799. }
  800. r_closedir(&dir);
  801. if (fileidx - start > 1)
  802. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  803. }
  804. }
  805. if (fileidx == 0)
  806. error(EXIT_FAILURE, 0, "No valid image file given, aborting");
  807. filecnt = fileidx;
  808. fileidx = options->startnum < filecnt ? options->startnum : 0;
  809. for (i = 0; i < ARRLEN(buttons); i++) {
  810. if (buttons[i].cmd == i_cursor_navigate) {
  811. imgcursor[0] = CURSOR_LEFT;
  812. imgcursor[2] = CURSOR_RIGHT;
  813. break;
  814. }
  815. }
  816. win_init(&win);
  817. img_init(&img, &win);
  818. arl_init(&arl);
  819. if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
  820. homedir = getenv("HOME");
  821. dsuffix = "/.config";
  822. }
  823. if (homedir != NULL) {
  824. extcmd_t *cmd[] = { &info.f, &keyhandler.f };
  825. const char *name[] = { "image-info", "key-handler" };
  826. for (i = 0; i < ARRLEN(cmd); i++) {
  827. n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
  828. cmd[i]->cmd = (char*) emalloc(n);
  829. snprintf(cmd[i]->cmd, n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
  830. if (access(cmd[i]->cmd, X_OK) != 0)
  831. cmd[i]->err = errno;
  832. }
  833. } else {
  834. error(0, 0, "Exec directory not found");
  835. }
  836. info.fd = -1;
  837. if (options->thumb_mode) {
  838. mode = MODE_THUMB;
  839. tns_init(&tns, files, &filecnt, &fileidx, &win);
  840. while (!tns_load(&tns, fileidx, false, false))
  841. remove_file(fileidx, false);
  842. } else {
  843. mode = MODE_IMAGE;
  844. tns.thumbs = NULL;
  845. load_image(fileidx);
  846. }
  847. win_open(&win);
  848. win_set_cursor(&win, CURSOR_WATCH);
  849. atexit(cleanup);
  850. set_timeout(redraw, 25, false);
  851. run();
  852. return 0;
  853. }