// written for www.tkscript.de by Carsten Busse <car.busse@gmx.de> 06.2005
// all code in this file has the same license as the tkscript language and plugin source
///     - Distributed under terms of the Lesser GNU General Public License (LGPL).
///       See COPYING and <http://www.gnu.org/licenses/licenses.html#LGPL> for further information.


#define YAC_PRINTF
#include <yac.h>

#include "sdl_includes.h"

#ifdef YAC_WIN32
#include <windows.h>
#include <ddraw.h>
#endif

#include "HAL.h"




#ifdef TKSDL_X11_CLIPBOARD

#include <X11/Xlib.h>


#include "Clipboard.h"

YAC_String *tmpClipboard=NULL;


//void SetCutBuffer(YAC_Object *_o) {
void SDL_SetClipboard(YAC_Object *_o) {
	yac_host->printf("SDL_SetClipboard entered\n");
	if (!tmpClipboard) tmpClipboard=YAC_New_String();
	if (YAC_BCHK(_o, YAC_CLID_STRING)) {
		YAC_String *s=(YAC_String*)_o;
		if (s->length>1) {
		       SDL_SysWMinfo wmInf_;
		       SDL_VERSION(&wmInf_.version);
	               if (SDL_GetWMInfo(&wmInf_)) {
			       yac_host->printf("Got all needed infos, trying to store string in X\n");
			       Display *disp=wmInf_.info.x11.display;
			       yac_host->printf("Copying string into a global buffer, also got Display Ptr %p\n",disp);
			       tmpClipboard->yacCopy(s);
			       yac_host->printf("Giving string (%s) of %d bytes length to X\n",tmpClipboard->chars,tmpClipboard->length);
			       XStoreBytes(disp,tmpClipboard->chars,tmpClipboard->length);
		       } else {
			       yac_host->printf("[---] SDL_SetClipboard_x11: SDL_GetWMInfo() failed.\n");
			       return;
		       }
		}
	}
	yac_host->printf("SDL_SetClipboard exited\n");
}

void SDL_GetClipboard(YAC_Value *_r) {
	yac_host->printf("SDL_GetClipboard entered\n");
	YAC_String *ret=YAC_New_String();
	_r->initString(ret,1);
	SDL_SysWMinfo wmInf_;
	SDL_VERSION(&wmInf_.version);
	if (SDL_GetWMInfo(&wmInf_)) {
		Display *disp=wmInf_.info.x11.display;
		if (disp) {
			yac_host->printf("GetClip got all needed infos, trying to get string from Display Ptr %p\n",disp);
		/*else {
			yac_host->printf("Trying local display :0\n");
			disp=XOpenDisplay(":0");
			if (disp) yac_host->printf("GetClip got all it needs for Display Ptr %p\n",disp);
			else {
				yac_host->printf("GetClip miserably failed, returning\n");
				return;
			}	
		}*/
		int length;
		yac_host->printf("XFetchBytes\n");
		char *getCut=XFetchBytes(disp,&length);
	//	char *getCut=XFetchBytes(wmInf_.info.x11.display,&length);

		yac_host->printf("XFetchBytes done\n");
		if (getCut[length]==0) {
			yac_host->printf("Got %d byte length String: (%s)\n",length,getCut);
         YAC_String t; t.visit(getCut);
			ret->yacCopy(&t); // use yacCopy in order to not mix memory managers
		} else {
		        int i;
			yac_host->printf("Got non-Null terminated String...\n");
			ret->yacArrayAlloc(length+1);
			for (i=0;i<length;i++) ret->chars[i]=getCut[i];
			ret->chars[i]=0;
		}
		XFree(getCut);
		} else yac_host->printf("No Display open the moment for me :(\n");

	} else {
/*		yac_host->printf("Trying local display :0\n");
		Display *disp=XOpenDisplay(":0");
		if (disp) {
			yac_host->printf("GetClip got all needed infos for Display Ptr %p\n",disp);
			int length;
			char *getCut=XFetchBytes(disp,&length);
			yac_host->printf("Got %d byte length String: (%s)\n",length,getCut);
			ret->copy(getCut);
			XFree(getCut);
		} else
			yac_host->printf("SDL_GetClipboard error in Open X Display\n");*/
		yac_host->printf("SDL_GetClipboard failed\n");
	}
	_r->initObject(ret,1);
	yac_host->printf("SDL_GetClipboard exited\n");
}



#endif // TKSDL_X11_CLIPBOARD
