home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / src / xmesa1.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  39KB  |  1,253 lines

  1. /* $Id: xmesa1.c,v 1.24 1996/05/08 20:23:33 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995-1996  Brian Paul  (brianp@ssec.wisc.edu)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25. $Log: xmesa1.c,v $
  26.  * Revision 1.24  1996/05/08  20:23:33  brianp
  27.  * added MESA_XSYNC environment variable support
  28.  *
  29.  * Revision 1.23  1996/04/26  16:59:50  brianp
  30.  * new colormap handling, added colormap argument to XMesaBindPixmap()
  31.  *
  32.  * Revision 1.22  1996/04/25  21:09:20  brianp
  33.  * better XAllocColor error handling, prototyped new gl_new_context() func
  34.  *
  35.  * Revision 1.21  1996/03/02  21:41:05  brianp
  36.  * added PF_8R8G8B pixel format
  37.  *
  38.  * Revision 1.20  1996/03/01  20:04:55  brianp
  39.  * test for byte swapping and added a discussion of the subject
  40.  *
  41.  * Revision 1.19  1996/02/26  16:23:09  brianp
  42.  * added PF_5R6G5B pixel format from Joerg Hessdoerfer
  43.  *
  44.  * Revision 1.18  1996/02/23  17:13:37  brianp
  45.  * removed erroneous XImage byte swapping code
  46.  *
  47.  * Revision 1.17  1996/02/19  21:50:48  brianp
  48.  * better XImage->data allocation
  49.  *
  50.  * Revision 1.16  1996/02/09  22:22:39  brianp
  51.  * fixed default gamma value bug
  52.  *
  53.  * Revision 1.15  1996/02/06  03:25:43  brianp
  54.  * added new gamma correction code
  55.  *
  56.  * Revision 1.14  1996/01/22  21:05:04  brianp
  57.  * changed HPCR undithered pixelformat to PF_HPCR, per Alex De Bruyn
  58.  *
  59.  * Revision 1.13  1996/01/19  18:09:27  brianp
  60.  * fixed potential X error handler bug in alloc_shm_back_buffer()
  61.  *
  62.  * Revision 1.12  1996/01/07  22:51:16  brianp
  63.  * re-enable XSync() in glXSwapBuffers()
  64.  *
  65.  * Revision 1.11  1996/01/05  01:19:01  brianp
  66.  * added profiling to XMesaSwapBuffers()
  67.  *
  68.  * Revision 1.10  1995/12/21  17:58:13  brianp
  69.  * fixed the call to get_drawable_size in case of 8-byte integers
  70.  *
  71.  * Revision 1.9  1995/12/12  21:44:04  brianp
  72.  * only print color warning message if MESA_DEBUG is defined
  73.  *
  74.  * Revision 1.8  1995/11/30  00:52:30  brianp
  75.  * fixed a few type warnings for Sun compiler
  76.  *
  77.  * Revision 1.7  1995/11/30  00:20:56  brianp
  78.  * added new PF_GRAYSCALE mode
  79.  *
  80.  * Revision 1.6  1995/11/08  22:08:50  brianp
  81.  * added 8-bit TrueColor dithering
  82.  *
  83.  * Revision 1.5  1995/11/03  17:41:48  brianp
  84.  * removed unused vars, fixed code for C++ compilation
  85.  *
  86.  * Revision 1.4  1995/11/01  15:14:03  brianp
  87.  * renamed all class variables per Steven Spitz
  88.  *
  89.  * Revision 1.3  1995/10/30  15:13:01  brianp
  90.  * replaced Current variable with XMesa
  91.  *
  92.  * Revision 1.2  1995/10/19  15:53:20  brianp
  93.  * new arguments to gl_new_context()
  94.  *
  95.  * Revision 1.1  1995/10/17  21:36:55  brianp
  96.  * Initial revision
  97.  *
  98.  */
  99.  
  100.  
  101.  
  102. /*
  103.  * Mesa/X11 interface, part 1.
  104.  *
  105.  * This file contains the implementations of all the XMesa* functions.
  106.  *
  107.  *
  108.  * NOTES:
  109.  *
  110.  * The window coordinate system origin (0,0) is in the lower-left corner
  111.  * of the window.  X11's window coordinate origin is in the upper-left
  112.  * corner of the window.  Therefore, most drawing functions in this
  113.  * file have to flip Y coordinates.
  114.  *
  115.  * Define SHM in the Makefile with -DSHM if you want to compile in support
  116.  * for the MIT Shared Memory extension.  If enabled, when you use an Ximage
  117.  * for the back buffer in double buffered mode, the "swap" operation will
  118.  * be faster.  You must also link with -lXext.
  119.  *
  120.  * Byte swapping:  If the Mesa host and the X display use a different
  121.  * byte order then there's some trickiness to be aware of when using
  122.  * XImages.  The byte ordering used for the XImage is that of the X
  123.  * display, not the Mesa host.
  124.  * The color-to-pixel encoding for True/DirectColor must be done
  125.  * according to the display's visual red_mask, green_mask, and blue_mask.
  126.  * If XPutPixel is used to put a pixel into an XImage then XPutPixel will
  127.  * do byte swapping if needed.  If one wants to directly "poke" the pixel
  128.  * into the XImage's buffer then the pixel must be byte swapped first.  In
  129.  * Mesa, when byte swapping is needed we use the PF_TRUECOLOR pixel format
  130.  * and use XPutPixel everywhere except in the implementation of
  131.  * glClear(GL_COLOR_BUFFER_BIT).  We want this function to be fast so
  132.  * instead of using XPutPixel we "poke" our values after byte-swapping
  133.  * the clear pixel value if needed.
  134.  *
  135.  */
  136.  
  137.  
  138. #include <assert.h>
  139. #include <math.h>
  140. #include <stdio.h>
  141. #include <stdlib.h>
  142. #include <string.h>
  143. #include <X11/Xlib.h>
  144. #include <X11/Xutil.h>
  145. #ifdef SHM
  146. #  include <sys/ipc.h>
  147. #  include <sys/shm.h>
  148. #  include <X11/extensions/XShm.h>
  149. #endif
  150. #include "GL/xmesa.h"
  151. #include "xmesaP.h"
  152. #include "context.h"
  153. /*#include "dd.h"*/
  154. #include "macros.h"
  155. #include "xform.h"
  156.  
  157.  
  158.  
  159. XMesaContext XMesa = NULL;
  160.  
  161.  
  162. /*
  163.  * Lookup tables for HPCR pixel format:
  164.  */
  165. short hpcr_rTbl[256] = {
  166.  16,  16,  17,  17,  18,  18,  19,  19,  20,  20,  21,  21,  22,  22,  23,  23,
  167.  24,  24,  25,  25,  26,  26,  27,  27,  28,  28,  29,  29,  30,  30,  31,  31,
  168.  32,  32,  33,  33,  34,  34,  35,  35,  36,  36,  37,  37,  38,  38,  39,  39,
  169.  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
  170.  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
  171.  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
  172.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  173.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  174. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  175. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  176. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  177. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  178. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  179. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  180. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  181. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
  182. };
  183.  
  184. short hpcr_gTbl[256] = {
  185.  16,  16,  17,  17,  18,  18,  19,  19,  20,  20,  21,  21,  22,  22,  23,  23,
  186.  24,  24,  25,  25,  26,  26,  27,  27,  28,  28,  29,  29,  30,  30,  31,  31,
  187.  32,  32,  33,  33,  34,  34,  35,  35,  36,  36,  37,  37,  38,  38,  39,  39,
  188.  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
  189.  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
  190.  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
  191.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  192.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  193. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  194. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  195. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  196. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  197. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  198. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  199. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  200. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239
  201. };
  202.  
  203. short hpcr_bTbl[256] = {
  204.  32,  32,  33,  33,  34,  34,  35,  35,  36,  36,  37,  37,  38,  38,  39,  39,
  205.  40,  40,  41,  41,  42,  42,  43,  43,  44,  44,  45,  45,  46,  46,  47,  47,
  206.  48,  48,  49,  49,  50,  50,  51,  51,  52,  52,  53,  53,  54,  54,  55,  55,
  207.  56,  56,  57,  57,  58,  58,  59,  59,  60,  60,  61,  61,  62,  62,  63,  63,
  208.  64,  64,  65,  65,  66,  66,  67,  67,  68,  68,  69,  69,  70,  70,  71,  71,
  209.  72,  72,  73,  73,  74,  74,  75,  75,  76,  76,  77,  77,  78,  78,  79,  79,
  210.  80,  80,  81,  81,  82,  82,  83,  83,  84,  84,  85,  85,  86,  86,  87,  87,
  211.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  212.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  213. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  214. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  215. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  216. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  217. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  218. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  219. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223
  220. };
  221.  
  222.  
  223.  
  224. /**********************************************************************/
  225. /*****                      Private Functions                     *****/
  226. /**********************************************************************/
  227.  
  228.  
  229. /*
  230.  * X/Mesa Error reporting function:
  231.  */
  232. static void error( const char *msg )
  233. {
  234.    fprintf( stderr, "X/Mesa error: %s\n", msg );
  235. }
  236.  
  237.  
  238. /*
  239.  * Return the host's byte order as LSBFirst or MSBFirst ala X.
  240.  */
  241. static int host_byte_order( void )
  242. {
  243.    int i = 1;
  244.    char *cptr = (char *) &i;
  245.    return (*cptr==1) ? LSBFirst : MSBFirst;
  246. }
  247.  
  248.  
  249.  
  250. /*
  251.  * Error handling.
  252.  */
  253. static int mesaXErrorFlag = 0;
  254.  
  255. static int mesaHandleXError( Display *dpy, XErrorEvent *event )
  256. {
  257.     mesaXErrorFlag = 1;
  258.     return 0;
  259. }
  260.  
  261.  
  262. /*
  263.  * Check if the X Shared Memory extension is available.
  264.  * Return:  0 = not available
  265.  *          1 = shared XImage support available
  266.  *          2 = shared Pixmap support available also
  267.  */
  268. static int check_for_xshm( Display *display )
  269. {
  270. #ifdef SHM
  271.    int major, minor, ignore;
  272.    Bool pixmaps;
  273.  
  274.    if (XQueryExtension( display, "MIT-SHM", &ignore, &ignore, &ignore )) {
  275.       if (XShmQueryVersion( display, &major, &minor, &pixmaps )==True) {
  276.      return (pixmaps==True) ? 2 : 1;
  277.       }
  278.       else {
  279.      return 0;
  280.       }
  281.    }
  282.    else {
  283.       return 0;
  284.    }
  285. #else
  286.    /* Can't compile XSHM support */
  287.    return 0;
  288. #endif
  289. }
  290.  
  291.  
  292. /*
  293.  * Allocate a shared memory XImage back buffer for the given context.
  294.  * Return:  GL_TRUE if success, GL_FALSE if error
  295.  */
  296. static GLboolean alloc_shm_back_buffer( XMesaContext c )
  297. {
  298. #ifdef SHM
  299.    /*
  300.     * We have to do a _lot_ of error checking here to be sure we can
  301.     * really use the XSHM extension.  It seems different servers trigger
  302.     * errors at different points if the extension won't work.  Therefore
  303.     * we have to be very careful...
  304.     */
  305.    GC gc;
  306.    int (*old_handler)( Display *, XErrorEvent * );
  307.  
  308.    c->backimage = XShmCreateImage( c->display, c->visual, c->depth,
  309.                    ZPixmap, NULL, &c->shminfo,
  310.                    c->width, c->height );
  311.    if (c->backimage == NULL) {
  312.       error( "alloc_back_buffer: Shared memory error (XShmCreateImage), disabling." );
  313.       c->shm = 0;
  314.       return GL_FALSE;
  315.    }
  316.  
  317.    c->shminfo.shmid = shmget( IPC_PRIVATE, c->backimage->bytes_per_line
  318.                  * c->backimage->height, IPC_CREAT|0777 );
  319.    if (c->shminfo.shmid < 0) {
  320.       perror("alloc_back_buffer");
  321.       XDestroyImage( c->backimage );
  322.       c->backimage = NULL;
  323.       error( "alloc_back_buffer: Shared memory error (shmget), disabling." );
  324.       c->shm = 0;
  325.       return GL_FALSE;
  326.    }
  327.  
  328.    c->shminfo.shmaddr = c->backimage->data
  329.                       = (char*)shmat( c->shminfo.shmid, 0, 0 );
  330.    if (c->shminfo.shmaddr == (char *) -1) {
  331.       perror("alloc_back_buffer");
  332.       XDestroyImage( c->backimage );
  333.       c->backimage = NULL;
  334.       error("alloc_back_buffer: Shared memory error (shmat), disabling.");
  335.       c->shm = 0;
  336.       return GL_FALSE;
  337.    }
  338.  
  339.    c->shminfo.readOnly = False;
  340.    mesaXErrorFlag = 0;
  341.    old_handler = XSetErrorHandler( mesaHandleXError );
  342.    /* This may trigger the X protocol error we're ready to catch: */
  343.    XShmAttach( c->display, &c->shminfo );
  344.    XSync( c->display, False );
  345.  
  346.    if (mesaXErrorFlag) {
  347.       /* we are on a remote display, this error is normal, don't print it */
  348.       XFlush( c->display );
  349.       mesaXErrorFlag = 0;
  350.       XDestroyImage( c->backimage );
  351.       shmdt( c->shminfo.shmaddr );
  352.       shmctl( c->shminfo.shmid, IPC_RMID, 0 );
  353.       c->backimage = NULL;
  354.       c->shm = 0;
  355.       (void) XSetErrorHandler( old_handler );
  356.       return GL_FALSE;
  357.    }
  358.  
  359.    shmctl( c->shminfo.shmid, IPC_RMID, 0 ); /* nobody else needs it */
  360.  
  361.    /* Finally, try an XShmPutImage to be really sure the extension works */
  362.    gc = XCreateGC( c->display, c->frontbuffer, 0, NULL );
  363.    XShmPutImage( c->display, c->frontbuffer, gc,
  364.          c->backimage, 0, 0, 0, 0, 1, 1 /*one pixel*/, False );
  365.    XSync( c->display, False );
  366.    XFreeGC( c->display, gc );
  367.    (void) XSetErrorHandler( old_handler );
  368.    if (mesaXErrorFlag) {
  369.       XFlush( c->display );
  370.       mesaXErrorFlag = 0;
  371.       XDestroyImage( c->backimage );
  372.       shmdt( c->shminfo.shmaddr );
  373.       shmctl( c->shminfo.shmid, IPC_RMID, 0 );
  374.       c->backimage = NULL;
  375.       c->shm = 0;
  376.       return GL_FALSE;
  377.    }
  378.  
  379.    return GL_TRUE;
  380. #else
  381.    /* Can't compile XSHM support */
  382.    return GL_FALSE;
  383. #endif
  384. }
  385.  
  386.  
  387.  
  388. /*
  389.  * Setup an off-screen pixmap or Ximage to use as the back buffer.
  390.  * Input:  c - the X/Mesa context.
  391.  */
  392. void xmesa_alloc_back_buffer( XMesaContext c )
  393. {
  394.    if (c->db_state==BACK_XIMAGE) {
  395.       /* Deallocate the old backimage, if any */
  396.       if (c->backimage) {
  397. #ifdef SHM
  398.      if (c->shm) {
  399.         XShmDetach( c->display, &c->shminfo );
  400.         XDestroyImage( c->backimage );
  401.         shmdt( c->shminfo.shmaddr );
  402.      }
  403.      else
  404. #endif
  405.        XDestroyImage( c->backimage );
  406.      c->backimage = NULL;
  407.       }
  408.  
  409.       /* Allocate new back buffer */
  410.       if (c->shm==0 || alloc_shm_back_buffer(c)==GL_FALSE) {
  411.      /* Allocate a regular XImage for the back buffer. */
  412.      c->backimage = XCreateImage( c->display, c->visual, c->depth,
  413.                       ZPixmap, 0,   /* format, offset */
  414.                       NULL, c->width, c->height,
  415.                       8, 0 );  /* pad, bytes_per_line */
  416.      if (!c->backimage) {
  417.         error("alloc_back_buffer: XCreateImage failed.");
  418.      }
  419.          c->backimage->data = malloc( c->backimage->height
  420.                                       * c->backimage->bytes_per_line );
  421.          if (!c->backimage->data) {
  422.             error("alloc_back_buffer: malloc failed.");
  423.             XDestroyImage( c->backimage );
  424.             c->backimage = NULL;
  425.          }
  426.       }
  427.       c->backpixmap = None;
  428.    }
  429.    else if (c->db_state==BACK_PIXMAP) {
  430.       Pixmap old_pixmap = c->backpixmap;
  431.       /* Free the old back pixmap */
  432.       if (c->backpixmap) {
  433.      XFreePixmap( c->display, c->backpixmap );
  434.       }
  435.       /* Allocate new back pixmap */
  436.       c->backpixmap = XCreatePixmap( c->display, c->frontbuffer,
  437.                      c->width, c->height, c->depth );
  438.       c->backimage = NULL;
  439.       /* update other references to backpixmap */
  440.       if (c->buffer==old_pixmap) {
  441.      c->buffer = c->backpixmap;
  442.       }
  443.    }
  444. }
  445.  
  446.  
  447.  
  448. /*
  449.  * Return the width and height of the given drawable.
  450.  */
  451. static void get_drawable_size( Display *dpy, Drawable d,
  452.                    unsigned int *width, unsigned int *height )
  453. {
  454.    Window root;
  455.    int x, y;
  456.    unsigned int bw, depth;
  457.  
  458.    XGetGeometry( dpy, d, &root, &x, &y, width, height, &bw, &depth );
  459. }
  460.  
  461.  
  462.  
  463. /*
  464.  * Apply gamma correction to an intensity value in [0..max].  Return the
  465.  * new intensity value.
  466.  */
  467. static GLint gamma_adjust( GLfloat gamma, GLint value, GLint max )
  468. {
  469.    double x = (double) value / (double) max;
  470.    return (GLint) ((GLfloat) max * pow( x, 1.0F/gamma ) );
  471. }
  472.  
  473.  
  474.  
  475. /*
  476.  * A replacement for XAllocColor.  This function should never
  477.  * fail to allocate a color.  When XAllocColor fails, we return
  478.  * the nearest matching color.  If we have to allocate many colors
  479.  * this function isn't too efficient; the XQueryColors() could be
  480.  * done just once.
  481.  * Written by Michael Pichler, Brian Paul, Mark Kilgard
  482.  * Input:  dpy - X display
  483.  *         cmap - X colormap
  484.  *         cmapSize - size of colormap
  485.  * In/Out: color - the XColor struct
  486.  * Output:  exact - 1=exact color match, 0=closest match
  487.  */
  488. static void
  489. noFaultXAllocColor( Display *dpy, Colormap cmap, int cmapSize,
  490.                     XColor *color, int *exact )
  491. {
  492.   XColor *ctable, subColor;
  493.   int i, bestmatch;
  494.   double mindist;       /* 3*2^16^2 exceeds long int precision. 
  495.                          */
  496.  
  497.   for (;;) {
  498.     /* First try just using XAllocColor. */
  499.     if (XAllocColor(dpy, cmap, color)) {
  500.        *exact = 1;
  501.        return;
  502.     }
  503.  
  504.     /* Retrieve color table entries. */
  505.     /* XXX alloca candidate. */
  506.     ctable = (XColor *) malloc(cmapSize * sizeof(XColor));
  507.     for (i = 0; i < cmapSize; i++)
  508.       ctable[i].pixel = i;
  509.     XQueryColors(dpy, cmap, ctable, cmapSize);
  510.  
  511.     /* Find best match. */
  512.     bestmatch = -1;
  513.     mindist = 0.0;
  514.     for (i = 0; i < cmapSize; i++) {
  515.       double dr = (double) color->red - (double) ctable[i].red;
  516.       double dg = (double) color->green - (double) ctable[i].green;
  517.       double db = (double) color->blue - (double) ctable[i].blue;
  518.       double dist = dr * dr + dg * dg + db * db;
  519.       if (bestmatch < 0 || dist < mindist) {
  520.         bestmatch = i;
  521.         mindist = dist;
  522.       }
  523.     }
  524.  
  525.     /* Return result. */
  526.     subColor.red = ctable[bestmatch].red;
  527.     subColor.green = ctable[bestmatch].green;
  528.     subColor.blue = ctable[bestmatch].blue;
  529.     free(ctable);
  530.     if (XAllocColor(dpy, cmap, &subColor)) {
  531.       *color = subColor;
  532.       *exact = 0;
  533.       return;
  534.     }
  535.     /* Extremely unlikely, but possibly color was deallocated
  536.        and reallocated by someone else before we could
  537.        XAllocColor the color cell we located.  If so, loop
  538.        again... */
  539.   }
  540. }
  541.  
  542.  
  543.  
  544. /*
  545.  * Do setup for PF_GRAYSCALE pixel format.
  546.  */
  547. static int setup_grayscale( XMesaContext c, Window window, Colormap cmap )
  548. {
  549.    int gray, i;
  550.    int colorsfailed = 0;
  551.    XColor xcol;
  552.    XColor *ctable = NULL;
  553.  
  554.    if (c->depth<4 || c->depth>16 || !cmap) {
  555.       return 0;
  556.    }
  557.  
  558.    /* Allocate 256 shades of gray */
  559.    for (gray=0;gray<256;gray++) {
  560.       GLint r = gamma_adjust( c->RedGamma,   gray, 255 );
  561.       GLint g = gamma_adjust( c->GreenGamma, gray, 255 );
  562.       GLint b = gamma_adjust( c->BlueGamma,  gray, 255 );
  563.       int exact;
  564.  
  565.       xcol.red   = (r << 8) | r;
  566.       xcol.green = (g << 8) | g;
  567.       xcol.blue  = (b << 8) | b;
  568.  
  569.       noFaultXAllocColor( c->display, cmap, c->visual->map_entries,
  570.                           &xcol, &exact );
  571.       if (!exact) {
  572.          colorsfailed++;
  573.       }
  574.  
  575.       c->color_table[gray] = xcol.pixel;
  576.       c->pixel_to_r[xcol.pixel] = gray * 30 / 100;
  577.       c->pixel_to_g[xcol.pixel] = gray * 59 / 100;
  578.       c->pixel_to_b[xcol.pixel] = gray * 11 / 100;
  579.    }
  580.  
  581.    if (colorsfailed && getenv("MESA_DEBUG")) {
  582.       fprintf( stderr,
  583.                "Note: %d out of 256 needed colors do not match exactly.\n",
  584.            colorsfailed, 256 );
  585.    }
  586.  
  587. #define WEIGHT
  588. #ifdef WEIGHT
  589.    c->rmult = 30 * 255 / 100;
  590.    c->gmult = 59 * 255 / 100;
  591.    c->bmult = 11 * 255 / 100;
  592. #else
  593.    c->rmult = 255/3;
  594.    c->gmult = 255/3;
  595.    c->bmult = 255/3;
  596. #endif
  597.    c->dithered_pf = PF_GRAYSCALE;
  598.    c->undithered_pf = PF_GRAYSCALE;
  599.    c->pixel = c->color_table[255];      /* white */
  600.    c->clearpixel = c->color_table[0];   /* black */
  601.  
  602.    return 1;
  603. }
  604.  
  605.  
  606.  
  607. /*
  608.  * Setup RGB rendering for a window with a PseudoColor, StaticColor,
  609.  * or 8-bit TrueColor visual visual.  We try to allocate a palette of 225
  610.  * colors (5 red, 9 green, 5 blue) and dither to approximate a 24-bit RGB
  611.  * color.  While this function was originally designed just for 8-bit
  612.  * visuals, it has also proven to work from 4-bit up to 16-bit visuals.
  613.  * Dithering code contributed by Bob Mercier.
  614.  */
  615. static int setup_dithered_color( XMesaContext c, Window window, Colormap cmap )
  616. {
  617.    int r, g, b, i;
  618.    int colorsfailed = 0;
  619.    XColor xcol;
  620.    XColor *allcolors = NULL, *acptr; 
  621.  
  622.    if (c->depth<4 || c->depth>16 || !cmap) {
  623.       return 0;
  624.    }
  625.  
  626.    /* Allocate X colors and initialize color_table[], red_table[], etc */
  627.    for (r = 0; r < _R; r++) {
  628.       for (g = 0; g < _G; g++) {
  629.      for (b = 0; b < _B; b++) {
  630.             int exact;
  631.  
  632.         xcol.red   = gamma_adjust( c->RedGamma,   r*65535/(_R-1), 65535 );
  633.         xcol.green = gamma_adjust( c->GreenGamma, g*65535/(_G-1), 65535 );
  634.         xcol.blue  = gamma_adjust( c->BlueGamma,  b*65535/(_B-1), 65535 );
  635.  
  636.             noFaultXAllocColor( c->display, cmap, c->visual->map_entries,
  637.                                 &xcol, &exact );
  638.  
  639.             if (!exact) {
  640.                colorsfailed++;
  641.             }
  642.  
  643.         i = _MIX( r, g, b );
  644.         c->color_table[i] = xcol.pixel;
  645.         c->pixel_to_r[xcol.pixel] = r * 255 / (_R-1);
  646.         c->pixel_to_g[xcol.pixel] = g * 255 / (_G-1);
  647.         c->pixel_to_b[xcol.pixel] = b * 255 / (_B-1);
  648.      }
  649.       }
  650.    }
  651.  
  652.    if (colorsfailed && getenv("MESA_DEBUG")) {
  653.       fprintf( stderr,
  654.                "Note: %d out of %d needed colors do not match exactly.\n",
  655.            colorsfailed, _R*_G*_B );
  656.    }
  657.  
  658.    c->rmult = 255;
  659.    c->gmult = 255;
  660.    c->bmult = 255;
  661.    c->dithered_pf = PF_DITHER;
  662.    c->undithered_pf = PF_LOOKUP;
  663.    c->pixel = c->color_table[_MIX(_R-1,_G-1,_B-1)];  /* white */
  664.    c->clearpixel = c->color_table[_MIX(0,0,0)];      /* black */
  665.  
  666.    return 1;
  667. }
  668.  
  669.  
  670.  
  671. /*
  672.  * Setup RGB rendering for a window with a True/DirectColor visual.
  673.  */
  674. static int setup_truecolor( XMesaContext c, Window window, Colormap cmap )
  675. {
  676.    unsigned long rmask, gmask, bmask;
  677.  
  678.    /* Red */
  679.    c->rshift = 0;
  680.    rmask = c->visual->red_mask;
  681.    while ((rmask & 1)==0) {
  682.       c->rshift++;
  683.       rmask = rmask >> 1;
  684.    }
  685.    c->rmult = (GLint) rmask;
  686.  
  687.    /* Green */
  688.    c->gshift = 0;
  689.    gmask = c->visual->green_mask;
  690.    while ((gmask & 1)==0) {
  691.       c->gshift++;
  692.       gmask = gmask >> 1;
  693.    }
  694.    c->gmult = (GLint) gmask;
  695.  
  696.    /* Blue */
  697.    c->bshift = 0;
  698.    bmask = c->visual->blue_mask;
  699.    while ((bmask & 1)==0) {
  700.       c->bshift++;
  701.       bmask = bmask >> 1;
  702.    }
  703.    c->bmult = (GLint) bmask;
  704.  
  705.    if (   c->visual->red_mask  ==0x0000ff
  706.        && c->visual->green_mask==0x00ff00
  707.        && c->visual->blue_mask ==0xff0000
  708.        && host_byte_order()==ImageByteOrder(c->display) && sizeof(GLuint)==4
  709.        && c->RedGamma==1.0 && c->GreenGamma==1.0 && c->BlueGamma==1.0) {
  710.       /* common 24-bit config used on SGI, Sun */
  711.       c->undithered_pf = c->dithered_pf = PF_8A8B8G8R;
  712.       c->pixel = 0xffffffff;  /* white */
  713.       c->clearpixel = 0x0;    /* black */
  714.    }
  715.    else if (c->visual->red_mask  ==0xff0000
  716.        &&   c->visual->green_mask==0x00ff00
  717.        &&   c->visual->blue_mask ==0x0000ff
  718.        && host_byte_order()==ImageByteOrder(c->display) && sizeof(GLuint)==4
  719.        && c->RedGamma==1.0 && c->GreenGamma==1.0 && c->BlueGamma==1.0) {
  720.       /* common 24-bit config used on Linux, HP, IBM */
  721.       c->undithered_pf = c->dithered_pf = PF_8R8G8B;
  722.       c->pixel = 0xffffff;    /* white */
  723.       c->clearpixel = 0x0;    /* black */
  724.    }
  725.    else if (c->visual->red_mask  ==0xf800
  726.        &&   c->visual->green_mask==0x07e0
  727.        &&   c->visual->blue_mask ==0x001f
  728.        && host_byte_order()==ImageByteOrder(c->display) && sizeof(GLushort)==2
  729.        && c->RedGamma==1.0 && c->GreenGamma==1.0 && c->BlueGamma==1.0) {
  730.       /* 5-6-5 color weight on common PC VGA boards */
  731.       c->undithered_pf = c->dithered_pf = PF_5R6G5B;
  732.       c->pixel = 0xffff;      /* white */
  733.       c->clearpixel = 0x0;    /* black */
  734.    }
  735.    else if (c->visual->red_mask  ==0xe0
  736.        &&   c->visual->green_mask==0x1c
  737.        &&   c->visual->blue_mask ==0x03
  738.        && XInternAtom(c->display, "_HP_RGB_SMOOTH_MAP_LIST", True)) {
  739.       /* HP Color Recovery
  740.        * Contributed by:  Alex De Bruyn (ad@lms.be)
  741.        * To work properly, the atom _HP_RGB_SMOOTH_MAP_LIST must be defined
  742.        * on the root window AND the colormap obtainable by XGetRGBColormaps
  743.        * for that atom must be set on the window.  (see also tkInitWindow)
  744.        * If that colormap is not set, the output will look stripy.
  745.        */
  746.  
  747.       /* Setup color tables with gamma correction */
  748.       int i;
  749.       double g = 1.0 / c->RedGamma; 
  750.       for (i=0; i<256; i++) { 
  751.          hpcr_rTbl[i] = 255.0 * pow( hpcr_rTbl[i]/255.0, g ) + 0.5;
  752.          if (hpcr_rTbl[i] < 16)
  753.             hpcr_rTbl[i] = 16;
  754.          else if (hpcr_rTbl[i] >= 240)
  755.             hpcr_rTbl[i] = 239;
  756.       }
  757.       g = 1.0 / c->GreenGamma;
  758.       for (i=0; i<256; i++) {
  759.          hpcr_gTbl[i] = 255.0 * pow( hpcr_gTbl[i]/255.0, g ) + 0.5;
  760.          if (hpcr_gTbl[i] < 16)
  761.             hpcr_gTbl[i] = 16;
  762.          else if (hpcr_gTbl[i] >= 240)
  763.             hpcr_gTbl[i] = 239;
  764.       }
  765.       g = 1.0 / c->BlueGamma;
  766.       for (i=0; i<256; i++) {
  767.          hpcr_bTbl[i] = 255.0 * pow( hpcr_bTbl[i]/255.0, g ) + 0.5;
  768.          if (hpcr_bTbl[i] < 32)
  769.             hpcr_bTbl[i] = 32;
  770.          else if (hpcr_bTbl[i] >= 224)
  771.             hpcr_bTbl[i] = 223;
  772.       }
  773.       c->undithered_pf = PF_HPCR;  /* can't really disable dithering for now */
  774.       c->dithered_pf = PF_HPCR;
  775.       c->pixel = c->visual->red_mask | c->visual->green_mask
  776.                | c->visual->blue_mask;  /* white */
  777.       c->clearpixel = 0x0;    /* black */
  778.       c->rmult = 255;
  779.       c->gmult = 255;
  780.       c->bmult = 255;
  781.    }
  782.    else if (c->depth==8) {
  783.       /* dither if 8-bit */
  784.       return setup_dithered_color( c, window, cmap );
  785.    }
  786.    else {
  787.       /* general case (12-bit TrueColor for example) */
  788.       GLint i;
  789.       /* setup r,g,btable[] arrays with gamma correction */
  790.       for (i=0;i<=c->rmult;i++) {
  791.          c->r_to_pixel[i] = gamma_adjust(c->RedGamma,   i, c->rmult) << c->rshift;
  792.       }
  793.       for (i=0;i<=c->gmult;i++) {
  794.          c->g_to_pixel[i] = gamma_adjust(c->GreenGamma, i, c->gmult) << c->gshift;
  795.       }
  796.       for (i=0;i<=c->bmult;i++) {
  797.          c->b_to_pixel[i] = gamma_adjust(c->BlueGamma,  i, c->bmult) << c->bshift;
  798.       }
  799.       c->undithered_pf = c->dithered_pf = PF_TRUECOLOR;
  800.       c->pixel = c->visual->red_mask | c->visual->green_mask
  801.              | c->visual->blue_mask;  /* white */
  802.       c->clearpixel = 0x0;    /* black */
  803.    }
  804.  
  805.    return 1;
  806. }
  807.  
  808.  
  809.  
  810. /*
  811.  * Setup RGB rendering for a window with a monochrome visual.
  812.  */
  813. static int setup_monochrome( XMesaContext c )
  814. {
  815.    /* Simulate RGB in monochrome */
  816.    c->rmult = 255;
  817.    c->gmult = 255;
  818.    c->bmult = 255;
  819.    c->dithered_pf = c->undithered_pf = PF_1BIT;
  820.    c->pixel      = WhitePixel( c->display, c->scrnum );  /* white */
  821.    c->clearpixel = BlackPixel( c->display, c->scrnum );  /* black */
  822.    return 1;
  823. }
  824.  
  825.  
  826.  
  827.  
  828. /*
  829.  * When a context is "made current" for the first time, we can finally
  830.  * finish initializing the context.
  831.  * Input:  c - the XMesaContext to initialize
  832.  *         window - the window/pixmap we're rendering into
  833.  *         cmap - the colormap associated with the window/pixmap
  834.  * Return:  GL_TRUE=success, GL_FALSE=failure
  835.  */
  836. static GLboolean initialize_context( XMesaContext c, Window window,
  837.                                      Colormap cmap )
  838. {
  839.    XGCValues gcvalues;
  840.    char *gamma;
  841.  
  842.    assert( c->initialized==GL_FALSE );
  843.  
  844.    /* check for MESA_GAMMA environment variable */
  845.    gamma = getenv("MESA_GAMMA");
  846.    if (gamma) {
  847.       c->RedGamma = c->GreenGamma = c->BlueGamma = 0.0;
  848.       sscanf( gamma, "%f %f %f", &c->RedGamma, &c->GreenGamma, &c->BlueGamma );
  849.       if (c->RedGamma<=0.0)    c->RedGamma = 1.0;
  850.       if (c->GreenGamma<=0.0)  c->GreenGamma = c->RedGamma;
  851.       if (c->BlueGamma<=0.0)   c->BlueGamma = c->RedGamma;
  852.    }
  853.    else {
  854.       c->RedGamma = c->GreenGamma = c->BlueGamma = 1.0;
  855.    }
  856.  
  857.    if (c->rgb_flag==GL_FALSE) {
  858.       /* COLOR-INDEXED WINDOW:
  859.        * Even if the visual is TrueColor or DirectColor we treat it as
  860.        * being color indexed.  This is weird but might be useful to someone.
  861.        */
  862.       c->dithered_pf = c->undithered_pf = PF_INDEX;
  863.       c->pixel = 1;
  864.       c->clearpixel = 0;
  865.       c->rmult = c->gmult = c->bmult = 0;
  866.    }
  867.    else {
  868.       /* RGB WINDOW:
  869.        * If the visual is TrueColor or DirectColor we're all set.  Other-
  870.        * wise, we simulate RGB mode using a color-mapped window.
  871.        */
  872.       int xclass;
  873. #if defined(__cplusplus) || defined(c_plusplus)
  874.       xclass = c->visual->c_class;
  875. #else
  876.       xclass = c->visual->class;
  877. #endif
  878.       if (xclass==TrueColor || xclass==DirectColor) {
  879.      if (!setup_truecolor( c, window, cmap )) {
  880.             return GL_FALSE;
  881.          }
  882.       }
  883.       else if (xclass==StaticGray && c->depth==1) {
  884.      if (!setup_monochrome( c )) {
  885.             return GL_FALSE;
  886.          }
  887.       }
  888.       else if (xclass==GrayScale || xclass==StaticGray) {
  889.          if (!setup_grayscale( c, window, cmap )) {
  890.             return GL_FALSE;
  891.          }
  892.       }
  893.       else if ((xclass==PseudoColor || xclass==StaticColor)
  894.                && c->depth>=4 && c->depth<=16) {
  895.      if (!setup_dithered_color( c, window, cmap )) {
  896.             return GL_FALSE;
  897.          }
  898.       }
  899.       else {
  900.      error("XMesaCreateContext: can't simulate RGB mode with given visual.");
  901.      return GL_FALSE;
  902.       }
  903.    }
  904.  
  905.    /* Now allocate the GL context */
  906.    c->gl_ctx = gl_new_context( c->rgb_flag,
  907.                                (GLfloat) c->rmult,
  908.                                (GLfloat) c->gmult,
  909.                                (GLfloat) c->bmult,
  910.                                255.0, /*alpha_scale*/
  911.                                c->db_state ? GL_TRUE : GL_FALSE,
  912.                                c->share_list ? c->share_list->gl_ctx : NULL );
  913. #ifdef FUTURE
  914.    c->gl_ctx = gl_new_context( c->rgb_flag,
  915.                                c->alpha_flag,
  916.                                c->db_state ? GL_TRUE : GL_FALSE,
  917.                                c->depth_size,
  918.                                c->stencil_size,
  919.                                c->accum_size,
  920.                                (GLfloat) c->rmult,
  921.                                (GLfloat) c->gmult,
  922.                                (GLfloat) c->bmult,
  923.                                (GLfloat) 255.0,
  924.                                c->share_list ? c->share_list->gl_ctx : NULL );
  925. #endif
  926.  
  927.    /* Dithering is enabled by default */
  928.    c->pixelformat = c->dithered_pf;
  929.  
  930.    {
  931.       unsigned int w, h;
  932.       get_drawable_size( c->display, window, &w, &h );
  933.       c->width = w;
  934.       c->height = h;
  935.    }
  936.  
  937.    if (c->db_state) {
  938.       /* Double buffered */
  939.       xmesa_alloc_back_buffer( c );
  940.       if (c->db_state==BACK_PIXMAP) {
  941.          c->buffer = c->backpixmap;
  942.       }
  943.       else {
  944.          c->buffer = XIMAGE;
  945.       }
  946.    }
  947.    else {
  948.       /* Single Buffered */
  949.       c->buffer = c->frontbuffer;
  950.    }
  951.  
  952.    /* X11 graphics context */
  953.    c->gc1 = XCreateGC( c->display, window, 0, NULL );
  954.    XSetForeground( c->display, c->gc1, c->pixel );
  955.    XSetBackground( c->display, c->gc1, c->pixel );
  956.    XSetFunction( c->display, c->gc1, GXcopy );
  957.    c->gc2 = XCreateGC( c->display, window, 0, NULL );
  958.    XSetForeground( c->display, c->gc2, c->pixel );
  959.    XSetBackground( c->display, c->gc2, c->pixel );
  960.    XSetFunction( c->display, c->gc2, GXcopy );
  961.    /*
  962.     * Don't generate Graphics Expose/NoExpose events in swapbuffers().
  963.     * Patch contributed by Michael Pichler May 15, 1995.
  964.     */
  965.    gcvalues.graphics_exposures = False;
  966.    c->cleargc = XCreateGC( c->display, window, GCGraphicsExposures, &gcvalues);
  967.    XSetForeground( c->display, c->cleargc, c->clearpixel );
  968.    XSetBackground( c->display, c->cleargc, c->clearpixel );
  969.    XSetFunction( c->display, c->cleargc, GXcopy );
  970.  
  971.    /* Initialize the row buffer XImage for use in write_color_span() */
  972.    c->rowimage = XCreateImage( c->display, c->visual, c->depth,
  973.                                ZPixmap, 0,                  /*format, offset*/
  974.                                (char*) malloc(MAX_WIDTH*4), /*data*/
  975.                                MAX_WIDTH, 1,                /*width, height*/
  976.                                32,                          /*bitmap_pad*/
  977.                                0                         /*bytes_per_line*/ );
  978.  
  979.  
  980.    c->initialized = GL_TRUE;
  981.  
  982.    return GL_TRUE;
  983. }
  984.  
  985.  
  986.  
  987.  
  988. /**********************************************************************/
  989. /*****                       Public Functions                     *****/
  990. /**********************************************************************/
  991.  
  992.  
  993. /*
  994.  * Create a new XMesaContext for rendering into an X11 window.
  995.  *
  996.  * Input:  display - X11 display
  997.  *         visinfo - X11 VisualInfo describing the colorbuffer we'll use
  998.  *         rgb_flag - GL_TRUE = RGB mode,
  999.  *                    GL_FALSE = color index mode
  1000.  *         db_flag - GL_TRUE = double-buffered,
  1001.  *                   GL_FALSE = single buffered
  1002.  *         ximage_flag - GL_TRUE = use an XImage for back buffer,
  1003.  *                       GL_FALSE = use an off-screen pixmap for back buffer
  1004.  *         share_list - another XMesaContext with which to share display
  1005.  *                      lists or NULL if no sharing is wanted.
  1006.  * Return:  an XMesaContext or NULL if error.
  1007.  */
  1008. XMesaContext XMesaCreateContext( Display *display,
  1009.                  XVisualInfo *visinfo,
  1010.                  GLboolean rgb_flag,
  1011.                                  GLboolean alpha_flag,
  1012.                  GLboolean db_flag,
  1013.                                  GLint depth_size,
  1014.                                  GLint stencil_size,
  1015.                                  GLint accum_size,
  1016.                  GLboolean ximage_flag,
  1017.                  XMesaContext share_list )
  1018. {
  1019.    XMesaContext c;
  1020.  
  1021.    /* allocate xmesa_context struct initialized to zeros */
  1022.    c = (struct xmesa_context *) calloc( 1, sizeof(struct xmesa_context) );
  1023.    if (!c) {
  1024.       return NULL;
  1025.    }
  1026.  
  1027.    c->rgb_flag = rgb_flag;
  1028.    c->share_list = share_list;
  1029.  
  1030.    if (host_byte_order()==ImageByteOrder(display)) {
  1031.       c->swapbytes = GL_FALSE;
  1032.    }
  1033.    else {
  1034.       c->swapbytes = GL_TRUE;
  1035.    }
  1036.  
  1037.    /* X stuff */
  1038.    c->display = display;
  1039.    c->visual = visinfo->visual;
  1040.    c->scrnum = visinfo->screen;
  1041.    c->depth = visinfo->depth;
  1042.    c->shm = check_for_xshm( display );
  1043.  
  1044.    /* Double buffering configuration */
  1045.    if (db_flag) {
  1046.       if (ximage_flag) {
  1047.      c->db_state = BACK_XIMAGE;
  1048.       }
  1049.       else {
  1050.      c->db_state = BACK_PIXMAP;
  1051.       }
  1052.    }
  1053.    else {
  1054.       c->db_state = 0;
  1055.    }
  1056.  
  1057.    if (getenv("MESA_XSYNC")) {
  1058.       XSynchronize( display, 1 );    /* This makes debugging X easier */
  1059.    }
  1060.  
  1061.    return c;
  1062. }
  1063.  
  1064.  
  1065.  
  1066.  
  1067. void XMesaDestroyContext( XMesaContext c )
  1068. {
  1069.    if (c->gl_ctx)  gl_destroy_context( c->gl_ctx );
  1070.  
  1071.    if (c->gc1)  XFreeGC( c->display, c->gc1 );
  1072.    if (c->gc2)  XFreeGC( c->display, c->gc2 );
  1073.    if (c->cleargc)  XFreeGC( c->display, c->cleargc );
  1074.  
  1075.    if (c->backimage) {
  1076. #ifdef SHM
  1077.        if (c->shm) {
  1078.        XShmDetach( c->display, &c->shminfo );
  1079.        XDestroyImage( c->backimage );
  1080.        shmdt( c->shminfo.shmaddr );
  1081.        }
  1082.        else
  1083. #endif
  1084.        XDestroyImage( c->backimage );
  1085.    }
  1086.    if (c->backpixmap) {
  1087.       XFreePixmap( c->display, c->backpixmap );
  1088.    }
  1089.    if (c->rowimage) {
  1090.       free( c->rowimage->data );
  1091.       c->rowimage->data = NULL;
  1092.       XDestroyImage( c->rowimage );
  1093.    }
  1094.    free( c );
  1095. }
  1096.  
  1097.  
  1098.  
  1099. /*
  1100.  * Bind an X/Mesa context to an X window.
  1101.  * Input:  c - the XMesaContext
  1102.  *         W - the window
  1103.  * Return:  GL_TRUE=success, GL_FALSE=failure
  1104.  */
  1105. GLboolean XMesaBindWindow( XMesaContext c, Window w )
  1106. {
  1107.    if (c->buffer==c->frontbuffer) {
  1108.       c->buffer = w;
  1109.    }
  1110.    c->frontbuffer = w;
  1111.    if (!c->initialized) {
  1112.       XWindowAttributes attr;
  1113.       XGetWindowAttributes( c->display, w, &attr );
  1114.       if (!initialize_context( c, w, attr.colormap )) {
  1115.      return GL_FALSE;
  1116.       }
  1117.    }
  1118.    return GL_TRUE;
  1119. }
  1120.  
  1121.  
  1122.  
  1123. /*
  1124.  * Bind an X/Mesa context to an X pixmap.
  1125.  * Input:  c - the XMesaContext
  1126.  *         p - the pixmap
  1127.  *         cmap - the colormap, may be 0 if using a TrueColor or DirectColor
  1128.  *                visual for the pixmap
  1129.  * Return:  GL_TRUE=success, GL_FALSE=failure
  1130.  */
  1131. GLboolean XMesaBindPixmap( XMesaContext c, Pixmap p, Colormap cmap )
  1132. {
  1133.    if (c->buffer==c->frontbuffer) {
  1134.       c->buffer = p;
  1135.    }
  1136.    c->frontbuffer = p;
  1137.  
  1138.    if (!c->initialized) {
  1139.       if (!initialize_context( c, p, cmap )) {
  1140.      return GL_FALSE;
  1141.       }
  1142.    }
  1143.    return GL_TRUE;
  1144. }
  1145.  
  1146.  
  1147.  
  1148. GLboolean XMesaMakeCurrent( XMesaContext c )
  1149. {
  1150.    if (c) {
  1151.       if (!c->initialized) {
  1152.          return GL_FALSE;
  1153.       }
  1154.       gl_set_context( c->gl_ctx );
  1155.       XMesa = c;
  1156.  
  1157.       xmesa_setup_DD_pointers();
  1158.  
  1159.       if (XMesa->gl_ctx->Viewport.Width==0) {
  1160.      /* initialize viewport to window size */
  1161.      gl_viewport( 0, 0, XMesa->width, XMesa->height );
  1162.      CC.Scissor.Width = XMesa->width;
  1163.      CC.Scissor.Height = XMesa->height;
  1164.       }
  1165.    }
  1166.    else {
  1167.       /* Detach */
  1168.       XMesa = NULL;
  1169.    }
  1170.    return GL_TRUE;
  1171. }
  1172.  
  1173.  
  1174.  
  1175. XMesaContext XMesaGetCurrentContext( void )
  1176. {
  1177.    return XMesa;
  1178. }
  1179.  
  1180.  
  1181.  
  1182. /*
  1183.  * Copy the back buffer to the front buffer.  If there's no back buffer
  1184.  * this is a no-op.
  1185.  */
  1186. void XMesaSwapBuffers( void )
  1187. {
  1188. #ifdef PROFILE
  1189.    GLdouble t0 = gl_time();
  1190. #endif
  1191.    if (XMesa->db_state) {
  1192.       if (XMesa->backimage) {
  1193.      /* Copy Ximage from host's memory to server's window */
  1194. #ifdef SHM
  1195.      if (XMesa->shm) {
  1196.         XShmPutImage( XMesa->display, XMesa->frontbuffer,
  1197.               XMesa->cleargc,
  1198.               XMesa->backimage, 0, 0,
  1199.               0, 0, XMesa->width, XMesa->height, False );
  1200.         /* wait for finished event??? */
  1201.      }
  1202.      else
  1203. #endif
  1204.          {
  1205.             XPutImage( XMesa->display, XMesa->frontbuffer,
  1206.                        XMesa->cleargc,
  1207.                        XMesa->backimage, 0, 0,
  1208.                        0, 0, XMesa->width, XMesa->height );
  1209.          }
  1210.       }
  1211.       else {
  1212.      /* Copy pixmap to window on server */
  1213.      XCopyArea( XMesa->display,
  1214.             XMesa->backpixmap,   /* source drawable */
  1215.             XMesa->frontbuffer,  /* dest. drawable */
  1216.             XMesa->cleargc,
  1217.             0, 0, XMesa->width, XMesa->height,  /* source region */
  1218.             0, 0                 /* dest region */
  1219.            );
  1220.       }
  1221.    }
  1222.    XSync( XMesa->display, False );
  1223. #ifdef PROFILE
  1224.    CC.SwapCount++;
  1225.    CC.SwapTime += gl_time() - t0;
  1226. #endif
  1227. }
  1228.  
  1229.  
  1230.  
  1231. /*
  1232.  * Return a pointer to the XMesa backbuffer Pixmap or XImage.  This function
  1233.  * is a way to get "under the hood" of X/Mesa so one can manipulate the
  1234.  * back buffer directly.
  1235.  * Output:  pixmap - pointer to back buffer's Pixmap, or 0
  1236.  *          ximage - pointer to back buffer's XImage, or NULL
  1237.  * Return:  GL_TRUE = context is double buffered
  1238.  *          GL_FALSE = context is single buffered
  1239.  */
  1240. GLboolean XMesaGetBackBuffer( Pixmap *pixmap, XImage **ximage )
  1241. {
  1242.    if (XMesa->db_state) {
  1243.       if (pixmap)  *pixmap = XMesa->backpixmap;
  1244.       if (ximage)  *ximage = XMesa->backimage;
  1245.       return GL_TRUE;
  1246.    }
  1247.    else {
  1248.       *pixmap = 0;
  1249.       *ximage = NULL;
  1250.       return GL_FALSE;
  1251.    }
  1252. }
  1253.