/*     
      SCOL ENVIRONMENT . Magma 1.0 . 1996 . Sylvain HUET

        hardload.c : chargement des packages systèmes
*/
//$ FA(26/10/2001): Recreate the base environment in a hash table

#include <stdio.h>
#include <string.h>
#include <dlfcn.h>

#include "../kernel/include/vscol.h"
#include "../kernel/include/kernel.h"
#include "../kernel/scol.h"
#include "../kernel/scolobj.h"
#include "cbmachine.h"
#include "../kernel/listlab.h"
#include "../kernel/macros.h"

int GETactiveXWindow ( mmachine m );

int SCOLloadlibHTML(mmachine m);
int SCOLloadlib2d(mmachine m);
int SCOLloadREAL(mmachine m);
int SCOLloadQuickTime(mmachine m);
int SCOLloadSound(mmachine m);
int SCOLloadXTension(mmachine m);

int SCOLloadZooEngine(mmachine m);
int SCOLloadZooParticle(mmachine m);
int SCOLloadZooCB3D(mmachine m);

int SCOLloadsurface(mmachine m);
int SCOLloadiwnhead(mmachine m);
int SCOLloadhelp(mmachine m);
int SCOLloadlisttab(mmachine m);
int SCOLloadlistbitmap(mmachine m);
int SCOLloadwtree(mmachine m);
int SCOLloadrichtext(mmachine m);
int SCOLloadlistbox(mmachine m);
int SCOLloadcursor(mmachine m);
int SCOLloadfont(mmachine m);
int SCOLloadbitmap(mmachine m);
int SCOLloadbox(mmachine m);
int SCOLloadmenu(mmachine m);
int SCOLloadbutton(mmachine m);
int SCOLloadtext(mmachine m);
int SCOLloadwindow(mmachine m);
int SCOLloadconst(mmachine m);
int SCOLloadtypes(mmachine m);

int SCOLloadJPEG(mmachine m);

int IniTELNET(mmachine m);
int SCOLloadHTTP(mmachine m);
int SCOLloadACTIVEX(mmachine m);
int SCOLloadTASKICON(mmachine m);
int SCOLloadVDOCODEC(mmachine m);
int SCOLloadVIDEO(mmachine m);
int SCOLloadPRINTER(mmachine m);
int SCOLloadAudioCmp(mmachine m);
int SCOLloadlogger(mmachine m);
int SCOLloadwave(mmachine m);
int SCOLloadBN(mmachine m);
int SCOLloadDLG(mmachine m);
int SCOLloadtxtsys(mmachine m);
int SCOLloadHTTPserver(mmachine m);
int SCOLloadAlphaBitmap(mmachine m);

int SCOLloadSYS(mmachine m);
int SCOLloadBase(mmachine m);

int (*CBinit)(mmachine m);

char* stdloadJpg(char *name,int *w,int *h)
{
  return NULL;
}

/*extern int hscol;*/
/*extern char hardware3d[128];*/
extern int Minterpreter(mmachine m);
extern int Msearchinsyspak(mmachine m, char *name);


void* SCgetExtra(char *funcname)
{
//  if (!strcmp(funcname,"hscol")) return hscol;
//  if (!strcmp(funcname,"this_inst")) return thisinst;
  if (!strcmp(funcname,"FirstPack")) return Firstpack;
//  if (!strcmp(funcname,"free")) return free;
//  if (!strcmp(funcname,"3dhardware")) return hardware3d;
//  if (!strcmp(funcname,"regX3D")) return regX3D;
//  if (!strcmp(funcname,"GRCreateBitmap")) return GRCreateBitmap;
//  if (!strcmp(funcname,"GRCreateBitmap8")) return GRCreateBitmap8;
//  if (!strcmp(funcname,"GRDestroyBitmap")) return GRDestroyBitmap;
//  if (!strcmp(funcname,"GRGetBitmapSize")) return GRGetBitmapSize;
//  if (!strcmp(funcname,"GRCopyBitmap")) return GRCopyBitmap;
//  if (!strcmp(funcname,"mt_start")) return mt_start;
//  if (!strcmp(funcname,"mt_del")) return mt_del;
//  if (!strcmp(funcname,"MX3registerDevice")) return MX3registerDevice;

  return NULL;
}

int SCinitCBm(cbmachine w)
{
    w->MMpush=MMpush;
    w->MMpushNoGC=MMpushNoGC;
    w->MMpull=MMpull;
    w->MMgetPP=MMgetPP;
    w->MMsetPP=MMsetPP;
    w->MMget=MMget;
    w->MMset=MMset;
    w->MMgetglobal=MMgetglobal;
    w->MMsetglobal=MMsetglobal;
    w->MMgetbase=MMgetbase;
    w->MMsetbase=MMsetbase;
    w->MMfetch=MMfetch;
    w->MMstore=MMstore;
    w->MMstart=MMstart;
    w->MMsize=MMsize;
    w->MMtype=MMtype;
    w->MMstartstr=MMstartstr;
    w->MMsizestr=MMsizestr;
    w->MMsetsizestr=MMsetsizestr;
    w->MMmalloc=MMmalloc;
    w->MMmallocCLR=MMmallocCLR;
    w->Mpushstrbloc=Mpushstrbloc;
    w->MBdeftab=MBdeftab;
    w->MBstrcat=MBstrcat;
    w->PKhardpak=PKhardpak;
    w->PKloader=PKloader;
    w->SPfindfile=SPfindfile;
    w->SPaddfile=SPaddfile;
    w->SCgetsrvsocket=SCgetsrvsocket;
    w->SCgetsocket=SCgetsocket;
    w->Mcutting=Mcutting;
    w->MMechostr=MMechostr;
    w->fread=fread;
    w->Firstpack=Firstpack;
    w->OBJcreate=OBJcreate;     
    w->OBJaddreflex=OBJaddreflex;  
    w->OBJbeginreflex=OBJbeginreflex;
    w->OBJdel=OBJdel;        
    w->OBJdestroy=OBJdestroy;    
    w->OBJdelTH=OBJdelTH;
    w->OBJcallreflex=OBJcallreflex;
    w->OBJregister=OBJregister;
    w->OBJtypebyname=OBJtypebyname;
    w->OBJdefEvent=NULL;//OBJdefEvent;
    w->OBJgetUserEvent=OBJgetUserEvent;
    w->SCgetExtra=SCgetExtra;
    w->stdloadJpg=stdloadJpg;
    w->OBJdelTM=OBJdelTM;
    w->OBJfindTH=OBJfindTH;
    w->OBJfindTM=OBJfindTM;
    w->Mpushstrblocn=Mpushstrblocn;
    w->Minterpreter=Minterpreter;
    w->Msearchinsyspak=Msearchinsyspak;
    w->MBstrcatn=MBstrcatn;
    w->Buffer2texture=NULL;
    return 0;
}

typedef int (*FCM)(mmachine,cbmachine);
typedef int (*FCLOSECM)();
typedef void *HANDLE;

struct CBmachine wcb;

#define NBDLLMAX 32
char namedll[NBDLLMAX][128];
char fundll[NBDLLMAX][128];
char funclosedll[NBDLLMAX][128];
HANDLE hdll[NBDLLMAX];
int idll=0;

int SCregistDLL(char *name,char *fun,char *funclose)
{
	if (idll>=NBDLLMAX) return 0;
	strcpy(namedll[idll],name);
	if (fun) strcpy(fundll[idll],fun);
	else fundll[idll][0]='\0';
	if (funclose) strcpy(funclosedll[idll],funclose);
	else funclosedll[idll][0]='\0';
	idll++;
	return 0;
}

int SCloadDLLs(mmachine m)
{
    int i,k;
    HANDLE hLibrary;
    FCM SCOLloadDLL;

    SCinitCBm(&wcb);
	for(i=0;i<idll;i++) {
	    printf("chargement %s\n",namedll[i]);
	    hLibrary=dlopen(namedll[i],RTLD_GLOBAL|RTLD_NOW);
		hdll[i]=hLibrary;
		if (hLibrary) {
			if (fundll[i][0]) {
				SCOLloadDLL=(FCM)dlsym(hLibrary,fundll[i]);
				if (SCOLloadDLL) {
					if ((k=SCOLloadDLL(m,&wcb))) return k;
				}
				else printf("%s\n",dlerror());
			}
		}
		else printf("%s\n",dlerror());
	}
    return 0;
}

int SCfreeDLLs()
{
    int i,k;
    HANDLE hLibrary;
	FCLOSECM SCOLcloseDLL;

	for(i=0;i<idll;i++)
	{
		hLibrary=hdll[i];
		if (hLibrary)
		{
			if ((funclosedll[i][0])
				&&(SCOLcloseDLL=(FCLOSECM)dlsym(hLibrary,funclosedll[i])))
			{
				if ((k=SCOLcloseDLL())) return k;
			}
			dlclose(hLibrary);
		}
	}
    return 0;
}

int SCinitsyspack(mmachine m)
{
  int k;

  if (MMpush(m,NIL)) return MERRMEM;


	if ((k=SCOLloadlibHTML(m))) return k;
	if ((k=SCOLloadlib2d(m))) return k;
	if ((k=SCOLloadREAL(m))) return k;
	if ((k=SCOLloadQuickTime(m))) return k;
	if ((k=SCOLloadSound(m))) return k;
	if ((k=SCOLloadXTension(m))) return k;

	if ((k=SCOLloadZooEngine(m))) return k;
	if ((k=SCOLloadZooParticle(m))) return k;
	if ((k=SCOLloadZooCB3D(m))) return k;

        if ((k=SCOLloadsurface(m))) return k;
	if ((k=SCOLloadiwnhead(m))) return k;
	if ((k=SCOLloadhelp(m))) return k;
	if ((k=SCOLloadlisttab(m))) return k;
	if ((k=SCOLloadlistbitmap(m))) return k;
	if ((k=SCOLloadwtree(m))) return k;
	if ((k=SCOLloadrichtext(m))) return k;
	if ((k=SCOLloadlistbox(m))) return k;
	if ((k=SCOLloadcursor(m))) return k;
	if ((k=SCOLloadfont(m))) return k;
	if ((k=SCOLloadbitmap(m))) return k;
	if ((k=SCOLloadbox(m))) return k;
	if ((k=SCOLloadmenu(m))) return k;
	if ((k=SCOLloadbutton(m))) return k;
	if ((k=SCOLloadtext(m))) return k;
	if ((k=SCOLloadwindow(m))) return k;
	if ((k=SCOLloadconst(m))) return k;
	if ((k=SCOLloadtypes(m))) return k;

	if ((k=SCOLloadJPEG(m))) return k;

	//$LB (19/03/2002)
	if ((k=SCOLloadINETCONNECT(m))) return k;

	if ((k=IniTELNET(m))) return k;
	if ((k=SCOLloadHTTP(m))) return k;
	if ((k=SCOLloadACTIVEX(m))) return k;
	if ((k=SCOLloadTASKICON(m))) return k;
	if ((k=SCOLloadVDOCODEC(m))) return k;
	if ((k=SCOLloadVIDEO(m))) return k;
	if ((k=SCOLloadPRINTER(m))) return k;
	if ((k=SCOLloadAudioCmp(m))) return k;
	if ((k=SCOLloadlogger(m))) return k;
	if ((k=SCOLloadwave(m))) return k;
	if ((k=SCOLloadBN(m))) return k;
	if ((k=SCOLloadDLG(m))) return k;
	if ((k=SCOLloadtxtsys(m))) return k;
	if ((k=SCOLloadHTTPserver(m))) return k;
	if ((k=SCOLloadAlphaBitmap(m))) return k;

  if ((k=SCloadDLLs(m))) return k;

  if ((k=SCOLloadSYS(m))) return k;
//$ FA(13/04/2001): Load base package
  if (k = SCOLloadBase(m))
    return k;
//$ FA(24/10/2001): Recreate the base environment in a hash table
#if !defined(SCOL_DEBUGGER_AWARE)
  SECHECK(createInitialEnvironment(m));
//printInitialEnvironment(m);
#endif
  SECHECK(SEDUP(m));
//
  if (CBinit) if ((k=CBinit(m))) return k;

  return 0;
}

