A Simple X Image Viewer
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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