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.
 
 
 
 
 
 

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