/*
        SCOLPLUGIN.H . 1996-99 . Sylvain HUET

         definitions pour les plugins Scol
*/

#ifdef __cplusplus
extern "C" {
#endif

#ifndef SCOLPLUGIN_H
#define SCOLPLUGIN_H

#include <stdio.h>
#include <windows.h>


/* DEFINITIONS DE TYPES ET DE CONSTANTES
  ---------------------------------------*/
#define SizeWord 4
#define SizeHeader 3

#define NIL -1

/* machine virtuelle Scol */
struct Mmachine
{
  int *tape;     /* pointeur vers la bande       */
  int sizetape;  /* taille de la bande (en mots) */
  int pp;        /* pointeur de pile             */
  int maxpp;     /* valeur max de pp             */
  int topheap;   /* sommet du tas                */
  int sigGC;     /* distance declenchant le GC   */
  int h;         /* position header courant      */
  int err;
};
typedef struct Mmachine *mmachine;

/* masque pour MMmalloc : tableau d'objets ou buffer quelconque */
#define TYPETAB 1
#define TYPEBUF 0

/* definitions des erreurs */
#define MERREND     1
#define MERROK      0
#define MERRMEM    -1
#define MERRTYP    -2
#define MERRRET    -3
#define MERRNUL    -4
#define MERRNF     -5
#define MERRLINK   -6
#define MERRFULL   -7
#define MERRUNKNOWN -8
#define MERRTOOLARGE -9

/* definition des offsets objets */
#define OFFSCCUR 0

#define OFFOBJMAG   0
#define OFFOBJCHN   1
#define OFFOBJTYP   2
#define OFFOBJHAND  3
#define OFFOBJTPERE 4
#define OFFOBJHPERE 5
#define OFFOBJREF0  6
#define OFFOBJUSER0 7

/* definitions des masques de message console */
#define MSKFOO     1
#define MSKRUNTIME 2
#define MSKWARNING 4
#define MSKTRACE   8
#define MSKDEBUG   16


/* types Scol predefinis */
#define TYPVAR    -1
#define TYPCONS   -2
#define TYPTYPE   -3
#define TYPFIELD  -4
#define TYPSTRUC  -5
#define TYPCONS0  -6
#define TYPCOM    -7
#define TYPCOMV   -8


/* GESTION DES FICHIERS EN SCOL
  ------------------------------*/

#define SIZESIGN 1024

struct Packdir
{
	char path[SIZESIGN];		// se termine par '/'
	int quota;					// -1: read only, 0: unlimited
	struct Packdir *next;
};
typedef struct Packdir *packdir;

#define TYPESNONE  0
#define TYPESLOGIC 1


/* FONCTIONS DU KERNEL EXPORTEES VERS LE PLUGIN
  ----------------------------------------------*/
// fonctions de conversion Entier(I)/Pointeur(P) <-> mot Scol(O)
#define ITOO(x) ((x)<<1)
#define PTOO(x) (((x)<<1)+1)
#define OTOI(x) ((x)>>1)
#define OTOP(x) ((x)>>1)






struct CBmachine
{
  int  (*MMpush)(mmachine m, int val);
  int  (*MMpushNoGC)(mmachine m, int val);
  int  (*MMpull)(mmachine m);
  int  (*MMgetPP)(mmachine m);
  void (*MMsetPP)(mmachine m,int i);
  int  (*MMget)(mmachine m,int i);
  void (*MMset)(mmachine m,int i,int v);
  int  (*MMgetglobal)(mmachine m,int i);
  void (*MMsetglobal)(mmachine m,int i,int v);
  int  (*MMgetbase)(mmachine m,int b,int i);
  void (*MMsetbase)(mmachine m,int b,int i,int v);
  int  (*MMfetch)(mmachine m,int s,int i);
  void (*MMstore)(mmachine m,int s,int i,int v);
  int* (*MMstart)(mmachine m,int s);
  int  (*MMsize)(mmachine m,int s);
  int  (*MMtype)(mmachine m,int s);
  char* (*MMstartstr)(mmachine m,int s);
  int  (*MMsizestr)(mmachine m,int s);
  void (*MMsetsizestr)(mmachine m,int s,int size);
  int  (*MMmalloc)(mmachine m, int size, int type);
  int  (*MMmallocCLR)(mmachine m, int size, int type);
  int  (*Mpushstrbloc)(mmachine m,char *buf);
  int  (*MBdeftab)(mmachine m);
  int  (*MBstrcat)(mmachine m);
  int  (*PKhardpak)(mmachine m, char *name,
                   int n, char **namefun, int (**fun)(mmachine z),
                   int *nargfun, char **typfun);
  int  (*PKloader)(mmachine m,char *name,char *simplename);
  int  (*SPfindfile)(packdir p,char *sign,int *size,char *path);
  int  (*SPaddfile)(packdir p,char *sign,int size,char *path);
  int  (*SCgetsrvsocket)(mmachine m, int server);
  int  (*SCgetsocket)(mmachine m, int channel);
  int  (*Mcutting)(char *comm, char **argv);
  void (*MMechostr)(int i,char *buf,...);
  size_t (*fread)(void* buf,size_t i,size_t j,FILE *f);
  //int (*fread)(char* buf,int i,int j,FILE *f);
  packdir Firstpack;
  int  (*OBJcreate)(mmachine m,int typ,int handsys,int typpere,int handsyspere);
  int  (*OBJaddreflex)(mmachine m,int typ, int num);
  int  (*OBJbeginreflex)(mmachine m,int typ,int handsys,int num);
  int  (*OBJdel)(mmachine m,int p,int flag);
  int  (*OBJdestroy)(mmachine m, int typ,int handsys,int objm,int flag);
  int  (*OBJdelTH)(mmachine m,int typ,int handsys);
  int  (*OBJcallreflex)(mmachine m,int nbarg);
  int  (*OBJregister)(int nbrefl,int recrefl,int (*destroy)(mmachine m,int handsys,int objm),char *name);
  int  (*OBJtypebyname)(char *name);
  //int  (*OBJdefEvent)(int event,int (*cbevent)(mmachine m,int hwnd, unsigned msg,
//				              int wParam,int lParam,int *ret));
  int (*OBJdefEvent)(int event,int (*cbevent)(mmachine m,HWND hwnd, unsigned msg,UINT wParam,LONG lParam,int *ret));
  int  (*OBJgetUserEvent)();
  void* (*SCgetExtra)(char *funcname);
  char* (*stdloadJpg)(char *name,int *w,int *h);
  int  (*OBJdelTM)(mmachine m,int typ,int q);
  int  (*OBJfindTH)(mmachine m,int typ,int handsys);
  int  (*OBJfindTM)(mmachine m,int typ,int objm);
  int (*Mpushstrblocn)(mmachine m,char *buf, int n);
  int (*Minterpreter)(mmachine m);
  int (*Msearchinsyspak)(mmachine m, char *name);
  int (*MBstrcatn)(mmachine m);
  int (*Buffer2texture)(mmachine m, char *buffer, int width, int height);
  void (*MMputs)(int i, const char* text);

  //$ LB (13/06/2002)
  int (*OBJgetNewHandler)();


};

typedef struct CBmachine *cbmachine;

extern	cbmachine ww;



#define SCOLinitplugin(X) ww=X

#define MMpush (ww->MMpush)
#define MMpushNoGC (ww->MMpushNoGC)
#define MMpull (ww->MMpull)
#define MMgetPP (ww->MMgetPP)
#define MMsetPP (ww->MMsetPP)
#define MMget (ww->MMget)
#define MMset (ww->MMset)
#define MMgetglobal (ww->MMgetglobal)
#define MMsetglobal (ww->MMsetglobal)
#define MMgetbase (ww->MMgetbase)
#define MMsetbase (ww->MMsetbase)
#define MMfetch (ww->MMfetch)
#define MMstore (ww->MMstore)
#define MMstart (ww->MMstart)
#define MMsize (ww->MMsize)
#define MMtype (ww->MMtype)
#define MMstartstr (ww->MMstartstr)
#define MMsizestr (ww->MMsizestr)
#define MMsetsizestr (ww->MMsetsizestr)
#define MMmalloc (ww->MMmalloc)
#define MMmallocCLR (ww->MMmallocCLR)
#define Mpushstrbloc (ww->Mpushstrbloc)
#define MBdeftab (ww->MBdeftab)
#define MBstrcat (ww->MBstrcat)
#define PKhardpak (ww->PKhardpak)
#define PKloader (ww->PKloader)
#define SPfindfile (ww->SPfindfile)
#define SPaddfile (ww->SPaddfile)
#define SCgetsrvsocket (ww->SCgetsrvsocket)
#define SCgetsocket (ww->SCgetsocket)
#define Mcutting (ww->Mcutting)
#define MMechostr (ww->MMechostr)
//#define fread (ww->fread)
#define Firstpack (ww->Firstpack)
#define OBJcreate (ww->OBJcreate)
#define OBJaddreflex (ww->OBJaddreflex)
#define OBJbeginreflex (ww->OBJbeginreflex)
#define OBJdel (ww->OBJdel)
#define OBJdestroy (ww->OBJdestroy)
#define OBJdelTH (ww->OBJdelTH)
#define OBJcallreflex (ww->OBJcallreflex)
#define OBJregister (ww->OBJregister)
#define OBJtypebyname (ww->OBJtypebyname)
#define OBJdefEvent (ww->OBJdefEvent)
#define OBJgetUserEvent (ww->OBJgetUserEvent)
#define SCgetExtra (ww->SCgetExtra)
#define stdloadJpg (ww->stdloadJpg)
#define OBJdelTM (ww->OBJdelTM)
#define OBJfindTH (ww->OBJfindTH)
#define OBJfindTM (ww->OBJfindTM)
#define Mpushstrblocn (ww->Mpushstrblocn)
#define Minterpreter (ww->Minterpreter)
#define Msearchinsyspak (ww->Msearchinsyspak)
#define MBstrcatn (ww->MBstrcatn)
#define Buf2Tex (ww->Buffer2texture)
#define MMputs (ww->MMputs)

//$ LB (13/06/2002)
#define OBJgetNewHandler (ww->OBJgetNewHandler)


#endif

#ifdef __cplusplus
}
#endif
