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.
 
 
 
 
 
 

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