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.
 
 
 
 
 
 

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