A Simple X Image Viewer
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

899 Zeilen
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. if (img.ss.delay % 10 != 0)
  330. bar_put(r, "%2.1fs | ", (float)img.ss.delay / 10);
  331. else
  332. bar_put(r, "%ds | ", img.ss.delay / 10);
  333. }
  334. if (img.gamma != 0)
  335. bar_put(r, "G%+d | ", img.gamma);
  336. bar_put(r, "%3d%% | ", (int) (img.zoom * 100.0));
  337. if (img.multi.cnt > 0) {
  338. for (fn = 0, i = img.multi.cnt; i > 0; fn++, i /= 10);
  339. bar_put(r, "%0*d/%d | ", fn, img.multi.sel + 1, img.multi.cnt);
  340. }
  341. bar_put(r, "%0*d/%d", fw, fileidx + 1, filecnt);
  342. ow_info = info.f.err != 0;
  343. }
  344. if (ow_info) {
  345. fn = strlen(files[fileidx].name);
  346. if (fn < l->size &&
  347. win_textwidth(&win.env, files[fileidx].name, fn, true) +
  348. win_textwidth(&win.env, r->buf, r->p - r->buf, true) < win.w)
  349. {
  350. strncpy(l->buf, files[fileidx].name, l->size);
  351. } else {
  352. strncpy(l->buf, files[fileidx].base, l->size);
  353. }
  354. }
  355. }
  356. void redraw(void)
  357. {
  358. int t;
  359. if (mode == MODE_IMAGE) {
  360. img_render(&img);
  361. if (img.ss.on) {
  362. t = img.ss.delay * 100;
  363. if (img.multi.cnt > 0 && img.multi.animate)
  364. t = MAX(t, img.multi.length);
  365. set_timeout(slideshow, t, false);
  366. }
  367. } else {
  368. tns_render(&tns);
  369. }
  370. update_info();
  371. win_draw(&win);
  372. reset_timeout(redraw);
  373. reset_cursor();
  374. }
  375. void reset_cursor(void)
  376. {
  377. int i;
  378. cursor_t cursor = CURSOR_NONE;
  379. if (mode == MODE_IMAGE) {
  380. for (i = 0; i < ARRLEN(timeouts); i++) {
  381. if (timeouts[i].handler == reset_cursor) {
  382. if (timeouts[i].active)
  383. cursor = CURSOR_ARROW;
  384. break;
  385. }
  386. }
  387. } else {
  388. if (tns.loadnext < tns.end || tns.initnext < filecnt)
  389. cursor = CURSOR_WATCH;
  390. else
  391. cursor = CURSOR_ARROW;
  392. }
  393. win_set_cursor(&win, cursor);
  394. }
  395. void animate(void)
  396. {
  397. if (img_frame_animate(&img)) {
  398. redraw();
  399. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  400. }
  401. }
  402. void slideshow(void)
  403. {
  404. load_image(fileidx + 1 < filecnt ? fileidx + 1 : 0);
  405. redraw();
  406. }
  407. void clear_resize(void)
  408. {
  409. resized = false;
  410. }
  411. Bool is_input_ev(Display *dpy, XEvent *ev, XPointer arg)
  412. {
  413. return ev->type == ButtonPress || ev->type == KeyPress;
  414. }
  415. void run_key_handler(const char *key, unsigned int mask)
  416. {
  417. pid_t pid;
  418. FILE *pfs;
  419. bool marked = mode == MODE_THUMB && markcnt > 0;
  420. bool changed = false;
  421. int f, i, pfd[2], status;
  422. int fcnt = marked ? markcnt : 1;
  423. char kstr[32];
  424. struct stat *oldst, st;
  425. XEvent dump;
  426. if (keyhandler.f.err != 0) {
  427. if (!keyhandler.warned) {
  428. error(0, keyhandler.f.err, "%s", keyhandler.f.cmd);
  429. keyhandler.warned = true;
  430. }
  431. return;
  432. }
  433. if (key == NULL)
  434. return;
  435. if (pipe(pfd) < 0) {
  436. error(0, errno, "pipe");
  437. return;
  438. }
  439. if ((pfs = fdopen(pfd[1], "w")) == NULL) {
  440. error(0, errno, "open pipe");
  441. close(pfd[0]), close(pfd[1]);
  442. return;
  443. }
  444. oldst = emalloc(fcnt * sizeof(*oldst));
  445. strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
  446. win_draw(&win);
  447. win_set_cursor(&win, CURSOR_WATCH);
  448. snprintf(kstr, sizeof(kstr), "%s%s%s%s",
  449. mask & ControlMask ? "C-" : "",
  450. mask & Mod1Mask ? "M-" : "",
  451. mask & ShiftMask ? "S-" : "", key);
  452. if ((pid = fork()) == 0) {
  453. close(pfd[1]);
  454. dup2(pfd[0], 0);
  455. execl(keyhandler.f.cmd, keyhandler.f.cmd, kstr, NULL);
  456. error(EXIT_FAILURE, errno, "exec: %s", keyhandler.f.cmd);
  457. }
  458. close(pfd[0]);
  459. if (pid < 0) {
  460. error(0, errno, "fork");
  461. fclose(pfs);
  462. goto end;
  463. }
  464. for (f = i = 0; f < fcnt; i++) {
  465. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  466. stat(files[i].path, &oldst[f]);
  467. fprintf(pfs, "%s\n", files[i].name);
  468. f++;
  469. }
  470. }
  471. fclose(pfs);
  472. waitpid(pid, &status, 0);
  473. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
  474. error(0, 0, "%s: Exited abnormally", keyhandler.f.cmd);
  475. for (f = i = 0; f < fcnt; i++) {
  476. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  477. if (stat(files[i].path, &st) != 0 ||
  478. memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
  479. {
  480. if (tns.thumbs != NULL) {
  481. tns_unload(&tns, i);
  482. tns.loadnext = MIN(tns.loadnext, i);
  483. }
  484. changed = true;
  485. }
  486. f++;
  487. }
  488. }
  489. /* drop user input events that occurred while running the key handler */
  490. while (XCheckIfEvent(win.env.dpy, &dump, is_input_ev, NULL));
  491. end:
  492. if (mode == MODE_IMAGE) {
  493. if (changed) {
  494. img_close(&img, true);
  495. load_image(fileidx);
  496. } else if (info.f.err == 0) {
  497. info.open = false;
  498. open_info();
  499. }
  500. }
  501. free(oldst);
  502. reset_cursor();
  503. redraw();
  504. }
  505. #define MODMASK(mask) ((mask) & (ShiftMask|ControlMask|Mod1Mask))
  506. void on_keypress(XKeyEvent *kev)
  507. {
  508. int i;
  509. unsigned int sh;
  510. KeySym ksym, shksym;
  511. char key;
  512. bool dirty = false;
  513. if (kev->state & ShiftMask) {
  514. kev->state &= ~ShiftMask;
  515. XLookupString(kev, &key, 1, &shksym, NULL);
  516. kev->state |= ShiftMask;
  517. }
  518. XLookupString(kev, &key, 1, &ksym, NULL);
  519. sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
  520. if (IsModifierKey(ksym))
  521. return;
  522. if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
  523. extprefix = False;
  524. } else if (extprefix) {
  525. run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
  526. extprefix = False;
  527. } else if (key >= '0' && key <= '9') {
  528. /* number prefix for commands */
  529. prefix = prefix * 10 + (int) (key - '0');
  530. return;
  531. } else for (i = 0; i < ARRLEN(keys); i++) {
  532. if (keys[i].ksym == ksym &&
  533. MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
  534. keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
  535. (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
  536. {
  537. if (cmds[keys[i].cmd].func(keys[i].arg))
  538. dirty = true;
  539. }
  540. }
  541. if (dirty)
  542. redraw();
  543. prefix = 0;
  544. }
  545. void on_buttonpress(XButtonEvent *bev)
  546. {
  547. int i, sel;
  548. bool dirty = false;
  549. static Time firstclick;
  550. if (mode == MODE_IMAGE) {
  551. win_set_cursor(&win, CURSOR_ARROW);
  552. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  553. for (i = 0; i < ARRLEN(buttons); i++) {
  554. if (buttons[i].button == bev->button &&
  555. MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
  556. buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
  557. (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
  558. {
  559. if (cmds[buttons[i].cmd].func(buttons[i].arg))
  560. dirty = true;
  561. }
  562. }
  563. if (dirty)
  564. redraw();
  565. } else {
  566. /* thumbnail mode (hard-coded) */
  567. switch (bev->button) {
  568. case Button1:
  569. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  570. if (sel != fileidx) {
  571. tns_highlight(&tns, fileidx, false);
  572. tns_highlight(&tns, sel, true);
  573. fileidx = sel;
  574. firstclick = bev->time;
  575. redraw();
  576. } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
  577. mode = MODE_IMAGE;
  578. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  579. load_image(fileidx);
  580. redraw();
  581. } else {
  582. firstclick = bev->time;
  583. }
  584. }
  585. break;
  586. case Button3:
  587. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  588. files[sel].flags ^= FF_MARK;
  589. markcnt += files[sel].flags & FF_MARK ? 1 : -1;
  590. tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
  591. redraw();
  592. }
  593. break;
  594. case Button4:
  595. case Button5:
  596. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  597. (bev->state & ControlMask) != 0))
  598. redraw();
  599. break;
  600. }
  601. }
  602. prefix = 0;
  603. }
  604. void run(void)
  605. {
  606. int xfd;
  607. fd_set fds;
  608. struct timeval timeout;
  609. bool discard, init_thumb, load_thumb, to_set;
  610. XEvent ev, nextev;
  611. while (true) {
  612. to_set = check_timeouts(&timeout);
  613. init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
  614. load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
  615. if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
  616. XPending(win.env.dpy) == 0)
  617. {
  618. if (load_thumb) {
  619. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  620. if (!tns_load(&tns, tns.loadnext, false, false)) {
  621. remove_file(tns.loadnext, false);
  622. tns.dirty = true;
  623. }
  624. if (tns.loadnext >= tns.end)
  625. redraw();
  626. } else if (init_thumb) {
  627. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  628. if (!tns_load(&tns, tns.initnext, false, true))
  629. remove_file(tns.initnext, false);
  630. } else {
  631. xfd = ConnectionNumber(win.env.dpy);
  632. FD_ZERO(&fds);
  633. FD_SET(xfd, &fds);
  634. if (info.fd != -1) {
  635. FD_SET(info.fd, &fds);
  636. xfd = MAX(xfd, info.fd);
  637. }
  638. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  639. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  640. read_info();
  641. }
  642. continue;
  643. }
  644. do {
  645. XNextEvent(win.env.dpy, &ev);
  646. discard = false;
  647. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  648. XPeekEvent(win.env.dpy, &nextev);
  649. switch (ev.type) {
  650. case ConfigureNotify:
  651. discard = ev.type == nextev.type;
  652. break;
  653. case KeyPress:
  654. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  655. && ev.xkey.keycode == nextev.xkey.keycode;
  656. break;
  657. }
  658. }
  659. } while (discard);
  660. switch (ev.type) {
  661. /* handle events */
  662. case ButtonPress:
  663. on_buttonpress(&ev.xbutton);
  664. break;
  665. case ClientMessage:
  666. if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
  667. return;
  668. break;
  669. case ConfigureNotify:
  670. if (win_configure(&win, &ev.xconfigure)) {
  671. if (mode == MODE_IMAGE) {
  672. img.dirty = true;
  673. img.checkpan = true;
  674. } else {
  675. tns.dirty = true;
  676. }
  677. if (!resized || win.fullscreen) {
  678. redraw();
  679. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  680. resized = true;
  681. } else {
  682. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  683. }
  684. }
  685. break;
  686. case KeyPress:
  687. on_keypress(&ev.xkey);
  688. break;
  689. case MotionNotify:
  690. if (mode == MODE_IMAGE) {
  691. win_set_cursor(&win, CURSOR_ARROW);
  692. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  693. }
  694. break;
  695. }
  696. }
  697. }
  698. int fncmp(const void *a, const void *b)
  699. {
  700. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  701. }
  702. int main(int argc, char **argv)
  703. {
  704. int i, start;
  705. size_t n;
  706. ssize_t len;
  707. char *filename;
  708. const char *homedir, *dsuffix = "";
  709. struct stat fstats;
  710. r_dir_t dir;
  711. signal(SIGPIPE, SIG_IGN);
  712. parse_options(argc, argv);
  713. if (options->clean_cache) {
  714. tns_init(&tns, NULL, NULL, NULL, NULL);
  715. tns_clean_cache(&tns);
  716. exit(EXIT_SUCCESS);
  717. }
  718. if (options->filecnt == 0 && !options->from_stdin) {
  719. print_usage();
  720. exit(EXIT_FAILURE);
  721. }
  722. if (options->recursive || options->from_stdin)
  723. filecnt = 1024;
  724. else
  725. filecnt = options->filecnt;
  726. files = emalloc(filecnt * sizeof(*files));
  727. memset(files, 0, filecnt * sizeof(*files));
  728. fileidx = 0;
  729. if (options->from_stdin) {
  730. n = 0;
  731. filename = NULL;
  732. while ((len = getline(&filename, &n, stdin)) > 0) {
  733. if (filename[len-1] == '\n')
  734. filename[len-1] = '\0';
  735. check_add_file(filename, true);
  736. }
  737. free(filename);
  738. }
  739. for (i = 0; i < options->filecnt; i++) {
  740. filename = options->filenames[i];
  741. if (stat(filename, &fstats) < 0) {
  742. error(0, errno, "%s", filename);
  743. continue;
  744. }
  745. if (!S_ISDIR(fstats.st_mode)) {
  746. check_add_file(filename, true);
  747. } else {
  748. if (r_opendir(&dir, filename, options->recursive) < 0) {
  749. error(0, errno, "%s", filename);
  750. continue;
  751. }
  752. start = fileidx;
  753. while ((filename = r_readdir(&dir)) != NULL) {
  754. check_add_file(filename, false);
  755. free((void*) filename);
  756. }
  757. r_closedir(&dir);
  758. if (fileidx - start > 1)
  759. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  760. }
  761. }
  762. if (fileidx == 0)
  763. error(EXIT_FAILURE, 0, "No valid image file given, aborting");
  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. extcmd_t *cmd[] = { &info.f, &keyhandler.f };
  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]->cmd = (char*) emalloc(n);
  778. snprintf(cmd[i]->cmd, n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
  779. if (access(cmd[i]->cmd, X_OK) != 0)
  780. cmd[i]->err = errno;
  781. }
  782. } else {
  783. error(0, 0, "Exec directory not found");
  784. }
  785. info.fd = -1;
  786. if (options->thumb_mode) {
  787. mode = MODE_THUMB;
  788. tns_init(&tns, files, &filecnt, &fileidx, &win);
  789. while (!tns_load(&tns, fileidx, false, false))
  790. remove_file(fileidx, false);
  791. } else {
  792. mode = MODE_IMAGE;
  793. tns.thumbs = NULL;
  794. load_image(fileidx);
  795. }
  796. win_open(&win);
  797. win_set_cursor(&win, CURSOR_WATCH);
  798. atexit(cleanup);
  799. set_timeout(redraw, 25, false);
  800. run();
  801. return 0;
  802. }