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.
 
 
 
 
 
 

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