A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

923 linhas
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. win_bar_t *l = &win.bar.l, *r = &win.bar.r;
  300. /* update window title */
  301. if (mode == MODE_THUMB) {
  302. win_set_title(&win, "sxiv");
  303. } else {
  304. snprintf(title, sizeof(title), "sxiv - %s", files[fileidx].name);
  305. win_set_title(&win, title);
  306. }
  307. /* update bar contents */
  308. if (win.bar.h == 0)
  309. return;
  310. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  311. mark = files[fileidx].flags & FF_MARK ? "* " : "";
  312. l->p = l->buf;
  313. r->p = r->buf;
  314. if (mode == MODE_THUMB) {
  315. if (tns.loadnext < tns.end)
  316. bar_put(l, "Loading... %0*d", fw, tns.loadnext + 1);
  317. else if (tns.initnext < filecnt)
  318. bar_put(l, "Caching... %0*d", fw, tns.initnext + 1);
  319. else
  320. strncpy(l->buf, files[fileidx].name, l->size);
  321. bar_put(r, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
  322. } else {
  323. bar_put(r, "%s", mark);
  324. if (img.ss.on) {
  325. if (img.ss.delay % 10 != 0)
  326. bar_put(r, "%2.1fs | ", (float)img.ss.delay / 10);
  327. else
  328. bar_put(r, "%ds | ", img.ss.delay / 10);
  329. }
  330. if (img.gamma != 0)
  331. bar_put(r, "G%+d | ", img.gamma);
  332. bar_put(r, "%3d%% | ", (int) (img.zoom * 100.0));
  333. if (img.multi.cnt > 0) {
  334. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  335. bar_put(r, "%0*d/%d | ", fn, img.multi.sel + 1, img.multi.cnt);
  336. }
  337. bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
  338. if (info.f.err)
  339. strncpy(l->buf, files[fileidx].name, l->size);
  340. }
  341. }
  342. int ptr_third_x(void)
  343. {
  344. int x, y;
  345. win_cursor_pos(&win, &x, &y);
  346. return MAX(0, MIN(2, (x / (win.w * 0.33))));
  347. }
  348. void redraw(void)
  349. {
  350. int t;
  351. if (mode == MODE_IMAGE) {
  352. img_render(&img);
  353. if (img.ss.on) {
  354. t = img.ss.delay * 100;
  355. if (img.multi.cnt > 0 && img.multi.animate)
  356. t = MAX(t, img.multi.length);
  357. set_timeout(slideshow, t, false);
  358. }
  359. } else {
  360. tns_render(&tns);
  361. }
  362. update_info();
  363. win_draw(&win);
  364. reset_timeout(redraw);
  365. reset_cursor();
  366. }
  367. void reset_cursor(void)
  368. {
  369. int c, i;
  370. cursor_t cursor = CURSOR_NONE;
  371. if (mode == MODE_IMAGE) {
  372. for (i = 0; i < ARRLEN(timeouts); i++) {
  373. if (timeouts[i].handler == reset_cursor) {
  374. if (timeouts[i].active) {
  375. c = ptr_third_x();
  376. c = MAX(fileidx > 0 ? 0 : 1, c);
  377. c = MIN(fileidx + 1 < filecnt ? 2 : 1, c);
  378. cursor = imgcursor[c];
  379. }
  380. break;
  381. }
  382. }
  383. } else {
  384. if (tns.loadnext < tns.end || tns.initnext < filecnt)
  385. cursor = CURSOR_WATCH;
  386. else
  387. cursor = CURSOR_ARROW;
  388. }
  389. win_set_cursor(&win, cursor);
  390. }
  391. void animate(void)
  392. {
  393. if (img_frame_animate(&img)) {
  394. redraw();
  395. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  396. }
  397. }
  398. void slideshow(void)
  399. {
  400. load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
  401. redraw();
  402. }
  403. void clear_resize(void)
  404. {
  405. resized = false;
  406. }
  407. Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
  408. {
  409. return ev->type == ButtonPress || ev->type == KeyPress;
  410. }
  411. void run_key_handler(const char *key, unsigned int mask)
  412. {
  413. pid_t pid;
  414. FILE *pfs;
  415. bool marked = mode == MODE_THUMB && markcnt > 0;
  416. bool changed = false;
  417. int f, i, pfd[2], status;
  418. int fcnt = marked ? markcnt : 1;
  419. char kstr[32];
  420. struct stat *oldst, st;
  421. XEvent dump;
  422. if (keyhandler.f.err != 0) {
  423. if (!keyhandler.warned) {
  424. error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
  425. keyhandler.warned = true;
  426. }
  427. return;
  428. }
  429. if (key == NULL)
  430. return;
  431. if (pipe(pfd) < 0) {
  432. error(0, errno, "pipe");
  433. return;
  434. }
  435. if ((pfs = fdopen(pfd[1], "w")) == NULL) {
  436. error(0, errno, "open pipe");
  437. close(pfd[0]), close(pfd[1]);
  438. return;
  439. }
  440. oldst = emalloc(fcnt * sizeof(*oldst));
  441. strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
  442. win_draw(&win);
  443. win_set_cursor(&win, CURSOR_WATCH);
  444. snprintf(kstr, sizeof(kstr), "%s%s%s%s",
  445. mask & ControlMask ? "C-" : "",
  446. mask & Mod1Mask ? "M-" : "",
  447. mask & ShiftMask ? "S-" : "", key);
  448. if ((pid = fork()) == 0) {
  449. close(pfd[1]);
  450. dup2(pfd[0], 0);
  451. execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
  452. error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
  453. }
  454. close(pfd[0]);
  455. if (pid < 0) {
  456. error(0, errno, "fork");
  457. fclose(pfs);
  458. goto end;
  459. }
  460. for (f = i = 0; f < fcnt; i++) {
  461. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  462. stat(files[i].path, &oldst[f]);
  463. fprintf(pfs, "%s\n", files[i].name);
  464. f++;
  465. }
  466. }
  467. fclose(pfs);
  468. waitpid(pid, &status, 0);
  469. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  470. error(0, 0, "%s: Exited abnormally", keyhandler.f.cmd);
  471. for (f = i = 0; f < fcnt; i++) {
  472. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  473. if (stat(files[i].path, &st) != 0 ||
  474. memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
  475. {
  476. if (tns.thumbs != NULL) {
  477. tns_unload(&tns, i);
  478. tns.loadnext = MIN(tns.loadnext, i);
  479. }
  480. changed = true;
  481. }
  482. f++;
  483. }
  484. }
  485. /* drop user input events that occurred while running the key handler */
  486. while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
  487. end:
  488. if (mode == MODE_IMAGE) {
  489. if (changed) {
  490. img_close(&img, true);
  491. load_image(fileidx);
  492. } else if (info.f.err == 0) {
  493. info.open = false;
  494. open_info();
  495. }
  496. }
  497. free(oldst);
  498. reset_cursor();
  499. redraw();
  500. }
  501. #define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
  502. void on_keypress(XKeyEvent *kev)
  503. {
  504. int i;
  505. unsigned int sh;
  506. KeySym ksym, shksym;
  507. char key;
  508. bool dirty = false;
  509. if (kev->state & ShiftMask) {
  510. kev->state &= ~ShiftMask;
  511. XLookupString(kev, &key, 1, &shksym, NULL);
  512. kev->state |= ShiftMask;
  513. }
  514. XLookupString(kev, &key, 1, &ksym, NULL);
  515. sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
  516. if (IsModifierKey(ksym))
  517. return;
  518. if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
  519. extprefix = False;
  520. } else if (extprefix) {
  521. run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
  522. extprefix = False;
  523. } else if (key >= '0' && key <= '9') {
  524. /* number prefix for commands */
  525. prefix = prefix * 10 + (int) (key - '0');
  526. return;
  527. } else for (i = 0; i < ARRLEN(keys); i++) {
  528. if (keys[i].ksym == ksym &&
  529. MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
  530. keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
  531. (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
  532. {
  533. if (cmds[keys[i].cmd].func(keys[i].arg))
  534. dirty = true;
  535. }
  536. }
  537. if (dirty)
  538. redraw();
  539. prefix = 0;
  540. }
  541. void on_buttonpress(XButtonEvent *bev)
  542. {
  543. int i, sel;
  544. bool dirty = false;
  545. static Time firstclick;
  546. if (mode == MODE_IMAGE) {
  547. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  548. reset_cursor();
  549. for (i = 0; i < ARRLEN(buttons); i++) {
  550. if (buttons[i].button == bev->button &&
  551. MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
  552. buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
  553. (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
  554. {
  555. if (cmds[buttons[i].cmd].func(buttons[i].arg))
  556. dirty = true;
  557. }
  558. }
  559. if (dirty)
  560. redraw();
  561. } else {
  562. /* thumbnail mode (hard-coded) */
  563. switch (bev->button) {
  564. case Button1:
  565. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  566. if (sel != fileidx) {
  567. tns_highlight(&tns, fileidx, false);
  568. tns_highlight(&tns, sel, true);
  569. fileidx = sel;
  570. firstclick = bev->time;
  571. redraw();
  572. } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
  573. mode = MODE_IMAGE;
  574. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  575. load_image(fileidx);
  576. redraw();
  577. } else {
  578. firstclick = bev->time;
  579. }
  580. }
  581. break;
  582. case Button3:
  583. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  584. files[sel].flags ^= FF_MARK;
  585. markcnt += files[sel].flags & FF_MARK ? 1 : -1;
  586. tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
  587. redraw();
  588. }
  589. break;
  590. case Button4:
  591. case Button5:
  592. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  593. (bev->state & ControlMask) != 0))
  594. redraw();
  595. break;
  596. }
  597. }
  598. prefix = 0;
  599. }
  600. const struct timespec ten_ms = {0, 10000000};
  601. void run(void)
  602. {
  603. int xfd;
  604. fd_set fds;
  605. struct timeval timeout;
  606. bool discard, init_thumb, load_thumb, to_set;
  607. XEvent ev, nextev;
  608. while (true) {
  609. to_set = check_timeouts(&timeout);
  610. init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
  611. load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
  612. if ((init_thumb || load_thumb || to_set || info.fd != -1 ||
  613. arl.fd != -1) && XPending(win.env.dpy) == 0)
  614. {
  615. if (load_thumb) {
  616. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  617. if (!tns_load(&tns, tns.loadnext, false, false)) {
  618. remove_file(tns.loadnext, false);
  619. tns.dirty = true;
  620. }
  621. if (tns.loadnext >= tns.end)
  622. redraw();
  623. } else if (init_thumb) {
  624. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  625. if (!tns_load(&tns, tns.initnext, false, true))
  626. remove_file(tns.initnext, false);
  627. } else {
  628. xfd = ConnectionNumber(win.env.dpy);
  629. FD_ZERO(&fds);
  630. FD_SET(xfd, &fds);
  631. if (info.fd != -1) {
  632. FD_SET(info.fd, &fds);
  633. xfd = MAX(xfd, info.fd);
  634. }
  635. if (arl.fd != -1) {
  636. FD_SET(arl.fd, &fds);
  637. xfd = MAX(xfd, arl.fd);
  638. }
  639. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  640. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  641. read_info();
  642. if (arl.fd != -1 && FD_ISSET(arl.fd, &fds)) {
  643. if (arl_handle(&arl)) {
  644. /* when too fast, imlib2 can't load the image */
  645. nanosleep(&ten_ms, NULL);
  646. load_image(fileidx);
  647. redraw();
  648. }
  649. }
  650. }
  651. continue;
  652. }
  653. do {
  654. XNextEvent(win.env.dpy, &ev);
  655. discard = false;
  656. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  657. XPeekEvent(win.env.dpy, &nextev);
  658. switch (ev.type) {
  659. case ConfigureNotify:
  660. case MotionNotify:
  661. discard = ev.type == nextev.type;
  662. break;
  663. case KeyPress:
  664. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  665. && ev.xkey.keycode == nextev.xkey.keycode;
  666. break;
  667. }
  668. }
  669. } while (discard);
  670. switch (ev.type) {
  671. /* handle events */
  672. case ButtonPress:
  673. on_buttonpress(&ev.xbutton);
  674. break;
  675. case ClientMessage:
  676. if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
  677. cmds[g_quit].func(0);
  678. break;
  679. case ConfigureNotify:
  680. if (win_configure(&win, &ev.xconfigure)) {
  681. if (mode == MODE_IMAGE) {
  682. img.dirty = true;
  683. img.checkpan = true;
  684. } else {
  685. tns.dirty = true;
  686. }
  687. if (!resized || win.fullscreen) {
  688. redraw();
  689. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  690. resized = true;
  691. } else {
  692. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  693. }
  694. }
  695. break;
  696. case KeyPress:
  697. on_keypress(&ev.xkey);
  698. break;
  699. case MotionNotify:
  700. if (mode == MODE_IMAGE) {
  701. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  702. reset_cursor();
  703. }
  704. break;
  705. }
  706. }
  707. }
  708. int fncmp(const void *a, const void *b)
  709. {
  710. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  711. }
  712. int main(int argc, char **argv)
  713. {
  714. int i, start;
  715. size_t n;
  716. ssize_t len;
  717. char *filename;
  718. const char *homedir, *dsuffix = "";
  719. struct stat fstats;
  720. r_dir_t dir;
  721. signal(SIGPIPE, SIG_IGN);
  722. setlocale(LC_COLLATE, "");
  723. parse_options(argc, argv);
  724. if (options->clean_cache) {
  725. tns_init(&tns, NULL, NULL, NULL, NULL);
  726. tns_clean_cache(&tns);
  727. exit(EXIT_SUCCESS);
  728. }
  729. if (options->filecnt == 0 && !options->from_stdin) {
  730. print_usage();
  731. exit(EXIT_FAILURE);
  732. }
  733. if (options->recursive || options->from_stdin)
  734. filecnt = 1024;
  735. else
  736. filecnt = options->filecnt;
  737. files = emalloc(filecnt * sizeof(*files));
  738. memset(files, 0, filecnt * sizeof(*files));
  739. fileidx = 0;
  740. if (options->from_stdin) {
  741. n = 0;
  742. filename = NULL;
  743. while ((len = getline(&filename, &n, stdin)) > 0) {
  744. if (filename[len-1] == '\n')
  745. filename[len-1] = '\0';
  746. check_add_file(filename, true);
  747. }
  748. free(filename);
  749. }
  750. for (i = 0; i < options->filecnt; i++) {
  751. filename = options->filenames[i];
  752. if (stat(filename, &fstats) < 0) {
  753. error(0, errno, "%s", filename);
  754. continue;
  755. }
  756. if (!S_ISDIR(fstats.st_mode)) {
  757. check_add_file(filename, true);
  758. } else {
  759. if (r_opendir(&dir, filename, options->recursive) < 0) {
  760. error(0, errno, "%s", filename);
  761. continue;
  762. }
  763. start = fileidx;
  764. while ((filename = r_readdir(&dir)) != NULL) {
  765. check_add_file(filename, false);
  766. free((void*) filename);
  767. }
  768. r_closedir(&dir);
  769. if (fileidx - start > 1)
  770. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  771. }
  772. }
  773. if (fileidx == 0)
  774. error(EXIT_FAILURE, 0, "No valid image file given, aborting");
  775. filecnt = fileidx;
  776. fileidx = options->startnum < filecnt ? options->startnum : 0;
  777. for (i = 0; i < ARRLEN(buttons); i++) {
  778. if (buttons[i].cmd == i_cursor_navigate) {
  779. imgcursor[0] = CURSOR_LEFT;
  780. imgcursor[2] = CURSOR_RIGHT;
  781. break;
  782. }
  783. }
  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. }