A Simple X Image Viewer
 
 
 
 
 
 

299 lines
5.4 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 "options.h"
  27. #include "window.h"
  28. void on_keypress(XEvent*);
  29. void on_configurenotify(XEvent*);
  30. void on_buttonpress(XEvent*);
  31. void update_title();
  32. static void (*handler[LASTEvent])(XEvent*) = {
  33. [ButtonPress] = on_buttonpress,
  34. [KeyPress] = on_keypress,
  35. [ConfigureNotify] = on_configurenotify
  36. };
  37. img_t img;
  38. win_t win;
  39. const char **filenames;
  40. int filecnt, fileidx;
  41. unsigned char timeout;
  42. #define TITLE_LEN 256
  43. char win_title[TITLE_LEN];
  44. void run() {
  45. int xfd;
  46. fd_set fds;
  47. struct timeval t;
  48. XEvent ev;
  49. timeout = 0;
  50. while (1) {
  51. if (timeout) {
  52. t.tv_sec = 0;
  53. t.tv_usec = 250;
  54. xfd = ConnectionNumber(win.env.dpy);
  55. FD_ZERO(&fds);
  56. FD_SET(xfd, &fds);
  57. if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
  58. img_render(&img, &win);
  59. timeout = 0;
  60. }
  61. }
  62. if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type])
  63. handler[ev.type](&ev);
  64. }
  65. }
  66. int main(int argc, char **argv) {
  67. int i;
  68. parse_options(argc, argv);
  69. if (!options->filecnt) {
  70. print_usage();
  71. exit(1);
  72. }
  73. if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
  74. DIE("could not allocate memory");
  75. fileidx = 0;
  76. filecnt = 0;
  77. for (i = 0; i < options->filecnt; ++i) {
  78. if (!(img_load(&img, options->filenames[i]) < 0))
  79. filenames[filecnt++] = options->filenames[i];
  80. }
  81. if (!filecnt) {
  82. fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
  83. exit(1);
  84. }
  85. win_open(&win);
  86. img_init(&img, &win);
  87. img_load(&img, filenames[fileidx]);
  88. img_render(&img, &win);
  89. update_title();
  90. run();
  91. cleanup();
  92. return 0;
  93. }
  94. void cleanup() {
  95. static int in = 0;
  96. if (!in++) {
  97. img_free(&img);
  98. win_close(&win);
  99. }
  100. }
  101. void on_buttonpress(XEvent *ev) {
  102. int changed;
  103. XButtonEvent *buttonevent;
  104. changed = 0;
  105. buttonevent = &ev->xbutton;
  106. switch (buttonevent->button) {
  107. case Button4:
  108. changed = img_zoom_in(&img);
  109. break;
  110. case Button5:
  111. changed = img_zoom_out(&img);
  112. break;
  113. default:
  114. return;
  115. }
  116. if (changed) {
  117. img_render(&img, &win);
  118. update_title();
  119. timeout = 0;
  120. }
  121. }
  122. void on_keypress(XEvent *ev) {
  123. char key;
  124. KeySym keysym;
  125. int changed;
  126. if (!ev)
  127. return;
  128. XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
  129. changed = 0;
  130. switch (keysym) {
  131. case XK_Escape:
  132. cleanup();
  133. exit(2);
  134. case XK_space:
  135. key = 'n';
  136. break;
  137. case XK_BackSpace:
  138. key = 'p';
  139. break;
  140. }
  141. switch (key) {
  142. case 'q':
  143. cleanup();
  144. exit(0);
  145. /* navigate image list */
  146. case 'n':
  147. if (fileidx + 1 < filecnt) {
  148. img_load(&img, filenames[++fileidx]);
  149. changed = 1;
  150. }
  151. break;
  152. case 'p':
  153. if (fileidx > 0) {
  154. img_load(&img, filenames[--fileidx]);
  155. changed = 1;
  156. }
  157. break;
  158. case '[':
  159. if (fileidx != 0) {
  160. fileidx = MAX(0, fileidx - 10);
  161. img_load(&img, filenames[fileidx]);
  162. changed = 1;
  163. }
  164. break;
  165. case ']':
  166. if (fileidx != filecnt - 1) {
  167. fileidx = MIN(fileidx + 10, filecnt - 1);
  168. img_load(&img, filenames[fileidx]);
  169. changed = 1;
  170. }
  171. break;
  172. case 'g':
  173. if (fileidx != 0) {
  174. fileidx = 0;
  175. img_load(&img, filenames[fileidx]);
  176. changed = 1;
  177. }
  178. break;
  179. case 'G':
  180. if (fileidx != filecnt - 1) {
  181. fileidx = filecnt - 1;
  182. img_load(&img, filenames[fileidx]);
  183. changed = 1;
  184. }
  185. break;
  186. /* zooming */
  187. case '+':
  188. case '=':
  189. changed = img_zoom_in(&img);
  190. break;
  191. case '-':
  192. changed = img_zoom_out(&img);
  193. break;
  194. /* panning */
  195. case 'h':
  196. changed = img_pan(&img, &win, PAN_LEFT);
  197. break;
  198. case 'j':
  199. changed = img_pan(&img, &win, PAN_DOWN);
  200. break;
  201. case 'k':
  202. changed = img_pan(&img, &win, PAN_UP);
  203. break;
  204. case 'l':
  205. changed = img_pan(&img, &win, PAN_RIGHT);
  206. break;
  207. /* rotation */
  208. case '<':
  209. changed = img_rotate_left(&img, &win);
  210. break;
  211. case '>':
  212. changed = img_rotate_right(&img, &win);
  213. break;
  214. /* control window */
  215. case 'f':
  216. win_toggle_fullscreen(&win);
  217. break;
  218. /* miscellaneous */
  219. case 'a':
  220. changed = img_toggle_antialias(&img);
  221. break;
  222. }
  223. if (changed) {
  224. img_render(&img, &win);
  225. update_title();
  226. timeout = 0;
  227. }
  228. }
  229. void on_configurenotify(XEvent *ev) {
  230. if (!ev)
  231. return;
  232. if (win_configure(&win, &ev->xconfigure)) {
  233. img.checkpan = 1;
  234. timeout = 1;
  235. }
  236. }
  237. void update_title() {
  238. int n;
  239. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
  240. filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
  241. if (n >= TITLE_LEN) {
  242. win_title[TITLE_LEN - 2] = '.';
  243. win_title[TITLE_LEN - 3] = '.';
  244. win_title[TITLE_LEN - 4] = '.';
  245. }
  246. win_set_title(&win, win_title);
  247. }