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.
 
 
 
 
 
 

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