home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src-tk / window.c < prev   
Encoding:
C/C++ Source or Header  |  1998-01-31  |  19.7 KB  |  606 lines

  1. #if !defined(FX)
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "gltk.h"
  7. #include "private.h"
  8.  
  9. /******************************************************************************/
  10.  
  11. Display *xDisplay = 0;
  12. int xScreen = 0; 
  13. Window wRoot = 0;
  14. Atom deleteWindowAtom;
  15. WINDOW_REC w = {
  16.     0, 0, 300, 300, TK_RGB|TK_SINGLE|TK_DIRECT
  17. };
  18. float colorMaps[] = {
  19.     0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 1.000000, 
  20.     0.000000, 1.000000, 0.333333, 0.776471, 0.443137, 0.556863, 
  21.     0.443137, 0.556863, 0.219608, 0.666667, 0.666667, 0.333333, 
  22.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  23.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  24.     0.666667, 0.333333, 0.039216, 0.078431, 0.117647, 0.156863, 
  25.     0.200000, 0.239216, 0.278431, 0.317647, 0.356863, 0.400000, 
  26.     0.439216, 0.478431, 0.517647, 0.556863, 0.600000, 0.639216, 
  27.     0.678431, 0.717647, 0.756863, 0.800000, 0.839216, 0.878431, 
  28.     0.917647, 0.956863, 0.000000, 0.000000, 0.000000, 0.000000, 
  29.     0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059, 
  30.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  31.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  32.     0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020, 
  33.     0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000, 
  34.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  35.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  36.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  37.     0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039, 
  38.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  39.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  40.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  41.     1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 
  42.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  43.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  44.     0.247059, 0.247059, 0.498039, 0.498039, 0.498039, 0.498039, 
  45.     0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020, 
  46.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  47.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  48.     1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  49.     0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059, 
  50.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  51.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  52.     0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020, 
  53.     0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000, 
  54.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  55.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  56.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  57.     0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039, 
  58.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  59.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  60.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  61.     1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 
  62.     1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 1.000000, 
  63.     0.333333, 0.443137, 0.776471, 0.556863, 0.443137, 0.219608, 
  64.     0.556863, 0.666667, 0.666667, 0.333333, 0.666667, 0.333333, 
  65.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  66.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  67.     0.039216, 0.078431, 0.117647, 0.156863, 0.200000, 0.239216, 
  68.     0.278431, 0.317647, 0.356863, 0.400000, 0.439216, 0.478431, 
  69.     0.517647, 0.556863, 0.600000, 0.639216, 0.678431, 0.717647, 
  70.     0.756863, 0.800000, 0.839216, 0.878431, 0.917647, 0.956863, 
  71.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  72.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  73.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  74.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  75.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  76.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  77.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  78.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  79.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  80.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  81.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  82.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  83.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  84.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  85.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  86.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  87.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  88.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  89.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  90.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  91.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  92.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  93.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  94.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  95.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  96.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  97.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  98.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  99.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  100.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  101.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  102.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  103.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  104.     0.854902, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  105.     1.000000, 1.000000, 1.000000, 1.000000, 0.333333, 0.443137, 
  106.     0.443137, 0.219608, 0.776471, 0.556863, 0.556863, 0.666667, 
  107.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  108.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  109.     0.666667, 0.333333, 0.666667, 0.333333, 0.039216, 0.078431, 
  110.     0.117647, 0.156863, 0.200000, 0.239216, 0.278431, 0.317647, 
  111.     0.356863, 0.400000, 0.439216, 0.478431, 0.517647, 0.556863, 
  112.     0.600000, 0.639216, 0.678431, 0.717647, 0.756863, 0.800000, 
  113.     0.839216, 0.878431, 0.917647, 0.956863, 0.000000, 0.000000, 
  114.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  115.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  116.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  117.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  118.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  119.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  120.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  121.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  122.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  123.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  124.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  125.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  126.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  127.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  128.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  129.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  130.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  131.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  132.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  133.     0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020, 
  134.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  135.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  136.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  137.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  138.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  139.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  140.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  141.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  142.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  143.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  144.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  145.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  146.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  147. };
  148. float tkRGBMap[8][3] = {
  149.     {
  150.     0, 0, 0
  151.     },
  152.     {
  153.     1, 0, 0
  154.     },
  155.     {
  156.     0, 1, 0
  157.     },
  158.     {
  159.     1, 1, 0
  160.     },
  161.     {
  162.     0, 0, 1
  163.     },
  164.     {
  165.     1, 0, 1
  166.     },
  167.     {
  168.     0, 1, 1
  169.     },
  170.     {
  171.     1, 1, 1
  172.     }
  173. };
  174.  
  175. /******************************************************************************/
  176.  
  177. void tkCloseWindow(void)
  178. {
  179.  
  180.     if (xDisplay) {
  181.     cursorNum = 0;
  182.  
  183.     ExposeFunc = 0;
  184.     ReshapeFunc = 0;
  185.     IdleFunc = 0;
  186.     DisplayFunc = 0;
  187.     KeyDownFunc = 0;
  188.     MouseDownFunc = 0;
  189.     MouseUpFunc = 0;
  190.     MouseMoveFunc = 0;
  191.  
  192.     glFlush();
  193.     glFinish();
  194.     if (TK_HAS_OVERLAY(w.type)) {
  195.         XDestroyWindow(xDisplay, w.wOverlay);
  196.         glXDestroyContext(xDisplay, w.cOverlay);
  197.         XFreeColormap(xDisplay, w.cMapOverlay);
  198.         XFree((char *)w.vInfoOverlay);
  199.     }
  200.     XDestroyWindow(xDisplay, w.wMain);
  201.     glXDestroyContext(xDisplay, w.cMain);
  202.         if (w.cMapAllocated)
  203.             XFreeColormap(xDisplay, w.cMapMain);
  204.     XFree((char *)w.vInfoMain);
  205.     XCloseDisplay(xDisplay);
  206.     xDisplay = 0;
  207.     }
  208. }
  209.  
  210. /******************************************************************************/
  211.  
  212. void tkInitDisplayMode(GLenum type)
  213. {
  214.  
  215.     w.type = type;
  216. }
  217.  
  218. /******************************************************************************/
  219.  
  220. void tkInitPosition(int x, int y, int width, int height)
  221. {
  222.  
  223.     w.x = x;
  224.     w.y = y;
  225.     w.w = width;
  226.     w.h = height;
  227. }
  228.  
  229. /******************************************************************************/
  230.  
  231. static int ErrorHandler(Display *xDisplay, XErrorEvent *event)
  232. {
  233.     char buf[80];
  234.  
  235.     printf("\nReceived X error!\n");
  236.     printf("\tError code   : %d\n", event->error_code);
  237.     printf("\tRequest code : %d\n", event->request_code);
  238.     printf("\tMinor code   : %d\n\n", event->minor_code);
  239.     XGetErrorText(xDisplay, event->error_code, buf, 80);
  240.     printf("\tError text : '%s'\n\n", buf);
  241.     return 0;
  242. }
  243.  
  244. static XVisualInfo *FindMainVisual(GLenum type)
  245. {
  246.     int list[32], i;
  247.  
  248.     i = 0;
  249.  
  250.     list[i++] = GLX_LEVEL;
  251.     list[i++] = 0;
  252.  
  253.     if (TK_IS_DOUBLE(type)) {
  254.     list[i++] = GLX_DOUBLEBUFFER;
  255.     }
  256.  
  257.     if (TK_IS_RGB(type)) {
  258.     list[i++] = GLX_RGBA;
  259.     list[i++] = GLX_RED_SIZE;
  260.     list[i++] = 1;
  261.     list[i++] = GLX_GREEN_SIZE;
  262.     list[i++] = 1;
  263.     list[i++] = GLX_BLUE_SIZE;
  264.     list[i++] = 1;
  265.     if (TK_HAS_ALPHA(type)) {
  266.         list[i++] = GLX_ALPHA_SIZE;
  267.         list[i++] = 1;
  268.     }
  269.     if (TK_HAS_ACCUM(type)) {
  270.         list[i++] = GLX_ACCUM_RED_SIZE;
  271.         list[i++] = 1;
  272.         list[i++] = GLX_ACCUM_GREEN_SIZE;
  273.         list[i++] = 1;
  274.         list[i++] = GLX_ACCUM_BLUE_SIZE;
  275.         list[i++] = 1;
  276.         if (TK_HAS_ALPHA(type)) {
  277.         list[i++] = GLX_ACCUM_ALPHA_SIZE;
  278.         list[i++] = 1;
  279.         }
  280.     }
  281.     } else if (TK_IS_INDEX(type)) {
  282.     list[i++] = GLX_BUFFER_SIZE;
  283.     list[i++] = 1;
  284.     }
  285.  
  286.     if (TK_HAS_DEPTH(type)) {
  287.     list[i++] = GLX_DEPTH_SIZE;
  288.     list[i++] = 1;
  289.     }
  290.  
  291.     if (TK_HAS_STENCIL(type)) {
  292.     list[i++] = GLX_STENCIL_SIZE;
  293.     list[i++] = 1;
  294.     }
  295.  
  296.     list[i] = (int)None;
  297.  
  298.     return glXChooseVisual(xDisplay, xScreen, list);
  299. }
  300.  
  301. static XVisualInfo *FindOverlayVisual(void)
  302. {
  303.     int list[10];
  304.  
  305.     list[0] = GLX_LEVEL;
  306.     list[1] = 1;
  307.     list[2] = (int)None;
  308. /*
  309.     list[2] = GLX_TRANSPARENT_TYPE;
  310.     list[3] = GLX_TRANSPARENT_INDEX;
  311.     list[4] = None;
  312. */
  313.     return glXChooseVisual(xDisplay, xScreen, list);
  314. }
  315.  
  316. static GLenum GetMainWindowType(XVisualInfo *vi)
  317. {
  318.     GLenum mask;
  319.     int x, y, z;
  320.  
  321.     mask = 0;
  322.  
  323.     glXGetConfig(xDisplay, vi, GLX_DOUBLEBUFFER, &x);
  324.     if (x) {
  325.     mask |= TK_DOUBLE;
  326.     } else {
  327.     mask |= TK_SINGLE;
  328.     }
  329.  
  330.     glXGetConfig(xDisplay, vi, GLX_RGBA, &x);
  331.     if (x) {
  332.     mask |= TK_RGB;
  333.     glXGetConfig(xDisplay, vi, GLX_ALPHA_SIZE, &x);
  334.     if (x > 0) {
  335.         mask |= TK_ALPHA;
  336.     }
  337.     glXGetConfig(xDisplay, vi, GLX_ACCUM_RED_SIZE, &x);
  338.     glXGetConfig(xDisplay, vi, GLX_ACCUM_GREEN_SIZE, &y);
  339.     glXGetConfig(xDisplay, vi, GLX_ACCUM_BLUE_SIZE, &z);
  340.     if (x > 0 && y > 0 && z > 0) {
  341.         mask |= TK_ACCUM;
  342.     }
  343.     } else {
  344.     mask |= TK_INDEX;
  345.     }
  346.  
  347.     glXGetConfig(xDisplay, vi, GLX_DEPTH_SIZE, &x);
  348.     if (x > 0) {
  349.     mask |= TK_DEPTH;
  350.     }
  351.  
  352.     glXGetConfig(xDisplay, vi, GLX_STENCIL_SIZE, &x);
  353.     if (x > 0) {
  354.     mask |= TK_STENCIL;
  355.     }
  356.  
  357.     if (glXIsDirect(xDisplay, w.cMain)) {
  358.     mask |= TK_DIRECT;
  359.     } else {
  360.     mask |= TK_INDIRECT;
  361.     }
  362.  
  363.     return mask;
  364. }
  365.  
  366. static int WaitForMainWindow(Display *d, XEvent *e, char *arg)
  367. {
  368.  
  369.     if (e->type == MapNotify && e->xmap.window == w.wMain) {
  370.     return GL_TRUE;
  371.     } else {
  372.     return GL_FALSE;
  373.     }
  374. }
  375.  
  376. static int WaitForOverlayWindow(Display *d, XEvent *e, char *arg)
  377. {
  378.  
  379.     if (e->type == MapNotify && e->xmap.window == w.wOverlay) {
  380.     return GL_TRUE;
  381.     } else {
  382.     return GL_FALSE;
  383.     }
  384. }
  385.  
  386. GLenum tkInitWindow(char *title)
  387. {
  388.     XSetWindowAttributes wa;
  389.     XTextProperty tp;
  390.     XSizeHints sh;
  391.     XEvent e;
  392.     int erb, evb;
  393.     GLenum overlayFlag;
  394.  
  395.     if (!xDisplay) {
  396.     xDisplay = XOpenDisplay(0);
  397.     if (!xDisplay) {
  398.         fprintf(stderr, "gltk: Can't connect to xDisplay!\n");
  399.         return GL_FALSE;
  400.     }
  401.     if (!glXQueryExtension(xDisplay, &erb, &evb)) {
  402.         fprintf(stderr, "gltk: No glx extension!\n");
  403.         return GL_FALSE;
  404.     }
  405.     xScreen = DefaultScreen(xDisplay);
  406.     wRoot = RootWindow(xDisplay, xScreen);
  407.     XSetErrorHandler(ErrorHandler);
  408.     }
  409.  
  410.     if (TK_HAS_OVERLAY(w.type)) {
  411.     overlayFlag = GL_TRUE;
  412.     } else {
  413.     overlayFlag = GL_FALSE;
  414.     }
  415.     w.type &= ~TK_OVERLAY;
  416.  
  417.     w.vInfoMain = FindMainVisual(w.type);
  418.     if (!w.vInfoMain) {
  419.        if (TK_IS_RGB(w.type)) {
  420.       fprintf(stderr, "gltk: Couldn't find visual for RGB mode!\n");
  421.        }
  422.        else {
  423.       fprintf(stderr, "gltk: Couldn't find visual for Color Index mode!\n");
  424.        }
  425.        xDisplay = 0;  /* to prevent crash in tkCloseWindow */
  426.        return GL_FALSE;
  427.     }
  428.  
  429.     /* make sure we get a reasonable visual type */
  430.     if (TK_IS_RGB(w.type)) {
  431.        /* any visual is OK */
  432.     }
  433.     else {
  434.        int xclass;
  435. #if defined(__cplusplus) || defined(c_plusplus)
  436.        xclass = w.vInfoMain->c_class;
  437. #else
  438.        xclass = w.vInfoMain->class;
  439. #endif
  440.        if (xclass==DirectColor || xclass==TrueColor) {
  441.           fprintf(stderr,
  442.             "gltk: Can't use DirectColor or TrueColor visual in CI mode!\n");
  443.           return GL_FALSE;
  444.        }
  445.     }
  446.  
  447.     w.cMain = glXCreateContext(xDisplay, w.vInfoMain, None,
  448.                    (TK_IS_DIRECT(w.type))?GL_TRUE:GL_FALSE);
  449.     if (!w.cMain) {
  450.     fprintf(stderr, "gltk: Can't create a context!\n");
  451.     return GL_FALSE;
  452.     }
  453.  
  454.     w.type = GetMainWindowType(w.vInfoMain);
  455.  
  456.     w.cMapAllocated = 1;
  457.     if (TK_IS_INDEX(w.type)) {
  458.         /* Color Indexed windows needs a writable colormap */
  459.     if (w.vInfoMain->class != StaticColor &&
  460.         w.vInfoMain->class != StaticGray) {
  461.         w.cMapMain = XCreateColormap(xDisplay, wRoot, w.vInfoMain->visual,
  462.                          AllocAll);
  463.     } else {
  464.         w.cMapMain = XCreateColormap(xDisplay, wRoot, w.vInfoMain->visual,
  465.                          AllocNone);
  466.     }
  467.     } else {
  468.         /* RGB colormap is AllocNone, share the root colormap if possible */
  469.     Screen *scr = DefaultScreenOfDisplay(xDisplay);
  470.     int scrnum = DefaultScreen(xDisplay);
  471.     if (MaxCmapsOfScreen(scr)==1
  472.         && w.vInfoMain->visual==DefaultVisual(xDisplay,scrnum)) {
  473.        /* the window and root are of the same visual type */
  474.        char *private_cmap = getenv("MESA_PRIVATE_CMAP");
  475.        if (private_cmap) {
  476.           /* user doesn't want to share colormaps */
  477.           w.cMapMain = XCreateColormap(xDisplay, wRoot,
  478.                        w.vInfoMain->visual, AllocNone);
  479.        }
  480.        else {
  481.           /* share the root colormap */
  482.           w.cMapMain = DefaultColormap(xDisplay,scrnum);
  483.        }
  484.     }
  485.     else {
  486.        /* window and root are different visual types, allocate new cmap */
  487. /****  ad@lms.be:  Added initialization for HP color recovery  *******/
  488.            Atom hp_cr_maps = XInternAtom(xDisplay, "_HP_RGB_SMOOTH_MAP_LIST", True);
  489.            w.cMapMain = 0;
  490.            if (hp_cr_maps) {
  491.                 XStandardColormap* colmaps = 0;
  492.                 int nrColmaps = 0;
  493.                 int i;
  494.                 XGetRGBColormaps( xDisplay, RootWindow(xDisplay, scrnum)
  495.                                 , &colmaps, &nrColmaps, hp_cr_maps);
  496.                 for (i=0; i<nrColmaps; i++) {
  497.                     if (colmaps[i].visualid == w.vInfoMain->visual->visualid) {
  498.                         w.cMapMain = colmaps[i].colormap;
  499.                         w.cMapAllocated = 0;
  500.                         break;
  501.                     }
  502.                 }
  503.                 if (colmaps) {
  504.                    XFree(colmaps);
  505.                 }
  506.            }  /*** end HP color recovery ***/
  507.            if (!w.cMapMain) {
  508.                if (w.vInfoMain->class==DirectColor) {
  509.                   w.cMapMain = XCreateColormap(xDisplay, wRoot,
  510.                                                w.vInfoMain->visual, AllocAll);
  511.                }
  512.                else {
  513.                   w.cMapMain = XCreateColormap(xDisplay, wRoot,
  514.                                                w.vInfoMain->visual, AllocNone);
  515.                }
  516.            }
  517.     }
  518.     }
  519.     if (TK_IS_INDEX(w.type) || w.vInfoMain->class==DirectColor) {
  520.        tkSetRGBMap(256, colorMaps);
  521.     }
  522.     wa.colormap = w.cMapMain;
  523.     wa.background_pixmap = None;
  524.     wa.border_pixel = 0;
  525.     wa.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask |
  526.             ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
  527.     w.wMain = XCreateWindow(xDisplay, wRoot, w.x, w.y, w.w, w.h, 0,
  528.                 w.vInfoMain->depth, InputOutput,
  529.                 w.vInfoMain->visual,
  530.                 CWBackPixmap|CWBorderPixel|CWEventMask|CWColormap,
  531.                 &wa);
  532.  
  533.     /*OLD: XInstallColormap( xDisplay, w.cMapMain );*/
  534.     XSetWMColormapWindows( xDisplay, w.wMain, &w.wMain, 1 );
  535.  
  536.     XStringListToTextProperty(&title, 1, &tp);
  537.     sh.flags = USPosition | USSize;
  538.     XSetWMProperties(xDisplay, w.wMain, &tp, &tp, 0, 0, &sh, 0, 0);
  539.     XFree(tp.value);
  540.     deleteWindowAtom = XInternAtom(xDisplay, "WM_DELETE_WINDOW", False);
  541.     XSetWMProtocols(xDisplay, w.wMain, &deleteWindowAtom, 1);
  542.     XMapWindow(xDisplay, w.wMain);
  543.     drawAllowFlag = GL_FALSE;
  544.     XIfEvent(xDisplay, &e, WaitForMainWindow, 0);
  545.  
  546.     if (overlayFlag == GL_TRUE) {
  547.     w.vInfoOverlay = FindOverlayVisual();
  548.     if (w.vInfoOverlay) {
  549.         w.cOverlay = glXCreateContext(xDisplay, w.vInfoOverlay, None,
  550.                       GL_TRUE);
  551.         w.cMapOverlay = XCreateColormap(xDisplay, wRoot,
  552.                         w.vInfoOverlay->visual,AllocNone);
  553.         tkSetOverlayMap(256, colorMaps);
  554.         wa.colormap = w.cMapOverlay;
  555.         wa.background_pixmap = None;
  556.         wa.border_pixel = 0;
  557.         w.wOverlay = XCreateWindow(xDisplay, w.wMain, 0, 0, w.w, w.h, 0,
  558.                        w.vInfoOverlay->depth, InputOutput,
  559.                        w.vInfoOverlay->visual,
  560.                        CWBackPixmap|CWBorderPixel|CWColormap,
  561.                        &wa);
  562.         XMapWindow(xDisplay, w.wOverlay);
  563.         XSetWMColormapWindows(xDisplay, w.wMain, &w.wOverlay, 1);
  564.         w.type |= TK_OVERLAY;
  565.     } else {
  566.         fprintf(stderr, "gltk: Can't create a overlay plane!\n");
  567.     }
  568.     }
  569.  
  570.     if (!glXMakeCurrent(xDisplay, w.wMain, w.cMain)) {
  571.     fprintf(stderr, "gltk: Can't make window current drawable!\n");
  572.     return GL_FALSE;
  573.     }
  574.     XFlush(xDisplay);
  575.  
  576.     return GL_TRUE;
  577. }
  578.  
  579. /******************************************************************************/
  580.  
  581. void tkQuit(void)
  582. {
  583.  
  584.     tkCloseWindow();
  585.     exit(0);
  586. }
  587.  
  588. /******************************************************************************/
  589.  
  590. void tkSwapBuffers(void)
  591. {
  592.  
  593.     if (xDisplay) {
  594.     glXSwapBuffers(xDisplay, w.wMain);
  595.     }
  596. }
  597.  
  598. /******************************************************************************/
  599.  
  600. #else
  601.  
  602. /* This is here to avoid ANSI C "empty source file" warnings */
  603. static void no_op () {}
  604.  
  605. #endif    /* !FX */
  606.