A Simple X Image Viewer
 
 
 
 
 
 

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