A Simple X Image Viewer
 
 
 
 
 
 

764 lines
16 KiB

  1. /* sxiv: main.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/select.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. #include <X11/Xlib.h>
  27. #include <X11/Xutil.h>
  28. #include <X11/keysym.h>
  29. #include "config.h"
  30. #include "image.h"
  31. #include "options.h"
  32. #include "thumbs.h"
  33. #include "util.h"
  34. #include "window.h"
  35. #ifdef EXT_COMMANDS
  36. #include "commands.h"
  37. #endif
  38. typedef enum {
  39. MODE_NORMAL = 0,
  40. MODE_THUMBS
  41. } appmode_t;
  42. void update_title();
  43. int check_append(const char*);
  44. void run();
  45. appmode_t mode;
  46. img_t img;
  47. tns_t tns;
  48. win_t win;
  49. #define FNAME_CNT 1024
  50. const char **filenames;
  51. int filecnt, fileidx;
  52. size_t filesize;
  53. #define TITLE_LEN 256
  54. char win_title[TITLE_LEN];
  55. void cleanup() {
  56. static int in = 0;
  57. if (!in++) {
  58. img_close(&img, 0);
  59. img_free(&img);
  60. tns_free(&tns, &win);
  61. win_close(&win);
  62. }
  63. }
  64. int load_image(int new) {
  65. int ret = 0;
  66. struct stat fstats;
  67. if (new >= 0 && new < filecnt) {
  68. win_set_cursor(&win, CURSOR_WATCH);
  69. img_close(&img, 0);
  70. fileidx = new;
  71. if (!stat(filenames[fileidx], &fstats))
  72. filesize = fstats.st_size;
  73. else
  74. filesize = 0;
  75. if (!(ret = img_load(&img, filenames[fileidx])))
  76. win_set_cursor(&win, CURSOR_NONE);
  77. }
  78. return ret;
  79. }
  80. int fncmp(const void *a, const void *b) {
  81. return strcoll(*((char* const*) a), *((char* const*) b));
  82. }
  83. int main(int argc, char **argv) {
  84. int i, start;
  85. const char *filename;
  86. struct stat fstats;
  87. r_dir_t dir;
  88. parse_options(argc, argv);
  89. if (options->clean_cache) {
  90. tns_init(&tns, 0);
  91. tns_clear_cache(&tns);
  92. exit(0);
  93. }
  94. if (!options->filecnt) {
  95. print_usage();
  96. exit(1);
  97. }
  98. if (options->recursive || options->from_stdin)
  99. filecnt = FNAME_CNT;
  100. else
  101. filecnt = options->filecnt;
  102. filenames = (const char**) s_malloc(filecnt * sizeof(const char*));
  103. fileidx = 0;
  104. if (options->from_stdin) {
  105. while ((filename = readline(stdin))) {
  106. if (!*filename || !check_append(filename))
  107. free((void*) filename);
  108. }
  109. } else {
  110. for (i = 0; i < options->filecnt; ++i) {
  111. filename = options->filenames[i];
  112. if (stat(filename, &fstats) || !S_ISDIR(fstats.st_mode)) {
  113. check_append(filename);
  114. } else {
  115. if (!options->recursive) {
  116. warn("ignoring directory: %s", filename);
  117. continue;
  118. }
  119. if (r_opendir(&dir, filename)) {
  120. warn("could not open directory: %s", filename);
  121. continue;
  122. }
  123. start = fileidx;
  124. while ((filename = r_readdir(&dir))) {
  125. if (!check_append(filename))
  126. free((void*) filename);
  127. }
  128. r_closedir(&dir);
  129. if (fileidx - start > 1)
  130. qsort(filenames + start, fileidx - start, sizeof(char*), fncmp);
  131. }
  132. }
  133. }
  134. filecnt = fileidx;
  135. fileidx = 0;
  136. if (!filecnt) {
  137. fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
  138. exit(1);
  139. }
  140. win_open(&win);
  141. img_init(&img, &win);
  142. if (options->thumbnails) {
  143. mode = MODE_THUMBS;
  144. tns_init(&tns, filecnt);
  145. win_clear(&win);
  146. win_draw(&win);
  147. } else {
  148. mode = MODE_NORMAL;
  149. tns.thumbs = NULL;
  150. load_image(fileidx);
  151. img_render(&img, &win);
  152. }
  153. update_title();
  154. run();
  155. cleanup();
  156. return 0;
  157. }
  158. void update_title() {
  159. int n;
  160. float size;
  161. const char *unit;
  162. if (mode == MODE_THUMBS) {
  163. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] %s",
  164. tns.cnt ? tns.sel + 1 : 0, tns.cnt,
  165. tns.cnt ? filenames[tns.sel] : "");
  166. } else {
  167. if (img.im) {
  168. size = filesize;
  169. size_readable(&size, &unit);
  170. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s",
  171. fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit,
  172. filenames[fileidx]);
  173. } else {
  174. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] invalid: %s",
  175. fileidx + 1, filecnt, filenames[fileidx]);
  176. }
  177. }
  178. if (n >= TITLE_LEN) {
  179. win_title[TITLE_LEN - 2] = '.';
  180. win_title[TITLE_LEN - 3] = '.';
  181. win_title[TITLE_LEN - 4] = '.';
  182. }
  183. win_set_title(&win, win_title);
  184. }
  185. int check_append(const char *filename) {
  186. if (!filename)
  187. return 0;
  188. if (access(filename, R_OK)) {
  189. warn("could not open file: %s", filename);
  190. return 0;
  191. } else if (options->all || img_check(filename)) {
  192. if (fileidx == filecnt) {
  193. filecnt *= 2;
  194. filenames = (const char**) s_realloc(filenames,
  195. filecnt * sizeof(const char*));
  196. }
  197. filenames[fileidx++] = filename;
  198. return 1;
  199. } else {
  200. return 0;
  201. }
  202. }
  203. #ifdef EXT_COMMANDS
  204. int run_command(const char *cline, Bool reload) {
  205. int fncnt, fnlen;
  206. char *cn, *cmdline;
  207. const char *co, *fname;
  208. pid_t pid;
  209. int ret, status;
  210. if (!cline || !*cline)
  211. return 0;
  212. fncnt = 0;
  213. co = cline - 1;
  214. while ((co = strchr(co + 1, '#')))
  215. ++fncnt;
  216. if (!fncnt)
  217. return 0;
  218. ret = 0;
  219. fname = filenames[mode == MODE_NORMAL ? fileidx : tns.sel];
  220. fnlen = strlen(fname);
  221. cn = cmdline = (char*) s_malloc((strlen(cline) + fncnt * (fnlen + 2)) *
  222. sizeof(char));
  223. /* replace all '#' with filename */
  224. for (co = cline; *co; ++co) {
  225. if (*co == '#') {
  226. *cn++ = '"';
  227. strcpy(cn, fname);
  228. cn += fnlen;
  229. *cn++ = '"';
  230. } else {
  231. *cn++ = *co;
  232. }
  233. }
  234. *cn = '\0';
  235. if ((pid = fork()) == 0) {
  236. execlp("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
  237. warn("could not exec: /bin/sh");
  238. exit(1);
  239. } else if (pid < 0) {
  240. warn("could not fork. command line was: %s", cmdline);
  241. } else if (reload) {
  242. waitpid(pid, &status, 0);
  243. if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
  244. ret = 1;
  245. else
  246. warn("child exited with non-zero return value: %d. command line was: %s",
  247. WEXITSTATUS(status), cmdline);
  248. }
  249. free(cmdline);
  250. return ret;
  251. }
  252. #endif /* EXT_COMMANDS */
  253. /* event handling */
  254. #define TO_WIN_RESIZE 75000;
  255. #define TO_IMAGE_DRAG 1000;
  256. #define TO_CURSOR_HIDE 1500000;
  257. #define TO_THUMBS_LOAD 75000;
  258. int timo_cursor;
  259. int timo_redraw;
  260. unsigned char drag;
  261. int mox, moy;
  262. void redraw() {
  263. if (mode == MODE_NORMAL) {
  264. img_render(&img, &win);
  265. if (timo_cursor)
  266. win_set_cursor(&win, CURSOR_ARROW);
  267. else if (!drag)
  268. win_set_cursor(&win, CURSOR_NONE);
  269. } else {
  270. tns_render(&tns, &win);
  271. }
  272. update_title();
  273. timo_redraw = 0;
  274. }
  275. void on_keypress(XKeyEvent *kev) {
  276. int i, x, y;
  277. unsigned int w, h;
  278. char key;
  279. KeySym ksym;
  280. int changed;
  281. if (!kev)
  282. return;
  283. XLookupString(kev, &key, 1, &ksym, NULL);
  284. changed = 0;
  285. #ifdef EXT_COMMANDS
  286. /* external commands from commands.h */
  287. if (CLEANMASK(kev->state) & ControlMask) {
  288. for (i = 0; i < LEN(commands); ++i) {
  289. if (commands[i].ksym == ksym) {
  290. win_set_cursor(&win, CURSOR_WATCH);
  291. if (run_command(commands[i].cmdline, commands[i].reload)) {
  292. if (mode == MODE_NORMAL) {
  293. img_close(&img, 1);
  294. load_image(fileidx);
  295. tns_load(&tns, &win, fileidx, filenames[fileidx]);
  296. } else {
  297. tns_load(&tns, &win, tns.sel, filenames[tns.sel]);
  298. }
  299. redraw();
  300. }
  301. if (mode == MODE_THUMBS)
  302. win_set_cursor(&win, CURSOR_ARROW);
  303. return;
  304. }
  305. }
  306. }
  307. #endif
  308. if (mode == MODE_NORMAL) {
  309. switch (ksym) {
  310. /* navigate image list */
  311. case XK_n:
  312. case XK_space:
  313. if (fileidx + 1 < filecnt)
  314. changed = load_image(fileidx + 1);
  315. break;
  316. case XK_p:
  317. case XK_BackSpace:
  318. if (fileidx > 0)
  319. changed = load_image(fileidx - 1);
  320. break;
  321. case XK_bracketleft:
  322. if (fileidx != 0)
  323. changed = load_image(MAX(0, fileidx - 10));
  324. break;
  325. case XK_bracketright:
  326. if (fileidx != filecnt - 1)
  327. changed = load_image(MIN(fileidx + 10, filecnt - 1));
  328. break;
  329. case XK_g:
  330. if (fileidx != 0)
  331. changed = load_image(0);
  332. break;
  333. case XK_G:
  334. if (fileidx != filecnt - 1)
  335. changed = load_image(filecnt - 1);
  336. break;
  337. /* zooming */
  338. case XK_plus:
  339. case XK_equal:
  340. changed = img_zoom_in(&img);
  341. break;
  342. case XK_minus:
  343. changed = img_zoom_out(&img);
  344. break;
  345. case XK_0:
  346. changed = img_zoom(&img, 1.0);
  347. break;
  348. case XK_w:
  349. if ((changed = img_fit_win(&img, &win)))
  350. img_center(&img, &win);
  351. break;
  352. /* panning */
  353. case XK_h:
  354. case XK_Left:
  355. changed = img_pan(&img, &win, PAN_LEFT);
  356. break;
  357. case XK_j:
  358. case XK_Down:
  359. changed = img_pan(&img, &win, PAN_DOWN);
  360. break;
  361. case XK_k:
  362. case XK_Up:
  363. changed = img_pan(&img, &win, PAN_UP);
  364. break;
  365. case XK_l:
  366. case XK_Right:
  367. changed = img_pan(&img, &win, PAN_RIGHT);
  368. break;
  369. /* rotation */
  370. case XK_less:
  371. img_rotate_left(&img, &win);
  372. changed = 1;
  373. break;
  374. case XK_greater:
  375. img_rotate_right(&img, &win);
  376. changed = 1;
  377. break;
  378. /* control window */
  379. case XK_W:
  380. x = MAX(0, win.x + img.x);
  381. y = MAX(0, win.y + img.y);
  382. w = img.w * img.zoom;
  383. h = img.h * img.zoom;
  384. if ((changed = win_moveresize(&win, x, y, w, h))) {
  385. img.x = x - win.x;
  386. img.y = y - win.y;
  387. }
  388. break;
  389. /* switch to thumbnail mode */
  390. case XK_Return:
  391. if (!tns.thumbs)
  392. tns_init(&tns, filecnt);
  393. img_close(&img, 0);
  394. mode = MODE_THUMBS;
  395. win_set_cursor(&win, CURSOR_ARROW);
  396. timo_cursor = 0;
  397. tns.sel = fileidx;
  398. changed = tns.dirty = 1;
  399. break;
  400. /* miscellaneous */
  401. case XK_a:
  402. img_toggle_antialias(&img);
  403. changed = 1;
  404. break;
  405. case XK_A:
  406. img.alpha ^= 1;
  407. changed = 1;
  408. break;
  409. case XK_r:
  410. changed = load_image(fileidx);
  411. break;
  412. }
  413. } else {
  414. /* thumbnail mode */
  415. switch (ksym) {
  416. /* open selected image */
  417. case XK_Return:
  418. load_image(tns.sel);
  419. mode = MODE_NORMAL;
  420. changed = 1;
  421. break;
  422. /* move selection */
  423. case XK_h:
  424. case XK_Left:
  425. changed = tns_move_selection(&tns, &win, TNS_LEFT);
  426. break;
  427. case XK_j:
  428. case XK_Down:
  429. changed = tns_move_selection(&tns, &win, TNS_DOWN);
  430. break;
  431. case XK_k:
  432. case XK_Up:
  433. changed = tns_move_selection(&tns, &win, TNS_UP);
  434. break;
  435. case XK_l:
  436. case XK_Right:
  437. changed = tns_move_selection(&tns, &win, TNS_RIGHT);
  438. break;
  439. case XK_g:
  440. if (tns.sel != 0) {
  441. tns.sel = 0;
  442. changed = tns.dirty = 1;
  443. }
  444. break;
  445. case XK_G:
  446. if (tns.sel != tns.cnt - 1) {
  447. tns.sel = tns.cnt - 1;
  448. changed = tns.dirty = 1;
  449. }
  450. }
  451. }
  452. /* common key mappings */
  453. switch (ksym) {
  454. case XK_q:
  455. cleanup();
  456. exit(0);
  457. case XK_f:
  458. win_toggle_fullscreen(&win);
  459. /* render on next configurenotify */
  460. break;
  461. case XK_D:
  462. if (mode == MODE_THUMBS) {
  463. if (tns.sel >= tns.cnt)
  464. break;
  465. i = tns.sel;
  466. } else {
  467. i = fileidx;
  468. }
  469. if (filecnt == 1) {
  470. cleanup();
  471. exit(0);
  472. }
  473. if (i + 1 < filecnt)
  474. memmove(filenames + i, filenames + i + 1, (filecnt - i - 1) *
  475. sizeof(const char*));
  476. else if (fileidx)
  477. fileidx--;
  478. if (i + 1 < tns.cnt) {
  479. memmove(tns.thumbs + i, tns.thumbs + i + 1, (tns.cnt - i - 1) *
  480. sizeof(thumb_t));
  481. memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
  482. } else if (tns.sel) {
  483. tns.sel--;
  484. }
  485. filecnt--;
  486. if (mode == MODE_NORMAL)
  487. load_image(fileidx);
  488. if (i < tns.cnt)
  489. tns.cnt--;
  490. changed = tns.dirty = 1;
  491. break;
  492. }
  493. if (changed)
  494. redraw();
  495. }
  496. void on_buttonpress(XButtonEvent *bev) {
  497. int changed, sel;
  498. unsigned int mask;
  499. if (!bev)
  500. return;
  501. mask = CLEANMASK(bev->state);
  502. changed = 0;
  503. if (mode == MODE_NORMAL) {
  504. win_set_cursor(&win, CURSOR_ARROW);
  505. timo_cursor = TO_CURSOR_HIDE;
  506. switch (bev->button) {
  507. case Button1:
  508. if (fileidx + 1 < filecnt)
  509. changed = load_image(fileidx + 1);
  510. break;
  511. case Button2:
  512. mox = bev->x;
  513. moy = bev->y;
  514. win_set_cursor(&win, CURSOR_HAND);
  515. timo_cursor = 0;
  516. drag = 1;
  517. break;
  518. case Button3:
  519. if (fileidx > 0)
  520. changed = load_image(fileidx - 1);
  521. break;
  522. case Button4:
  523. if (mask == ControlMask)
  524. changed = img_zoom_in(&img);
  525. else if (mask == ShiftMask)
  526. changed = img_pan(&img, &win, PAN_LEFT);
  527. else
  528. changed = img_pan(&img, &win, PAN_UP);
  529. break;
  530. case Button5:
  531. if (mask == ControlMask)
  532. changed = img_zoom_out(&img);
  533. else if (mask == ShiftMask)
  534. changed = img_pan(&img, &win, PAN_RIGHT);
  535. else
  536. changed = img_pan(&img, &win, PAN_DOWN);
  537. break;
  538. case 6:
  539. changed = img_pan(&img, &win, PAN_LEFT);
  540. break;
  541. case 7:
  542. changed = img_pan(&img, &win, PAN_RIGHT);
  543. break;
  544. }
  545. } else {
  546. /* thumbnail mode */
  547. switch (bev->button) {
  548. case Button1:
  549. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  550. if (sel == tns.sel) {
  551. load_image(tns.sel);
  552. mode = MODE_NORMAL;
  553. timo_cursor = TO_CURSOR_HIDE;
  554. } else {
  555. tns_highlight(&tns, &win, tns.sel, False);
  556. tns_highlight(&tns, &win, sel, True);
  557. tns.sel = sel;
  558. }
  559. changed = 1;
  560. break;
  561. }
  562. break;
  563. case Button4:
  564. changed = tns_scroll(&tns, TNS_UP);
  565. break;
  566. case Button5:
  567. changed = tns_scroll(&tns, TNS_DOWN);
  568. break;
  569. }
  570. }
  571. if (changed)
  572. redraw();
  573. }
  574. void on_motionnotify(XMotionEvent *mev) {
  575. if (!mev)
  576. return;
  577. if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
  578. if (img_move(&img, &win, mev->x - mox, mev->y - moy))
  579. timo_redraw = TO_IMAGE_DRAG;
  580. mox = mev->x;
  581. moy = mev->y;
  582. }
  583. }
  584. void run() {
  585. int xfd, timeout;
  586. fd_set fds;
  587. struct timeval tt, t0, t1;
  588. XEvent ev;
  589. timo_cursor = timo_redraw = 0;
  590. drag = 0;
  591. while (1) {
  592. if (mode == MODE_THUMBS && tns.cnt < filecnt) {
  593. win_set_cursor(&win, CURSOR_WATCH);
  594. gettimeofday(&t0, 0);
  595. while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
  596. /* tns.cnt is increased inside tns_load */
  597. tns_load(&tns, &win, tns.cnt, filenames[tns.cnt]);
  598. gettimeofday(&t1, 0);
  599. if (TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0) >= 0.25)
  600. break;
  601. }
  602. if (tns.cnt == filecnt)
  603. win_set_cursor(&win, CURSOR_ARROW);
  604. if (!XPending(win.env.dpy)) {
  605. redraw();
  606. continue;
  607. } else {
  608. timo_redraw = TO_THUMBS_LOAD;
  609. }
  610. } else if (timo_cursor || timo_redraw) {
  611. gettimeofday(&t0, 0);
  612. if (timo_cursor && timo_redraw)
  613. timeout = MIN(timo_cursor, timo_redraw);
  614. else if (timo_cursor)
  615. timeout = timo_cursor;
  616. else
  617. timeout = timo_redraw;
  618. tt.tv_sec = timeout / 1000000;
  619. tt.tv_usec = timeout % 1000000;
  620. xfd = ConnectionNumber(win.env.dpy);
  621. FD_ZERO(&fds);
  622. FD_SET(xfd, &fds);
  623. if (!XPending(win.env.dpy))
  624. select(xfd + 1, &fds, 0, 0, &tt);
  625. gettimeofday(&t1, 0);
  626. timeout = MIN((TV_TO_DOUBLE(t1) - TV_TO_DOUBLE(t0)) * 1000000, timeout);
  627. /* timeouts fired? */
  628. if (timo_cursor) {
  629. timo_cursor = MAX(0, timo_cursor - timeout);
  630. if (!timo_cursor)
  631. win_set_cursor(&win, CURSOR_NONE);
  632. }
  633. if (timo_redraw) {
  634. timo_redraw = MAX(0, timo_redraw - timeout);
  635. if (!timo_redraw)
  636. redraw();
  637. }
  638. if (!XPending(win.env.dpy) && (timo_cursor || timo_redraw))
  639. continue;
  640. }
  641. if (!XNextEvent(win.env.dpy, &ev)) {
  642. switch (ev.type) {
  643. case KeyPress:
  644. on_keypress(&ev.xkey);
  645. break;
  646. case ButtonPress:
  647. on_buttonpress(&ev.xbutton);
  648. break;
  649. case ButtonRelease:
  650. if (ev.xbutton.button == Button2) {
  651. drag = 0;
  652. if (mode == MODE_NORMAL) {
  653. win_set_cursor(&win, CURSOR_ARROW);
  654. timo_cursor = TO_CURSOR_HIDE;
  655. }
  656. }
  657. break;
  658. case MotionNotify:
  659. if (drag) {
  660. on_motionnotify(&ev.xmotion);
  661. } else if (mode == MODE_NORMAL) {
  662. if (!timo_cursor)
  663. win_set_cursor(&win, CURSOR_ARROW);
  664. timo_cursor = TO_CURSOR_HIDE;
  665. }
  666. break;
  667. case ConfigureNotify:
  668. if (win_configure(&win, &ev.xconfigure)) {
  669. timo_redraw = TO_WIN_RESIZE;
  670. if (mode == MODE_NORMAL)
  671. img.checkpan = 1;
  672. else
  673. tns.dirty = 1;
  674. }
  675. break;
  676. case ClientMessage:
  677. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  678. return;
  679. break;
  680. }
  681. }
  682. }
  683. }