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.
 
 
 
 
 
 

546 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. switch (ksym) {
  239. case XK_Escape:
  240. cleanup();
  241. exit(2);
  242. case XK_q:
  243. cleanup();
  244. exit(0);
  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_f:
  327. win_toggle_fullscreen(&win);
  328. /* render on next configurenotify */
  329. break;
  330. case XK_W:
  331. x = win.x + img.x;
  332. y = win.y + img.y;
  333. w = img.w * img.zoom;
  334. h = img.h * img.zoom;
  335. if ((changed = win_moveresize(&win, x, y, w, h))) {
  336. img.x = x - win.x;
  337. img.y = y - win.y;
  338. }
  339. break;
  340. /* miscellaneous */
  341. case XK_a:
  342. img_toggle_antialias(&img);
  343. changed = 1;
  344. break;
  345. case XK_r:
  346. changed = load_image();
  347. break;
  348. }
  349. if (changed)
  350. redraw();
  351. }
  352. void on_buttonpress(XButtonEvent *bev) {
  353. int changed;
  354. unsigned int mask;
  355. if (!bev)
  356. return;
  357. mask = CLEANMASK(bev->state);
  358. changed = 0;
  359. switch (bev->button) {
  360. case Button1:
  361. if (fileidx + 1 < filecnt) {
  362. ++fileidx;
  363. changed = load_image();
  364. }
  365. break;
  366. case Button2:
  367. mox = bev->x;
  368. moy = bev->y;
  369. win_set_cursor(&win, CURSOR_HAND);
  370. break;
  371. case Button3:
  372. if (fileidx > 0) {
  373. --fileidx;
  374. changed = load_image();
  375. }
  376. break;
  377. case Button4:
  378. if (mask == ControlMask)
  379. changed = img_zoom_in(&img);
  380. else if (mask == ShiftMask)
  381. changed = img_pan(&img, &win, PAN_LEFT);
  382. else
  383. changed = img_pan(&img, &win, PAN_UP);
  384. break;
  385. case Button5:
  386. if (mask == ControlMask)
  387. changed = img_zoom_out(&img);
  388. else if (mask == ShiftMask)
  389. changed = img_pan(&img, &win, PAN_RIGHT);
  390. else
  391. changed = img_pan(&img, &win, PAN_DOWN);
  392. break;
  393. case 6:
  394. changed = img_pan(&img, &win, PAN_LEFT);
  395. break;
  396. case 7:
  397. changed = img_pan(&img, &win, PAN_RIGHT);
  398. break;
  399. }
  400. if (changed)
  401. redraw();
  402. }
  403. void on_motionnotify(XMotionEvent *mev) {
  404. if (!mev)
  405. return;
  406. if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
  407. if (img_move(&img, &win, mev->x - mox, mev->y - moy))
  408. timeout = 1;
  409. mox = mev->x;
  410. moy = mev->y;
  411. }
  412. }
  413. void run() {
  414. int xfd;
  415. fd_set fds;
  416. struct timeval t, t0;
  417. XEvent ev;
  418. timeout = 0;
  419. while (1) {
  420. if (mode == MODE_THUMBS && tns_loaded < filecnt) {
  421. win_set_cursor(&win, CURSOR_WATCH);
  422. gettimeofday(&t0, 0);
  423. while (!XPending(win.env.dpy) && tns_loaded < filecnt) {
  424. tns_load(&tns, &win, filenames[tns_loaded++]);
  425. gettimeofday(&t, 0);
  426. if (TV_TO_DOUBLE(t) - TV_TO_DOUBLE(t0) >= 0.25)
  427. break;
  428. }
  429. if (tns_loaded == filecnt)
  430. win_set_cursor(&win, CURSOR_ARROW);
  431. if (!XPending(win.env.dpy)) {
  432. redraw();
  433. continue;
  434. } else {
  435. timeout = 1;
  436. }
  437. } else if (timeout) {
  438. t.tv_sec = 0;
  439. t.tv_usec = 75000;
  440. xfd = ConnectionNumber(win.env.dpy);
  441. FD_ZERO(&fds);
  442. FD_SET(xfd, &fds);
  443. if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
  444. redraw();
  445. }
  446. if (!XNextEvent(win.env.dpy, &ev)) {
  447. switch (ev.type) {
  448. case KeyPress:
  449. on_keypress(&ev.xkey);
  450. break;
  451. case ButtonPress:
  452. on_buttonpress(&ev.xbutton);
  453. break;
  454. case ButtonRelease:
  455. if (ev.xbutton.button == Button2)
  456. win_set_cursor(&win, CURSOR_ARROW);
  457. break;
  458. case MotionNotify:
  459. on_motionnotify(&ev.xmotion);
  460. break;
  461. case ConfigureNotify:
  462. if (win_configure(&win, &ev.xconfigure)) {
  463. timeout = 1;
  464. if (mode == MODE_NORMAL)
  465. img.checkpan = 1;
  466. }
  467. break;
  468. case ClientMessage:
  469. if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
  470. return;
  471. break;
  472. }
  473. }
  474. }
  475. }