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.
 
 
 
 
 
 

876 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 (mode == MODE_THUMB && fileidx >= tns.cnt)
  160. fileidx = tns.cnt - 1;
  161. if (n < alternate)
  162. alternate--;
  163. }
  164. void set_timeout(timeout_f handler, int time, bool overwrite)
  165. {
  166. int i;
  167. for (i = 0; i < ARRLEN(timeouts); i++) {
  168. if (timeouts[i].handler == handler) {
  169. if (!timeouts[i].active || overwrite) {
  170. gettimeofday(&timeouts[i].when, 0);
  171. TV_ADD_MSEC(&timeouts[i].when, time);
  172. timeouts[i].active = true;
  173. }
  174. return;
  175. }
  176. }
  177. }
  178. void reset_timeout(timeout_f handler)
  179. {
  180. int i;
  181. for (i = 0; i < ARRLEN(timeouts); i++) {
  182. if (timeouts[i].handler == handler) {
  183. timeouts[i].active = false;
  184. return;
  185. }
  186. }
  187. }
  188. bool check_timeouts(struct timeval *t)
  189. {
  190. int i = 0, tdiff, tmin = -1;
  191. struct timeval now;
  192. while (i < ARRLEN(timeouts)) {
  193. if (timeouts[i].active) {
  194. gettimeofday(&now, 0);
  195. tdiff = TV_DIFF(&timeouts[i].when, &now);
  196. if (tdiff <= 0) {
  197. timeouts[i].active = false;
  198. if (timeouts[i].handler != NULL)
  199. timeouts[i].handler();
  200. i = tmin = -1;
  201. } else if (tmin < 0 || tdiff < tmin) {
  202. tmin = tdiff;
  203. }
  204. }
  205. i++;
  206. }
  207. if (tmin > 0 && t != NULL)
  208. TV_SET_MSEC(t, tmin);
  209. return tmin > 0;
  210. }
  211. void open_info(void)
  212. {
  213. static pid_t pid;
  214. int pfd[2];
  215. if (info.cmd == NULL || info.open || win.bar.h == 0)
  216. return;
  217. if (info.fd != -1) {
  218. close(info.fd);
  219. kill(pid, SIGTERM);
  220. info.fd = -1;
  221. }
  222. win.bar.l[0] = '\0';
  223. if (pipe(pfd) < 0)
  224. return;
  225. pid = fork();
  226. if (pid > 0) {
  227. close(pfd[1]);
  228. fcntl(pfd[0], F_SETFL, O_NONBLOCK);
  229. info.fd = pfd[0];
  230. info.i = info.lastsep = 0;
  231. info.open = true;
  232. } else if (pid == 0) {
  233. close(pfd[0]);
  234. dup2(pfd[1], 1);
  235. execl(info.cmd, info.cmd, files[fileidx].name, NULL);
  236. warn("could not exec: %s", info.cmd);
  237. exit(EXIT_FAILURE);
  238. }
  239. }
  240. void read_info(void)
  241. {
  242. ssize_t i, n;
  243. char buf[BAR_L_LEN];
  244. while (true) {
  245. n = read(info.fd, buf, sizeof(buf));
  246. if (n < 0 && errno == EAGAIN)
  247. return;
  248. else if (n == 0)
  249. goto end;
  250. for (i = 0; i < n; i++) {
  251. if (buf[i] == '\n') {
  252. if (info.lastsep == 0) {
  253. win.bar.l[info.i++] = ' ';
  254. info.lastsep = 1;
  255. }
  256. } else {
  257. win.bar.l[info.i++] = buf[i];
  258. info.lastsep = 0;
  259. }
  260. if (info.i + 1 == sizeof(win.bar.l))
  261. goto end;
  262. }
  263. }
  264. end:
  265. info.i -= info.lastsep;
  266. win.bar.l[info.i] = '\0';
  267. win_draw(&win);
  268. close(info.fd);
  269. info.fd = -1;
  270. while (waitpid(-1, NULL, WNOHANG) > 0);
  271. }
  272. void load_image(int new)
  273. {
  274. if (new < 0 || new >= filecnt)
  275. return;
  276. win_set_cursor(&win, CURSOR_WATCH);
  277. reset_timeout(slideshow);
  278. if (new != fileidx)
  279. alternate = fileidx;
  280. img_close(&img, false);
  281. while (!img_load(&img, &files[new])) {
  282. remove_file(new, false);
  283. if (new >= filecnt)
  284. new = filecnt - 1;
  285. else if (new > 0 && new < fileidx)
  286. new--;
  287. }
  288. files[new].loaded = true;
  289. fileidx = new;
  290. info.open = false;
  291. open_info();
  292. if (img.multi.cnt > 0 && img.multi.animate)
  293. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  294. else
  295. reset_timeout(animate);
  296. }
  297. void update_info(void)
  298. {
  299. unsigned int i, fn, fw, n;
  300. unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
  301. char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
  302. const char * mark;
  303. bool ow_info;
  304. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  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[fileidx].name);
  310. win_set_title(&win, title);
  311. }
  312. /* update bar contents */
  313. if (win.bar.h == 0)
  314. return;
  315. mark = files[fileidx].marked ? "* " : "";
  316. if (mode == MODE_THUMB) {
  317. if (tns.loadnext >= filecnt) {
  318. n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
  319. ow_info = true;
  320. } else {
  321. snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.loadnext, 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, fileidx + 1, filecnt);
  338. ow_info = info.cmd == NULL;
  339. }
  340. if (ow_info) {
  341. fn = strlen(files[fileidx].name);
  342. if (fn < llen &&
  343. win_textwidth(files[fileidx].name, fn, true) +
  344. win_textwidth(rt, n, true) < win.w)
  345. {
  346. strncpy(lt, files[fileidx].name, llen);
  347. } else {
  348. strncpy(lt, files[fileidx].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.loadnext < 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;
  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[fileidx].path, &oldst);
  433. if ((pid = fork()) == 0) {
  434. execl(keyhandler.cmd, keyhandler.cmd, kstr, files[fileidx].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[fileidx].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, fileidx, &files[fileidx], true, mode == MODE_IMAGE) &&
  459. mode == MODE_THUMB)
  460. {
  461. remove_file(fileidx, false);
  462. tns.dirty = true;
  463. }
  464. end:
  465. if (restore_bar)
  466. memcpy(win.bar.l, oldbar, sizeof(win.bar.l));
  467. reset_cursor();
  468. redraw();
  469. }
  470. #define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
  471. void on_keypress(XKeyEvent *kev)
  472. {
  473. int i;
  474. unsigned int sh;
  475. KeySym ksym, shksym;
  476. char key;
  477. bool dirty = false;
  478. if (kev == NULL)
  479. return;
  480. if (kev->state & ShiftMask) {
  481. kev->state &= ~ShiftMask;
  482. XLookupString(kev, &key, 1, &shksym, NULL);
  483. kev->state |= ShiftMask;
  484. }
  485. XLookupString(kev, &key, 1, &ksym, NULL);
  486. sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
  487. if (IsModifierKey(ksym))
  488. return;
  489. if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
  490. extprefix = False;
  491. } else if (extprefix) {
  492. run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
  493. extprefix = False;
  494. } else if (key >= '0' && key <= '9') {
  495. /* number prefix for commands */
  496. prefix = prefix * 10 + (int) (key - '0');
  497. return;
  498. } else for (i = 0; i < ARRLEN(keys); i++) {
  499. if (keys[i].ksym == ksym &&
  500. MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
  501. keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
  502. (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
  503. {
  504. if (cmds[keys[i].cmd].func(keys[i].arg))
  505. dirty = true;
  506. }
  507. }
  508. if (dirty)
  509. redraw();
  510. prefix = 0;
  511. }
  512. void on_buttonpress(XButtonEvent *bev)
  513. {
  514. int i, sel;
  515. bool dirty = false;
  516. static Time firstclick;
  517. if (bev == NULL)
  518. return;
  519. if (mode == MODE_IMAGE) {
  520. win_set_cursor(&win, CURSOR_ARROW);
  521. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  522. for (i = 0; i < ARRLEN(buttons); i++) {
  523. if (buttons[i].button == bev->button &&
  524. MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
  525. buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
  526. (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
  527. {
  528. if (cmds[buttons[i].cmd].func(buttons[i].arg))
  529. dirty = true;
  530. }
  531. }
  532. if (dirty)
  533. redraw();
  534. } else {
  535. /* thumbnail mode (hard-coded) */
  536. switch (bev->button) {
  537. case Button1:
  538. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  539. if (sel != fileidx) {
  540. tns_highlight(&tns, fileidx, false);
  541. tns_highlight(&tns, sel, true);
  542. fileidx = sel;
  543. firstclick = bev->time;
  544. redraw();
  545. } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
  546. mode = MODE_IMAGE;
  547. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  548. load_image(fileidx);
  549. redraw();
  550. } else {
  551. firstclick = bev->time;
  552. }
  553. }
  554. break;
  555. case Button3:
  556. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  557. files[sel].marked = !files[sel].marked;
  558. tns_mark(&tns, sel, files[sel].marked);
  559. redraw();
  560. }
  561. break;
  562. case Button4:
  563. case Button5:
  564. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  565. (bev->state & ControlMask) != 0))
  566. redraw();
  567. break;
  568. }
  569. }
  570. prefix = 0;
  571. }
  572. void run(void)
  573. {
  574. int xfd;
  575. fd_set fds;
  576. struct timeval timeout;
  577. bool discard, to_set;
  578. XEvent ev, nextev;
  579. set_timeout(redraw, 25, false);
  580. while (true) {
  581. while (mode == MODE_THUMB && tns.loadnext < filecnt &&
  582. XPending(win.env.dpy) == 0)
  583. {
  584. /* load thumbnails */
  585. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  586. if (tns_load(&tns, tns.loadnext, &files[tns.loadnext], false, false)) {
  587. if (tns.cnt == tns.loadnext)
  588. tns.cnt++;
  589. } else {
  590. remove_file(tns.loadnext, false);
  591. }
  592. while (tns.loadnext < filecnt && tns.thumbs[tns.loadnext].loaded)
  593. tns.loadnext++;
  594. if (tns.loadnext >= filecnt)
  595. redraw();
  596. else
  597. check_timeouts(NULL);
  598. }
  599. while (XPending(win.env.dpy) == 0
  600. && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
  601. {
  602. /* check for timeouts & input */
  603. xfd = ConnectionNumber(win.env.dpy);
  604. FD_ZERO(&fds);
  605. FD_SET(xfd, &fds);
  606. if (info.fd != -1) {
  607. FD_SET(info.fd, &fds);
  608. xfd = MAX(xfd, info.fd);
  609. }
  610. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  611. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  612. read_info();
  613. }
  614. do {
  615. XNextEvent(win.env.dpy, &ev);
  616. discard = false;
  617. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  618. XPeekEvent(win.env.dpy, &nextev);
  619. switch (ev.type) {
  620. case ConfigureNotify:
  621. discard = ev.type == nextev.type;
  622. break;
  623. case KeyPress:
  624. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  625. && ev.xkey.keycode == nextev.xkey.keycode;
  626. break;
  627. }
  628. }
  629. } while (discard);
  630. switch (ev.type) {
  631. /* handle events */
  632. case ButtonPress:
  633. on_buttonpress(&ev.xbutton);
  634. break;
  635. case ClientMessage:
  636. if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
  637. return;
  638. break;
  639. case ConfigureNotify:
  640. if (win_configure(&win, &ev.xconfigure)) {
  641. if (mode == MODE_IMAGE) {
  642. img.dirty = true;
  643. img.checkpan = true;
  644. } else {
  645. tns.dirty = true;
  646. }
  647. if (!resized || win.fullscreen) {
  648. redraw();
  649. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  650. resized = true;
  651. } else {
  652. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  653. }
  654. }
  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, 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, &fileidx);
  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. win_set_cursor(&win, CURSOR_WATCH);
  775. run();
  776. cleanup();
  777. return 0;
  778. }