A Simple X Image Viewer
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

952 lines
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 (fileidx > n || fileidx == filecnt)
  136. fileidx--;
  137. if (alternate > n || alternate == filecnt)
  138. alternate--;
  139. if (markidx > n || markidx == filecnt)
  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. const char * mark;
  308. win_bar_t *l = &win.bar.l, *r = &win.bar.r;
  309. /* update bar contents */
  310. if (win.bar.h == 0)
  311. return;
  312. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  313. mark = files[fileidx].flags & FF_MARK ? "* " : "";
  314. l->p = l->buf;
  315. r->p = r->buf;
  316. if (mode == MODE_THUMB) {
  317. if (tns.loadnext < tns.end)
  318. bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
  319. else if (tns.initnext < filecnt)
  320. bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
  321. else
  322. strncpy(l->buf, files[fileidx].name, l->size);
  323. bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
  324. } else {
  325. bar_put(r, "%s", mark);
  326. if (img.ss.on) {
  327. if (img.ss.delay % 10 != 0)
  328. bar_put(r, "%2.1fs" BAR_SEP, (float)img.ss.delay / 10);
  329. else
  330. bar_put(r, "%ds" BAR_SEP, img.ss.delay / 10);
  331. }
  332. if (img.gamma != 0)
  333. bar_put(r, "G%+d" BAR_SEP, img.gamma);
  334. bar_put(r, "%3d%%" BAR_SEP, (int) (img.zoom * 100.0));
  335. if (img.multi.cnt > 0) {
  336. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  337. bar_put(r, "%0*d/%d" BAR_SEP, fn, img.multi.sel + 1, img.multi.cnt);
  338. }
  339. bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
  340. if (info.f.err)
  341. strncpy(l->buf, files[fileidx].name, l->size);
  342. }
  343. }
  344. int ptr_third_x(void)
  345. {
  346. int x, y;
  347. win_cursor_pos(&win, &x, &y);
  348. return MAX(0, MIN(2, (x / (win.w * 0.33))));
  349. }
  350. void redraw(void)
  351. {
  352. int t;
  353. if (mode == MODE_IMAGE) {
  354. img_render(&img);
  355. if (img.ss.on) {
  356. t = img.ss.delay * 100;
  357. if (img.multi.cnt > 0 && img.multi.animate)
  358. t = MAX(t, img.multi.length);
  359. set_timeout(slideshow, t, false);
  360. }
  361. } else {
  362. tns_render(&tns);
  363. }
  364. update_info();
  365. win_draw(&win);
  366. reset_timeout(redraw);
  367. reset_cursor();
  368. }
  369. void reset_cursor(void)
  370. {
  371. int c, i;
  372. cursor_t cursor = CURSOR_NONE;
  373. if (mode == MODE_IMAGE) {
  374. for (i = 0; i < ARRLEN(timeouts); i++) {
  375. if (timeouts[i].handler == reset_cursor) {
  376. if (timeouts[i].active) {
  377. c = ptr_third_x();
  378. c = MAX(fileidx > 0 ? 0 : 1, c);
  379. c = MIN(fileidx + 1 < filecnt ? 2 : 1, c);
  380. cursor = imgcursor[c];
  381. }
  382. break;
  383. }
  384. }
  385. } else {
  386. if (tns.loadnext < tns.end || tns.initnext < filecnt)
  387. cursor = CURSOR_WATCH;
  388. else
  389. cursor = CURSOR_ARROW;
  390. }
  391. win_set_cursor(&win, cursor);
  392. }
  393. void animate(void)
  394. {
  395. if (img_frame_animate(&img)) {
  396. redraw();
  397. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  398. }
  399. }
  400. void slideshow(void)
  401. {
  402. load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
  403. redraw();
  404. }
  405. void clear_resize(void)
  406. {
  407. resized = false;
  408. }
  409. Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
  410. {
  411. return ev->type == ButtonPress || ev->type == KeyPress;
  412. }
  413. void run_key_handler(const char *key, unsigned int mask)
  414. {
  415. pid_t pid;
  416. FILE *pfs;
  417. bool marked = mode == MODE_THUMB && markcnt > 0;
  418. bool changed = false;
  419. int f, i, pfd[2];
  420. int fcnt = marked ? markcnt : 1;
  421. char kstr[32];
  422. struct stat *oldst, st;
  423. XEvent dump;
  424. if (keyhandler.f.err != 0) {
  425. if (!keyhandler.warned) {
  426. error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
  427. keyhandler.warned = true;
  428. }
  429. return;
  430. }
  431. if (key == NULL)
  432. return;
  433. if (pipe(pfd) < 0) {
  434. error(0, errno, "pipe");
  435. return;
  436. }
  437. if ((pfs = fdopen(pfd[1], "w")) == NULL) {
  438. error(0, errno, "open pipe");
  439. close(pfd[0]), close(pfd[1]);
  440. return;
  441. }
  442. oldst = emalloc(fcnt * sizeof(*oldst));
  443. close_info();
  444. strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
  445. win_draw(&win);
  446. win_set_cursor(&win, CURSOR_WATCH);
  447. snprintf(kstr, sizeof(kstr), "%s%s%s%s",
  448. mask & ControlMask ? "C-" : "",
  449. mask & Mod1Mask ? "M-" : "",
  450. mask & ShiftMask ? "S-" : "", key);
  451. if ((pid = fork()) == 0) {
  452. close(pfd[1]);
  453. dup2(pfd[0], 0);
  454. execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
  455. error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
  456. }
  457. close(pfd[0]);
  458. if (pid < 0) {
  459. error(0, errno, "fork");
  460. fclose(pfs);
  461. goto end;
  462. }
  463. for (f = i = 0; f < fcnt; i++) {
  464. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  465. stat(files[i].path, &oldst[f]);
  466. fprintf(pfs, "%s\n", files[i].name);
  467. f++;
  468. }
  469. }
  470. fclose(pfs);
  471. while (waitpid(pid, NULL, 0) == -1 && errno == EINTR);
  472. for (f = i = 0; f < fcnt; i++) {
  473. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  474. if (stat(files[i].path, &st) != 0 ||
  475. memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
  476. {
  477. if (tns.thumbs != NULL) {
  478. tns_unload(&tns, i);
  479. tns.loadnext = MIN(tns.loadnext, i);
  480. }
  481. changed = true;
  482. }
  483. f++;
  484. }
  485. }
  486. /* drop user input events that occurred while running the key handler */
  487. while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
  488. end:
  489. if (mode == MODE_IMAGE) {
  490. if (changed) {
  491. img_close(&img, true);
  492. load_image(fileidx);
  493. } else {
  494. open_info();
  495. }
  496. }
  497. free(oldst);
  498. reset_cursor();
  499. redraw();
  500. }
  501. #define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
  502. void on_keypress(XKeyEvent *kev)
  503. {
  504. int i;
  505. unsigned int sh = 0;
  506. KeySym ksym, shksym;
  507. char dummy, key;
  508. bool dirty = false;
  509. XLookupString(kev, &key, 1, &ksym, NULL);
  510. if (kev->state & ShiftMask) {
  511. kev->state &= ~ShiftMask;
  512. XLookupString(kev, &dummy, 1, &shksym, NULL);
  513. kev->state |= ShiftMask;
  514. if (ksym != shksym)
  515. sh = ShiftMask;
  516. }
  517. if (IsModifierKey(ksym))
  518. return;
  519. if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
  520. extprefix = False;
  521. } else if (extprefix) {
  522. run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
  523. extprefix = False;
  524. } else if (key >= '0' && key <= '9') {
  525. /* number prefix for commands */
  526. prefix = prefix * 10 + (int) (key - '0');
  527. return;
  528. } else for (i = 0; i < ARRLEN(keys); i++) {
  529. if (keys[i].ksym == ksym &&
  530. MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
  531. keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
  532. (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
  533. {
  534. if (cmds[keys[i].cmd].func(keys[i].arg))
  535. dirty = true;
  536. }
  537. }
  538. if (dirty)
  539. redraw();
  540. prefix = 0;
  541. }
  542. void on_buttonpress(XButtonEvent *bev)
  543. {
  544. int i, sel;
  545. bool dirty = false;
  546. static Time firstclick;
  547. if (mode == MODE_IMAGE) {
  548. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  549. reset_cursor();
  550. for (i = 0; i < ARRLEN(buttons); i++) {
  551. if (buttons[i].button == bev->button &&
  552. MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
  553. buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
  554. (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
  555. {
  556. if (cmds[buttons[i].cmd].func(buttons[i].arg))
  557. dirty = true;
  558. }
  559. }
  560. if (dirty)
  561. redraw();
  562. } else {
  563. /* thumbnail mode (hard-coded) */
  564. switch (bev->button) {
  565. case Button1:
  566. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  567. if (sel != fileidx) {
  568. tns_highlight(&tns, fileidx, false);
  569. tns_highlight(&tns, sel, true);
  570. fileidx = sel;
  571. firstclick = bev->time;
  572. redraw();
  573. } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
  574. mode = MODE_IMAGE;
  575. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  576. load_image(fileidx);
  577. redraw();
  578. } else {
  579. firstclick = bev->time;
  580. }
  581. }
  582. break;
  583. case Button3:
  584. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  585. bool on = !(files[sel].flags & FF_MARK);
  586. XEvent e;
  587. for (;;) {
  588. if (sel >= 0 && mark_image(sel, on))
  589. redraw();
  590. XMaskEvent(win.env.dpy,
  591. ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
  592. if (e.type == ButtonPress || e.type == ButtonRelease)
  593. break;
  594. while (XCheckTypedEvent(win.env.dpy, MotionNotify, &e));
  595. sel = tns_translate(&tns, e.xbutton.x, e.xbutton.y);
  596. }
  597. }
  598. break;
  599. case Button4:
  600. case Button5:
  601. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  602. (bev->state & ControlMask) != 0))
  603. redraw();
  604. break;
  605. }
  606. }
  607. prefix = 0;
  608. }
  609. const struct timespec ten_ms = {0, 10000000};
  610. void run(void)
  611. {
  612. int xfd;
  613. fd_set fds;
  614. struct timeval timeout;
  615. bool discard, init_thumb, load_thumb, to_set;
  616. XEvent ev, nextev;
  617. while (true) {
  618. to_set = check_timeouts(&timeout);
  619. init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
  620. load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
  621. if ((init_thumb || load_thumb || to_set || info.fd != -1 ||
  622. arl.fd != -1) && XPending(win.env.dpy) == 0)
  623. {
  624. if (load_thumb) {
  625. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  626. if (!tns_load(&tns, tns.loadnext, false, false)) {
  627. remove_file(tns.loadnext, false);
  628. tns.dirty = true;
  629. }
  630. if (tns.loadnext >= tns.end)
  631. redraw();
  632. } else if (init_thumb) {
  633. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  634. if (!tns_load(&tns, tns.initnext, false, true))
  635. remove_file(tns.initnext, false);
  636. } else {
  637. xfd = ConnectionNumber(win.env.dpy);
  638. FD_ZERO(&fds);
  639. FD_SET(xfd, &fds);
  640. if (info.fd != -1) {
  641. FD_SET(info.fd, &fds);
  642. xfd = MAX(xfd, info.fd);
  643. }
  644. if (arl.fd != -1) {
  645. FD_SET(arl.fd, &fds);
  646. xfd = MAX(xfd, arl.fd);
  647. }
  648. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  649. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  650. read_info();
  651. if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
  652. if (arl_handle(&arl)) {
  653. /* when too fast, imlib2 can't load the image */
  654. nanosleep(&ten_ms, NULL);
  655. img_close(&img, true);
  656. load_image(fileidx);
  657. redraw();
  658. }
  659. }
  660. }
  661. continue;
  662. }
  663. do {
  664. XNextEvent(win.env.dpy, &ev);
  665. discard = false;
  666. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  667. XPeekEvent(win.env.dpy, &nextev);
  668. switch (ev.type) {
  669. case ConfigureNotify:
  670. case MotionNotify:
  671. discard = ev.type == nextev.type;
  672. break;
  673. case KeyPress:
  674. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  675. && ev.xkey.keycode == nextev.xkey.keycode;
  676. break;
  677. }
  678. }
  679. } while (discard);
  680. switch (ev.type) {
  681. /* handle events */
  682. case ButtonPress:
  683. on_buttonpress(&ev.xbutton);
  684. break;
  685. case ClientMessage:
  686. if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
  687. cmds[g_quit].func(0);
  688. break;
  689. case ConfigureNotify:
  690. if (win_configure(&win, &ev.xconfigure)) {
  691. if (mode == MODE_IMAGE) {
  692. img.dirty = true;
  693. img.checkpan = true;
  694. } else {
  695. tns.dirty = true;
  696. }
  697. if (!resized) {
  698. redraw();
  699. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  700. resized = true;
  701. } else {
  702. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  703. }
  704. }
  705. break;
  706. case KeyPress:
  707. on_keypress(&ev.xkey);
  708. break;
  709. case MotionNotify:
  710. if (mode == MODE_IMAGE) {
  711. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  712. reset_cursor();
  713. }
  714. break;
  715. }
  716. }
  717. }
  718. int fncmp(const void *a, const void *b)
  719. {
  720. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  721. }
  722. void sigchld(int sig)
  723. {
  724. while (waitpid(-1, NULL, WNOHANG) > 0);
  725. }
  726. void setup_signal(int sig, void (*handler)(int sig))
  727. {
  728. struct sigaction sa;
  729. sa.sa_handler = handler;
  730. sigemptyset(&sa.sa_mask);
  731. sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
  732. if (sigaction(sig, &sa, 0) == -1)
  733. error(EXIT_FAILURE, errno, "signal %d", sig);
  734. }
  735. int main(int argc, char **argv)
  736. {
  737. int i, start;
  738. size_t n;
  739. ssize_t len;
  740. char *filename;
  741. const char *homedir, *dsuffix = "";
  742. struct stat fstats;
  743. r_dir_t dir;
  744. setup_signal(SIGCHLD, sigchld);
  745. setup_signal(SIGPIPE, SIG_IGN);
  746. setlocale(LC_COLLATE, "");
  747. parse_options(argc, argv);
  748. if (options->clean_cache) {
  749. tns_init(&tns, NULL, NULL, NULL, NULL);
  750. tns_clean_cache(&tns);
  751. exit(EXIT_SUCCESS);
  752. }
  753. if (options->filecnt == 0 && !options->from_stdin) {
  754. print_usage();
  755. exit(EXIT_FAILURE);
  756. }
  757. if (options->recursive || options->from_stdin)
  758. filecnt = 1024;
  759. else
  760. filecnt = options->filecnt;
  761. files = emalloc(filecnt * sizeof(*files));
  762. memset(files, 0, filecnt * sizeof(*files));
  763. fileidx = 0;
  764. if (options->from_stdin) {
  765. n = 0;
  766. filename = NULL;
  767. while ((len = getline(&filename, &n, stdin)) > 0) {
  768. if (filename[len-1] == '\n')
  769. filename[len-1] = '\0';
  770. check_add_file(filename, true);
  771. }
  772. free(filename);
  773. }
  774. for (i = 0; i < options->filecnt; i++) {
  775. filename = options->filenames[i];
  776. if (stat(filename, &fstats) < 0) {
  777. error(0, errno, "%s", filename);
  778. continue;
  779. }
  780. if (!S_ISDIR(fstats.st_mode)) {
  781. check_add_file(filename, true);
  782. } else {
  783. if (r_opendir(&dir, filename, options->recursive) < 0) {
  784. error(0, errno, "%s", filename);
  785. continue;
  786. }
  787. start = fileidx;
  788. while ((filename = r_readdir(&dir, true)) != NULL) {
  789. check_add_file(filename, false);
  790. free((void*) filename);
  791. }
  792. r_closedir(&dir);
  793. if (fileidx - start > 1)
  794. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  795. }
  796. }
  797. if (fileidx == 0)
  798. error(EXIT_FAILURE, 0, "No valid image file given, aborting");
  799. filecnt = fileidx;
  800. fileidx = options->startnum < filecnt ? options->startnum : 0;
  801. for (i = 0; i < ARRLEN(buttons); i++) {
  802. if (buttons[i].cmd == i_cursor_navigate) {
  803. imgcursor[0] = CURSOR_LEFT;
  804. imgcursor[2] = CURSOR_RIGHT;
  805. break;
  806. }
  807. }
  808. win_init(&win);
  809. img_init(&img, &win);
  810. arl_init(&arl);
  811. if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
  812. homedir = getenv("HOME");
  813. dsuffix = "/.config";
  814. }
  815. if (homedir != NULL) {
  816. extcmd_t *cmd[] = { &info.f, &keyhandler.f };
  817. const char *name[] = { "image-info", "key-handler" };
  818. for (i = 0; i < ARRLEN(cmd); i++) {
  819. n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
  820. cmd[i]->cmd = (char*) emalloc(n);
  821. snprintf(cmd[i]->cmd, n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
  822. if (access(cmd[i]->cmd, X_OK) != 0)
  823. cmd[i]->err = errno;
  824. }
  825. } else {
  826. error(0, 0, "Exec directory not found");
  827. }
  828. info.fd = -1;
  829. if (options->thumb_mode) {
  830. mode = MODE_THUMB;
  831. tns_init(&tns, files, &filecnt, &fileidx, &win);
  832. while (!tns_load(&tns, fileidx, false, false))
  833. remove_file(fileidx, false);
  834. } else {
  835. mode = MODE_IMAGE;
  836. tns.thumbs = NULL;
  837. load_image(fileidx);
  838. }
  839. win_open(&win);
  840. win_set_cursor(&win, CURSOR_WATCH);
  841. atexit(cleanup);
  842. set_timeout(redraw, 25, false);
  843. run();
  844. return 0;
  845. }