A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

main.c 20 KiB

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