A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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