A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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