Don't cast the return value of malloc/realloc
[mesa.git] / src / glx / glx_pbuffer.c
index b8d0f21bf0620a9387d1dfe13944251744319c1f..e4b2c86a08da222d55d8069086d51b2a3beaf391 100644 (file)
@@ -57,7 +57,7 @@
 static void
 warn_GLX_1_3(Display * dpy, const char *function_name)
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   struct glx_display *priv = __glXInitialize(dpy);
 
    if (priv->minorVersion < 3) {
       fprintf(stderr,
@@ -77,17 +77,18 @@ warn_GLX_1_3(Display * dpy, const char *function_name)
  * \note
  * This function dynamically determines whether to use the SGIX_pbuffer
  * version of the protocol or the GLX 1.3 version of the protocol.
- *
- * \todo
- * This function needs to be modified to work with direct-rendering drivers.
  */
 static void
 ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
                         const CARD32 * attribs, size_t num_attribs)
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   struct glx_display *priv = __glXInitialize(dpy);
+#ifdef GLX_DIRECT_RENDERING
+   __GLXDRIdrawable *pdraw;
+#endif
    CARD32 *output;
    CARD8 opcode;
+   int i;
 
    if ((dpy == NULL) || (drawable == 0)) {
       return;
@@ -102,7 +103,7 @@ ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
    if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
       xGLXChangeDrawableAttributesReq *req;
 
-      GetReqExtra(GLXChangeDrawableAttributes, 8 + (8 * num_attribs), req);
+      GetReqExtra(GLXChangeDrawableAttributes, 8 * num_attribs, req);
       output = (CARD32 *) (req + 1);
 
       req->reqType = opcode;
@@ -113,7 +114,7 @@ ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
    else {
       xGLXVendorPrivateWithReplyReq *vpreq;
 
-      GetReqExtra(GLXVendorPrivateWithReply, 4 + (8 * num_attribs), vpreq);
+      GetReqExtra(GLXVendorPrivateWithReply, 8 + (8 * num_attribs), vpreq);
       output = (CARD32 *) (vpreq + 1);
 
       vpreq->reqType = opcode;
@@ -121,7 +122,8 @@ ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
       vpreq->vendorCode = X_GLXvop_ChangeDrawableAttributesSGIX;
 
       output[0] = (CARD32) drawable;
-      output++;
+      output[1] = num_attribs;
+      output += 2;
    }
 
    (void) memcpy(output, attribs, sizeof(CARD32) * 2 * num_attribs);
@@ -129,6 +131,22 @@ ChangeDrawableAttribute(Display * dpy, GLXDrawable drawable,
    UnlockDisplay(dpy);
    SyncHandle();
 
+#ifdef GLX_DIRECT_RENDERING
+   pdraw = GetGLXDRIDrawable(dpy, drawable);
+
+   if (!pdraw)
+      return;
+
+   for (i = 0; i < num_attribs; i++) {
+      switch(attribs[i * 2]) {
+      case GLX_EVENT_MASK:
+        /* Keep a local copy for masking out DRI2 proto events as needed */
+        pdraw->eventMask = attribs[i * 2 + 1];
+        break;
+      }
+   }
+#endif
+
    return;
 }
 
@@ -169,58 +187,61 @@ determineTextureFormat(const int *attribs, int numAttribs)
    return 0;
 }
 
-static void
-CreateDRIDrawable(Display *dpy, const __GLcontextModes *fbconfig,
+static GLboolean
+CreateDRIDrawable(Display *dpy, struct glx_config *config,
                  XID drawable, XID glxdrawable,
                  const int *attrib_list, size_t num_attribs)
 {
-   __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
+   struct glx_display *const priv = __glXInitialize(dpy);
    __GLXDRIdrawable *pdraw;
-   __GLXscreenConfigs *psc;
+   struct glx_screen *psc;
 
-   psc = &priv->screenConfigs[fbconfig->screen];
+   psc = priv->screens[config->screen];
    if (psc->driScreen == NULL)
-      return;
+      return GL_TRUE;
 
    pdraw = psc->driScreen->createDrawable(psc, drawable,
-                                         glxdrawable, fbconfig);
+                                         glxdrawable, config);
    if (pdraw == NULL) {
       fprintf(stderr, "failed to create drawable\n");
-      return;
+      return GL_FALSE;
    }
 
-   if (__glxHashInsert(psc->drawHash, glxdrawable, pdraw)) {
+   if (__glxHashInsert(priv->drawHash, glxdrawable, pdraw)) {
       (*pdraw->destroyDrawable) (pdraw);
-      return; /* FIXME: Check what we're supposed to do here... */
+      return GL_FALSE;
    }
 
    pdraw->textureTarget = determineTextureTarget(attrib_list, num_attribs);
    pdraw->textureFormat = determineTextureFormat(attrib_list, num_attribs);
+
+   return GL_TRUE;
 }
 
 static void
 DestroyDRIDrawable(Display *dpy, GLXDrawable drawable, int destroy_xdrawable)
 {
-   int screen;
-   __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs *psc = &priv->screenConfigs[screen];
+   struct glx_display *const priv = __glXInitialize(dpy);
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
+   XID xid;
 
    if (pdraw != NULL) {
-      if (destroy_xdrawable)
-         XFreePixmap(psc->dpy, pdraw->xDrawable);
+      xid = pdraw->xDrawable;
       (*pdraw->destroyDrawable) (pdraw);
-      __glxHashDelete(psc->drawHash, drawable);
+      __glxHashDelete(priv->drawHash, drawable);
+      if (destroy_xdrawable)
+         XFreePixmap(priv->dpy, xid);
    }
 }
 
 #else
 
-static void
-CreateDRIDrawable(Display *dpy, const __GLcontextModes * fbconfig,
+static GLboolean
+CreateDRIDrawable(Display *dpy, const struct glx_config * fbconfig,
                  XID drawable, XID glxdrawable,
                  const int *attrib_list, size_t num_attribs)
 {
+    return GL_FALSE;
 }
 
 static void
@@ -244,15 +265,12 @@ DestroyDRIDrawable(Display *dpy, GLXDrawable drawable, int destroy_xdrawable)
  * The number of attributes returned is likely to be small, probably less than
  * 10.  Given that, this routine should try to use an array on the stack to
  * capture the reply rather than always calling Xmalloc.
- *
- * \todo
- * This function needs to be modified to work with direct-rendering drivers.
  */
 static int
 GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
                      int attribute, unsigned int *value)
 {
-   __GLXdisplayPrivate *priv;
+   struct glx_display *priv;
    xGLXGetDrawableAttributesReply reply;
    CARD32 *data;
    CARD8 opcode;
@@ -280,7 +298,7 @@ GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
    if (use_glx_1_3) {
       xGLXGetDrawableAttributesReq *req;
 
-      GetReqExtra(GLXGetDrawableAttributes, 4, req);
+      GetReq(GLXGetDrawableAttributes, req);
       req->reqType = opcode;
       req->glxCode = X_GLXGetDrawableAttributes;
       req->drawable = drawable;
@@ -308,7 +326,7 @@ GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
    length = reply.length;
    if (length) {
       num_attributes = (use_glx_1_3) ? reply.numAttribs : length / 2;
-      data = (CARD32 *) Xmalloc(length * sizeof(CARD32));
+      data = malloc(length * sizeof(CARD32));
       if (data == NULL) {
          /* Throw data on the floor */
          _XEatData(dpy, length);
@@ -328,7 +346,7 @@ GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
 
 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
          {
-            __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
+            __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
 
             if (pdraw != NULL && !pdraw->textureTarget)
                pdraw->textureTarget =
@@ -339,7 +357,7 @@ GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
          }
 #endif
 
-         Xfree(data);
+         free(data);
       }
    }
 
@@ -349,20 +367,40 @@ GetDrawableAttribute(Display * dpy, GLXDrawable drawable,
    return 0;
 }
 
+static void
+protocolDestroyDrawable(Display *dpy, GLXDrawable drawable, CARD32 glxCode)
+{
+   xGLXDestroyPbufferReq *req;
+   CARD8 opcode;
+
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode)
+      return;
+
+   LockDisplay(dpy);
+
+   GetReq(GLXDestroyPbuffer, req);
+   req->reqType = opcode;
+   req->glxCode = glxCode;
+   req->pbuffer = (GLXPbuffer) drawable;
+
+   UnlockDisplay(dpy);
+   SyncHandle();
+}
+
 /**
  * Create a non-pbuffer GLX drawable.
- *
- * \todo
- * This function needs to be modified to work with direct-rendering drivers.
  */
 static GLXDrawable
-CreateDrawable(Display * dpy, const __GLcontextModes * fbconfig,
+CreateDrawable(Display *dpy, struct glx_config *config,
                Drawable drawable, const int *attrib_list, CARD8 glxCode)
 {
    xGLXCreateWindowReq *req;
+   struct glx_drawable *glxDraw;
    CARD32 *data;
    unsigned int i;
    CARD8 opcode;
+   GLXDrawable xid;
 
    i = 0;
    if (attrib_list) {
@@ -374,17 +412,21 @@ CreateDrawable(Display * dpy, const __GLcontextModes * fbconfig,
    if (!opcode)
       return None;
 
+   glxDraw = malloc(sizeof(*glxDraw));
+   if (!glxDraw)
+      return None;
+
    LockDisplay(dpy);
    GetReqExtra(GLXCreateWindow, 8 * i, req);
    data = (CARD32 *) (req + 1);
 
    req->reqType = opcode;
    req->glxCode = glxCode;
-   req->screen = (CARD32) fbconfig->screen;
-   req->fbconfig = fbconfig->fbconfigID;
-   req->window = (CARD32) drawable;
-   req->glxwindow = (GLXWindow) XAllocID(dpy);
-   req->numAttribs = (CARD32) i;
+   req->screen = config->screen;
+   req->fbconfig = config->fbconfigID;
+   req->window = drawable;
+   req->glxwindow = xid = XAllocID(dpy);
+   req->numAttribs = i;
 
    if (attrib_list)
       memcpy(data, attrib_list, 8 * i);
@@ -392,9 +434,21 @@ CreateDrawable(Display * dpy, const __GLcontextModes * fbconfig,
    UnlockDisplay(dpy);
    SyncHandle();
 
-   CreateDRIDrawable(dpy, fbconfig, drawable, req->glxwindow, attrib_list, i);
+   if (InitGLXDrawable(dpy, glxDraw, drawable, xid)) {
+      free(glxDraw);
+      return None;
+   }
 
-   return (GLXDrawable) req->glxwindow;
+   if (!CreateDRIDrawable(dpy, config, drawable, xid, attrib_list, i)) {
+      if (glxCode == X_GLXCreatePixmap)
+         glxCode = X_GLXDestroyPixmap;
+      else
+         glxCode = X_GLXDestroyWindow;
+      protocolDestroyDrawable(dpy, xid, glxCode);
+      xid = None;
+   }
+
+   return xid;
 }
 
 
@@ -404,28 +458,13 @@ CreateDrawable(Display * dpy, const __GLcontextModes * fbconfig,
 static void
 DestroyDrawable(Display * dpy, GLXDrawable drawable, CARD32 glxCode)
 {
-   xGLXDestroyPbufferReq *req;
-   CARD8 opcode;
-
    if ((dpy == NULL) || (drawable == 0)) {
       return;
    }
 
+   protocolDestroyDrawable(dpy, drawable, glxCode);
 
-   opcode = __glXSetupForCommand(dpy);
-   if (!opcode)
-      return;
-
-   LockDisplay(dpy);
-
-   GetReqExtra(GLXDestroyPbuffer, 4, req);
-   req->reqType = opcode;
-   req->glxCode = glxCode;
-   req->pbuffer = (GLXPbuffer) drawable;
-
-   UnlockDisplay(dpy);
-   SyncHandle();
-
+   DestroyGLXDrawable(dpy, drawable);
    DestroyDRIDrawable(dpy, drawable, GL_FALSE);
 
    return;
@@ -441,21 +480,19 @@ DestroyDrawable(Display * dpy, GLXDrawable drawable, CARD32 glxCode)
  * \note
  * This function dynamically determines whether to use the SGIX_pbuffer
  * version of the protocol or the GLX 1.3 version of the protocol.
- *
- * \todo
- * This function needs to be modified to work with direct-rendering drivers.
  */
 static GLXDrawable
-CreatePbuffer(Display * dpy, const __GLcontextModes * fbconfig,
+CreatePbuffer(Display * dpy, struct glx_config *config,
               unsigned int width, unsigned int height,
               const int *attrib_list, GLboolean size_in_attribs)
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   struct glx_display *priv = __glXInitialize(dpy);
    GLXDrawable id = 0;
    CARD32 *data;
    CARD8 opcode;
    unsigned int i;
    Pixmap pixmap;
+   GLboolean glx_1_3 = GL_FALSE;
 
    i = 0;
    if (attrib_list) {
@@ -474,15 +511,17 @@ CreatePbuffer(Display * dpy, const __GLcontextModes * fbconfig,
       xGLXCreatePbufferReq *req;
       unsigned int extra = (size_in_attribs) ? 0 : 2;
 
+      glx_1_3 = GL_TRUE;
+
       GetReqExtra(GLXCreatePbuffer, (8 * (i + extra)), req);
       data = (CARD32 *) (req + 1);
 
       req->reqType = opcode;
       req->glxCode = X_GLXCreatePbuffer;
-      req->screen = (CARD32) fbconfig->screen;
-      req->fbconfig = fbconfig->fbconfigID;
-      req->pbuffer = (GLXPbuffer) id;
-      req->numAttribs = (CARD32) (i + extra);
+      req->screen = config->screen;
+      req->fbconfig = config->fbconfigID;
+      req->pbuffer = id;
+      req->numAttribs = i + extra;
 
       if (!size_in_attribs) {
          data[(2 * i) + 0] = GLX_PBUFFER_WIDTH;
@@ -502,11 +541,11 @@ CreatePbuffer(Display * dpy, const __GLcontextModes * fbconfig,
       vpreq->glxCode = X_GLXVendorPrivate;
       vpreq->vendorCode = X_GLXvop_CreateGLXPbufferSGIX;
 
-      data[0] = (CARD32) fbconfig->screen;
-      data[1] = (CARD32) fbconfig->fbconfigID;
-      data[2] = (CARD32) id;
-      data[3] = (CARD32) width;
-      data[4] = (CARD32) height;
+      data[0] = config->screen;
+      data[1] = config->fbconfigID;
+      data[2] = id;
+      data[3] = width;
+      data[4] = height;
       data += 5;
    }
 
@@ -515,10 +554,15 @@ CreatePbuffer(Display * dpy, const __GLcontextModes * fbconfig,
    UnlockDisplay(dpy);
    SyncHandle();
 
-   pixmap = XCreatePixmap(dpy, RootWindow(dpy, fbconfig->screen),
-                         width, height, fbconfig->rgbBits);
+   pixmap = XCreatePixmap(dpy, RootWindow(dpy, config->screen),
+                         width, height, config->rgbBits);
 
-   CreateDRIDrawable(dpy, fbconfig, pixmap, id, attrib_list, i);
+   if (!CreateDRIDrawable(dpy, config, pixmap, id, attrib_list, i)) {
+      CARD32 o = glx_1_3 ? X_GLXDestroyPbuffer : X_GLXvop_DestroyGLXPbufferSGIX;
+      XFreePixmap(dpy, pixmap);
+      protocolDestroyDrawable(dpy, id, o);
+      id = None;
+   }
 
    return id;
 }
@@ -536,7 +580,7 @@ CreatePbuffer(Display * dpy, const __GLcontextModes * fbconfig,
 static void
 DestroyPbuffer(Display * dpy, GLXDrawable drawable)
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   struct glx_display *priv = __glXInitialize(dpy);
    CARD8 opcode;
 
    if ((dpy == NULL) || (drawable == 0)) {
@@ -582,12 +626,12 @@ DestroyPbuffer(Display * dpy, GLXDrawable drawable)
 /**
  * Create a new pbuffer.
  */
-PUBLIC GLXPbufferSGIX
+_X_EXPORT GLXPbufferSGIX
 glXCreateGLXPbufferSGIX(Display * dpy, GLXFBConfigSGIX config,
                         unsigned int width, unsigned int height,
                         int *attrib_list)
 {
-   return (GLXPbufferSGIX) CreatePbuffer(dpy, (__GLcontextModes *) config,
+   return (GLXPbufferSGIX) CreatePbuffer(dpy, (struct glx_config *) config,
                                          width, height,
                                          attrib_list, GL_FALSE);
 }
@@ -597,7 +641,7 @@ glXCreateGLXPbufferSGIX(Display * dpy, GLXFBConfigSGIX config,
 /**
  * Create a new pbuffer.
  */
-PUBLIC GLXPbuffer
+_X_EXPORT GLXPbuffer
 glXCreatePbuffer(Display * dpy, GLXFBConfig config, const int *attrib_list)
 {
    int i, width, height;
@@ -663,7 +707,7 @@ glXCreatePbuffer(Display * dpy, GLXFBConfig config, const int *attrib_list)
       }
    }
 
-   return (GLXPbuffer) CreatePbuffer(dpy, (__GLcontextModes *) config,
+   return (GLXPbuffer) CreatePbuffer(dpy, (struct glx_config *) config,
                                      width, height, attrib_list, GL_TRUE);
 #endif
 }
@@ -672,7 +716,7 @@ glXCreatePbuffer(Display * dpy, GLXFBConfig config, const int *attrib_list)
 /**
  * Destroy an existing pbuffer.
  */
-PUBLIC void
+_X_EXPORT void
 glXDestroyPbuffer(Display * dpy, GLXPbuffer pbuf)
 {
 #ifdef GLX_USE_APPLEGL
@@ -688,7 +732,7 @@ glXDestroyPbuffer(Display * dpy, GLXPbuffer pbuf)
 /**
  * Query an attribute of a drawable.
  */
-PUBLIC void
+_X_EXPORT void
 glXQueryDrawable(Display * dpy, GLXDrawable drawable,
                  int attribute, unsigned int *value)
 {
@@ -737,7 +781,7 @@ glXQueryDrawable(Display * dpy, GLXDrawable drawable,
 /**
  * Query an attribute of a pbuffer.
  */
-PUBLIC int
+_X_EXPORT int
 glXQueryGLXPbufferSGIX(Display * dpy, GLXPbufferSGIX drawable,
                        int attribute, unsigned int *value)
 {
@@ -748,7 +792,7 @@ glXQueryGLXPbufferSGIX(Display * dpy, GLXPbufferSGIX drawable,
 /**
  * Select the event mask for a drawable.
  */
-PUBLIC void
+_X_EXPORT void
 glXSelectEvent(Display * dpy, GLXDrawable drawable, unsigned long mask)
 {
 #ifdef GLX_USE_APPLEGL
@@ -781,7 +825,7 @@ glXSelectEvent(Display * dpy, GLXDrawable drawable, unsigned long mask)
 /**
  * Get the selected event mask for a drawable.
  */
-PUBLIC void
+_X_EXPORT void
 glXGetSelectedEvent(Display * dpy, GLXDrawable drawable, unsigned long *mask)
 {
 #ifdef GLX_USE_APPLEGL
@@ -818,27 +862,27 @@ glXGetSelectedEvent(Display * dpy, GLXDrawable drawable, unsigned long *mask)
 }
 
 
-PUBLIC GLXPixmap
+_X_EXPORT GLXPixmap
 glXCreatePixmap(Display * dpy, GLXFBConfig config, Pixmap pixmap,
                 const int *attrib_list)
 {
    WARN_ONCE_GLX_1_3(dpy, __func__);
 
 #ifdef GLX_USE_APPLEGL
-   const __GLcontextModes *modes = (const __GLcontextModes *) config;
+   const struct glx_config *modes = (const struct glx_config *) config;
 
    if (apple_glx_pixmap_create(dpy, modes->screen, pixmap, modes))
       return None;
 
    return pixmap;
 #else
-   return CreateDrawable(dpy, (__GLcontextModes *) config,
+   return CreateDrawable(dpy, (struct glx_config *) config,
                          (Drawable) pixmap, attrib_list, X_GLXCreatePixmap);
 #endif
 }
 
 
-PUBLIC GLXWindow
+_X_EXPORT GLXWindow
 glXCreateWindow(Display * dpy, GLXFBConfig config, Window win,
                 const int *attrib_list)
 {
@@ -863,17 +907,17 @@ glXCreateWindow(Display * dpy, GLXFBConfig config, Window win,
       return None;
    }
 
-   XFree(visinfo);
+   free(visinfo);
 
    return win;
 #else
-   return CreateDrawable(dpy, (__GLcontextModes *) config,
+   return CreateDrawable(dpy, (struct glx_config *) config,
                          (Drawable) win, attrib_list, X_GLXCreateWindow);
 #endif
 }
 
 
-PUBLIC void
+_X_EXPORT void
 glXDestroyPixmap(Display * dpy, GLXPixmap pixmap)
 {
    WARN_ONCE_GLX_1_3(dpy, __func__);
@@ -886,7 +930,7 @@ glXDestroyPixmap(Display * dpy, GLXPixmap pixmap)
 }
 
 
-PUBLIC void
+_X_EXPORT void
 glXDestroyWindow(Display * dpy, GLXWindow win)
 {
    WARN_ONCE_GLX_1_3(dpy, __func__);
@@ -896,17 +940,17 @@ glXDestroyWindow(Display * dpy, GLXWindow win)
 }
 
 #ifndef GLX_USE_APPLEGL
-PUBLIC
+_X_EXPORT
 GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
                (Display * dpy, GLXPbufferSGIX pbuf),
                (dpy, pbuf), glXDestroyPbuffer)
 
-PUBLIC
+_X_EXPORT
 GLX_ALIAS_VOID(glXSelectEventSGIX,
                (Display * dpy, GLXDrawable drawable,
                 unsigned long mask), (dpy, drawable, mask), glXSelectEvent)
 
-PUBLIC
+_X_EXPORT
 GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
                (Display * dpy, GLXDrawable drawable,
                 unsigned long *mask), (dpy, drawable, mask),