A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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