A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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