A Simple X Image Viewer
 
 
 
 
 
 

835 lines
18 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. #define _POSIX_C_SOURCE 200112L
  19. #define _MAPPINGS_CONFIG
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <signal.h>
  27. #include <sys/select.h>
  28. #include <sys/stat.h>
  29. #include <sys/time.h>
  30. #include <sys/wait.h>
  31. #include <X11/keysym.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. #include "config.h"
  40. enum {
  41. FILENAME_CNT = 1024,
  42. TITLE_LEN = 256
  43. };
  44. #define EXEC_REL_DIR ".sxiv/exec"
  45. enum {
  46. EXEC_INFO,
  47. EXEC_KEY
  48. };
  49. typedef struct {
  50. const char *name;
  51. char *cmd;
  52. } exec_t;
  53. typedef struct {
  54. struct timeval when;
  55. bool active;
  56. timeout_f handler;
  57. } timeout_t;
  58. /* timeout handler functions: */
  59. void redraw(void);
  60. void reset_cursor(void);
  61. void animate(void);
  62. void slideshow(void);
  63. void clear_resize(void);
  64. appmode_t mode;
  65. img_t img;
  66. tns_t tns;
  67. win_t win;
  68. fileinfo_t *files;
  69. int filecnt, fileidx;
  70. int alternate;
  71. int prefix;
  72. bool resized = false;
  73. exec_t exec[] = {
  74. { "image-info", NULL },
  75. { "key-handler", NULL }
  76. };
  77. struct {
  78. char *cmd;
  79. int fd;
  80. unsigned int i, lastsep;
  81. bool open;
  82. } info;
  83. timeout_t timeouts[] = {
  84. { { 0, 0 }, false, redraw },
  85. { { 0, 0 }, false, reset_cursor },
  86. { { 0, 0 }, false, animate },
  87. { { 0, 0 }, false, slideshow },
  88. { { 0, 0 }, false, clear_resize },
  89. };
  90. void cleanup(void)
  91. {
  92. static bool in = false;
  93. if (!in) {
  94. in = true;
  95. img_close(&img, false);
  96. tns_free(&tns);
  97. win_close(&win);
  98. }
  99. }
  100. void check_add_file(char *filename)
  101. {
  102. const char *bn;
  103. if (filename == NULL || *filename == '\0')
  104. return;
  105. if (access(filename, R_OK) < 0) {
  106. warn("could not open file: %s", filename);
  107. return;
  108. }
  109. if (fileidx == filecnt) {
  110. filecnt *= 2;
  111. files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t));
  112. }
  113. if (*filename != '/') {
  114. files[fileidx].path = absolute_path(filename);
  115. if (files[fileidx].path == NULL) {
  116. warn("could not get absolute path of file: %s\n", filename);
  117. return;
  118. }
  119. }
  120. files[fileidx].loaded = false;
  121. files[fileidx].name = s_strdup(filename);
  122. if (*filename == '/')
  123. files[fileidx].path = files[fileidx].name;
  124. if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
  125. files[fileidx].base = ++bn;
  126. else
  127. files[fileidx].base = files[fileidx].name;
  128. fileidx++;
  129. }
  130. void remove_file(int n, bool manual)
  131. {
  132. if (n < 0 || n >= filecnt)
  133. return;
  134. if (filecnt == 1) {
  135. if (!manual)
  136. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  137. cleanup();
  138. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  139. }
  140. if (files[n].path != files[n].name)
  141. free((void*) files[n].path);
  142. free((void*) files[n].name);
  143. if (n + 1 < filecnt)
  144. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
  145. if (n + 1 < tns.cnt) {
  146. memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
  147. sizeof(thumb_t));
  148. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  149. }
  150. filecnt--;
  151. if (n < tns.cnt)
  152. tns.cnt--;
  153. if (n < alternate)
  154. alternate--;
  155. }
  156. void set_timeout(timeout_f handler, int time, bool overwrite)
  157. {
  158. int i;
  159. for (i = 0; i < ARRLEN(timeouts); i++) {
  160. if (timeouts[i].handler == handler) {
  161. if (!timeouts[i].active || overwrite) {
  162. gettimeofday(&timeouts[i].when, 0);
  163. TV_ADD_MSEC(&timeouts[i].when, time);
  164. timeouts[i].active = true;
  165. }
  166. return;
  167. }
  168. }
  169. }
  170. void reset_timeout(timeout_f handler)
  171. {
  172. int i;
  173. for (i = 0; i < ARRLEN(timeouts); i++) {
  174. if (timeouts[i].handler == handler) {
  175. timeouts[i].active = false;
  176. return;
  177. }
  178. }
  179. }
  180. bool check_timeouts(struct timeval *t)
  181. {
  182. int i = 0, tdiff, tmin = -1;
  183. struct timeval now;
  184. while (i < ARRLEN(timeouts)) {
  185. if (timeouts[i].active) {
  186. gettimeofday(&now, 0);
  187. tdiff = TV_DIFF(&timeouts[i].when, &now);
  188. if (tdiff <= 0) {
  189. timeouts[i].active = false;
  190. if (timeouts[i].handler != NULL)
  191. timeouts[i].handler();
  192. i = tmin = -1;
  193. } else if (tmin < 0 || tdiff < tmin) {
  194. tmin = tdiff;
  195. }
  196. }
  197. i++;
  198. }
  199. if (tmin > 0 && t != NULL)
  200. TV_SET_MSEC(t, tmin);
  201. return tmin > 0;
  202. }
  203. void open_info(void)
  204. {
  205. static pid_t pid;
  206. int pfd[2];
  207. if (info.cmd == NULL || info.open || win.bar.h == 0)
  208. return;
  209. if (info.fd != -1) {
  210. close(info.fd);
  211. kill(pid, SIGTERM);
  212. info.fd = -1;
  213. }
  214. win.bar.l[0] = '\0';
  215. if (pipe(pfd) < 0)
  216. return;
  217. pid = fork();
  218. if (pid > 0) {
  219. close(pfd[1]);
  220. fcntl(pfd[0], F_SETFL, O_NONBLOCK);
  221. info.fd = pfd[0];
  222. info.i = info.lastsep = 0;
  223. info.open = true;
  224. } else if (pid == 0) {
  225. close(pfd[0]);
  226. dup2(pfd[1], 1);
  227. execl(info.cmd, info.cmd, files[fileidx].name, NULL);
  228. warn("could not exec: %s", info.cmd);
  229. exit(EXIT_FAILURE);
  230. }
  231. }
  232. void read_info(void)
  233. {
  234. ssize_t i, n;
  235. char buf[BAR_L_LEN];
  236. while (true) {
  237. n = read(info.fd, buf, sizeof(buf));
  238. if (n < 0 && errno == EAGAIN)
  239. return;
  240. else if (n == 0)
  241. goto end;
  242. for (i = 0; i < n; i++) {
  243. if (buf[i] == '\n') {
  244. if (info.lastsep == 0) {
  245. win.bar.l[info.i++] = ' ';
  246. info.lastsep = 1;
  247. }
  248. } else {
  249. win.bar.l[info.i++] = buf[i];
  250. info.lastsep = 0;
  251. }
  252. if (info.i + 1 == sizeof(win.bar.l))
  253. goto end;
  254. }
  255. }
  256. end:
  257. info.i -= info.lastsep;
  258. win.bar.l[info.i] = '\0';
  259. win_update_bar(&win);
  260. close(info.fd);
  261. info.fd = -1;
  262. while (waitpid(-1, NULL, WNOHANG) > 0);
  263. }
  264. void load_image(int new)
  265. {
  266. if (new < 0 || new >= filecnt)
  267. return;
  268. win_set_cursor(&win, CURSOR_WATCH);
  269. reset_timeout(slideshow);
  270. if (new != fileidx)
  271. alternate = fileidx;
  272. img_close(&img, false);
  273. while (!img_load(&img, &files[new])) {
  274. remove_file(new, false);
  275. if (new >= filecnt)
  276. new = filecnt - 1;
  277. else if (new > 0 && new < fileidx)
  278. new--;
  279. }
  280. files[new].loaded = true;
  281. fileidx = new;
  282. info.open = false;
  283. open_info();
  284. if (img.multi.cnt > 0 && img.multi.animate)
  285. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  286. else
  287. reset_timeout(animate);
  288. }
  289. void update_info(void)
  290. {
  291. int sel;
  292. unsigned int i, fn, fw, n;
  293. unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
  294. char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
  295. const char * mark;
  296. bool ow_info;
  297. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  298. sel = mode == MODE_IMAGE ? fileidx : tns.sel;
  299. /* update window title */
  300. if (mode == MODE_THUMB) {
  301. win_set_title(&win, "sxiv");
  302. } else {
  303. snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
  304. win_set_title(&win, title);
  305. }
  306. /* update bar contents */
  307. if (win.bar.h == 0)
  308. return;
  309. mark = files[sel].marked ? "* " : "";
  310. if (mode == MODE_THUMB) {
  311. if (tns.cnt == filecnt) {
  312. n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, sel + 1, filecnt);
  313. ow_info = true;
  314. } else {
  315. snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.cnt, filecnt);
  316. rt[0] = '\0';
  317. ow_info = false;
  318. }
  319. } else {
  320. n = snprintf(rt, rlen, "%s", mark);
  321. if (img.ss.on)
  322. n += snprintf(rt + n, rlen - n, "%ds | ", img.ss.delay);
  323. if (img.gamma != 0)
  324. n += snprintf(rt + n, rlen - n, "G%+d | ", img.gamma);
  325. n += snprintf(rt + n, rlen - n, "%3d%% | ", (int) (img.zoom * 100.0));
  326. if (img.multi.cnt > 0) {
  327. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  328. n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
  329. fn, img.multi.sel + 1, img.multi.cnt);
  330. }
  331. n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
  332. ow_info = info.cmd == NULL;
  333. }
  334. if (ow_info) {
  335. fn = strlen(files[sel].name);
  336. if (fn < llen &&
  337. win_textwidth(files[sel].name, fn, true) +
  338. win_textwidth(rt, n, true) < win.w)
  339. {
  340. strncpy(lt, files[sel].name, llen);
  341. } else {
  342. strncpy(lt, files[sel].base, llen);
  343. }
  344. }
  345. }
  346. void redraw(void)
  347. {
  348. int t;
  349. if (mode == MODE_IMAGE) {
  350. img_render(&img);
  351. if (img.ss.on) {
  352. t = img.ss.delay * 1000;
  353. if (img.multi.cnt > 0 && img.multi.animate)
  354. t = MAX(t, img.multi.length);
  355. set_timeout(slideshow, t, false);
  356. }
  357. } else {
  358. tns_render(&tns);
  359. }
  360. update_info();
  361. win_draw(&win);
  362. reset_timeout(redraw);
  363. reset_cursor();
  364. }
  365. void reset_cursor(void)
  366. {
  367. int i;
  368. cursor_t cursor = CURSOR_NONE;
  369. if (mode == MODE_IMAGE) {
  370. for (i = 0; i < ARRLEN(timeouts); i++) {
  371. if (timeouts[i].handler == reset_cursor) {
  372. if (timeouts[i].active)
  373. cursor = CURSOR_ARROW;
  374. break;
  375. }
  376. }
  377. } else {
  378. if (tns.cnt != filecnt)
  379. cursor = CURSOR_WATCH;
  380. else
  381. cursor = CURSOR_ARROW;
  382. }
  383. win_set_cursor(&win, cursor);
  384. }
  385. void animate(void)
  386. {
  387. if (img_frame_animate(&img, false)) {
  388. redraw();
  389. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  390. }
  391. }
  392. void slideshow(void)
  393. {
  394. load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
  395. redraw();
  396. }
  397. void clear_resize(void)
  398. {
  399. resized = false;
  400. }
  401. void key_handler(const char *key, unsigned int mask)
  402. {
  403. pid_t pid;
  404. int retval, status, n = mode == MODE_IMAGE ? fileidx : tns.sel;
  405. char *cmd = exec[EXEC_KEY].cmd, kstr[32];
  406. struct stat oldst, newst;
  407. if (cmd == NULL || key == NULL)
  408. return;
  409. snprintf(kstr, sizeof(kstr), "%s%s%s%s",
  410. mask & ControlMask ? "C-" : "",
  411. mask & Mod1Mask ? "M-" : "",
  412. mask & ShiftMask ? "S-" : "", key);
  413. stat(files[n].path, &oldst);
  414. if ((pid = fork()) == 0) {
  415. execl(cmd, cmd, kstr, files[n].path, NULL);
  416. warn("could not exec key handler");
  417. exit(EXIT_FAILURE);
  418. } else if (pid < 0) {
  419. warn("could not fork key handler");
  420. return;
  421. }
  422. win_set_cursor(&win, CURSOR_WATCH);
  423. waitpid(pid, &status, 0);
  424. retval = WEXITSTATUS(status);
  425. if (WIFEXITED(status) == 0 || retval != 0)
  426. warn("key handler exited with non-zero return value: %d", retval);
  427. if (stat(files[n].path, &newst) == 0 &&
  428. memcmp(&oldst.st_mtime, &newst.st_mtime, sizeof(oldst.st_mtime)) == 0)
  429. {
  430. /* file has not changed */
  431. win_set_cursor(&win, CURSOR_ARROW);
  432. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  433. return;
  434. }
  435. if (mode == MODE_IMAGE) {
  436. img_close(&img, true);
  437. load_image(fileidx);
  438. }
  439. if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
  440. mode == MODE_THUMB)
  441. {
  442. remove_file(tns.sel, false);
  443. tns.dirty = true;
  444. if (tns.sel >= tns.cnt)
  445. tns.sel = tns.cnt - 1;
  446. }
  447. redraw();
  448. }
  449. #define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
  450. void on_keypress(XKeyEvent *kev)
  451. {
  452. int i;
  453. unsigned int sh;
  454. KeySym ksym, shksym;
  455. char key;
  456. if (kev == NULL)
  457. return;
  458. if (kev->state & ShiftMask) {
  459. kev->state &= ~ShiftMask;
  460. XLookupString(kev, &key, 1, &shksym, NULL);
  461. kev->state |= ShiftMask;
  462. }
  463. XLookupString(kev, &key, 1, &ksym, NULL);
  464. sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
  465. if (IsModifierKey(ksym))
  466. return;
  467. if ((ksym == XK_Escape && MODMASK(kev->state) == 0) ||
  468. (key >= '0' && key <= '9'))
  469. {
  470. /* number prefix for commands */
  471. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  472. return;
  473. }
  474. for (i = 0; i < ARRLEN(keys); i++) {
  475. if (keys[i].ksym == ksym &&
  476. MODMASK(keys[i].mask | sh) == MODMASK(kev->state))
  477. {
  478. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  479. redraw();
  480. prefix = 0;
  481. return;
  482. }
  483. }
  484. key_handler(XKeysymToString(ksym), kev->state & ~sh);
  485. }
  486. void on_buttonpress(XButtonEvent *bev)
  487. {
  488. int i, sel;
  489. if (bev == NULL)
  490. return;
  491. if (mode == MODE_IMAGE) {
  492. win_set_cursor(&win, CURSOR_ARROW);
  493. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  494. for (i = 0; i < ARRLEN(buttons); i++) {
  495. if (buttons[i].button == bev->button &&
  496. MODMASK(buttons[i].mask) == MODMASK(bev->state))
  497. {
  498. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  499. redraw();
  500. return;
  501. }
  502. }
  503. } else {
  504. /* thumbnail mode (hard-coded) */
  505. switch (bev->button) {
  506. case Button1:
  507. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  508. if (sel == tns.sel) {
  509. mode = MODE_IMAGE;
  510. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  511. load_image(tns.sel);
  512. } else {
  513. tns_highlight(&tns, tns.sel, false);
  514. tns_highlight(&tns, sel, true);
  515. tns.sel = sel;
  516. }
  517. redraw();
  518. break;
  519. }
  520. break;
  521. case Button3:
  522. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  523. files[sel].marked = !files[sel].marked;
  524. tns_mark(&tns, sel, files[sel].marked);
  525. redraw();
  526. }
  527. break;
  528. case Button4:
  529. case Button5:
  530. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  531. (bev->state & ControlMask) != 0))
  532. redraw();
  533. break;
  534. }
  535. }
  536. }
  537. void run(void)
  538. {
  539. int xfd;
  540. fd_set fds;
  541. struct timeval timeout;
  542. bool discard, to_set;
  543. XEvent ev, nextev;
  544. redraw();
  545. while (true) {
  546. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  547. XPending(win.env.dpy) == 0)
  548. {
  549. /* load thumbnails */
  550. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  551. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  552. tns.cnt++;
  553. } else {
  554. remove_file(tns.cnt, false);
  555. if (tns.sel > 0 && tns.sel >= tns.cnt)
  556. tns.sel--;
  557. }
  558. if (tns.cnt == filecnt)
  559. redraw();
  560. else
  561. check_timeouts(NULL);
  562. }
  563. while (XPending(win.env.dpy) == 0
  564. && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
  565. {
  566. /* check for timeouts & input */
  567. xfd = ConnectionNumber(win.env.dpy);
  568. FD_ZERO(&fds);
  569. FD_SET(xfd, &fds);
  570. if (info.fd != -1) {
  571. FD_SET(info.fd, &fds);
  572. xfd = MAX(xfd, info.fd);
  573. }
  574. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  575. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  576. read_info();
  577. }
  578. do {
  579. XNextEvent(win.env.dpy, &ev);
  580. discard = false;
  581. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  582. XPeekEvent(win.env.dpy, &nextev);
  583. switch (ev.type) {
  584. case ConfigureNotify:
  585. discard = ev.type == nextev.type;
  586. break;
  587. case KeyPress:
  588. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  589. && ev.xkey.keycode == nextev.xkey.keycode;
  590. break;
  591. }
  592. }
  593. } while (discard);
  594. switch (ev.type) {
  595. /* handle events */
  596. case ButtonPress:
  597. on_buttonpress(&ev.xbutton);
  598. break;
  599. case ClientMessage:
  600. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  601. return;
  602. break;
  603. case ConfigureNotify:
  604. if (win_configure(&win, &ev.xconfigure)) {
  605. if (mode == MODE_IMAGE) {
  606. img.dirty = true;
  607. img.checkpan = true;
  608. } else {
  609. tns.dirty = true;
  610. }
  611. if (!resized || win.fullscreen) {
  612. redraw();
  613. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  614. resized = true;
  615. } else {
  616. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  617. }
  618. }
  619. break;
  620. case Expose:
  621. win_expose(&win, &ev.xexpose);
  622. break;
  623. case KeyPress:
  624. on_keypress(&ev.xkey);
  625. break;
  626. case MotionNotify:
  627. if (mode == MODE_IMAGE) {
  628. win_set_cursor(&win, CURSOR_ARROW);
  629. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  630. }
  631. break;
  632. }
  633. }
  634. }
  635. int fncmp(const void *a, const void *b)
  636. {
  637. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  638. }
  639. int main(int argc, char **argv)
  640. {
  641. int i, start;
  642. size_t n;
  643. ssize_t len;
  644. char *filename;
  645. const char *homedir;
  646. struct stat fstats;
  647. r_dir_t dir;
  648. parse_options(argc, argv);
  649. if (options->clean_cache) {
  650. tns_init(&tns, 0, NULL);
  651. tns_clean_cache(&tns);
  652. exit(EXIT_SUCCESS);
  653. }
  654. if (options->filecnt == 0 && !options->from_stdin) {
  655. print_usage();
  656. exit(EXIT_FAILURE);
  657. }
  658. if (options->recursive || options->from_stdin)
  659. filecnt = FILENAME_CNT;
  660. else
  661. filecnt = options->filecnt;
  662. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  663. fileidx = 0;
  664. if (options->from_stdin) {
  665. filename = NULL;
  666. while ((len = get_line(&filename, &n, stdin)) > 0) {
  667. if (filename[len-1] == '\n')
  668. filename[len-1] = '\0';
  669. check_add_file(filename);
  670. }
  671. if (filename != NULL)
  672. free(filename);
  673. }
  674. for (i = 0; i < options->filecnt; i++) {
  675. filename = options->filenames[i];
  676. if (stat(filename, &fstats) < 0) {
  677. warn("could not stat file: %s", filename);
  678. continue;
  679. }
  680. if (!S_ISDIR(fstats.st_mode)) {
  681. check_add_file(filename);
  682. } else {
  683. if (!options->recursive) {
  684. warn("ignoring directory: %s", filename);
  685. continue;
  686. }
  687. if (r_opendir(&dir, filename) < 0) {
  688. warn("could not open directory: %s", filename);
  689. continue;
  690. }
  691. start = fileidx;
  692. while ((filename = r_readdir(&dir)) != NULL) {
  693. check_add_file(filename);
  694. free((void*) filename);
  695. }
  696. r_closedir(&dir);
  697. if (fileidx - start > 1)
  698. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  699. }
  700. }
  701. if (fileidx == 0) {
  702. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  703. exit(EXIT_FAILURE);
  704. }
  705. filecnt = fileidx;
  706. fileidx = options->startnum < filecnt ? options->startnum : 0;
  707. win_init(&win);
  708. img_init(&img, &win);
  709. if ((homedir = getenv("HOME")) == NULL) {
  710. warn("could not locate home directory");
  711. } else for (i = 0; i < ARRLEN(exec); i++) {
  712. len = strlen(homedir) + strlen(EXEC_REL_DIR) + strlen(exec[i].name) + 3;
  713. exec[i].cmd = (char*) s_malloc(len);
  714. snprintf(exec[i].cmd, len, "%s/%s/%s", homedir, EXEC_REL_DIR, exec[i].name);
  715. if (access(exec[i].cmd, X_OK) != 0) {
  716. free(exec[i].cmd);
  717. exec[i].cmd = NULL;
  718. }
  719. }
  720. info.fd = -1;
  721. info.cmd = exec[EXEC_INFO].cmd;
  722. if (options->thumb_mode) {
  723. mode = MODE_THUMB;
  724. tns_init(&tns, filecnt, &win);
  725. while (!tns_load(&tns, 0, &files[0], false, false))
  726. remove_file(0, false);
  727. tns.cnt = 1;
  728. } else {
  729. mode = MODE_IMAGE;
  730. tns.thumbs = NULL;
  731. load_image(fileidx);
  732. }
  733. win_open(&win);
  734. run();
  735. cleanup();
  736. return 0;
  737. }