A Simple X Image Viewer
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

197 строки
3.6 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. img.zoom = 1.0;
  68. img.scalemode = SCALE_MODE;
  69. win.w = WIN_WIDTH;
  70. win.h = WIN_HEIGHT;
  71. win_open(&win);
  72. imlib_init(&win);
  73. img_load(&img, filenames[fileidx]);
  74. img_display(&img, &win);
  75. update_title();
  76. run();
  77. cleanup();
  78. return 0;
  79. }
  80. void cleanup() {
  81. static int in = 0;
  82. if (!in++) {
  83. imlib_destroy();
  84. win_close(&win);
  85. }
  86. }
  87. void on_keypress(XEvent *ev) {
  88. char key;
  89. int len;
  90. KeySym keysym;
  91. if (!ev)
  92. return;
  93. len = XLookupString(&ev->xkey, &key, 1, &keysym, NULL);
  94. switch (keysym) {
  95. case XK_Escape:
  96. cleanup();
  97. exit(2);
  98. case XK_space:
  99. key = 'n';
  100. len = 1;
  101. break;
  102. case XK_BackSpace:
  103. key = 'p';
  104. len = 1;
  105. break;
  106. }
  107. if (!len)
  108. return;
  109. switch (key) {
  110. case 'q':
  111. cleanup();
  112. exit(0);
  113. case 'n':
  114. if (fileidx + 1 < filecnt) {
  115. img_load(&img, filenames[++fileidx]);
  116. img_display(&img, &win);
  117. update_title();
  118. }
  119. break;
  120. case 'p':
  121. if (fileidx > 0) {
  122. img_load(&img, filenames[--fileidx]);
  123. img_display(&img, &win);
  124. update_title();
  125. }
  126. break;
  127. case '+':
  128. case '=':
  129. if (img_zoom_in(&img)) {
  130. img_render(&img, &win);
  131. update_title();
  132. }
  133. break;
  134. case '-':
  135. if (img_zoom_out(&img)) {
  136. img_render(&img, &win);
  137. update_title();
  138. }
  139. break;
  140. }
  141. }
  142. void on_configurenotify(XEvent *ev) {
  143. if (!ev)
  144. return;
  145. win_configure(&win, &ev->xconfigure);
  146. }
  147. void update_title() {
  148. int n;
  149. n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1,
  150. filecnt, (int) (img.zoom * 100.0), filenames[fileidx]);
  151. if (n >= TITLE_LEN) {
  152. win_title[TITLE_LEN - 2] = '.';
  153. win_title[TITLE_LEN - 3] = '.';
  154. win_title[TITLE_LEN - 4] = '.';
  155. }
  156. win_set_title(&win, win_title);
  157. }