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.
 
 
 
 
 
 

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