A Simple X Image Viewer
 
 
 
 
 
 

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