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.
 
 
 
 
 
 

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