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.
 
 
 
 
 
 

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