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

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "tk.h"
  41. #include "private.h"
  42.  
  43. /******************************************************************************/
  44.  
  45. Display *xDisplay = 0;
  46. int xScreen = 0; 
  47. Window wRoot = 0;
  48. Atom deleteWindowAtom;
  49. WINDOW_REC w = {
  50.     0, 0, 300, 300, TK_RGB|TK_SINGLE|TK_DIRECT
  51. };
  52. float colorMaps[] = {
  53.     0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 1.000000, 
  54.     0.000000, 1.000000, 0.333333, 0.776471, 0.443137, 0.556863, 
  55.     0.443137, 0.556863, 0.219608, 0.666667, 0.666667, 0.333333, 
  56.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  57.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  58.     0.666667, 0.333333, 0.039216, 0.078431, 0.117647, 0.156863, 
  59.     0.200000, 0.239216, 0.278431, 0.317647, 0.356863, 0.400000, 
  60.     0.439216, 0.478431, 0.517647, 0.556863, 0.600000, 0.639216, 
  61.     0.678431, 0.717647, 0.756863, 0.800000, 0.839216, 0.878431, 
  62.     0.917647, 0.956863, 0.000000, 0.000000, 0.000000, 0.000000, 
  63.     0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059, 
  64.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  65.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  66.     0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020, 
  67.     0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000, 
  68.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  69.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  70.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  71.     0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039, 
  72.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  73.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  74.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  75.     1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 
  76.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  77.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  78.     0.247059, 0.247059, 0.498039, 0.498039, 0.498039, 0.498039, 
  79.     0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020, 
  80.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  81.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  82.     1.000000, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  83.     0.000000, 0.000000, 0.000000, 0.000000, 0.247059, 0.247059, 
  84.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  85.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  86.     0.498039, 0.498039, 0.749020, 0.749020, 0.749020, 0.749020, 
  87.     0.749020, 0.749020, 0.749020, 0.749020, 1.000000, 1.000000, 
  88.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  89.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  90.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  91.     0.247059, 0.247059, 0.247059, 0.247059, 0.498039, 0.498039, 
  92.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  93.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  94.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  95.     1.000000, 1.000000, 1.000000, 1.000000, 0.000000, 0.000000, 
  96.     1.000000, 1.000000, 0.000000, 0.000000, 1.000000, 1.000000, 
  97.     0.333333, 0.443137, 0.776471, 0.556863, 0.443137, 0.219608, 
  98.     0.556863, 0.666667, 0.666667, 0.333333, 0.666667, 0.333333, 
  99.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  100.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  101.     0.039216, 0.078431, 0.117647, 0.156863, 0.200000, 0.239216, 
  102.     0.278431, 0.317647, 0.356863, 0.400000, 0.439216, 0.478431, 
  103.     0.517647, 0.556863, 0.600000, 0.639216, 0.678431, 0.717647, 
  104.     0.756863, 0.800000, 0.839216, 0.878431, 0.917647, 0.956863, 
  105.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  106.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  107.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  108.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  109.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  110.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  111.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  112.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  113.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  114.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  115.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  116.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  117.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  118.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  119.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  120.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  121.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  122.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  123.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  124.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  125.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  126.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  127.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  128.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  129.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  130.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  131.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  132.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  133.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  134.     0.854902, 1.000000, 0.000000, 0.141176, 0.282353, 0.427451, 
  135.     0.568627, 0.713726, 0.854902, 1.000000, 0.000000, 0.141176, 
  136.     0.282353, 0.427451, 0.568627, 0.713726, 0.854902, 1.000000, 
  137.     0.000000, 0.141176, 0.282353, 0.427451, 0.568627, 0.713726, 
  138.     0.854902, 1.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  139.     1.000000, 1.000000, 1.000000, 1.000000, 0.333333, 0.443137, 
  140.     0.443137, 0.219608, 0.776471, 0.556863, 0.556863, 0.666667, 
  141.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  142.     0.666667, 0.333333, 0.666667, 0.333333, 0.666667, 0.333333, 
  143.     0.666667, 0.333333, 0.666667, 0.333333, 0.039216, 0.078431, 
  144.     0.117647, 0.156863, 0.200000, 0.239216, 0.278431, 0.317647, 
  145.     0.356863, 0.400000, 0.439216, 0.478431, 0.517647, 0.556863, 
  146.     0.600000, 0.639216, 0.678431, 0.717647, 0.756863, 0.800000, 
  147.     0.839216, 0.878431, 0.917647, 0.956863, 0.000000, 0.000000, 
  148.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  149.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  150.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  151.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  152.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  153.     0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 
  154.     0.000000, 0.000000, 0.247059, 0.247059, 0.247059, 0.247059, 
  155.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  156.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  157.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  158.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  159.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  160.     0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 0.247059, 
  161.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  162.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  163.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  164.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  165.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  166.     0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 0.498039, 
  167.     0.498039, 0.498039, 0.498039, 0.498039, 0.749020, 0.749020, 
  168.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  169.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  170.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  171.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  172.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  173.     0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 0.749020, 
  174.     0.749020, 0.749020, 1.000000, 1.000000, 1.000000, 1.000000, 
  175.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  176.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  177.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  178.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  179.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  180.     1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 
  181. };
  182. float tkRGBMap[8][3] = {
  183.     {
  184.     0, 0, 0
  185.     },
  186.     {
  187.     1, 0, 0
  188.     },
  189.     {
  190.     0, 1, 0
  191.     },
  192.     {
  193.     1, 1, 0
  194.     },
  195.     {
  196.     0, 0, 1
  197.     },
  198.     {
  199.     1, 0, 1
  200.     },
  201.     {
  202.     0, 1, 1
  203.     },
  204.     {
  205.     1, 1, 1
  206.     }
  207. };
  208.  
  209. /******************************************************************************/
  210.  
  211. void tkCloseWindow(void)
  212. {
  213.  
  214.     if (xDisplay) {
  215.     cursorNum = 0;
  216.  
  217.     ExposeFunc = 0;
  218.     ReshapeFunc = 0;
  219.     IdleFunc = 0;
  220.     DisplayFunc = 0;
  221.     KeyDownFunc = 0;
  222.     MouseDownFunc = 0;
  223.     MouseUpFunc = 0;
  224.     MouseMoveFunc = 0;
  225.  
  226.     glFlush();
  227.     glFinish();
  228.     if (TK_HAS_OVERLAY(w.type)) {
  229.         XDestroyWindow(xDisplay, w.wOverlay);
  230.         glXDestroyContext(xDisplay, w.cOverlay);
  231.         XFreeColormap(xDisplay, w.cMapOverlay);
  232.         XFree((char *)w.vInfoOverlay);
  233.     }
  234.     XDestroyWindow(xDisplay, w.wMain);
  235.     glXDestroyContext(xDisplay, w.cMain);
  236.     XFreeColormap(xDisplay, w.cMapMain);
  237.     XFree((char *)w.vInfoMain);
  238.     XCloseDisplay(xDisplay);
  239.     xDisplay = 0;
  240.     }
  241. }
  242.  
  243. /******************************************************************************/
  244.  
  245. void tkInitDisplayMode(GLenum type)
  246. {
  247.  
  248.     w.type = type;
  249. }
  250.  
  251. /******************************************************************************/
  252.  
  253. void tkInitPosition(int x, int y, int width, int height)
  254. {
  255.  
  256.     w.x = x;
  257.     w.y = y;
  258.     w.w = width;
  259.     w.h = height;
  260. }
  261.  
  262. /******************************************************************************/
  263.  
  264. static int ErrorHandler(Display *xDisplay, XErrorEvent *event)
  265. {
  266.     char buf[80];
  267.  
  268.     printf("\nReceived X error!\n");
  269.     printf("\tError code   : %d\n", event->error_code);
  270.     printf("\tRequest code : %d\n", event->request_code);
  271.     printf("\tMinor code   : %d\n\n", event->minor_code);
  272.     XGetErrorText(xDisplay, event->error_code, buf, 80);
  273.     printf("\tError text : '%s'\n\n", buf);
  274.     return 0;
  275. }
  276.  
  277. static XVisualInfo *FindMainVisual(GLenum type)
  278. {
  279.     int list[32], i;
  280.  
  281.     i = 0;
  282.  
  283.     list[i++] = GLX_LEVEL;
  284.     list[i++] = 0;
  285.  
  286.     if (TK_IS_DOUBLE(type)) {
  287.     list[i++] = GLX_DOUBLEBUFFER;
  288.     }
  289.  
  290.     if (TK_IS_RGB(type)) {
  291.     list[i++] = GLX_RGBA;
  292.     list[i++] = GLX_RED_SIZE;
  293.     list[i++] = 1;
  294.     list[i++] = GLX_GREEN_SIZE;
  295.     list[i++] = 1;
  296.     list[i++] = GLX_BLUE_SIZE;
  297.     list[i++] = 1;
  298.     if (TK_HAS_ALPHA(type)) {
  299.         list[i++] = GLX_ALPHA_SIZE;
  300.         list[i++] = 1;
  301.     } else {
  302.         list[i++] = GLX_ALPHA_SIZE;
  303.         list[i++] = 0;
  304.     }
  305.     if (TK_HAS_ACCUM(type)) {
  306.         list[i++] = GLX_ACCUM_RED_SIZE;
  307.         list[i++] = 1;
  308.         list[i++] = GLX_ACCUM_GREEN_SIZE;
  309.         list[i++] = 1;
  310.         list[i++] = GLX_ACCUM_BLUE_SIZE;
  311.         list[i++] = 1;
  312.         if (TK_HAS_ALPHA(type)) {
  313.         list[i++] = GLX_ACCUM_ALPHA_SIZE;
  314.         list[i++] = 1;
  315.         } else {
  316.         list[i++] = GLX_ACCUM_ALPHA_SIZE;
  317.         list[i++] = 0;
  318.         }
  319.     }
  320.     } else if (TK_IS_INDEX(type)) {
  321.     list[i++] = GLX_BUFFER_SIZE;
  322.     list[i++] = 1;
  323.     }
  324.  
  325.     if (TK_HAS_DEPTH(type)) {
  326.     list[i++] = GLX_DEPTH_SIZE;
  327.     list[i++] = 1;
  328.     }
  329.  
  330.     if (TK_HAS_STENCIL(type)) {
  331.     list[i++] = GLX_STENCIL_SIZE;
  332.     list[i++] = 1;
  333.     }
  334.  
  335.     list[i] = (int)None;
  336.  
  337.     return glXChooseVisual(xDisplay, xScreen, list);
  338. }
  339.  
  340. static XVisualInfo *FindOverlayVisual(void)
  341. {
  342.     int list[3];
  343.  
  344.     list[0] = GLX_LEVEL;
  345.     list[1] = 1;
  346.     list[2] = (int)None;
  347.  
  348.     return glXChooseVisual(xDisplay, xScreen, list);
  349. }
  350.  
  351. static GLenum GetMainWindowType(XVisualInfo *vi)
  352. {
  353.     GLenum mask;
  354.     int x, y, z;
  355.  
  356.     mask = 0;
  357.  
  358.     glXGetConfig(xDisplay, vi, GLX_DOUBLEBUFFER, &x);
  359.     if (x) {
  360.     mask |= TK_DOUBLE;
  361.     } else {
  362.     mask |= TK_SINGLE;
  363.     }
  364.  
  365.     glXGetConfig(xDisplay, vi, GLX_RGBA, &x);
  366.     if (x) {
  367.     mask |= TK_RGB;
  368.     glXGetConfig(xDisplay, vi, GLX_ALPHA_SIZE, &x);
  369.     if (x > 0) {
  370.         mask |= TK_ALPHA;
  371.     }
  372.     glXGetConfig(xDisplay, vi, GLX_ACCUM_RED_SIZE, &x);
  373.     glXGetConfig(xDisplay, vi, GLX_ACCUM_GREEN_SIZE, &y);
  374.     glXGetConfig(xDisplay, vi, GLX_ACCUM_BLUE_SIZE, &z);
  375.     if (x > 0 && y > 0 && z > 0) {
  376.         mask |= TK_ACCUM;
  377.     }
  378.     } else {
  379.     mask |= TK_INDEX;
  380.     }
  381.  
  382.     glXGetConfig(xDisplay, vi, GLX_DEPTH_SIZE, &x);
  383.     if (x > 0) {
  384.     mask |= TK_DEPTH;
  385.     }
  386.  
  387.     glXGetConfig(xDisplay, vi, GLX_STENCIL_SIZE, &x);
  388.     if (x > 0) {
  389.     mask |= TK_STENCIL;
  390.     }
  391.  
  392.     if (glXIsDirect(xDisplay, w.cMain)) {
  393.     mask |= TK_DIRECT;
  394.     } else {
  395.     mask |= TK_INDIRECT;
  396.     }
  397.  
  398.     return mask;
  399. }
  400.  
  401. static int WaitForMainWindow(Display *d, XEvent *e, char *arg)
  402. {
  403.  
  404.     if (e->type == MapNotify && e->xmap.window == w.wMain) {
  405.     return GL_TRUE;
  406.     } else {
  407.     return GL_FALSE;
  408.     }
  409. }
  410.  
  411. static int WaitForOverlayWindow(Display *d, XEvent *e, char *arg)
  412. {
  413.  
  414.     if (e->type == MapNotify && e->xmap.window == w.wOverlay) {
  415.     return GL_TRUE;
  416.     } else {
  417.     return GL_FALSE;
  418.     }
  419. }
  420.  
  421. GLenum tkInitWindow(char *title)
  422. {
  423.     XSetWindowAttributes wa;
  424.     XTextProperty tp;
  425.     XSizeHints sh;
  426.     XEvent e;
  427.     int erb, evb;
  428.     GLenum overlayFlag;
  429.  
  430.     if (!xDisplay) {
  431.     xDisplay = XOpenDisplay(0);
  432.     if (!xDisplay) {
  433.         fprintf(stderr, "Can't connect to xDisplay!\n");
  434.         return GL_FALSE;
  435.     }
  436.     if (!glXQueryExtension(xDisplay, &erb, &evb)) {
  437.         fprintf(stderr, "No glx extension!\n");
  438.         return GL_FALSE;
  439.     }
  440.     xScreen = DefaultScreen(xDisplay);
  441.     wRoot = RootWindow(xDisplay, xScreen);
  442.     XSetErrorHandler(ErrorHandler);
  443.     }
  444.  
  445.     if (TK_HAS_OVERLAY(w.type)) {
  446.     overlayFlag = GL_TRUE;
  447.     } else {
  448.     overlayFlag = GL_FALSE;
  449.     }
  450.     w.type &= ~TK_OVERLAY;
  451.  
  452.     w.vInfoMain = FindMainVisual(w.type);
  453.     if (!w.vInfoMain) {
  454.     fprintf(stderr, "Window type not found!\n");
  455.     return GL_FALSE;
  456.     }
  457.  
  458.     w.cMain = glXCreateContext(xDisplay, w.vInfoMain, None,
  459.                    (TK_IS_DIRECT(w.type))?GL_TRUE:GL_FALSE);
  460.     if (!w.cMain) {
  461.     fprintf(stderr, "Can't create a context!\n");
  462.     return GL_FALSE;
  463.     }
  464.  
  465.     w.type = GetMainWindowType(w.vInfoMain);
  466.  
  467.     if (TK_IS_INDEX(w.type)) {
  468.     if (w.vInfoMain->class != StaticColor &&
  469.         w.vInfoMain->class != StaticGray) {
  470.         w.cMapMain = XCreateColormap(xDisplay, wRoot, w.vInfoMain->visual,
  471.                          AllocAll);
  472.     } else {
  473.         w.cMapMain = XCreateColormap(xDisplay, wRoot, w.vInfoMain->visual,
  474.                          AllocNone);
  475.     }
  476.     } else {
  477.     w.cMapMain = XCreateColormap(xDisplay, wRoot, w.vInfoMain->visual,
  478.                      AllocNone);
  479.     }
  480.     tkSetRGBMap(256, colorMaps);
  481.     wa.colormap = w.cMapMain;
  482.     wa.background_pixmap = None;
  483.     wa.border_pixel = 0;
  484.     wa.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask |
  485.             ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
  486.     w.wMain = XCreateWindow(xDisplay, wRoot, w.x, w.y, w.w, w.h, 0,
  487.                 w.vInfoMain->depth, InputOutput,
  488.                 w.vInfoMain->visual,
  489.                 CWBackPixmap|CWBorderPixel|CWEventMask|CWColormap,
  490.                 &wa);
  491.  
  492.     XStringListToTextProperty(&title, 1, &tp);
  493.     sh.flags = USPosition | USSize;
  494.     XSetWMProperties(xDisplay, w.wMain, &tp, &tp, 0, 0, &sh, 0, 0);
  495.     deleteWindowAtom = XInternAtom(xDisplay, "WM_DELETE_WINDOW", False);
  496.     XSetWMProtocols(xDisplay, w.wMain, &deleteWindowAtom, 1);
  497.     XMapWindow(xDisplay, w.wMain);
  498.     drawAllowFlag = GL_FALSE;
  499.     XIfEvent(xDisplay, &e, WaitForMainWindow, 0);
  500.  
  501.     if (overlayFlag == GL_TRUE) {
  502.     w.vInfoOverlay = FindOverlayVisual();
  503.     if (w.vInfoOverlay) {
  504.         w.cOverlay = glXCreateContext(xDisplay, w.vInfoOverlay, None,
  505.                       GL_TRUE);
  506.         w.cMapOverlay = XCreateColormap(xDisplay, wRoot,
  507.                         w.vInfoOverlay->visual, AllocNone);
  508.         tkSetOverlayMap(256, colorMaps);
  509.         wa.colormap = w.cMapOverlay;
  510.         wa.background_pixmap = None;
  511.         wa.border_pixel = 0;
  512.         w.wOverlay = XCreateWindow(xDisplay, w.wMain, 0, 0, w.w, w.h, 0,
  513.                        w.vInfoOverlay->depth, InputOutput,
  514.                        w.vInfoOverlay->visual,
  515.                        CWBackPixmap|CWBorderPixel|CWColormap,
  516.                        &wa);
  517.         XMapWindow(xDisplay, w.wOverlay);
  518.         XSetWMColormapWindows(xDisplay, w.wMain, &w.wOverlay, 1);
  519.         w.type |= TK_OVERLAY;
  520.     } else {
  521.         fprintf(stderr, "Can't create a overlay plane!\n");
  522.     }
  523.     }
  524.  
  525.     if (!glXMakeCurrent(xDisplay, w.wMain, w.cMain)) {
  526.     return GL_FALSE;
  527.     }
  528.     XFlush(xDisplay);
  529.  
  530.     return GL_TRUE;
  531. }
  532.  
  533. /******************************************************************************/
  534.  
  535. void tkQuit(void)
  536. {
  537.  
  538.     tkCloseWindow();
  539.     exit(0);
  540. }
  541.  
  542. /******************************************************************************/
  543.  
  544. void tkSwapBuffers(void)
  545. {
  546.  
  547.     if (xDisplay) {
  548.     glXSwapBuffers(xDisplay, w.wMain);
  549.     }
  550. }
  551.  
  552. /******************************************************************************/
  553.