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.
 
 
 
 
 
 

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