A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

822 lignes
17 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. t = img.multi.animate ? MAX(t, img.multi.length) : t;
  354. set_timeout(slideshow, t, false);
  355. }
  356. } else {
  357. tns_render(&tns);
  358. }
  359. update_info();
  360. win_draw(&win);
  361. reset_timeout(redraw);
  362. reset_cursor();
  363. }
  364. void reset_cursor(void)
  365. {
  366. int i;
  367. cursor_t cursor = CURSOR_NONE;
  368. if (mode == MODE_IMAGE) {
  369. for (i = 0; i < ARRLEN(timeouts); i++) {
  370. if (timeouts[i].handler == reset_cursor) {
  371. if (timeouts[i].active)
  372. cursor = CURSOR_ARROW;
  373. break;
  374. }
  375. }
  376. } else {
  377. if (tns.cnt != filecnt)
  378. cursor = CURSOR_WATCH;
  379. else
  380. cursor = CURSOR_ARROW;
  381. }
  382. win_set_cursor(&win, cursor);
  383. }
  384. void animate(void)
  385. {
  386. if (img_frame_animate(&img, false)) {
  387. redraw();
  388. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  389. }
  390. }
  391. void slideshow(void)
  392. {
  393. load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
  394. redraw();
  395. }
  396. void clear_resize(void)
  397. {
  398. resized = false;
  399. }
  400. void key_handler(const char *key, unsigned int mask)
  401. {
  402. pid_t pid;
  403. int retval, status, n = mode == MODE_IMAGE ? fileidx : tns.sel;
  404. char *cmd = exec[EXEC_KEY].cmd, kstr[32];
  405. if (cmd == NULL || key == NULL)
  406. return;
  407. snprintf(kstr, sizeof(kstr), "%s%s%s%s",
  408. mask & ControlMask ? "C-" : "",
  409. mask & Mod1Mask ? "M-" : "",
  410. mask & ShiftMask ? "S-" : "", key);
  411. if ((pid = fork()) == 0) {
  412. execl(cmd, cmd, kstr, files[n].path, NULL);
  413. warn("could not exec key handler");
  414. exit(EXIT_FAILURE);
  415. } else if (pid < 0) {
  416. warn("could not fork key handler");
  417. return;
  418. }
  419. win_set_cursor(&win, CURSOR_WATCH);
  420. waitpid(pid, &status, 0);
  421. retval = WEXITSTATUS(status);
  422. if (WIFEXITED(status) == 0 || retval != 0)
  423. warn("key handler exited with non-zero return value: %d", retval);
  424. if (mode == MODE_IMAGE) {
  425. img_close(&img, true);
  426. load_image(fileidx);
  427. }
  428. if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
  429. mode == MODE_THUMB)
  430. {
  431. remove_file(tns.sel, false);
  432. tns.dirty = true;
  433. if (tns.sel >= tns.cnt)
  434. tns.sel = tns.cnt - 1;
  435. }
  436. redraw();
  437. }
  438. #define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
  439. void on_keypress(XKeyEvent *kev)
  440. {
  441. int i;
  442. unsigned int sh;
  443. KeySym ksym, shksym;
  444. char key;
  445. if (kev == NULL)
  446. return;
  447. if (kev->state & ShiftMask) {
  448. kev->state &= ~ShiftMask;
  449. XLookupString(kev, &key, 1, &shksym, NULL);
  450. kev->state |= ShiftMask;
  451. }
  452. XLookupString(kev, &key, 1, &ksym, NULL);
  453. sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
  454. if (IsModifierKey(ksym))
  455. return;
  456. if ((ksym == XK_Escape && MODMASK(kev->state) == 0) ||
  457. (key >= '0' && key <= '9'))
  458. {
  459. /* number prefix for commands */
  460. prefix = ksym == XK_Escape ? 0 : prefix * 10 + (int) (key - '0');
  461. return;
  462. }
  463. for (i = 0; i < ARRLEN(keys); i++) {
  464. if (keys[i].ksym == ksym &&
  465. MODMASK(keys[i].mask | sh) == MODMASK(kev->state))
  466. {
  467. if (keys[i].cmd != NULL && keys[i].cmd(keys[i].arg))
  468. redraw();
  469. prefix = 0;
  470. return;
  471. }
  472. }
  473. key_handler(XKeysymToString(ksym), kev->state & ~sh);
  474. }
  475. void on_buttonpress(XButtonEvent *bev)
  476. {
  477. int i, sel;
  478. if (bev == NULL)
  479. return;
  480. if (mode == MODE_IMAGE) {
  481. win_set_cursor(&win, CURSOR_ARROW);
  482. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  483. for (i = 0; i < ARRLEN(buttons); i++) {
  484. if (buttons[i].button == bev->button &&
  485. MODMASK(buttons[i].mask) == MODMASK(bev->state))
  486. {
  487. if (buttons[i].cmd != NULL && buttons[i].cmd(buttons[i].arg))
  488. redraw();
  489. return;
  490. }
  491. }
  492. } else {
  493. /* thumbnail mode (hard-coded) */
  494. switch (bev->button) {
  495. case Button1:
  496. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  497. if (sel == tns.sel) {
  498. mode = MODE_IMAGE;
  499. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  500. load_image(tns.sel);
  501. } else {
  502. tns_highlight(&tns, tns.sel, false);
  503. tns_highlight(&tns, sel, true);
  504. tns.sel = sel;
  505. }
  506. redraw();
  507. break;
  508. }
  509. break;
  510. case Button3:
  511. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  512. files[sel].marked = !files[sel].marked;
  513. tns_mark(&tns, sel, files[sel].marked);
  514. redraw();
  515. }
  516. break;
  517. case Button4:
  518. case Button5:
  519. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  520. (bev->state & ControlMask) != 0))
  521. redraw();
  522. break;
  523. }
  524. }
  525. }
  526. void run(void)
  527. {
  528. int xfd;
  529. fd_set fds;
  530. struct timeval timeout;
  531. bool discard, to_set;
  532. XEvent ev, nextev;
  533. redraw();
  534. while (true) {
  535. while (mode == MODE_THUMB && tns.cnt < filecnt &&
  536. XPending(win.env.dpy) == 0)
  537. {
  538. /* load thumbnails */
  539. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  540. if (tns_load(&tns, tns.cnt, &files[tns.cnt], false, false)) {
  541. tns.cnt++;
  542. } else {
  543. remove_file(tns.cnt, false);
  544. if (tns.sel > 0 && tns.sel >= tns.cnt)
  545. tns.sel--;
  546. }
  547. if (tns.cnt == filecnt)
  548. redraw();
  549. else
  550. check_timeouts(NULL);
  551. }
  552. while (XPending(win.env.dpy) == 0
  553. && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
  554. {
  555. /* check for timeouts & input */
  556. xfd = ConnectionNumber(win.env.dpy);
  557. FD_ZERO(&fds);
  558. FD_SET(xfd, &fds);
  559. if (info.fd != -1) {
  560. FD_SET(info.fd, &fds);
  561. xfd = MAX(xfd, info.fd);
  562. }
  563. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  564. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  565. read_info();
  566. }
  567. do {
  568. XNextEvent(win.env.dpy, &ev);
  569. discard = false;
  570. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  571. XPeekEvent(win.env.dpy, &nextev);
  572. switch (ev.type) {
  573. case ConfigureNotify:
  574. discard = ev.type == nextev.type;
  575. break;
  576. case KeyPress:
  577. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  578. && ev.xkey.keycode == nextev.xkey.keycode;
  579. break;
  580. }
  581. }
  582. } while (discard);
  583. switch (ev.type) {
  584. /* handle events */
  585. case ButtonPress:
  586. on_buttonpress(&ev.xbutton);
  587. break;
  588. case ClientMessage:
  589. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  590. return;
  591. break;
  592. case ConfigureNotify:
  593. if (win_configure(&win, &ev.xconfigure)) {
  594. if (mode == MODE_IMAGE) {
  595. img.dirty = true;
  596. img.checkpan = true;
  597. } else {
  598. tns.dirty = true;
  599. }
  600. if (!resized || win.fullscreen) {
  601. redraw();
  602. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  603. resized = true;
  604. } else {
  605. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  606. }
  607. }
  608. break;
  609. case Expose:
  610. win_expose(&win, &ev.xexpose);
  611. break;
  612. case KeyPress:
  613. on_keypress(&ev.xkey);
  614. break;
  615. case MotionNotify:
  616. if (mode == MODE_IMAGE) {
  617. win_set_cursor(&win, CURSOR_ARROW);
  618. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  619. }
  620. break;
  621. }
  622. }
  623. }
  624. int fncmp(const void *a, const void *b)
  625. {
  626. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  627. }
  628. int main(int argc, char **argv)
  629. {
  630. int i, start;
  631. size_t n;
  632. ssize_t len;
  633. char *filename;
  634. const char *homedir;
  635. struct stat fstats;
  636. r_dir_t dir;
  637. parse_options(argc, argv);
  638. if (options->clean_cache) {
  639. tns_init(&tns, 0, NULL);
  640. tns_clean_cache(&tns);
  641. exit(EXIT_SUCCESS);
  642. }
  643. if (options->filecnt == 0 && !options->from_stdin) {
  644. print_usage();
  645. exit(EXIT_FAILURE);
  646. }
  647. if (options->recursive || options->from_stdin)
  648. filecnt = FILENAME_CNT;
  649. else
  650. filecnt = options->filecnt;
  651. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  652. fileidx = 0;
  653. if (options->from_stdin) {
  654. filename = NULL;
  655. while ((len = get_line(&filename, &n, stdin)) > 0) {
  656. if (filename[len-1] == '\n')
  657. filename[len-1] = '\0';
  658. check_add_file(filename);
  659. }
  660. if (filename != NULL)
  661. free(filename);
  662. }
  663. for (i = 0; i < options->filecnt; i++) {
  664. filename = options->filenames[i];
  665. if (stat(filename, &fstats) < 0) {
  666. warn("could not stat file: %s", filename);
  667. continue;
  668. }
  669. if (!S_ISDIR(fstats.st_mode)) {
  670. check_add_file(filename);
  671. } else {
  672. if (!options->recursive) {
  673. warn("ignoring directory: %s", filename);
  674. continue;
  675. }
  676. if (r_opendir(&dir, filename) < 0) {
  677. warn("could not open directory: %s", filename);
  678. continue;
  679. }
  680. start = fileidx;
  681. while ((filename = r_readdir(&dir)) != NULL) {
  682. check_add_file(filename);
  683. free((void*) filename);
  684. }
  685. r_closedir(&dir);
  686. if (fileidx - start > 1)
  687. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  688. }
  689. }
  690. if (fileidx == 0) {
  691. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  692. exit(EXIT_FAILURE);
  693. }
  694. filecnt = fileidx;
  695. fileidx = options->startnum < filecnt ? options->startnum : 0;
  696. win_init(&win);
  697. img_init(&img, &win);
  698. if ((homedir = getenv("HOME")) == NULL) {
  699. warn("could not locate home directory");
  700. } else for (i = 0; i < ARRLEN(exec); i++) {
  701. len = strlen(homedir) + strlen(EXEC_REL_DIR) + strlen(exec[i].name) + 3;
  702. exec[i].cmd = (char*) s_malloc(len);
  703. snprintf(exec[i].cmd, len, "%s/%s/%s", homedir, EXEC_REL_DIR, exec[i].name);
  704. if (access(exec[i].cmd, X_OK) != 0) {
  705. free(exec[i].cmd);
  706. exec[i].cmd = NULL;
  707. }
  708. }
  709. info.fd = -1;
  710. info.cmd = exec[EXEC_INFO].cmd;
  711. if (options->thumb_mode) {
  712. mode = MODE_THUMB;
  713. tns_init(&tns, filecnt, &win);
  714. while (!tns_load(&tns, 0, &files[0], false, false))
  715. remove_file(0, false);
  716. tns.cnt = 1;
  717. } else {
  718. mode = MODE_IMAGE;
  719. tns.thumbs = NULL;
  720. load_image(fileidx);
  721. }
  722. win_open(&win);
  723. run();
  724. cleanup();
  725. return 0;
  726. }