diff --git a/Makefile b/Makefile
index 692cfab..57420bb 100644
--- a/Makefile
+++ b/Makefile
@@ -77,6 +77,7 @@ darwin \
 darwin-static \
 darwin-static-x86ppc \
 darwin-x86ppc \
+darwin-dri \
 freebsd \
 freebsd-dri \
 freebsd-dri-amd64 \
diff --git a/bin/mklib b/bin/mklib
index 90bf834..fa6a2e0 100755
--- a/bin/mklib
+++ b/bin/mklib
@@ -107,6 +107,10 @@ do
 	-L*)
 	    DEPS="$DEPS $1"
 	    ;;
+	-framework)
+	    shift 1;
+	    DEPS="$DEPS -framework $1"
+	    ;;
 	-pthread)
 	    # this is a special case (see bugzilla 10876)
 	    DEPS="$DEPS $1"
diff --git a/configs/darwin-dri b/configs/darwin-dri
new file mode 100644
index 0000000..41106f7
--- /dev/null
+++ b/configs/darwin-dri
@@ -0,0 +1,33 @@
+# Configuration for Darwin / MacOS X, making dynamic libs
+
+include $(TOP)/configs/darwin
+
+CONFIG_NAME = darwin-dri
+
+DEFINES = -DPTHREADS -DUSE_EXTERNAL_DXTN_LIB=1 -DIN_DRI_DRIVER \
+	  -DGLX_DIRECT_RENDERING \
+	  -DHAVE_ALIAS -DGLX_ALIAS_UNSUPPORTED \
+	  -DGLX_USE_APPLEGL -Ddarwin
+
+# Compiler and flags
+AS := /System/Library/Frameworks/ApplicationServices.framework
+CFLAGS = -I/usr/X11/include -O3 -fPIC -fno-common -ffast-math -funroll-loops -fexpensive-optimizations -dynamic $(DEFINES) -F$(AS)/Frameworks
+CXXFLAGS = -I/usr/X11/include -O3 -fPIC -fno-common -ffast-math -funroll-loops -fexpensive-optimizations -dynamic $(DEFINES)
+MKDEP_OPTIONS = -I/usr/X11/include $(DEFINES)
+
+# Work around aliasing bugs - developers should comment this out
+CFLAGS += -fno-strict-aliasing
+CXXFLAGS += -fno-strict-aliasing
+
+# Library names (actual file names)
+GL_LIB_DEPS = -framework OpenGL /usr/X11/lib/libX{11,ext}.dylib -lXplugin /System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib -sub_library libGL
+GLU_LIB_DEPS = -framework OpenGL
+#GLUT_LIB_DEPS = -framework OpenGL /usr/X11/lib/libX{11,ext}.dylib -L../../../lib -lGL
+GLW_LIB_DEPS = ../../lib/libGL.dylib /usr/X11/lib/libX{11,t}.dylib
+# Major hack
+GLUT_LIB = c
+APP_LIB_DEPS = -framework GLUT -framework OpenGL /System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib 
+LIBRARY_PATH = 
+
+SRC_DIRS = glx/x11 glu glw
+
diff --git a/include/GL/internal/dri_interface.h b/include/GL/internal/dri_interface.h
index b83aff1..cfb9503 100644
--- a/include/GL/internal/dri_interface.h
+++ b/include/GL/internal/dri_interface.h
@@ -39,7 +39,9 @@
 #define DRI_INTERFACE_H
 
 #include <GL/internal/glcore.h>
+#ifndef GLX_USE_APPLEGL
 #include <drm.h>
+#endif
 
 /**
  * \name DRI interface structures
@@ -257,7 +259,11 @@ struct __DRItexBufferExtensionRec {
 
 #define __DRI_INTERFACE_VERSION 20070105
 
-typedef void *(CREATENEWSCREENFUNC)(int scr, __DRIscreen *psc,
+typedef void *(CREATENEWSCREENFUNC)(
+#ifdef GLX_USE_APPLEGL
+    Display *dpy,
+#endif
+    int scr, __DRIscreen *psc,
     const __DRIversion * ddx_version, const __DRIversion * dri_version,
     const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
     void * pSAREA, int fd, int internal_api_version,
@@ -334,6 +340,7 @@ struct __DRIinterfaceMethodsRec {
     void (*destroyContextModes)( __GLcontextModes * modes );
 
 
+#ifndef GLX_USE_APPLEGL
     /**
      * \name Client/server protocol functions.
      *
@@ -354,6 +361,7 @@ struct __DRIinterfaceMethodsRec {
         int * backX, int * backY,
         int * numBackClipRects, drm_clip_rect_t ** pBackClipRects );
     /*@}*/
+#endif
 
 
     /**
@@ -376,6 +384,7 @@ struct __DRIinterfaceMethodsRec {
 			    int32_t * numerator, int32_t * denominator);
     /*@}*/
 
+#ifndef GLX_USE_APPLEGL
     /**
      * Reports areas of the given drawable which have been modified by the
      * driver.
@@ -394,6 +403,7 @@ struct __DRIinterfaceMethodsRec {
 			 int x, int y,
 			 drm_clip_rect_t *rects, int num_rects,
 			 GLboolean front_buffer);
+#endif
 
     /**
      * Ping the windowing system to get it to reemit info for the
@@ -402,7 +412,6 @@ struct __DRIinterfaceMethodsRec {
      * \param draw the drawable for which to request info
      */
     void (*reemitDrawableInfo)(__DRIdrawable *draw);
-
 };
 
    
@@ -456,7 +465,11 @@ struct __DRIscreenRec {
     void *(*createNewDrawable)(__DRIscreen *screen,
 			       const __GLcontextModes *modes,
 			       __DRIdrawable *pdraw,
+#ifndef GLX_USE_APPLEGL
 			       drm_drawable_t hwDrawable,
+#else
+			       GLXDrawable draw,
+#endif
 			       int renderType, const int *attrs);
 
     /**
@@ -476,7 +489,10 @@ struct __DRIscreenRec {
 			       const __GLcontextModes *modes,
 			       int render_type,
 			       __DRIcontext *shared,
-			       drm_context_t hwContext, __DRIcontext *pctx);
+#ifndef GLX_USE_APPLEGL
+			       drm_context_t hwContext,
+#endif
+			       __DRIcontext *pctx);
 };
 
 /**
diff --git a/src/glx/x11/Makefile b/src/glx/x11/Makefile
index 00a568c..c71e543 100644
--- a/src/glx/x11/Makefile
+++ b/src/glx/x11/Makefile
@@ -32,6 +32,9 @@ SOURCES = \
 	  glx_texture_compression.c \
 	  dri_glx.c \
 	  XF86dri.c \
+	  appledri.c \
+	  apple_driver.c \
+	  apple_dispatch.c \
 	  glxhash.c
 
 include $(TOP)/src/mesa/sources
@@ -89,4 +92,8 @@ clean:
 	-rm -f *.o *~
 	-rm -f depend
 
+apple_dispatch.h: ../../mesa/glapi/glapitable.h apple_dispatch.pl
+	./apple_dispatch.pl
+	mv -f apple_dispatch.h.tmp apple_dispatch.h
+
 include depend
diff --git a/src/glx/x11/XF86dri.c b/src/glx/x11/XF86dri.c
index 5b0bf14..abe226a 100644
--- a/src/glx/x11/XF86dri.c
+++ b/src/glx/x11/XF86dri.c
@@ -37,7 +37,7 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 /* THIS IS NOT AN X CONSORTIUM STANDARD */
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 
 #define NEED_REPLIES
 #include <X11/Xlibint.h>
@@ -641,4 +641,4 @@ PUBLIC Bool XF86DRICloseFullScreen(dpy, screen, drawable)
     return True;
 }
 
-#endif /* GLX_DIRECT_RENDERING */
+#endif /* GLX_DIRECT_RENDERING && !GLX_USE_APPLEGL */
diff --git a/src/glx/x11/apple_dispatch.c b/src/glx/x11/apple_dispatch.c
new file mode 100644
index 0000000..ec06c59
--- /dev/null
+++ b/src/glx/x11/apple_dispatch.c
@@ -0,0 +1,146 @@
+/* dri_dispatch.c
+   $Id: dri_dispatch.c,v 1.14 2004/07/16 19:09:24 jharper Exp $
+
+   Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
+
+   Permission is hereby granted, free of charge, to any person
+   obtaining a copy of this software and associated documentation files
+   (the "Software"), to deal in the Software without restriction,
+   including without limitation the rights to use, copy, modify, merge,
+   publish, distribute, sublicense, and/or sell copies of the Software,
+   and to permit persons to whom the Software is furnished to do so,
+   subject to the following conditions:
+
+   The above copyright notice and this permission notice shall be
+   included in all copies or substantial portions of the Software.
+
+   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+   NONINFRINGEMENT.  IN NO EVENT SHALL THE THE ABOVE LISTED COPYRIGHT
+   HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+   DEALINGS IN THE SOFTWARE.
+
+   Except as contained in this notice, the name(s) of the above
+   copyright holders shall not be used in advertising or otherwise to
+   promote the sale, use or other dealings in this Software without
+   prior written authorization. */
+/* $XFree86: xc/lib/GL/apple/dri_dispatch.c,v 1.3 2003/10/31 02:22:12 torrey Exp $ */
+
+#include <OpenGL/OpenGL.h>
+#include <OpenGL/CGLContext.h>
+
+#include "glapi.h"
+#include "glapitable.h"
+#include "glxclient.h"
+
+#include <X11/Xlibint.h>
+#include <stdio.h>
+
+#ifdef __GNUC__
+# define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
+#else
+# define GCC_VERSION 0
+#endif
+
+#if GCC_VERSION < 3000
+# define __builtin_expect(a, b) a
+#endif
+
+static int indirect_noop (void)
+{
+    return 0;
+}
+
+/* Macro used for gl functions that exist in OpenGL.framework. We'll
+   use the existing stub for the initial dispatch, but need this
+   function to handle indirect contexts. */
+#define DEFUN_LOCAL_1(ret, return, gn, proto, args)		\
+static ret indirect__ ## gn proto				\
+{								\
+    struct _glapi_table *disp;					\
+								\
+    disp = _glapi_Dispatch;					\
+    if (__builtin_expect (disp == NULL, 0))			\
+	disp = _glapi_get_dispatch ();				\
+								\
+    return (*disp->gn) args;					\
+}
+
+/* Macro used for gl functions that don't exist in OpenGL.framework.
+   We drop them on the floor in direct rendering mode, but pass them
+   over the wire normally for indirect contexts. */
+#define DEFUN_EXTERN_1(ret, return, gn, proto, args)		\
+ret gl ## gn proto						\
+{								\
+    __GLXcontext *gc;						\
+    struct _glapi_table *disp;					\
+								\
+    gc = __glXGetCurrentContext ();				\
+    if (!gc->isDirect) {					\
+	disp = _glapi_Dispatch;					\
+	if (__builtin_expect (disp == NULL, 0))			\
+	    disp = _glapi_get_dispatch ();			\
+								\
+	return (*disp->gn) args;				\
+    } else {							\
+	int a; return a = 0;					\
+    }								\
+}
+
+/* Macro for functions that already exist, but with a different name. */
+#define DEFUN_ALIAS_1(ret, return, gn, on, proto, args)	\
+ret gl ## gn proto					\
+{							\
+    return gl ## on args;				\
+}
+
+#define DEFUN_LOCAL(r, gn, p, a) \
+    DEFUN_LOCAL_1 (r, return, gn, p, a)
+#define DEFUN_LOCAL_VOID(gn, p, a) \
+    DEFUN_LOCAL_1 (void, , gn, p, a)
+
+#define DEFUN_EXTERN(r, gn, p, a) \
+    DEFUN_EXTERN_1 (r, return, gn, p, a)
+#define DEFUN_EXTERN_VOID(gn, p, a) \
+    DEFUN_EXTERN_1 (void, , gn, p, a)
+
+#define DEFUN_ALIAS(r, gn, on, p, a) \
+    DEFUN_ALIAS_1 (r, return, gn, on, p, a)
+#define DEFUN_ALIAS_VOID(gn, on, p, a) \
+    DEFUN_ALIAS_1 (void, , gn, on, p, a)
+
+#include "apple_dispatch.h"
+
+#ifdef GLX_INDIRECT_RENDERING
+__private_extern__ CGLContextObj
+XAppleDRIGetIndirectContext (void)
+{
+    static CGLContextObj ctx;
+    void **t;
+    int i;
+
+    if (ctx != NULL)
+        return ctx;
+
+    /* initialize gl */
+    CGLSetOption (kCGLGOResetLibrary, 0);
+
+    /* Create an empty "context" for dispatching purposes. Add some slop
+       in case the dispatch table grows in future updates. */
+    ctx = Xcalloc (1, sizeof (struct _CGLContextObject) + 1024);
+
+    /* fill it with no-op vectors */
+    t = (void **) &ctx->disp;
+    for (i = 0; i < (int) (sizeof (ctx->disp) / sizeof (t[0])); i++)
+        t[i] = &indirect_noop;
+
+    /* then install the functions we actually support */
+    INDIRECT_DISPATCH_INIT (((void **) (&ctx->disp)), indirect__);
+
+    return ctx;
+}
+#endif
+
diff --git a/src/glx/x11/apple_dispatch.pl b/src/glx/x11/apple_dispatch.pl
new file mode 100755
index 0000000..1a384e6
--- /dev/null
+++ b/src/glx/x11/apple_dispatch.pl
@@ -0,0 +1,141 @@
+#!/usr/bin/perl -w
+
+my $glapitable = "../../mesa/glapi/glapitable.h";
+my $gllib = "/System/Library/Frameworks/OpenGL.framework/Libraries/libGL.dylib";
+my $outfile = "apple_dispatch.h.tmp";
+
+my %symbols;
+my %extensions;
+my @functions;
+my %passedfuns;
+
+my %skipme = (
+		"GetQueryObjecti64vEXT" => 1,
+		"GetQueryObjectui64vEXT" => 1
+	     );
+my %knownfalse = (
+		"FlushVertexArrayRangeNV" => "FlushVertexArrayRangeAPPLE",
+		"SetFenceNV" => "SetFenceAPPLE",
+		"VertexAttribPointerNV" => "VertexAttribPointer"
+		);
+
+open GLSYMS, "-|", "nm $gllib";
+while (<GLSYMS>) {
+	/^[0-9a-z]+ [tT] _gl(.*)$/ or next;
+
+	my $sym = $1;
+
+	if ($sym =~ /([A-Z]{2,})$/) {
+		my $ext = $1;
+
+		print "symbol $sym has new extension $ext\n" if !$extensions{$ext};
+		$extensions{$ext} = 1;
+	}
+
+	$symbols{$sym} = 1;
+}
+close GLSYMS;
+
+my $extre = '^(.*?)(' . join('|', keys(%extensions)) . ')$';
+open GLTABLE, "<", $glapitable;
+open OUT, ">", $outfile;
+print OUT <<EOT
+/* Automatically generated, do not edit */
+
+EOT
+;
+while (<GLTABLE>) {
+	m~^[[:space:]]*((const )?[A-Za-z0-9_]+( \*+)?) \(GLAPIENTRYP ([A-Za-z0-9_]+)\)(\(.*\));[[:space:]]+/\* ([0-9]+) \*/~ or next;
+
+	my $rettype = $1;
+	my $fun = $4;
+	my $args = $5;
+	my $idx = $6;
+	my $found;
+
+	if ($fun =~ /([A-Z]{2,})$/) {
+		my $ext = $1;
+
+		print "function $fun has new extension $ext\n" if !$extensions{$ext};
+		$extensions{$ext} = 1;
+	}
+
+	next if $skipme{$fun};
+
+	if ($symbols{$fun}) {
+		$found = $fun;
+	} else {
+		my $sfun = $fun;
+		
+		$sfun = $1 if $sfun =~ /$extre/;
+
+		if ($symbols{$sfun}) {
+			$found = $sfun;
+		} else {
+			foreach $e (keys %extensions) {
+				if ($symbols{"$sfun$e"}) {
+					$found = "$sfun$e";
+					last;
+				}
+			}
+		}
+	}
+
+	$found = undef if ($found && $knownfalse{$fun} && $knownfalse{$fun} eq $found);
+
+	if ($found && $found ne $fun && $passedfuns{$found}) {
+		# Check for false friend
+		my ($passedrettype, $passedargs) = @{$passedfuns{$found}};
+
+		$found = undef if ($passedrettype ne $rettype || $passedargs ne $args);
+	}
+
+	if ($found && $found eq $fun) {
+		if ($rettype eq "void") {
+			print OUT "DEFUN_LOCAL_VOID ($fun,\n";
+		} else {
+			print OUT "DEFUN_LOCAL ($rettype, $fun,\n";
+		}
+	} elsif ($found) {
+		print "function $fun found as $found\n" if $found ne $fun;
+		if ($rettype eq "void") {
+			print OUT "DEFUN_ALIAS_VOID ($fun, $found,\n";
+		} else {
+			print OUT "DEFUN_ALIAS ($rettype, $fun, $found,\n";
+		}
+	} else {
+		print "function $fun not found.\n";
+		if ($rettype eq "void") {
+			print OUT "DEFUN_EXTERN_VOID ($fun,\n";
+		} else {
+			print OUT "DEFUN_EXTERN ($rettype, $fun,\n";
+		}
+	}
+	print OUT "   $args,\n";
+	if ($args eq "(void)") {
+		print OUT "    ())\n";
+	} else {
+		my $argnames = $args;
+		$argnames =~ s/([(,]) ?(const )?[A-Za-z0-9_]+ ((const )?\*+ )*/\1 /g;
+		print OUT "   $argnames)\n\n";
+	}
+
+	$functions[$idx] = $fun;
+	$passedfuns{$fun} = [$rettype, $args];
+}
+close GLTABLE;
+
+# XXX The indexes are incorrect, supposed to be CGL dispatch table indexes I believe.
+#print OUT <<EOT
+##define INDIRECT_DISPATCH_INIT(d,p) \\
+#do { \\
+#EOT
+#;
+#for my $i (0 .. $#functions) {
+#    print OUT "    (d)[$i] = (void *) &p ## $functions[$i]; \\\n";
+#}
+#print OUT <<EOT
+#} while (0)
+#EOT
+#;
+
diff --git a/src/glx/x11/apple_driver.c b/src/glx/x11/apple_driver.c
new file mode 100644
index 0000000..05e6333
--- /dev/null
+++ b/src/glx/x11/apple_driver.c
@@ -0,0 +1,887 @@
+	/* $XFree86: xc/lib/GL/mesa/dri/dri_mesa.c,v 1.17 2001/08/27 17:40:57 dawes Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright (c) 2002 Apple Computer, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * Authors:
+ *   Kevin E. Martin <kevin@precisioninsight.com>
+ *   Brian E. Paul <brian@precisioninsight.com>
+ */
+
+/*
+ * This file gets compiled into each of the DRI 3D drivers.  The
+ * functions defined here are called from the GL library via
+ * function pointers in the __DRIdisplayRec, __DRIscreenRec,
+ * __DRIcontextRec, __DRIdrawableRec structures defined in glxclient.h
+ *
+ * Those function pointers are initialized by code in this file.
+ * The process starts when libGL calls the __driCreateScreen() function
+ * at the end of this file.
+ *
+ * The above-mentioned DRI structures have no dependencies on Mesa.
+ * Each structure instead has a generic (void *) private pointer that
+ * points to a private structure.  For Mesa drivers, these private
+ * structures are the __DRIdrawablePrivateRec, __DRIcontextPrivateRec,
+ * __DRIscreenPrivateRec, and __DRIvisualPrivateRec structures defined
+ * in dri_mesaint.h.  We allocate and attach those structs here in
+ * this file.
+ */
+
+
+
+#ifdef GLX_USE_APPLEGL
+
+/* Need to include these first to ensure we pick up the correct gl.h
+   and glext.h (from the framework) */
+#include <OpenGL/OpenGL.h>
+#include <OpenGL/CGLContext.h>
+
+#include <unistd.h>
+#include <X11/Xlibint.h>
+#include <X11/extensions/Xext.h>
+#include <X11/extensions/extutil.h>
+#include "glxclient.h"
+#include "appledri.h"
+#include "apple_driver.h"
+
+/* Context binding */
+static GLboolean driMesaBindContext(__DRIcontext *ctx,
+				    __DRIdrawable *pdraw,
+				    __DRIdrawable *pread);
+static GLboolean driMesaUnbindContext(__DRIcontext *ctx);
+
+/* Drawable methods */
+static void *driMesaCreateDrawable (__DRIscreen *screen,
+			       const __GLcontextModes *modes,
+			       __DRIdrawable *pdraw,
+			       GLXDrawable draw,
+			       int renderType, const int *attrs);
+static void driMesaSwapBuffers(__DRIdrawable *pdraw);
+static void driMesaDestroyDrawable(__DRIdrawable *drawPrivate);
+
+/* Context methods */
+static void *driMesaCreateContext(__DRIscreen *screen,
+			       const __GLcontextModes *modes,
+			       int render_type,
+			       __DRIcontext *shared,
+			       __DRIcontext *pctx);
+static void driMesaDestroyContext(__DRIcontext *pctx);
+
+/* Screen methods */
+static void *driMesaCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
+				 __GLcontextModes **modes);
+static void driMesaDestroyScreen(__DRIscreen *screen);
+
+static void driMesaCreateSurface(__DRIscreenPrivate *psp,
+				 __DRIdrawablePrivate *pdp);
+
+static void unwrap_context (__DRIcontextPrivate *pcp);
+static void wrap_context (__DRIcontextPrivate *pcp);
+
+#ifdef GLX_INDIRECT_RENDERING
+extern CGLContextObj XAppleDRIGetIndirectContext (void);
+#endif
+
+/*****************************************************************/
+
+static Bool __driMesaWindowExistsFlag;
+
+static int __driMesaWindowExistsErrorHandler(Display *dpy, XErrorEvent *xerr)
+{
+    if (xerr->error_code == BadWindow) {
+	__driMesaWindowExistsFlag = GL_FALSE;
+    }
+    return 0;
+}
+
+static Bool __driMesaWindowExists(Display *dpy, GLXDrawable draw)
+{
+    XWindowAttributes xwa;
+    int (*oldXErrorHandler)(Display *, XErrorEvent *);
+
+    __driMesaWindowExistsFlag = GL_TRUE;
+    oldXErrorHandler = XSetErrorHandler(__driMesaWindowExistsErrorHandler);
+    XGetWindowAttributes(dpy, draw, &xwa); /* dummy request */
+    XSetErrorHandler(oldXErrorHandler);
+    return __driMesaWindowExistsFlag;
+}
+
+/*****************************************************************/
+
+/* returns with psp->mutex locked if successful. */
+static Bool
+driMesaFindDrawableByUID (Display *dpy,unsigned int uid,
+			  __DRIscreenPrivate **psp_ret,
+			  __DRIdrawablePrivate **pdp_ret)
+{
+    __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
+    int scrn;
+    __GLXdrawable *pdraw;
+    GLXDrawable drawable;
+
+    if (priv == NULL || priv->driDisplay.private == NULL)
+	return False;
+
+    for (scrn = 0; scrn < ScreenCount (dpy); scrn++) {
+	__GLXscreenConfigs *sc = &priv->screenConfigs[scrn];
+	__DRIscreenPrivate *psp = sc->driScreen.private;
+
+	pthread_mutex_lock (&psp->mutex);
+        if (__glxHashFirst(sc->drawHash, &drawable, (void**)&pdraw)) {
+	    do {
+		__DRIdrawablePrivate *pdp = pdraw->driDrawable.private;
+
+		if (pdp->uid == uid) {
+			pthread_mutex_unlock (&psp->mutex);
+			*psp_ret = psp;
+			*pdp_ret = pdp;
+			return True;
+		}
+	    } while (__glxHashNext(sc->drawHash, &drawable, (void**)&pdraw));
+	}
+	pthread_mutex_unlock (&psp->mutex);
+    }
+    return False;
+}
+
+static void
+unbind_context (__DRIcontextPrivate *pcp)
+{
+    /* Unbind the context from its old drawable. */
+
+    if (pcp->driDrawablePriv != NULL)
+    {
+	if (pcp->next != NULL)
+	    pcp->next->prev = pcp->prev;
+	if (pcp->prev != NULL)
+	    pcp->prev->next = pcp->next;
+
+	if (pcp->driDrawablePriv->driContextPriv == pcp)
+	    pcp->driDrawablePriv->driContextPriv = pcp->next;
+
+	pcp->driDrawablePriv = NULL;
+	pcp->prev = pcp->next = NULL;
+    }
+
+    if (pcp->surface_id != 0)
+    {
+	pcp->surface_id = 0;
+	pcp->pending_clear = TRUE;
+    }
+}
+
+static void
+unbind_drawable (__DRIdrawablePrivate *pdp)
+{
+    __DRIcontextPrivate *pcp, *next;
+
+    for (pcp = pdp->driContextPriv; pcp != NULL; pcp = next)
+    {
+	next = pcp->next;
+	unbind_context (pcp);
+    }
+}
+
+static void
+update_context (__DRIcontextPrivate *pcp)
+{
+    if (pcp->pending_clear)
+    {
+	CGLClearDrawable (pcp->ctx);
+	pcp->pending_clear = FALSE;
+    }
+
+    if (pcp->pending_update && pcp->surface_id != 0)
+    {
+	xp_update_gl_context (pcp->ctx);
+	pcp->pending_update = FALSE;
+    }
+}
+
+static GLboolean driMesaUnbindContext(__DRIcontext *ctx)
+{
+    __DRIscreen *pDRIScreen;
+//  __DRIdrawable *pdraw;
+    __DRIcontextPrivate *pcp;
+    __DRIscreenPrivate *psp;
+    __DRIdrawablePrivate *pdp;
+
+    /*
+    ** Assume error checking is done properly in glXMakeCurrent before
+    ** calling driMesaUnbindContext.
+    */
+
+    if (ctx == NULL) {
+	/* ERROR!!! */
+	return GL_FALSE;
+    }
+
+    pcp = ctx->private;
+    psp = pcp->driScreenPriv;
+
+    pthread_mutex_lock (&psp->mutex);
+
+    pdp = pcp->driDrawablePriv;
+    if (pdp == NULL) {
+	/* ERROR!!! */
+	pthread_mutex_unlock (&psp->mutex);
+	return GL_FALSE;
+    }
+
+#ifdef GLX_INDIRECT_RENDERING
+    /* Put this thread back into normal (indirect) dispatch mode. */
+    CGLSetCurrentContext (XAppleDRIGetIndirectContext ());
+#endif
+    pcp->thread_id = 0;
+
+    /* Lazily unbind the drawable from the context */
+    unbind_context (pcp);
+
+    if (pdp->refcount == 0) {
+	/* ERROR!!! */
+	pthread_mutex_unlock (&psp->mutex);
+	return GL_FALSE;
+    } else if (--pdp->refcount == 0) {
+#if 0
+	/*
+	** NOT_DONE: When a drawable is unbound from one direct
+	** rendering context and then bound to another, we do not want
+	** to destroy the drawable data structure each time only to
+	** recreate it immediatly afterwards when binding to the next
+	** context.  This also causes conflicts with caching of the
+	** drawable stamp.
+	**
+	** In addition, we don't destroy the drawable here since Mesa
+	** keeps private data internally (e.g., software accumulation
+	** buffers) that should not be destroyed unless the client
+	** explicitly requests that the window be destroyed.
+	**
+	** When GLX 1.3 is integrated, the create and destroy drawable
+	** functions will have user level counterparts and the memory
+	** will be able to be recovered.
+	** 
+	** Below is an example of what needs to go into the destroy
+	** drawable routine to support GLX 1.3.
+	*/
+	__driMesaRemoveDrawable(psp->drawHash, pdraw);
+	(*pdraw->destroyDrawable)(dpy, pdraw->private);
+	Xfree(pdraw);
+#endif
+    }
+
+    pthread_mutex_unlock (&psp->mutex);
+    return GL_TRUE;
+}
+
+static GLboolean driMesaBindContext(__DRIcontext *ctx,
+				    __DRIdrawable *pdraw,
+				    __DRIdrawable *pread)
+{
+    __DRIdrawablePrivate *pdp;
+    __DRIscreenPrivate *psp;
+    __DRIcontextPrivate *pcp = ctx->private;
+
+    /*
+    ** Assume error checking is done properly in glXMakeCurrent before
+    ** calling driMesaBindContext.
+    */
+    if (pdraw == NULL || pread == NULL)
+	    return GL_FALSE;
+
+    psp = pcp->driScreenPriv;
+
+    pthread_mutex_lock (&psp->mutex);
+
+    pdp = (__DRIdrawablePrivate *)pdraw->private;
+
+    if (pdp->surface_id == 0)
+    {
+	/* Surface got destroyed. Try to create a new one. */
+
+	driMesaCreateSurface (psp, pdp);
+    }
+
+    unbind_context (pcp);
+
+    /* Bind the drawable to the context */
+    pcp->driDrawablePriv = pdp;
+    pcp->prev = NULL;
+    pcp->next = pdp->driContextPriv;
+    pdp->driContextPriv = pcp;
+    pdp->refcount++;
+
+    /* And the physical surface to the physical context */
+    if (pcp->surface_id != pdp->surface_id)
+    {
+	pcp->surface_id = 0;
+
+	/* Attaching the drawable sets the default viewport. But we don't
+	   want to catch that call to glViewport in our wrappers. */
+	unwrap_context (pcp);
+
+	if (pdp->surface_id == 0)
+	    CGLClearDrawable (pcp->ctx);
+	else if (xp_attach_gl_context (pcp->ctx, pdp->surface_id) == Success)
+	    pcp->surface_id = pdp->surface_id;
+	else
+	    fprintf (stderr, "failed to bind to surface\n");
+
+	wrap_context (pcp);
+
+	pcp->pending_clear = FALSE;
+	pcp->pending_update = FALSE;
+    }
+    else if (pcp->pending_clear)
+    {
+	CGLClearDrawable (pcp->ctx);
+	pcp->pending_clear = FALSE;
+    }
+
+    /* Activate the CGL context and remember which thread it's current for. */
+    CGLSetCurrentContext (pcp->ctx);
+    pcp->thread_id = xthread_self ();
+
+    pthread_mutex_unlock (&psp->mutex);
+    return GL_TRUE;
+}
+
+/*****************************************************************/
+
+static xp_client_id
+get_client_id (void)
+{
+    static xp_client_id id;
+
+    if (id == 0)
+    {
+	if (xp_init (XP_IN_BACKGROUND) != Success
+	    || xp_get_client_id (&id) != Success)
+	{
+	    return 0;
+	}
+    }
+
+    return id;
+}
+
+static void driMesaCreateSurface(__DRIscreenPrivate *psp,
+				 __DRIdrawablePrivate *pdp)
+{
+    xp_client_id client_id;
+    unsigned int key[2];
+
+    pdp->surface_id = 0;
+    pdp->uid = 0;
+
+    client_id = get_client_id ();
+    if (client_id == 0)
+	return;
+
+    if (XAppleDRICreateSurface (psp->display,
+			        psp->myNum, pdp->draw,
+				client_id, key, &pdp->uid))
+    {
+	xp_import_surface (key, &pdp->surface_id);
+    }
+}
+
+static void *driMesaCreateDrawable (__DRIscreen *screen,
+			       const __GLcontextModes *modes,
+			       __DRIdrawable *pdraw,
+			       GLXDrawable draw,
+			       int renderType, const int *attrs)
+{
+    __DRIscreenPrivate *psp;
+    __DRIdrawablePrivate *pdp;
+
+    pdp = (__DRIdrawablePrivate *)Xmalloc(sizeof(__DRIdrawablePrivate));
+    if (!pdp) {
+	return NULL;
+    }
+
+    pdp->draw = draw;
+    pdp->refcount = 0;
+    pdp->surface_id = 0;
+    pdp->uid = 0;
+    pdp->destroyed = FALSE;
+
+    psp = screen->private;
+    pdp->driScreenPriv = psp;
+    pdp->driContextPriv = NULL;
+
+    driMesaCreateSurface (psp, pdp);
+    if (pdp->surface_id == 0) {
+	Xfree (pdp);
+	return NULL;
+    }
+
+    pdraw->destroyDrawable = driMesaDestroyDrawable;
+    pdraw->swapBuffers = driMesaSwapBuffers;
+
+    return (void *)pdp;
+}
+
+static void driMesaSwapBuffers(__DRIdrawable *pdraw)
+{
+    __DRIdrawablePrivate *pdp = pdraw->private;
+    __DRIcontextPrivate *pcp;
+    xthread_t self = xthread_self ();
+    static Bool warned;
+
+    pthread_mutex_lock (&pdp->driScreenPriv->mutex);
+
+    /* FIXME: this is sub-optimal, since we may not always find a context
+       bound to the given drawable on this thread. */
+
+    for (pcp = pdp->driContextPriv; pcp != NULL; pcp = pcp->next)
+    {
+	if (pcp->thread_id == self || pcp->thread_id == 0)
+	    break;
+    }
+
+    if (pcp != NULL)
+    {
+	CGLFlushDrawable (pcp->ctx);
+    }
+    else
+    {
+	if (!warned) {
+	    fprintf (stderr, "glXSwapBuffers: no context for this drawable\n");
+	    warned = TRUE;
+	}
+    }
+
+    pthread_mutex_unlock (&pdp->driScreenPriv->mutex);
+}
+
+/* psp->mutex is held. */
+static void driMesaDestroyDrawable(__DRIdrawable *pdraw)
+{
+    __DRIdrawablePrivate *pdp = pdraw->private;
+
+    if (pdp) {
+	unbind_drawable (pdp);
+	if (pdp->surface_id != 0) {
+	    xp_destroy_surface (pdp->surface_id);
+	    pdp->surface_id = 0;
+	}
+	if (!pdp->destroyed) {
+	    /* don't try to destroy an already destroyed surface. */
+	    XAppleDRIDestroySurface(pdp->driScreenPriv->display, pdp->driScreenPriv->myNum, pdp->draw);
+	}
+	Xfree(pdp);
+    }
+}
+
+/*****************************************************************/
+
+static CGLPixelFormatObj
+driCreatePixelFormat (Display *dpy, __DRIscreenPrivate *psp,
+		      __GLcontextModes *mode)
+{
+    int i;
+    CGLPixelFormatAttribute attr[64]; // currently uses max of 30
+    CGLPixelFormatObj result;
+    GLint n_formats;
+
+    i = 0;
+
+    if (!mode->rgbMode)
+	return NULL;
+
+    if (mode->stereoMode)
+	attr[i++] = kCGLPFAStereo;
+
+    if (mode->doubleBufferMode)
+	attr[i++] = kCGLPFADoubleBuffer;
+
+    attr[i++] = kCGLPFAColorSize;
+    attr[i++] = mode->redBits + mode->greenBits + mode->blueBits;
+    attr[i++] = kCGLPFAAlphaSize;
+    attr[i++] = mode->alphaBits > 1 ? mode->alphaBits : 1;
+
+    if (mode->accumRedBits + mode->accumGreenBits
+	+ mode->accumBlueBits + mode->accumAlphaBits > 0)
+    {
+	attr[i++] = kCGLPFAAccumSize;
+	attr[i++] = (mode->accumRedBits + mode->accumGreenBits +
+			mode->accumBlueBits + mode->accumAlphaBits);
+    }
+
+    if (mode->depthBits > 0) {
+	attr[i++] = kCGLPFADepthSize;
+	attr[i++] = mode->depthBits;
+    }
+
+    if (mode->stencilBits > 0) {
+	attr[i++] = kCGLPFAStencilSize;
+	attr[i++] = mode->stencilBits;
+    }
+
+    if (mode->numAuxBuffers > 0) {
+	attr[i++] = kCGLPFAAuxBuffers;
+	attr[i++] = mode->numAuxBuffers;
+    }
+
+    /* FIXME: things we don't handle: color/alpha masks, level,
+       visualrating, transparentFoo */
+
+    attr[i++] = 0;
+
+    result = NULL;
+    CGLChoosePixelFormat(attr, &result, &n_formats);
+
+    return result;
+}
+
+static void *driMesaCreateContext(__DRIscreen *screen,
+			       const __GLcontextModes *modes,
+			       int render_type,
+			       __DRIcontext *shared,
+			       __DRIcontext *pctx)
+{
+    __DRIscreen *pDRIScreen;
+    __DRIcontextPrivate *pcp;
+    __DRIcontextPrivate *pshare = (__DRIcontextPrivate *)shared;
+    __DRIscreenPrivate *psp;
+    int i;
+
+    psp = screen->private;
+
+    pcp = (__DRIcontextPrivate *)Xmalloc(sizeof(__DRIcontextPrivate));
+    if (!pcp) {
+	return NULL;
+    }
+
+    pcp->display = psp->display;
+    pcp->driScreenPriv = psp;
+    pcp->driDrawablePriv = NULL;
+
+    pcp->ctx = NULL;
+    pcp->surface_id = 0;
+
+    pcp->pending_clear = FALSE;
+    pcp->pending_update = FALSE;
+
+    pcp->ctx = NULL;
+    for (i = 0; pcp->ctx == NULL && i < psp->numVisuals; i++) {
+        if (psp->visuals[i].vid == modes->visualID) {
+	    CGLCreateContext (psp->visuals[i].pixel_format,
+			      pshare ? pshare->ctx : NULL, &pcp->ctx);
+        }
+    }
+
+    if (!pcp->ctx) {
+	Xfree(pcp);
+	return NULL;
+    }
+
+    pctx->destroyContext = driMesaDestroyContext;
+    pctx->bindContext    = driMesaBindContext;
+    pctx->unbindContext  = driMesaUnbindContext;
+
+    wrap_context (pcp);
+
+    return pcp;
+}
+
+static void driMesaDestroyContext(__DRIcontext *pctx)
+{
+    __DRIcontextPrivate  *pcp   = pctx->private;
+
+    if (pcp) {
+	pthread_mutex_lock (&pcp->driScreenPriv->mutex);
+	unbind_context (pcp);
+	pthread_mutex_unlock (&pcp->driScreenPriv->mutex);
+	CGLDestroyContext (pcp->ctx);
+	Xfree(pcp);
+    }
+}
+
+/*****************************************************************/
+
+static void *driMesaCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
+				 __GLcontextModes **modes)
+{
+    int directCapable, i, n;
+    __DRIscreenPrivate *psp;
+    XVisualInfo visTmpl, *visinfo;
+    pthread_mutexattr_t attr;
+    int num_modes;
+    __GLcontextModes *m;
+
+    if (!XAppleDRIQueryDirectRenderingCapable(dpy, scrn, &directCapable)) {
+	return NULL;
+    }
+
+    if (!directCapable) {
+	return NULL;
+    }
+
+    psp = (__DRIscreenPrivate *)Xmalloc(sizeof(__DRIscreenPrivate));
+    if (!psp) {
+	return NULL;
+    }
+
+    /* Need a recursive lock here. */
+    pthread_mutexattr_init (&attr);
+    pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
+    pthread_mutex_init (&psp->mutex, &attr);
+    pthread_mutexattr_destroy (&attr);
+
+    psp->display = dpy;
+    psp->myNum = scrn;
+
+#if 0
+    if (!XAppleDRIAuthConnection(dpy, scrn, magic)) {
+	Xfree(psp);
+	(void)XAppleDRICloseConnection(dpy, scrn);
+	return NULL;
+    }
+#endif
+
+    /*
+     * Allocate space for an array of visual records and initialize them.
+     */
+    for (m = *modes, num_modes = 0; m; m = m->next)
+	    num_modes++;
+    psp->visuals = (__DRIvisualPrivate *)Xmalloc(num_modes *
+						 sizeof(__DRIvisualPrivate));
+    if (!psp->visuals) {
+	Xfree(psp);
+	return NULL;
+    }
+
+    visTmpl.screen = scrn;
+    visinfo = XGetVisualInfo(dpy, VisualScreenMask, &visTmpl, &n);
+    if (n != num_modes) {
+	Xfree(psp);
+	return NULL;
+    }
+
+    psp->numVisuals = 0;
+    for (i = 0, m = * modes; i < num_modes; i++, m = m->next) {
+	psp->visuals[psp->numVisuals].vid = visinfo[i].visualid;
+        psp->visuals[psp->numVisuals].pixel_format =
+                 driCreatePixelFormat (dpy, psp, m);
+	if (psp->visuals[psp->numVisuals].pixel_format != NULL) {
+	    psp->numVisuals++;
+	}
+    }
+
+    XFree(visinfo);
+
+    if (psp->numVisuals == 0) {
+	/* Couldn't create any pixel formats. */
+	Xfree(psp->visuals);
+	Xfree(psp);
+	return NULL;
+    }
+
+    psc->destroyScreen  = driMesaDestroyScreen;
+    psc->createNewContext  = driMesaCreateContext;
+    psc->createNewDrawable = driMesaCreateDrawable;
+
+    return (void *)psp;
+}
+
+static void driMesaDestroyScreen(__DRIscreen *screen)
+{
+    __DRIscreenPrivate *psp = screen->private;
+
+    if (psp) {
+	//FIXME resetDriver ?
+	Xfree(psp->visuals);
+	Xfree(psp);
+    }
+}
+
+/* Note: definitely can't make any X protocol requests here. */
+static void driAppleSurfaceNotify (Display *dpy, unsigned int uid, int kind)
+{
+    __DRIscreenPrivate *psp;
+    __DRIdrawablePrivate *pdp;
+    __DRIcontextPrivate *pcp;
+
+    /* locks psp->mutex if successful. */
+    if (driMesaFindDrawableByUID (dpy, uid, &psp, &pdp))
+    {
+	xthread_t self = xthread_self ();
+
+	switch (kind)
+	{
+	    Bool all_safe;
+
+	case AppleDRISurfaceNotifyDestroyed:
+	    xp_destroy_surface (pdp->surface_id);
+	    pdp->surface_id = 0;
+
+	    for (pcp = pdp->driContextPriv; pcp != NULL; pcp = pcp->next)
+	    {
+		pcp->surface_id = 0;
+
+		if (pcp->thread_id == self || pcp->thread_id == 0) {
+		    CGLClearDrawable (pcp->ctx);
+		    pcp->pending_clear = FALSE;
+		} else
+		    pcp->pending_clear = TRUE;
+	    }
+	    break;
+
+	case AppleDRISurfaceNotifyChanged:
+	    all_safe = TRUE;
+	    for (pcp = pdp->driContextPriv; pcp != NULL; pcp = pcp->next)
+	    {
+		if (pcp->thread_id != 0 && pcp->thread_id != self) {
+		    all_safe = FALSE;
+		    break;
+		}
+	    }
+	    for (pcp = pdp->driContextPriv; pcp != NULL; pcp = pcp->next)
+	    {
+		if (all_safe) {
+		    xp_update_gl_context (pcp->ctx);
+		    pcp->pending_update = FALSE;
+		} else
+		    pcp->pending_update = TRUE;
+	    }
+	    break;
+	}
+
+	pthread_mutex_unlock (&psp->mutex);
+    }
+}
+
+void __driRegisterExtensions (void)
+{
+}
+
+#ifdef GLX_INDIRECT_RENDERING
+__private_extern__ void XAppleDRIUseIndirectDispatch (void)
+{
+    CGLSetCurrentContext (XAppleDRIGetIndirectContext ());
+}
+#endif
+
+/*****************************************************************/
+
+/*
+ * Currently (Mac OS X 10.3) the only way we have of regaining control
+ * from threads calling GL and nothing else is by patching the dispatch
+ * table of the CGLContext, so that glViewport, glFlush and glFinish
+ * call us back.
+ *
+ * Since glNewList and glEndList overwrite the entire dispatch table we
+ * also need to patch those so we can restore the others.
+ *
+ * WARNING: this is not expected to work on future OS releases.
+ */
+
+#define WRAP_CGL(context, vec, fun)			\
+    do {						\
+	(context)->disp.vec = (context)->ctx->disp.vec;	\
+	(context)->ctx->disp.vec = (fun);		\
+    } while (0)
+
+#define UNWRAP_CGL(context, vec)			\
+    do {						\
+	(context)->ctx->disp.vec = (context)->disp.vec;	\
+    } while (0)
+
+#define WRAP_BOILERPLATE					\
+    GLXContext gc;						\
+    __DRIcontextPrivate *pcp;					\
+    gc = __glXGetCurrentContext ();				\
+    if (gc == NULL || !gc->isDirect) return;			\
+    pcp = (__DRIcontextPrivate *) gc->driContext.private;	\
+    if (pcp == NULL) return;
+
+static void viewport_callback (GLIContext ctx, GLint x, GLint y,
+			       GLsizei width, GLsizei height)
+{
+    WRAP_BOILERPLATE
+
+    pthread_mutex_lock (&pcp->driScreenPriv->mutex);
+    update_context (pcp);
+    pthread_mutex_unlock (&pcp->driScreenPriv->mutex);
+
+    (*pcp->disp.viewport) (ctx, x, y, width, height);
+}
+
+static void new_list_callback (GLIContext ctx, GLuint list, GLenum mode)
+{
+    WRAP_BOILERPLATE
+
+    unwrap_context (pcp);
+    (*pcp->ctx->disp.new_list) (ctx, list, mode);
+    wrap_context (pcp);
+}
+
+static void end_list_callback (GLIContext ctx)
+{
+    WRAP_BOILERPLATE
+
+    unwrap_context (pcp);
+    (*pcp->ctx->disp.end_list) (ctx);
+    wrap_context (pcp);
+}
+
+static void unwrap_context (__DRIcontextPrivate *pcp)
+{
+    UNWRAP_CGL (pcp, viewport);
+    UNWRAP_CGL (pcp, new_list);
+    UNWRAP_CGL (pcp, end_list);
+}
+
+static void wrap_context (__DRIcontextPrivate *pcp)
+{
+    WRAP_CGL (pcp, new_list, new_list_callback);
+    WRAP_CGL (pcp, end_list, end_list_callback);
+    WRAP_CGL (pcp, viewport, viewport_callback);
+}
+
+/* XXX unfitting name. */
+void *__AppleDRICreateScreen(Display *dpy, int scr, __DRIscreen *psc,
+		const __DRIversion * ddx_version, const __DRIversion * dri_version,
+		const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
+		void * pSAREA, int fd, int internal_api_version,
+		const __DRIinterfaceMethods * interface,
+		__GLcontextModes ** driver_modes)
+{
+   static int here_before;
+
+   if (!here_before)
+   {
+      XAppleDRISetSurfaceNotifyHandler (driAppleSurfaceNotify);
+      here_before = True;
+   }
+
+   return driMesaCreateScreen(dpy, scr, psc, driver_modes);
+}
+
+#endif /* GLX_DIRECT_RENDERING */
diff --git a/src/glx/x11/apple_driver.h b/src/glx/x11/apple_driver.h
new file mode 100644
index 0000000..f44b019
--- /dev/null
+++ b/src/glx/x11/apple_driver.h
@@ -0,0 +1,181 @@
+/* $XFree86: xc/lib/GL/mesa/dri/dri_mesa.h,v 1.4 2000/06/17 00:02:50 martin Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright (c) 2002 Apple Computer, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * Authors:
+ *   Kevin E. Martin <kevin@precisioninsight.com>
+ *   Brian Paul <brian@precisioninsight.com>
+ */
+
+#ifndef _DRI_DRIVER_H_
+#define _DRI_DRIVER_H_
+
+#define XP_NO_X_HEADERS
+#include <Xplugin.h>
+#include <X11/Xthreads.h>
+#include <CoreGraphics/CoreGraphics.h>
+#include <OpenGL/gliContext.h>
+
+#ifdef GLX_DIRECT_RENDERING
+
+typedef struct __DRIdisplayPrivateRec  __DRIdisplayPrivate;
+typedef struct __DRIscreenPrivateRec   __DRIscreenPrivate;
+typedef struct __DRIvisualPrivateRec   __DRIvisualPrivate;
+typedef struct __DRIcontextPrivateRec  __DRIcontextPrivate;
+typedef struct __DRIdrawablePrivateRec __DRIdrawablePrivate;
+
+#endif
+
+#define DRI_MESA_VALIDATE_DRAWABLE_INFO(dpy,scrn,pDrawPriv)  \
+    do {                                                     \
+	if (*(pDrawPriv->pStamp) != pDrawPriv->lastStamp) {  \
+	    driMesaUpdateDrawableInfo(dpy,scrn,pDrawPriv);   \
+	}                                                    \
+    } while (0)
+
+struct __DRIdrawablePrivateRec {
+    /*
+    ** X's drawable ID associated with this private drawable.
+    */
+    GLXDrawable draw;
+
+    /*
+    ** Reference count for number of context's currently bound to this
+    ** drawable.  Once the refcount reaches 0, the drawable can be
+    ** destroyed.  This behavior will change with GLX 1.3.
+    */
+    int refcount;
+
+    xp_surface_id surface_id;
+    unsigned int uid;
+
+    /*
+    ** Pointer to contexts to which this drawable is currently bound.
+    */
+    __DRIcontextPrivate *driContextPriv;
+
+    /*
+    ** Pointer to screen on which this drawable was created.
+    */
+    __DRIscreenPrivate *driScreenPriv;
+
+    /*
+    ** Set when the drawable on the server is known to have gone away
+    */
+    unsigned int destroyed :1;
+};
+
+struct __DRIcontextPrivateRec {
+    /*
+    ** Other contexts bound to the same drawable.
+    */
+    __DRIcontextPrivate *next, *prev; 
+
+    /*
+    ** Kernel context handle used to access the device lock.
+    */
+    XID contextID;
+
+    CGLContextObj ctx;
+
+    /*
+    ** Set when attached
+    */
+    xp_surface_id surface_id;
+    xthread_t thread_id;
+
+    /*
+    ** This context's display pointer.
+    */
+    Display *display;
+
+    /*
+    ** Pointer to drawable currently bound to this context.
+    */
+    __DRIdrawablePrivate *driDrawablePriv;
+
+    /*
+    ** Pointer to screen on which this context was created.
+    */
+    __DRIscreenPrivate *driScreenPriv;
+
+    /*
+    ** wrapped CGL vectors
+    */
+    struct {
+	void (*viewport) (GLIContext ctx, GLint x, GLint y,
+			  GLsizei width, GLsizei height);
+	void (*new_list)(GLIContext ctx, GLuint list, GLenum mode);
+	void (*end_list) (GLIContext ctx);
+    } disp;
+
+    unsigned int pending_update :1;
+    unsigned int pending_clear :1;
+};
+
+struct __DRIvisualPrivateRec {
+    /*
+    ** X's visual ID associated with this private visual.
+    */
+    VisualID vid;
+
+    /*
+    ** CGL object representing the visual
+    */
+    CGLPixelFormatObj pixel_format;
+};
+
+struct __DRIscreenPrivateRec {
+    /*
+    ** Display for this screen
+    */
+    Display *display;
+
+    /*
+    ** Mutex for this screen
+    */
+    pthread_mutex_t mutex;
+
+    /*
+    ** Current screen's number
+    */
+    int myNum;
+
+    /*
+    ** Core rendering library's visuals associated with the current
+    ** screen.
+    */
+    __DRIvisualPrivate *visuals;
+    int numVisuals;
+};
+
+extern void driMesaUpdateDrawableInfo(Display *dpy, int scrn,
+				      __DRIdrawablePrivate *pdp);
+
+#endif /* _DRI_DRIVER_H_ */
diff --git a/src/glx/x11/appledri.c b/src/glx/x11/appledri.c
new file mode 100644
index 0000000..39556d6
--- /dev/null
+++ b/src/glx/x11/appledri.c
@@ -0,0 +1,312 @@
+/* $XFree86: xc/lib/GL/dri/XF86dri.c,v 1.12 2001/08/27 17:40:57 dawes Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright 2000 VA Linux Systems, Inc.
+Copyright (c) 2002 Apple Computer, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * Authors:
+ *   Kevin E. Martin <martin@valinux.com>
+ *   Jens Owen <jens@valinux.com>
+ *   Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ */
+
+/* THIS IS NOT AN X CONSORTIUM STANDARD */
+
+#ifdef GLX_USE_APPLEGL
+
+#define NEED_EVENTS
+#define NEED_REPLIES
+#include <X11/Xlibint.h>
+#include <X11/extensions/Xext.h>
+#include <X11/extensions/extutil.h>
+#include <X11/Xthreads.h>
+#include "appledristr.h"
+#include <stdio.h>
+#include <OpenGL/CGLTypes.h>
+//#include "dri_glx.h"
+#include "apple_driver.h"
+
+static XExtensionInfo _appledri_info_data;
+static XExtensionInfo *appledri_info = &_appledri_info_data;
+static char *appledri_extension_name = APPLEDRINAME;
+
+#define AppleDRICheckExtension(dpy,i,val) \
+  XextCheckExtension (dpy, i, appledri_extension_name, val)
+
+/*****************************************************************************
+ *                                                                           *
+ *			   private utility routines                          *
+ *                                                                           *
+ *****************************************************************************/
+
+static int close_display(Display *dpy, XExtCodes *extCodes);
+static Bool wire_to_event();
+
+static /* const */ XExtensionHooks appledri_extension_hooks = {
+    NULL,				/* create_gc */
+    NULL,				/* copy_gc */
+    NULL,				/* flush_gc */
+    NULL,				/* free_gc */
+    NULL,				/* create_font */
+    NULL,				/* free_font */
+    close_display,			/* close_display */
+    wire_to_event,			/* wire_to_event */
+    NULL,				/* event_to_wire */
+    NULL,				/* error */
+    NULL,				/* error_string */
+};
+
+static XEXT_GENERATE_FIND_DISPLAY (find_display, appledri_info, 
+				   appledri_extension_name, 
+				   &appledri_extension_hooks, 
+				   AppleDRINumberEvents, NULL)
+
+static XEXT_GENERATE_CLOSE_DISPLAY (close_display, appledri_info)
+
+static void (*surface_notify_handler) ();
+
+void *
+XAppleDRISetSurfaceNotifyHandler (void (*fun) ())
+{
+    void *old = surface_notify_handler;
+    surface_notify_handler = fun;
+    return old;
+}
+
+static Bool wire_to_event (dpy, re, event)
+    Display *dpy;
+    XEvent  *re;
+    xEvent  *event;
+{
+    XExtDisplayInfo *info = find_display (dpy);
+    xAppleDRINotifyEvent *sevent;
+
+    AppleDRICheckExtension (dpy, info, False);
+
+    switch ((event->u.u.type & 0x7f) - info->codes->first_event) {
+    case AppleDRISurfaceNotify:
+	sevent = (xAppleDRINotifyEvent *) event;
+	if (surface_notify_handler != NULL)
+	{
+	    (*surface_notify_handler) (dpy, (unsigned int) sevent->arg,
+				       (int) sevent->kind);
+	}
+	return False;
+    }
+    return False;
+}
+
+/*****************************************************************************
+ *                                                                           *
+ *		    public Apple-DRI Extension routines                      *
+ *                                                                           *
+ *****************************************************************************/
+
+#if 0
+#include <stdio.h>
+#define TRACE(msg)  fprintf(stderr, "AppleDRI%s\n", msg);
+#else
+#define TRACE(msg)
+#endif
+
+
+Bool XAppleDRIQueryExtension (dpy, event_basep, error_basep)
+    Display *dpy;
+    int *event_basep, *error_basep;
+{
+    XExtDisplayInfo *info = find_display (dpy);
+
+    TRACE("QueryExtension...");
+    if (XextHasExtension(info)) {
+	*event_basep = info->codes->first_event;
+	*error_basep = info->codes->first_error;
+        TRACE("QueryExtension... return True");
+	return True;
+    } else {
+        TRACE("QueryExtension... return False");
+	return False;
+    }
+}
+
+Bool XAppleDRIQueryVersion(dpy, majorVersion, minorVersion, patchVersion)
+    Display* dpy;
+    int* majorVersion; 
+    int* minorVersion;
+    int* patchVersion;
+{
+    XExtDisplayInfo *info = find_display (dpy);
+    xAppleDRIQueryVersionReply rep;
+    xAppleDRIQueryVersionReq *req;
+
+    TRACE("QueryVersion...");
+    AppleDRICheckExtension (dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(AppleDRIQueryVersion, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_AppleDRIQueryVersion;
+    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
+	UnlockDisplay(dpy);
+	SyncHandle();
+        TRACE("QueryVersion... return False");
+	return False;
+    }
+    *majorVersion = rep.majorVersion;
+    *minorVersion = rep.minorVersion;
+    *patchVersion = rep.patchVersion;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("QueryVersion... return True");
+    return True;
+}
+
+Bool XAppleDRIQueryDirectRenderingCapable(dpy, screen, isCapable)
+    Display* dpy;
+    int screen;
+    Bool* isCapable;
+{
+    XExtDisplayInfo *info = find_display (dpy);
+    xAppleDRIQueryDirectRenderingCapableReply rep;
+    xAppleDRIQueryDirectRenderingCapableReq *req;
+
+    TRACE("QueryDirectRenderingCapable...");
+    AppleDRICheckExtension (dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(AppleDRIQueryDirectRenderingCapable, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_AppleDRIQueryDirectRenderingCapable;
+    req->screen = screen;
+    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse)) {
+	UnlockDisplay(dpy);
+	SyncHandle();
+        TRACE("QueryDirectRenderingCapable... return False");
+	return False;
+    }
+    *isCapable = rep.isCapable;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("QueryDirectRenderingCapable... return True");
+    return True;
+}
+
+Bool XAppleDRIAuthConnection(dpy, screen, magic)
+    Display* dpy;
+    int screen;
+    unsigned int magic;
+{
+    XExtDisplayInfo *info = find_display (dpy);
+    xAppleDRIAuthConnectionReq *req;
+    xAppleDRIAuthConnectionReply rep;
+
+    TRACE("AuthConnection...");
+    AppleDRICheckExtension (dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(AppleDRIAuthConnection, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_AppleDRIAuthConnection;
+    req->screen = screen;
+    req->magic = magic;
+    rep.authenticated = 0;
+    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.authenticated) {
+	UnlockDisplay(dpy);
+	SyncHandle();
+        TRACE("AuthConnection... return False");
+	return False;
+    }
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("AuthConnection... return True");
+    return True;
+}
+
+Bool XAppleDRICreateSurface(dpy, screen, drawable, client_id, key, uid)
+    Display* dpy;
+    int screen;
+    Drawable drawable;
+    unsigned int client_id;
+    unsigned int *key;
+    unsigned int *uid;
+{
+    XExtDisplayInfo *info = find_display (dpy);
+    xAppleDRICreateSurfaceReply rep;
+    xAppleDRICreateSurfaceReq *req;
+
+    TRACE("CreateSurface...");
+    AppleDRICheckExtension (dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(AppleDRICreateSurface, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_AppleDRICreateSurface;
+    req->screen = screen;
+    req->drawable = drawable;
+    req->client_id = client_id;
+    rep.key_0 = rep.key_1 = rep.uid = 0;
+    if (!_XReply(dpy, (xReply *)&rep, 0, xFalse) || !rep.key_0) {
+	UnlockDisplay(dpy);
+	SyncHandle();
+        TRACE("CreateSurface... return False");
+	return False;
+    }
+    key[0] = rep.key_0;
+    key[1] = rep.key_1;
+    *uid = rep.uid;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("CreateSurface... return True");
+    return True;
+}
+
+Bool XAppleDRIDestroySurface(dpy, screen, drawable)
+    Display* dpy;
+    int screen;
+    Drawable drawable;
+{
+    XExtDisplayInfo *info = find_display (dpy);
+    xAppleDRIDestroySurfaceReq *req;
+
+    TRACE("DestroySurface...");
+    AppleDRICheckExtension (dpy, info, False);
+
+    LockDisplay(dpy);
+    GetReq(AppleDRIDestroySurface, req);
+    req->reqType = info->codes->major_opcode;
+    req->driReqType = X_AppleDRIDestroySurface;
+    req->screen = screen;
+    req->drawable = drawable;
+    UnlockDisplay(dpy);
+    SyncHandle();
+    TRACE("DestroySurface... return True");
+    return True;
+}
+
+#endif /* GLX_USE_APPLEGL */
+
diff --git a/src/glx/x11/appledri.h b/src/glx/x11/appledri.h
new file mode 100644
index 0000000..2930abe
--- /dev/null
+++ b/src/glx/x11/appledri.h
@@ -0,0 +1,114 @@
+/* $XFree86: xc/lib/GL/dri/xf86dri.h,v 1.7 2000/12/07 20:26:02 dawes Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright 2000 VA Linux Systems, Inc.
+Copyright (c) 2002 Apple Computer, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * Authors:
+ *   Kevin E. Martin <martin@valinux.com>
+ *   Jens Owen <jens@valinux.com>
+ *   Rickard E. (Rik) Faith <faith@valinux.com>
+ *
+ */
+
+#ifndef _APPLEDRI_H_
+#define _APPLEDRI_H_
+
+#include <X11/Xfuncproto.h>
+#include "glxclient.h"
+
+#define X_AppleDRIQueryVersion			0
+#define X_AppleDRIQueryDirectRenderingCapable	1
+#define X_AppleDRICreateSurface			2
+#define X_AppleDRIDestroySurface		3
+#define X_AppleDRIAuthConnection                4
+/* Requests up to and including 18 were used in a previous version */
+
+/* Events */
+#define AppleDRIObsoleteEvent1		0
+#define AppleDRIObsoleteEvent2		1
+#define AppleDRIObsoleteEvent3		2
+#define AppleDRISurfaceNotify		3
+#define AppleDRINumberEvents		4
+
+/* Errors */
+#define AppleDRIClientNotLocal		0
+#define AppleDRIOperationNotSupported	1
+#define AppleDRINumberErrors		(AppleDRIOperationNotSupported + 1)
+
+/* Kinds of SurfaceNotify events: */
+#define AppleDRISurfaceNotifyChanged	0
+#define AppleDRISurfaceNotifyDestroyed	1
+
+#ifndef _APPLEDRI_SERVER_
+
+typedef struct {
+    int	type;		    /* of event */
+    unsigned long serial;   /* # of last request processed by server */
+    Bool send_event;	    /* true if this came frome a SendEvent request */
+    Display *display;	    /* Display the event was read from */
+    Window window;	    /* window of event */
+    Time time;		    /* server timestamp when event happened */
+    int kind;		    /* subtype of event */
+    int arg;
+} XAppleDRINotifyEvent;
+
+_XFUNCPROTOBEGIN
+
+Bool XAppleDRIQueryExtension (Display *dpy, int *event_base, int *error_base);
+
+Bool XAppleDRIQueryVersion (Display *dpy, int *majorVersion,
+			    int *minorVersion, int *patchVersion);
+
+Bool XAppleDRIQueryDirectRenderingCapable (Display *dpy, int screen,
+					   Bool *isCapable);
+
+void *XAppleDRISetSurfaceNotifyHandler (void (*fun) (Display *dpy,
+						     unsigned uid, int kind));
+
+Bool XAppleDRIAuthConnection (Display *dpy, int screen, unsigned int magic);
+
+Bool XAppleDRICreateSurface (Display *dpy, int screen, Drawable drawable,
+			     unsigned int client_id, unsigned int key[2],
+			     unsigned int* uid);
+
+Bool XAppleDRIDestroySurface (Display *dpy, int screen, Drawable drawable);
+
+Bool XAppleDRISynchronizeSurfaces (Display *dpy);
+
+void *__AppleDRICreateScreen(Display *dpy, int scr, __DRIscreen *psc,
+		const __DRIversion * ddx_version, const __DRIversion * dri_version,
+		const __DRIversion * drm_version, const __DRIframebuffer * frame_buffer,
+		void * pSAREA, int fd, int internal_api_version,
+		const __DRIinterfaceMethods * interface,
+		__GLcontextModes ** driver_modes);
+_XFUNCPROTOEND
+
+#endif /* _APPLEDRI_SERVER_ */
+#endif /* _APPLEDRI_H_ */
+
diff --git a/src/glx/x11/appledristr.h b/src/glx/x11/appledristr.h
new file mode 100644
index 0000000..36a2e89
--- /dev/null
+++ b/src/glx/x11/appledristr.h
@@ -0,0 +1,176 @@
+/* $XFree86: xc/lib/GL/dri/xf86dristr.h,v 1.9 2001/03/21 16:01:08 dawes Exp $ */
+/**************************************************************************
+
+Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
+Copyright 2000 VA Linux Systems, Inc.
+Copyright (c) 2002 Apple Computer, Inc.
+All Rights Reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sub license, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice (including the
+next paragraph) shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
+ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+**************************************************************************/
+
+/*
+ * Authors:
+ *   Kevin E. Martin <martin@valinux.com>
+ *   Jens Owen <jens@valinux.com>
+ *   Rickard E. (Rik) Fiath <faith@valinux.com>
+ *
+ */
+
+#ifndef _APPLEDRISTR_H_
+#define _APPLEDRISTR_H_
+
+#include "appledri.h"
+
+#define APPLEDRINAME "Apple-DRI"
+
+#define APPLE_DRI_MAJOR_VERSION	1	/* current version numbers */
+#define APPLE_DRI_MINOR_VERSION	0
+#define APPLE_DRI_PATCH_VERSION	0
+
+typedef struct _AppleDRIQueryVersion {
+    CARD8	reqType;		/* always DRIReqCode */
+    CARD8	driReqType;		/* always X_DRIQueryVersion */
+    CARD16	length B16;
+} xAppleDRIQueryVersionReq;
+#define sz_xAppleDRIQueryVersionReq	4
+
+typedef struct {
+    BYTE	type;			/* X_Reply */
+    BOOL	pad1;
+    CARD16	sequenceNumber B16;
+    CARD32	length B32;
+    CARD16	majorVersion B16;	/* major version of DRI protocol */
+    CARD16	minorVersion B16;	/* minor version of DRI protocol */
+    CARD32	patchVersion B32;       /* patch version of DRI protocol */
+    CARD32	pad3 B32;
+    CARD32	pad4 B32;
+    CARD32	pad5 B32;
+    CARD32	pad6 B32;
+} xAppleDRIQueryVersionReply;
+#define sz_xAppleDRIQueryVersionReply	32
+
+typedef struct _AppleDRIQueryDirectRenderingCapable {
+    CARD8	reqType;		/* always DRIReqCode */
+    CARD8	driReqType;		/* X_DRIQueryDirectRenderingCapable */
+    CARD16	length B16;
+    CARD32	screen B32;
+} xAppleDRIQueryDirectRenderingCapableReq;
+#define sz_xAppleDRIQueryDirectRenderingCapableReq	8
+
+typedef struct {
+    BYTE	type;			/* X_Reply */
+    BOOL	pad1;
+    CARD16	sequenceNumber B16;
+    CARD32	length B32;
+    BOOL	isCapable;
+    BOOL	pad2;
+    BOOL	pad3;
+    BOOL	pad4;
+    CARD32	pad5 B32;
+    CARD32	pad6 B32;
+    CARD32	pad7 B32;
+    CARD32	pad8 B32;
+    CARD32	pad9 B32;
+} xAppleDRIQueryDirectRenderingCapableReply;
+#define sz_xAppleDRIQueryDirectRenderingCapableReply	32
+
+typedef struct _AppleDRIAuthConnection {
+    CARD8	reqType;		/* always DRIReqCode */
+    CARD8	driReqType;		/* always X_DRICloseConnection */
+    CARD16	length B16;
+    CARD32	screen B32;
+    CARD32      magic B32;
+} xAppleDRIAuthConnectionReq;
+#define sz_xAppleDRIAuthConnectionReq	12
+
+typedef struct {
+    BYTE        type;
+    BOOL        pad1;
+    CARD16      sequenceNumber B16;
+    CARD32      length B32;
+    CARD32      authenticated B32;
+    CARD32      pad2 B32;
+    CARD32      pad3 B32;
+    CARD32      pad4 B32;
+    CARD32      pad5 B32;
+    CARD32      pad6 B32;
+} xAppleDRIAuthConnectionReply;
+#define zx_xAppleDRIAuthConnectionReply  32
+
+typedef struct _AppleDRICreateSurface {
+    CARD8	reqType;		/* always DRIReqCode */
+    CARD8	driReqType;		/* always X_DRICreateSurface */
+    CARD16	length B16;
+    CARD32	screen B32;
+    CARD32	drawable B32;
+    CARD32	client_id B32;
+} xAppleDRICreateSurfaceReq;
+#define sz_xAppleDRICreateSurfaceReq	16
+
+typedef struct {
+    BYTE	type;			/* X_Reply */
+    BOOL	pad1;
+    CARD16	sequenceNumber B16;
+    CARD32	length B32;
+    CARD32	key_0 B32;
+    CARD32	key_1 B32;
+    CARD32	uid B32;
+    CARD32	pad4 B32;
+    CARD32	pad5 B32;
+    CARD32	pad6 B32;
+} xAppleDRICreateSurfaceReply;
+#define sz_xAppleDRICreateSurfaceReply	32
+
+typedef struct _AppleDRIDestroySurface {
+    CARD8	reqType;		/* always DRIReqCode */
+    CARD8	driReqType;		/* always X_DRIDestroySurface */
+    CARD16	length B16;
+    CARD32	screen B32;
+    CARD32	drawable B32;
+} xAppleDRIDestroySurfaceReq;
+#define sz_xAppleDRIDestroySurfaceReq	12
+
+typedef struct _AppleDRINotify {
+	BYTE	type;		/* always eventBase + event type */
+	BYTE	kind;
+	CARD16	sequenceNumber B16;
+	Time	time B32;	/* time of change */
+	CARD16	pad1 B16;
+	CARD32	arg B32;
+	CARD32	pad3 B32;
+} xAppleDRINotifyEvent;
+#define sz_xAppleDRINotifyEvent	20
+
+#ifdef _APPLEDRI_SERVER_
+
+void AppleDRISendEvent (
+#if NeedFunctionPrototypes
+    int			/* type */,
+    unsigned int	/* mask */,
+    int			/* which */,
+    int			/* arg */
+#endif
+);
+
+#endif /* _APPLEDRI_SERVER_ */
+#endif /* _APPLEDRISTR_H_ */
diff --git a/src/glx/x11/clientattrib.c b/src/glx/x11/clientattrib.c
index bfb263c..660bc72 100644
--- a/src/glx/x11/clientattrib.c
+++ b/src/glx/x11/clientattrib.c
@@ -57,6 +57,7 @@ do_enable_disable(GLenum array, GLboolean val )
     }
 }
 
+#ifdef GLX_INDIRECT_RENDERING
 void __indirect_glEnableClientState(GLenum array)
 {
     do_enable_disable( array, GL_TRUE );
@@ -123,6 +124,7 @@ void __indirect_glPopClientAttrib(void)
 	return;
     }
 }
+#endif /* GLX_INDIRECT_RENDERING */
 
 void __glFreeAttributeState(__GLXcontext *gc)
 {
diff --git a/src/glx/x11/dri_glx.c b/src/glx/x11/dri_glx.c
index dab454e..91aa79e 100644
--- a/src/glx/x11/dri_glx.c
+++ b/src/glx/x11/dri_glx.c
@@ -41,8 +41,12 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include <X11/extensions/extutil.h>
 #include "glheader.h"
 #include "glxclient.h"
+#ifdef GLX_USE_APPLEGL
+#include "appledri.h"
+#else
 #include "xf86dri.h"
 #include "sarea.h"
+#endif
 #include <stdio.h>
 #include <dlfcn.h>
 #include "dri_glx.h"
@@ -185,6 +189,16 @@ static const char createNewScreenName[] = __DRI_CREATE_NEW_SCREEN_STRING;
  */
 static __DRIdriver *OpenDriver(const char *driverName)
 {
+#ifdef GLX_USE_APPLEGL
+   static __DRIdriver driver = {
+	   "Apple-DRI",
+	   NULL,
+	   NULL,
+	   __AppleDRICreateScreen,
+	   NULL
+   };
+   return &driver;
+#else
    void *glhandle = NULL;
    char *libPaths = NULL;
    char libDir[1000];
@@ -283,6 +297,7 @@ static __DRIdriver *OpenDriver(const char *driverName)
       dlclose(glhandle);
 
    return driver;
+#endif
 }
 
 
@@ -293,6 +308,24 @@ static __DRIdriver *OpenDriver(const char *driverName)
  */
 static Bool GetDriverName(Display *dpy, int scrNum, char **driverName)
 {
+#ifdef GLX_USE_APPLEGL
+   int directCapable;
+
+   if (!XAppleDRIQueryDirectRenderingCapable(dpy, scrNum, &directCapable)) {
+      ErrorMessageF("XAppleDRIQueryDirectRenderingCapable failed\n");
+      return False;
+   }
+   if (!directCapable) {
+      ErrorMessageF("XAppleDRIQueryDirectRenderingCapable returned false\n");
+      return False;
+   }
+
+   *driverName = __glXstrdup("Apple-DRI");
+   if (!*driverName)
+	   return False;
+
+   return True;
+#else
    int directCapable;
    Bool b;
    int driverMajor, driverMinor, driverPatch;
@@ -319,6 +352,7 @@ static Bool GetDriverName(Display *dpy, int scrNum, char **driverName)
 	     driverMajor, driverMinor, driverPatch, *driverName, scrNum);
 
    return True;
+#endif
 }
 
 
@@ -376,11 +410,15 @@ PUBLIC const char *glXGetScreenDriver (Display *dpy, int scrNum) {
  * Note: The driver remains opened after this function returns.
  */
 PUBLIC const char *glXGetDriverConfig (const char *driverName) {
+#ifdef GLX_USE_APPLEGL
+   return NULL;
+#else
    __DRIdriver *driver = OpenDriver (driverName);
    if (driver)
       return dlsym (driver->handle, "__driConfigOptions");
    else
       return NULL;
+#endif
 }
 
 
@@ -391,6 +429,7 @@ static void driDestroyDisplay(Display *dpy, void *private)
     __DRIdisplayPrivate *pdpyp = (__DRIdisplayPrivate *)private;
 
     if (pdpyp) {
+#ifndef GLX_USE_APPLEGL
         const int numScreens = ScreenCount(dpy);
         int i;
         for (i = 0; i < numScreens; i++) {
@@ -417,6 +456,7 @@ static void driDestroyDisplay(Display *dpy, void *private)
 	   }
         }
         Xfree(pdpyp->libraryHandles);
+#endif
 	Xfree(pdpyp);
     }
 }
@@ -442,6 +482,15 @@ void *driCreateDisplay(Display *dpy, __DRIdisplay *pdisp)
     pdisp->private = NULL;
     pdisp->destroyDisplay = NULL;
 
+#ifdef GLX_USE_APPLEGL
+    if (!XAppleDRIQueryExtension(dpy, &eventBase, &errorBase)) {
+	return NULL;
+    }
+
+    if (!XAppleDRIQueryVersion(dpy, &major, &minor, &patch)) {
+	return NULL;
+    }
+#else
     if (!XF86DRIQueryExtension(dpy, &eventBase, &errorBase)) {
 	return NULL;
     }
@@ -449,6 +498,7 @@ void *driCreateDisplay(Display *dpy, __DRIdisplay *pdisp)
     if (!XF86DRIQueryVersion(dpy, &major, &minor, &patch)) {
 	return NULL;
     }
+#endif
 
     pdpyp = (__DRIdisplayPrivate *)Xmalloc(sizeof(__DRIdisplayPrivate));
     if (!pdpyp) {
@@ -469,6 +519,7 @@ void *driCreateDisplay(Display *dpy, __DRIdisplay *pdisp)
        return NULL;
     }
 
+#ifndef GLX_USE_APPLEGL
     /* allocate array of library handles */
     pdpyp->libraryHandles = (void **) Xmalloc(numScreens * sizeof(void*));
     if (!pdpyp->libraryHandles) {
@@ -476,6 +527,7 @@ void *driCreateDisplay(Display *dpy, __DRIdisplay *pdisp)
        Xfree(pdpyp);
        return NULL;
     }
+#endif
 
     /* dynamically discover DRI drivers for all screens, saving each
      * driver's "__driCreateScreen" function pointer.  That's the bootstrap
@@ -485,11 +537,15 @@ void *driCreateDisplay(Display *dpy, __DRIdisplay *pdisp)
         __DRIdriver *driver = driGetDriver(dpy, scrn);
         if (driver) {
            pdisp->createNewScreen[scrn] = driver->createNewScreenFunc;
+#ifndef GLX_USE_APPLEGL
            pdpyp->libraryHandles[scrn] = driver->handle;
+#endif
         }
         else {
            pdisp->createNewScreen[scrn] = NULL;
+#ifndef GLX_USE_APPLEGL
            pdpyp->libraryHandles[scrn] = NULL;
+#endif
         }
     }
 
diff --git a/src/glx/x11/dri_glx.h b/src/glx/x11/dri_glx.h
index 7556168..3126e25 100644
--- a/src/glx/x11/dri_glx.h
+++ b/src/glx/x11/dri_glx.h
@@ -45,10 +45,12 @@ struct __DRIdisplayPrivateRec {
     int driMinor;
     int driPatch;
 
+#ifndef GLX_USE_APPLEGL
     /*
     ** Array of library handles [indexed by screen number]
     */
     void **libraryHandles;
+#endif
 };
 
 typedef struct __DRIdisplayPrivateRec  __DRIdisplayPrivate;
diff --git a/src/glx/x11/glxclient.h b/src/glx/x11/glxclient.h
index 7054f3c..36b8f14 100644
--- a/src/glx/x11/glxclient.h
+++ b/src/glx/x11/glxclient.h
@@ -276,12 +276,14 @@ struct __GLXcontextRec {
      */
     GLboolean fastImageUnpack;
 
+#ifdef GLX_INDIRECT_RENDERING
     /**
      * Fill newImage with the unpacked form of \c oldImage getting it
      * ready for transport to the server.
      */
     void (*fillImage)(__GLXcontext*, GLint, GLint, GLint, GLint, GLenum,
 		      GLenum, const GLvoid*, GLubyte*, GLubyte*);
+#endif
 
     /**
      * Client side attribs.
diff --git a/src/glx/x11/glxcmds.c b/src/glx/x11/glxcmds.c
index 7ff2bf7..7286b41 100644
--- a/src/glx/x11/glxcmds.c
+++ b/src/glx/x11/glxcmds.c
@@ -39,6 +39,13 @@
  * Client-side GLX interface.
  */
 
+#ifdef GLX_USE_APPLEGL
+/* Need to include these first to ensure we pick up the correct gl.h
+   and glext.h (from the framework) */
+#include <OpenGL/OpenGL.h>
+#include <OpenGL/CGLContext.h>
+#endif
+
 #include <inttypes.h>
 #include "glxclient.h"
 #include <X11/extensions/extutil.h>
@@ -49,8 +56,10 @@
 #ifdef GLX_DIRECT_RENDERING
 #include "indirect_init.h"
 #include <X11/extensions/xf86vmode.h>
+#ifndef GLX_USE_APPLEGL
 #include "xf86dri.h"
 #endif
+#endif
 #include "glxextensions.h"
 #include "glcontextmodes.h"
 #include "glheader.h"
@@ -100,7 +109,9 @@ static void GarbageCollectDRIDrawables(Display *dpy, __GLXscreenConfigs *sc)
 		/* Destroy the local drawable data, if the drawable no
 		   longer exists in the Xserver */
 		(*pdraw->driDrawable.destroyDrawable)(&pdraw->driDrawable);
+#ifndef GLX_USE_APPLEGL
 		XF86DRIDestroyDrawable(dpy, sc->scr, draw);
+#endif
                 __glxHashDelete(sc->drawHash, draw);
 		Xfree(pdraw);
 	    }
@@ -312,7 +323,9 @@ GLXContext AllocateGLXContext( Display *dpy )
     ** to GL_TRUE.
     */
     gc->fastImageUnpack = GL_FALSE;
+#ifdef GLX_INDIRECT_RENDERING
     gc->fillImage = __glFillImage;
+#endif
     gc->isDirect = GL_FALSE;
     gc->pc = gc->buf;
     gc->bufEnd = gc->buf + bufSize;
@@ -378,8 +391,6 @@ CreateContext(Display *dpy, XVisualInfo *vis,
 	    int screen = (fbconfig == NULL) ? vis->screen : fbconfig->screen;
 	    __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
 	    const __GLcontextModes * mode;
-	    drm_context_t hwContext;
-
 
 	    if (fbconfig == NULL) {
 		mode = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
@@ -403,6 +414,8 @@ CreateContext(Display *dpy, XVisualInfo *vis,
 	    if (psc && psc->driScreen.private) {
 		__DRIcontext *shared = (shareList != NULL)
 		    ? &shareList->driContext : NULL;
+#ifndef GLX_USE_APPLEGL
+		drm_context_t hwContext;
 
 
 		if (!XF86DRICreateContextWithConfig(dpy, psc->scr,
@@ -410,12 +423,15 @@ CreateContext(Display *dpy, XVisualInfo *vis,
 						    &gc->hwContextID, &hwContext))
 		    /* gah, handle this better */
 		    return NULL;
+#endif
 
 		gc->driContext.private = 
 		  (*psc->driScreen.createNewContext)( &psc->driScreen,
 						      mode, renderType,
 						      shared,
+#ifndef GLX_USE_APPLEGL
 						      hwContext,
+#endif
 						      &gc->driContext );
 		if (gc->driContext.private) {
 		    gc->isDirect = GL_TRUE;
@@ -424,7 +440,9 @@ CreateContext(Display *dpy, XVisualInfo *vis,
 		    gc->mode = mode;
 		}
 		else {
+#ifndef GLX_USE_APPLEGL
 		    XF86DRIDestroyContext(dpy, psc->scr, gc->hwContextID);
+#endif
 		}
 	    }
 	}
@@ -535,7 +553,9 @@ DestroyContext(Display *dpy, GLXContext gc)
     if (gc->isDirect) {
 	if (gc->driContext.private) {
 	    (*gc->driContext.destroyContext)(&gc->driContext);
+#ifndef GLX_USE_APPLEGL
 	    XF86DRIDestroyContext(dpy, gc->psc->scr, gc->hwContextID);
+#endif
 	    gc->driContext.private = NULL;
 	}
 	GarbageCollectDRIDrawables(dpy, gc->psc);
@@ -2174,7 +2194,7 @@ static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
 GLboolean
 __driGetMscRateOML(__DRIdrawable *draw, int32_t *numerator, int32_t *denominator)
 {
-#ifdef XF86VIDMODE
+#if defined(XF86VIDMODE) && !defined(GLX_USE_APPLEGL)
     __GLXscreenConfigs *psc;
     XF86VidModeModeLine   mode_line;
     int   dot_clock;
diff --git a/src/glx/x11/glxext.c b/src/glx/x11/glxext.c
index b98d48d..4b5779c 100644
--- a/src/glx/x11/glxext.c
+++ b/src/glx/x11/glxext.c
@@ -60,9 +60,11 @@
 #ifdef GLX_DIRECT_RENDERING
 #include <inttypes.h>
 #include <sys/mman.h>
+#ifndef GLX_USE_APPLEGL
 #include "xf86dri.h"
 #include "xf86drm.h"
 #include "sarea.h"
+#endif
 #include "dri_glx.h"
 #endif
 
@@ -134,10 +136,12 @@ static __GLXcontext dummyContext = {
 };
 
 
+#ifdef GLX_INDIRECT_RENDERING
 /*
 ** All indirect rendering contexts will share the same indirect dispatch table.
 */
 static __GLapi *IndirectAPI = NULL;
+#endif
 
 
 /*
@@ -711,6 +715,7 @@ static GLboolean has_damage_post(Display *dpy)
 }
 #endif /* XDAMAGE_1_1_INTERFACE */
 
+#ifndef GLX_USE_APPLEGL
 static void __glXReportDamage(__DRIdrawable *driDraw,
 			      int x, int y,
 			      drm_clip_rect_t *rects, int num_rects,
@@ -776,6 +781,7 @@ __glXDRIGetDrawableInfo(__DRIdrawable *drawable,
 				  backX, backY,
 				  numBackClipRects, pBackClipRects);
 }
+#endif
 
 
 /**
@@ -785,12 +791,16 @@ static const __DRIinterfaceMethods interface_methods = {
     _gl_context_modes_create,
     _gl_context_modes_destroy,
 
+#ifndef GLX_USE_APPLEGL
     __glXDRIGetDrawableInfo,
+#endif
 
     __glXGetUST,
     __driGetMscRateOML,
 
+#ifndef GLX_USE_APPLEGL
     __glXReportDamage,
+#endif
 };
 
 
@@ -818,6 +828,7 @@ CallCreateNewScreen(Display *dpy, int scrn, __GLXscreenConfigs *psc,
 		    PFNCREATENEWSCREENFUNC createNewScreen)
 {
     __DRIscreenPrivate *psp = NULL;
+    int api_ver = __glXGetInternalVersion();
 #ifndef GLX_USE_APPLEGL
     drm_handle_t hSAREA;
     drmAddress pSAREA = MAP_FAILED;
@@ -830,7 +841,6 @@ CallCreateNewScreen(Display *dpy, int scrn, __GLXscreenConfigs *psc,
     int   status;
     const char * err_msg;
     const char * err_extra;
-    int api_ver = __glXGetInternalVersion();
 
 
     dri_version.major = driDpy->private->driMajor;
@@ -990,7 +1000,21 @@ CallCreateNewScreen(Display *dpy, int scrn, __GLXscreenConfigs *psc,
 
         fprintf(stderr, "libGL error: reverting to (slow) indirect rendering\n");
     }
-#endif /* !GLX_USE_APPLEGL */
+#else  /* !GLX_USE_APPLEGL */
+    psp = (*createNewScreen)(dpy,
+		    	     scrn,
+			     &psc->driScreen,
+			     NULL,
+			     NULL,
+			     NULL,
+			     NULL,
+			     NULL,
+			     -1,
+			     api_ver,
+			     & interface_methods,
+			     &psc->visuals);
+
+#endif /* GLX_USE_APPLEGL */
 
     return psp;
 }
@@ -1177,8 +1201,10 @@ static Bool AllocAndFetchScreenConfigs(Display *dpy, __GLXdisplayPrivate *priv)
 		    CallCreateNewScreen(dpy, i, psc,
 					& priv->driDisplay,
 					priv->driDisplay.createNewScreen[i] );
+#ifndef GLX_USE_APPLEGL
 		if (psc->driScreen.private != NULL)
 		    __glXScrEnableDRIExtension(psc);
+#endif
 	    }
 	}
 #endif
@@ -1592,7 +1618,9 @@ FetchDRIDrawable( Display *dpy, GLXDrawable drawable, GLXContext gc)
     __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
     __GLXdrawable *pdraw;
     __GLXscreenConfigs *sc;
+#ifndef GLX_USE_APPLEGL
     drm_drawable_t hwDrawable;
+#endif
     void *empty_attribute_list = NULL;
 
     if (priv == NULL || priv->driDisplay.private == NULL)
@@ -1610,27 +1638,37 @@ FetchDRIDrawable( Display *dpy, GLXDrawable drawable, GLXContext gc)
     pdraw->drawable = drawable;
     pdraw->psc = sc;
 
+#ifndef GLX_USE_APPLEGL
     if (!XF86DRICreateDrawable(dpy, sc->scr, drawable, &hwDrawable))
 	return NULL;
+#endif
 
     /* Create a new drawable */
     pdraw->driDrawable.private =
 	(*sc->driScreen.createNewDrawable)(&sc->driScreen,
 					   gc->mode,
 					   &pdraw->driDrawable,
+#ifndef GLX_USE_APPLEGL
 					   hwDrawable,
+#else
+					   drawable,
+#endif
 					   GLX_WINDOW_BIT,
 					   empty_attribute_list);
 
     if (!pdraw->driDrawable.private) {
+#ifndef GLX_USE_APPLEGL
 	XF86DRIDestroyDrawable(dpy, sc->scr, drawable);
+#endif
 	Xfree(pdraw);
 	return NULL;
     }
 
     if (__glxHashInsert(sc->drawHash, drawable, pdraw)) {
 	(*pdraw->driDrawable.destroyDrawable)(&pdraw->driDrawable);
+#ifndef GLX_USE_APPLEGL
 	XF86DRIDestroyDrawable(dpy, sc->scr, drawable);
+#endif
 	Xfree(pdraw);
 	return NULL;
     }
@@ -1689,7 +1727,9 @@ USED static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
     }
 #endif
 
+#ifndef GLX_USE_APPLEGL
     _glapi_check_multithread();
+#endif
 
 #ifdef GLX_DIRECT_RENDERING
     /* Bind the direct rendering context to the drawable */
@@ -1763,9 +1803,11 @@ USED static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
 		    if (oldGC->driContext.private) {
 			(*oldGC->driContext.destroyContext)
 			    (&oldGC->driContext);
+#ifndef GLX_USE_APPLEGL
 			XF86DRIDestroyContext(oldGC->createDpy,
 					      oldGC->psc->scr,
 					      gc->hwContextID);
+#endif
 			oldGC->driContext.private = NULL;
 		    }
 		}
@@ -1781,6 +1823,7 @@ USED static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
 	    gc->currentReadable = read;
 
             if (!gc->isDirect) {
+#ifdef GLX_INDIRECT_RENDERING
                if (!IndirectAPI)
                   IndirectAPI = __glXNewIndirectAPI();
                _glapi_set_dispatch(IndirectAPI);
@@ -1801,6 +1844,7 @@ USED static Bool MakeContextCurrent(Display *dpy, GLXDrawable draw,
 		    (void) glGetString(GL_VERSION);
 		    __glXInitVertexArrayState(gc);
 		}
+#endif
 	    }
 	    else {
 		gc->currentContextTag = -1;
diff --git a/src/glx/x11/indirect_init.c b/src/glx/x11/indirect_init.c
index 852fe71..c86f05c 100644
--- a/src/glx/x11/indirect_init.c
+++ b/src/glx/x11/indirect_init.c
@@ -35,6 +35,8 @@
  * \author Ian Romanick <idr@us.ibm.com>
  */
 
+#ifdef GLX_INDIRECT_RENDER
+
 #include "indirect_init.h"
 #include "indirect.h"
 #include "glapi.h"
@@ -771,3 +773,4 @@ __GLapi * __glXNewIndirectAPI( void )
     return glAPI;
 }
 
+#endif /*GLX_INDIRECT_RENDER*/
diff --git a/src/glx/x11/indirect_transpose_matrix.c b/src/glx/x11/indirect_transpose_matrix.c
index 2144410..26b128e 100644
--- a/src/glx/x11/indirect_transpose_matrix.c
+++ b/src/glx/x11/indirect_transpose_matrix.c
@@ -22,6 +22,7 @@
  * USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
 
+#ifdef GLX_INDIRECT_RENDER
 #include <GL/gl.h>
 #include "indirect.h"
 
@@ -81,3 +82,4 @@ __indirect_glMultTransposeMatrixfARB( const GLfloat * m )
    TransposeMatrixf( m, mt );
    __indirect_glMultMatrixf( mt );
 }
+#endif /*GLX_INDIRECT_RENDER*/
diff --git a/src/glx/x11/indirect_vertex_array.c b/src/glx/x11/indirect_vertex_array.c
index 4f82845..e5ee718 100644
--- a/src/glx/x11/indirect_vertex_array.c
+++ b/src/glx/x11/indirect_vertex_array.c
@@ -23,6 +23,7 @@
  * SOFTWARE.
  */
 
+#ifdef GLX_INDERECT_RENDERING
 #include <inttypes.h>
 #include <assert.h>
 #include <string.h>
@@ -1845,3 +1846,4 @@ __glXPopArrayState( __GLXattribute * state )
     arrays->active_texture_unit = 
       arrays->active_texture_unit_stack[ arrays->stack_index ];
 }
+#endif /* GLX_INDERECT_RENDERING */
diff --git a/src/glx/x11/indirect_vertex_program.c b/src/glx/x11/indirect_vertex_program.c
index 5383dcc..d51d9bf 100644
--- a/src/glx/x11/indirect_vertex_program.c
+++ b/src/glx/x11/indirect_vertex_program.c
@@ -23,6 +23,8 @@
  * SOFTWARE.
  */
 
+#ifdef GLX_INDIRECT_RENDER
+
 #include <inttypes.h>
 #include <GL/gl.h>
 #include "indirect.h"
@@ -271,3 +273,5 @@ void __indirect_glGetVertexAttribdvARB( GLuint index, GLenum pname,
     UnlockDisplay(dpy);
     SyncHandle();
 }
+
+#endif /*GLX_INDIRECT_RENDER*/
diff --git a/src/glx/x11/indirect_window_pos.c b/src/glx/x11/indirect_window_pos.c
index 533f8ef..2f6b5d5 100644
--- a/src/glx/x11/indirect_window_pos.c
+++ b/src/glx/x11/indirect_window_pos.c
@@ -24,6 +24,7 @@
  * SOFTWARE.
  */
 
+#ifdef GLX_INDIRECT_RENDER
 #include <GL/gl.h>
 #include "indirect.h"
 
@@ -96,3 +97,4 @@ void __indirect_glWindowPos3svMESA(const GLshort * p)
 {
 	__indirect_glWindowPos3fMESA(p[0], p[1], p[2]);
 }
+#endif /*GLX_INDIRECT_RENDER*/
diff --git a/src/glx/x11/pixel.c b/src/glx/x11/pixel.c
index 3b3a181..adb1444 100644
--- a/src/glx/x11/pixel.c
+++ b/src/glx/x11/pixel.c
@@ -157,6 +157,7 @@ static void FillBitmap(__GLXcontext *gc, GLint width, GLint height,
     }
 }
 
+#ifndef GLX_USE_APPLEGL
 /*
 ** Extract array from user's data applying all pixel store modes.
 ** The internal packed array format used has LSB_FIRST = FALSE and 
@@ -257,6 +258,7 @@ void __glFillImage(__GLXcontext *gc, GLint dim, GLint width, GLint height,
        }
     }
 }
+#endif
 
 /*
 ** Empty a bitmap in LSB_FIRST=GL_FALSE and ALIGNMENT=4 format packing it
diff --git a/src/glx/x11/pixelstore.c b/src/glx/x11/pixelstore.c
index 3bf1b35..375e58a 100644
--- a/src/glx/x11/pixelstore.c
+++ b/src/glx/x11/pixelstore.c
@@ -37,6 +37,7 @@
 #include "glxclient.h"
 #include "indirect.h"
 
+#ifdef GLX_INDIRECT_RENDERING
 /**
  * Send glPixelStore command to the server
  * 
@@ -335,3 +336,4 @@ void __indirect_glPixelStorei(GLenum pname, GLint param)
 	break;
     }
 }
+#endif /* GLX_INDIRECT_RENDERING */
diff --git a/src/glx/x11/render2.c b/src/glx/x11/render2.c
index 21ba270..ae358e3 100644
--- a/src/glx/x11/render2.c
+++ b/src/glx/x11/render2.c
@@ -44,6 +44,7 @@
 ** use the pixel header.  See renderpix.c for those routines.
 */
 
+#ifdef GLX_INDIRECT_RENDERING
 void __indirect_glMap1d(GLenum target, GLdouble u1, GLdouble u2, GLint stride,
 	     GLint order, const GLdouble *pnts)
 {
@@ -361,3 +362,4 @@ void __indirect_glDisable(GLenum cap)
     __GLX_PUT_LONG(4,cap);
     __GLX_END(8);
 }
+#endif /* GLX_INDIRECT_RENDERING */
diff --git a/src/glx/x11/renderpix.c b/src/glx/x11/renderpix.c
index b7d01dc..c8ef065 100644
--- a/src/glx/x11/renderpix.c
+++ b/src/glx/x11/renderpix.c
@@ -62,6 +62,7 @@
 #include "packrender.h"
 #include "indirect.h"
 
+#ifdef GLX_INDIRECT_RENDERING
 /**
  * Send a large image to the server.  If necessary, a buffer is allocated
  * to hold the unpacked data that is copied from the clients memory.
@@ -213,3 +214,4 @@ void __indirect_glSeparableFilter2D(GLenum target, GLenum internalformat,
 	}
     }
 }
+#endif /* GLX_INDIRECT_RENDERING */
diff --git a/src/glx/x11/single2.c b/src/glx/x11/single2.c
index d535757..6b8ca15 100644
--- a/src/glx/x11/single2.c
+++ b/src/glx/x11/single2.c
@@ -115,6 +115,7 @@ RemapTransposeEnum( GLenum e )
 }
 
 
+#ifdef GLX_INDIRECT_RENDERING
 GLenum __indirect_glGetError(void)
 {
     __GLX_SINGLE_DECLARE_VARIABLES();
@@ -136,6 +137,7 @@ GLenum __indirect_glGetError(void)
 
     return retval;
 }
+#endif
 
 
 /**
@@ -302,6 +304,7 @@ get_client_data( __GLXcontext * gc, GLenum cap, GLintptr * data )
 }
 
 
+#ifdef GLX_INDIRECT_RENDERING
 void __indirect_glGetBooleanv(GLenum val, GLboolean *b)
 {
     const GLenum origVal = val;
@@ -587,6 +590,7 @@ void __indirect_glFinish(void)
     __GLX_SINGLE_READ_XREPLY();
     __GLX_SINGLE_END();
 }
+#endif /*GLX_INDERIECT_RENDERING*/
 
 
 /**
@@ -606,7 +610,7 @@ version_from_string( const char * ver,
     *minor_version = minor;
 }
 
-
+#ifdef GLX_INDIRECT_RENDERING
 const GLubyte *__indirect_glGetString(GLenum name)
 {
     __GLXcontext *gc = __glXGetCurrentContext();
@@ -830,4 +834,5 @@ void __indirect_glGetPointerv(GLenum pname, void **params)
 	return;
     }
 }
+#endif /*GLX_INDIRECT_RENDERING*/
 
diff --git a/src/glx/x11/singlepix.c b/src/glx/x11/singlepix.c
index a7b5b79..ab6ffe6 100644
--- a/src/glx/x11/singlepix.c
+++ b/src/glx/x11/singlepix.c
@@ -110,6 +110,7 @@ void __indirect_glGetSeparableFilter(GLenum target, GLenum format, GLenum type,
 }
 
 
+#ifndef GLX_USE_APPLEGL
 #define CONCAT(a,b) a ## b
 #define NAME(o) CONCAT(gl_dispatch_stub_, o)
 
@@ -190,3 +191,4 @@ void NAME(_gloffset_GetSeparableFilter)(GLenum target, GLenum format, GLenum typ
 	}
     }
 }
+#endif
diff --git a/src/glx/x11/vertarr.c b/src/glx/x11/vertarr.c
index 483a166..a56404b 100644
--- a/src/glx/x11/vertarr.c
+++ b/src/glx/x11/vertarr.c
@@ -41,6 +41,7 @@
 
 /*****************************************************************************/
 
+#ifdef GLX_INDIRECT_RENDERING
 /**
  * \name Vertex array pointer bridge functions
  *
@@ -201,3 +202,5 @@ void __indirect_glInterleavedArrays(GLenum format, GLsizei stride, const GLvoid
 				trueStride, 
 				(const char *)pointer + offsets[3] );
 }
+#endif /* GLX_INDIRECT_RENDERING */
+
diff --git a/src/mesa/glapi/glapitable.h b/src/mesa/glapi/glapitable.h
index 48941f5..878cf8e 100644
--- a/src/mesa/glapi/glapitable.h
+++ b/src/mesa/glapi/glapitable.h
@@ -809,8 +809,10 @@ struct _glapi_table
    void (GLAPIENTRYP StencilFuncSeparateATI)(GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask); /* 765 */
    void (GLAPIENTRYP ProgramEnvParameters4fvEXT)(GLenum target, GLuint index, GLsizei count, const GLfloat * params); /* 766 */
    void (GLAPIENTRYP ProgramLocalParameters4fvEXT)(GLenum target, GLuint index, GLsizei count, const GLfloat * params); /* 767 */
+#ifndef GLX_USE_APPLEGL
    void (GLAPIENTRYP GetQueryObjecti64vEXT)(GLuint id, GLenum pname, GLint64EXT * params); /* 768 */
    void (GLAPIENTRYP GetQueryObjectui64vEXT)(GLuint id, GLenum pname, GLuint64EXT * params); /* 769 */
+#endif
 };
 
 #endif /* !defined( _GLAPI_TABLE_H_ ) */
diff --git a/src/mesa/glapi/glapitemp.h b/src/mesa/glapi/glapitemp.h
index 09259f4..f251c39 100644
--- a/src/mesa/glapi/glapitemp.h
+++ b/src/mesa/glapi/glapitemp.h
@@ -5474,6 +5474,7 @@ KEYWORD1_ALT void KEYWORD2 NAME(_dispatch_stub_767)(GLenum target, GLuint index,
    DISPATCH(ProgramLocalParameters4fvEXT, (target, index, count, params), (F, "glProgramLocalParameters4fvEXT(0x%x, %d, %d, %p);\n", target, index, count, (const void *) params));
 }
 
+#ifndef GLX_USE_APPLEGL
 KEYWORD1_ALT void KEYWORD2 NAME(_dispatch_stub_768)(GLuint id, GLenum pname, GLint64EXT * params);
 
 KEYWORD1_ALT void KEYWORD2 NAME(_dispatch_stub_768)(GLuint id, GLenum pname, GLint64EXT * params)
@@ -5487,6 +5488,7 @@ KEYWORD1_ALT void KEYWORD2 NAME(_dispatch_stub_769)(GLuint id, GLenum pname, GLu
 {
    DISPATCH(GetQueryObjectui64vEXT, (id, pname, params), (F, "glGetQueryObjectui64vEXT(%d, 0x%x, %p);\n", id, pname, (const void *) params));
 }
+#endif
 
 
 #endif /* defined( NAME ) */
@@ -6270,8 +6272,10 @@ static _glapi_proc DISPATCH_TABLE_NAME[] = {
    TABLE_ENTRY(_dispatch_stub_765),
    TABLE_ENTRY(_dispatch_stub_766),
    TABLE_ENTRY(_dispatch_stub_767),
+#ifndef GLX_USE_APPLEGL
    TABLE_ENTRY(_dispatch_stub_768),
    TABLE_ENTRY(_dispatch_stub_769),
+#endif
    /* A whole bunch of no-op functions.  These might be called
     * when someone tries to call a dynamically-registered
     * extension function without a current rendering context.
diff --git a/src/mesa/glapi/glprocs.h b/src/mesa/glapi/glprocs.h
index 5a99d50..712a43f 100644
--- a/src/mesa/glapi/glprocs.h
+++ b/src/mesa/glapi/glprocs.h
@@ -1767,6 +1767,7 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET( 8911, glGetAttribLocationARB, glGetAttribLocationARB, NULL, _gloffset_GetAttribLocationARB),
     NAME_FUNC_OFFSET( 8934, glDrawBuffersARB, glDrawBuffersARB, NULL, _gloffset_DrawBuffersARB),
     NAME_FUNC_OFFSET( 8951, glPolygonOffsetEXT, glPolygonOffsetEXT, NULL, _gloffset_PolygonOffsetEXT),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET( 8970, gl_dispatch_stub_562, gl_dispatch_stub_562, NULL, _gloffset_GetPixelTexGenParameterfvSGIS),
     NAME_FUNC_OFFSET( 9002, gl_dispatch_stub_563, gl_dispatch_stub_563, NULL, _gloffset_GetPixelTexGenParameterivSGIS),
     NAME_FUNC_OFFSET( 9034, gl_dispatch_stub_564, gl_dispatch_stub_564, NULL, _gloffset_PixelTexGenParameterfSGIS),
@@ -1775,6 +1776,7 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET( 9119, gl_dispatch_stub_567, gl_dispatch_stub_567, NULL, _gloffset_PixelTexGenParameterivSGIS),
     NAME_FUNC_OFFSET( 9148, gl_dispatch_stub_568, gl_dispatch_stub_568, NULL, _gloffset_SampleMaskSGIS),
     NAME_FUNC_OFFSET( 9165, gl_dispatch_stub_569, gl_dispatch_stub_569, NULL, _gloffset_SamplePatternSGIS),
+#endif
     NAME_FUNC_OFFSET( 9185, glColorPointerEXT, glColorPointerEXT, NULL, _gloffset_ColorPointerEXT),
     NAME_FUNC_OFFSET( 9203, glEdgeFlagPointerEXT, glEdgeFlagPointerEXT, NULL, _gloffset_EdgeFlagPointerEXT),
     NAME_FUNC_OFFSET( 9224, glIndexPointerEXT, glIndexPointerEXT, NULL, _gloffset_IndexPointerEXT),
@@ -1785,8 +1787,10 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET( 9322, glPointParameterfvEXT, glPointParameterfvEXT, NULL, _gloffset_PointParameterfvEXT),
     NAME_FUNC_OFFSET( 9344, glLockArraysEXT, glLockArraysEXT, NULL, _gloffset_LockArraysEXT),
     NAME_FUNC_OFFSET( 9360, glUnlockArraysEXT, glUnlockArraysEXT, NULL, _gloffset_UnlockArraysEXT),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET( 9378, gl_dispatch_stub_580, gl_dispatch_stub_580, NULL, _gloffset_CullParameterdvEXT),
     NAME_FUNC_OFFSET( 9399, gl_dispatch_stub_581, gl_dispatch_stub_581, NULL, _gloffset_CullParameterfvEXT),
+#endif
     NAME_FUNC_OFFSET( 9420, glSecondaryColor3bEXT, glSecondaryColor3bEXT, NULL, _gloffset_SecondaryColor3bEXT),
     NAME_FUNC_OFFSET( 9442, glSecondaryColor3bvEXT, glSecondaryColor3bvEXT, NULL, _gloffset_SecondaryColor3bvEXT),
     NAME_FUNC_OFFSET( 9465, glSecondaryColor3dEXT, glSecondaryColor3dEXT, NULL, _gloffset_SecondaryColor3dEXT),
@@ -1811,7 +1815,9 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET( 9893, glFogCoorddvEXT, glFogCoorddvEXT, NULL, _gloffset_FogCoorddvEXT),
     NAME_FUNC_OFFSET( 9909, glFogCoordfEXT, glFogCoordfEXT, NULL, _gloffset_FogCoordfEXT),
     NAME_FUNC_OFFSET( 9924, glFogCoordfvEXT, glFogCoordfvEXT, NULL, _gloffset_FogCoordfvEXT),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET( 9940, gl_dispatch_stub_606, gl_dispatch_stub_606, NULL, _gloffset_PixelTexGenSGIX),
+#endif
     NAME_FUNC_OFFSET( 9958, glBlendFuncSeparateEXT, glBlendFuncSeparateEXT, NULL, _gloffset_BlendFuncSeparateEXT),
     NAME_FUNC_OFFSET( 9981, glFlushVertexArrayRangeNV, glFlushVertexArrayRangeNV, NULL, _gloffset_FlushVertexArrayRangeNV),
     NAME_FUNC_OFFSET(10007, glVertexArrayRangeNV, glVertexArrayRangeNV, NULL, _gloffset_VertexArrayRangeNV),
@@ -1853,6 +1859,7 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET(10794, glWindowPos4ivMESA, glWindowPos4ivMESA, NULL, _gloffset_WindowPos4ivMESA),
     NAME_FUNC_OFFSET(10813, glWindowPos4sMESA, glWindowPos4sMESA, NULL, _gloffset_WindowPos4sMESA),
     NAME_FUNC_OFFSET(10831, glWindowPos4svMESA, glWindowPos4svMESA, NULL, _gloffset_WindowPos4svMESA),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET(10850, gl_dispatch_stub_648, gl_dispatch_stub_648, NULL, _gloffset_MultiModeDrawArraysIBM),
     NAME_FUNC_OFFSET(10875, gl_dispatch_stub_649, gl_dispatch_stub_649, NULL, _gloffset_MultiModeDrawElementsIBM),
     NAME_FUNC_OFFSET(10902, gl_dispatch_stub_650, gl_dispatch_stub_650, NULL, _gloffset_DeleteFencesNV),
@@ -1862,6 +1869,7 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET(10964, gl_dispatch_stub_654, gl_dispatch_stub_654, NULL, _gloffset_IsFenceNV),
     NAME_FUNC_OFFSET(10976, gl_dispatch_stub_655, gl_dispatch_stub_655, NULL, _gloffset_SetFenceNV),
     NAME_FUNC_OFFSET(10989, gl_dispatch_stub_656, gl_dispatch_stub_656, NULL, _gloffset_TestFenceNV),
+#endif
     NAME_FUNC_OFFSET(11003, glAreProgramsResidentNV, glAreProgramsResidentNV, NULL, _gloffset_AreProgramsResidentNV),
     NAME_FUNC_OFFSET(11027, glBindProgramNV, glBindProgramNV, NULL, _gloffset_BindProgramNV),
     NAME_FUNC_OFFSET(11043, glDeleteProgramsNV, glDeleteProgramsNV, NULL, _gloffset_DeleteProgramsNV),
@@ -1938,19 +1946,23 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET(12520, glSetFragmentShaderConstantATI, glSetFragmentShaderConstantATI, NULL, _gloffset_SetFragmentShaderConstantATI),
     NAME_FUNC_OFFSET(12551, glPointParameteriNV, glPointParameteriNV, NULL, _gloffset_PointParameteriNV),
     NAME_FUNC_OFFSET(12571, glPointParameterivNV, glPointParameterivNV, NULL, _gloffset_PointParameterivNV),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET(12592, gl_dispatch_stub_733, gl_dispatch_stub_733, NULL, _gloffset_ActiveStencilFaceEXT),
     NAME_FUNC_OFFSET(12615, gl_dispatch_stub_734, gl_dispatch_stub_734, NULL, _gloffset_BindVertexArrayAPPLE),
     NAME_FUNC_OFFSET(12638, gl_dispatch_stub_735, gl_dispatch_stub_735, NULL, _gloffset_DeleteVertexArraysAPPLE),
     NAME_FUNC_OFFSET(12664, gl_dispatch_stub_736, gl_dispatch_stub_736, NULL, _gloffset_GenVertexArraysAPPLE),
     NAME_FUNC_OFFSET(12687, gl_dispatch_stub_737, gl_dispatch_stub_737, NULL, _gloffset_IsVertexArrayAPPLE),
+#endif
     NAME_FUNC_OFFSET(12708, glGetProgramNamedParameterdvNV, glGetProgramNamedParameterdvNV, NULL, _gloffset_GetProgramNamedParameterdvNV),
     NAME_FUNC_OFFSET(12739, glGetProgramNamedParameterfvNV, glGetProgramNamedParameterfvNV, NULL, _gloffset_GetProgramNamedParameterfvNV),
     NAME_FUNC_OFFSET(12770, glProgramNamedParameter4dNV, glProgramNamedParameter4dNV, NULL, _gloffset_ProgramNamedParameter4dNV),
     NAME_FUNC_OFFSET(12798, glProgramNamedParameter4dvNV, glProgramNamedParameter4dvNV, NULL, _gloffset_ProgramNamedParameter4dvNV),
     NAME_FUNC_OFFSET(12827, glProgramNamedParameter4fNV, glProgramNamedParameter4fNV, NULL, _gloffset_ProgramNamedParameter4fNV),
     NAME_FUNC_OFFSET(12855, glProgramNamedParameter4fvNV, glProgramNamedParameter4fvNV, NULL, _gloffset_ProgramNamedParameter4fvNV),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET(12884, gl_dispatch_stub_744, gl_dispatch_stub_744, NULL, _gloffset_DepthBoundsEXT),
     NAME_FUNC_OFFSET(12901, gl_dispatch_stub_745, gl_dispatch_stub_745, NULL, _gloffset_BlendEquationSeparateEXT),
+#endif
     NAME_FUNC_OFFSET(12928, glBindFramebufferEXT, glBindFramebufferEXT, NULL, _gloffset_BindFramebufferEXT),
     NAME_FUNC_OFFSET(12949, glBindRenderbufferEXT, glBindRenderbufferEXT, NULL, _gloffset_BindRenderbufferEXT),
     NAME_FUNC_OFFSET(12971, glCheckFramebufferStatusEXT, glCheckFramebufferStatusEXT, NULL, _gloffset_CheckFramebufferStatusEXT),
@@ -1968,13 +1980,17 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET(13291, glIsFramebufferEXT, glIsFramebufferEXT, NULL, _gloffset_IsFramebufferEXT),
     NAME_FUNC_OFFSET(13310, glIsRenderbufferEXT, glIsRenderbufferEXT, NULL, _gloffset_IsRenderbufferEXT),
     NAME_FUNC_OFFSET(13330, glRenderbufferStorageEXT, glRenderbufferStorageEXT, NULL, _gloffset_RenderbufferStorageEXT),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET(13355, gl_dispatch_stub_763, gl_dispatch_stub_763, NULL, _gloffset_BlitFramebufferEXT),
+#endif
     NAME_FUNC_OFFSET(13376, glFramebufferTextureLayerEXT, glFramebufferTextureLayerEXT, NULL, _gloffset_FramebufferTextureLayerEXT),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET(13405, gl_dispatch_stub_765, gl_dispatch_stub_765, NULL, _gloffset_StencilFuncSeparateATI),
     NAME_FUNC_OFFSET(13430, gl_dispatch_stub_766, gl_dispatch_stub_766, NULL, _gloffset_ProgramEnvParameters4fvEXT),
     NAME_FUNC_OFFSET(13459, gl_dispatch_stub_767, gl_dispatch_stub_767, NULL, _gloffset_ProgramLocalParameters4fvEXT),
     NAME_FUNC_OFFSET(13490, gl_dispatch_stub_768, gl_dispatch_stub_768, NULL, _gloffset_GetQueryObjecti64vEXT),
     NAME_FUNC_OFFSET(13514, gl_dispatch_stub_769, gl_dispatch_stub_769, NULL, _gloffset_GetQueryObjectui64vEXT),
+#endif
     NAME_FUNC_OFFSET(13539, glArrayElement, glArrayElement, NULL, _gloffset_ArrayElement),
     NAME_FUNC_OFFSET(13557, glBindTexture, glBindTexture, NULL, _gloffset_BindTexture),
     NAME_FUNC_OFFSET(13574, glDrawArrays, glDrawArrays, NULL, _gloffset_DrawArrays),
@@ -2178,8 +2194,10 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET(17370, glGetAttribLocationARB, glGetAttribLocationARB, NULL, _gloffset_GetAttribLocationARB),
     NAME_FUNC_OFFSET(17390, glDrawBuffersARB, glDrawBuffersARB, NULL, _gloffset_DrawBuffersARB),
     NAME_FUNC_OFFSET(17404, glDrawBuffersARB, glDrawBuffersARB, NULL, _gloffset_DrawBuffersARB),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET(17421, gl_dispatch_stub_568, gl_dispatch_stub_568, NULL, _gloffset_SampleMaskSGIS),
     NAME_FUNC_OFFSET(17437, gl_dispatch_stub_569, gl_dispatch_stub_569, NULL, _gloffset_SamplePatternSGIS),
+#endif
     NAME_FUNC_OFFSET(17456, glPointParameterfEXT, glPointParameterfEXT, NULL, _gloffset_PointParameterfEXT),
     NAME_FUNC_OFFSET(17474, glPointParameterfEXT, glPointParameterfEXT, NULL, _gloffset_PointParameterfEXT),
     NAME_FUNC_OFFSET(17495, glPointParameterfEXT, glPointParameterfEXT, NULL, _gloffset_PointParameterfEXT),
@@ -2252,8 +2270,10 @@ static const glprocs_table_t static_functions[] = {
     NAME_FUNC_OFFSET(18694, glIsProgramNV, glIsProgramNV, NULL, _gloffset_IsProgramNV),
     NAME_FUNC_OFFSET(18709, glPointParameteriNV, glPointParameteriNV, NULL, _gloffset_PointParameteriNV),
     NAME_FUNC_OFFSET(18727, glPointParameterivNV, glPointParameterivNV, NULL, _gloffset_PointParameterivNV),
+#ifndef GLX_USE_APPLEGL
     NAME_FUNC_OFFSET(18746, gl_dispatch_stub_745, gl_dispatch_stub_745, NULL, _gloffset_BlendEquationSeparateEXT),
     NAME_FUNC_OFFSET(18770, gl_dispatch_stub_745, gl_dispatch_stub_745, NULL, _gloffset_BlendEquationSeparateEXT),
+#endif
     NAME_FUNC_OFFSET(-1, NULL, NULL, NULL, 0)
 };
 
