A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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