home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src-glut / win32_x11.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  14.0 KB  |  570 lines

  1.  
  2. /* Copyright (c) Nate Robins, 1997. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8.  
  9. #include <stdio.h>
  10. #include "win32_x11.h"
  11.  
  12.  
  13. /* global variable that must be set for some functions to operate
  14.    correctly. */
  15. HDC XHDC;
  16.  
  17.  
  18. Window
  19. XCreateWindow(Display* display, Window parent, int x, int y,
  20.           unsigned int width, unsigned int height, unsigned int border,
  21.           int depth, unsigned int class, Visual* visual, 
  22.           unsigned long valuemask, XSetWindowAttributes* attributes)
  23. {
  24.   /* KLUDGE: make a window within the GLUT class (registered in
  25.      glut_init.c).  If the parent exists, make this a child window,
  26.      otherwise, make it top-level.  */
  27.  
  28.   return CreateWindow("GLUT", "GLUT", WS_CLIPSIBLINGS | WS_CLIPCHILDREN |
  29.               (parent ? WS_CHILD : WS_OVERLAPPEDWINDOW),
  30.               x, y, width, height, parent, NULL,
  31.               GetModuleHandle(NULL), 0);
  32. }
  33.  
  34. XVisualInfo*
  35. XGetVisualInfo(Display* display, long mask, XVisualInfo* template, int* nitems)
  36. {
  37.   /* KLUDGE: this function needs XHDC to be set to the HDC currently
  38.      being operated on before it is invoked! */
  39.  
  40.   PIXELFORMATDESCRIPTOR* pfds;
  41.   int i, n;
  42.  
  43.   n = DescribePixelFormat(XHDC, 0, 0, NULL);
  44.   pfds = (PIXELFORMATDESCRIPTOR*)malloc(sizeof(PIXELFORMATDESCRIPTOR) * n);
  45.   memset(pfds, 0, sizeof(PIXELFORMATDESCRIPTOR) * n);
  46.   
  47.   for (i = 0; i < n; i++) {
  48.     DescribePixelFormat(XHDC, i, sizeof(PIXELFORMATDESCRIPTOR), &pfds[i]);
  49.   }
  50.  
  51.   *nitems = n;
  52.   return pfds;
  53. }
  54.  
  55. Colormap
  56. XCreateColormap(Display* display, Window root, Visual* visual, int alloc)
  57. {
  58.   /* KLUDGE: this function needs XHDC to be set to the HDC currently
  59.      being operated on before it is invoked! */
  60.  
  61.   PIXELFORMATDESCRIPTOR pfd;
  62.   LOGPALETTE *logical;
  63.   HPALETTE    palette;
  64.   int n;
  65.  
  66.   /* grab the pixel format */
  67.   memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  68.   DescribePixelFormat(XHDC, GetPixelFormat(XHDC), 
  69.               sizeof(PIXELFORMATDESCRIPTOR), &pfd);
  70.  
  71.   if (!(pfd.dwFlags & PFD_NEED_PALETTE ||
  72.       pfd.iPixelType == PFD_TYPE_COLORINDEX))
  73.   {
  74. #if 0
  75.     printf("XCreateColormap(): no palette needed.\n");
  76. #endif
  77.     return 0;
  78.   }
  79.  
  80.   n = 1 << pfd.cColorBits;
  81.  
  82.   /* allocate a bunch of memory for the logical palette (assume 256
  83.      colors in a Win32 palette */
  84.   logical = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +
  85.                 sizeof(PALETTEENTRY) * n);
  86.   memset(logical, 0, sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * n);
  87.  
  88.   /* set the entries in the logical palette */
  89.   logical->palVersion = 0x300;
  90.   logical->palNumEntries = n;
  91.  
  92.   /* start with a copy of the current system palette */
  93.   GetSystemPaletteEntries(XHDC, 0, 256, &logical->palPalEntry[0]);
  94.     
  95.   if (pfd.iPixelType == PFD_TYPE_RGBA) {
  96.     int redMask = (1 << pfd.cRedBits) - 1;
  97.     int greenMask = (1 << pfd.cGreenBits) - 1;
  98.     int blueMask = (1 << pfd.cBlueBits) - 1;
  99.     int i;
  100.  
  101. #if 0
  102.     printf("XCreateColormap(): creating RGB ramp colormap of (%d) "
  103.        "colors. %d %d %d %d %d %d\n", n, redMask, greenMask, 
  104.        blueMask, pfd.cRedShift, pfd.cGreenShift, pfd.cBlueShift);
  105. #endif
  106.  
  107.     /* fill in an RGBA color palette */
  108.     for (i = 0; i < n; ++i) {
  109.       logical->palPalEntry[i].peRed = 
  110.     (((i >> pfd.cRedShift)   & redMask)   * 255) / redMask;
  111.       logical->palPalEntry[i].peGreen = 
  112.     (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
  113.     logical->palPalEntry[i].peBlue = 
  114.     (((i >> pfd.cBlueShift)  & blueMask)  * 255) / blueMask;
  115.       logical->palPalEntry[i].peFlags = 0;
  116.     }
  117.   }
  118.  
  119.   palette = CreatePalette(logical);
  120.   free(logical);
  121.  
  122.   SelectPalette(XHDC, palette, FALSE);
  123.   RealizePalette(XHDC);
  124.  
  125.   return palette;
  126. }
  127.  
  128. void
  129. XAllocColorCells(Display* display, Colormap colormap, Bool contig, 
  130.          unsigned long plane_masks_return[], unsigned int nplanes,
  131.          unsigned long pixels_return[], unsigned int npixels)
  132. {
  133.   /* NOP -- we did all the allocating in XCreateColormap! */
  134. }
  135.  
  136. void
  137. XStoreColor(Display* display, Colormap colormap, XColor* color)
  138. {
  139.   /* KLUDGE: set XHDC to 0 if the palette should NOT be realized after
  140.      setting the color.  set XHDC to the correct HDC if it should. */
  141.  
  142.   PALETTEENTRY pe;
  143.  
  144.   /* X11 stores color from 0-65535, Win32 expects them to be 0-256, so
  145.      twiddle the bits ( / 256). */
  146.   pe.peRed = color->red / 256;
  147.   pe.peGreen = color->green / 256;
  148.   pe.peBlue = color->blue / 256;
  149.  
  150.   /* make sure we use this flag, otherwise the colors might get mapped
  151.      to another place in the colormap, and when we glIndex() that
  152.      color, it may have moved (argh!!) */
  153.   pe.peFlags = PC_NOCOLLAPSE;
  154.  
  155.   /* the pixel field of the XColor structure is the index into the
  156.      colormap */
  157.   SetPaletteEntries(colormap, color->pixel, 1, &pe);
  158.  
  159.   if (XHDC) {
  160.     UnrealizeObject(colormap);
  161.     SelectPalette(XHDC, colormap, FALSE);
  162.     RealizePalette(XHDC);
  163.   }
  164. }
  165.  
  166. void
  167. XSetWindowColormap(Display* display, Window window, Colormap colormap)
  168. {
  169.   HDC hdc = GetDC(window);
  170.  
  171.   /* if the third parameter is FALSE, the logical colormap is copied
  172.      into the device palette when the application is in the
  173.      foreground, if it is TRUE, the colors are mapped into the current
  174.      palette in the best possible way. */
  175.   SelectPalette(hdc, colormap, FALSE);
  176.   RealizePalette(hdc);
  177.  
  178.   /* note that we don't have to release the DC, since our window class
  179.      uses the WC_OWNDC flag! */
  180. }
  181.  
  182. void
  183. XFreeColormap(Display* display, Colormap colormap)
  184. {
  185.   /* nothing magic about this. */
  186.   DeleteObject(colormap);
  187. }
  188.  
  189. Cursor
  190. XCreateFontCursor(Display* display, char* shape)
  191. {
  192.   /* the actual XCreateFontCursor takes an unsigned int, but Win32
  193.      cursors are char*'s. */
  194.   return LoadCursor(NULL, shape);
  195. }
  196.  
  197. void
  198. XDefineCursor(Display* display, Window window, Cursor cursor)
  199. {
  200.   /* not too much magic here. */
  201.   SetCursor(cursor);
  202. }
  203.  
  204. void
  205. XFlush(Display* display)
  206. {
  207.   /* this really isn't needed in Win32 the same way it is in X11, so
  208.      it'll be a nop for now.  It could be GdiFlush(), if you REALLY
  209.      wanted an equivalent. */
  210.  
  211.   /* GdiFlush(); */
  212. }
  213.  
  214. Bool
  215. XTranslateCoordinates(Display *display, Window src, Window dst, 
  216.               int src_x, int src_y, 
  217.               int* dest_x_return, int* dest_y_return,
  218.               Window* child_return)
  219. {
  220.   /* KLUDGE: this isn't really a translate coordinates into some other
  221.   windows coordinate system...it only translates coordinates into the
  222.   root window (screen) coordinate system. */
  223.  
  224.   POINT point;
  225.  
  226.   point.x = src_x;
  227.   point.y = src_y;
  228.  
  229.   ClientToScreen(src, &point);
  230.  
  231.   *dest_x_return = point.x;
  232.   *dest_y_return = point.y;
  233.  
  234.   /* just to make compilers happy...we don't use the return value. */
  235.   return True;
  236. }
  237.  
  238. Status
  239. XGetGeometry(Display* display, Window window, Window* root_return, 
  240.          int* x_return, int* y_return, 
  241.          unsigned int* width_return, unsigned int* height_return,
  242.          unsigned int *border_width_return, unsigned int* depth_return)
  243. {
  244.   /* KLUDGE: doesn't return the border_width or depth or root, x & y
  245.      are in screen coordinates. */
  246.  
  247.   RECT rect;
  248.   POINT point;
  249.  
  250.   GetClientRect(window, &rect);
  251.  
  252.   point.x = 0;
  253.   point.y = 0;
  254.   ClientToScreen(window, &point);
  255.  
  256.   *x_return = point.x;
  257.   *y_return = point.y;
  258.   *width_return = rect.right;
  259.   *height_return = rect.bottom;
  260.  
  261.   /* just to make compilers happy...we don't use the return value. */
  262.   return 1;  
  263. }
  264.  
  265. int
  266. DisplayWidth(Display* display, int screen)
  267. {
  268.   /* not much magic here. */
  269.   return GetSystemMetrics(SM_CXSCREEN);  
  270. }
  271.  
  272. int
  273. DisplayHeight(Display* display, int screen)
  274. {
  275.   /* not much magic here. */
  276.   return GetSystemMetrics(SM_CYSCREEN);
  277. }
  278.  
  279. int
  280. DisplayWidthMM(Display* display, int screen)
  281. {
  282.   int width;
  283.   HWND hwnd = GetDesktopWindow();
  284.   HDC hdc = GetDC(hwnd);
  285.   
  286.   width = GetDeviceCaps(hdc, HORZSIZE);
  287.  
  288.   /* make sure to release this DC (it's the desktops, not ours) */
  289.   ReleaseDC(hwnd, hdc);
  290.  
  291.   return width;
  292. }
  293.  
  294. int
  295. DisplayHeightMM(Display* display, int screen)
  296. {
  297.   int height;
  298.   HWND hwnd = GetDesktopWindow();
  299.   HDC hdc = GetDC(hwnd);
  300.   
  301.   height = GetDeviceCaps(hdc, VERTSIZE);
  302.  
  303.   /* make sure to release this DC (it's the desktops, not ours) */
  304.   ReleaseDC(hwnd, hdc);
  305.  
  306.   return height;
  307. }
  308.  
  309. void
  310. XMapWindow(Display* display, Window window)
  311. {
  312.   /* no magic here */
  313.   ShowWindow(window, SW_SHOWNORMAL);
  314. }
  315.  
  316. void
  317. XUnmapWindow(Display* display, Window window)
  318. {
  319.   /* no magic here */
  320.   ShowWindow(window, SW_HIDE);
  321. }
  322.  
  323. void
  324. XIconifyWindow(Display* display, Window window, int screen)
  325. {
  326.   /* no magic here */
  327.   ShowWindow(window, SW_MINIMIZE);
  328. }
  329.  
  330. void
  331. XWithdrawWindow(Display* display, Window window, int screen)
  332. {
  333.   /* no magic here */
  334.   ShowWindow(window, SW_HIDE);
  335. }
  336.  
  337. void
  338. XLowerWindow(Display* display, Window window)
  339. {
  340.   /* little magic here */
  341.   SetWindowPos(window, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  342. }
  343.  
  344. void
  345. XWarpPointer(Display* display, Window src, Window dst, 
  346.          int src_x, int src_y, int src_width, int src_height,
  347.          int dst_x, int dst_y)
  348. {
  349.   /* KLUDGE: this isn't really a warp pointer into some other windows
  350.   coordinate system...it only warps the pointer into the root window
  351.   (screen) coordinate system. */
  352.  
  353.   POINT point;
  354.  
  355.   point.x = dst_x;
  356.   point.y = dst_y;
  357.   ClientToScreen(dst, &point);
  358.  
  359.   SetCursorPos(point.x, point.y);
  360. }
  361.  
  362. void
  363. XSetWMName(Display* display, Window window, XTextProperty *tp)
  364. {
  365.   /* pretty simple in Win32. */
  366.   SetWindowText(window, tp->value);
  367. }
  368.  
  369. void
  370. XSetWMIconName(Display* display, Window window, XTextProperty *tp)
  371. {
  372.   /* there really isn't a way to set the icon name separate from the
  373.      windows name in Win32, so, just set the windows name. */
  374.   XSetWMName(display, window, tp);
  375. }
  376.  
  377. int
  378. XPending(Display* display)
  379. {
  380.   /* similar functionality...I don't think that it is exact, but this
  381.      will have to do. */
  382.   MSG msg;
  383.  
  384.   return PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
  385. }
  386.  
  387. void
  388. XDestroyWindow(Display* display, Window window)
  389. {
  390.   /* nothing magic about this. */
  391.   DestroyWindow(window);
  392. }
  393.  
  394. void
  395. XFree(void* data)
  396. {
  397.   /* anything that needs to be freed was allocated with malloc in our
  398.      fake X windows library for Win32, so free it with plain old
  399.      free(). */
  400.   free(data);
  401. }
  402.  
  403. void
  404. XUngrabPointer(Display* display, int time)
  405. {
  406.   /* nothing to be done for this...the pointer is always 'ungrabbed'
  407.      in Win32. */
  408. }
  409.  
  410.  
  411. /* the following function was stolen from the X sources as indicated. */
  412.  
  413. /* Copyright     Massachusetts Institute of Technology  1985, 1986, 1987 */
  414. /* $XConsortium: XParseGeom.c,v 11.18 91/02/21 17:23:05 rws Exp $ */
  415.  
  416. /*
  417. Permission to use, copy, modify, distribute, and sell this software and its
  418. documentation for any purpose is hereby granted without fee, provided that
  419. the above copyright notice appear in all copies and that both that
  420. copyright notice and this permission notice appear in supporting
  421. documentation, and that the name of M.I.T. not be used in advertising or
  422. publicity pertaining to distribution of the software without specific,
  423. written prior permission.  M.I.T. makes no representations about the
  424. suitability of this software for any purpose.  It is provided "as is"
  425. without express or implied warranty.
  426. */
  427.  
  428. /* 
  429.  *Returns pointer to first char ins search which is also in what, else NULL.
  430.  */
  431. static char *strscan (search, what)
  432. char *search, *what;
  433. {
  434.     int i, len = strlen (what);
  435.     char c;
  436.  
  437.     while ((c = *(search++)) != (int)NULL)
  438.         for (i = 0; i < len; i++)
  439.             if (c == what [i])
  440.                 return (--search);
  441.     return (NULL);
  442. }
  443.  
  444. /*
  445.  *    XParseGeometry parses strings of the form
  446.  *   "=<width>x<height>{+-}<xoffset>{+-}<yoffset>", where
  447.  *   width, height, xoffset, and yoffset are unsigned integers.
  448.  *   Example:  "=80x24+300-49"
  449.  *   The equal sign is optional.
  450.  *   It returns a bitmask that indicates which of the four values
  451.  *   were actually found in the string.  For each value found,
  452.  *   the corresponding argument is updated;  for each value
  453.  *   not found, the corresponding argument is left unchanged. 
  454.  */
  455.  
  456. static int
  457. ReadInteger(string, NextString)
  458. register char *string;
  459. char **NextString;
  460. {
  461.     register int Result = 0;
  462.     int Sign = 1;
  463.     
  464.     if (*string == '+')
  465.     string++;
  466.     else if (*string == '-')
  467.     {
  468.     string++;
  469.     Sign = -1;
  470.     }
  471.     for (; (*string >= '0') && (*string <= '9'); string++)
  472.     {
  473.     Result = (Result * 10) + (*string - '0');
  474.     }
  475.     *NextString = string;
  476.     if (Sign >= 0)
  477.     return (Result);
  478.     else
  479.     return (-Result);
  480. }
  481.  
  482. int XParseGeometry (string, x, y, width, height)
  483. char *string;
  484. int *x, *y;
  485. unsigned int *width, *height;    /* RETURN */
  486. {
  487.     int mask = NoValue;
  488.     register char *strind;
  489.     unsigned int tempWidth, tempHeight;
  490.     int tempX, tempY;
  491.     char *nextCharacter;
  492.  
  493.     if ( (string == NULL) || (*string == '\0')) return(mask);
  494.     if (*string == '=')
  495.         string++;  /* ignore possible '=' at beg of geometry spec */
  496.  
  497.     strind = (char *)string;
  498.     if (*strind != '+' && *strind != '-' && *strind != 'x') {
  499.         tempWidth = ReadInteger(strind, &nextCharacter);
  500.         if (strind == nextCharacter) 
  501.             return (0);
  502.         strind = nextCharacter;
  503.         mask |= WidthValue;
  504.     }
  505.  
  506.     if (*strind == 'x' || *strind == 'X') {    
  507.         strind++;
  508.         tempHeight = ReadInteger(strind, &nextCharacter);
  509.         if (strind == nextCharacter)
  510.             return (0);
  511.         strind = nextCharacter;
  512.         mask |= HeightValue;
  513.     }
  514.  
  515.     if ((*strind == '+') || (*strind == '-')) {
  516.         if (*strind == '-') {
  517.               strind++;
  518.             tempX = -ReadInteger(strind, &nextCharacter);
  519.             if (strind == nextCharacter)
  520.                 return (0);
  521.             strind = nextCharacter;
  522.             mask |= XNegative;
  523.  
  524.         }
  525.         else
  526.         {    strind++;
  527.             tempX = ReadInteger(strind, &nextCharacter);
  528.             if (strind == nextCharacter)
  529.                 return(0);
  530.             strind = nextCharacter;
  531.         }
  532.         mask |= XValue;
  533.         if ((*strind == '+') || (*strind == '-')) {
  534.             if (*strind == '-') {
  535.                 strind++;
  536.                 tempY = -ReadInteger(strind, &nextCharacter);
  537.                 if (strind == nextCharacter)
  538.                         return(0);
  539.                 strind = nextCharacter;
  540.                 mask |= YNegative;
  541.  
  542.             }
  543.             else
  544.             {
  545.                 strind++;
  546.                 tempY = ReadInteger(strind, &nextCharacter);
  547.                 if (strind == nextCharacter)
  548.                         return(0);
  549.                 strind = nextCharacter;
  550.             }
  551.             mask |= YValue;
  552.         }
  553.     }
  554.     
  555.     /* If strind isn't at the end of the string the it's an invalid
  556.         geometry specification. */
  557.  
  558.     if (*strind != '\0') return (0);
  559.  
  560.     if (mask & XValue)
  561.         *x = tempX;
  562.      if (mask & YValue)
  563.         *y = tempY;
  564.     if (mask & WidthValue)
  565.             *width = tempWidth;
  566.     if (mask & HeightValue)
  567.             *height = tempHeight;
  568.     return (mask);
  569. }
  570.