A Simple X Image Viewer
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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