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.
 
 
 
 
 
 

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