A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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