A Simple X Image Viewer
 
 
 
 
 
 

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