A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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