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.
 
 
 
 
 
 

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