A Simple X Image Viewer
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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