#include <inttypes.h>
#include "glxclient.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"
#include "glheader.h"
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 );
static void
ChangeDrawableAttribute( Display * dpy, GLXDrawable drawable,
const CARD32 * attribs, size_t num_attribs )
{
__GLXdisplayPrivate *priv = __glXInitialize(dpy);
CARD32 * output;
CARD8 opcode;
if ( (dpy == NULL) || (drawable == 0) ) {
return;
}
opcode = __glXSetupForCommand(dpy);
if (!opcode)
return;
LockDisplay(dpy);
if ( (priv->majorVersion > 1) || (priv->minorVersion >= 3) ) {
xGLXChangeDrawableAttributesReq *req;
GetReqExtra( GLXChangeDrawableAttributes, 8 + (8 * num_attribs), req );
output = (CARD32 *) (req + 1);
req->reqType = opcode;
req->glxCode = X_GLXChangeDrawableAttributes;
req->drawable = drawable;
req->numAttribs = (CARD32) num_attribs;
}
else {
xGLXVendorPrivateWithReplyReq *vpreq;
GetReqExtra( GLXVendorPrivateWithReply, 4 + (8 * num_attribs), vpreq );
output = (CARD32 *) (vpreq + 1);
vpreq->reqType = opcode;
vpreq->glxCode = X_GLXVendorPrivateWithReply;
vpreq->vendorCode = X_GLXvop_ChangeDrawableAttributesSGIX;
output[0] = (CARD32) drawable;
output++;
}
(void) memcpy( output, attribs, sizeof( CARD32 ) * 2 * num_attribs );
UnlockDisplay(dpy);
SyncHandle();
return;
}
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 = opcode;
req->glxCode = X_GLXDestroyPbuffer;
req->pbuffer = (GLXPbuffer) drawable;
}
else {
xGLXVendorPrivateWithReplyReq *vpreq;
CARD32 * data;
GetReqExtra( GLXVendorPrivateWithReply, 4, vpreq );
data = (CARD32 *) (vpreq + 1);
data[0] = (CARD32) drawable;
vpreq->reqType = opcode;
vpreq->glxCode = X_GLXVendorPrivateWithReply;
vpreq->vendorCode = X_GLXvop_DestroyGLXPbufferSGIX;
}
UnlockDisplay(dpy);
SyncHandle();
return;
}
static int
GetDrawableAttribute( Display *dpy, GLXDrawable drawable,
int attribute, unsigned int *value )
{
__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;
opcode = __glXSetupForCommand(dpy);
if (!opcode)
return 0;
LockDisplay(dpy);
if ( use_glx_1_3 ) {
xGLXGetDrawableAttributesReq *req;
GetReqExtra( GLXGetDrawableAttributes, 4, req );
req->reqType = opcode;
req->glxCode = X_GLXGetDrawableAttributes;
req->drawable = drawable;
}
else {
xGLXVendorPrivateWithReplyReq *vpreq;
GetReqExtra( GLXVendorPrivateWithReply, 4, vpreq );
data = (CARD32 *) (vpreq + 1);
data[0] = (CARD32) drawable;
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;
if (length)
{
num_attributes = (use_glx_1_3) ? reply.numAttribs : length / 2;
data = (CARD32 *) Xmalloc( length * sizeof(CARD32) );
if ( data == NULL ) {
_XEatData(dpy, length);
} else {
_XRead(dpy, (char *)data, length * sizeof(CARD32) );
for ( i = 0 ; i < num_attributes ; i++ ) {
if ( data[i*2] == attribute ) {
*value = data[ (i*2) + 1 ];
break;
}
}
Xfree( data );
}
}
UnlockDisplay(dpy);
SyncHandle();
return 0;
}
static GLXDrawable
CreateDrawable( Display *dpy, const __GLcontextModes * fbconfig,
Drawable drawable, const int *attrib_list,
CARD8 glxCode )
{
xGLXCreateWindowReq * req;
CARD32 * data;
unsigned int i;
CARD8 opcode;
i = 0;
if (attrib_list) {
while (attrib_list[i * 2] != None)
i++;
}
opcode = __glXSetupForCommand(dpy);
if (!opcode)
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 = (GLXPbuffer) drawable;
req->glxwindow = (GLXWindow) XAllocID(dpy);
req->numAttribs = (CARD32) i;
memcpy( data, attrib_list, 8 * i );
UnlockDisplay(dpy);
SyncHandle();
return (GLXDrawable)req->glxwindow;
}
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 = opcode;
req->glxCode = glxCode;
req->pbuffer = (GLXPbuffer) drawable;
UnlockDisplay(dpy);
SyncHandle();
return;
}
static GLXDrawable
CreatePbuffer( Display *dpy, const __GLcontextModes * fbconfig,
unsigned int width, unsigned int height,
const int *attrib_list, GLboolean size_in_attribs )
{
__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++;
}
opcode = __glXSetupForCommand(dpy);
if (!opcode)
return None;
LockDisplay(dpy);
id = XAllocID(dpy);
if ( (priv->majorVersion > 1) || (priv->minorVersion >= 3) ) {
xGLXCreatePbufferReq * req;
unsigned int extra = (size_in_attribs) ? 0 : 2;
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);
if ( ! size_in_attribs ) {
data[(2 * i) + 0] = GLX_PBUFFER_WIDTH;
data[(2 * i) + 1] = width;
data[(2 * i) + 2] = GLX_PBUFFER_HEIGHT;
data[(2 * i) + 3] = height;
data += 4;
}
}
else {
xGLXVendorPrivateReq *vpreq;
GetReqExtra( GLXVendorPrivate, 20 + (8 * i), vpreq );
data = (CARD32 *) (vpreq + 1);
vpreq->reqType = opcode;
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 += 5;
}
(void) memcpy( data, attrib_list, sizeof(CARD32) * 2 * i );
UnlockDisplay(dpy);
SyncHandle();
return id;
}
PUBLIC GLXPbufferSGIX
glXCreateGLXPbufferSGIX(Display *dpy, GLXFBConfigSGIX config,
unsigned int width, unsigned int height,
int *attrib_list)
{
return (GLXPbufferSGIX) CreatePbuffer( dpy, (__GLcontextModes *) config,
width, height,
attrib_list, GL_FALSE );
}
PUBLIC GLXPbuffer
glXCreatePbuffer(Display *dpy, GLXFBConfig config, const int *attrib_list)
{
return (GLXPbuffer) CreatePbuffer( dpy, (__GLcontextModes *) config,
0, 0,
attrib_list, GL_TRUE );
}
PUBLIC void
glXDestroyPbuffer(Display *dpy, GLXPbuffer pbuf)
{
DestroyPbuffer( dpy, pbuf );
}
PUBLIC void
glXQueryDrawable(Display *dpy, GLXDrawable drawable,
int attribute, unsigned int *value)
{
GetDrawableAttribute( dpy, drawable, attribute, value );
}
PUBLIC int
glXQueryGLXPbufferSGIX(Display *dpy, GLXPbufferSGIX drawable,
int attribute, unsigned int *value)
{
return GetDrawableAttribute( dpy, drawable, attribute, value );
}
PUBLIC void
glXSelectEvent(Display *dpy, GLXDrawable drawable, unsigned long mask)
{
CARD32 attribs[2];
attribs[0] = (CARD32) GLX_EVENT_MASK;
attribs[1] = (CARD32) mask;
ChangeDrawableAttribute( dpy, drawable, attribs, 1 );
}
PUBLIC void
glXGetSelectedEvent(Display *dpy, GLXDrawable drawable, unsigned long *mask)
{
unsigned int value;
GetDrawableAttribute( dpy, drawable, GLX_EVENT_MASK_SGIX, & value );
*mask = value;
}
PUBLIC GLXPixmap
glXCreatePixmap( Display *dpy, GLXFBConfig config, Pixmap pixmap,
const int *attrib_list )
{
return CreateDrawable( dpy, (__GLcontextModes *) config,
(Drawable) pixmap, attrib_list,
X_GLXCreatePixmap );
}
PUBLIC GLXWindow
glXCreateWindow( Display *dpy, GLXFBConfig config, Window win,
const int *attrib_list )
{
return CreateDrawable( dpy, (__GLcontextModes *) config,
(Drawable) win, attrib_list,
X_GLXCreateWindow );
}
PUBLIC void
glXDestroyPixmap(Display *dpy, GLXPixmap pixmap)
{
DestroyDrawable( dpy, (GLXDrawable) pixmap, X_GLXDestroyPixmap );
}
PUBLIC void
glXDestroyWindow(Display *dpy, GLXWindow win)
{
DestroyDrawable( dpy, (GLXDrawable) win, X_GLXDestroyWindow );
}
PUBLIC GLX_ALIAS_VOID(glXDestroyGLXPbufferSGIX,
(Display *dpy, GLXPbufferSGIX pbuf),
(dpy, pbuf),
glXDestroyPbuffer)
PUBLIC GLX_ALIAS_VOID(glXSelectEventSGIX,
(Display *dpy, GLXDrawable drawable, unsigned long mask),
(dpy, drawable, mask),
glXSelectEvent)
PUBLIC GLX_ALIAS_VOID(glXGetSelectedEventSGIX,
(Display *dpy, GLXDrawable drawable, unsigned long *mask),
(dpy, drawable, mask),
glXGetSelectedEvent)