Clone of mesa.
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.

win32_x11.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright (c) Nate Robins, 1997. */
  2. /* portions Copyright (c) Mark Kilgard, 1998. */
  3. /* This program is freely distributable without licensing fees
  4. and is provided without guarantee or warrantee expressed or
  5. implied. This program is -not- in the public domain. */
  6. #include "glutint.h"
  7. /* global variable that must be set for some functions to operate
  8. correctly. */
  9. HDC XHDC;
  10. XVisualInfo*
  11. XGetVisualInfo(Display* display, long mask, XVisualInfo* template, int* nitems)
  12. {
  13. /* KLUDGE: this function needs XHDC to be set to the HDC currently
  14. being operated on before it is invoked! */
  15. PIXELFORMATDESCRIPTOR* pfds;
  16. int i, n;
  17. n = DescribePixelFormat(XHDC, 0, 0, NULL);
  18. pfds = (PIXELFORMATDESCRIPTOR*)malloc(sizeof(PIXELFORMATDESCRIPTOR) * n);
  19. memset(pfds, 0, sizeof(PIXELFORMATDESCRIPTOR) * n);
  20. for (i = 0; i < n; i++) {
  21. DescribePixelFormat(XHDC, i + 1, sizeof(PIXELFORMATDESCRIPTOR), &pfds[i]);
  22. }
  23. *nitems = n;
  24. return pfds;
  25. }
  26. Colormap
  27. XCreateColormap(Display* display, Window root, Visual* visual, int alloc)
  28. {
  29. /* KLUDGE: this function needs XHDC to be set to the HDC currently
  30. being operated on before it is invoked! */
  31. PIXELFORMATDESCRIPTOR pfd;
  32. LOGPALETTE *logical;
  33. HPALETTE palette;
  34. int n;
  35. /* grab the pixel format */
  36. memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  37. DescribePixelFormat(XHDC, GetPixelFormat(XHDC),
  38. sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  39. if (!(pfd.dwFlags & PFD_NEED_PALETTE ||
  40. pfd.iPixelType == PFD_TYPE_COLORINDEX))
  41. {
  42. return 0;
  43. }
  44. n = 1 << pfd.cColorBits;
  45. /* allocate a bunch of memory for the logical palette (assume 256
  46. colors in a Win32 palette */
  47. logical = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +
  48. sizeof(PALETTEENTRY) * n);
  49. memset(logical, 0, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * n);
  50. /* set the entries in the logical palette */
  51. logical->palVersion = 0x300;
  52. logical->palNumEntries = n;
  53. /* start with a copy of the current system palette */
  54. GetSystemPaletteEntries(XHDC, 0, 256, &logical->palPalEntry[0]);
  55. if (pfd.iPixelType == PFD_TYPE_RGBA) {
  56. int redMask = (1 << pfd.cRedBits) - 1;
  57. int greenMask = (1 << pfd.cGreenBits) - 1;
  58. int blueMask = (1 << pfd.cBlueBits) - 1;
  59. int i;
  60. /* fill in an RGBA color palette */
  61. for (i = 0; i < n; ++i) {
  62. logical->palPalEntry[i].peRed =
  63. (((i >> pfd.cRedShift) & redMask) * 255) / redMask;
  64. logical->palPalEntry[i].peGreen =
  65. (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
  66. logical->palPalEntry[i].peBlue =
  67. (((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
  68. logical->palPalEntry[i].peFlags = 0;
  69. }
  70. }
  71. palette = CreatePalette(logical);
  72. free(logical);
  73. SelectPalette(XHDC, palette, FALSE);
  74. RealizePalette(XHDC);
  75. return palette;
  76. }
  77. void
  78. XAllocColorCells(Display* display, Colormap colormap, Bool contig,
  79. unsigned long plane_masks_return[], unsigned int nplanes,
  80. unsigned long pixels_return[], unsigned int npixels)
  81. {
  82. /* NOP -- we did all the allocating in XCreateColormap! */
  83. }
  84. void
  85. XStoreColor(Display* display, Colormap colormap, XColor* color)
  86. {
  87. /* KLUDGE: set XHDC to 0 if the palette should NOT be realized after
  88. setting the color. set XHDC to the correct HDC if it should. */
  89. PALETTEENTRY pe;
  90. /* X11 stores color from 0-65535, Win32 expects them to be 0-256, so
  91. twiddle the bits ( / 256). */
  92. pe.peRed = color->red / 256;
  93. pe.peGreen = color->green / 256;
  94. pe.peBlue = color->blue / 256;
  95. /* make sure we use this flag, otherwise the colors might get mapped
  96. to another place in the colormap, and when we glIndex() that
  97. color, it may have moved (argh!!) */
  98. pe.peFlags = PC_NOCOLLAPSE;
  99. /* the pixel field of the XColor structure is the index into the
  100. colormap */
  101. SetPaletteEntries(colormap, color->pixel, 1, &pe);
  102. if (XHDC) {
  103. UnrealizeObject(colormap);
  104. SelectPalette(XHDC, colormap, FALSE);
  105. RealizePalette(XHDC);
  106. }
  107. }
  108. void
  109. XSetWindowColormap(Display* display, Window window, Colormap colormap)
  110. {
  111. HDC hdc = GetDC(window);
  112. /* if the third parameter is FALSE, the logical colormap is copied
  113. into the device palette when the application is in the
  114. foreground, if it is TRUE, the colors are mapped into the current
  115. palette in the best possible way. */
  116. SelectPalette(hdc, colormap, FALSE);
  117. RealizePalette(hdc);
  118. /* note that we don't have to release the DC, since our window class
  119. uses the WC_OWNDC flag! */
  120. }
  121. Bool
  122. XTranslateCoordinates(Display *display, Window src, Window dst,
  123. int src_x, int src_y,
  124. int* dest_x_return, int* dest_y_return,
  125. Window* child_return)
  126. {
  127. /* KLUDGE: this isn't really a translate coordinates into some other
  128. windows coordinate system...it only translates coordinates into the
  129. root window (screen) coordinate system. */
  130. POINT point;
  131. point.x = src_x;
  132. point.y = src_y;
  133. ClientToScreen(src, &point);
  134. *dest_x_return = point.x;
  135. *dest_y_return = point.y;
  136. /* just to make compilers happy...we don't use the return value. */
  137. return True;
  138. }
  139. Status
  140. XGetGeometry(Display* display, Window window, Window* root_return,
  141. int* x_return, int* y_return,
  142. unsigned int* width_return, unsigned int* height_return,
  143. unsigned int *border_width_return, unsigned int* depth_return)
  144. {
  145. /* KLUDGE: doesn't return the border_width or depth or root, x & y
  146. are in screen coordinates. */
  147. RECT rect;
  148. POINT point;
  149. GetClientRect(window, &rect);
  150. point.x = 0;
  151. point.y = 0;
  152. ClientToScreen(window, &point);
  153. *x_return = point.x;
  154. *y_return = point.y;
  155. *width_return = rect.right;
  156. *height_return = rect.bottom;
  157. /* just to make compilers happy...we don't use the return value. */
  158. return 1;
  159. }
  160. int
  161. DisplayWidthMM(Display* display, int screen)
  162. {
  163. int width;
  164. HWND hwnd = GetDesktopWindow();
  165. HDC hdc = GetDC(hwnd);
  166. width = GetDeviceCaps(hdc, HORZSIZE);
  167. /* make sure to release this DC (it's the desktops, not ours) */
  168. ReleaseDC(hwnd, hdc);
  169. return width;
  170. }
  171. int
  172. DisplayHeightMM(Display* display, int screen)
  173. {
  174. int height;
  175. HWND hwnd = GetDesktopWindow();
  176. HDC hdc = GetDC(hwnd);
  177. height = GetDeviceCaps(hdc, VERTSIZE);
  178. /* make sure to release this DC (it's the desktops, not ours) */
  179. ReleaseDC(hwnd, hdc);
  180. return height;
  181. }
  182. void
  183. XWarpPointer(Display* display, Window src, Window dst,
  184. int src_x, int src_y, int src_width, int src_height,
  185. int dst_x, int dst_y)
  186. {
  187. /* KLUDGE: this isn't really a warp pointer into some other windows
  188. coordinate system...it only warps the pointer into the root window
  189. (screen) coordinate system. */
  190. POINT point;
  191. point.x = dst_x;
  192. point.y = dst_y;
  193. ClientToScreen(dst, &point);
  194. SetCursorPos(point.x, point.y);
  195. }
  196. int
  197. XPending(Display* display)
  198. {
  199. /* similar functionality...I don't think that it is exact, but this
  200. will have to do. */
  201. MSG msg;
  202. return PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
  203. }
  204. /* the following function was stolen from the X sources as indicated. */
  205. /* Copyright Massachusetts Institute of Technology 1985, 1986, 1987 */
  206. /* $XConsortium: XParseGeom.c,v 11.18 91/02/21 17:23:05 rws Exp $ */
  207. /*
  208. Permission to use, copy, modify, distribute, and sell this software and its
  209. documentation for any purpose is hereby granted without fee, provided that
  210. the above copyright notice appear in all copies and that both that
  211. copyright notice and this permission notice appear in supporting
  212. documentation, and that the name of M.I.T. not be used in advertising or
  213. publicity pertaining to distribution of the software without specific,
  214. written prior permission. M.I.T. makes no representations about the
  215. suitability of this software for any purpose. It is provided "as is"
  216. without express or implied warranty.
  217. */
  218. /*
  219. * XParseGeometry parses strings of the form
  220. * "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
  221. * width, height, xoffset, and yoffset are unsigned integers.
  222. * Example: "=80x24+300-49"
  223. * The equal sign is optional.
  224. * It returns a bitmask that indicates which of the four values
  225. * were actually found in the string. For each value found,
  226. * the corresponding argument is updated; for each value
  227. * not found, the corresponding argument is left unchanged.
  228. */
  229. static int
  230. ReadInteger(char *string, char **NextString)
  231. {
  232. register int Result = 0;
  233. int Sign = 1;
  234. if (*string == '+')
  235. string++;
  236. else if (*string == '-')
  237. {
  238. string++;
  239. Sign = -1;
  240. }
  241. for (; (*string >= '0') && (*string <= '9'); string++)
  242. {
  243. Result = (Result * 10) + (*string - '0');
  244. }
  245. *NextString = string;
  246. if (Sign >= 0)
  247. return (Result);
  248. else
  249. return (-Result);
  250. }
  251. int XParseGeometry(char *string, int *x, int *y, unsigned int *width, unsigned int *height)
  252. {
  253. int mask = NoValue;
  254. register char *strind;
  255. unsigned int tempWidth, tempHeight;
  256. int tempX, tempY;
  257. char *nextCharacter;
  258. if ( (string == NULL) || (*string == '\0')) return(mask);
  259. if (*string == '=')
  260. string++; /* ignore possible '=' at beg of geometry spec */
  261. strind = (char *)string;
  262. if (*strind != '+' && *strind != '-' && *strind != 'x') {
  263. tempWidth = ReadInteger(strind, &nextCharacter);
  264. if (strind == nextCharacter)
  265. return (0);
  266. strind = nextCharacter;
  267. mask |= WidthValue;
  268. }
  269. if (*strind == 'x' || *strind == 'X') {
  270. strind++;
  271. tempHeight = ReadInteger(strind, &nextCharacter);
  272. if (strind == nextCharacter)
  273. return (0);
  274. strind = nextCharacter;
  275. mask |= HeightValue;
  276. }
  277. if ((*strind == '+') || (*strind == '-')) {
  278. if (*strind == '-') {
  279. strind++;
  280. tempX = -ReadInteger(strind, &nextCharacter);
  281. if (strind == nextCharacter)
  282. return (0);
  283. strind = nextCharacter;
  284. mask |= XNegative;
  285. }
  286. else
  287. { strind++;
  288. tempX = ReadInteger(strind, &nextCharacter);
  289. if (strind == nextCharacter)
  290. return(0);
  291. strind = nextCharacter;
  292. }
  293. mask |= XValue;
  294. if ((*strind == '+') || (*strind == '-')) {
  295. if (*strind == '-') {
  296. strind++;
  297. tempY = -ReadInteger(strind, &nextCharacter);
  298. if (strind == nextCharacter)
  299. return(0);
  300. strind = nextCharacter;
  301. mask |= YNegative;
  302. }
  303. else
  304. {
  305. strind++;
  306. tempY = ReadInteger(strind, &nextCharacter);
  307. if (strind == nextCharacter)
  308. return(0);
  309. strind = nextCharacter;
  310. }
  311. mask |= YValue;
  312. }
  313. }
  314. /* If strind isn't at the end of the string the it's an invalid
  315. geometry specification. */
  316. if (*strind != '\0') return (0);
  317. if (mask & XValue)
  318. *x = tempX;
  319. if (mask & YValue)
  320. *y = tempY;
  321. if (mask & WidthValue)
  322. *width = tempWidth;
  323. if (mask & HeightValue)
  324. *height = tempHeight;
  325. return (mask);
  326. }