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.
 
 
 
 
 
 

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