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.

main.c 20 KiB

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