A Simple X Image Viewer
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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