A Simple X Image Viewer
 
 
 
 
 
 

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