A Simple X Image Viewer
 
 
 
 
 
 

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