libGL: Consolidate DRI initialization in dri_glx.c
[mesa.git] / src / glx / x11 / glx_pbuffer.c
index a03189ae3e6313d77007f1e9010314bdff304f9a..52dad65170fd3e8abb05b4c25ff97d5f435f1f6b 100644 (file)
 
 #include <inttypes.h>
 #include "glxclient.h"
-#include <extutil.h>
-#include <Xext.h>
+#include <X11/extensions/extutil.h>
+#include <X11/extensions/Xext.h>
 #include <assert.h>
 #include <string.h>
 #include "glapi.h"
 #include "glxextensions.h"
 #include "glcontextmodes.h"
-
-#ifdef IN_DOXYGEN
-#define GLX_PREFIX(x) x
-#endif /* IN_DOXYGEN */
-
-static void ChangeDrawableAttribute( Display * dpy, GLXDrawable drawable,
-    const CARD32 * attribs, size_t num_attribs );
-
-static void DestroyPbuffer( Display * dpy, GLXDrawable drawable );
-
-static GLXDrawable CreatePbuffer( Display *dpy,
-    const __GLcontextModes * fbconfig, unsigned int width, unsigned int height,
-    const int *attrib_list, GLboolean size_in_attribs );
-
-static int GetDrawableAttribute( Display *dpy, GLXDrawable drawable,
-    int attribute, unsigned int *value );
+#include "glheader.h"
 
 
 /**
@@ -75,12 +60,15 @@ ChangeDrawableAttribute( Display * dpy, GLXDrawable drawable,
 {
    __GLXdisplayPrivate *priv = __glXInitialize(dpy);
    CARD32 * output;
-
+   CARD8 opcode;
 
    if ( (dpy == NULL) || (drawable == 0) ) {
       return;
    }
 
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode)
+      return;
 
    LockDisplay(dpy);
 
@@ -90,7 +78,7 @@ ChangeDrawableAttribute( Display * dpy, GLXDrawable drawable,
       GetReqExtra( GLXChangeDrawableAttributes, 8 + (8 * num_attribs), req );
       output = (CARD32 *) (req + 1);
 
-      req->reqType = __glXSetupForCommand(dpy);
+      req->reqType = opcode;
       req->glxCode = X_GLXChangeDrawableAttributes;
       req->drawable = drawable;
       req->numAttribs = (CARD32) num_attribs;
@@ -101,7 +89,7 @@ ChangeDrawableAttribute( Display * dpy, GLXDrawable drawable,
       GetReqExtra( GLXVendorPrivateWithReply, 4 + (8 * num_attribs), vpreq );
       output = (CARD32 *) (vpreq + 1);
 
-      vpreq->reqType = __glXSetupForCommand(dpy);
+      vpreq->reqType = opcode;
       vpreq->glxCode = X_GLXVendorPrivateWithReply;
       vpreq->vendorCode = X_GLXvop_ChangeDrawableAttributesSGIX;
 
@@ -135,19 +123,23 @@ static void
 DestroyPbuffer( Display * dpy, GLXDrawable drawable )
 {
    __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   CARD8 opcode;
 
    if ( (dpy == NULL) || (drawable == 0) ) {
       return;
    }
 
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode)
+      return;
 
    LockDisplay(dpy);
 
    if ( (priv->majorVersion > 1) || (priv->minorVersion >= 3) ) {
       xGLXDestroyPbufferReq * req;
 
-      GetReqExtra( GLXDestroyPbuffer, 4, req );
-      req->reqType = __glXSetupForCommand(dpy);
+      GetReq( GLXDestroyPbuffer, req );
+      req->reqType = opcode;
       req->glxCode = X_GLXDestroyPbuffer;
       req->pbuffer = (GLXPbuffer) drawable;
    }
@@ -160,7 +152,7 @@ DestroyPbuffer( Display * dpy, GLXDrawable drawable )
 
       data[0] = (CARD32) drawable;
 
-      vpreq->reqType = __glXSetupForCommand(dpy);
+      vpreq->reqType = opcode;
       vpreq->glxCode = X_GLXVendorPrivateWithReply;
       vpreq->vendorCode = X_GLXvop_DestroyGLXPbufferSGIX;
    }
@@ -194,28 +186,36 @@ static int
 GetDrawableAttribute( Display *dpy, GLXDrawable drawable,
                      int attribute, unsigned int *value )
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   __GLXdisplayPrivate *priv;
    xGLXGetDrawableAttributesReply reply;
    CARD32 * data;
+   CARD8 opcode;
    unsigned int length;
    unsigned int i;
    unsigned int num_attributes;
+
+   if ( (dpy == NULL) || (drawable == 0) ) {
+      return 0;
+   }
+
+   priv = __glXInitialize(dpy);
    GLboolean use_glx_1_3 = ((priv->majorVersion > 1)
                            || (priv->minorVersion >= 3));
 
+   *value = 0;
 
-   if ( (dpy == NULL) || (drawable == 0) ) {
+
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode)
       return 0;
-   }
 
-   
    LockDisplay(dpy);
 
    if ( use_glx_1_3 ) {
       xGLXGetDrawableAttributesReq *req;
 
       GetReqExtra( GLXGetDrawableAttributes, 4, req );
-      req->reqType = __glXSetupForCommand(dpy);
+      req->reqType = opcode;
       req->glxCode = X_GLXGetDrawableAttributes;
       req->drawable = drawable;
    }
@@ -226,40 +226,48 @@ GetDrawableAttribute( Display *dpy, GLXDrawable drawable,
       data = (CARD32 *) (vpreq + 1);
       data[0] = (CARD32) drawable;
 
-      vpreq->reqType = __glXSetupForCommand(dpy);
+      vpreq->reqType = opcode;
       vpreq->glxCode = X_GLXVendorPrivateWithReply;
       vpreq->vendorCode = X_GLXvop_GetDrawableAttributesSGIX;
    }
 
    _XReply(dpy, (xReply*) &reply, 0, False);
 
+   if (reply.type == X_Error)
+   {
+       UnlockDisplay(dpy);
+       SyncHandle();
+       return 0;
+   }
+
    length = reply.length;
-   num_attributes = (use_glx_1_3) ? reply.numAttribs : length / 2;
-   data = (CARD32 *) Xmalloc( length * sizeof(CARD32) );
-   if ( data == NULL ) {
-      /* Throw data on the floor */
-      _XEatData(dpy, length);
-   } else {
-      _XRead(dpy, (char *)data, length * sizeof(CARD32) );
+   if (length)
+   {
+       num_attributes = (use_glx_1_3) ? reply.numAttribs : length / 2;
+       data = (CARD32 *) Xmalloc( length * sizeof(CARD32) );
+       if ( data == NULL ) {
+          /* Throw data on the floor */
+          _XEatData(dpy, length);
+       } else {
+          _XRead(dpy, (char *)data, length * sizeof(CARD32) );
+
+          /* Search the set of returned attributes for the attribute requested by
+           * the caller.
+           */
+          for ( i = 0 ; i < num_attributes ; i++ ) {
+              if ( data[i*2] == attribute ) {
+                  *value = data[ (i*2) + 1 ];
+                  break;
+              }
+          }
+
+          Xfree( data );
+       }
    }
 
    UnlockDisplay(dpy);
    SyncHandle();
 
-
-   /* Search the set of returned attributes for the attribute requested by
-    * the caller.
-    */
-
-   for ( i = 0 ; i < num_attributes ; i++ ) {
-      if ( data[i*2] == attribute ) {
-        *value = data[ (i*2) + 1 ];
-        break;
-      }
-   }
-
-   Xfree( data );
-
    return 0;
 }
 
@@ -277,27 +285,37 @@ CreateDrawable( Display *dpy, const __GLcontextModes * fbconfig,
 {
    xGLXCreateWindowReq * req;
    CARD32 * data;
-   unsigned int  i;
+   unsigned int i;
+   CARD8 opcode;
 
+   i = 0;
+   if (attrib_list) {
+       while (attrib_list[i * 2] != None)
+          i++;
+   }
 
-   for ( i = 0 ; attrib_list[i * 2] != None ; i++ )
-      /* empty */ ;
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode)
+      return None;
 
    LockDisplay(dpy);
-   GetReqExtra( GLXCreateWindow, 20 + (8 * i), req );
+   GetReqExtra( GLXCreateWindow, 8 * i, req );
    data = (CARD32 *) (req + 1);
 
-   req->reqType = __glXSetupForCommand(dpy);
+   req->reqType = opcode;
    req->glxCode = glxCode;
    req->screen = (CARD32) fbconfig->screen;
    req->fbconfig = fbconfig->fbconfigID;
    req->window = (GLXPbuffer) drawable;
+   req->glxwindow = (GLXWindow) XAllocID(dpy);
    req->numAttribs = (CARD32) i;
 
+   memcpy( data, attrib_list, 8 * i );
+   
    UnlockDisplay(dpy);
    SyncHandle();
    
-   return drawable;
+   return (GLXDrawable)req->glxwindow;
 }
 
 
@@ -311,16 +329,21 @@ static void
 DestroyDrawable( Display * dpy, GLXDrawable drawable, CARD32 glxCode )
 {
    xGLXDestroyPbufferReq * req;
+   CARD8 opcode;
 
    if ( (dpy == NULL) || (drawable == 0) ) {
       return;
    }
 
 
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode)
+      return;
+
    LockDisplay(dpy);
 
    GetReqExtra( GLXDestroyPbuffer, 4, req );
-   req->reqType = __glXSetupForCommand(dpy);
+   req->reqType = opcode;
    req->glxCode = glxCode;
    req->pbuffer = (GLXPbuffer) drawable;
 
@@ -352,12 +375,18 @@ CreatePbuffer( Display *dpy, const __GLcontextModes * fbconfig,
    __GLXdisplayPrivate *priv = __glXInitialize(dpy);
    GLXDrawable id = 0;
    CARD32 * data;
+   CARD8 opcode;
    unsigned int  i;
 
+   i = 0;
+   if (attrib_list) {
+       while (attrib_list[i * 2])
+          i++;
+   }
 
-   for ( i = 0 ; attrib_list[i * 2] != None ; i++ )
-      /* empty */ ;
-
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode)
+      return None;
 
    LockDisplay(dpy);
    id = XAllocID(dpy);
@@ -369,7 +398,7 @@ CreatePbuffer( Display *dpy, const __GLcontextModes * fbconfig,
       GetReqExtra( GLXCreatePbuffer, (8 * (i + extra)), req );
       data = (CARD32 *) (req + 1);
 
-      req->reqType = __glXSetupForCommand(dpy);
+      req->reqType = opcode;
       req->glxCode = X_GLXCreatePbuffer;
       req->screen = (CARD32) fbconfig->screen;
       req->fbconfig = fbconfig->fbconfigID;
@@ -390,7 +419,7 @@ CreatePbuffer( Display *dpy, const __GLcontextModes * fbconfig,
       GetReqExtra( GLXVendorPrivate, 20 + (8 * i), vpreq );
       data = (CARD32 *) (vpreq + 1);
 
-      vpreq->reqType = __glXSetupForCommand(dpy);
+      vpreq->reqType = opcode;
       vpreq->glxCode = X_GLXVendorPrivate;
       vpreq->vendorCode = X_GLXvop_CreateGLXPbufferSGIX;
 
@@ -414,10 +443,10 @@ CreatePbuffer( Display *dpy, const __GLcontextModes * fbconfig,
 /**
  * Create a new pbuffer.
  */
-GLXPbufferSGIX
-GLX_PREFIX(glXCreateGLXPbufferSGIX)(Display *dpy, GLXFBConfigSGIX config,
-                                   unsigned int width, unsigned int height,
-                                   int *attrib_list)
+PUBLIC GLXPbufferSGIX
+glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config,
+                       unsigned int width, unsigned int height,
+                       int *attrib_list)
 {
    return (GLXPbufferSGIX) CreatePbuffer( dpy, (__GLcontextModes *) config,
                                          width, height,
@@ -428,12 +457,27 @@ GLX_PREFIX(glXCreateGLXPbufferSGIX)(Display *dpy, GLXFBConfigSGIX config,
 /**
  * Create a new pbuffer.
  */
-GLXPbuffer
-GLX_PREFIX(glXCreatePbuffer)(Display *dpy, GLXFBConfig config,
-                            const int *attrib_list)
+PUBLIC GLXPbuffer
+glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attrib_list)
 {
+   int i, width, height;
+
+   width = 0;
+   height = 0;
+
+   for (i = 0; attrib_list[i * 2]; i++) {
+      switch (attrib_list[i * 2]) {
+      case GLX_PBUFFER_WIDTH:
+        width = attrib_list[i * 2 + 1];
+        break;
+      case GLX_PBUFFER_HEIGHT:
+        height = attrib_list[i * 2 + 1];
+        break;
+      }
+   }
+
    return (GLXPbuffer) CreatePbuffer( dpy, (__GLcontextModes *) config,
-                                     0, 0,
+                                     width, height,
                                      attrib_list, GL_TRUE );
 }
 
@@ -441,8 +485,8 @@ GLX_PREFIX(glXCreatePbuffer)(Display *dpy, GLXFBConfig config,
 /**
  * Destroy an existing pbuffer.
  */
-void
-GLX_PREFIX(glXDestroyPbuffer)(Display *dpy, GLXPbuffer pbuf)
+PUBLIC void
+glXDestroyPbuffer(Display *dpy, GLXPbuffer pbuf)
 {
    DestroyPbuffer( dpy, pbuf );
 }
@@ -451,9 +495,9 @@ GLX_PREFIX(glXDestroyPbuffer)(Display *dpy, GLXPbuffer pbuf)
 /**
  * Query an attribute of a drawable.
  */
-void
-GLX_PREFIX(glXQueryDrawable)(Display *dpy, GLXDrawable drawable,
-                                  int attribute, unsigned int *value)
+PUBLIC void
+glXQueryDrawable(Display *dpy, GLXDrawable drawable,
+                int attribute, unsigned int *value)
 {
    GetDrawableAttribute( dpy, drawable, attribute, value );
 }
@@ -462,9 +506,9 @@ GLX_PREFIX(glXQueryDrawable)(Display *dpy, GLXDrawable drawable,
 /**
  * Query an attribute of a pbuffer.
  */
-int
-GLX_PREFIX(glXQueryGLXPbufferSGIX)(Display *dpy, GLXPbufferSGIX drawable,
-                                  int attribute, unsigned int *value)
+PUBLIC int
+glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX drawable,
+                      int attribute, unsigned int *value)
 {
    return GetDrawableAttribute( dpy, drawable, attribute, value );
 }
@@ -473,9 +517,8 @@ GLX_PREFIX(glXQueryGLXPbufferSGIX)(Display *dpy, GLXPbufferSGIX drawable,
 /**
  * Select the event mask for a drawable.
  */
-void
-GLX_PREFIX(glXSelectEvent)(Display *dpy, GLXDrawable drawable,
-                          unsigned long mask)
+PUBLIC void
+glXSelectEvent(Display *dpy, GLXDrawable drawable, unsigned long mask)
 {
    CARD32 attribs[2];
 
@@ -489,9 +532,8 @@ GLX_PREFIX(glXSelectEvent)(Display *dpy, GLXDrawable drawable,
 /**
  * Get the selected event mask for a drawable.
  */
-void
-GLX_PREFIX(glXGetSelectedEvent)(Display *dpy, GLXDrawable drawable,
-                               unsigned long *mask)
+PUBLIC void
+glXGetSelectedEvent(Display *dpy, GLXDrawable drawable, unsigned long *mask)
 {
    unsigned int value;
 
@@ -506,9 +548,9 @@ GLX_PREFIX(glXGetSelectedEvent)(Display *dpy, GLXDrawable drawable,
 }
 
 
-GLXPixmap
-GLX_PREFIX(glXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap,
-                            const int *attrib_list )
+PUBLIC GLXPixmap
+glXCreatePixmap( Display *dpy, GLXFBConfig config, Pixmap pixmap,
+                const int *attrib_list )
 {
    return CreateDrawable( dpy, (__GLcontextModes *) config,
                          (Drawable) pixmap, attrib_list,
@@ -516,9 +558,9 @@ GLX_PREFIX(glXCreatePixmap)( Display *dpy, GLXFBConfig config, Pixmap pixmap,
 }
 
 
-GLXWindow
-GLX_PREFIX(glXCreateWindow)( Display *dpy, GLXFBConfig config, Window win,
-                            const int *attrib_list )
+PUBLIC GLXWindow
+glXCreateWindow( Display *dpy, GLXFBConfig config, Window win,
+                const int *attrib_list )
 {
    return CreateDrawable( dpy, (__GLcontextModes *) config,
                          (Drawable) win, attrib_list,
@@ -526,31 +568,31 @@ GLX_PREFIX(glXCreateWindow)( Display *dpy, GLXFBConfig config, Window win,
 }
 
 
-void
-GLX_PREFIX(glXDestroyPixmap)(Display *dpy, GLXPixmap pixmap)
+PUBLIC void
+glXDestroyPixmap(Display *dpy, GLXPixmap pixmap)
 {
    DestroyDrawable( dpy, (GLXDrawable) pixmap, X_GLXDestroyPixmap );
 }
 
 
-void
-GLX_PREFIX(glXDestroyWindow)(Display *dpy, GLXWindow win)
+PUBLIC void
+glXDestroyWindow(Display *dpy, GLXWindow win)
 {
    DestroyDrawable( dpy, (GLXDrawable) win, X_GLXDestroyWindow );
 }
 
 
-GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
+PUBLIC GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
          (Display *dpy, GLXPbufferSGIX pbuf),
          (dpy, pbuf),
          glXDestroyPbuffer)
 
-GLX_ALIAS_VOID(glXSelectEventSGIX,
+PUBLIC GLX_ALIAS_VOID(glXSelectEventSGIX,
          (Display *dpy, GLXDrawable drawable, unsigned long mask),
          (dpy, drawable, mask),
          glXSelectEvent)
 
-GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
+PUBLIC GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
          (Display *dpy, GLXDrawable drawable, unsigned long *mask),
          (dpy, drawable, mask),
          glXGetSelectedEvent)