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.
 
 
 
 
 
 

623 lines
12 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. tns_init(&tns, filecnt);
  111. if (options->thumbnails == 2) {
  112. mode = MODE_THUMBS;
  113. win_clear(&win);
  114. win_draw(&win);
  115. } else {
  116. mode = MODE_NORMAL;
  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 ? fileidx + 1 : 0, tns.cnt,
  132. tns.cnt ? filenames[fileidx] : "");
  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. int timeout;
  216. int mox, moy;
  217. void redraw() {
  218. if (mode == MODE_NORMAL)
  219. img_render(&img, &win);
  220. else
  221. tns_render(&tns, &win);
  222. update_title();
  223. timeout = 0;
  224. }
  225. void on_keypress(XKeyEvent *kev) {
  226. int x, y;
  227. unsigned int w, h;
  228. char key;
  229. KeySym ksym;
  230. int changed;
  231. if (!kev)
  232. return;
  233. XLookupString(kev, &key, 1, &ksym, NULL);
  234. changed = 0;
  235. if (mode == MODE_NORMAL) {
  236. switch (ksym) {
  237. /* navigate image list */
  238. case XK_n:
  239. case XK_space:
  240. if (fileidx + 1 < filecnt) {
  241. ++fileidx;
  242. changed = load_image();
  243. }
  244. break;
  245. case XK_p:
  246. case XK_BackSpace:
  247. if (fileidx > 0) {
  248. --fileidx;
  249. changed = load_image();
  250. }
  251. break;
  252. case XK_bracketleft:
  253. if (fileidx != 0) {
  254. fileidx = MAX(0, fileidx - 10);
  255. changed = load_image();
  256. }
  257. break;
  258. case XK_bracketright:
  259. if (fileidx != filecnt - 1) {
  260. fileidx = MIN(fileidx + 10, filecnt - 1);
  261. changed = load_image();
  262. }
  263. break;
  264. /* zooming */
  265. case XK_plus:
  266. case XK_equal:
  267. changed = img_zoom_in(&img);
  268. break;
  269. case XK_minus:
  270. changed = img_zoom_out(&img);
  271. break;
  272. case XK_0:
  273. changed = img_zoom(&img, 1.0);
  274. break;
  275. case XK_w:
  276. if ((changed = img_fit_win(&img, &win)))
  277. img_center(&img, &win);
  278. break;
  279. /* panning */
  280. case XK_h:
  281. case XK_Left:
  282. changed = img_pan(&img, &win, PAN_LEFT);
  283. break;
  284. case XK_j:
  285. case XK_Down:
  286. changed = img_pan(&img, &win, PAN_DOWN);
  287. break;
  288. case XK_k:
  289. case XK_Up:
  290. changed = img_pan(&img, &win, PAN_UP);
  291. break;
  292. case XK_l:
  293. case XK_Right:
  294. changed = img_pan(&img, &win, PAN_RIGHT);
  295. break;
  296. /* rotation */
  297. case XK_less:
  298. img_rotate_left(&img, &win);
  299. changed = 1;
  300. break;
  301. case XK_greater:
  302. img_rotate_right(&img, &win);
  303. changed = 1;
  304. break;
  305. /* control window */
  306. case XK_W:
  307. x = MAX(0, win.x + img.x);
  308. y = MAX(0, win.y + img.y);
  309. w = img.w * img.zoom;
  310. h = img.h * img.zoom;
  311. if ((changed = win_moveresize(&win, x, y, w, h))) {
  312. img.x = x - win.x;
  313. img.y = y - win.y;
  314. }
  315. break;
  316. /* switch to thumnail mode */
  317. case XK_Return:
  318. if (options->thumbnails) {
  319. mode = MODE_THUMBS;
  320. changed = tns.dirty = 1;
  321. }
  322. break;
  323. /* miscellaneous */
  324. case XK_a:
  325. img_toggle_antialias(&img);
  326. changed = 1;
  327. break;
  328. case XK_r:
  329. changed = load_image();
  330. break;
  331. }
  332. } else {
  333. /* thumbnail mode */
  334. switch (ksym) {
  335. /* open selected image */
  336. case XK_Return:
  337. load_image();
  338. mode = MODE_NORMAL;
  339. win_set_cursor(&win, CURSOR_ARROW);
  340. changed = 1;
  341. break;
  342. /* move selection */
  343. case XK_h:
  344. case XK_Left:
  345. changed = tns_move_selection(&tns, &win, TNS_LEFT);
  346. break;
  347. case XK_j:
  348. case XK_Down:
  349. changed = tns_move_selection(&tns, &win, TNS_DOWN);
  350. break;
  351. case XK_k:
  352. case XK_Up:
  353. changed = tns_move_selection(&tns, &win, TNS_UP);
  354. break;
  355. case XK_l:
  356. case XK_Right:
  357. changed = tns_move_selection(&tns, &win, TNS_RIGHT);
  358. break;
  359. }
  360. }
  361. /* common key mappings */
  362. switch (ksym) {
  363. case XK_Escape:
  364. cleanup();
  365. exit(2);
  366. case XK_q:
  367. cleanup();
  368. exit(0);
  369. case XK_g:
  370. if (fileidx != 0) {
  371. fileidx = 0;
  372. changed = 1;
  373. if (mode == MODE_NORMAL)
  374. load_image();
  375. else
  376. tns.dirty = 1;
  377. }
  378. break;
  379. case XK_G:
  380. if (fileidx != filecnt - 1) {
  381. fileidx = filecnt - 1;
  382. changed = 1;
  383. if (mode == MODE_NORMAL)
  384. load_image();
  385. else
  386. tns.dirty = 1;
  387. }
  388. break;
  389. case XK_f:
  390. win_toggle_fullscreen(&win);
  391. /* render on next configurenotify */
  392. break;
  393. }
  394. if (changed)
  395. redraw();
  396. }
  397. void on_buttonpress(XButtonEvent *bev) {
  398. int changed, sel;
  399. unsigned int mask;
  400. if (!bev)
  401. return;
  402. mask = CLEANMASK(bev->state);
  403. changed = 0;
  404. if (mode == MODE_NORMAL) {
  405. switch (bev->button) {
  406. case Button1:
  407. if (fileidx + 1 < filecnt) {
  408. ++fileidx;
  409. changed = load_image();
  410. }
  411. break;
  412. case Button2:
  413. mox = bev->x;
  414. moy = bev->y;
  415. win_set_cursor(&win, CURSOR_HAND);
  416. break;
  417. case Button3:
  418. if (fileidx > 0) {
  419. --fileidx;
  420. changed = load_image();
  421. }
  422. break;
  423. case Button4:
  424. if (mask == ControlMask)
  425. changed = img_zoom_in(&img);
  426. else if (mask == ShiftMask)
  427. changed = img_pan(&img, &win, PAN_LEFT);
  428. else
  429. changed = img_pan(&img, &win, PAN_UP);
  430. break;
  431. case Button5:
  432. if (mask == ControlMask)
  433. changed = img_zoom_out(&img);
  434. else if (mask == ShiftMask)
  435. changed = img_pan(&img, &win, PAN_RIGHT);
  436. else
  437. changed = img_pan(&img, &win, PAN_DOWN);
  438. break;
  439. case 6:
  440. changed = img_pan(&img, &win, PAN_LEFT);
  441. break;
  442. case 7:
  443. changed = img_pan(&img, &win, PAN_RIGHT);
  444. break;
  445. }
  446. } else {
  447. /* thumbnail mode */
  448. switch (bev->button) {
  449. case Button1:
  450. if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
  451. if (sel == fileidx) {
  452. load_image();
  453. mode = MODE_NORMAL;
  454. win_set_cursor(&win, CURSOR_ARROW);
  455. } else {
  456. tns_highlight(&tns, &win, fileidx, False);
  457. tns_highlight(&tns, &win, sel, True);
  458. fileidx = sel;
  459. }
  460. changed = 1;
  461. break;
  462. }
  463. break;
  464. case Button4:
  465. changed = tns_scroll(&tns, TNS_UP);
  466. break;
  467. case Button5:
  468. changed = tns_scroll(&tns, TNS_DOWN);
  469. break;
  470. }
  471. }
  472. if (changed)
  473. redraw();
  474. }
  475. void on_motionnotify(XMotionEvent *mev) {
  476. if (!mev || mode != MODE_NORMAL)
  477. return;
  478. if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
  479. if (img_move(&img, &win, mev->x - mox, mev->y - moy))
  480. timeout = 1000;
  481. mox = mev->x;
  482. moy = mev->y;
  483. }
  484. }
  485. void run() {
  486. int xfd;
  487. fd_set fds;
  488. struct timeval t, t0;
  489. XEvent ev;
  490. timeout = 0;
  491. while (1) {
  492. if (mode == MODE_THUMBS && tns.cnt < filecnt) {
  493. win_set_cursor(&win, CURSOR_WATCH);
  494. gettimeofday(&t0, 0);
  495. while (!XPending(win.env.dpy) && tns.cnt < filecnt) {
  496. tns_load(&tns, &win, filenames[tns.cnt]);
  497. gettimeofday(&t, 0);
  498. if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
  499. break;
  500. }
  501. if (tns.cnt == filecnt)
  502. win_set_cursor(&win, CURSOR_ARROW);
  503. if (!XPending(win.env.dpy)) {
  504. redraw();
  505. continue;
  506. } else {
  507. timeout = 75000;
  508. }
  509. } else if (timeout) {
  510. t.tv_sec = 0;
  511. t.tv_usec = timeout;
  512. xfd = ConnectionNumber(win.env.dpy);
  513. FD_ZERO(&fds);
  514. FD_SET(xfd, &fds);
  515. if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
  516. redraw();
  517. }
  518. if (!XNextEvent(win.env.dpy, &ev)) {
  519. switch (ev.type) {
  520. case KeyPress:
  521. on_keypress(&ev.xkey);
  522. break;
  523. case ButtonPress:
  524. on_buttonpress(&ev.xbutton);
  525. break;
  526. case ButtonRelease:
  527. if (ev.xbutton.button == Button2)
  528. win_set_cursor(&win, CURSOR_ARROW);
  529. break;
  530. case MotionNotify:
  531. on_motionnotify(&ev.xmotion);
  532. break;
  533. case ConfigureNotify:
  534. if (win_configure(&win, &ev.xconfigure)) {
  535. timeout = 75000;
  536. if (mode == MODE_NORMAL)
  537. img.checkpan = 1;
  538. else
  539. tns.dirty = 1;
  540. }
  541. break;
  542. case ClientMessage:
  543. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  544. return;
  545. break;
  546. }
  547. }
  548. }
  549. }