A Simple X Image Viewer
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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