A Simple X Image Viewer
 
 
 
 
 
 

122 lines
2.3 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 <X11/Xlib.h>
  20. #include <X11/keysym.h>
  21. #include "sxiv.h"
  22. #include "image.h"
  23. #include "options.h"
  24. #include "window.h"
  25. void on_keypress(XEvent*);
  26. void on_configurenotify(XEvent*);
  27. void on_expose(XEvent*);
  28. static void (*handler[LASTEvent])(XEvent*) = {
  29. [Expose] = on_expose,
  30. [ConfigureNotify] = on_configurenotify,
  31. [KeyPress] = on_keypress
  32. };
  33. img_t img;
  34. win_t win;
  35. unsigned int fileidx;
  36. void run() {
  37. XEvent ev;
  38. while (!XNextEvent(win.env.dpy, &ev)) {
  39. if (handler[ev.type])
  40. handler[ev.type](&ev);
  41. }
  42. }
  43. int main(int argc, char **argv) {
  44. parse_options(argc, argv);
  45. if (!options->filecnt) {
  46. print_usage();
  47. exit(1);
  48. }
  49. fileidx = 0;
  50. img.zoom = 1.0;
  51. img.scalemode = SCALE_MODE;
  52. win.w = WIN_WIDTH;
  53. win.h = WIN_HEIGHT;
  54. win_open(&win);
  55. imlib_init(&win);
  56. img_load(&img, options->filenames[fileidx]);
  57. img_display(&img, &win);
  58. run();
  59. cleanup();
  60. return 0;
  61. }
  62. void cleanup() {
  63. static int in = 0;
  64. if (!in++) {
  65. imlib_destroy();
  66. win_close(&win);
  67. }
  68. }
  69. void on_keypress(XEvent *ev) {
  70. KeySym keysym;
  71. if (!ev)
  72. return;
  73. keysym = XLookupKeysym(&ev->xkey, 0);
  74. switch (keysym) {
  75. case XK_Escape:
  76. cleanup();
  77. exit(1);
  78. case XK_q:
  79. cleanup();
  80. exit(0);
  81. }
  82. }
  83. void on_configurenotify(XEvent *ev) {
  84. if (!ev)
  85. return;
  86. win_configure(&win, &ev->xconfigure);
  87. }
  88. void on_expose(XEvent *ev) {
  89. if (!ev)
  90. return;
  91. img_render(&img, &win, ev->xexpose.x, ev->xexpose.y,
  92. ev->xexpose.width, ev->xexpose.height);
  93. }