A Simple X Image Viewer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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