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.
 
 
 
 
 
 

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