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.
 
 
 
 
 
 

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