A Simple X Image Viewer
 
 
 
 
 
 

179 行
3.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 <X11/Xlib.h>
  21. #include <X11/keysym.h>
  22. #include "sxiv.h"
  23. #include "image.h"
  24. #include "options.h"
  25. #include "window.h"
  26. void on_keypress(XEvent*);
  27. void on_configurenotify(XEvent*);
  28. void on_expose(XEvent*);
  29. void update_title();
  30. static void (*handler[LASTEvent])(XEvent*) = {
  31. [Expose] = on_expose,
  32. [ConfigureNotify] = on_configurenotify,
  33. [KeyPress] = on_keypress
  34. };
  35. img_t img;
  36. win_t win;
  37. const char **filenames;
  38. unsigned int filecnt;
  39. unsigned int fileidx;
  40. #define TITLE_LEN 256
  41. char win_title[TITLE_LEN];
  42. void run() {
  43. XEvent ev;
  44. while (!XNextEvent(win.env.dpy, &ev)) {
  45. if (handler[ev.type])
  46. handler[ev.type](&ev);
  47. }
  48. }
  49. int main(int argc, char **argv) {
  50. int i;
  51. parse_options(argc, argv);
  52. if (!options->filecnt) {
  53. print_usage();
  54. exit(1);
  55. }
  56. if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*))))
  57. DIE("could not allocate memory");
  58. fileidx = 0;
  59. filecnt = 0;
  60. for (i = 0; i < options->filecnt; ++i) {
  61. if (!(img_load(&img, options->filenames[i]) < 0))
  62. filenames[filecnt++] = options->filenames[i];
  63. }
  64. if (!filecnt) {
  65. fprintf(stderr, "sxiv: no valid image filename given, aborting\n");
  66. exit(1);
  67. }
  68. img.zoom = 1.0;
  69. img.scalemode = SCALE_MODE;
  70. win.w = WIN_WIDTH;
  71. win.h = WIN_HEIGHT;
  72. win_open(&win);
  73. imlib_init(&win);
  74. img_load(&img, filenames[fileidx]);
  75. img_display(&img, &win);
  76. update_title();
  77. run();
  78. cleanup();
  79. return 0;
  80. }
  81. void cleanup() {
  82. static int in = 0;
  83. if (!in++) {
  84. imlib_destroy();
  85. win_close(&win);
  86. }
  87. }
  88. void on_keypress(XEvent *ev) {
  89. KeySym keysym;
  90. if (!ev)
  91. return;
  92. keysym = XLookupKeysym(&ev->xkey, 0);
  93. switch (keysym) {
  94. case XK_Escape:
  95. cleanup();
  96. exit(1);
  97. case XK_q:
  98. cleanup();
  99. exit(0);
  100. case XK_n:
  101. case XK_space:
  102. if (fileidx + 1 < filecnt) {
  103. img_load(&img, filenames[++fileidx]);
  104. img_display(&img, &win);
  105. update_title();
  106. }
  107. break;
  108. case XK_p:
  109. case XK_BackSpace:
  110. if (fileidx > 0) {
  111. img_load(&img, filenames[--fileidx]);
  112. img_display(&img, &win);
  113. update_title();
  114. }
  115. break;
  116. }
  117. }
  118. void on_configurenotify(XEvent *ev) {
  119. if (!ev)
  120. return;
  121. win_configure(&win, &ev->xconfigure);
  122. }
  123. void on_expose(XEvent *ev) {
  124. if (!ev)
  125. return;
  126. img_render(&img, &win, ev->xexpose.x, ev->xexpose.y,
  127. ev->xexpose.width, ev->xexpose.height);
  128. }
  129. void update_title() {
  130. int n;
  131. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
  132. filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
  133. if (n >= TITLE_LEN) {
  134. win_title[TITLE_LEN - 2] = '.';
  135. win_title[TITLE_LEN - 3] = '.';
  136. win_title[TITLE_LEN - 4] = '.';
  137. }
  138. win_set_title(&win, win_title);
  139. }