A Simple X Image Viewer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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