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

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