#!/usr/bin/env python
from ctypes import *
from time import *
width=320
height=200
name=c_char_p("pyGLES window")
#gles = CDLL("libGLES_CL.so") # <--- this will need changing this is the desktop software reference driver library
gles = CDLL("libGLES_CM.so") 
xlib = CDLL("libX11.so")
GL_DEPTH_BUFFER_BIT     =   0x00000100
GL_STENCIL_BUFFER_BIT   =   0x00000400
GL_COLOR_BUFFER_BIT     =   0x00004000
EGL_RED_SIZE            =   0x3024
EGL_DEPTH_SIZE          =   0x3025
EGL_NONE                =   0x3038
EGL_FALSE               =   0
EGL_TRUE                =   1
EGL_NATIVE_VISUAL_ID    =   0x302E
VisualIDMask		    =   0x1
AllocNone		        =   0	
PMinSize                =   8
PMaxSize                =   16
ExposureMask			=   1<<15 
StructureNotifyMask		=   1<<17 
KeyPressMask			=   1<<0  
ButtonPressMask			=   1<<2
ButtonReleaseMask		=   1<<3
InputOutput		        =   1
CWBorderPixel           =   1<<3
CWColormap		        =   1<<13
CWEventMask		        =   1<<11
class XVisualInfo(Structure):
    _fields_ = [
        ("visual", POINTER(c_long)), 
        ("visualid", c_long),
        ("screen", c_int),
        ("depth", c_uint),
        ("class", c_int),
        ("red_mask", c_ulong),
        ("green_mask", c_ulong),
        ("blue_mask", c_ulong),
        ("colormap_size", c_int),
        ("bits_per_rpg", c_int)
        ]
class XSetWindowAttributes(Structure):
    _fields_ = [
        ("background_pixmap", c_ulong), 
        ("background_pixel", c_ulong), 
        ("border_pixmap", c_ulong), 
        ("border_pixel", c_ulong), 
        ("bit_gravity", c_int), 
        ("win_gravity", c_int), 
        ("backing_store", c_int), 
        ("backing_planes", c_ulong), 
        ("backing_pixel", c_ulong), 
        ("save_under", c_int), 
        ("event_mask", c_long), 
        ("do_not_propagate_mask", c_long), 
        ("override_redirect", c_int), 
        ("colormap", c_ulong), 
        ("cursor", c_ulong)
        ]
class XSizeHints(Structure):
   _fields_ = [("flags",c_long),
               ("x",c_int),        # Obsolete
               ("y",c_int),        # Obsolete
               ("width",c_int),    # Obsolete
               ("height",c_int),   # Obsolete
               ("min_width",c_int),
               ("min_height",c_int),
               ("max_width",c_int),
               ("max_height",c_int),
               ("width_inc",c_int),
               ("height_inc",c_int),
               ("min_asp_width",c_int),
               ("min_asp_height",c_int),
               ("max_asp_width",c_int),
               ("max_asp_height",c_int),
               ("base_width",c_int),
               ("base_height",c_int),
               ("win_gravity",c_int)]
dpy = xlib.XOpenDisplay(0)
egldisplay = gles.eglGetDisplay(dpy);
gles.eglInitialize(egldisplay, 0, 0)
attributeList = (c_int * 5)(EGL_RED_SIZE, 1, EGL_DEPTH_SIZE, 1, EGL_NONE)
config = (c_int * 4)(0,0,0,0)
nconf = c_int(0)
r=gles.eglChooseConfig(egldisplay,attributeList,config,4,pointer(nconf))
if r!=EGL_TRUE:
    print "can't find requested config"
    exit()
cx = gles.eglCreateContext(egldisplay, config[0], 0, 0);
vid=c_int(0)
gles.eglGetConfigAttrib(egldisplay, config[0], EGL_NATIVE_VISUAL_ID, pointer(vid))
vi=XVisualInfo()
tmp=XVisualInfo()
tmp.visualid = vid
n=c_int(0)
vi = XVisualInfo(cast(xlib.XGetVisualInfo(dpy, VisualIDMask, pointer(tmp), pointer(n)),POINTER(c_long)))
swa=XSetWindowAttributes()
rw=xlib.XDefaultRootWindow(dpy)
swa.colormap = xlib.XCreateColormap(dpy, rw, vi.visual, AllocNone);
sizehints=XSizeHints()
sizehints.flags = 0;
sizehints.flags = PMinSize | PMaxSize;
sizehints.min_width = sizehints.max_width = width;
sizehints.min_height = sizehints.max_height = height;
swa.border_pixel = 0;
swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask;
window = xlib.XCreateWindow(dpy, rw, 0, 0, width, height,
                        0, vi.depth, InputOutput, vi.visual,
                        CWBorderPixel|CWColormap|CWEventMask, pointer(swa));
xlib.XSetStandardProperties(dpy, window, name, name, 0, 0, 0, pointer(sizehints));
xlib.XMapWindow(dpy, window)
eglwindow = gles.eglCreateWindowSurface(egldisplay, config[0], window, 0)
gles.eglMakeCurrent(egldisplay, eglwindow, eglwindow, cx)
gles.glClearColorx(32768, 32768, 32768, 0)
gles.glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
gles.eglSwapBuffers(egldisplay, eglwindow)
sleep(1)