A Simple X Image Viewer
 
 
 
 
 
 

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