A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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