#include	<stdio.h>
#include	<exec/types.h>
#include	<exec/tasks.h>
#include	<exec/ports.h>
#include	<graphics/displayinfo.h>
#include	<exec/tasks.h>
#include	<dos/dosextens.h>
#include	<clib/exec_protos.h>
#include	"/includes/struct.h"
#include	"/includes/tom_gadget.h"

struct my_data	*p_info;
struct	appel_proc	*p_data_proc;

struct	Library	*IntuitionBase;
struct	Library	*GfxBase;
extern	struct	tom_gadget	my_gadg[];
extern	void	__saveds	dark();
extern	void	proc_init();
extern	void	proc_end();
extern	char	*p_text_info;

BPTR	hd=0;
char	pr_buff[100];

/* Note: The folloing SPrintF Routine is taken from the program "FracBlank " */
/* by:									     */
UWORD PutChar[2] = {0x16C0,0x4E75};

/* dirty hack to avoid assembler part :-)
   16C0: move.b d0,(a3)+
   4E75: rts
*/

#define VOID_STDARGS   void __stdargs

VOID_STDARGS SPrintF(char *pr_buff,char *FormatString,...)

{
 RawDoFmt (FormatString,(APTR)((LONG *)&FormatString+1L),
           (void *)PutChar,pr_buff);
}
mystrlen(char	*pc)
{
	int	ret;
	ret=0;
	while(*pc++!=0){
		ret++;
	}
	return	ret;
}

void	MyWrite(char	*pc)
{
	if(hd!=0)Write(hd,pc,mystrlen(pc));
}
void	My_Print(struct RastPort *rp,UWORD x,UWORD y,char	*pc)
{
	Move(rp,x,y);
	Text(rp,pc,mystrlen(pc));
}

#ifdef	DEBUG
VOID_STDARGS printt(char *FormatString,...)

{
	char	buffer[100];
	 RawDoFmt (FormatString,(APTR)((LONG *)&FormatString+1L),
           (void *)PutChar,buffer);
	MyWrite(buffer);
}
#endif

/* Checrche une resolution correspondant aux mieu a nos demandes */
/* si flg_depth=TRUE, alors on cherche la prof max corresp a la	 */
/* resolution demandee						 */

int	find_res(USHORT flg_depth,SHORT *p_depth,SHORT *p_tx,SHORT *p_ty,ULONG *p_d_id)
{
 struct ModeNode *ModeNode;
 struct ModeNode *ModeNodeFound;

 ModeNode=(struct ModeNode *)p_data_proc->ml->lh_Head;
 ModeNodeFound=ModeNode;

 while (ModeNode->mn_Node.ln_Succ)
  {
	if(flg_depth){
		if(*p_depth<ModeNode->MaxDepth){
			*p_depth=ModeNode->MaxDepth;
			if(*p_tx<=ModeNode->MaxX){
				*p_tx=ModeNode->MaxX;
				if(*p_ty<=ModeNode->MaxY){
					*p_ty=ModeNode->MaxY;
				}
			}
			ModeNodeFound=ModeNode;
		}
	}else{
		if(*p_tx<=ModeNode->MaxX){
			*p_tx=ModeNode->MaxX;
			if(*p_ty<=ModeNode->MaxY){
				*p_ty=ModeNode->MaxY;
				if(*p_depth<ModeNode->MaxDepth){
					*p_depth=ModeNode->MaxDepth;
				}
			}
			ModeNodeFound=ModeNode;
		}
	}
	ModeNode=(struct ModeNode *)ModeNode->mn_Node.ln_Succ;
  }
  *p_depth=ModeNodeFound->MaxDepth;

  *p_tx=ModeNodeFound->MaxX;
  *p_ty=ModeNodeFound->MaxY;

  *p_d_id=ModeNodeFound->DisplayID;

  return TRUE;
}

_main(line)
register	char	*line;
{
    struct	Task	*my_t;
    struct	MsgPort	*mp;
    int		signal;

   /****************************************************************/
   /* Recherche du port qui contient les infos relatives a l'ecran */
   /* Ce port permet de passer des donnes de la tache main vers ici*/ 
   /****************************************************************/

   IntuitionBase=(struct Library *)OpenLibrary("intuition.library",0);
   GfxBase=(struct Library *)OpenLibrary("graphics.library",0);
#ifdef	DEBUG
   hd=(char *)Open("con:10/10/320/80/",MODE_OLDFILE);
#endif

   if((mp=FindPort("That's Dark!"))==0){
		dark();
   }else{
	/*********************************************/
	/* On recupere les informaitons ainsi passee */
	/*********************************************/
	p_data_proc=(struct appel_proc *)mp;
	p_info=p_data_proc->p_info;
	p_data_proc->p_gadg=my_gadg;
	p_data_proc->p_texte=p_text_info;
	p_data_proc->type_screen=SCR_OWN;
	proc_init();

	/* Si on n'a pas encore notifie le type de verision */
	if(p_data_proc->code_ret==-1){
		p_data_proc->code_ret=DARK_WB_13;
	}

	my_t=FindTask(0);
#ifdef	DEBUG
	MyWrite("Avant le do\n");
	SPrintF(pr_buff,"Tache mt:%lx\n",my_t);
	MyWrite(pr_buff);
#endif
	/* Attend que le main nous reveille */
	do{
		signal=Wait(SIG_MAIN_DARK_DO_DARK|SIG_MAIN_DARK_EXIT|SIGBREAKF_CTRL_C);
#ifdef	DEBUG
		SPrintF(pr_buff,"Signal recu:%lx\n",signal);
		MyWrite(pr_buff);
#endif
		if((signal&SIG_MAIN_DARK_DO_DARK)!=0){
#ifdef	DEBUG
			MyWrite("Signal dark...\n");
			MyWrite("J'attend le ctrl-C\n");
#endif
			/* On efface les eventuels CTRL_C prec */
			SetSignal(0,SIGBREAKF_CTRL_C);
			MyWrite("Signaux remis a 0\n");
			dark();
#ifdef	DEBUG
			MyWrite("ctrl-C recu, j'averti le main\n");
#endif
			Signal(my_t->tc_UserData,SIG_DARK_MAIN_STOPPED);
		}
	}while((signal&SIG_MAIN_DARK_EXIT)==0);
	proc_end();
#ifdef	DEBUG
	MyWrite("Signal Exit..\n");
#endif
/*
	Signal(my_t->tc_UserData,SIG_DARK_MAIN_EXITED);
*/
     }
#ifdef	DEBUG
     Close(hd);
#endif
    return 0;
}

int	__saveds	tst_end()
{

	if((SetSignal(0,0)&SIGBREAKF_CTRL_C)!=0){
		return TRUE;
	}else{
		return FALSE;
	}
}

void	__saveds	wait_end()
{
	Wait(SIGBREAKF_CTRL_C);
}
