A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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