A Simple X Image Viewer
 
 
 
 
 
 

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