A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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