fakeglx.c revision c7037ccd
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.5
4 *
5 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6 * Copyright (C) 2009  VMware, Inc.   All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27/*
28 * This is an emulation of the GLX API which allows Mesa/GLX-based programs
29 * to run on X servers which do not have the real GLX extension.
30 *
31 * Thanks to the contributors:
32 *
33 * Initial version:  Philip Brown (phil@bolthole.com)
34 * Better glXGetConfig() support: Armin Liebchen (liebchen@asylum.cs.utah.edu)
35 * Further visual-handling refinements: Wolfram Gloger
36 *    (wmglo@Dent.MED.Uni-Muenchen.DE).
37 *
38 * Notes:
39 *   Don't be fooled, stereo isn't supported yet.
40 */
41
42
43
44#include "glxheader.h"
45#include "glxapi.h"
46#include "main/context.h"
47#include "main/config.h"
48#include "main/macros.h"
49#include "main/imports.h"
50#include "main/mtypes.h"
51#include "main/version.h"
52#include "xfonts.h"
53#include "xmesaP.h"
54
55#ifdef __VMS
56#define _mesa_sprintf sprintf
57#endif
58
59/* This indicates the client-side GLX API and GLX encoder version. */
60#define CLIENT_MAJOR_VERSION 1
61#define CLIENT_MINOR_VERSION 4  /* but don't have 1.3's pbuffers, etc yet */
62
63/* This indicates the server-side GLX decoder version.
64 * GLX 1.4 indicates OpenGL 1.3 support
65 */
66#define SERVER_MAJOR_VERSION 1
67#define SERVER_MINOR_VERSION 4
68
69/* This is appended onto the glXGetClient/ServerString version strings. */
70#define MESA_GLX_VERSION "Mesa " MESA_VERSION_STRING
71
72/* Who implemented this GLX? */
73#define VENDOR "Brian Paul"
74
75#define EXTENSIONS \
76   "GLX_MESA_set_3dfx_mode " \
77   "GLX_MESA_copy_sub_buffer " \
78   "GLX_MESA_pixmap_colormap " \
79   "GLX_MESA_release_buffers " \
80   "GLX_ARB_get_proc_address " \
81   "GLX_EXT_texture_from_pixmap " \
82   "GLX_EXT_visual_info " \
83   "GLX_EXT_visual_rating " \
84   /*"GLX_SGI_video_sync "*/ \
85   "GLX_SGIX_fbconfig " \
86   "GLX_SGIX_pbuffer "
87
88/*
89 * Our fake GLX context will contain a "real" GLX context and an XMesa context.
90 *
91 * Note that a pointer to a __GLXcontext is a pointer to a fake_glx_context,
92 * and vice versa.
93 *
94 * We really just need this structure in order to make the libGL functions
95 * glXGetCurrentContext(), glXGetCurrentDrawable() and glXGetCurrentDisplay()
96 * work correctly.
97 */
98struct fake_glx_context {
99   __GLXcontext glxContext;   /* this MUST be first! */
100   XMesaContext xmesaContext;
101};
102
103
104
105/**********************************************************************/
106/***                       GLX Visual Code                          ***/
107/**********************************************************************/
108
109#define DONT_CARE -1
110
111
112static XMesaVisual *VisualTable = NULL;
113static int NumVisuals = 0;
114
115
116/*
117 * This struct and some code fragments borrowed
118 * from Mark Kilgard's GLUT library.
119 */
120typedef struct _OverlayInfo {
121  /* Avoid 64-bit portability problems by being careful to use
122     longs due to the way XGetWindowProperty is specified. Note
123     that these parameters are passed as CARD32s over X
124     protocol. */
125  unsigned long overlay_visual;
126  long transparent_type;
127  long value;
128  long layer;
129} OverlayInfo;
130
131
132
133/* Macro to handle c_class vs class field name in XVisualInfo struct */
134#if defined(__cplusplus) || defined(c_plusplus)
135#define CLASS c_class
136#else
137#define CLASS class
138#endif
139
140
141
142/*
143 * Test if the given XVisualInfo is usable for Mesa rendering.
144 */
145static GLboolean
146is_usable_visual( XVisualInfo *vinfo )
147{
148   switch (vinfo->CLASS) {
149      case StaticGray:
150      case GrayScale:
151         /* Any StaticGray/GrayScale visual works in RGB or CI mode */
152         return GL_TRUE;
153      case StaticColor:
154      case PseudoColor:
155	 /* Any StaticColor/PseudoColor visual of at least 4 bits */
156	 if (vinfo->depth>=4) {
157	    return GL_TRUE;
158	 }
159	 else {
160	    return GL_FALSE;
161	 }
162      case TrueColor:
163      case DirectColor:
164	 /* Any depth of TrueColor or DirectColor works in RGB mode */
165	 return GL_TRUE;
166      default:
167	 /* This should never happen */
168	 return GL_FALSE;
169   }
170}
171
172
173
174/**
175 * Get an array OverlayInfo records for specified screen.
176 * \param dpy  the display
177 * \param screen  screen number
178 * \param numOverlays  returns numver of OverlayInfo records
179 * \return  pointer to OverlayInfo array, free with XFree()
180 */
181static OverlayInfo *
182GetOverlayInfo(Display *dpy, int screen, int *numOverlays)
183{
184   Atom overlayVisualsAtom;
185   Atom actualType;
186   Status status;
187   unsigned char *ovInfo;
188   unsigned long sizeData, bytesLeft;
189   int actualFormat;
190
191   /*
192    * The SERVER_OVERLAY_VISUALS property on the root window contains
193    * a list of overlay visuals.  Get that list now.
194    */
195   overlayVisualsAtom = XInternAtom(dpy,"SERVER_OVERLAY_VISUALS", True);
196   if (overlayVisualsAtom == None) {
197      return 0;
198   }
199
200   status = XGetWindowProperty(dpy, RootWindow(dpy, screen),
201                               overlayVisualsAtom, 0L, (long) 10000, False,
202                               overlayVisualsAtom, &actualType, &actualFormat,
203                               &sizeData, &bytesLeft,
204                               &ovInfo);
205
206   if (status != Success || actualType != overlayVisualsAtom ||
207       actualFormat != 32 || sizeData < 4) {
208      /* something went wrong */
209      XFree((void *) ovInfo);
210      *numOverlays = 0;
211      return NULL;
212   }
213
214   *numOverlays = sizeData / 4;
215   return (OverlayInfo *) ovInfo;
216}
217
218
219
220/**
221 * Return the level (overlay, normal, underlay) of a given XVisualInfo.
222 * Input:  dpy - the X display
223 *         vinfo - the XVisualInfo to test
224 * Return:  level of the visual:
225 *             0 = normal planes
226 *            >0 = overlay planes
227 *            <0 = underlay planes
228 */
229static int
230level_of_visual( Display *dpy, XVisualInfo *vinfo )
231{
232   OverlayInfo *overlay_info;
233   int numOverlaysPerScreen, i;
234
235   overlay_info = GetOverlayInfo(dpy, vinfo->screen, &numOverlaysPerScreen);
236   if (!overlay_info) {
237      return 0;
238   }
239
240   /* search the overlay visual list for the visual ID of interest */
241   for (i = 0; i < numOverlaysPerScreen; i++) {
242      const OverlayInfo *ov = overlay_info + i;
243      if (ov->overlay_visual == vinfo->visualid) {
244         /* found the visual */
245         if (/*ov->transparent_type==1 &&*/ ov->layer!=0) {
246            int level = ov->layer;
247            XFree((void *) overlay_info);
248            return level;
249         }
250         else {
251            XFree((void *) overlay_info);
252            return 0;
253         }
254      }
255   }
256
257   /* The visual ID was not found in the overlay list. */
258   XFree((void *) overlay_info);
259   return 0;
260}
261
262
263
264
265/*
266 * Given an XVisualInfo and RGB, Double, and Depth buffer flags, save the
267 * configuration in our list of GLX visuals.
268 */
269static XMesaVisual
270save_glx_visual( Display *dpy, XVisualInfo *vinfo,
271                 GLboolean rgbFlag, GLboolean alphaFlag, GLboolean dbFlag,
272                 GLboolean stereoFlag,
273                 GLint depth_size, GLint stencil_size,
274                 GLint accumRedSize, GLint accumGreenSize,
275                 GLint accumBlueSize, GLint accumAlphaSize,
276                 GLint level, GLint numAuxBuffers )
277{
278   GLboolean ximageFlag = GL_TRUE;
279   XMesaVisual xmvis;
280   GLint i;
281   GLboolean comparePointers;
282
283   if (dbFlag) {
284      /* Check if the MESA_BACK_BUFFER env var is set */
285      char *backbuffer = _mesa_getenv("MESA_BACK_BUFFER");
286      if (backbuffer) {
287         if (backbuffer[0]=='p' || backbuffer[0]=='P') {
288            ximageFlag = GL_FALSE;
289         }
290         else if (backbuffer[0]=='x' || backbuffer[0]=='X') {
291            ximageFlag = GL_TRUE;
292         }
293         else {
294            _mesa_warning(NULL, "Mesa: invalid value for MESA_BACK_BUFFER environment variable, using an XImage.");
295         }
296      }
297   }
298
299   if (stereoFlag) {
300      /* stereo not supported */
301      return NULL;
302   }
303
304   /* Comparing IDs uses less memory but sometimes fails. */
305   /* XXX revisit this after 3.0 is finished. */
306   if (_mesa_getenv("MESA_GLX_VISUAL_HACK"))
307      comparePointers = GL_TRUE;
308   else
309      comparePointers = GL_FALSE;
310
311   /* Force the visual to have an alpha channel */
312   if (rgbFlag && _mesa_getenv("MESA_GLX_FORCE_ALPHA"))
313      alphaFlag = GL_TRUE;
314
315   /* First check if a matching visual is already in the list */
316   for (i=0; i<NumVisuals; i++) {
317      XMesaVisual v = VisualTable[i];
318      if (v->display == dpy
319          && v->mesa_visual.level == level
320          && v->mesa_visual.numAuxBuffers == numAuxBuffers
321          && v->ximage_flag == ximageFlag
322          && v->mesa_visual.rgbMode == rgbFlag
323          && v->mesa_visual.doubleBufferMode == dbFlag
324          && v->mesa_visual.stereoMode == stereoFlag
325          && (v->mesa_visual.alphaBits > 0) == alphaFlag
326          && (v->mesa_visual.depthBits >= depth_size || depth_size == 0)
327          && (v->mesa_visual.stencilBits >= stencil_size || stencil_size == 0)
328          && (v->mesa_visual.accumRedBits >= accumRedSize || accumRedSize == 0)
329          && (v->mesa_visual.accumGreenBits >= accumGreenSize || accumGreenSize == 0)
330          && (v->mesa_visual.accumBlueBits >= accumBlueSize || accumBlueSize == 0)
331          && (v->mesa_visual.accumAlphaBits >= accumAlphaSize || accumAlphaSize == 0)) {
332         /* now either compare XVisualInfo pointers or visual IDs */
333         if ((!comparePointers && v->visinfo->visualid == vinfo->visualid)
334             || (comparePointers && v->vishandle == vinfo)) {
335            return v;
336         }
337      }
338   }
339
340   /* Create a new visual and add it to the list. */
341
342   xmvis = XMesaCreateVisual( dpy, vinfo, rgbFlag, alphaFlag, dbFlag,
343                              stereoFlag, ximageFlag,
344                              depth_size, stencil_size,
345                              accumRedSize, accumBlueSize,
346                              accumBlueSize, accumAlphaSize, 0, level,
347                              GLX_NONE_EXT );
348   if (xmvis) {
349      /* Save a copy of the pointer now so we can find this visual again
350       * if we need to search for it in find_glx_visual().
351       */
352      xmvis->vishandle = vinfo;
353      /* Allocate more space for additional visual */
354      VisualTable = (XMesaVisual *) _mesa_realloc( VisualTable,
355                                   sizeof(XMesaVisual) * NumVisuals,
356                                   sizeof(XMesaVisual) * (NumVisuals + 1));
357      /* add xmvis to the list */
358      VisualTable[NumVisuals] = xmvis;
359      NumVisuals++;
360      /* XXX minor hack, because XMesaCreateVisual doesn't support an
361       * aux buffers parameter.
362       */
363      xmvis->mesa_visual.numAuxBuffers = numAuxBuffers;
364   }
365   return xmvis;
366}
367
368
369/**
370 * Return the default number of bits for the Z buffer.
371 * If defined, use the MESA_GLX_DEPTH_BITS env var value.
372 * Otherwise, use the DEFAULT_SOFTWARE_DEPTH_BITS constant.
373 * XXX probably do the same thing for stencil, accum, etc.
374 */
375static GLint
376default_depth_bits(void)
377{
378   int zBits;
379   const char *zEnv = _mesa_getenv("MESA_GLX_DEPTH_BITS");
380   if (zEnv)
381      zBits = _mesa_atoi(zEnv);
382   else
383      zBits = DEFAULT_SOFTWARE_DEPTH_BITS;
384   return zBits;
385}
386
387static GLint
388default_alpha_bits(void)
389{
390   int aBits;
391   const char *aEnv = _mesa_getenv("MESA_GLX_ALPHA_BITS");
392   if (aEnv)
393      aBits = _mesa_atoi(aEnv);
394   else
395      aBits = 0;
396   return aBits;
397}
398
399static GLint
400default_accum_bits(void)
401{
402   return 16;
403}
404
405
406
407/*
408 * Create a GLX visual from a regular XVisualInfo.
409 * This is called when Fake GLX is given an XVisualInfo which wasn't
410 * returned by glXChooseVisual.  Since this is the first time we're
411 * considering this visual we'll take a guess at reasonable values
412 * for depth buffer size, stencil size, accum size, etc.
413 * This is the best we can do with a client-side emulation of GLX.
414 */
415static XMesaVisual
416create_glx_visual( Display *dpy, XVisualInfo *visinfo )
417{
418   int vislevel;
419   GLint zBits = default_depth_bits();
420   GLint accBits = default_accum_bits();
421   GLboolean alphaFlag = default_alpha_bits() > 0;
422
423   vislevel = level_of_visual( dpy, visinfo );
424   if (vislevel) {
425      /* Configure this visual as a CI, single-buffered overlay */
426      return save_glx_visual( dpy, visinfo,
427                              GL_FALSE,  /* rgb */
428                              GL_FALSE,  /* alpha */
429                              GL_FALSE,  /* double */
430                              GL_FALSE,  /* stereo */
431                              0,         /* depth bits */
432                              0,         /* stencil bits */
433                              0,0,0,0,   /* accum bits */
434                              vislevel,  /* level */
435                              0          /* numAux */
436                            );
437   }
438   else if (is_usable_visual( visinfo )) {
439      if (_mesa_getenv("MESA_GLX_FORCE_CI")) {
440         /* Configure this visual as a COLOR INDEX visual. */
441         return save_glx_visual( dpy, visinfo,
442                                 GL_FALSE,   /* rgb */
443                                 GL_FALSE,  /* alpha */
444                                 GL_TRUE,   /* double */
445                                 GL_FALSE,  /* stereo */
446                                 zBits,
447                                 STENCIL_BITS,
448                                 0, 0, 0, 0, /* accum bits */
449                                 0,         /* level */
450                                 0          /* numAux */
451                               );
452      }
453      else {
454         /* Configure this visual as RGB, double-buffered, depth-buffered. */
455         /* This is surely wrong for some people's needs but what else */
456         /* can be done?  They should use glXChooseVisual(). */
457         return save_glx_visual( dpy, visinfo,
458                                 GL_TRUE,   /* rgb */
459                                 alphaFlag, /* alpha */
460                                 GL_TRUE,   /* double */
461                                 GL_FALSE,  /* stereo */
462                                 zBits,
463                                 STENCIL_BITS,
464                                 accBits, /* r */
465                                 accBits, /* g */
466                                 accBits, /* b */
467                                 accBits, /* a */
468                                 0,         /* level */
469                                 0          /* numAux */
470                               );
471      }
472   }
473   else {
474      _mesa_warning(NULL, "Mesa: error in glXCreateContext: bad visual\n");
475      return NULL;
476   }
477}
478
479
480
481/*
482 * Find the GLX visual associated with an XVisualInfo.
483 */
484static XMesaVisual
485find_glx_visual( Display *dpy, XVisualInfo *vinfo )
486{
487   int i;
488
489   /* try to match visual id */
490   for (i=0;i<NumVisuals;i++) {
491      if (VisualTable[i]->display==dpy
492          && VisualTable[i]->visinfo->visualid == vinfo->visualid) {
493         return VisualTable[i];
494      }
495   }
496
497   /* if that fails, try to match pointers */
498   for (i=0;i<NumVisuals;i++) {
499      if (VisualTable[i]->display==dpy && VisualTable[i]->vishandle==vinfo) {
500         return VisualTable[i];
501      }
502   }
503
504   return NULL;
505}
506
507
508
509/**
510 * Return the transparent pixel value for a GLX visual.
511 * Input:  glxvis - the glx_visual
512 * Return:  a pixel value or -1 if no transparent pixel
513 */
514static int
515transparent_pixel( XMesaVisual glxvis )
516{
517   Display *dpy = glxvis->display;
518   XVisualInfo *vinfo = glxvis->visinfo;
519   OverlayInfo *overlay_info;
520   int numOverlaysPerScreen, i;
521
522   overlay_info = GetOverlayInfo(dpy, vinfo->screen, &numOverlaysPerScreen);
523   if (!overlay_info) {
524      return -1;
525   }
526
527   for (i = 0; i < numOverlaysPerScreen; i++) {
528      const OverlayInfo *ov = overlay_info + i;
529      if (ov->overlay_visual == vinfo->visualid) {
530         /* found it! */
531         if (ov->transparent_type == 0) {
532            /* type 0 indicates no transparency */
533            XFree((void *) overlay_info);
534            return -1;
535         }
536         else {
537            /* ov->value is the transparent pixel */
538            XFree((void *) overlay_info);
539            return ov->value;
540         }
541      }
542   }
543
544   /* The visual ID was not found in the overlay list. */
545   XFree((void *) overlay_info);
546   return -1;
547}
548
549
550
551/**
552 * Try to get an X visual which matches the given arguments.
553 */
554static XVisualInfo *
555get_visual( Display *dpy, int scr, unsigned int depth, int xclass )
556{
557   XVisualInfo temp, *vis;
558   long mask;
559   int n;
560   unsigned int default_depth;
561   int default_class;
562
563   mask = VisualScreenMask | VisualDepthMask | VisualClassMask;
564   temp.screen = scr;
565   temp.depth = depth;
566   temp.CLASS = xclass;
567
568   default_depth = DefaultDepth(dpy,scr);
569   default_class = DefaultVisual(dpy,scr)->CLASS;
570
571   if (depth==default_depth && xclass==default_class) {
572      /* try to get root window's visual */
573      temp.visualid = DefaultVisual(dpy,scr)->visualid;
574      mask |= VisualIDMask;
575   }
576
577   vis = XGetVisualInfo( dpy, mask, &temp, &n );
578
579   /* In case bits/pixel > 24, make sure color channels are still <=8 bits.
580    * An SGI Infinite Reality system, for example, can have 30bpp pixels:
581    * 10 bits per color channel.  Mesa's limited to a max of 8 bits/channel.
582    */
583   if (vis && depth > 24 && (xclass==TrueColor || xclass==DirectColor)) {
584      if (_mesa_bitcount((GLuint) vis->red_mask  ) <= 8 &&
585          _mesa_bitcount((GLuint) vis->green_mask) <= 8 &&
586          _mesa_bitcount((GLuint) vis->blue_mask ) <= 8) {
587         return vis;
588      }
589      else {
590         XFree((void *) vis);
591         return NULL;
592      }
593   }
594
595   return vis;
596}
597
598
599
600/*
601 * Retrieve the value of the given environment variable and find
602 * the X visual which matches it.
603 * Input:  dpy - the display
604 *         screen - the screen number
605 *         varname - the name of the environment variable
606 * Return:  an XVisualInfo pointer to NULL if error.
607 */
608static XVisualInfo *
609get_env_visual(Display *dpy, int scr, const char *varname)
610{
611   char value[100], type[100];
612   int depth, xclass = -1;
613   XVisualInfo *vis;
614
615   if (!_mesa_getenv( varname )) {
616      return NULL;
617   }
618
619   _mesa_strncpy( value, _mesa_getenv(varname), 100 );
620   value[99] = 0;
621
622   sscanf( value, "%s %d", type, &depth );
623
624   if (_mesa_strcmp(type,"TrueColor")==0)          xclass = TrueColor;
625   else if (_mesa_strcmp(type,"DirectColor")==0)   xclass = DirectColor;
626   else if (_mesa_strcmp(type,"PseudoColor")==0)   xclass = PseudoColor;
627   else if (_mesa_strcmp(type,"StaticColor")==0)   xclass = StaticColor;
628   else if (_mesa_strcmp(type,"GrayScale")==0)     xclass = GrayScale;
629   else if (_mesa_strcmp(type,"StaticGray")==0)    xclass = StaticGray;
630
631   if (xclass>-1 && depth>0) {
632      vis = get_visual( dpy, scr, depth, xclass );
633      if (vis) {
634	 return vis;
635      }
636   }
637
638   _mesa_warning(NULL, "GLX unable to find visual class=%s, depth=%d.",
639                 type, depth);
640
641   return NULL;
642}
643
644
645
646/*
647 * Select an X visual which satisfies the RGBA/CI flag and minimum depth.
648 * Input:  dpy, screen - X display and screen number
649 *         rgba - GL_TRUE = RGBA mode, GL_FALSE = CI mode
650 *         min_depth - minimum visual depth
651 *         preferred_class - preferred GLX visual class or DONT_CARE
652 * Return:  pointer to an XVisualInfo or NULL.
653 */
654static XVisualInfo *
655choose_x_visual( Display *dpy, int screen, GLboolean rgba, int min_depth,
656                 int preferred_class )
657{
658   XVisualInfo *vis;
659   int xclass, visclass = 0;
660   int depth;
661
662   if (rgba) {
663      Atom hp_cr_maps = XInternAtom(dpy, "_HP_RGB_SMOOTH_MAP_LIST", True);
664      /* First see if the MESA_RGB_VISUAL env var is defined */
665      vis = get_env_visual( dpy, screen, "MESA_RGB_VISUAL" );
666      if (vis) {
667	 return vis;
668      }
669      /* Otherwise, search for a suitable visual */
670      if (preferred_class==DONT_CARE) {
671         for (xclass=0;xclass<6;xclass++) {
672            switch (xclass) {
673               case 0:  visclass = TrueColor;    break;
674               case 1:  visclass = DirectColor;  break;
675               case 2:  visclass = PseudoColor;  break;
676               case 3:  visclass = StaticColor;  break;
677               case 4:  visclass = GrayScale;    break;
678               case 5:  visclass = StaticGray;   break;
679            }
680            if (min_depth==0) {
681               /* start with shallowest */
682               for (depth=0;depth<=32;depth++) {
683                  if (visclass==TrueColor && depth==8 && !hp_cr_maps) {
684                     /* Special case:  try to get 8-bit PseudoColor before */
685                     /* 8-bit TrueColor */
686                     vis = get_visual( dpy, screen, 8, PseudoColor );
687                     if (vis) {
688                        return vis;
689                     }
690                  }
691                  vis = get_visual( dpy, screen, depth, visclass );
692                  if (vis) {
693                     return vis;
694                  }
695               }
696            }
697            else {
698               /* start with deepest */
699               for (depth=32;depth>=min_depth;depth--) {
700                  if (visclass==TrueColor && depth==8 && !hp_cr_maps) {
701                     /* Special case:  try to get 8-bit PseudoColor before */
702                     /* 8-bit TrueColor */
703                     vis = get_visual( dpy, screen, 8, PseudoColor );
704                     if (vis) {
705                        return vis;
706                     }
707                  }
708                  vis = get_visual( dpy, screen, depth, visclass );
709                  if (vis) {
710                     return vis;
711                  }
712               }
713            }
714         }
715      }
716      else {
717         /* search for a specific visual class */
718         switch (preferred_class) {
719            case GLX_TRUE_COLOR_EXT:    visclass = TrueColor;    break;
720            case GLX_DIRECT_COLOR_EXT:  visclass = DirectColor;  break;
721            case GLX_PSEUDO_COLOR_EXT:  visclass = PseudoColor;  break;
722            case GLX_STATIC_COLOR_EXT:  visclass = StaticColor;  break;
723            case GLX_GRAY_SCALE_EXT:    visclass = GrayScale;    break;
724            case GLX_STATIC_GRAY_EXT:   visclass = StaticGray;   break;
725            default:   return NULL;
726         }
727         if (min_depth==0) {
728            /* start with shallowest */
729            for (depth=0;depth<=32;depth++) {
730               vis = get_visual( dpy, screen, depth, visclass );
731               if (vis) {
732                  return vis;
733               }
734            }
735         }
736         else {
737            /* start with deepest */
738            for (depth=32;depth>=min_depth;depth--) {
739               vis = get_visual( dpy, screen, depth, visclass );
740               if (vis) {
741                  return vis;
742               }
743            }
744         }
745      }
746   }
747   else {
748      /* First see if the MESA_CI_VISUAL env var is defined */
749      vis = get_env_visual( dpy, screen, "MESA_CI_VISUAL" );
750      if (vis) {
751	 return vis;
752      }
753      /* Otherwise, search for a suitable visual, starting with shallowest */
754      if (preferred_class==DONT_CARE) {
755         for (xclass=0;xclass<4;xclass++) {
756            switch (xclass) {
757               case 0:  visclass = PseudoColor;  break;
758               case 1:  visclass = StaticColor;  break;
759               case 2:  visclass = GrayScale;    break;
760               case 3:  visclass = StaticGray;   break;
761            }
762            /* try 8-bit up through 16-bit */
763            for (depth=8;depth<=16;depth++) {
764               vis = get_visual( dpy, screen, depth, visclass );
765               if (vis) {
766                  return vis;
767               }
768            }
769            /* try min_depth up to 8-bit */
770            for (depth=min_depth;depth<8;depth++) {
771               vis = get_visual( dpy, screen, depth, visclass );
772               if (vis) {
773                  return vis;
774               }
775            }
776         }
777      }
778      else {
779         /* search for a specific visual class */
780         switch (preferred_class) {
781            case GLX_TRUE_COLOR_EXT:    visclass = TrueColor;    break;
782            case GLX_DIRECT_COLOR_EXT:  visclass = DirectColor;  break;
783            case GLX_PSEUDO_COLOR_EXT:  visclass = PseudoColor;  break;
784            case GLX_STATIC_COLOR_EXT:  visclass = StaticColor;  break;
785            case GLX_GRAY_SCALE_EXT:    visclass = GrayScale;    break;
786            case GLX_STATIC_GRAY_EXT:   visclass = StaticGray;   break;
787            default:   return NULL;
788         }
789         /* try 8-bit up through 16-bit */
790         for (depth=8;depth<=16;depth++) {
791            vis = get_visual( dpy, screen, depth, visclass );
792            if (vis) {
793               return vis;
794            }
795         }
796         /* try min_depth up to 8-bit */
797         for (depth=min_depth;depth<8;depth++) {
798            vis = get_visual( dpy, screen, depth, visclass );
799            if (vis) {
800               return vis;
801            }
802         }
803      }
804   }
805
806   /* didn't find a visual */
807   return NULL;
808}
809
810
811
812/*
813 * Find the deepest X over/underlay visual of at least min_depth.
814 * Input:  dpy, screen - X display and screen number
815 *         level - the over/underlay level
816 *         trans_type - transparent pixel type: GLX_NONE_EXT,
817 *                      GLX_TRANSPARENT_RGB_EXT, GLX_TRANSPARENT_INDEX_EXT,
818 *                      or DONT_CARE
819 *         trans_value - transparent pixel value or DONT_CARE
820 *         min_depth - minimum visual depth
821 *         preferred_class - preferred GLX visual class or DONT_CARE
822 * Return:  pointer to an XVisualInfo or NULL.
823 */
824static XVisualInfo *
825choose_x_overlay_visual( Display *dpy, int scr, GLboolean rgbFlag,
826                         int level, int trans_type, int trans_value,
827                         int min_depth, int preferred_class )
828{
829   OverlayInfo *overlay_info;
830   int numOverlaysPerScreen;
831   int i;
832   XVisualInfo *deepvis;
833   int deepest;
834
835   /*DEBUG int tt, tv; */
836
837   switch (preferred_class) {
838      case GLX_TRUE_COLOR_EXT:    preferred_class = TrueColor;    break;
839      case GLX_DIRECT_COLOR_EXT:  preferred_class = DirectColor;  break;
840      case GLX_PSEUDO_COLOR_EXT:  preferred_class = PseudoColor;  break;
841      case GLX_STATIC_COLOR_EXT:  preferred_class = StaticColor;  break;
842      case GLX_GRAY_SCALE_EXT:    preferred_class = GrayScale;    break;
843      case GLX_STATIC_GRAY_EXT:   preferred_class = StaticGray;   break;
844      default:                    preferred_class = DONT_CARE;
845   }
846
847   overlay_info = GetOverlayInfo(dpy, scr, &numOverlaysPerScreen);
848   if (!overlay_info) {
849      return NULL;
850   }
851
852   /* Search for the deepest overlay which satisifies all criteria. */
853   deepest = min_depth;
854   deepvis = NULL;
855
856   for (i = 0; i < numOverlaysPerScreen; i++) {
857      const OverlayInfo *ov = overlay_info + i;
858      XVisualInfo *vislist, vistemplate;
859      int count;
860
861      if (ov->layer!=level) {
862         /* failed overlay level criteria */
863         continue;
864      }
865      if (!(trans_type==DONT_CARE
866            || (trans_type==GLX_TRANSPARENT_INDEX_EXT
867                && ov->transparent_type>0)
868            || (trans_type==GLX_NONE_EXT && ov->transparent_type==0))) {
869         /* failed transparent pixel type criteria */
870         continue;
871      }
872      if (trans_value!=DONT_CARE && trans_value!=ov->value) {
873         /* failed transparent pixel value criteria */
874         continue;
875      }
876
877      /* get XVisualInfo and check the depth */
878      vistemplate.visualid = ov->overlay_visual;
879      vistemplate.screen = scr;
880      vislist = XGetVisualInfo( dpy, VisualIDMask | VisualScreenMask,
881                                &vistemplate, &count );
882
883      if (count!=1) {
884         /* something went wrong */
885         continue;
886      }
887      if (preferred_class!=DONT_CARE && preferred_class!=vislist->CLASS) {
888         /* wrong visual class */
889         continue;
890      }
891
892      /* if RGB was requested, make sure we have True/DirectColor */
893      if (rgbFlag && vislist->CLASS != TrueColor
894          && vislist->CLASS != DirectColor)
895         continue;
896
897      /* if CI was requested, make sure we have a color indexed visual */
898      if (!rgbFlag
899          && (vislist->CLASS == TrueColor || vislist->CLASS == DirectColor))
900         continue;
901
902      if (deepvis==NULL || vislist->depth > deepest) {
903         /* YES!  found a satisfactory visual */
904         if (deepvis) {
905            XFree( deepvis );
906         }
907         deepest = vislist->depth;
908         deepvis = vislist;
909         /* DEBUG  tt = ov->transparent_type;*/
910         /* DEBUG  tv = ov->value; */
911      }
912   }
913
914/*DEBUG
915   if (deepvis) {
916      printf("chose 0x%x:  layer=%d depth=%d trans_type=%d trans_value=%d\n",
917             deepvis->visualid, level, deepvis->depth, tt, tv );
918   }
919*/
920   return deepvis;
921}
922
923
924/**********************************************************************/
925/***             Display-related functions                          ***/
926/**********************************************************************/
927
928
929/**
930 * Free all XMesaVisuals which are associated with the given display.
931 */
932static void
933destroy_visuals_on_display(Display *dpy)
934{
935   int i;
936   for (i = 0; i < NumVisuals; i++) {
937      if (VisualTable[i]->display == dpy) {
938         /* remove this visual */
939         int j;
940         free(VisualTable[i]);
941         for (j = i; j < NumVisuals - 1; j++)
942            VisualTable[j] = VisualTable[j + 1];
943         NumVisuals--;
944      }
945   }
946}
947
948
949/**
950 * Called from XCloseDisplay() to let us free our display-related data.
951 */
952static int
953close_display_callback(Display *dpy, XExtCodes *codes)
954{
955   destroy_visuals_on_display(dpy);
956   xmesa_destroy_buffers_on_display(dpy);
957   return 0;
958}
959
960
961/**
962 * Look for the named extension on given display and return a pointer
963 * to the _XExtension data, or NULL if extension not found.
964 */
965static _XExtension *
966lookup_extension(Display *dpy, const char *extName)
967{
968   _XExtension *ext;
969   for (ext = dpy->ext_procs; ext; ext = ext->next) {
970      if (ext->name && strcmp(ext->name, extName) == 0) {
971         return ext;
972      }
973   }
974   return NULL;
975}
976
977
978/**
979 * Whenever we're given a new Display pointer, call this function to
980 * register our close_display_callback function.
981 */
982static void
983register_with_display(Display *dpy)
984{
985   const char *extName = "MesaGLX";
986   _XExtension *ext;
987
988   ext = lookup_extension(dpy, extName);
989   if (!ext) {
990      XExtCodes *c = XAddExtension(dpy);
991      ext = dpy->ext_procs;  /* new extension is at head of list */
992      assert(c->extension == ext->codes.extension);
993      ext->name = _mesa_strdup(extName);
994      ext->close_display = close_display_callback;
995   }
996}
997
998
999/**********************************************************************/
1000/***                  Begin Fake GLX API Functions                  ***/
1001/**********************************************************************/
1002
1003
1004/**
1005 * Helper used by glXChooseVisual and glXChooseFBConfig.
1006 * The fbConfig parameter must be GL_FALSE for the former and GL_TRUE for
1007 * the later.
1008 * In either case, the attribute list is terminated with the value 'None'.
1009 */
1010static XMesaVisual
1011choose_visual( Display *dpy, int screen, const int *list, GLboolean fbConfig )
1012{
1013   const GLboolean rgbModeDefault = fbConfig;
1014   const int *parselist;
1015   XVisualInfo *vis;
1016   int min_ci = 0;
1017   int min_red=0, min_green=0, min_blue=0;
1018   GLboolean rgb_flag = rgbModeDefault;
1019   GLboolean alpha_flag = GL_FALSE;
1020   GLboolean double_flag = GL_FALSE;
1021   GLboolean stereo_flag = GL_FALSE;
1022   GLint depth_size = 0;
1023   GLint stencil_size = 0;
1024   GLint accumRedSize = 0;
1025   GLint accumGreenSize = 0;
1026   GLint accumBlueSize = 0;
1027   GLint accumAlphaSize = 0;
1028   int level = 0;
1029   int visual_type = DONT_CARE;
1030   int trans_type = DONT_CARE;
1031   int trans_value = DONT_CARE;
1032   GLint caveat = DONT_CARE;
1033   XMesaVisual xmvis = NULL;
1034   int desiredVisualID = -1;
1035   int numAux = 0;
1036
1037   parselist = list;
1038
1039   while (*parselist) {
1040
1041      switch (*parselist) {
1042	 case GLX_USE_GL:
1043            if (fbConfig) {
1044               /* invalid token */
1045               return NULL;
1046            }
1047            else {
1048               /* skip */
1049               parselist++;
1050            }
1051	    break;
1052	 case GLX_BUFFER_SIZE:
1053	    parselist++;
1054	    min_ci = *parselist++;
1055	    break;
1056	 case GLX_LEVEL:
1057	    parselist++;
1058            level = *parselist++;
1059	    break;
1060	 case GLX_RGBA:
1061            if (fbConfig) {
1062               /* invalid token */
1063               return NULL;
1064            }
1065            else {
1066               rgb_flag = GL_TRUE;
1067               parselist++;
1068            }
1069	    break;
1070	 case GLX_DOUBLEBUFFER:
1071            parselist++;
1072            if (fbConfig) {
1073               double_flag = *parselist++;
1074            }
1075            else {
1076               double_flag = GL_TRUE;
1077            }
1078	    break;
1079	 case GLX_STEREO:
1080            parselist++;
1081            if (fbConfig) {
1082               stereo_flag = *parselist++;
1083            }
1084            else {
1085               stereo_flag = GL_TRUE;
1086            }
1087            break;
1088	 case GLX_AUX_BUFFERS:
1089	    parselist++;
1090            numAux = *parselist++;
1091            if (numAux > MAX_AUX_BUFFERS)
1092               return NULL;
1093	    break;
1094	 case GLX_RED_SIZE:
1095	    parselist++;
1096	    min_red = *parselist++;
1097	    break;
1098	 case GLX_GREEN_SIZE:
1099	    parselist++;
1100	    min_green = *parselist++;
1101	    break;
1102	 case GLX_BLUE_SIZE:
1103	    parselist++;
1104	    min_blue = *parselist++;
1105	    break;
1106	 case GLX_ALPHA_SIZE:
1107	    parselist++;
1108            {
1109               GLint size = *parselist++;
1110               alpha_flag = size ? GL_TRUE : GL_FALSE;
1111            }
1112	    break;
1113	 case GLX_DEPTH_SIZE:
1114	    parselist++;
1115	    depth_size = *parselist++;
1116	    break;
1117	 case GLX_STENCIL_SIZE:
1118	    parselist++;
1119	    stencil_size = *parselist++;
1120	    break;
1121	 case GLX_ACCUM_RED_SIZE:
1122	    parselist++;
1123            {
1124               GLint size = *parselist++;
1125               accumRedSize = MAX2( accumRedSize, size );
1126            }
1127            break;
1128	 case GLX_ACCUM_GREEN_SIZE:
1129	    parselist++;
1130            {
1131               GLint size = *parselist++;
1132               accumGreenSize = MAX2( accumGreenSize, size );
1133            }
1134            break;
1135	 case GLX_ACCUM_BLUE_SIZE:
1136	    parselist++;
1137            {
1138               GLint size = *parselist++;
1139               accumBlueSize = MAX2( accumBlueSize, size );
1140            }
1141            break;
1142	 case GLX_ACCUM_ALPHA_SIZE:
1143	    parselist++;
1144            {
1145               GLint size = *parselist++;
1146               accumAlphaSize = MAX2( accumAlphaSize, size );
1147            }
1148	    break;
1149
1150         /*
1151          * GLX_EXT_visual_info extension
1152          */
1153         case GLX_X_VISUAL_TYPE_EXT:
1154            parselist++;
1155            visual_type = *parselist++;
1156            break;
1157         case GLX_TRANSPARENT_TYPE_EXT:
1158            parselist++;
1159            trans_type = *parselist++;
1160            break;
1161         case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1162            parselist++;
1163            trans_value = *parselist++;
1164            break;
1165         case GLX_TRANSPARENT_RED_VALUE_EXT:
1166         case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1167         case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1168         case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1169	    /* ignore */
1170	    parselist++;
1171	    parselist++;
1172	    break;
1173
1174         /*
1175          * GLX_EXT_visual_info extension
1176          */
1177         case GLX_VISUAL_CAVEAT_EXT:
1178            parselist++;
1179            caveat = *parselist++; /* ignored for now */
1180            break;
1181
1182         /*
1183          * GLX_ARB_multisample
1184          */
1185         case GLX_SAMPLE_BUFFERS_ARB:
1186         case GLX_SAMPLES_ARB:
1187	    parselist++;
1188	    if (*parselist++ != 0)
1189	       /* ms not supported */
1190	       return NULL;
1191	    break;
1192
1193         /*
1194          * FBConfig attribs.
1195          */
1196         case GLX_RENDER_TYPE:
1197            if (!fbConfig)
1198               return NULL;
1199            parselist++;
1200            if (*parselist == GLX_RGBA_BIT) {
1201               rgb_flag = GL_TRUE;
1202            }
1203            else if (*parselist == GLX_COLOR_INDEX_BIT) {
1204               rgb_flag = GL_FALSE;
1205            }
1206            else if (*parselist == 0) {
1207               rgb_flag = GL_TRUE;
1208            }
1209            parselist++;
1210            break;
1211         case GLX_DRAWABLE_TYPE:
1212            if (!fbConfig)
1213               return NULL;
1214            parselist++;
1215            if (*parselist & ~(GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT)) {
1216               return NULL; /* bad bit */
1217            }
1218            parselist++;
1219            break;
1220         case GLX_FBCONFIG_ID:
1221            if (!fbConfig)
1222               return NULL;
1223            parselist++;
1224            desiredVisualID = *parselist++;
1225            break;
1226         case GLX_X_RENDERABLE:
1227            if (!fbConfig)
1228               return NULL;
1229            parselist += 2;
1230            /* ignore */
1231            break;
1232
1233#ifdef GLX_EXT_texture_from_pixmap
1234         case GLX_BIND_TO_TEXTURE_RGB_EXT:
1235            parselist++; /*skip*/
1236            break;
1237         case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1238            parselist++; /*skip*/
1239            break;
1240         case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1241            parselist++; /*skip*/
1242            break;
1243         case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1244            parselist++;
1245            if (*parselist & ~(GLX_TEXTURE_1D_BIT_EXT |
1246                               GLX_TEXTURE_2D_BIT_EXT |
1247                               GLX_TEXTURE_RECTANGLE_BIT_EXT)) {
1248               /* invalid bit */
1249               return NULL;
1250            }
1251            break;
1252         case GLX_Y_INVERTED_EXT:
1253            parselist++; /*skip*/
1254            break;
1255#endif
1256
1257	 case None:
1258            /* end of list */
1259	    break;
1260
1261	 default:
1262	    /* undefined attribute */
1263            _mesa_warning(NULL, "unexpected attrib 0x%x in choose_visual()",
1264                          *parselist);
1265	    return NULL;
1266      }
1267   }
1268
1269   (void) caveat;
1270
1271   /*
1272    * Since we're only simulating the GLX extension this function will never
1273    * find any real GL visuals.  Instead, all we can do is try to find an RGB
1274    * or CI visual of appropriate depth.  Other requested attributes such as
1275    * double buffering, depth buffer, etc. will be associated with the X
1276    * visual and stored in the VisualTable[].
1277    */
1278   if (desiredVisualID != -1) {
1279      /* try to get a specific visual, by visualID */
1280      XVisualInfo temp;
1281      int n;
1282      temp.visualid = desiredVisualID;
1283      temp.screen = screen;
1284      vis = XGetVisualInfo(dpy, VisualIDMask | VisualScreenMask, &temp, &n);
1285      if (vis) {
1286         /* give the visual some useful GLX attributes */
1287         double_flag = GL_TRUE;
1288         if (vis->depth > 8)
1289            rgb_flag = GL_TRUE;
1290         depth_size = default_depth_bits();
1291         stencil_size = STENCIL_BITS;
1292         /* XXX accum??? */
1293      }
1294   }
1295   else if (level==0) {
1296      /* normal color planes */
1297      if (rgb_flag) {
1298         /* Get an RGB visual */
1299         int min_rgb = min_red + min_green + min_blue;
1300         if (min_rgb>1 && min_rgb<8) {
1301            /* a special case to be sure we can get a monochrome visual */
1302            min_rgb = 1;
1303         }
1304         vis = choose_x_visual( dpy, screen, rgb_flag, min_rgb, visual_type );
1305      }
1306      else {
1307         /* Get a color index visual */
1308         vis = choose_x_visual( dpy, screen, rgb_flag, min_ci, visual_type );
1309         accumRedSize = accumGreenSize = accumBlueSize = accumAlphaSize = 0;
1310      }
1311   }
1312   else {
1313      /* over/underlay planes */
1314      if (rgb_flag) {
1315         /* rgba overlay */
1316         int min_rgb = min_red + min_green + min_blue;
1317         if (min_rgb>1 && min_rgb<8) {
1318            /* a special case to be sure we can get a monochrome visual */
1319            min_rgb = 1;
1320         }
1321         vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1322                              trans_type, trans_value, min_rgb, visual_type );
1323      }
1324      else {
1325         /* color index overlay */
1326         vis = choose_x_overlay_visual( dpy, screen, rgb_flag, level,
1327                              trans_type, trans_value, min_ci, visual_type );
1328      }
1329   }
1330
1331   if (vis) {
1332      /* Note: we're not exactly obeying the glXChooseVisual rules here.
1333       * When GLX_DEPTH_SIZE = 1 is specified we're supposed to choose the
1334       * largest depth buffer size, which is 32bits/value.  Instead, we
1335       * return 16 to maintain performance with earlier versions of Mesa.
1336       */
1337      if (depth_size > 24)
1338         depth_size = 32;
1339      else if (depth_size > 16)
1340         depth_size = 24;
1341      else if (depth_size > 0) {
1342         depth_size = default_depth_bits();
1343      }
1344
1345      if (!alpha_flag) {
1346         alpha_flag = default_alpha_bits() > 0;
1347      }
1348
1349      /* we only support one size of stencil and accum buffers. */
1350      if (stencil_size > 0)
1351         stencil_size = STENCIL_BITS;
1352      if (accumRedSize > 0 || accumGreenSize > 0 || accumBlueSize > 0 ||
1353          accumAlphaSize > 0) {
1354         accumRedSize =
1355         accumGreenSize =
1356         accumBlueSize = default_accum_bits();
1357         accumAlphaSize = alpha_flag ? accumRedSize : 0;
1358      }
1359
1360      xmvis = save_glx_visual( dpy, vis, rgb_flag, alpha_flag, double_flag,
1361                               stereo_flag, depth_size, stencil_size,
1362                               accumRedSize, accumGreenSize,
1363                               accumBlueSize, accumAlphaSize, level, numAux );
1364   }
1365
1366   return xmvis;
1367}
1368
1369
1370static XVisualInfo *
1371Fake_glXChooseVisual( Display *dpy, int screen, int *list )
1372{
1373   XMesaVisual xmvis;
1374
1375   /* register ourselves as an extension on this display */
1376   register_with_display(dpy);
1377
1378   xmvis = choose_visual(dpy, screen, list, GL_FALSE);
1379   if (xmvis) {
1380#if 0
1381      return xmvis->vishandle;
1382#else
1383      /* create a new vishandle - the cached one may be stale */
1384      xmvis->vishandle = (XVisualInfo *) _mesa_malloc(sizeof(XVisualInfo));
1385      if (xmvis->vishandle) {
1386         _mesa_memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
1387      }
1388      return xmvis->vishandle;
1389#endif
1390   }
1391   else
1392      return NULL;
1393}
1394
1395
1396/**
1397 * Init basic fields of a new fake_glx_context.
1398 * If the MESA_GLX_FORCE_DIRECT env var is set, the context will be marked as
1399 * a direct rendering context.  Some apps won't run without this.
1400 */
1401static void
1402init_glx_context(struct fake_glx_context *glxCtx, Display *dpy)
1403{
1404   GLboolean direct = _mesa_getenv("MESA_GLX_FORCE_DIRECT") ? GL_TRUE : GL_FALSE;
1405   glxCtx->xmesaContext->direct = direct;
1406   glxCtx->glxContext.isDirect = direct;
1407   glxCtx->glxContext.currentDpy = dpy;
1408   glxCtx->glxContext.xid = (XID) glxCtx;  /* self pointer */
1409
1410   assert((void *) glxCtx == (void *) &(glxCtx->glxContext));
1411}
1412
1413
1414
1415static GLXContext
1416Fake_glXCreateContext( Display *dpy, XVisualInfo *visinfo,
1417                       GLXContext share_list, Bool direct )
1418{
1419   XMesaVisual xmvis;
1420   struct fake_glx_context *glxCtx;
1421   struct fake_glx_context *shareCtx = (struct fake_glx_context *) share_list;
1422
1423   if (!dpy || !visinfo)
1424      return 0;
1425
1426   glxCtx = CALLOC_STRUCT(fake_glx_context);
1427   if (!glxCtx)
1428      return 0;
1429
1430   /* deallocate unused windows/buffers */
1431#if 0
1432   XMesaGarbageCollect();
1433#endif
1434
1435   xmvis = find_glx_visual( dpy, visinfo );
1436   if (!xmvis) {
1437      /* This visual wasn't found with glXChooseVisual() */
1438      xmvis = create_glx_visual( dpy, visinfo );
1439      if (!xmvis) {
1440         /* unusable visual */
1441         _mesa_free(glxCtx);
1442         return NULL;
1443      }
1444   }
1445
1446   glxCtx->xmesaContext = XMesaCreateContext(xmvis,
1447                                   shareCtx ? shareCtx->xmesaContext : NULL);
1448   if (!glxCtx->xmesaContext) {
1449      _mesa_free(glxCtx);
1450      return NULL;
1451   }
1452
1453   init_glx_context(glxCtx, dpy);
1454
1455   return (GLXContext) glxCtx;
1456}
1457
1458
1459/* XXX these may have to be removed due to thread-safety issues. */
1460static GLXContext MakeCurrent_PrevContext = 0;
1461static GLXDrawable MakeCurrent_PrevDrawable = 0;
1462static GLXDrawable MakeCurrent_PrevReadable = 0;
1463static XMesaBuffer MakeCurrent_PrevDrawBuffer = 0;
1464static XMesaBuffer MakeCurrent_PrevReadBuffer = 0;
1465
1466
1467/* GLX 1.3 and later */
1468static Bool
1469Fake_glXMakeContextCurrent( Display *dpy, GLXDrawable draw,
1470                            GLXDrawable read, GLXContext ctx )
1471{
1472   struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
1473
1474   if (ctx && draw && read) {
1475      XMesaBuffer drawBuffer, readBuffer;
1476      XMesaContext xmctx = glxCtx->xmesaContext;
1477
1478      /* Find the XMesaBuffer which corresponds to the GLXDrawable 'draw' */
1479      if (ctx == MakeCurrent_PrevContext
1480          && draw == MakeCurrent_PrevDrawable) {
1481         drawBuffer = MakeCurrent_PrevDrawBuffer;
1482      }
1483      else {
1484         drawBuffer = XMesaFindBuffer( dpy, draw );
1485      }
1486      if (!drawBuffer) {
1487         /* drawable must be a new window! */
1488         drawBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, draw );
1489         if (!drawBuffer) {
1490            /* Out of memory, or context/drawable depth mismatch */
1491            return False;
1492         }
1493#ifdef FX
1494         FXcreateContext( xmctx->xm_visual, draw, xmctx, drawBuffer );
1495#endif
1496      }
1497
1498      /* Find the XMesaBuffer which corresponds to the GLXDrawable 'read' */
1499      if (ctx == MakeCurrent_PrevContext
1500          && read == MakeCurrent_PrevReadable) {
1501         readBuffer = MakeCurrent_PrevReadBuffer;
1502      }
1503      else {
1504         readBuffer = XMesaFindBuffer( dpy, read );
1505      }
1506      if (!readBuffer) {
1507         /* drawable must be a new window! */
1508         readBuffer = XMesaCreateWindowBuffer( xmctx->xm_visual, read );
1509         if (!readBuffer) {
1510            /* Out of memory, or context/drawable depth mismatch */
1511            return False;
1512         }
1513#ifdef FX
1514         FXcreateContext( xmctx->xm_visual, read, xmctx, readBuffer );
1515#endif
1516      }
1517
1518      MakeCurrent_PrevContext = ctx;
1519      MakeCurrent_PrevDrawable = draw;
1520      MakeCurrent_PrevReadable = read;
1521      MakeCurrent_PrevDrawBuffer = drawBuffer;
1522      MakeCurrent_PrevReadBuffer = readBuffer;
1523
1524      /* Now make current! */
1525      if (XMesaMakeCurrent2(xmctx, drawBuffer, readBuffer)) {
1526         ((__GLXcontext *) ctx)->currentDpy = dpy;
1527         ((__GLXcontext *) ctx)->currentDrawable = draw;
1528         ((__GLXcontext *) ctx)->currentReadable = read;
1529         return True;
1530      }
1531      else {
1532         return False;
1533      }
1534   }
1535   else if (!ctx && !draw && !read) {
1536      /* release current context w/out assigning new one. */
1537      XMesaMakeCurrent( NULL, NULL );
1538      MakeCurrent_PrevContext = 0;
1539      MakeCurrent_PrevDrawable = 0;
1540      MakeCurrent_PrevReadable = 0;
1541      MakeCurrent_PrevDrawBuffer = 0;
1542      MakeCurrent_PrevReadBuffer = 0;
1543      return True;
1544   }
1545   else {
1546      /* The args must either all be non-zero or all zero.
1547       * This is an error.
1548       */
1549      return False;
1550   }
1551}
1552
1553
1554static Bool
1555Fake_glXMakeCurrent( Display *dpy, GLXDrawable drawable, GLXContext ctx )
1556{
1557   return Fake_glXMakeContextCurrent( dpy, drawable, drawable, ctx );
1558}
1559
1560
1561static GLXPixmap
1562Fake_glXCreateGLXPixmap( Display *dpy, XVisualInfo *visinfo, Pixmap pixmap )
1563{
1564   XMesaVisual v;
1565   XMesaBuffer b;
1566
1567   v = find_glx_visual( dpy, visinfo );
1568   if (!v) {
1569      v = create_glx_visual( dpy, visinfo );
1570      if (!v) {
1571         /* unusable visual */
1572         return 0;
1573      }
1574   }
1575
1576   b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
1577   if (!b) {
1578      return 0;
1579   }
1580   return b->frontxrb->pixmap;
1581}
1582
1583
1584/*** GLX_MESA_pixmap_colormap ***/
1585
1586static GLXPixmap
1587Fake_glXCreateGLXPixmapMESA( Display *dpy, XVisualInfo *visinfo,
1588                             Pixmap pixmap, Colormap cmap )
1589{
1590   XMesaVisual v;
1591   XMesaBuffer b;
1592
1593   v = find_glx_visual( dpy, visinfo );
1594   if (!v) {
1595      v = create_glx_visual( dpy, visinfo );
1596      if (!v) {
1597         /* unusable visual */
1598         return 0;
1599      }
1600   }
1601
1602   b = XMesaCreatePixmapBuffer( v, pixmap, cmap );
1603   if (!b) {
1604      return 0;
1605   }
1606   return b->frontxrb->pixmap;
1607}
1608
1609
1610static void
1611Fake_glXDestroyGLXPixmap( Display *dpy, GLXPixmap pixmap )
1612{
1613   XMesaBuffer b = XMesaFindBuffer(dpy, pixmap);
1614   if (b) {
1615      XMesaDestroyBuffer(b);
1616   }
1617   else if (_mesa_getenv("MESA_DEBUG")) {
1618      _mesa_warning(NULL, "Mesa: glXDestroyGLXPixmap: invalid pixmap\n");
1619   }
1620}
1621
1622
1623static void
1624Fake_glXCopyContext( Display *dpy, GLXContext src, GLXContext dst,
1625                     unsigned long mask )
1626{
1627   struct fake_glx_context *fakeSrc = (struct fake_glx_context *) src;
1628   struct fake_glx_context *fakeDst = (struct fake_glx_context *) dst;
1629   XMesaContext xm_src = fakeSrc->xmesaContext;
1630   XMesaContext xm_dst = fakeDst->xmesaContext;
1631   (void) dpy;
1632   if (MakeCurrent_PrevContext == src) {
1633      _mesa_Flush();
1634   }
1635   _mesa_copy_context( &(xm_src->mesa), &(xm_dst->mesa), (GLuint) mask );
1636}
1637
1638
1639static Bool
1640Fake_glXQueryExtension( Display *dpy, int *errorb, int *event )
1641{
1642   /* Mesa's GLX isn't really an X extension but we try to act like one. */
1643   (void) dpy;
1644   (void) errorb;
1645   (void) event;
1646   return True;
1647}
1648
1649
1650extern void _kw_ungrab_all( Display *dpy );
1651void _kw_ungrab_all( Display *dpy )
1652{
1653   XUngrabPointer( dpy, CurrentTime );
1654   XUngrabKeyboard( dpy, CurrentTime );
1655}
1656
1657
1658static void
1659Fake_glXDestroyContext( Display *dpy, GLXContext ctx )
1660{
1661   struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
1662   (void) dpy;
1663   MakeCurrent_PrevContext = 0;
1664   MakeCurrent_PrevDrawable = 0;
1665   MakeCurrent_PrevReadable = 0;
1666   MakeCurrent_PrevDrawBuffer = 0;
1667   MakeCurrent_PrevReadBuffer = 0;
1668   XMesaDestroyContext( glxCtx->xmesaContext );
1669   XMesaGarbageCollect();
1670   _mesa_free(glxCtx);
1671}
1672
1673
1674static Bool
1675Fake_glXIsDirect( Display *dpy, GLXContext ctx )
1676{
1677   struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
1678   (void) dpy;
1679   return glxCtx->xmesaContext->direct;
1680}
1681
1682
1683
1684static void
1685Fake_glXSwapBuffers( Display *dpy, GLXDrawable drawable )
1686{
1687   XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1688
1689   if (buffer) {
1690      XMesaSwapBuffers(buffer);
1691   }
1692   else if (_mesa_getenv("MESA_DEBUG")) {
1693      _mesa_warning(NULL, "glXSwapBuffers: invalid drawable 0x%x\n",
1694                    (int) drawable);
1695   }
1696}
1697
1698
1699
1700/*** GLX_MESA_copy_sub_buffer ***/
1701
1702static void
1703Fake_glXCopySubBufferMESA( Display *dpy, GLXDrawable drawable,
1704                           int x, int y, int width, int height )
1705{
1706   XMesaBuffer buffer = XMesaFindBuffer( dpy, drawable );
1707   if (buffer) {
1708      XMesaCopySubBuffer(buffer, x, y, width, height);
1709   }
1710   else if (_mesa_getenv("MESA_DEBUG")) {
1711      _mesa_warning(NULL, "Mesa: glXCopySubBufferMESA: invalid drawable\n");
1712   }
1713}
1714
1715
1716static Bool
1717Fake_glXQueryVersion( Display *dpy, int *maj, int *min )
1718{
1719   (void) dpy;
1720   /* Return GLX version, not Mesa version */
1721   assert(CLIENT_MAJOR_VERSION == SERVER_MAJOR_VERSION);
1722   *maj = CLIENT_MAJOR_VERSION;
1723   *min = MIN2( CLIENT_MINOR_VERSION, SERVER_MINOR_VERSION );
1724   return True;
1725}
1726
1727
1728/*
1729 * Query the GLX attributes of the given XVisualInfo.
1730 */
1731static int
1732get_config( XMesaVisual xmvis, int attrib, int *value, GLboolean fbconfig )
1733{
1734   ASSERT(xmvis);
1735   switch(attrib) {
1736      case GLX_USE_GL:
1737         if (fbconfig)
1738            return GLX_BAD_ATTRIBUTE;
1739         *value = (int) True;
1740	 return 0;
1741      case GLX_BUFFER_SIZE:
1742	 *value = xmvis->visinfo->depth;
1743	 return 0;
1744      case GLX_LEVEL:
1745	 *value = xmvis->mesa_visual.level;
1746	 return 0;
1747      case GLX_RGBA:
1748         if (fbconfig)
1749            return GLX_BAD_ATTRIBUTE;
1750	 if (xmvis->mesa_visual.rgbMode) {
1751	    *value = True;
1752	 }
1753	 else {
1754	    *value = False;
1755	 }
1756	 return 0;
1757      case GLX_DOUBLEBUFFER:
1758	 *value = (int) xmvis->mesa_visual.doubleBufferMode;
1759	 return 0;
1760      case GLX_STEREO:
1761	 *value = (int) xmvis->mesa_visual.stereoMode;
1762	 return 0;
1763      case GLX_AUX_BUFFERS:
1764	 *value = xmvis->mesa_visual.numAuxBuffers;
1765	 return 0;
1766      case GLX_RED_SIZE:
1767         *value = xmvis->mesa_visual.redBits;
1768	 return 0;
1769      case GLX_GREEN_SIZE:
1770         *value = xmvis->mesa_visual.greenBits;
1771	 return 0;
1772      case GLX_BLUE_SIZE:
1773         *value = xmvis->mesa_visual.blueBits;
1774	 return 0;
1775      case GLX_ALPHA_SIZE:
1776         *value = xmvis->mesa_visual.alphaBits;
1777	 return 0;
1778      case GLX_DEPTH_SIZE:
1779         *value = xmvis->mesa_visual.depthBits;
1780	 return 0;
1781      case GLX_STENCIL_SIZE:
1782	 *value = xmvis->mesa_visual.stencilBits;
1783	 return 0;
1784      case GLX_ACCUM_RED_SIZE:
1785	 *value = xmvis->mesa_visual.accumRedBits;
1786	 return 0;
1787      case GLX_ACCUM_GREEN_SIZE:
1788	 *value = xmvis->mesa_visual.accumGreenBits;
1789	 return 0;
1790      case GLX_ACCUM_BLUE_SIZE:
1791	 *value = xmvis->mesa_visual.accumBlueBits;
1792	 return 0;
1793      case GLX_ACCUM_ALPHA_SIZE:
1794         *value = xmvis->mesa_visual.accumAlphaBits;
1795	 return 0;
1796
1797      /*
1798       * GLX_EXT_visual_info extension
1799       */
1800      case GLX_X_VISUAL_TYPE_EXT:
1801         switch (xmvis->visinfo->CLASS) {
1802            case StaticGray:   *value = GLX_STATIC_GRAY_EXT;   return 0;
1803            case GrayScale:    *value = GLX_GRAY_SCALE_EXT;    return 0;
1804            case StaticColor:  *value = GLX_STATIC_GRAY_EXT;   return 0;
1805            case PseudoColor:  *value = GLX_PSEUDO_COLOR_EXT;  return 0;
1806            case TrueColor:    *value = GLX_TRUE_COLOR_EXT;    return 0;
1807            case DirectColor:  *value = GLX_DIRECT_COLOR_EXT;  return 0;
1808         }
1809         return 0;
1810      case GLX_TRANSPARENT_TYPE_EXT:
1811         if (xmvis->mesa_visual.level==0) {
1812            /* normal planes */
1813            *value = GLX_NONE_EXT;
1814         }
1815         else if (xmvis->mesa_visual.level>0) {
1816            /* overlay */
1817            if (xmvis->mesa_visual.rgbMode) {
1818               *value = GLX_TRANSPARENT_RGB_EXT;
1819            }
1820            else {
1821               *value = GLX_TRANSPARENT_INDEX_EXT;
1822            }
1823         }
1824         else if (xmvis->mesa_visual.level<0) {
1825            /* underlay */
1826            *value = GLX_NONE_EXT;
1827         }
1828         return 0;
1829      case GLX_TRANSPARENT_INDEX_VALUE_EXT:
1830         {
1831            int pixel = transparent_pixel( xmvis );
1832            if (pixel>=0) {
1833               *value = pixel;
1834            }
1835            /* else undefined */
1836         }
1837         return 0;
1838      case GLX_TRANSPARENT_RED_VALUE_EXT:
1839         /* undefined */
1840         return 0;
1841      case GLX_TRANSPARENT_GREEN_VALUE_EXT:
1842         /* undefined */
1843         return 0;
1844      case GLX_TRANSPARENT_BLUE_VALUE_EXT:
1845         /* undefined */
1846         return 0;
1847      case GLX_TRANSPARENT_ALPHA_VALUE_EXT:
1848         /* undefined */
1849         return 0;
1850
1851      /*
1852       * GLX_EXT_visual_info extension
1853       */
1854      case GLX_VISUAL_CAVEAT_EXT:
1855         /* test for zero, just in case */
1856         if (xmvis->mesa_visual.visualRating > 0)
1857            *value = xmvis->mesa_visual.visualRating;
1858         else
1859            *value = GLX_NONE_EXT;
1860         return 0;
1861
1862      /*
1863       * GLX_ARB_multisample
1864       */
1865      case GLX_SAMPLE_BUFFERS_ARB:
1866         *value = 0;
1867         return 0;
1868      case GLX_SAMPLES_ARB:
1869         *value = 0;
1870         return 0;
1871
1872      /*
1873       * For FBConfigs:
1874       */
1875      case GLX_SCREEN_EXT:
1876         if (!fbconfig)
1877            return GLX_BAD_ATTRIBUTE;
1878         *value = xmvis->visinfo->screen;
1879         break;
1880      case GLX_DRAWABLE_TYPE: /*SGIX too */
1881         if (!fbconfig)
1882            return GLX_BAD_ATTRIBUTE;
1883         *value = GLX_WINDOW_BIT | GLX_PIXMAP_BIT | GLX_PBUFFER_BIT;
1884         break;
1885      case GLX_RENDER_TYPE_SGIX:
1886         if (!fbconfig)
1887            return GLX_BAD_ATTRIBUTE;
1888         if (xmvis->mesa_visual.rgbMode)
1889            *value = GLX_RGBA_BIT;
1890         else
1891            *value = GLX_COLOR_INDEX_BIT;
1892         break;
1893      case GLX_X_RENDERABLE_SGIX:
1894         if (!fbconfig)
1895            return GLX_BAD_ATTRIBUTE;
1896         *value = True; /* XXX really? */
1897         break;
1898      case GLX_FBCONFIG_ID_SGIX:
1899         if (!fbconfig)
1900            return GLX_BAD_ATTRIBUTE;
1901         *value = xmvis->visinfo->visualid;
1902         break;
1903      case GLX_MAX_PBUFFER_WIDTH:
1904         if (!fbconfig)
1905            return GLX_BAD_ATTRIBUTE;
1906         /* XXX or MAX_WIDTH? */
1907         *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen);
1908         break;
1909      case GLX_MAX_PBUFFER_HEIGHT:
1910         if (!fbconfig)
1911            return GLX_BAD_ATTRIBUTE;
1912         *value = DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1913         break;
1914      case GLX_MAX_PBUFFER_PIXELS:
1915         if (!fbconfig)
1916            return GLX_BAD_ATTRIBUTE;
1917         *value = DisplayWidth(xmvis->display, xmvis->visinfo->screen) *
1918                  DisplayHeight(xmvis->display, xmvis->visinfo->screen);
1919         break;
1920      case GLX_VISUAL_ID:
1921         if (!fbconfig)
1922            return GLX_BAD_ATTRIBUTE;
1923         *value = xmvis->visinfo->visualid;
1924         break;
1925
1926#ifdef GLX_EXT_texture_from_pixmap
1927      case GLX_BIND_TO_TEXTURE_RGB_EXT:
1928         *value = True; /*XXX*/
1929         break;
1930      case GLX_BIND_TO_TEXTURE_RGBA_EXT:
1931         /* XXX review */
1932         *value = xmvis->mesa_visual.alphaBits > 0 ? True : False;
1933         break;
1934      case GLX_BIND_TO_MIPMAP_TEXTURE_EXT:
1935         *value = True; /*XXX*/
1936         break;
1937      case GLX_BIND_TO_TEXTURE_TARGETS_EXT:
1938         *value = (GLX_TEXTURE_1D_BIT_EXT |
1939                   GLX_TEXTURE_2D_BIT_EXT |
1940                   GLX_TEXTURE_RECTANGLE_BIT_EXT); /*XXX*/
1941         break;
1942      case GLX_Y_INVERTED_EXT:
1943         *value = True; /*XXX*/
1944         break;
1945#endif
1946
1947      default:
1948	 return GLX_BAD_ATTRIBUTE;
1949   }
1950   return Success;
1951}
1952
1953
1954static int
1955Fake_glXGetConfig( Display *dpy, XVisualInfo *visinfo,
1956                   int attrib, int *value )
1957{
1958   XMesaVisual xmvis;
1959   int k;
1960   if (!dpy || !visinfo)
1961      return GLX_BAD_ATTRIBUTE;
1962
1963   xmvis = find_glx_visual( dpy, visinfo );
1964   if (!xmvis) {
1965      /* this visual wasn't obtained with glXChooseVisual */
1966      xmvis = create_glx_visual( dpy, visinfo );
1967      if (!xmvis) {
1968	 /* this visual can't be used for GL rendering */
1969	 if (attrib==GLX_USE_GL) {
1970	    *value = (int) False;
1971	    return 0;
1972	 }
1973	 else {
1974	    return GLX_BAD_VISUAL;
1975	 }
1976      }
1977   }
1978
1979   k = get_config(xmvis, attrib, value, GL_FALSE);
1980   return k;
1981}
1982
1983
1984static void
1985Fake_glXWaitGL( void )
1986{
1987   XMesaContext xmesa = XMesaGetCurrentContext();
1988   XMesaFlush( xmesa );
1989}
1990
1991
1992
1993static void
1994Fake_glXWaitX( void )
1995{
1996   XMesaContext xmesa = XMesaGetCurrentContext();
1997   XMesaFlush( xmesa );
1998}
1999
2000
2001static const char *
2002get_extensions( void )
2003{
2004#ifdef FX
2005   const char *fx = _mesa_getenv("MESA_GLX_FX");
2006   if (fx && fx[0] != 'd') {
2007      return EXTENSIONS;
2008   }
2009#endif
2010   return EXTENSIONS + 23; /* skip "GLX_MESA_set_3dfx_mode" */
2011}
2012
2013
2014
2015/* GLX 1.1 and later */
2016static const char *
2017Fake_glXQueryExtensionsString( Display *dpy, int screen )
2018{
2019   (void) dpy;
2020   (void) screen;
2021   return get_extensions();
2022}
2023
2024
2025
2026/* GLX 1.1 and later */
2027static const char *
2028Fake_glXQueryServerString( Display *dpy, int screen, int name )
2029{
2030   static char version[1000];
2031   _mesa_sprintf(version, "%d.%d %s",
2032                 SERVER_MAJOR_VERSION, SERVER_MINOR_VERSION, MESA_GLX_VERSION);
2033
2034   (void) dpy;
2035   (void) screen;
2036
2037   switch (name) {
2038      case GLX_EXTENSIONS:
2039         return get_extensions();
2040      case GLX_VENDOR:
2041	 return VENDOR;
2042      case GLX_VERSION:
2043	 return version;
2044      default:
2045         return NULL;
2046   }
2047}
2048
2049
2050
2051/* GLX 1.1 and later */
2052static const char *
2053Fake_glXGetClientString( Display *dpy, int name )
2054{
2055   static char version[1000];
2056   _mesa_sprintf(version, "%d.%d %s", CLIENT_MAJOR_VERSION,
2057                 CLIENT_MINOR_VERSION, MESA_GLX_VERSION);
2058
2059   (void) dpy;
2060
2061   switch (name) {
2062      case GLX_EXTENSIONS:
2063         return get_extensions();
2064      case GLX_VENDOR:
2065	 return VENDOR;
2066      case GLX_VERSION:
2067	 return version;
2068      default:
2069         return NULL;
2070   }
2071}
2072
2073
2074
2075/*
2076 * GLX 1.3 and later
2077 */
2078
2079
2080static int
2081Fake_glXGetFBConfigAttrib( Display *dpy, GLXFBConfig config,
2082                           int attribute, int *value )
2083{
2084   XMesaVisual v = (XMesaVisual) config;
2085   (void) dpy;
2086   (void) config;
2087
2088   if (!dpy || !config || !value)
2089      return -1;
2090
2091   return get_config(v, attribute, value, GL_TRUE);
2092}
2093
2094
2095static GLXFBConfig *
2096Fake_glXGetFBConfigs( Display *dpy, int screen, int *nelements )
2097{
2098   XVisualInfo *visuals, visTemplate;
2099   const long visMask = VisualScreenMask;
2100   int i;
2101
2102   /* Get list of all X visuals */
2103   visTemplate.screen = screen;
2104   visuals = XGetVisualInfo(dpy, visMask, &visTemplate, nelements);
2105   if (*nelements > 0) {
2106      XMesaVisual *results;
2107      results = (XMesaVisual *) _mesa_malloc(*nelements * sizeof(XMesaVisual));
2108      if (!results) {
2109         *nelements = 0;
2110         return NULL;
2111      }
2112      for (i = 0; i < *nelements; i++) {
2113         results[i] = create_glx_visual(dpy, visuals + i);
2114      }
2115      return (GLXFBConfig *) results;
2116   }
2117   return NULL;
2118}
2119
2120
2121static GLXFBConfig *
2122Fake_glXChooseFBConfig( Display *dpy, int screen,
2123                        const int *attribList, int *nitems )
2124{
2125   XMesaVisual xmvis;
2126
2127   if (!attribList || !attribList[0]) {
2128      /* return list of all configs (per GLX_SGIX_fbconfig spec) */
2129      return Fake_glXGetFBConfigs(dpy, screen, nitems);
2130   }
2131
2132   xmvis = choose_visual(dpy, screen, attribList, GL_TRUE);
2133   if (xmvis) {
2134      GLXFBConfig *config = (GLXFBConfig *) _mesa_malloc(sizeof(XMesaVisual));
2135      if (!config) {
2136         *nitems = 0;
2137         return NULL;
2138      }
2139      *nitems = 1;
2140      config[0] = (GLXFBConfig) xmvis;
2141      return (GLXFBConfig *) config;
2142   }
2143   else {
2144      *nitems = 0;
2145      return NULL;
2146   }
2147}
2148
2149
2150static XVisualInfo *
2151Fake_glXGetVisualFromFBConfig( Display *dpy, GLXFBConfig config )
2152{
2153   if (dpy && config) {
2154      XMesaVisual xmvis = (XMesaVisual) config;
2155#if 0
2156      return xmvis->vishandle;
2157#else
2158      /* create a new vishandle - the cached one may be stale */
2159      xmvis->vishandle = (XVisualInfo *) _mesa_malloc(sizeof(XVisualInfo));
2160      if (xmvis->vishandle) {
2161         _mesa_memcpy(xmvis->vishandle, xmvis->visinfo, sizeof(XVisualInfo));
2162      }
2163      return xmvis->vishandle;
2164#endif
2165   }
2166   else {
2167      return NULL;
2168   }
2169}
2170
2171
2172static GLXWindow
2173Fake_glXCreateWindow( Display *dpy, GLXFBConfig config, Window win,
2174                      const int *attribList )
2175{
2176   XMesaVisual xmvis = (XMesaVisual) config;
2177   XMesaBuffer xmbuf;
2178   if (!xmvis)
2179      return 0;
2180
2181   xmbuf = XMesaCreateWindowBuffer(xmvis, win);
2182   if (!xmbuf)
2183      return 0;
2184
2185#ifdef FX
2186   /* XXX this will segfault if actually called */
2187   FXcreateContext(xmvis, win, NULL, xmbuf);
2188#endif
2189
2190   (void) dpy;
2191   (void) attribList;  /* Ignored in GLX 1.3 */
2192
2193   return win;  /* A hack for now */
2194}
2195
2196
2197static void
2198Fake_glXDestroyWindow( Display *dpy, GLXWindow window )
2199{
2200   XMesaBuffer b = XMesaFindBuffer(dpy, (XMesaDrawable) window);
2201   if (b)
2202      XMesaDestroyBuffer(b);
2203   /* don't destroy X window */
2204}
2205
2206
2207/* XXX untested */
2208static GLXPixmap
2209Fake_glXCreatePixmap( Display *dpy, GLXFBConfig config, Pixmap pixmap,
2210                      const int *attribList )
2211{
2212   XMesaVisual v = (XMesaVisual) config;
2213   XMesaBuffer b;
2214   const int *attr;
2215   int target = 0, format = 0, mipmap = 0;
2216   int value;
2217
2218   if (!dpy || !config || !pixmap)
2219      return 0;
2220
2221   for (attr = attribList; attr && *attr; attr++) {
2222      switch (*attr) {
2223      case GLX_TEXTURE_FORMAT_EXT:
2224         attr++;
2225         switch (*attr) {
2226         case GLX_TEXTURE_FORMAT_NONE_EXT:
2227         case GLX_TEXTURE_FORMAT_RGB_EXT:
2228         case GLX_TEXTURE_FORMAT_RGBA_EXT:
2229            format = *attr;
2230            break;
2231         default:
2232            /* error */
2233            return 0;
2234         }
2235         break;
2236      case GLX_TEXTURE_TARGET_EXT:
2237         attr++;
2238         switch (*attr) {
2239         case GLX_TEXTURE_1D_EXT:
2240         case GLX_TEXTURE_2D_EXT:
2241         case GLX_TEXTURE_RECTANGLE_EXT:
2242            target = *attr;
2243            break;
2244         default:
2245            /* error */
2246            return 0;
2247         }
2248         break;
2249      case GLX_MIPMAP_TEXTURE_EXT:
2250         attr++;
2251         if (*attr)
2252            mipmap = 1;
2253         break;
2254      default:
2255         /* error */
2256         return 0;
2257      }
2258   }
2259
2260   if (format == GLX_TEXTURE_FORMAT_RGB_EXT) {
2261      if (get_config(v, GLX_BIND_TO_TEXTURE_RGB_EXT,
2262                     &value, GL_TRUE) != Success
2263          || !value) {
2264         return 0; /* error! */
2265      }
2266   }
2267   else if (format == GLX_TEXTURE_FORMAT_RGBA_EXT) {
2268      if (get_config(v, GLX_BIND_TO_TEXTURE_RGBA_EXT,
2269                     &value, GL_TRUE) != Success
2270          || !value) {
2271         return 0; /* error! */
2272      }
2273   }
2274   if (mipmap) {
2275      if (get_config(v, GLX_BIND_TO_MIPMAP_TEXTURE_EXT,
2276                     &value, GL_TRUE) != Success
2277          || !value) {
2278         return 0; /* error! */
2279      }
2280   }
2281   if (target == GLX_TEXTURE_1D_EXT) {
2282      if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2283                     &value, GL_TRUE) != Success
2284          || (value & GLX_TEXTURE_1D_BIT_EXT) == 0) {
2285         return 0; /* error! */
2286      }
2287   }
2288   else if (target == GLX_TEXTURE_2D_EXT) {
2289      if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2290                     &value, GL_TRUE) != Success
2291          || (value & GLX_TEXTURE_2D_BIT_EXT) == 0) {
2292         return 0; /* error! */
2293      }
2294   }
2295   if (target == GLX_TEXTURE_RECTANGLE_EXT) {
2296      if (get_config(v, GLX_BIND_TO_TEXTURE_TARGETS_EXT,
2297                     &value, GL_TRUE) != Success
2298          || (value & GLX_TEXTURE_RECTANGLE_BIT_EXT) == 0) {
2299         return 0; /* error! */
2300      }
2301   }
2302
2303   if (format || target || mipmap) {
2304      /* texture from pixmap */
2305      b = XMesaCreatePixmapTextureBuffer(v, pixmap, 0, format, target, mipmap);
2306   }
2307   else {
2308      b = XMesaCreatePixmapBuffer( v, pixmap, 0 );
2309   }
2310   if (!b) {
2311      return 0;
2312   }
2313
2314   return pixmap;
2315}
2316
2317
2318static void
2319Fake_glXDestroyPixmap( Display *dpy, GLXPixmap pixmap )
2320{
2321   XMesaBuffer b = XMesaFindBuffer(dpy, (XMesaDrawable)pixmap);
2322   if (b)
2323      XMesaDestroyBuffer(b);
2324   /* don't destroy X pixmap */
2325}
2326
2327
2328static GLXPbuffer
2329Fake_glXCreatePbuffer( Display *dpy, GLXFBConfig config,
2330                       const int *attribList )
2331{
2332   XMesaVisual xmvis = (XMesaVisual) config;
2333   XMesaBuffer xmbuf;
2334   const int *attrib;
2335   int width = 0, height = 0;
2336   GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2337
2338   (void) dpy;
2339
2340   for (attrib = attribList; *attrib; attrib++) {
2341      switch (*attrib) {
2342         case GLX_PBUFFER_WIDTH:
2343            attrib++;
2344            width = *attrib;
2345            break;
2346         case GLX_PBUFFER_HEIGHT:
2347            attrib++;
2348            height = *attrib;
2349            break;
2350         case GLX_PRESERVED_CONTENTS:
2351            attrib++;
2352            preserveContents = *attrib; /* ignored */
2353            break;
2354         case GLX_LARGEST_PBUFFER:
2355            attrib++;
2356            useLargest = *attrib; /* ignored */
2357            break;
2358         default:
2359            return 0;
2360      }
2361   }
2362
2363   /* not used at this time */
2364   (void) useLargest;
2365   (void) preserveContents;
2366
2367   if (width == 0 || height == 0)
2368      return 0;
2369
2370   xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2371   /* A GLXPbuffer handle must be an X Drawable because that's what
2372    * glXMakeCurrent takes.
2373    */
2374   if (xmbuf)
2375      return (GLXPbuffer) xmbuf->frontxrb->pixmap;
2376   else
2377      return 0;
2378}
2379
2380
2381static void
2382Fake_glXDestroyPbuffer( Display *dpy, GLXPbuffer pbuf )
2383{
2384   XMesaBuffer b = XMesaFindBuffer(dpy, pbuf);
2385   if (b) {
2386      XMesaDestroyBuffer(b);
2387   }
2388}
2389
2390
2391static void
2392Fake_glXQueryDrawable( Display *dpy, GLXDrawable draw, int attribute,
2393                       unsigned int *value )
2394{
2395   XMesaBuffer xmbuf = XMesaFindBuffer(dpy, draw);
2396   if (!xmbuf)
2397      return;
2398
2399   switch (attribute) {
2400      case GLX_WIDTH:
2401         *value = xmbuf->mesa_buffer.Width;
2402         break;
2403      case GLX_HEIGHT:
2404         *value = xmbuf->mesa_buffer.Height;
2405         break;
2406      case GLX_PRESERVED_CONTENTS:
2407         *value = True;
2408         break;
2409      case GLX_LARGEST_PBUFFER:
2410         *value = xmbuf->mesa_buffer.Width * xmbuf->mesa_buffer.Height;
2411         break;
2412      case GLX_FBCONFIG_ID:
2413         *value = xmbuf->xm_visual->visinfo->visualid;
2414         return;
2415#ifdef GLX_EXT_texture_from_pixmap
2416      case GLX_TEXTURE_FORMAT_EXT:
2417         *value = xmbuf->TextureFormat;
2418         break;
2419      case GLX_TEXTURE_TARGET_EXT:
2420         *value = xmbuf->TextureTarget;
2421         break;
2422      case GLX_MIPMAP_TEXTURE_EXT:
2423         *value = xmbuf->TextureMipmap;
2424         break;
2425#endif
2426
2427      default:
2428         return; /* raise BadValue error */
2429   }
2430}
2431
2432
2433static GLXContext
2434Fake_glXCreateNewContext( Display *dpy, GLXFBConfig config,
2435                          int renderType, GLXContext shareList, Bool direct )
2436{
2437   struct fake_glx_context *glxCtx;
2438   struct fake_glx_context *shareCtx = (struct fake_glx_context *) shareList;
2439   XMesaVisual xmvis = (XMesaVisual) config;
2440
2441   if (!dpy || !config ||
2442       (renderType != GLX_RGBA_TYPE && renderType != GLX_COLOR_INDEX_TYPE))
2443      return 0;
2444
2445   glxCtx = CALLOC_STRUCT(fake_glx_context);
2446   if (!glxCtx)
2447      return 0;
2448
2449   /* deallocate unused windows/buffers */
2450   XMesaGarbageCollect();
2451
2452   glxCtx->xmesaContext = XMesaCreateContext(xmvis,
2453                                   shareCtx ? shareCtx->xmesaContext : NULL);
2454   if (!glxCtx->xmesaContext) {
2455      _mesa_free(glxCtx);
2456      return NULL;
2457   }
2458
2459   init_glx_context(glxCtx, dpy);
2460
2461   return (GLXContext) glxCtx;
2462}
2463
2464
2465static int
2466Fake_glXQueryContext( Display *dpy, GLXContext ctx, int attribute, int *value )
2467{
2468   struct fake_glx_context *glxCtx = (struct fake_glx_context *) ctx;
2469   XMesaContext xmctx = glxCtx->xmesaContext;
2470
2471   (void) dpy;
2472   (void) ctx;
2473
2474   switch (attribute) {
2475   case GLX_FBCONFIG_ID:
2476      *value = xmctx->xm_visual->visinfo->visualid;
2477      break;
2478   case GLX_RENDER_TYPE:
2479      if (xmctx->xm_visual->mesa_visual.rgbMode)
2480         *value = GLX_RGBA_BIT;
2481      else
2482         *value = GLX_COLOR_INDEX_BIT;
2483      break;
2484   case GLX_SCREEN:
2485      *value = 0;
2486      return Success;
2487   default:
2488      return GLX_BAD_ATTRIBUTE;
2489   }
2490   return 0;
2491}
2492
2493
2494static void
2495Fake_glXSelectEvent( Display *dpy, GLXDrawable drawable, unsigned long mask )
2496{
2497   XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2498   if (xmbuf)
2499      xmbuf->selectedEvents = mask;
2500}
2501
2502
2503static void
2504Fake_glXGetSelectedEvent( Display *dpy, GLXDrawable drawable,
2505                          unsigned long *mask )
2506{
2507   XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2508   if (xmbuf)
2509      *mask = xmbuf->selectedEvents;
2510   else
2511      *mask = 0;
2512}
2513
2514
2515
2516/*** GLX_SGI_swap_control ***/
2517
2518static int
2519Fake_glXSwapIntervalSGI(int interval)
2520{
2521   (void) interval;
2522   return 0;
2523}
2524
2525
2526
2527/*** GLX_SGI_video_sync ***/
2528
2529static unsigned int FrameCounter = 0;
2530
2531static int
2532Fake_glXGetVideoSyncSGI(unsigned int *count)
2533{
2534   /* this is a bogus implementation */
2535   *count = FrameCounter++;
2536   return 0;
2537}
2538
2539static int
2540Fake_glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
2541{
2542   if (divisor <= 0 || remainder < 0)
2543      return GLX_BAD_VALUE;
2544   /* this is a bogus implementation */
2545   FrameCounter++;
2546   while (FrameCounter % divisor != remainder)
2547      FrameCounter++;
2548   *count = FrameCounter;
2549   return 0;
2550}
2551
2552
2553
2554/*** GLX_SGI_make_current_read ***/
2555
2556static Bool
2557Fake_glXMakeCurrentReadSGI(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx)
2558{
2559   return Fake_glXMakeContextCurrent( dpy, draw, read, ctx );
2560}
2561
2562/* not used
2563static GLXDrawable
2564Fake_glXGetCurrentReadDrawableSGI(void)
2565{
2566   return 0;
2567}
2568*/
2569
2570
2571/*** GLX_SGIX_video_source ***/
2572#if defined(_VL_H)
2573
2574static GLXVideoSourceSGIX
2575Fake_glXCreateGLXVideoSourceSGIX(Display *dpy, int screen, VLServer server, VLPath path, int nodeClass, VLNode drainNode)
2576{
2577   (void) dpy;
2578   (void) screen;
2579   (void) server;
2580   (void) path;
2581   (void) nodeClass;
2582   (void) drainNode;
2583   return 0;
2584}
2585
2586static void
2587Fake_glXDestroyGLXVideoSourceSGIX(Display *dpy, GLXVideoSourceSGIX src)
2588{
2589   (void) dpy;
2590   (void) src;
2591}
2592
2593#endif
2594
2595
2596/*** GLX_EXT_import_context ***/
2597
2598static void
2599Fake_glXFreeContextEXT(Display *dpy, GLXContext context)
2600{
2601   (void) dpy;
2602   (void) context;
2603}
2604
2605static GLXContextID
2606Fake_glXGetContextIDEXT(const GLXContext context)
2607{
2608   (void) context;
2609   return 0;
2610}
2611
2612static GLXContext
2613Fake_glXImportContextEXT(Display *dpy, GLXContextID contextID)
2614{
2615   (void) dpy;
2616   (void) contextID;
2617   return 0;
2618}
2619
2620static int
2621Fake_glXQueryContextInfoEXT(Display *dpy, GLXContext context, int attribute, int *value)
2622{
2623   (void) dpy;
2624   (void) context;
2625   (void) attribute;
2626   (void) value;
2627   return 0;
2628}
2629
2630
2631
2632/*** GLX_SGIX_fbconfig ***/
2633
2634static int
2635Fake_glXGetFBConfigAttribSGIX(Display *dpy, GLXFBConfigSGIX config, int attribute, int *value)
2636{
2637   return Fake_glXGetFBConfigAttrib(dpy, config, attribute, value);
2638}
2639
2640static GLXFBConfigSGIX *
2641Fake_glXChooseFBConfigSGIX(Display *dpy, int screen, int *attrib_list, int *nelements)
2642{
2643   return (GLXFBConfig *) Fake_glXChooseFBConfig(dpy, screen, attrib_list, nelements);
2644}
2645
2646
2647static GLXPixmap
2648Fake_glXCreateGLXPixmapWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config, Pixmap pixmap)
2649{
2650   XMesaVisual xmvis = (XMesaVisual) config;
2651   XMesaBuffer xmbuf = XMesaCreatePixmapBuffer(xmvis, pixmap, 0);
2652   return xmbuf->frontxrb->pixmap; /* need to return an X ID */
2653}
2654
2655
2656static GLXContext
2657Fake_glXCreateContextWithConfigSGIX(Display *dpy, GLXFBConfigSGIX config, int render_type, GLXContext share_list, Bool direct)
2658{
2659   XMesaVisual xmvis = (XMesaVisual) config;
2660   struct fake_glx_context *glxCtx;
2661   struct fake_glx_context *shareCtx = (struct fake_glx_context *) share_list;
2662
2663   glxCtx = CALLOC_STRUCT(fake_glx_context);
2664   if (!glxCtx)
2665      return 0;
2666
2667   /* deallocate unused windows/buffers */
2668   XMesaGarbageCollect();
2669
2670   glxCtx->xmesaContext = XMesaCreateContext(xmvis,
2671                                   shareCtx ? shareCtx->xmesaContext : NULL);
2672   if (!glxCtx->xmesaContext) {
2673      _mesa_free(glxCtx);
2674      return NULL;
2675   }
2676
2677   init_glx_context(glxCtx, dpy);
2678
2679   return (GLXContext) glxCtx;
2680}
2681
2682
2683static XVisualInfo *
2684Fake_glXGetVisualFromFBConfigSGIX(Display *dpy, GLXFBConfigSGIX config)
2685{
2686   return Fake_glXGetVisualFromFBConfig(dpy, config);
2687}
2688
2689
2690static GLXFBConfigSGIX
2691Fake_glXGetFBConfigFromVisualSGIX(Display *dpy, XVisualInfo *vis)
2692{
2693   XMesaVisual xmvis = find_glx_visual(dpy, vis);
2694   if (!xmvis) {
2695      /* This visual wasn't found with glXChooseVisual() */
2696      xmvis = create_glx_visual(dpy, vis);
2697   }
2698
2699   return (GLXFBConfigSGIX) xmvis;
2700}
2701
2702
2703
2704/*** GLX_SGIX_pbuffer ***/
2705
2706static GLXPbufferSGIX
2707Fake_glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config,
2708                             unsigned int width, unsigned int height,
2709                             int *attribList)
2710{
2711   XMesaVisual xmvis = (XMesaVisual) config;
2712   XMesaBuffer xmbuf;
2713   const int *attrib;
2714   GLboolean useLargest = GL_FALSE, preserveContents = GL_FALSE;
2715
2716   (void) dpy;
2717
2718   for (attrib = attribList; attrib && *attrib; attrib++) {
2719      switch (*attrib) {
2720         case GLX_PRESERVED_CONTENTS_SGIX:
2721            attrib++;
2722            preserveContents = *attrib; /* ignored */
2723            break;
2724         case GLX_LARGEST_PBUFFER_SGIX:
2725            attrib++;
2726            useLargest = *attrib; /* ignored */
2727            break;
2728         default:
2729            return 0;
2730      }
2731   }
2732
2733   /* not used at this time */
2734   (void) useLargest;
2735   (void) preserveContents;
2736
2737   xmbuf = XMesaCreatePBuffer( xmvis, 0, width, height);
2738   /* A GLXPbuffer handle must be an X Drawable because that's what
2739    * glXMakeCurrent takes.
2740    */
2741   return (GLXPbuffer) xmbuf->frontxrb->pixmap;
2742}
2743
2744
2745static void
2746Fake_glXDestroyGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf)
2747{
2748   XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2749   if (xmbuf) {
2750      XMesaDestroyBuffer(xmbuf);
2751   }
2752}
2753
2754
2755static int
2756Fake_glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuf, int attribute, unsigned int *value)
2757{
2758   const XMesaBuffer xmbuf = XMesaFindBuffer(dpy, pbuf);
2759
2760   if (!xmbuf) {
2761      /* Generate GLXBadPbufferSGIX for bad pbuffer */
2762      return 0;
2763   }
2764
2765   switch (attribute) {
2766      case GLX_PRESERVED_CONTENTS_SGIX:
2767         *value = True;
2768         break;
2769      case GLX_LARGEST_PBUFFER_SGIX:
2770         *value = xmbuf->mesa_buffer.Width * xmbuf->mesa_buffer.Height;
2771         break;
2772      case GLX_WIDTH_SGIX:
2773         *value = xmbuf->mesa_buffer.Width;
2774         break;
2775      case GLX_HEIGHT_SGIX:
2776         *value = xmbuf->mesa_buffer.Height;
2777         break;
2778      case GLX_EVENT_MASK_SGIX:
2779         *value = 0;  /* XXX might be wrong */
2780         break;
2781      default:
2782         *value = 0;
2783   }
2784   return 0;
2785}
2786
2787
2788static void
2789Fake_glXSelectEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long mask)
2790{
2791   XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2792   if (xmbuf) {
2793      /* Note: we'll never generate clobber events */
2794      xmbuf->selectedEvents = mask;
2795   }
2796}
2797
2798
2799static void
2800Fake_glXGetSelectedEventSGIX(Display *dpy, GLXDrawable drawable, unsigned long *mask)
2801{
2802   XMesaBuffer xmbuf = XMesaFindBuffer(dpy, drawable);
2803   if (xmbuf) {
2804      *mask = xmbuf->selectedEvents;
2805   }
2806   else {
2807      *mask = 0;
2808   }
2809}
2810
2811
2812
2813/*** GLX_SGI_cushion ***/
2814
2815static void
2816Fake_glXCushionSGI(Display *dpy, Window win, float cushion)
2817{
2818   (void) dpy;
2819   (void) win;
2820   (void) cushion;
2821}
2822
2823
2824
2825/*** GLX_SGIX_video_resize ***/
2826
2827static int
2828Fake_glXBindChannelToWindowSGIX(Display *dpy, int screen, int channel , Window window)
2829{
2830   (void) dpy;
2831   (void) screen;
2832   (void) channel;
2833   (void) window;
2834   return 0;
2835}
2836
2837static int
2838Fake_glXChannelRectSGIX(Display *dpy, int screen, int channel, int x, int y, int w, int h)
2839{
2840   (void) dpy;
2841   (void) screen;
2842   (void) channel;
2843   (void) x;
2844   (void) y;
2845   (void) w;
2846   (void) h;
2847   return 0;
2848}
2849
2850static int
2851Fake_glXQueryChannelRectSGIX(Display *dpy, int screen, int channel, int *x, int *y, int *w, int *h)
2852{
2853   (void) dpy;
2854   (void) screen;
2855   (void) channel;
2856   (void) x;
2857   (void) y;
2858   (void) w;
2859   (void) h;
2860   return 0;
2861}
2862
2863static int
2864Fake_glXQueryChannelDeltasSGIX(Display *dpy, int screen, int channel, int *dx, int *dy, int *dw, int *dh)
2865{
2866   (void) dpy;
2867   (void) screen;
2868   (void) channel;
2869   (void) dx;
2870   (void) dy;
2871   (void) dw;
2872   (void) dh;
2873   return 0;
2874}
2875
2876static int
2877Fake_glXChannelRectSyncSGIX(Display *dpy, int screen, int channel, GLenum synctype)
2878{
2879   (void) dpy;
2880   (void) screen;
2881   (void) channel;
2882   (void) synctype;
2883   return 0;
2884}
2885
2886
2887
2888/*** GLX_SGIX_dmbuffer **/
2889
2890#if defined(_DM_BUFFER_H_)
2891static Bool
2892Fake_glXAssociateDMPbufferSGIX(Display *dpy, GLXPbufferSGIX pbuffer, DMparams *params, DMbuffer dmbuffer)
2893{
2894   (void) dpy;
2895   (void) pbuffer;
2896   (void) params;
2897   (void) dmbuffer;
2898   return False;
2899}
2900#endif
2901
2902
2903/*** GLX_SGIX_swap_group ***/
2904
2905static void
2906Fake_glXJoinSwapGroupSGIX(Display *dpy, GLXDrawable drawable, GLXDrawable member)
2907{
2908   (void) dpy;
2909   (void) drawable;
2910   (void) member;
2911}
2912
2913
2914
2915/*** GLX_SGIX_swap_barrier ***/
2916
2917static void
2918Fake_glXBindSwapBarrierSGIX(Display *dpy, GLXDrawable drawable, int barrier)
2919{
2920   (void) dpy;
2921   (void) drawable;
2922   (void) barrier;
2923}
2924
2925static Bool
2926Fake_glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
2927{
2928   (void) dpy;
2929   (void) screen;
2930   (void) max;
2931   return False;
2932}
2933
2934
2935
2936/*** GLX_SUN_get_transparent_index ***/
2937
2938static Status
2939Fake_glXGetTransparentIndexSUN(Display *dpy, Window overlay, Window underlay, long *pTransparent)
2940{
2941   (void) dpy;
2942   (void) overlay;
2943   (void) underlay;
2944   (void) pTransparent;
2945   return 0;
2946}
2947
2948
2949
2950/*** GLX_MESA_release_buffers ***/
2951
2952/*
2953 * Release the depth, stencil, accum buffers attached to a GLXDrawable
2954 * (a window or pixmap) prior to destroying the GLXDrawable.
2955 */
2956static Bool
2957Fake_glXReleaseBuffersMESA( Display *dpy, GLXDrawable d )
2958{
2959   XMesaBuffer b = XMesaFindBuffer(dpy, d);
2960   if (b) {
2961      XMesaDestroyBuffer(b);
2962      return True;
2963   }
2964   return False;
2965}
2966
2967
2968
2969/*** GLX_MESA_set_3dfx_mode ***/
2970
2971static Bool
2972Fake_glXSet3DfxModeMESA( int mode )
2973{
2974   return XMesaSetFXmode( mode );
2975}
2976
2977
2978
2979/*** GLX_NV_vertex_array range ***/
2980static void *
2981Fake_glXAllocateMemoryNV( GLsizei size,
2982                          GLfloat readFrequency,
2983                          GLfloat writeFrequency,
2984                          GLfloat priority )
2985{
2986   (void) size;
2987   (void) readFrequency;
2988   (void) writeFrequency;
2989   (void) priority;
2990   return NULL;
2991}
2992
2993
2994static void
2995Fake_glXFreeMemoryNV( GLvoid *pointer )
2996{
2997   (void) pointer;
2998}
2999
3000
3001/*** GLX_MESA_agp_offset ***/
3002
3003static GLuint
3004Fake_glXGetAGPOffsetMESA( const GLvoid *pointer )
3005{
3006   (void) pointer;
3007   return ~0;
3008}
3009
3010
3011/*** GLX_EXT_texture_from_pixmap ***/
3012
3013static void
3014Fake_glXBindTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer,
3015                        const int *attrib_list)
3016{
3017   XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
3018   if (b)
3019      XMesaBindTexImage(dpy, b, buffer, attrib_list);
3020}
3021
3022static void
3023Fake_glXReleaseTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer)
3024{
3025   XMesaBuffer b = XMesaFindBuffer(dpy, drawable);
3026   if (b)
3027      XMesaReleaseTexImage(dpy, b, buffer);
3028}
3029
3030
3031/* silence warning */
3032extern struct _glxapi_table *_mesa_GetGLXDispatchTable(void);
3033
3034
3035/**
3036 * Create a new GLX API dispatch table with its function pointers
3037 * initialized to point to Mesa's "fake" GLX API functions.
3038 * Note: there's a similar function (_real_GetGLXDispatchTable) that
3039 * returns a new dispatch table with all pointers initalized to point
3040 * to "real" GLX functions (which understand GLX wire protocol, etc).
3041 */
3042struct _glxapi_table *
3043_mesa_GetGLXDispatchTable(void)
3044{
3045   static struct _glxapi_table glx;
3046
3047   /* be sure our dispatch table size <= libGL's table */
3048   {
3049      GLuint size = sizeof(struct _glxapi_table) / sizeof(void *);
3050      (void) size;
3051      assert(_glxapi_get_dispatch_table_size() >= size);
3052   }
3053
3054   /* initialize the whole table to no-ops */
3055   _glxapi_set_no_op_table(&glx);
3056
3057   /* now initialize the table with the functions I implement */
3058   glx.ChooseVisual = Fake_glXChooseVisual;
3059   glx.CopyContext = Fake_glXCopyContext;
3060   glx.CreateContext = Fake_glXCreateContext;
3061   glx.CreateGLXPixmap = Fake_glXCreateGLXPixmap;
3062   glx.DestroyContext = Fake_glXDestroyContext;
3063   glx.DestroyGLXPixmap = Fake_glXDestroyGLXPixmap;
3064   glx.GetConfig = Fake_glXGetConfig;
3065   /*glx.GetCurrentContext = Fake_glXGetCurrentContext;*/
3066   /*glx.GetCurrentDrawable = Fake_glXGetCurrentDrawable;*/
3067   glx.IsDirect = Fake_glXIsDirect;
3068   glx.MakeCurrent = Fake_glXMakeCurrent;
3069   glx.QueryExtension = Fake_glXQueryExtension;
3070   glx.QueryVersion = Fake_glXQueryVersion;
3071   glx.SwapBuffers = Fake_glXSwapBuffers;
3072   glx.UseXFont = Fake_glXUseXFont;
3073   glx.WaitGL = Fake_glXWaitGL;
3074   glx.WaitX = Fake_glXWaitX;
3075
3076   /*** GLX_VERSION_1_1 ***/
3077   glx.GetClientString = Fake_glXGetClientString;
3078   glx.QueryExtensionsString = Fake_glXQueryExtensionsString;
3079   glx.QueryServerString = Fake_glXQueryServerString;
3080
3081   /*** GLX_VERSION_1_2 ***/
3082   /*glx.GetCurrentDisplay = Fake_glXGetCurrentDisplay;*/
3083
3084   /*** GLX_VERSION_1_3 ***/
3085   glx.ChooseFBConfig = Fake_glXChooseFBConfig;
3086   glx.CreateNewContext = Fake_glXCreateNewContext;
3087   glx.CreatePbuffer = Fake_glXCreatePbuffer;
3088   glx.CreatePixmap = Fake_glXCreatePixmap;
3089   glx.CreateWindow = Fake_glXCreateWindow;
3090   glx.DestroyPbuffer = Fake_glXDestroyPbuffer;
3091   glx.DestroyPixmap = Fake_glXDestroyPixmap;
3092   glx.DestroyWindow = Fake_glXDestroyWindow;
3093   /*glx.GetCurrentReadDrawable = Fake_glXGetCurrentReadDrawable;*/
3094   glx.GetFBConfigAttrib = Fake_glXGetFBConfigAttrib;
3095   glx.GetFBConfigs = Fake_glXGetFBConfigs;
3096   glx.GetSelectedEvent = Fake_glXGetSelectedEvent;
3097   glx.GetVisualFromFBConfig = Fake_glXGetVisualFromFBConfig;
3098   glx.MakeContextCurrent = Fake_glXMakeContextCurrent;
3099   glx.QueryContext = Fake_glXQueryContext;
3100   glx.QueryDrawable = Fake_glXQueryDrawable;
3101   glx.SelectEvent = Fake_glXSelectEvent;
3102
3103   /*** GLX_SGI_swap_control ***/
3104   glx.SwapIntervalSGI = Fake_glXSwapIntervalSGI;
3105
3106   /*** GLX_SGI_video_sync ***/
3107   glx.GetVideoSyncSGI = Fake_glXGetVideoSyncSGI;
3108   glx.WaitVideoSyncSGI = Fake_glXWaitVideoSyncSGI;
3109
3110   /*** GLX_SGI_make_current_read ***/
3111   glx.MakeCurrentReadSGI = Fake_glXMakeCurrentReadSGI;
3112   /*glx.GetCurrentReadDrawableSGI = Fake_glXGetCurrentReadDrawableSGI;*/
3113
3114/*** GLX_SGIX_video_source ***/
3115#if defined(_VL_H)
3116   glx.CreateGLXVideoSourceSGIX = Fake_glXCreateGLXVideoSourceSGIX;
3117   glx.DestroyGLXVideoSourceSGIX = Fake_glXDestroyGLXVideoSourceSGIX;
3118#endif
3119
3120   /*** GLX_EXT_import_context ***/
3121   glx.FreeContextEXT = Fake_glXFreeContextEXT;
3122   glx.GetContextIDEXT = Fake_glXGetContextIDEXT;
3123   /*glx.GetCurrentDisplayEXT = Fake_glXGetCurrentDisplayEXT;*/
3124   glx.ImportContextEXT = Fake_glXImportContextEXT;
3125   glx.QueryContextInfoEXT = Fake_glXQueryContextInfoEXT;
3126
3127   /*** GLX_SGIX_fbconfig ***/
3128   glx.GetFBConfigAttribSGIX = Fake_glXGetFBConfigAttribSGIX;
3129   glx.ChooseFBConfigSGIX = Fake_glXChooseFBConfigSGIX;
3130   glx.CreateGLXPixmapWithConfigSGIX = Fake_glXCreateGLXPixmapWithConfigSGIX;
3131   glx.CreateContextWithConfigSGIX = Fake_glXCreateContextWithConfigSGIX;
3132   glx.GetVisualFromFBConfigSGIX = Fake_glXGetVisualFromFBConfigSGIX;
3133   glx.GetFBConfigFromVisualSGIX = Fake_glXGetFBConfigFromVisualSGIX;
3134
3135   /*** GLX_SGIX_pbuffer ***/
3136   glx.CreateGLXPbufferSGIX = Fake_glXCreateGLXPbufferSGIX;
3137   glx.DestroyGLXPbufferSGIX = Fake_glXDestroyGLXPbufferSGIX;
3138   glx.QueryGLXPbufferSGIX = Fake_glXQueryGLXPbufferSGIX;
3139   glx.SelectEventSGIX = Fake_glXSelectEventSGIX;
3140   glx.GetSelectedEventSGIX = Fake_glXGetSelectedEventSGIX;
3141
3142   /*** GLX_SGI_cushion ***/
3143   glx.CushionSGI = Fake_glXCushionSGI;
3144
3145   /*** GLX_SGIX_video_resize ***/
3146   glx.BindChannelToWindowSGIX = Fake_glXBindChannelToWindowSGIX;
3147   glx.ChannelRectSGIX = Fake_glXChannelRectSGIX;
3148   glx.QueryChannelRectSGIX = Fake_glXQueryChannelRectSGIX;
3149   glx.QueryChannelDeltasSGIX = Fake_glXQueryChannelDeltasSGIX;
3150   glx.ChannelRectSyncSGIX = Fake_glXChannelRectSyncSGIX;
3151
3152   /*** GLX_SGIX_dmbuffer **/
3153#if defined(_DM_BUFFER_H_)
3154   glx.AssociateDMPbufferSGIX = NULL;
3155#endif
3156
3157   /*** GLX_SGIX_swap_group ***/
3158   glx.JoinSwapGroupSGIX = Fake_glXJoinSwapGroupSGIX;
3159
3160   /*** GLX_SGIX_swap_barrier ***/
3161   glx.BindSwapBarrierSGIX = Fake_glXBindSwapBarrierSGIX;
3162   glx.QueryMaxSwapBarriersSGIX = Fake_glXQueryMaxSwapBarriersSGIX;
3163
3164   /*** GLX_SUN_get_transparent_index ***/
3165   glx.GetTransparentIndexSUN = Fake_glXGetTransparentIndexSUN;
3166
3167   /*** GLX_MESA_copy_sub_buffer ***/
3168   glx.CopySubBufferMESA = Fake_glXCopySubBufferMESA;
3169
3170   /*** GLX_MESA_release_buffers ***/
3171   glx.ReleaseBuffersMESA = Fake_glXReleaseBuffersMESA;
3172
3173   /*** GLX_MESA_pixmap_colormap ***/
3174   glx.CreateGLXPixmapMESA = Fake_glXCreateGLXPixmapMESA;
3175
3176   /*** GLX_MESA_set_3dfx_mode ***/
3177   glx.Set3DfxModeMESA = Fake_glXSet3DfxModeMESA;
3178
3179   /*** GLX_NV_vertex_array_range ***/
3180   glx.AllocateMemoryNV = Fake_glXAllocateMemoryNV;
3181   glx.FreeMemoryNV = Fake_glXFreeMemoryNV;
3182
3183   /*** GLX_MESA_agp_offset ***/
3184   glx.GetAGPOffsetMESA = Fake_glXGetAGPOffsetMESA;
3185
3186   /*** GLX_EXT_texture_from_pixmap ***/
3187   glx.BindTexImageEXT = Fake_glXBindTexImageEXT;
3188   glx.ReleaseTexImageEXT = Fake_glXReleaseTexImageEXT;
3189
3190   return &glx;
3191}
3192