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.

14 年之前
14 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 carrow;
  26. static Cursor chand;
  27. static Cursor cwatch;
  28. static GC bgc;
  29. Atom wm_delete_win;
  30. void win_set_sizehints(win_t *win) {
  31. XSizeHints sizehints;
  32. if (!win)
  33. return;
  34. sizehints.flags = PMinSize | PMaxSize;
  35. sizehints.min_width = win->w;
  36. sizehints.max_width = win->w;
  37. sizehints.min_height = win->h;
  38. sizehints.max_height = win->h;
  39. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  40. }
  41. void win_open(win_t *win) {
  42. win_env_t *e;
  43. XClassHint classhint;
  44. XColor bgcol;
  45. int gmask;
  46. if (!win)
  47. return;
  48. e = &win->env;
  49. if (!(e->dpy = XOpenDisplay(NULL)))
  50. die("could not open display");
  51. e->scr = DefaultScreen(e->dpy);
  52. e->scrw = DisplayWidth(e->dpy, e->scr);
  53. e->scrh = DisplayHeight(e->dpy, e->scr);
  54. e->vis = DefaultVisual(e->dpy, e->scr);
  55. e->cmap = DefaultColormap(e->dpy, e->scr);
  56. e->depth = DefaultDepth(e->dpy, e->scr);
  57. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  58. &bgcol, &bgcol))
  59. die("could not allocate color: %s", BG_COLOR);
  60. win->bgcol = bgcol.pixel;
  61. win->pm = 0;
  62. win->fullscreen = 0;
  63. /* determine window offsets, width & height */
  64. if (!options->geometry)
  65. gmask = 0;
  66. else
  67. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  68. &win->w, &win->h);
  69. if (!(gmask & WidthValue))
  70. win->w = WIN_WIDTH;
  71. if (win->w > e->scrw)
  72. win->w = e->scrw;
  73. if (!(gmask & HeightValue))
  74. win->h = WIN_HEIGHT;
  75. if (win->h > e->scrh)
  76. win->h = e->scrh;
  77. if (!(gmask & XValue))
  78. win->x = (e->scrw - win->w) / 2;
  79. else if (gmask & XNegative)
  80. win->x += e->scrw - win->w;
  81. if (!(gmask & YValue))
  82. win->y = (e->scrh - win->h) / 2;
  83. else if (gmask & YNegative)
  84. win->y += e->scrh - win->h;
  85. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  86. win->x, win->y, win->w, win->h, 0,
  87. e->depth, InputOutput, e->vis, 0, None);
  88. if (win->xwin == None)
  89. die("could not create window");
  90. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  91. ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
  92. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  93. chand = XCreateFontCursor(e->dpy, XC_fleur);
  94. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  95. bgc = XCreateGC(e->dpy, win->xwin, 0, None);
  96. win_set_title(win, "sxiv");
  97. classhint.res_name = "sxiv";
  98. classhint.res_class = "sxiv";
  99. XSetClassHint(e->dpy, win->xwin, &classhint);
  100. if (options->fixed)
  101. win_set_sizehints(win);
  102. XMapWindow(e->dpy, win->xwin);
  103. XFlush(e->dpy);
  104. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  105. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  106. if (options->fullscreen)
  107. win_toggle_fullscreen(win);
  108. }
  109. void win_close(win_t *win) {
  110. if (!win)
  111. return;
  112. XFreeCursor(win->env.dpy, carrow);
  113. XFreeCursor(win->env.dpy, chand);
  114. XFreeCursor(win->env.dpy, cwatch);
  115. XFreeGC(win->env.dpy, bgc);
  116. XDestroyWindow(win->env.dpy, win->xwin);
  117. XCloseDisplay(win->env.dpy);
  118. }
  119. int win_configure(win_t *win, XConfigureEvent *c) {
  120. int changed;
  121. if (!win)
  122. return 0;
  123. changed = win->w != c->width || win->h != c->height;
  124. win->x = c->x;
  125. win->y = c->y;
  126. win->w = c->width;
  127. win->h = c->height;
  128. win->bw = c->border_width;
  129. return changed;
  130. }
  131. int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  132. if (!win)
  133. return 0;
  134. x = MAX(0, x);
  135. y = MAX(0, y);
  136. w = MIN(w, win->env.scrw - 2 * win->bw);
  137. h = MIN(h, win->env.scrh - 2 * win->bw);
  138. if (win->x == x && win->y == y && win->w == w && win->h == h)
  139. return 0;
  140. win->x = x;
  141. win->y = y;
  142. win->w = w;
  143. win->h = h;
  144. if (options->fixed)
  145. win_set_sizehints(win);
  146. XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
  147. return 1;
  148. }
  149. void win_toggle_fullscreen(win_t *win) {
  150. XEvent ev;
  151. XClientMessageEvent *cm;
  152. if (!win)
  153. return;
  154. win->fullscreen ^= 1;
  155. memset(&ev, 0, sizeof(ev));
  156. ev.type = ClientMessage;
  157. cm = &ev.xclient;
  158. cm->window = win->xwin;
  159. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  160. cm->format = 32;
  161. cm->data.l[0] = win->fullscreen;
  162. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  163. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  164. cm->data.l[3] = 0;
  165. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  166. SubstructureNotifyMask, &ev);
  167. }
  168. Pixmap win_create_pixmap(win_t *win, int w, int h) {
  169. if (!win)
  170. return 0;
  171. return XCreatePixmap(win->env.dpy, win->xwin, w, h, win->env.depth);
  172. }
  173. void win_free_pixmap(win_t *win, Pixmap pm) {
  174. if (win && pm)
  175. XFreePixmap(win->env.dpy, pm);
  176. }
  177. void win_draw_pixmap(win_t *win, Pixmap pm, int x, int y, int w, int h) {
  178. if (win)
  179. XCopyArea(win->env.dpy, pm, win->pm, bgc, 0, 0, w, h, x, y);
  180. }
  181. void win_clear(win_t *win) {
  182. win_env_t *e;
  183. XGCValues gcval;
  184. if (!win)
  185. return;
  186. e = &win->env;
  187. if (win->pm)
  188. XFreePixmap(e->dpy, win->pm);
  189. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  190. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
  191. XChangeGC(e->dpy, bgc, GCForeground, &gcval);
  192. XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
  193. }
  194. void win_draw(win_t *win) {
  195. if (!win)
  196. return;
  197. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  198. XClearWindow(win->env.dpy, win->xwin);
  199. }
  200. void win_set_title(win_t *win, const char *title) {
  201. if (!win)
  202. return;
  203. if (!title)
  204. title = "sxiv";
  205. XStoreName(win->env.dpy, win->xwin, title);
  206. XSetIconName(win->env.dpy, win->xwin, title);
  207. XChangeProperty(win->env.dpy, win->xwin,
  208. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  209. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  210. PropModeReplace, (unsigned char *) title, strlen(title));
  211. XChangeProperty(win->env.dpy, win->xwin,
  212. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  213. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  214. PropModeReplace, (unsigned char *) title, strlen(title));
  215. }
  216. void win_set_cursor(win_t *win, win_cur_t cursor) {
  217. if (!win)
  218. return;
  219. switch (cursor) {
  220. case CURSOR_HAND:
  221. XDefineCursor(win->env.dpy, win->xwin, chand);
  222. break;
  223. case CURSOR_WATCH:
  224. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  225. break;
  226. case CURSOR_ARROW:
  227. default:
  228. XDefineCursor(win->env.dpy, win->xwin, carrow);
  229. break;
  230. }
  231. }