A Simple X Image Viewer
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

899 lines
20 KiB

  1. /* Copyright 2011-2013 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _MAPPINGS_CONFIG
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <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 && 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. if (new < 0 || new >= filecnt)
  278. return;
  279. win_set_cursor(&win, CURSOR_WATCH);
  280. reset_timeout(slideshow);
  281. if (new != fileidx)
  282. alternate = fileidx;
  283. img_close(&img, false);
  284. while (!img_load(&img, &files[new])) {
  285. remove_file(new, false);
  286. if (new >= filecnt)
  287. new = filecnt - 1;
  288. else if (new > 0 && new < fileidx)
  289. new--;
  290. }
  291. files[new].loaded = true;
  292. fileidx = new;
  293. info.open = false;
  294. open_info();
  295. if (img.multi.cnt > 0 && img.multi.animate)
  296. set_timeout(animate, img.multi.frames[img.multi.sel].delay, true);
  297. else
  298. reset_timeout(animate);
  299. }
  300. void update_info(void)
  301. {
  302. unsigned int i, fn, fw, n;
  303. unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
  304. char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
  305. const char * mark;
  306. bool ow_info;
  307. for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
  308. /* update window title */
  309. if (mode == MODE_THUMB) {
  310. win_set_title(&win, "sxiv");
  311. } else {
  312. snprintf(title, sizeof(title), "sxiv - %s", files[fileidx].name);
  313. win_set_title(&win, title);
  314. }
  315. /* update bar contents */
  316. if (win.bar.h == 0)
  317. return;
  318. mark = files[fileidx].marked ? "+ " : "";
  319. if (mode == MODE_THUMB) {
  320. if (tns.loadnext >= filecnt) {
  321. n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
  322. ow_info = true;
  323. } else {
  324. snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.loadnext, filecnt);
  325. rt[0] = '\0';
  326. ow_info = false;
  327. }
  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 < filecnt)
  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, false)) {
  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.thumbs[finfo[i].fn].loaded = false;
  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, reload, to_set;
  599. XEvent ev, nextev;
  600. set_timeout(redraw, 25, false);
  601. while (true) {
  602. while (mode == MODE_THUMB && tns.loadnext < filecnt &&
  603. XPending(win.env.dpy) == 0)
  604. {
  605. /* load thumbnails */
  606. reload = tns.loadnext != tns.cnt;
  607. set_timeout(redraw, TO_REDRAW_THUMBS, false);
  608. if (tns_load(&tns, tns.loadnext, reload)) {
  609. if (!reload)
  610. tns.cnt++;
  611. } else {
  612. remove_file(tns.loadnext, false);
  613. if (reload)
  614. tns.dirty = true;
  615. }
  616. while (tns.loadnext < filecnt && tns.thumbs[tns.loadnext].loaded)
  617. tns.loadnext++;
  618. if (tns.loadnext >= filecnt)
  619. redraw();
  620. else
  621. check_timeouts(NULL);
  622. }
  623. while (XPending(win.env.dpy) == 0
  624. && ((to_set = check_timeouts(&timeout)) || info.fd != -1))
  625. {
  626. /* check for timeouts & input */
  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. do {
  639. XNextEvent(win.env.dpy, &ev);
  640. discard = false;
  641. if (XEventsQueued(win.env.dpy, QueuedAlready) > 0) {
  642. XPeekEvent(win.env.dpy, &nextev);
  643. switch (ev.type) {
  644. case ConfigureNotify:
  645. discard = ev.type == nextev.type;
  646. break;
  647. case KeyPress:
  648. discard = (nextev.type == KeyPress || nextev.type == KeyRelease)
  649. && ev.xkey.keycode == nextev.xkey.keycode;
  650. break;
  651. }
  652. }
  653. } while (discard);
  654. switch (ev.type) {
  655. /* handle events */
  656. case ButtonPress:
  657. on_buttonpress(&ev.xbutton);
  658. break;
  659. case ClientMessage:
  660. if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
  661. return;
  662. break;
  663. case ConfigureNotify:
  664. if (win_configure(&win, &ev.xconfigure)) {
  665. if (mode == MODE_IMAGE) {
  666. img.dirty = true;
  667. img.checkpan = true;
  668. } else {
  669. tns.dirty = true;
  670. }
  671. if (!resized || win.fullscreen) {
  672. redraw();
  673. set_timeout(clear_resize, TO_REDRAW_RESIZE, false);
  674. resized = true;
  675. } else {
  676. set_timeout(redraw, TO_REDRAW_RESIZE, false);
  677. }
  678. }
  679. break;
  680. case KeyPress:
  681. on_keypress(&ev.xkey);
  682. break;
  683. case MotionNotify:
  684. if (mode == MODE_IMAGE) {
  685. win_set_cursor(&win, CURSOR_ARROW);
  686. set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
  687. }
  688. break;
  689. }
  690. }
  691. }
  692. int fncmp(const void *a, const void *b)
  693. {
  694. return strcoll(((fileinfo_t*) a)->name, ((fileinfo_t*) b)->name);
  695. }
  696. int main(int argc, char **argv)
  697. {
  698. int i, start;
  699. size_t n;
  700. ssize_t len;
  701. char *filename;
  702. const char *homedir, *dsuffix = "";
  703. struct stat fstats;
  704. r_dir_t dir;
  705. parse_options(argc, argv);
  706. if (options->clean_cache) {
  707. tns_init(&tns, NULL, 0, NULL, NULL);
  708. tns_clean_cache(&tns);
  709. exit(EXIT_SUCCESS);
  710. }
  711. if (options->filecnt == 0 && !options->from_stdin) {
  712. print_usage();
  713. exit(EXIT_FAILURE);
  714. }
  715. if (options->recursive || options->from_stdin)
  716. filecnt = FILENAME_CNT;
  717. else
  718. filecnt = options->filecnt;
  719. files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t));
  720. fileidx = 0;
  721. if (options->from_stdin) {
  722. filename = NULL;
  723. while ((len = get_line(&filename, &n, stdin)) > 0) {
  724. if (filename[len-1] == '\n')
  725. filename[len-1] = '\0';
  726. check_add_file(filename);
  727. }
  728. if (filename != NULL)
  729. free(filename);
  730. }
  731. for (i = 0; i < options->filecnt; i++) {
  732. filename = options->filenames[i];
  733. if (stat(filename, &fstats) < 0) {
  734. warn("could not stat file: %s", filename);
  735. continue;
  736. }
  737. if (!S_ISDIR(fstats.st_mode)) {
  738. check_add_file(filename);
  739. } else {
  740. if (!options->recursive) {
  741. warn("ignoring directory: %s", filename);
  742. continue;
  743. }
  744. if (r_opendir(&dir, filename) < 0) {
  745. warn("could not open directory: %s", filename);
  746. continue;
  747. }
  748. start = fileidx;
  749. while ((filename = r_readdir(&dir)) != NULL) {
  750. check_add_file(filename);
  751. free((void*) filename);
  752. }
  753. r_closedir(&dir);
  754. if (fileidx - start > 1)
  755. qsort(files + start, fileidx - start, sizeof(fileinfo_t), fncmp);
  756. }
  757. }
  758. if (fileidx == 0) {
  759. fprintf(stderr, "sxiv: no valid image file given, aborting\n");
  760. exit(EXIT_FAILURE);
  761. }
  762. filecnt = fileidx;
  763. fileidx = options->startnum < filecnt ? options->startnum : 0;
  764. win_init(&win);
  765. img_init(&img, &win);
  766. if ((homedir = getenv("XDG_CONFIG_HOME")) == NULL || homedir[0] == '\0') {
  767. homedir = getenv("HOME");
  768. dsuffix = "/.config";
  769. }
  770. if (homedir != NULL) {
  771. char **cmd[] = { &info.cmd, &keyhandler.cmd };
  772. const char *name[] = { "image-info", "key-handler" };
  773. for (i = 0; i < ARRLEN(cmd); i++) {
  774. len = strlen(homedir) + strlen(dsuffix) + strlen(name[i]) + 12;
  775. *cmd[i] = (char*) s_malloc(len);
  776. snprintf(*cmd[i], len, "%s%s/sxiv/exec/%s", homedir, dsuffix, name[i]);
  777. if (access(*cmd[i], X_OK) != 0) {
  778. free(*cmd[i]);
  779. *cmd[i] = NULL;
  780. }
  781. }
  782. } else {
  783. warn("could not locate exec directory");
  784. }
  785. info.fd = -1;
  786. if (options->thumb_mode) {
  787. mode = MODE_THUMB;
  788. tns_init(&tns, files, filecnt, &fileidx, &win);
  789. while (!tns_load(&tns, 0, false))
  790. remove_file(0, false);
  791. tns.cnt = 1;
  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. run();
  800. cleanup();
  801. return 0;
  802. }