A Simple X Image Viewer
 
 
 
 
 
 

202 lines
3.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 <X11/Xlib.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/keysym.h>
  23. #include "sxiv.h"
  24. #include "image.h"
  25. #include "options.h"
  26. #include "window.h"
  27. void on_keypress(XEvent*);
  28. void on_configurenotify(XEvent*);
  29. void update_title();
  30. static void (*handler[LASTEvent])(XEvent*) = {
  31. [KeyPress] = on_keypress,
  32. [ConfigureNotify] = on_configurenotify
  33. };
  34. img_t img;
  35. win_t win;
  36. const char **filenames;
  37. unsigned int filecnt;
  38. unsigned int fileidx;
  39. #define TITLE_LEN 256
  40. char win_title[TITLE_LEN];
  41. void run() {
  42. XEvent ev;
  43. while (!XNextEvent(win.env.dpy, &ev)) {
  44. if (handler[ev.type])
  45. handler[ev.type](&ev);
  46. }
  47. }
  48. int main(int argc, char **argv) {
  49. int i;
  50. parse_options(argc, argv);
  51. if (!options->filecnt) {
  52. print_usage();
  53. exit(1);
  54. }
  55. if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
  56. DIE("could not allocate memory");
  57. fileidx = 0;
  58. filecnt = 0;
  59. for (i = 0; i < options->filecnt; ++i) {
  60. if (!(img_load(&img, options->filenames[i]) < 0))
  61. filenames[filecnt++] = options->filenames[i];
  62. }
  63. if (!filecnt) {
  64. fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
  65. exit(1);
  66. }
  67. win_open(&win);
  68. img_init(&img, &win);
  69. img_load(&img, filenames[fileidx]);
  70. img_render(&img, &win);
  71. update_title();
  72. run();
  73. cleanup();
  74. return 0;
  75. }
  76. void cleanup() {
  77. static int in = 0;
  78. if (!in++) {
  79. img_free(&img);
  80. win_close(&win);
  81. }
  82. }
  83. void on_keypress(XEvent *ev) {
  84. char key;
  85. KeySym keysym;
  86. int changed;
  87. if (!ev)
  88. return;
  89. XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
  90. changed = 0;
  91. switch (keysym) {
  92. case XK_Escape:
  93. cleanup();
  94. exit(2);
  95. case XK_space:
  96. key = 'n';
  97. break;
  98. case XK_BackSpace:
  99. key = 'p';
  100. break;
  101. }
  102. switch (key) {
  103. case 'q':
  104. cleanup();
  105. exit(0);
  106. /* navigate through image list */
  107. case 'n':
  108. if (fileidx + 1 < filecnt) {
  109. img_load(&img, filenames[++fileidx]);
  110. changed = 1;
  111. }
  112. break;
  113. case 'p':
  114. if (fileidx > 0) {
  115. img_load(&img, filenames[--fileidx]);
  116. changed = 1;
  117. }
  118. break;
  119. /* zooming */
  120. case '+':
  121. case '=':
  122. changed = img_zoom_in(&img);
  123. break;
  124. case '-':
  125. changed = img_zoom_out(&img);
  126. break;
  127. /* panning */
  128. case 'h':
  129. changed = img_pan(&img, &win, PAN_LEFT);
  130. break;
  131. case 'j':
  132. changed = img_pan(&img, &win, PAN_DOWN);
  133. break;
  134. case 'k':
  135. changed = img_pan(&img, &win, PAN_UP);
  136. break;
  137. case 'l':
  138. changed = img_pan(&img, &win, PAN_RIGHT);
  139. break;
  140. }
  141. if (changed) {
  142. img_render(&img, &win);
  143. update_title();
  144. }
  145. }
  146. void on_configurenotify(XEvent *ev) {
  147. if (!ev)
  148. return;
  149. win_configure(&win, &ev->xconfigure);
  150. }
  151. void update_title() {
  152. int n;
  153. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
  154. filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
  155. if (n >= TITLE_LEN) {
  156. win_title[TITLE_LEN - 2] = '.';
  157. win_title[TITLE_LEN - 3] = '.';
  158. win_title[TITLE_LEN - 4] = '.';
  159. }
  160. win_set_title(&win, win_title);
  161. }