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.
 
 
 
 
 
 

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