A Simple X Image Viewer
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

361 líneas
6.7 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 <sys/select.h>
  21. #include <X11/Xlib.h>
  22. #include <X11/Xutil.h>
  23. #include <X11/keysym.h>
  24. #include "sxiv.h"
  25. #include "image.h"
  26. #include "window.h"
  27. void on_keypress(XEvent*);
  28. void on_buttonpress(XEvent*);
  29. void on_buttonrelease(XEvent*);
  30. void on_motionnotify(XEvent*);
  31. void on_configurenotify(XEvent*);
  32. void update_title();
  33. static void (*handler[LASTEvent])(XEvent*) = {
  34. [KeyPress] = on_keypress,
  35. [ButtonPress] = on_buttonpress,
  36. [ButtonRelease] = on_buttonrelease,
  37. [MotionNotify] = on_motionnotify,
  38. [ConfigureNotify] = on_configurenotify
  39. };
  40. img_t img;
  41. win_t win;
  42. const char **filenames;
  43. int filecnt, fileidx;
  44. unsigned char timeout;
  45. int mox;
  46. int moy;
  47. #define TITLE_LEN 256
  48. char win_title[TITLE_LEN];
  49. void run() {
  50. int xfd;
  51. fd_set fds;
  52. struct timeval t;
  53. XEvent ev;
  54. timeout = 0;
  55. while (1) {
  56. if (timeout) {
  57. t.tv_sec = 0;
  58. t.tv_usec = 250;
  59. xfd = ConnectionNumber(win.env.dpy);
  60. FD_ZERO(&fds);
  61. FD_SET(xfd, &fds);
  62. if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
  63. img_render(&img, &win);
  64. timeout = 0;
  65. }
  66. }
  67. if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type])
  68. handler[ev.type](&ev);
  69. }
  70. }
  71. int main(int argc, char **argv) {
  72. int i;
  73. parse_options(argc, argv);
  74. if (!options->filecnt) {
  75. print_usage();
  76. exit(1);
  77. }
  78. if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
  79. DIE("could not allocate memory");
  80. fileidx = 0;
  81. filecnt = 0;
  82. for (i = 0; i < options->filecnt; ++i) {
  83. if (!(img_load(&img, options->filenames[i]) < 0))
  84. filenames[filecnt++] = options->filenames[i];
  85. }
  86. if (!filecnt) {
  87. fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
  88. exit(1);
  89. }
  90. win_open(&win);
  91. img_init(&img, &win);
  92. img_load(&img, filenames[fileidx]);
  93. img_render(&img, &win);
  94. update_title();
  95. run();
  96. cleanup();
  97. return 0;
  98. }
  99. void cleanup() {
  100. static int in = 0;
  101. if (!in++) {
  102. img_free(&img);
  103. win_close(&win);
  104. }
  105. }
  106. void on_keypress(XEvent *ev) {
  107. char key;
  108. KeySym ksym;
  109. int changed;
  110. if (!ev)
  111. return;
  112. XLookupString(&ev->xkey, &key, 1, &ksym, NULL);
  113. changed = 0;
  114. switch (ksym) {
  115. case XK_Escape:
  116. cleanup();
  117. exit(2);
  118. case XK_q:
  119. cleanup();
  120. exit(0);
  121. /* navigate image list */
  122. case XK_n:
  123. case XK_space:
  124. if (fileidx + 1 < filecnt) {
  125. img_load(&img, filenames[++fileidx]);
  126. changed = 1;
  127. }
  128. break;
  129. case XK_p:
  130. case XK_BackSpace:
  131. if (fileidx > 0) {
  132. img_load(&img, filenames[--fileidx]);
  133. changed = 1;
  134. }
  135. break;
  136. case XK_bracketleft:
  137. if (fileidx != 0) {
  138. fileidx = MAX(0, fileidx - 10);
  139. img_load(&img, filenames[fileidx]);
  140. changed = 1;
  141. }
  142. break;
  143. case XK_bracketright:
  144. if (fileidx != filecnt - 1) {
  145. fileidx = MIN(fileidx + 10, filecnt - 1);
  146. img_load(&img, filenames[fileidx]);
  147. changed = 1;
  148. }
  149. break;
  150. case XK_g:
  151. if (fileidx != 0) {
  152. fileidx = 0;
  153. img_load(&img, filenames[fileidx]);
  154. changed = 1;
  155. }
  156. break;
  157. case XK_G:
  158. if (fileidx != filecnt - 1) {
  159. fileidx = filecnt - 1;
  160. img_load(&img, filenames[fileidx]);
  161. changed = 1;
  162. }
  163. break;
  164. /* zooming */
  165. case XK_plus:
  166. case XK_equal:
  167. changed = img_zoom_in(&img);
  168. break;
  169. case XK_minus:
  170. changed = img_zoom_out(&img);
  171. break;
  172. /* panning */
  173. case XK_h:
  174. case XK_Left:
  175. changed = img_pan(&img, &win, PAN_LEFT);
  176. break;
  177. case XK_j:
  178. case XK_Down:
  179. changed = img_pan(&img, &win, PAN_DOWN);
  180. break;
  181. case XK_k:
  182. case XK_Up:
  183. changed = img_pan(&img, &win, PAN_UP);
  184. break;
  185. case XK_l:
  186. case XK_Right:
  187. changed = img_pan(&img, &win, PAN_RIGHT);
  188. break;
  189. /* rotation */
  190. case XK_less:
  191. changed = img_rotate_left(&img, &win);
  192. break;
  193. case XK_greater:
  194. changed = img_rotate_right(&img, &win);
  195. break;
  196. /* control window */
  197. case XK_f:
  198. win_toggle_fullscreen(&win);
  199. break;
  200. /* miscellaneous */
  201. case XK_a:
  202. changed = img_toggle_antialias(&img);
  203. break;
  204. }
  205. if (changed) {
  206. img_render(&img, &win);
  207. update_title();
  208. timeout = 0;
  209. }
  210. }
  211. void on_buttonpress(XEvent *ev) {
  212. int changed;
  213. unsigned int mask;
  214. if (!ev)
  215. return;
  216. mask = CLEANMASK(ev->xbutton.state);
  217. changed = 0;
  218. switch (ev->xbutton.button) {
  219. case Button1:
  220. if (fileidx + 1 < filecnt) {
  221. img_load(&img, filenames[++fileidx]);
  222. changed = 1;
  223. }
  224. break;
  225. case Button2:
  226. mox = ev->xbutton.x;
  227. moy = ev->xbutton.y;
  228. win_set_cursor(&win, CURSOR_HAND);
  229. break;
  230. case Button3:
  231. if (fileidx > 0) {
  232. img_load(&img, filenames[--fileidx]);
  233. changed = 1;
  234. }
  235. break;
  236. case Button4:
  237. if (mask == ControlMask)
  238. changed = img_zoom_in(&img);
  239. else if (mask == ShiftMask)
  240. changed = img_pan(&img, &win, PAN_LEFT);
  241. else
  242. changed = img_pan(&img, &win, PAN_UP);
  243. break;
  244. case Button5:
  245. if (mask == ControlMask)
  246. changed = img_zoom_out(&img);
  247. else if (mask == ShiftMask)
  248. changed = img_pan(&img, &win, PAN_RIGHT);
  249. else
  250. changed = img_pan(&img, &win, PAN_DOWN);
  251. break;
  252. case 6:
  253. changed = img_pan(&img, &win, PAN_LEFT);
  254. break;
  255. case 7:
  256. changed = img_pan(&img, &win, PAN_RIGHT);
  257. break;
  258. }
  259. if (changed) {
  260. img_render(&img, &win);
  261. update_title();
  262. timeout = 0;
  263. }
  264. }
  265. void on_buttonrelease(XEvent *ev) {
  266. if (!ev)
  267. return;
  268. if (ev->xbutton.button == Button2)
  269. win_set_cursor(&win, CURSOR_ARROW);
  270. }
  271. void on_motionnotify(XEvent *ev) {
  272. XMotionEvent *m;
  273. if (!ev)
  274. return;
  275. m = &ev->xmotion;
  276. if (m->x >= 0 && m->x <= win.w && m->y >= 0 && m->y <= win.h) {
  277. if (img_move(&img, &win, m->x - mox, m->y - moy))
  278. timeout = 1;
  279. mox = m->x;
  280. moy = m->y;
  281. }
  282. }
  283. void on_configurenotify(XEvent *ev) {
  284. if (!ev)
  285. return;
  286. if (win_configure(&win, &ev->xconfigure)) {
  287. img.checkpan = 1;
  288. timeout = 1;
  289. }
  290. }
  291. void update_title() {
  292. int n;
  293. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
  294. filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
  295. if (n >= TITLE_LEN) {
  296. win_title[TITLE_LEN - 2] = '.';
  297. win_title[TITLE_LEN - 3] = '.';
  298. win_title[TITLE_LEN - 4] = '.';
  299. }
  300. win_set_title(&win, win_title);
  301. }