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.
 
 
 
 
 
 

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