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.
 
 
 
 
 
 

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