A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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