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.
 
 
 
 
 
 

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