home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / utilities / isfast / libtk / window.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-02  |  17.2 KB  |  553 lines

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