A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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