Changed escaping of double quotes.
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
index c99708b83d7bdf14b0d69b88880c3570b5f7d172..302994e0c23a9417fe0d48dace5177c0ecfc47b5 100644 (file)
  */
 
 
-#ifdef GLX_DIRECT_RENDERING
-
-#include <inttypes.h>
 #include <assert.h>
 #include <stdarg.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include <X11/Xlibint.h>
-#include <Xext.h>
-#include <extutil.h>
 #include <stdio.h>
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
+#ifndef DRI_NEW_INTERFACE_ONLY
+# include <X11/Xlibint.h>
+# include <Xext.h>
+# include <extutil.h>
+# include "xf86dri.h"
+# define _mesa_malloc(b) Xmalloc(b)
+# define _mesa_free(m) Xfree(m)
+#else
+# include "imports.h"
+# define None 0
+#endif /* DRI_NEW_INTERFACE_ONLY */
+
 #include "dri_util.h"
-#include "xf86dri.h"
-#include "sarea.h"
+#include "drm_sarea.h"
 #include "glcontextmodes.h"
 
-/*#define DRI_NEW_INTERFACE_ONLY*/
-
-#ifndef GLX_OML_sync_control
-typedef Bool ( * PFNGLXGETMSCRATEOMLPROC) (Display *dpy, GLXDrawable drawable, int32_t *numerator, int32_t *denominator);
+#ifndef PFNGLXGETMSCRATEOMLPROC
+typedef GLboolean ( * PFNGLXGETMSCRATEOMLPROC) (__DRInativeDisplay *dpy, __DRIid drawable, int32_t *numerator, int32_t *denominator);
 #endif
 
+/**
+ * Weak thread-safety dispatch pointer.  Older versions of libGL will not have
+ * this symbol, so a "weak" version is included here so that the driver will
+ * dynamically link properly.  The value is set to \c NULL.  This forces the
+ * driver to fall back to the old dispatch interface.
+ */
+struct _glapi_table *_glapi_DispatchTSD __attribute__((weak)) = NULL;
+
 /**
  * This is used in a couple of places that call \c driCreateNewDrawable.
  */
@@ -61,8 +77,8 @@ static const int empty_attribute_list[1] = { None };
  */
 static PFNGLXWINDOWEXISTSPROC window_exists;
 
-typedef Bool (*PFNGLXCREATECONTEXTWITHCONFIGPROC)( Display*, int, int, void *,
-    drmContextPtr );
+typedef GLboolean (*PFNGLXCREATECONTEXTWITHCONFIGPROC)( __DRInativeDisplay*, int, int, void *,
+    drm_context_t * );
 
 static PFNGLXCREATECONTEXTWITHCONFIGPROC create_context_with_config;
 
@@ -73,20 +89,20 @@ static PFNGLXCREATECONTEXTWITHCONFIGPROC create_context_with_config;
 static int api_ver = 0;
 
 /* forward declarations */
-static int driQueryFrameTracking( Display * dpy, void * priv,
+static int driQueryFrameTracking( __DRInativeDisplay * dpy, void * priv,
     int64_t * sbc, int64_t * missedFrames, float * lastMissedUsage,
     float * usage );
 
-static void *driCreateNewDrawable(Display *dpy, const __GLcontextModes *modes,
-    GLXDrawable draw, __DRIdrawable *pdraw, int renderType, const int *attrs);
+static void *driCreateNewDrawable(__DRInativeDisplay *dpy, const __GLcontextModes *modes,
+    __DRIid draw, __DRIdrawable *pdraw, int renderType, const int *attrs);
 
-static void driDestroyDrawable(Display *dpy, void *drawablePrivate);
+static void driDestroyDrawable(__DRInativeDisplay *dpy, void *drawablePrivate);
 
 
 
 
 #ifdef not_defined
-static Bool driFeatureOn(const char *name)
+static GLboolean driFeatureOn(const char *name)
 {
     char *env = getenv(name);
 
@@ -126,6 +142,26 @@ __driUtilMessage(const char *f, ...)
     }
 }
 
+/*
+ * fd.o bug #1713: Some rare libGL's have __glXFindDRIScreen defined but do not
+ * export it via glXGetProcAddress.  These are not supported anymore, so print
+ * an error message to that effect.  - ajax 2004-10-26
+ */
+typedef __DRIscreen *(*PFNGLXFINDDRISCREEN)(__DRInativeDisplay *, int);
+
+static __DRIscreen *glx_find_dri_screen(__DRInativeDisplay *d, int i)
+{
+    PFNGLXFINDDRISCREEN findscreen = 
+        (PFNGLXFINDDRISCREEN)glXGetProcAddress("__glXFindDRIScreen");
+
+    if (!findscreen)
+    {
+        __driUtilMessage("glXGetProcAddress(\"__glXFindDRIScreen\") failed!");
+        __driUtilMessage("Your libGL is too old, please upgrade.");
+        return NULL;
+    }
+    else return findscreen(d, i);
+}
 
 /*****************************************************************/
 /** \name Visual utility functions                               */
@@ -144,7 +180,7 @@ __driUtilMessage(const char *f, ...)
  *          if found, or \c NULL if no match is found.
  */
 static const __GLcontextModes *
-findConfigMode(Display *dpy, int scrn, VisualID vid, 
+findConfigMode(__DRInativeDisplay *dpy, int scrn, VisualID vid, 
               const __DRIscreen * pDRIScreen)
 {
     if ( (pDRIScreen != NULL) && (pDRIScreen->private != NULL) ) {
@@ -165,9 +201,9 @@ findConfigMode(Display *dpy, int scrn, VisualID vid,
  * drivers that are built to work without XFree86) shouldn't have to know
  * about X structures like a \c Visual.
  */
-static Bool
-fake_XF86DRICreateContextWithConfig( Display* dpy, int screen, int configID,
-                                    XID* context, drmContextPtr hHWContext )
+static GLboolean
+fake_XF86DRICreateContextWithConfig( __DRInativeDisplay* dpy, int screen, int configID,
+                                    XID* context, drm_context_t * hHWContext )
 {
     Visual  vis;
     
@@ -184,7 +220,7 @@ fake_XF86DRICreateContextWithConfig( Display* dpy, int screen, int configID,
 /*****************************************************************/
 /*@{*/
 
-static Bool __driAddDrawable(void *drawHash, __DRIdrawable *pdraw)
+static GLboolean __driAddDrawable(void *drawHash, __DRIdrawable *pdraw)
 {
     __DRIdrawablePrivate *pdp = (__DRIdrawablePrivate *)pdraw->private;
 
@@ -194,31 +230,20 @@ static Bool __driAddDrawable(void *drawHash, __DRIdrawable *pdraw)
     return GL_TRUE;
 }
 
-static __DRIdrawable *__driFindDrawable(void *drawHash, GLXDrawable draw)
+static __DRIdrawable *__driFindDrawable(void *drawHash, __DRIid draw)
 {
     int retcode;
     __DRIdrawable *pdraw;
 
-    retcode = drmHashLookup(drawHash, draw, (void **)&pdraw);
+    retcode = drmHashLookup(drawHash, draw, (void *)&pdraw);
     if (retcode)
        return NULL;
 
     return pdraw;
 }
 
-static void __driRemoveDrawable(void *drawHash, __DRIdrawable *pdraw)
-{
-    int retcode;
-    __DRIdrawablePrivate *pdp = (__DRIdrawablePrivate *)pdraw->private;
-
-    retcode = drmHashLookup(drawHash, pdp->draw, (void **)&pdraw);
-    if (!retcode) { /* Found */
-       drmHashDelete(drawHash, pdp->draw);
-    }
-}
-
 #ifndef DRI_NEW_INTERFACE_ONLY
-static Bool __driWindowExistsFlag;
+static GLboolean __driWindowExistsFlag;
 
 static int __driWindowExistsErrorHandler(Display *dpy, XErrorEvent *xerr)
 {
@@ -248,7 +273,7 @@ static int __driWindowExistsErrorHandler(Display *dpy, XErrorEvent *xerr)
  * 
  * \sa __glXWindowExists glXGetProcAddress window_exists
  */
-static Bool __driWindowExists(Display *dpy, GLXDrawable draw)
+static GLboolean __driWindowExists(Display *dpy, GLXDrawable draw)
 {
     XWindowAttributes xwa;
     int (*oldXErrorHandler)(Display *, XErrorEvent *);
@@ -270,22 +295,22 @@ static Bool __driWindowExists(Display *dpy, GLXDrawable draw)
  */
 static void __driGarbageCollectDrawables(void *drawHash)
 {
-    GLXDrawable draw;
+    __DRIid draw;
+    __DRInativeDisplay *dpy;
     __DRIdrawable *pdraw;
-    Display *dpy;
 
-    if (drmHashFirst(drawHash, &draw, (void **)&pdraw)) {
+    if (drmHashFirst(drawHash, &draw, (void *)&pdraw) == 1) {
        do {
            __DRIdrawablePrivate *pdp = (__DRIdrawablePrivate *)pdraw->private;
            dpy = pdp->driScreenPriv->display;
            if (! (*window_exists)(dpy, draw)) {
                /* Destroy the local drawable data in the hash table, if the
                   drawable no longer exists in the Xserver */
-               __driRemoveDrawable(drawHash, pdraw);
+               drmHashDelete(drawHash, draw);
                (*pdraw->destroyDrawable)(dpy, pdraw->private);
-               Xfree(pdraw);
+               _mesa_free(pdraw);
            }
-       } while (drmHashNext(drawHash, &draw, (void **)&pdraw));
+       } while (drmHashNext(drawHash, &draw, (void *)&pdraw) == 1);
     }
 }
 
@@ -316,8 +341,8 @@ static void __driGarbageCollectDrawables(void *drawHash)
  * While casting the opaque private pointers associated with the parameters
  * into their respective real types it also assures they are not \c NULL. 
  */
-static Bool driUnbindContext3(Display *dpy, int scrn,
-                             GLXDrawable draw, GLXDrawable read,
+static GLboolean driUnbindContext3(__DRInativeDisplay *dpy, int scrn,
+                             __DRIid draw, __DRIid read,
                              __DRIcontext *ctx)
 {
     __DRIscreen *pDRIScreen;
@@ -338,7 +363,7 @@ static Bool driUnbindContext3(Display *dpy, int scrn,
        return GL_FALSE;
     }
 
-    pDRIScreen = __glXFindDRIScreen(dpy, scrn);
+    pDRIScreen = glx_find_dri_screen(dpy, scrn);
     if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        /* ERROR!!! */
        return GL_FALSE;
@@ -407,8 +432,8 @@ static Bool driUnbindContext3(Display *dpy, int scrn,
  *      be needed in those places when support for pbuffers and / or pixmaps
  *      is added.  Is it safe to assume that the drawable is a window?
  */
-static Bool DoBindContext(Display *dpy,
-                         GLXDrawable draw, GLXDrawable read,
+static GLboolean DoBindContext(__DRInativeDisplay *dpy,
+                         __DRIid draw, __DRIid read,
                          __DRIcontext *ctx, const __GLcontextModes * modes,
                          __DRIscreenPrivate *psp)
 {
@@ -419,11 +444,11 @@ static Bool DoBindContext(Display *dpy,
     __DRIcontextPrivate * const pcp = ctx->private;
 
 
-    /* Find the _DRIdrawable which corresponds to the writing GLXDrawable */
+    /* Find the _DRIdrawable which corresponds to the writing drawable. */
     pdraw = __driFindDrawable(psp->drawHash, draw);
     if (!pdraw) {
        /* Allocate a new drawable */
-       pdraw = (__DRIdrawable *)Xmalloc(sizeof(__DRIdrawable));
+       pdraw = (__DRIdrawable *)_mesa_malloc(sizeof(__DRIdrawable));
        if (!pdraw) {
            /* ERROR!!! */
            return GL_FALSE;
@@ -434,14 +459,14 @@ static Bool DoBindContext(Display *dpy,
                             empty_attribute_list);
        if (!pdraw->private) {
            /* ERROR!!! */
-           Xfree(pdraw);
+           _mesa_free(pdraw);
            return GL_FALSE;
        }
 
     }
     pdp = (__DRIdrawablePrivate *) pdraw->private;
 
-    /* Find the _DRIdrawable which corresponds to the reading GLXDrawable */
+    /* Find the _DRIdrawable which corresponds to the reading drawable. */
     if (read == draw) {
         /* read buffer == draw buffer */
         prp = pdp;
@@ -450,7 +475,7 @@ static Bool DoBindContext(Display *dpy,
         pread = __driFindDrawable(psp->drawHash, read);
         if (!pread) {
             /* Allocate a new drawable */
-            pread = (__DRIdrawable *)Xmalloc(sizeof(__DRIdrawable));
+            pread = (__DRIdrawable *)_mesa_malloc(sizeof(__DRIdrawable));
             if (!pread) {
                 /* ERROR!!! */
                 return GL_FALSE;
@@ -461,7 +486,7 @@ static Bool DoBindContext(Display *dpy,
                                 empty_attribute_list);
             if (!pread->private) {
                 /* ERROR!!! */
-                Xfree(pread);
+                _mesa_free(pread);
                 return GL_FALSE;
             }
         }
@@ -498,8 +523,8 @@ static Bool DoBindContext(Display *dpy,
  * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
  * function.
  */
-static Bool driBindContext3(Display *dpy, int scrn,
-                            GLXDrawable draw, GLXDrawable read,
+static GLboolean driBindContext3(__DRInativeDisplay *dpy, int scrn,
+                            __DRIid draw, __DRIid read,
                             __DRIcontext * ctx)
 {
     __DRIscreen *pDRIScreen;
@@ -514,7 +539,7 @@ static Bool driBindContext3(Display *dpy, int scrn,
        return GL_FALSE;
     }
 
-    pDRIScreen = __glXFindDRIScreen(dpy, scrn);
+    pDRIScreen = glx_find_dri_screen(dpy, scrn);
     if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        /* ERROR!!! */
        return GL_FALSE;
@@ -531,7 +556,7 @@ static Bool driBindContext3(Display *dpy, int scrn,
  * for \c glXMakeCurrentReadSGI or GLX 1.3's \c glXMakeContextCurrent
  * function.
  */
-static Bool driBindContext2(Display *dpy, int scrn,
+static GLboolean driBindContext2(Display *dpy, int scrn,
                             GLXDrawable draw, GLXDrawable read,
                             GLXContext gc)
 {
@@ -548,7 +573,7 @@ static Bool driBindContext2(Display *dpy, int scrn,
        return GL_FALSE;
     }
 
-    pDRIScreen = __glXFindDRIScreen(dpy, scrn);
+    pDRIScreen = glx_find_dri_screen(dpy, scrn);
     modes = (driCompareGLXAPIVersion( 20040317 ) >= 0)
        ? gc->driContext.mode
        : findConfigMode( dpy, scrn, gc->vid, pDRIScreen );
@@ -567,7 +592,7 @@ static Bool driBindContext2(Display *dpy, int scrn,
                          (__DRIscreenPrivate *)pDRIScreen->private );
 }
 
-static Bool driUnbindContext2(Display *dpy, int scrn,
+static GLboolean driUnbindContext2(Display *dpy, int scrn,
                              GLXDrawable draw, GLXDrawable read,
                              GLXContext gc)
 {
@@ -577,7 +602,7 @@ static Bool driUnbindContext2(Display *dpy, int scrn,
 /*
  * Simply call bind with the same GLXDrawable for the read and draw buffers.
  */
-static Bool driBindContext(Display *dpy, int scrn,
+static GLboolean driBindContext(Display *dpy, int scrn,
                            GLXDrawable draw, GLXContext gc)
 {
     return driBindContext2(dpy, scrn, draw, draw, gc);
@@ -587,7 +612,7 @@ static Bool driBindContext(Display *dpy, int scrn,
 /*
  * Simply call bind with the same GLXDrawable for the read and draw buffers.
  */
-static Bool driUnbindContext(Display *dpy, int scrn,
+static GLboolean driUnbindContext(Display *dpy, int scrn,
                              GLXDrawable draw, GLXContext gc,
                              int will_rebind)
 {
@@ -634,11 +659,11 @@ __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
     }
 
     if (pdp->pClipRects) {
-       Xfree(pdp->pClipRects); 
+       _mesa_free(pdp->pClipRects); 
     }
 
     if (pdp->pBackClipRects) {
-       Xfree(pdp->pBackClipRects); 
+       _mesa_free(pdp->pBackClipRects); 
     }
 
     DRM_SPINUNLOCK(&psp->pSAREA->drawable_lock, psp->drawLockID);
@@ -686,7 +711,7 @@ __driUtilUpdateDrawableInfo(__DRIdrawablePrivate *pdp)
  * 
  * Is called directly from glXSwapBuffers().
  */
-static void driSwapBuffers( Display *dpy, void *drawablePrivate )
+static void driSwapBuffers( __DRInativeDisplay *dpy, void *drawablePrivate )
 {
     __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePrivate;
     dPriv->swapBuffers(dPriv);
@@ -706,7 +731,7 @@ static int driGetMSC( void *screenPrivate, int64_t *msc )
 /**
  * Called directly from a number of higher-level GLX functions.
  */
-static int driGetSBC( Display *dpy, void *drawablePrivate, int64_t *sbc )
+static int driGetSBC( __DRInativeDisplay *dpy, void *drawablePrivate, int64_t *sbc )
 {
    __DRIdrawablePrivate *dPriv = (__DRIdrawablePrivate *) drawablePrivate;
    __DRIswapInfo  sInfo;
@@ -719,7 +744,7 @@ static int driGetSBC( Display *dpy, void *drawablePrivate, int64_t *sbc )
    return status;
 }
 
-static int driWaitForSBC( Display * dpy, void *drawablePriv,
+static int driWaitForSBC( __DRInativeDisplay * dpy, void *drawablePriv,
                          int64_t target_sbc,
                          int64_t * msc, int64_t * sbc )
 {
@@ -729,7 +754,7 @@ static int driWaitForSBC( Display * dpy, void *drawablePriv,
                                                        msc, sbc );
 }
 
-static int driWaitForMSC( Display * dpy, void *drawablePriv,
+static int driWaitForMSC( __DRInativeDisplay * dpy, void *drawablePriv,
                          int64_t target_msc,
                          int64_t divisor, int64_t remainder,
                          int64_t * msc, int64_t * sbc )
@@ -756,7 +781,7 @@ static int driWaitForMSC( Display * dpy, void *drawablePriv,
     return status;
 }
 
-static int64_t driSwapBuffersMSC( Display * dpy, void *drawablePriv,
+static int64_t driSwapBuffersMSC( __DRInativeDisplay * dpy, void *drawablePriv,
                                  int64_t target_msc,
                                  int64_t divisor, int64_t remainder )
 {
@@ -771,18 +796,20 @@ static int64_t driSwapBuffersMSC( Display * dpy, void *drawablePriv,
 /**
  * This is called via __DRIscreenRec's createNewDrawable pointer.
  */
-static void *driCreateNewDrawable(Display *dpy,
+static void *driCreateNewDrawable(__DRInativeDisplay *dpy,
                                  const __GLcontextModes *modes,
-                                 GLXDrawable draw,
+                                 __DRIid draw,
                                  __DRIdrawable *pdraw,
                                  int renderType,
                                  const int *attrs)
 {
-    __DRIscreen * const pDRIScreen = __glXFindDRIScreen(dpy, modes->screen);
+    __DRIscreen * const pDRIScreen = glx_find_dri_screen(dpy, modes->screen);
     __DRIscreenPrivate *psp;
     __DRIdrawablePrivate *pdp;
 
 
+    pdraw->private = NULL;
+
     /* Since pbuffers are not yet supported, no drawable attributes are
      * supported either.
      */
@@ -792,13 +819,13 @@ static void *driCreateNewDrawable(Display *dpy,
        return NULL;
     }
 
-    pdp = (__DRIdrawablePrivate *)Xmalloc(sizeof(__DRIdrawablePrivate));
+    pdp = (__DRIdrawablePrivate *)_mesa_malloc(sizeof(__DRIdrawablePrivate));
     if (!pdp) {
        return NULL;
     }
 
     if (!XF86DRICreateDrawable(dpy, modes->screen, draw, &pdp->hHWDrawable)) {
-       Xfree(pdp);
+       _mesa_free(pdp);
        return NULL;
     }
 
@@ -823,16 +850,22 @@ static void *driCreateNewDrawable(Display *dpy,
     pdp->driScreenPriv = psp;
     pdp->driContextPriv = &psp->dummyContextPriv;
 
-    pdp->getInfo = (GetDrawableInfo *)
+    pdp->getInfo = (PFNGLXGETDRAWABLEINFOPROC)
        glXGetProcAddress( (const GLubyte *) "__glXGetDrawableInfo" );
     if ( pdp->getInfo == NULL ) {
-       pdp->getInfo = XF86DRIGetDrawableInfo;
+#ifdef DRI_NEW_INTERFACE_ONLY
+        (void)XF86DRIDestroyDrawable(dpy, modes->screen, pdp->draw);
+       _mesa_free(pdp);
+       return NULL;
+#else
+       pdp->getInfo = (PFNGLXGETDRAWABLEINFOPROC) XF86DRIGetDrawableInfo;
+#endif /* DRI_NEW_INTERFACE_ONLY */
     }
 
     if (!(*psp->DriverAPI.CreateBuffer)(psp, pdp, modes,
                                        renderType == GLX_PIXMAP_BIT)) {
        (void)XF86DRIDestroyDrawable(dpy, modes->screen, pdp->draw);
-       Xfree(pdp);
+       _mesa_free(pdp);
        return NULL;
     }
 
@@ -860,7 +893,7 @@ static void *driCreateNewDrawable(Display *dpy,
     if (!__driAddDrawable(psp->drawHash, pdraw)) {
        /* ERROR!!! */
        (*pdraw->destroyDrawable)(dpy, pdp);
-       Xfree(pdp);
+       _mesa_free(pdp);
        pdp = NULL;
        pdraw->private = NULL;
     }
@@ -868,7 +901,7 @@ static void *driCreateNewDrawable(Display *dpy,
    return (void *) pdp;
 }
 
-static __DRIdrawable *driGetDrawable(Display *dpy, GLXDrawable draw,
+static __DRIdrawable *driGetDrawable(__DRInativeDisplay *dpy, __DRIid draw,
                                         void *screenPrivate)
 {
     __DRIscreenPrivate *psp = (__DRIscreenPrivate *) screenPrivate;
@@ -880,7 +913,7 @@ static __DRIdrawable *driGetDrawable(Display *dpy, GLXDrawable draw,
     return __driFindDrawable(psp->drawHash, draw);
 }
 
-static void driDestroyDrawable(Display *dpy, void *drawablePrivate)
+static void driDestroyDrawable(__DRInativeDisplay *dpy, void *drawablePrivate)
 {
     __DRIdrawablePrivate *pdp = (__DRIdrawablePrivate *) drawablePrivate;
     __DRIscreenPrivate *psp = pdp->driScreenPriv;
@@ -891,14 +924,14 @@ static void driDestroyDrawable(Display *dpy, void *drawablePrivate)
        if ((*window_exists)(dpy, pdp->draw))
            (void)XF86DRIDestroyDrawable(dpy, scrn, pdp->draw);
        if (pdp->pClipRects) {
-           Xfree(pdp->pClipRects);
+           _mesa_free(pdp->pClipRects);
            pdp->pClipRects = NULL;
        }
        if (pdp->pBackClipRects) {
-           Xfree(pdp->pBackClipRects);
+           _mesa_free(pdp->pBackClipRects);
            pdp->pBackClipRects = NULL;
        }
-       Xfree(pdp);
+       _mesa_free(pdp);
     }
 }
 
@@ -921,7 +954,7 @@ static void driDestroyDrawable(Display *dpy, void *drawablePrivate)
  * This function calls __DriverAPIRec::DestroyContext on \p contextPrivate, calls
  * drmDestroyContext(), and finally frees \p contextPrivate.
  */
-static void driDestroyContext(Display *dpy, int scrn, void *contextPrivate)
+static void driDestroyContext(__DRInativeDisplay *dpy, int scrn, void *contextPrivate)
 {
     __DRIcontextPrivate  *pcp   = (__DRIcontextPrivate *) contextPrivate;
 
@@ -929,7 +962,7 @@ static void driDestroyContext(Display *dpy, int scrn, void *contextPrivate)
        (*pcp->driScreenPriv->DriverAPI.DestroyContext)(pcp);
        __driGarbageCollectDrawables(pcp->driScreenPriv->drawHash);
        (void)XF86DRIDestroyContext(dpy, scrn, pcp->contextID);
-       Xfree(pcp);
+       _mesa_free(pcp);
     }
 }
 
@@ -956,7 +989,7 @@ static void driDestroyContext(Display *dpy, int scrn, void *contextPrivate)
  *
  */
 static void *
-driCreateNewContext(Display *dpy, const __GLcontextModes *modes,
+driCreateNewContext(__DRInativeDisplay *dpy, const __GLcontextModes *modes,
                    int render_type, void *sharedPrivate, __DRIcontext *pctx)
 {
     __DRIscreen *pDRIScreen;
@@ -965,7 +998,7 @@ driCreateNewContext(Display *dpy, const __GLcontextModes *modes,
     __DRIscreenPrivate *psp;
     void * const shareCtx = (pshare != NULL) ? pshare->driverPrivate : NULL;
 
-    pDRIScreen = __glXFindDRIScreen(dpy, modes->screen);
+    pDRIScreen = glx_find_dri_screen(dpy, modes->screen);
     if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        /* ERROR!!! */
        return NULL;
@@ -973,14 +1006,14 @@ driCreateNewContext(Display *dpy, const __GLcontextModes *modes,
 
     psp = (__DRIscreenPrivate *)pDRIScreen->private;
 
-    pcp = (__DRIcontextPrivate *)Xmalloc(sizeof(__DRIcontextPrivate));
+    pcp = (__DRIcontextPrivate *)_mesa_malloc(sizeof(__DRIcontextPrivate));
     if (!pcp) {
        return NULL;
     }
 
     if (! (*create_context_with_config)(dpy, modes->screen, modes->fbconfigID,
                                        &pcp->contextID, &pcp->hHWContext)) {
-       Xfree(pcp);
+       _mesa_free(pcp);
        return NULL;
     }
 
@@ -1010,22 +1043,22 @@ driCreateNewContext(Display *dpy, const __GLcontextModes *modes,
     pctx->bindContext3   = driBindContext3;
     pctx->unbindContext3 = driUnbindContext3;
 #else
-    pctx->bindContext    = driBindContext;
-    pctx->unbindContext  = driUnbindContext;
+    pctx->bindContext    = (void *)driBindContext;
+    pctx->unbindContext  = (void *)driUnbindContext;
     if ( driCompareGLXAPIVersion( 20030606 ) >= 0 ) {
-        pctx->bindContext2   = driBindContext2;
-        pctx->unbindContext2 = driUnbindContext2;
+        pctx->bindContext2   = (void *)driBindContext2;
+        pctx->unbindContext2 = (void *)driUnbindContext2;
     }
 
     if ( driCompareGLXAPIVersion( 20040415 ) >= 0 ) {
-        pctx->bindContext3   = driBindContext3;
-        pctx->unbindContext3 = driUnbindContext3;
+        pctx->bindContext3   = (void *)driBindContext3;
+        pctx->unbindContext3 = (void *)driUnbindContext3;
     }
 #endif
 
     if ( !(*psp->DriverAPI.CreateContext)(modes, pcp, shareCtx) ) {
         (void)XF86DRIDestroyContext(dpy, modes->screen, pcp->contextID);
-        Xfree(pcp);
+        _mesa_free(pcp);
         return NULL;
     }
 
@@ -1065,7 +1098,7 @@ static void *driCreateContext(Display *dpy, XVisualInfo *vis,
     __DRIscreen *pDRIScreen;
     const __GLcontextModes *modes;
 
-    pDRIScreen = __glXFindDRIScreen(dpy, vis->screen);
+    pDRIScreen = glx_find_dri_screen(dpy, vis->screen);
     if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
        /* ERROR!!! */
        return NULL;
@@ -1103,7 +1136,7 @@ static void *driCreateContext(Display *dpy, XVisualInfo *vis,
  * This function calls __DriverAPIRec::DestroyScreen on \p screenPrivate, calls
  * drmClose(), and finally frees \p screenPrivate.
  */
-static void driDestroyScreen(Display *dpy, int scrn, void *screenPrivate)
+static void driDestroyScreen(__DRInativeDisplay *dpy, int scrn, void *screenPrivate)
 {
     __DRIscreenPrivate *psp = (__DRIscreenPrivate *) screenPrivate;
 
@@ -1118,12 +1151,12 @@ static void driDestroyScreen(Display *dpy, int scrn, void *screenPrivate)
 
        (void)drmUnmap((drmAddress)psp->pSAREA, SAREA_MAX);
        (void)drmUnmap((drmAddress)psp->pFB, psp->fbSize);
-       Xfree(psp->pDevPriv);
+       _mesa_free(psp->pDevPriv);
        (void)drmClose(psp->fd);
        if ( psp->modes != NULL ) {
            _gl_context_modes_destroy( psp->modes );
        }
-       Xfree(psp);
+       _mesa_free(psp);
     }
 }
 
@@ -1153,13 +1186,13 @@ static void driDestroyScreen(Display *dpy, int scrn, void *screenPrivate)
  * \param driverAPI Driver API functions used by other routines in dri_util.c.
  */
 __DRIscreenPrivate *
-__driUtilCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
+__driUtilCreateNewScreen(__DRInativeDisplay *dpy, int scrn, __DRIscreen *psc,
                         __GLcontextModes * modes,
                         const __DRIversion * ddx_version,
                         const __DRIversion * dri_version,
                         const __DRIversion * drm_version,
                         const __DRIframebuffer * frame_buffer,
-                        drmAddress pSAREA,
+                        drm_sarea_t *pSAREA,
                         int fd,
                         int internal_api_version,
                         const struct __DriverAPIRec *driverAPI)
@@ -1168,9 +1201,9 @@ __driUtilCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
 
 
 #ifdef DRI_NEW_INTERFACE_ONLY
-    if ( internal_api_version < 20040415 ) {
+    if ( internal_api_version < 20040602 ) {
        fprintf( stderr, "libGL error: libGL.so version (%08u) is too old.  "
-                "20040415 or later is required.\n", internal_api_version );
+                "20040602 or later is required.\n", internal_api_version );
        return NULL;
     }
 #else
@@ -1211,7 +1244,7 @@ __driUtilCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
        
     api_ver = internal_api_version;
 
-    psp = (__DRIscreenPrivate *)Xmalloc(sizeof(__DRIscreenPrivate));
+    psp = (__DRIscreenPrivate *)_mesa_malloc(sizeof(__DRIscreenPrivate));
     if (!psp) {
        return NULL;
     }
@@ -1219,7 +1252,7 @@ __driUtilCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
     /* Create the hash table */
     psp->drawHash = drmHashCreate();
     if ( psp->drawHash == NULL ) {
-       Xfree( psp );
+       _mesa_free( psp );
        return NULL;
     }
 
@@ -1290,7 +1323,7 @@ __driUtilCreateNewScreen(Display *dpy, int scrn, __DRIscreen *psc,
 
     if ( (psp->DriverAPI.InitDriver != NULL)
         && !(*psp->DriverAPI.InitDriver)(psp) ) {
-       Xfree( psp );
+       _mesa_free( psp );
        return NULL;
     }
 
@@ -1323,7 +1356,7 @@ __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
 {
     int directCapable;
     __DRIscreenPrivate *psp = NULL;
-    drmHandle hSAREA;
+    drm_handle_t hSAREA;
     drmAddress pSAREA;
     char *BusID;
     __GLcontextModes *modes;
@@ -1368,13 +1401,13 @@ __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
 
     if (XF86DRIOpenConnection(dpy, scrn, &hSAREA, &BusID)) {
        fd = drmOpen(NULL,BusID);
-       Xfree(BusID); /* No longer needed */
+       _mesa_free(BusID); /* No longer needed */
 
        err_msg = "open DRM";
        err_extra = strerror( -fd );
 
        if (fd >= 0) {
-           drmMagic magic;
+           drm_magic_t magic;
 
            err_msg = "drmGetMagic";
            err_extra = NULL;
@@ -1410,7 +1443,7 @@ __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
                                                   &driverName)) {
 
                        /* No longer needed. */
-                       Xfree( driverName );
+                       _mesa_free( driverName );
 
                        /*
                         * Get the DRI X extension version.
@@ -1420,7 +1453,7 @@ __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
                                                &dri_version.major,
                                                &dri_version.minor,
                                                &dri_version.patch)) {
-                           drmHandle  hFB;
+                           drm_handle_t  hFB;
                            int        junk;
 
                            /*
@@ -1497,7 +1530,7 @@ __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
        }
 
        if ( framebuffer.dev_priv != NULL ) {
-           Xfree(framebuffer.dev_priv);
+           _mesa_free(framebuffer.dev_priv);
        }
 
        if ( fd >= 0 ) {
@@ -1540,7 +1573,7 @@ __driUtilCreateScreen(Display *dpy, int scrn, __DRIscreen *psc,
  *
  * \sa __glXGetInternalVersion().
  */
-int driCompareGLXAPIVersion( GLuint required_version )
+int driCompareGLXAPIVersion( GLint required_version )
 {
    if ( api_ver > required_version ) {
       return 1;
@@ -1554,7 +1587,7 @@ int driCompareGLXAPIVersion( GLuint required_version )
 
 
 static int
-driQueryFrameTracking( Display * dpy, void * priv,
+driQueryFrameTracking( __DRInativeDisplay * dpy, void * priv,
                       int64_t * sbc, int64_t * missedFrames,
                       float * lastMissedUsage, float * usage )
 {
@@ -1654,5 +1687,3 @@ driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
 }
 
 /*@}*/
-
-#endif /* GLX_DIRECT_RENDERING */