A Simple X Image Viewer
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

204 wiersze
4.8 KiB

  1. /* sxiv: window.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 <string.h>
  21. #include <X11/Xutil.h>
  22. #include <X11/cursorfont.h>
  23. #include "sxiv.h"
  24. #include "options.h"
  25. #include "window.h"
  26. static Cursor arrow;
  27. static Cursor hand;
  28. static GC bgc;
  29. void win_open(win_t *win) {
  30. win_env_t *e;
  31. XClassHint *classhint;
  32. XColor bgcol;
  33. if (!win)
  34. return;
  35. e = &win->env;
  36. if (!(e->dpy = XOpenDisplay(NULL)))
  37. DIE("could not open display");
  38. e->scr = DefaultScreen(e->dpy);
  39. e->scrw = DisplayWidth(e->dpy, e->scr);
  40. e->scrh = DisplayHeight(e->dpy, e->scr);
  41. e->vis = DefaultVisual(e->dpy, e->scr);
  42. e->cmap = DefaultColormap(e->dpy, e->scr);
  43. e->depth = DefaultDepth(e->dpy, e->scr);
  44. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  45. &bgcol, &bgcol))
  46. DIE("could not allocate color: %s", BG_COLOR);
  47. win->bgcol = bgcol.pixel;
  48. win->pm = 0;
  49. win->fullscreen = 0;
  50. win->w = MIN(options->winw, e->scrw);
  51. win->h = MIN(options->winh, e->scrh);
  52. win->x = (e->scrw - win->w) / 2;
  53. win->y = (e->scrh - win->h) / 2;
  54. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  55. win->x, win->y, win->w, win->h, 0,
  56. e->depth, InputOutput, e->vis, 0, None);
  57. if (win->xwin == None)
  58. DIE("could not create window");
  59. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  60. ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
  61. arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  62. hand = XCreateFontCursor(e->dpy, XC_fleur);
  63. bgc = XCreateGC(e->dpy, win->xwin, 0, None);
  64. win_set_title(win, "sxiv");
  65. if ((classhint = XAllocClassHint())) {
  66. classhint->res_name = "sxiv";
  67. classhint->res_class = "sxiv";
  68. XSetClassHint(e->dpy, win->xwin, classhint);
  69. XFree(classhint);
  70. }
  71. XMapWindow(e->dpy, win->xwin);
  72. XFlush(e->dpy);
  73. if (options->fullscreen)
  74. win_toggle_fullscreen(win);
  75. }
  76. void win_close(win_t *win) {
  77. if (!win)
  78. return;
  79. XFreeCursor(win->env.dpy, arrow);
  80. XFreeCursor(win->env.dpy, hand);
  81. XFreeGC(win->env.dpy, bgc);
  82. XDestroyWindow(win->env.dpy, win->xwin);
  83. XCloseDisplay(win->env.dpy);
  84. }
  85. void win_set_title(win_t *win, const char *title) {
  86. if (!win)
  87. return;
  88. if (!title)
  89. title = "sxiv";
  90. XStoreName(win->env.dpy, win->xwin, title);
  91. XSetIconName(win->env.dpy, win->xwin, title);
  92. }
  93. int win_configure(win_t *win, XConfigureEvent *c) {
  94. int changed;
  95. if (!win)
  96. return 0;
  97. changed = win->w != c->width || win->h != c->height;
  98. win->x = c->x;
  99. win->y = c->y;
  100. win->w = c->width;
  101. win->h = c->height;
  102. win->bw = c->border_width;
  103. return changed;
  104. }
  105. void win_toggle_fullscreen(win_t *win) {
  106. XEvent ev;
  107. XClientMessageEvent *cm;
  108. if (!win)
  109. return;
  110. win->fullscreen ^= 1;
  111. memset(&ev, 0, sizeof(ev));
  112. ev.type = ClientMessage;
  113. cm = &ev.xclient;
  114. cm->window = win->xwin;
  115. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  116. cm->format = 32;
  117. cm->data.l[0] = win->fullscreen;
  118. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  119. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  120. cm->data.l[3] = 0;
  121. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  122. SubstructureNotifyMask, &ev);
  123. }
  124. void win_clear(win_t *win) {
  125. win_env_t *e;
  126. XGCValues gcval;
  127. if (!win)
  128. return;
  129. e = &win->env;
  130. if (win->pm)
  131. XFreePixmap(e->dpy, win->pm);
  132. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  133. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
  134. XChangeGC(e->dpy, bgc, GCForeground, &gcval);
  135. XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
  136. }
  137. void win_draw(win_t *win) {
  138. if (!win)
  139. return;
  140. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  141. XClearWindow(win->env.dpy, win->xwin);
  142. }
  143. void win_set_cursor(win_t *win, win_cur_t cursor) {
  144. if (!win)
  145. return;
  146. switch (cursor) {
  147. case CURSOR_HAND:
  148. XDefineCursor(win->env.dpy, win->xwin, hand);
  149. break;
  150. case CURSOR_ARROW:
  151. default:
  152. XDefineCursor(win->env.dpy, win->xwin, arrow);
  153. break;
  154. }
  155. }