A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

919 lines
20 KiB

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