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.
 
 
 
 
 
 

920 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. enum {
  42. FILENAME_CNT = 1024,
  43. TITLE_LEN = 256
  44. };
  45. typedef struct {
  46. const char *name;
  47. char *cmd;
  48. } exec_t;
  49. typedef struct {
  50. struct timeval when;
  51. bool active;
  52. timeout_f handler;
  53. } timeout_t;
  54. /* timeout handler functions: */
  55. void redraw(void);
  56. void reset_cursor(void);
  57. void animate(void);
  58. void slideshow(void);
  59. void clear_resize(void);
  60. appmode_t mode;
  61. img_t img;
  62. tns_t tns;
  63. win_t win;
  64. fileinfo_t *files;
  65. int filecnt, fileidx;
  66. int alternate;
  67. int markcnt;
  68. int prefix;
  69. bool extprefix;
  70. bool resized = false;
  71. struct {
  72. char *cmd;
  73. int fd;
  74. unsigned int i, lastsep;
  75. bool open;
  76. } info;
  77. struct {
  78. char *cmd;
  79. bool warned;
  80. } keyhandler;
  81. timeout_t timeouts[] = {
  82. { { 0, 0 }, false, redraw },
  83. { { 0, 0 }, false, reset_cursor },
  84. { { 0, 0 }, false, animate },
  85. { { 0, 0 }, false, slideshow },
  86. { { 0, 0 }, false, clear_resize },
  87. };
  88. void cleanup(void)
  89. {
  90. static bool in = false;
  91. if (!in) {
  92. in = true;
  93. img_close(&img, false);
  94. tns_free(&tns);
  95. win_close(&win);
  96. }
  97. }
  98. void check_add_file(char *filename, bool given)
  99. {
  100. const char *bn;
  101. if (*filename == '\0')
  102. return;
  103. if (access(filename, R_OK) < 0) {
  104. if (given)
  105. warn("could not open file: %s", filename);
  106. return;
  107. }
  108. if (fileidx == filecnt) {
  109. filecnt *= 2;
  110. files = s_realloc(files, filecnt * sizeof(*files));
  111. memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
  112. }
  113. #if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
  114. ((_XOPEN_SOURCE - 0) >= 500 || defined _XOPEN_SOURCE_EXTENDED)
  115. if ((files[fileidx].path = realpath(filename, NULL)) == NULL) {
  116. warn("could not get real path of file: %s\n", filename);
  117. return;
  118. }
  119. #else
  120. if (*filename != '/') {
  121. if ((files[fileidx].path = absolute_path(filename)) == NULL) {
  122. warn("could not get absolute path of file: %s\n", filename);
  123. return;
  124. }
  125. } else {
  126. files[fileidx].path = NULL;
  127. }
  128. #endif
  129. files[fileidx].name = s_strdup(filename);
  130. if (files[fileidx].path == NULL)
  131. files[fileidx].path = files[fileidx].name;
  132. if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
  133. files[fileidx].base = ++bn;
  134. else
  135. files[fileidx].base = files[fileidx].name;
  136. if (given)
  137. files[fileidx].flags |= FF_WARN;
  138. fileidx++;
  139. }
  140. void remove_file(int n, bool manual)
  141. {
  142. if (n < 0 || n >= filecnt)
  143. return;
  144. if (filecnt == 1) {
  145. if (!manual)
  146. fprintf(stderr, "sxiv: no more files to display, aborting\n");
  147. cleanup();
  148. exit(manual ? EXIT_SUCCESS : EXIT_FAILURE);
  149. }
  150. if (files[n].flags & FF_MARK)
  151. markcnt--;
  152. if (files[n].path != files[n].name)
  153. free((void*) files[n].path);
  154. free((void*) files[n].name);
  155. if (n + 1 < filecnt) {
  156. if (tns.thumbs != NULL) {
  157. memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
  158. sizeof(*tns.thumbs));
  159. memset(tns.thumbs + filecnt - 1, 0, sizeof(*tns.thumbs));
  160. }
  161. memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(*files));
  162. }
  163. filecnt--;
  164. if (fileidx >= filecnt)
  165. fileidx = filecnt - 1;
  166. if (n < alternate)
  167. alternate--;
  168. }
  169. void set_timeout(timeout_f handler, int time, bool overwrite)
  170. {
  171. int i;
  172. for (i = 0; i < ARRLEN(timeouts); i++) {
  173. if (timeouts[i].handler == handler) {
  174. if (!timeouts[i].active || overwrite) {
  175. gettimeofday(&timeouts[i].when, 0);
  176. TV_ADD_MSEC(&timeouts[i].when, time);
  177. timeouts[i].active = true;
  178. }
  179. return;
  180. }
  181. }
  182. }
  183. void reset_timeout(timeout_f handler)
  184. {
  185. int i;
  186. for (i = 0; i < ARRLEN(timeouts); i++) {
  187. if (timeouts[i].handler == handler) {
  188. timeouts[i].active = false;
  189. return;
  190. }
  191. }
  192. }
  193. bool check_timeouts(struct timeval *t)
  194. {
  195. int i = 0, tdiff, tmin = -1;
  196. struct timeval now;
  197. while (i < ARRLEN(timeouts)) {
  198. if (timeouts[i].active) {
  199. gettimeofday(&now, 0);
  200. tdiff = TV_DIFF(&timeouts[i].when, &now);
  201. if (tdiff <= 0) {
  202. timeouts[i].active = false;
  203. if (timeouts[i].handler != NULL)
  204. timeouts[i].handler();
  205. i = tmin = -1;
  206. } else if (tmin < 0 || tdiff < tmin) {
  207. tmin = tdiff;
  208. }
  209. }
  210. i++;
  211. }
  212. if (tmin > 0 && t != NULL)
  213. TV_SET_MSEC(t, tmin);
  214. return tmin > 0;
  215. }
  216. void open_info(void)
  217. {
  218. static pid_t pid;
  219. int pfd[2];
  220. if (info.cmd == NULL || info.open || win.bar.h == 0)
  221. return;
  222. if (info.fd != -1) {
  223. close(info.fd);
  224. kill(pid, SIGTERM);
  225. info.fd = -1;
  226. }
  227. win.bar.l.buf[0] = '\0';
  228. if (pipe(pfd) < 0)
  229. return;
  230. if ((pid = fork()) == 0) {
  231. close(pfd[0]);
  232. dup2(pfd[1], 1);
  233. execl(info.cmd, info.cmd, files[fileidx].name, NULL);
  234. warn("could not exec: %s", info.cmd);
  235. exit(EXIT_FAILURE);
  236. }
  237. close(pfd[1]);
  238. if (pid < 0) {
  239. close(pfd[0]);
  240. } else {
  241. fcntl(pfd[0], F_SETFL, O_NONBLOCK);
  242. info.fd = pfd[0];
  243. info.i = info.lastsep = 0;
  244. info.open = true;
  245. }
  246. }
  247. void read_info(void)
  248. {
  249. ssize_t i, n;
  250. char buf[BAR_L_LEN];
  251. while (true) {
  252. n = read(info.fd, buf, sizeof(buf));
  253. if (n < 0 && errno == EAGAIN)
  254. return;
  255. else if (n == 0)
  256. goto end;
  257. for (i = 0; i < n; i++) {
  258. if (buf[i] == '\n') {
  259. if (info.lastsep == 0) {
  260. win.bar.l.buf[info.i++] = ' ';
  261. info.lastsep = 1;
  262. }
  263. } else {
  264. win.bar.l.buf[info.i++] = buf[i];
  265. info.lastsep = 0;
  266. }
  267. if (info.i + 1 == win.bar.l.size)
  268. goto end;
  269. }
  270. }
  271. end:
  272. info.i -= info.lastsep;
  273. win.bar.l.buf[info.i] = '\0';
  274. win_draw(&win);
  275. close(info.fd);
  276. info.fd = -1;
  277. while (waitpid(-1, NULL, WNOHANG) > 0);
  278. }
  279. void load_image(int new)
  280. {
  281. static int current;
  282. if (new < 0 || new >= filecnt)
  283. return;
  284. if (win.xwin != None)
  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];
  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. strncpy(win.bar.l.buf, "Running key handler...", win.bar.l.size);
  458. win_draw(&win);
  459. win_set_cursor(&win, CURSOR_WATCH);
  460. snprintf(kstr, sizeof(kstr), "%s%s%s%s",
  461. mask & ControlMask ? "C-" : "",
  462. mask & Mod1Mask ? "M-" : "",
  463. mask & ShiftMask ? "S-" : "", key);
  464. if ((pid = fork()) == 0) {
  465. close(pfd[1]);
  466. dup2(pfd[0], 0);
  467. execl(keyhandler.cmd, keyhandler.cmd, kstr, NULL);
  468. warn("could not exec key handler");
  469. exit(EXIT_FAILURE);
  470. }
  471. close(pfd[0]);
  472. if (pid < 0) {
  473. fclose(pfs);
  474. warn("could not fork key handler");
  475. goto end;
  476. }
  477. for (f = i = 0; f < fcnt; i++) {
  478. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  479. stat(files[i].path, &oldst[f]);
  480. fprintf(pfs, "%s\n", files[i].path);
  481. f++;
  482. }
  483. }
  484. fclose(pfs);
  485. waitpid(pid, &status, 0);
  486. retval = WEXITSTATUS(status);
  487. if (WIFEXITED(status) == 0 || retval != 0)
  488. warn("key handler exited with non-zero return value: %d", retval);
  489. for (f = i = 0; f < fcnt; i++) {
  490. if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) {
  491. if (stat(files[i].path, &st) != 0 ||
  492. memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0)
  493. {
  494. if (tns.thumbs != NULL) {
  495. tns_unload(&tns, i);
  496. tns.loadnext = MIN(tns.loadnext, i);
  497. }
  498. changed = true;
  499. }
  500. f++;
  501. }
  502. }
  503. end:
  504. if (mode == MODE_IMAGE) {
  505. if (changed) {
  506. img_close(&img, true);
  507. load_image(fileidx);
  508. } else if (info.cmd != NULL) {
  509. info.open = false;
  510. open_info();
  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->state & ShiftMask) {
  526. kev->state &= ~ShiftMask;
  527. XLookupString(kev, &key, 1, &shksym, NULL);
  528. kev->state |= ShiftMask;
  529. }
  530. XLookupString(kev, &key, 1, &ksym, NULL);
  531. sh = (kev->state & ShiftMask) && ksym != shksym ? ShiftMask : 0;
  532. if (IsModifierKey(ksym))
  533. return;
  534. if (ksym == XK_Escape && MODMASK(kev->state) == 0) {
  535. extprefix = False;
  536. } else if (extprefix) {
  537. run_key_handler(XKeysymToString(ksym), kev->state & ~sh);
  538. extprefix = False;
  539. } else if (key >= '0' && key <= '9') {
  540. /* number prefix for commands */
  541. prefix = prefix * 10 + (int) (key - '0');
  542. return;
  543. } else for (i = 0; i < ARRLEN(keys); i++) {
  544. if (keys[i].ksym == ksym &&
  545. MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
  546. keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
  547. (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
  548. {
  549. if (cmds[keys[i].cmd].func(keys[i].arg))
  550. dirty = true;
  551. }
  552. }
  553. if (dirty)
  554. redraw();
  555. prefix = 0;
  556. }
  557. void on_buttonpress(XButtonEvent *bev)
  558. {
  559. int i, sel;
  560. bool dirty = false;
  561. static Time firstclick;
  562. if (mode == MODE_IMAGE) {
  563. win_set_cursor(&win, CURSOR_ARROW);
  564. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  565. for (i = 0; i < ARRLEN(buttons); i++) {
  566. if (buttons[i].button == bev->button &&
  567. MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
  568. buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
  569. (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
  570. {
  571. if (cmds[buttons[i].cmd].func(buttons[i].arg))
  572. dirty = true;
  573. }
  574. }
  575. if (dirty)
  576. redraw();
  577. } else {
  578. /* thumbnail mode (hard-coded) */
  579. switch (bev->button) {
  580. case Button1:
  581. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  582. if (sel != fileidx) {
  583. tns_highlight(&tns, fileidx, false);
  584. tns_highlight(&tns, sel, true);
  585. fileidx = sel;
  586. firstclick = bev->time;
  587. redraw();
  588. } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
  589. mode = MODE_IMAGE;
  590. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  591. load_image(fileidx);
  592. redraw();
  593. } else {
  594. firstclick = bev->time;
  595. }
  596. }
  597. break;
  598. case Button3:
  599. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  600. files[sel].flags ^= FF_MARK;
  601. markcnt += files[sel].flags & FF_MARK ? 1 : -1;
  602. tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK));
  603. redraw();
  604. }
  605. break;
  606. case Button4:
  607. case Button5:
  608. if (tns_scroll(&tns, bev->button == Button4 ? DIR_UP : DIR_DOWN,
  609. (bev->state & ControlMask) != 0))
  610. redraw();
  611. break;
  612. }
  613. }
  614. prefix = 0;
  615. }
  616. void run(void)
  617. {
  618. int xfd;
  619. fd_set fds;
  620. struct timeval timeout;
  621. bool discard, init_thumb, load_thumb, to_set;
  622. XEvent ev, nextev;
  623. while (true) {
  624. to_set = check_timeouts(&timeout);
  625. init_thumb = mode == MODE_THUMB && tns.initnext < filecnt;
  626. load_thumb = mode == MODE_THUMB && tns.loadnext < tns.end;
  627. if ((init_thumb || load_thumb || to_set || info.fd != -1) &&
  628. XPending(win.env.dpy) == 0)
  629. {
  630. if (load_thumb) {
  631. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  632. if (!tns_load(&tns, tns.loadnext, false, false)) {
  633. remove_file(tns.loadnext, false);
  634. tns.dirty = true;
  635. }
  636. if (tns.loadnext >= tns.end)
  637. redraw();
  638. } else if (init_thumb) {
  639. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  640. if (!tns_load(&tns, tns.initnext, false, true))
  641. remove_file(tns.initnext, false);
  642. } else {
  643. xfd = ConnectionNumber(win.env.dpy);
  644. FD_ZERO(&fds);
  645. FD_SET(xfd, &fds);
  646. if (info.fd != -1) {
  647. FD_SET(info.fd, &fds);
  648. xfd = MAX(xfd, info.fd);
  649. }
  650. select(xfd + 1, &fds, 0, 0, to_set ? &timeout : NULL);
  651. if (info.fd != -1 && FD_ISSET(info.fd, &fds))
  652. read_info();
  653. }
  654. continue;
  655. }
  656. do {
  657. XNextEvent(win.env.dpy, &ev);
  658. discard = false;
  659. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  660. XPeekEvent(win.env.dpy, &nextev);
  661. switch (ev.type) {
  662. case ConfigureNotify:
  663. discard = ev.type == nextev.type;
  664. break;
  665. case KeyPress:
  666. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  667. && ev.xkey.keycode == nextev.xkey.keycode;
  668. break;
  669. }
  670. }
  671. } while (discard);
  672. switch (ev.type) {
  673. /* handle events */
  674. case ButtonPress:
  675. on_buttonpress(&ev.xbutton);
  676. break;
  677. case ClientMessage:
  678. if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
  679. return;
  680. break;
  681. case ConfigureNotify:
  682. if (win_configure(&win, &ev.xconfigure)) {
  683. if (mode == MODE_IMAGE) {
  684. img.dirty = true;
  685. img.checkpan = true;
  686. } else {
  687. tns.dirty = true;
  688. }
  689. if (!resized || win.fullscreen) {
  690. redraw();
  691. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  692. resized = true;
  693. } else {
  694. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  695. }
  696. }
  697. break;
  698. case KeyPress:
  699. on_keypress(&ev.xkey);
  700. break;
  701. case MotionNotify:
  702. if (mode == MODE_IMAGE) {
  703. win_set_cursor(&win, CURSOR_ARROW);
  704. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  705. }
  706. break;
  707. }
  708. }
  709. }
  710. int fncmp(const void *a, const void *b)
  711. {
  712. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  713. }
  714. int main(int argc, char **argv)
  715. {
  716. int i, start;
  717. size_t n;
  718. ssize_t len;
  719. char *filename;
  720. const char *homedir, *dsuffix = "";
  721. struct stat fstats;
  722. r_dir_t dir;
  723. signal(SIGPIPE, SIG_IGN);
  724. parse_options(argc, argv);
  725. if (options->clean_cache) {
  726. tns_init(&tns, NULL, NULL, NULL, NULL);
  727. tns_clean_cache(&tns);
  728. exit(EXIT_SUCCESS);
  729. }
  730. if (options->filecnt == 0 && !options->from_stdin) {
  731. print_usage();
  732. exit(EXIT_FAILURE);
  733. }
  734. if (options->recursive || options->from_stdin)
  735. filecnt = FILENAME_CNT;
  736. else
  737. filecnt = options->filecnt;
  738. files = s_malloc(filecnt * sizeof(*files));
  739. memset(files, 0, filecnt * sizeof(*files));
  740. fileidx = 0;
  741. if (options->from_stdin) {
  742. n = 0;
  743. filename = NULL;
  744. while ((len = getline(&filename, &n, stdin)) > 0) {
  745. if (filename[len-1] == '\n')
  746. filename[len-1] = '\0';
  747. check_add_file(filename, true);
  748. }
  749. free(filename);
  750. }
  751. for (i = 0; i < options->filecnt; i++) {
  752. filename = options->filenames[i];
  753. if (stat(filename, &fstats) < 0) {
  754. warn("could not stat file: %s", filename);
  755. continue;
  756. }
  757. if (!S_ISDIR(fstats.st_mode)) {
  758. check_add_file(filename, true);
  759. } else {
  760. if (!options->recursive) {
  761. warn("ignoring directory: %s", filename);
  762. continue;
  763. }
  764. if (r_opendir(&dir, filename) < 0) {
  765. warn("could not open directory: %s", filename);
  766. continue;
  767. }
  768. start = fileidx;
  769. while ((filename = r_readdir(&dir)) != NULL) {
  770. check_add_file(filename, false);
  771. free((void*) filename);
  772. }
  773. r_closedir(&dir);
  774. if (fileidx - start > 1)
  775. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  776. }
  777. }
  778. if (fileidx == 0) {
  779. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  780. exit(EXIT_FAILURE);
  781. }
  782. filecnt = fileidx;
  783. fileidx = options->startnum < filecnt ? options->startnum : 0;
  784. win_init(&win);
  785. img_init(&img, &win);
  786. if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
  787. homedir = getenv("HOME");
  788. dsuffix = "/.config";
  789. }
  790. if (homedir != NULL) {
  791. char **cmd[] = { &info.cmd, &keyhandler.cmd };
  792. const char *name[] = { "image-info", "key-handler" };
  793. for (i = 0; i < ARRLEN(cmd); i++) {
  794. n = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
  795. *cmd[i] = (char*) s_malloc(n);
  796. snprintf(*cmd[i], n, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
  797. if (access(*cmd[i], X_OK) != 0) {
  798. free(*cmd[i]);
  799. *cmd[i] = NULL;
  800. }
  801. }
  802. } else {
  803. warn("could not locate exec directory");
  804. }
  805. info.fd = -1;
  806. if (options->thumb_mode) {
  807. mode = MODE_THUMB;
  808. tns_init(&tns, files, &filecnt, &fileidx, &win);
  809. while (!tns_load(&tns, fileidx, false, false))
  810. remove_file(fileidx, false);
  811. } else {
  812. mode = MODE_IMAGE;
  813. tns.thumbs = NULL;
  814. load_image(fileidx);
  815. }
  816. win_open(&win);
  817. win_set_cursor(&win, CURSOR_WATCH);
  818. set_timeout(redraw, 25, false);
  819. run();
  820. cleanup();
  821. return 0;
  822. }