A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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