A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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