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.
 
 
 
 
 
 

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