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.
 
 
 
 
 
 

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