/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.51 from the
 * contents of GLX.xs. Do not edit this file, edit GLX.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "lib/OpenGL/GLX.xs"
/*  Copyright (c) 1998 Kenneth Albanowski. All rights reserved.
 *  Copyright (c) 2007 Bob Free. All rights reserved.
 *  Copyright (c) 2009 Chris Marshall. All rights reserved.
 *  This program is free software; you can redistribute it and/or
 *  modify it under the same terms as Perl itself.
 */

#include <stdio.h>

#include "pgopogl.h"

#ifdef HAVE_GL
#include "gl_util.h"
#endif /* defined HAVE_GL */

#ifdef HAVE_GLU
#include "glu_util.h"
#endif

#ifdef HAVE_GLX
#include "glx_util.h"
#endif /* defined HAVE_GLX */

#ifdef HAVE_GLX
#  define nativeWindowId(d, w)	(w)
static Bool WaitForNotify(Display *d, XEvent *e, char *arg) {
    return (e->type == MapNotify) && (e->xmap.window == (Window)arg);
}
#  define glpResizeWindow(s1,s2,w,d)	XResizeWindow(d,w,s1,s2)
#  define glpMoveWindow(s1,s2,w,d)		XMoveWindow(d,w,s1,s2)
#  define glpMoveResizeWindow(s1,s2,s3,s4,w,d)	XMoveResizeWindow(d,w,s1,s2,s3,s4)
#endif	/* defined HAVE_GLX */

static int debug = 0;

#ifdef HAVE_GLX

#  define NUM_ARG 7			/* Number of mandatory args to glpcOpenWindow */

Display *dpy;
int dpy_open;
XVisualInfo *vi;
Colormap cmap;
XSetWindowAttributes swa;
Window win;
GLXContext ctx;
int requested_major = -1, requested_minor = -1, requested_core = -1;

static int default_attributes[] = {
  GLX_DOUBLEBUFFER, True,
  GLX_RENDER_TYPE, GLX_RGBA_BIT,
  None
};

#endif	/* defined HAVE_GLX */

static int DBUFFER_HACK = 0;
#define __had_dbuffer_hack() (DBUFFER_HACK)


#line 71 "lib/OpenGL/GLX.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 215 "lib/OpenGL/GLX.c"

XS_EUPXS(XS_OpenGL__GLX__have_gl); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX__have_gl)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	int	RETVAL;
	dXSTARG;
#line 69 "lib/OpenGL/GLX.xs"
#ifdef HAVE_GL
	RETVAL = 1;
#else
	RETVAL = 0;
#endif /* defined HAVE_GL */
#line 232 "lib/OpenGL/GLX.c"
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_OpenGL__GLX__have_glx); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX__have_glx)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	int	RETVAL;
	dXSTARG;
#line 81 "lib/OpenGL/GLX.xs"
#ifdef HAVE_GLX
	RETVAL = 1;
#else
	RETVAL = 0;
#endif /* defined HAVE_GLX */
#line 255 "lib/OpenGL/GLX.c"
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_OpenGL__GLX__have_glp); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX__have_glp)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	int	RETVAL;
	dXSTARG;
#line 93 "lib/OpenGL/GLX.xs"
#ifdef HAVE_GLX
	RETVAL = 1;
#else
	RETVAL = 0;
#endif /* defined HAVE_GLX */
#line 278 "lib/OpenGL/GLX.c"
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_OpenGL__GLX___had_dbuffer_hack); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX___had_dbuffer_hack)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    {
	int	RETVAL;
	dXSTARG;

	RETVAL = __had_dbuffer_hack();
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}

#ifdef HAVE_GLX			/* GLX */
#define XSubPPtmpAAAA 1


XS_EUPXS(XS_OpenGL__GLX_glpRequestContext); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpRequestContext)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "major, minor, want_core");
    {
	int	major = (int)SvIV(ST(0))
;
	int	minor = (int)SvIV(ST(1))
;
	int	want_core = (int)SvIV(ST(2))
;
#line 114 "lib/OpenGL/GLX.xs"
#if defined(GLX_ARB_create_context_profile) && !defined(__APPLE__)
  requested_major = major;
  requested_minor = minor;
  requested_core = want_core;
#else
  warn("requested context but GLX_ARB_create_context_profile not available");
#endif
#line 328 "lib/OpenGL/GLX.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_OpenGL__GLX_glpcOpenWindow); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpcOpenWindow)
{
    dVAR; dXSARGS;
    if (items < 7)
       croak_xs_usage(cv,  "x, y, w, h, pw, event_mask, steal, ...");
    {
	int	x = (int)SvIV(ST(0))
;
	int	y = (int)SvIV(ST(1))
;
	int	w = (int)SvIV(ST(2))
;
	int	h = (int)SvIV(ST(3))
;
	int	pw = (int)SvIV(ST(4))
;
	long	event_mask = (long)SvIV(ST(5))
;
	int	steal = (int)SvIV(ST(6))
;
	HV *	RETVAL;
#line 133 "lib/OpenGL/GLX.xs"
{
    XEvent event;
    Window pwin = (Window)pw;
    unsigned int err;
    int *attributes = default_attributes + 2;
    int *a_buf = NULL;

    RETVAL = newHV(); /* Create hash to return GL Object info */

    if (items > NUM_ARG) {
        int i, buf_ind;
        a_buf = (int *) malloc((items-NUM_ARG+1) * 2 * sizeof(int));
        a_buf[0] = GLX_DOUBLEBUFFER; a_buf[1] = True; /* Preallocate */
        attributes = a_buf + 2;
        /* convert from glXChooseVisual to glXChooseFBConfig style */
        for (i=NUM_ARG, buf_ind=0; i<items; i++) {
            int val = SvIV(ST(i));
            switch (val) {
              case GLX_DOUBLEBUFFER:
                attributes[buf_ind++] = val;
                attributes[buf_ind++] = True;
                break;
              case GLX_RGBA:
                attributes[buf_ind++] = GLX_RENDER_TYPE;
                attributes[buf_ind++] = GLX_RGBA_BIT;
                break;
              default:
                attributes[buf_ind++] = val;
                break;
            }
        }
        attributes[buf_ind++] = None;
    }
    if (debug) {
        int i;
        for (i=0; attributes[i] != None; i++) {
            printf("att=%d 0x%x\n", i, attributes[i]);
        }
    }
    /* get a connection */
    if (!dpy_open) {
        dpy = XOpenDisplay(NULL);
        dpy_open = 1;
    }
    if (!dpy) {
        croak("ERROR: failed to get an X connection");
    } else if (debug) {
        printf("Display open %p\n", dpy);
    }

    /* get an appropriate visual */
    int num_fbc = 0;
    GLXFBConfig *fbc = glXChooseFBConfig(dpy, DefaultScreen(dpy),
                                         attributes, &num_fbc);
    if (!fbc) {/* Might have happened that one does not
                * *need* DOUBLEBUFFER, but the display does
                * not provide SINGLEBUFFER; and the semantic
                * of GLX_DOUBLEBUFFER is that if it misses,
                * only SINGLEBUFFER visuals are selected.  */
      attributes -= 2; /* GLX_DOUBLEBUFFER preallocated there */
      fbc = glXChooseFBConfig(dpy, DefaultScreen(dpy), attributes, &num_fbc); /* Retry */
      if (fbc)
          DBUFFER_HACK = 1;
    }
    if (a_buf)
        free(a_buf);
    if (!fbc)
        croak("ERROR: failed to get an XFBConfig\n");
    if (debug)
        printf("XFBConfig found %p\n", fbc[0]);
    vi = glXGetVisualFromFBConfig(dpy, fbc[0]);
    if (!vi)
        croak("ERROR: failed to get an X visual\n");
    if (debug)
        printf("Visual open %p\n", vi);

    /* create a GLX context */
#if defined(GLX_ARB_create_context_profile) && !defined(__APPLE__)
    int context_attribs[7], *ctx_attr_ptr = context_attribs;
    if (requested_major > 0 && requested_minor >= 0) {
      *ctx_attr_ptr++ = GLX_CONTEXT_MAJOR_VERSION_ARB;
      *ctx_attr_ptr++ = requested_major;
      *ctx_attr_ptr++ = GLX_CONTEXT_MINOR_VERSION_ARB;
      *ctx_attr_ptr++ = requested_minor;
    }
    if (requested_core) {
      *ctx_attr_ptr++ = GLX_CONTEXT_PROFILE_MASK_ARB;
      *ctx_attr_ptr++ = GLX_CONTEXT_CORE_PROFILE_BIT_ARB;
    }
    *ctx_attr_ptr++ = None;
    ctx = glXCreateContextAttribsARB(dpy, fbc[0], NULL, 1, context_attribs);
#else
    ctx = glXCreateContext(dpy, vi, 0, GL_TRUE);
#endif
    if (!ctx)
        croak("ERROR: failed to get an X Context");
    if (debug)
        printf("Context Created %p\n", ctx);

    /* create a color map */
    cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
            vi->visual, AllocNone);

    /* create a window */
    swa.colormap = cmap;
    swa.border_pixel = 0;
    swa.event_mask = event_mask;

    if (!pwin) {
        pwin = RootWindow(dpy, vi->screen);
        if (debug) printf("Using root as parent window 0x%lx\n", pwin);
    }
    if (steal) {
        win = nativeWindowId(dpy, pwin); /* What about depth/visual */
    } else {
        win = XCreateWindow(dpy, pwin,
                x, y, w, h,
                0, vi->depth, InputOutput, vi->visual,
                CWBorderPixel|CWColormap|CWEventMask, &swa);
        /* NOTE: PDL code had CWBackPixel above */
    }
    if (!win) {
        croak("No Window");
    } else {
        if (debug) printf("win = 0x%lx\n", win);
    }
    XMapWindow(dpy, win);
    if ( (event_mask & StructureNotifyMask) && !steal ) {
        XIfEvent(dpy, &event, WaitForNotify, (char*)win);
    }

    /* connect the context to the window */
    if (!glXMakeCurrent(dpy, win, ctx))
        croak("Non current");

    if (debug)
        printf("Display=%p Window=0x%lx Context=%p\n",dpy, win, ctx);

    /* Create the GL object hash information */
    hv_store(RETVAL, "Display", strlen("Display"), newSViv(PTR2IV(dpy)), 0);
    hv_store(RETVAL, "Window", strlen("Window"),   newSViv(  (IV) win ), 0);
    hv_store(RETVAL, "Context", strlen("Context"), newSViv(PTR2IV(ctx)), 0);

    hv_store(RETVAL, "GL_Version",strlen("GL_Version"),
            newSVpv((char *) glGetString(GL_VERSION),0),0);
    hv_store(RETVAL, "GL_Vendor",strlen("GL_Vendor"),
            newSVpv((char *) glGetString(GL_VENDOR),0),0);
    hv_store(RETVAL, "GL_Renderer",strlen("GL_Renderer"),
            newSVpv((char *) glGetString(GL_RENDERER),0),0);

    /* clear the buffer */
    glClearColor(0,0,0,1);
    while ( (err = glGetError()) != GL_NO_ERROR ) {
        printf("ERROR issued in GL %s\n", gluErrorString(err));
    }
}
#line 513 "lib/OpenGL/GLX.c"
	{
	    SV * RETVALSV;
	    RETVALSV = newRV((SV*)RETVAL);
	    RETVALSV = sv_2mortal(RETVALSV);
	    ST(0) = RETVALSV;
	}
    }
    XSRETURN(1);
}


XS_EUPXS(XS_OpenGL__GLX_glpRasterFont); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpRasterFont)
{
    dVAR; dXSARGS;
    if (items != 4)
       croak_xs_usage(cv,  "name, base, number, d");
    {
	char *	name = (char *)SvPV_nolen(ST(0))
;
	int	base = (int)SvIV(ST(1))
;
	int	number = (int)SvIV(ST(2))
;
	Display *	d = INT2PTR(Display *,SvIV(ST(3)))
;
	int	RETVAL;
	dXSTARG;
#line 300 "lib/OpenGL/GLX.xs"
        {
                XFontStruct *fi;
                int lb;
                fi = XLoadQueryFont(d,name);
                if (fi == NULL) {
                        die("No font %s found",name);
                }
                lb = glGenLists(number);
                if (lb == 0) {
                        die("No display lists left for font %s (need %d)",name,number);
                }
                glXUseXFont(fi->fid, base, number, lb);
                RETVAL=lb;
        }
#line 557 "lib/OpenGL/GLX.c"
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_OpenGL__GLX_glpPrintString); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpPrintString)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "base, str");
    {
	int	base = (int)SvIV(ST(0))
;
	char *	str = (char *)SvPV_nolen(ST(1))
;
#line 323 "lib/OpenGL/GLX.xs"
        {
                glPushAttrib(GL_LIST_BIT);
                glListBase(base);
                glCallLists(strlen(str),GL_UNSIGNED_BYTE,(GLubyte*)str);
                glPopAttrib();
        }
#line 583 "lib/OpenGL/GLX.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_OpenGL__GLX_glpDisplay); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpDisplay)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "name");
    {
	char *	name = (char *)SvPV_nolen(ST(0))
;
	Display *	RETVAL;
	dXSTARG;
#line 335 "lib/OpenGL/GLX.xs"
	    /* get a connection */
	    if (!dpy_open) {
		dpy = XOpenDisplay(name);
		dpy_open = 1;
	    }
	    if (!dpy)
		croak("No display!");
	    RETVAL = dpy;
#line 609 "lib/OpenGL/GLX.c"
	XSprePUSH;
	PUSHi(PTR2IV(RETVAL));
    }
    XSRETURN(1);
}


XS_EUPXS(XS_OpenGL__GLX_glpMoveResizeWindow); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpMoveResizeWindow)
{
    dVAR; dXSARGS;
    if (items < 4 || items > 6)
       croak_xs_usage(cv,  "x, y, width, height, w=win, d=dpy");
    {
	void*	d;
	GLXDrawable	w;
	int	x = (int)SvIV(ST(0))
;
	int	y = (int)SvIV(ST(1))
;
	unsigned int	width = (unsigned int)SvUV(ST(2))
;
	unsigned int	height = (unsigned int)SvUV(ST(3))
;

	if (items < 6)
	    d = dpy;
	else {
	    d = INT2PTR(void *,SvIV(ST(5)))
;
	}

	if (items < 5)
	    w = win;
	else {
	    w = INT2PTR(GLXDrawable,SvIV(ST(4)))
;
	}

	glpMoveResizeWindow(x, y, width, height, w, d);
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_OpenGL__GLX_glpMoveWindow); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpMoveWindow)
{
    dVAR; dXSARGS;
    if (items < 2 || items > 4)
       croak_xs_usage(cv,  "x, y, w=win, d=dpy");
    {
	void*	d;
	GLXDrawable	w;
	int	x = (int)SvIV(ST(0))
;
	int	y = (int)SvIV(ST(1))
;

	if (items < 4)
	    d = dpy;
	else {
	    d = INT2PTR(void *,SvIV(ST(3)))
;
	}

	if (items < 3)
	    w = win;
	else {
	    w = INT2PTR(GLXDrawable,SvIV(ST(2)))
;
	}

	glpMoveWindow(x, y, w, d);
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_OpenGL__GLX_glpResizeWindow); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpResizeWindow)
{
    dVAR; dXSARGS;
    if (items < 2 || items > 4)
       croak_xs_usage(cv,  "width, height, w=win, d=dpy");
    {
	void*	d;
	GLXDrawable	w;
	unsigned int	width = (unsigned int)SvUV(ST(0))
;
	unsigned int	height = (unsigned int)SvUV(ST(1))
;

	if (items < 4)
	    d = dpy;
	else {
	    d = INT2PTR(void *,SvIV(ST(3)))
;
	}

	if (items < 3)
	    w = win;
	else {
	    w = INT2PTR(GLXDrawable,SvIV(ST(2)))
;
	}

	glpResizeWindow(width, height, w, d);
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_OpenGL__GLX_glXSwapBuffers); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glXSwapBuffers)
{
    dVAR; dXSARGS;
    if (items < 0 || items > 2)
       croak_xs_usage(cv,  "w=win, d=dpy");
    {
	void *	d;
	GLXDrawable	w;

	if (items < 2)
	    d = dpy;
	else {
	    d = INT2PTR(void *,SvIV(ST(1)))
;
	}

	if (items < 1)
	    w = win;
	else {
	    w = INT2PTR(GLXDrawable,SvIV(ST(0)))
;
	}
#line 381 "lib/OpenGL/GLX.xs"
	{
	    glXSwapBuffers(d,w);
	}
#line 750 "lib/OpenGL/GLX.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_OpenGL__GLX_XPending); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_XPending)
{
    dVAR; dXSARGS;
    if (items < 0 || items > 1)
       croak_xs_usage(cv,  "d=dpy");
    {
	void *	d;
	int	RETVAL;
	dXSTARG;

	if (items < 1)
	    d = dpy;
	else {
	    d = INT2PTR(void *,SvIV(ST(0)))
;
	}
#line 390 "lib/OpenGL/GLX.xs"
		if (!d) croak("ERROR: called XPending with null X connection");
		RETVAL = XPending(d);
#line 776 "lib/OpenGL/GLX.c"
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_OpenGL__GLX_glpXNextEvent); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpXNextEvent)
{
    dVAR; dXSARGS;
    if (items < 0 || items > 1)
       croak_xs_usage(cv,  "d=dpy");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	void *	d;

	if (items < 1)
	    d = dpy;
	else {
	    d = INT2PTR(void *,SvIV(ST(0)))
;
	}
#line 400 "lib/OpenGL/GLX.xs"
	{
		XEvent event;
		char buf[10];
		KeySym ks;
		XNextEvent(d,&event);
		switch(event.type) {
			case ConfigureNotify:
				EXTEND(sp,3);
				PUSHs(sv_2mortal(newSViv(event.type)));
				PUSHs(sv_2mortal(newSViv(event.xconfigure.width)));
				PUSHs(sv_2mortal(newSViv(event.xconfigure.height)));
				break;
			case KeyPress:
			case KeyRelease:
				EXTEND(sp,2);
				PUSHs(sv_2mortal(newSViv(event.type)));
				XLookupString(&event.xkey,buf,sizeof(buf),&ks,0);
				buf[0]=(char)ks;buf[1]='\0';
				PUSHs(sv_2mortal(newSVpv(buf,1)));
				break;
			case ButtonPress:
			case ButtonRelease:
				EXTEND(sp,7);
				PUSHs(sv_2mortal(newSViv(event.type)));
				PUSHs(sv_2mortal(newSViv(event.xbutton.button)));
				PUSHs(sv_2mortal(newSViv(event.xbutton.x)));
				PUSHs(sv_2mortal(newSViv(event.xbutton.y)));
				PUSHs(sv_2mortal(newSViv(event.xbutton.x_root)));
				PUSHs(sv_2mortal(newSViv(event.xbutton.y_root)));
				PUSHs(sv_2mortal(newSViv(event.xbutton.state)));
				break;
			case MotionNotify:
				EXTEND(sp,4);
				PUSHs(sv_2mortal(newSViv(event.type)));
				PUSHs(sv_2mortal(newSViv(event.xmotion.state)));
				PUSHs(sv_2mortal(newSViv(event.xmotion.x)));
				PUSHs(sv_2mortal(newSViv(event.xmotion.y)));
				break;
			case Expose:
			default:
				EXTEND(sp,1);
				PUSHs(sv_2mortal(newSViv(event.type)));
				break;
		}
	}
#line 847 "lib/OpenGL/GLX.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_OpenGL__GLX_glpXQueryPointer); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpXQueryPointer)
{
    dVAR; dXSARGS;
    if (items < 0 || items > 2)
       croak_xs_usage(cv,  "w=win, d=dpy");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	void *	d;
	GLXDrawable	w;

	if (items < 2)
	    d = dpy;
	else {
	    d = INT2PTR(void *,SvIV(ST(1)))
;
	}

	if (items < 1)
	    w = win;
	else {
	    w = INT2PTR(GLXDrawable,SvIV(ST(0)))
;
	}
#line 452 "lib/OpenGL/GLX.xs"
	{
		int x,y,rx,ry;
		Window r,c;
		unsigned int m;
		XQueryPointer(d,w,&r,&c,&rx,&ry,&x,&y,&m);
		EXTEND(sp,3);
		PUSHs(sv_2mortal(newSViv(x)));
		PUSHs(sv_2mortal(newSViv(y)));
		PUSHs(sv_2mortal(newSViv(m)));
	}
#line 890 "lib/OpenGL/GLX.c"
	PUTBACK;
	return;
    }
}

#endif /* defined HAVE_GLX */

XS_EUPXS(XS_OpenGL__GLX_glpSetDebug); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpSetDebug)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "flag");
    {
	int	flag = (int)SvIV(ST(0))
;
#line 471 "lib/OpenGL/GLX.xs"
        {
        debug = flag;
        }
#line 911 "lib/OpenGL/GLX.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_OpenGL__GLX_glpReadTex); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_OpenGL__GLX_glpReadTex)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "file");
    {
	char *	file = (char *)SvPV_nolen(ST(0))
;
#line 481 "lib/OpenGL/GLX.xs"
	{
		GLsizei w,h;
		int d,i;
		char buf[250];
		unsigned char *image;
		FILE *fp;
		char *ret;

		fp=fopen(file,"r");

		if (!fp)	croak("couldn't open file %s",file);

		ret = fgets(buf,250,fp);		/* P3 */

		if (buf[0] != 'P' || buf[1] != '3')
			croak("Format is not P3 in file %s",file);

		ret = fgets(buf,250,fp);

		while (buf[0] == '#' && fgets(buf,250,fp));	/* Empty */

		if (2 != sscanf(buf,"%d%d",&w,&h))
			croak("couldn't read image size from file %s",file);
		if (1 != fscanf(fp,"%d",&d))
			croak("couldn't read image depth from file %s",file);
		if (d != 255)
			croak("image depth != 255 in file %s unsupported",file);
		if (w>10000 || h>10000)
			croak("suspicious size w=%d d=%d in file %s", w, d, file);

		New(1431, image, w*h*3, unsigned char);

		for (i=0;i<w*h*3;i++) {
			int v;

			if (1 != fscanf(fp,"%d",&v)) {
				Safefree(image);
				croak("Error reading number #%d of %d from file %s", i, w*h*3,file);
			}

			image[i]=(unsigned char) v;
		}

		fclose(fp);

		glTexImage2D(GL_TEXTURE_2D, 0, 3, w,h,
			0, GL_RGB, GL_UNSIGNED_BYTE,image);
	}
#line 975 "lib/OpenGL/GLX.c"
    }
    XSRETURN_EMPTY;
}

#ifdef __cplusplus
extern "C" {
#endif
XS_EXTERNAL(boot_OpenGL__GLX); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_OpenGL__GLX)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        newXS_deffile("OpenGL::GLX::_have_gl", XS_OpenGL__GLX__have_gl);
        newXS_deffile("OpenGL::GLX::_have_glx", XS_OpenGL__GLX__have_glx);
        newXS_deffile("OpenGL::GLX::_have_glp", XS_OpenGL__GLX__have_glp);
        newXS_deffile("OpenGL::GLX::__had_dbuffer_hack", XS_OpenGL__GLX___had_dbuffer_hack);
#if XSubPPtmpAAAA
        newXS_deffile("OpenGL::GLX::glpRequestContext", XS_OpenGL__GLX_glpRequestContext);
        newXS_deffile("OpenGL::GLX::glpcOpenWindow", XS_OpenGL__GLX_glpcOpenWindow);
        newXS_deffile("OpenGL::GLX::glpRasterFont", XS_OpenGL__GLX_glpRasterFont);
        newXS_deffile("OpenGL::GLX::glpPrintString", XS_OpenGL__GLX_glpPrintString);
        newXS_deffile("OpenGL::GLX::glpDisplay", XS_OpenGL__GLX_glpDisplay);
        newXS_deffile("OpenGL::GLX::glpMoveResizeWindow", XS_OpenGL__GLX_glpMoveResizeWindow);
        newXS_deffile("OpenGL::GLX::glpMoveWindow", XS_OpenGL__GLX_glpMoveWindow);
        newXS_deffile("OpenGL::GLX::glpResizeWindow", XS_OpenGL__GLX_glpResizeWindow);
        newXS_deffile("OpenGL::GLX::glXSwapBuffers", XS_OpenGL__GLX_glXSwapBuffers);
        newXS_deffile("OpenGL::GLX::XPending", XS_OpenGL__GLX_XPending);
        newXS_deffile("OpenGL::GLX::glpXNextEvent", XS_OpenGL__GLX_glpXNextEvent);
        newXS_deffile("OpenGL::GLX::glpXQueryPointer", XS_OpenGL__GLX_glpXQueryPointer);
#endif
        newXS_deffile("OpenGL::GLX::glpSetDebug", XS_OpenGL__GLX_glpSetDebug);
        newXS_deffile("OpenGL::GLX::glpReadTex", XS_OpenGL__GLX_glpReadTex);

    /* Initialisation Section */

#if XSubPPtmpAAAA
#endif
#line 531 "lib/OpenGL/GLX.xs"
{
   HV *stash = gv_stashpvn("OpenGL::GLX", strlen("OpenGL::GLX"), TRUE);
#include "glx_const.h"
}

#line 1039 "lib/OpenGL/GLX.c"

    /* End of Initialisation Section */

#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

#ifdef __cplusplus
}
#endif
