A Simple X Image Viewer
 
 
 
 
 
 

191 lines
3.5 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. int len;
  86. KeySym keysym;
  87. if (!ev)
  88. return;
  89. len = XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
  90. switch (keysym) {
  91. case XK_Escape:
  92. cleanup();
  93. exit(2);
  94. case XK_space:
  95. key = 'n';
  96. len = 1;
  97. break;
  98. case XK_BackSpace:
  99. key = 'p';
  100. len = 1;
  101. break;
  102. }
  103. if (!len)
  104. return;
  105. switch (key) {
  106. case 'q':
  107. cleanup();
  108. exit(0);
  109. case 'n':
  110. if (fileidx + 1 < filecnt) {
  111. img_load(&img, filenames[++fileidx]);
  112. img_render(&img, &win);
  113. update_title();
  114. }
  115. break;
  116. case 'p':
  117. if (fileidx > 0) {
  118. img_load(&img, filenames[--fileidx]);
  119. img_render(&img, &win);
  120. update_title();
  121. }
  122. break;
  123. case '+':
  124. case '=':
  125. if (img_zoom_in(&img)) {
  126. img_render(&img, &win);
  127. update_title();
  128. }
  129. break;
  130. case '-':
  131. if (img_zoom_out(&img)) {
  132. img_render(&img, &win);
  133. update_title();
  134. }
  135. break;
  136. }
  137. }
  138. void on_configurenotify(XEvent *ev) {
  139. if (!ev)
  140. return;
  141. win_configure(&win, &ev->xconfigure);
  142. }
  143. void update_title() {
  144. int n;
  145. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
  146. filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
  147. if (n >= TITLE_LEN) {
  148. win_title[TITLE_LEN - 2] = '.';
  149. win_title[TITLE_LEN - 3] = '.';
  150. win_title[TITLE_LEN - 4] = '.';
  151. }
  152. win_set_title(&win, win_title);
  153. }