

#include <X11/Xlib.h>

#include <X11/keysym.h>

#define GL_GLEXT_PROTOTYPES

#include <GL/glext.h>

#include <GL/gl.h> // GL 1.1 functions 

#include <GL/glx.h>

#include <stdio.h>

#include <hints.h>

static int DoubleBufferAttributes[] = {
	GLX_RGBA,
	GLX_RED_SIZE, 1,
	GLX_GREEN_SIZE, 1,
	GLX_BLUE_SIZE, 1,
	GLX_DEPTH_SIZE, 12,
	GLX_DOUBLEBUFFER,
	None,
};



class windowManager{
	
	Display * 	mainDisplay;

	Window 		mainWindow;

	int 		MainScreen;

	Window 		RootWindow;


	setupDisplay() {

		this->mainDisplay			= XOpenDisplay( 0 );

		this->MainScreen			= XDefaultScreen( this->mainDisplay );

	}

	setupWindow() {


		this->RootWindow			= XDefaultRootWindow( this->mainDisplay );


		int empty;

		int ResultStatus			= glXQueryExtension( this->mainDisplay, &empty, &empty );

		XVisualInfo* VisualInfo		= glXChooseVisual( this->mainDisplay, this->MainScreen, DoubleBufferAttributes );

		GLXContext ShareList		= None;

		int IsDirectRendering		= True;


		  //XMatchVisualInfo(	this->mainDisplay, this->MainScreen, 32, TrueColor, &VisualInfo);


		GLXContext OpenGLContext	= glXCreateContext( this->mainDisplay, VisualInfo, ShareList, IsDirectRendering );



		int WindowX 			= 0;

		int WindowY 			= 0;

		int WindowWidth 		= 1000;//;1920;

		int WindowHeight 		= 1024;//;72;

		int BorderWidth 		= 0;

		int WindowClass 		= InputOutput;

		int WindowDepth 		= VisualInfo->depth;


		Visual* WindowVisual 	= VisualInfo->visual;

		int AttributeValueMask 	= CWBackPixel | CWEventMask | CWColormap;



  


		XSetWindowAttributes WindowAttributes 	= {};

		WindowAttributes.colormap 				= XCreateColormap( this->mainDisplay, this->RootWindow, VisualInfo->visual, AllocNone );

		//WindowAttributes.background_pixel 		= 0xffafe9af;

		WindowAttributes.background_pixel = 0;


		WindowAttributes.event_mask 			= StructureNotifyMask | KeyPressMask | KeyReleaseMask | PointerMotionMask;

		this->mainWindow 						= XCreateWindow( this->mainDisplay, this->RootWindow, 
																WindowX, WindowY, WindowWidth, WindowHeight,
																BorderWidth, WindowDepth, WindowClass, WindowVisual,
																AttributeValueMask, &WindowAttributes);


		//XMoveWindow( this->mainDisplay, this->mainWindow, 0, 800 );

		XMoveWindow( this->mainDisplay, this->mainWindow, -400, 0 );

		XStoreName( this->mainDisplay, this->mainWindow, "Opengl: Fixed function pipeline" );

		glXMakeCurrent( this->mainDisplay, this->mainWindow, OpenGLContext );

		XMapWindow( this->mainDisplay, this->mainWindow );

		
//W = DisplayWidth(display, defaultScreen);
//H = DisplayHeight(display, defaultScreen);
		

		/*

			Remove borders

		*/
		
/*

		Atom window_type 				= XInternAtom( this->mainDisplay, "_MOTIF_WM_HINTS", True );

		Hints hints;

		Atom property;

		hints.flags 		= 2;

		hints.decorations 	= 0;

		XChangeProperty( this->mainDisplay, this->mainWindow, window_type, window_type, 32, PropModeReplace, ( unsigned char * ) & hints, 5 );


*/

		Atom WM_DELETE_WINDOW 			= XInternAtom( this->mainDisplay, "WM_DELETE_WINDOW", False );


		if( !XSetWMProtocols( this->mainDisplay, this->mainWindow, &WM_DELETE_WINDOW, 1) ) {

		    printf( "Couldn't register WM_DELETE_WINDOW\n" );

		}

	}

}