#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <iostream>

int 
main()
{
  int                width  = 100;
  int                height = 100; 
  Display*              dpy = XOpenDisplay(0);
  int               attrs[] = { GLX_RGBA, 
				   GLX_DOUBLEBUFFER,
				   GLX_DEPTH_SIZE, 
				   24, 
				   None };
  XVisualInfo*           vi = glXChooseVisual(dpy, DefaultScreen(dpy), 
					     attrs);
  GLXContext             cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  Colormap             cmap = XCreateColormap(dpy, 
					      RootWindow(dpy, vi->screen),
					      vi->visual, AllocNone);
  XSetWindowAttributes  swa;
  swa.colormap              = cmap;
  swa.border_pixel          = 0;
  swa.event_mask            = StructureNotifyMask;
  Window                win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 
					    0, 0, width, height,
					    0, vi->depth, InputOutput, 
					    vi->visual,
					    CWBorderPixel|
					    CWColormap|
					    CWEventMask, 
					    &swa);
  XMapWindow(dpy, win);
  /* Wait for it to be placed on the screen */
  while(true) {
    XEvent event;
    XNextEvent(dpy,&event);
    if ((event.type == MapNotify) && (event.xmap.window == win))
      break;
  }
  
  // connect the context to the window
  glXMakeCurrent(dpy, win, cx);

  const GLubyte* vendor     = glGetString(GL_VENDOR);
  const GLubyte* renderer   = glGetString(GL_RENDERER);
  const GLubyte* version    = glGetString(GL_VERSION);
  const GLubyte* extensions = glGetString(GL_EXTENSIONS); 
  
  printf("GL strings\n"
	 "\tVendor:\t%s\n"
	 "\tRenderer:\t%s\n"
	 "\tVersion:\t%s\n",
	 vendor, renderer, version);
  
  // sleep(10);

  return 0;
}

