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.
 
 
 
 
 
 

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