fix some 0->NULLs
[mesa.git] / src / mesa / drivers / dri / common / dri_util.c
index 6b2fcdfa7db53f79aa0729b36237a4535dba254e..302994e0c23a9417fe0d48dace5177c0ecfc47b5 100644 (file)
  */
 
 
-#ifdef GLX_DIRECT_RENDERING
-
-#include <inttypes.h>
 #include <assert.h>
 #include <stdarg.h>
 #include <unistd.h>
 #include <sys/mman.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 "drm_sarea.h"
 #include "glcontextmodes.h"
 
-#ifndef GLX_OML_sync_control
+#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.
  */
@@ -133,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                               */
@@ -206,24 +235,13 @@ 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 GLboolean __driWindowExistsFlag;
 
@@ -278,21 +296,21 @@ static GLboolean __driWindowExists(Display *dpy, GLXDrawable draw)
 static void __driGarbageCollectDrawables(void *drawHash)
 {
     __DRIid draw;
-    __DRIdrawable *pdraw;
     __DRInativeDisplay *dpy;
+    __DRIdrawable *pdraw;
 
-    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);
                _mesa_free(pdraw);
            }
-       } while (drmHashNext(drawHash, &draw, (void **)&pdraw));
+       } while (drmHashNext(drawHash, &draw, (void *)&pdraw) == 1);
     }
 }
 
@@ -345,7 +363,7 @@ static GLboolean driUnbindContext3(__DRInativeDisplay *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;
@@ -521,7 +539,7 @@ static GLboolean driBindContext3(__DRInativeDisplay *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;
@@ -555,7 +573,7 @@ static GLboolean 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 );
@@ -785,7 +803,7 @@ static void *driCreateNewDrawable(__DRInativeDisplay *dpy,
                                  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;
 
@@ -840,7 +858,7 @@ static void *driCreateNewDrawable(__DRInativeDisplay *dpy,
        _mesa_free(pdp);
        return NULL;
 #else
-       pdp->getInfo = XF86DRIGetDrawableInfo;
+       pdp->getInfo = (PFNGLXGETDRAWABLEINFOPROC) XF86DRIGetDrawableInfo;
 #endif /* DRI_NEW_INTERFACE_ONLY */
     }
 
@@ -980,7 +998,7 @@ driCreateNewContext(__DRInativeDisplay *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;
@@ -1025,16 +1043,16 @@ driCreateNewContext(__DRInativeDisplay *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
 
@@ -1080,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;
@@ -1555,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;
@@ -1669,5 +1687,3 @@ driCalculateSwapUsage( __DRIdrawablePrivate *dPriv, int64_t last_swap_ust,
 }
 
 /*@}*/
-
-#endif /* GLX_DIRECT_RENDERING */