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.
 
 
 
 
 
 

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