/*********************************************************************/
/* SAMPLE .WNX APPLICATION SKELETON */
/*	portions borrowed from: */
/*	started 5/28/85 R.Z.   Copyright ATARI Corp. 1985 */
/*********************************************************************/
/*********************************************************************/
/* INCLUDE FILES */
/*********************************************************************/
#include <stdio.h>
#include <obdefs.h>
#include <gemdefs.h>
#include <osbind.h>
#include <xesblk.h>
#include <xesdefs.h>
/*********************************************************************/
/* DEFINES */
/*********************************************************************/
#define TRUE 1
#define FALSE 0

#define	ACCNAME	"  File Converter "

#define WI_KIND		(SIZER|MOVER|FULLER|CLOSER|NAME)

#define NO_WINDOW (-1)

#define MIN_WIDTH  (2*gl_wbox)
#define MIN_HEIGHT (3*gl_hbox)

/*********************************************************************/
/* EXTERNALS						   	     */
/*********************************************************************/

extern int	gl_apid,_app,errno;
extern long	base,stksize,stack;

extern struct __c {
	int *cb_pcontrol;
	int *cb_pglobal;
	int *cb_pintin;
	int *cb_pintout;
	long *cb_padrin;
	long *cb_padrout;
} _c, *_ad_c;

extern int control[], global[]; 
extern int_in[], int_out[];
extern long addr_in[], addr_out[];

/*********************************************************************/
/* GLOBAL VARIABLES					   	     */
/*********************************************************************/

long	oldstack;

int	gl_hchar;
int	gl_wchar;
int	gl_wbox;
int	gl_hbox;	/* system sizes */

int 	phys_handle;	/* physical workstation handle */
int 	handle;		/* virtual workstation handle */
int		wi_handle;	/* window handle */
int		top_window;	/* handle of topped window */

int	xdesk,ydesk,hdesk,wdesk;
int	xold,yold,hold,wold;
int	xwork,ywork,hwork,wwork;	/* desktop and work areas */

int	msgbuff[8];	/* event message buffer */
int	keycode;	/* keycode returned by event-keyboard */
int	mx,my;		/* mouse x and y pos. */
int	butdown;	/* button state tested for, UP/DOWN */
int	ret;		/* dummy return variable */

int	hidden;		/* current state of cursor */

int	fulled;		/* current state of window */

int	contrl[12];
int	intin[128];
int	ptsin[128];
int	intout[128];
int	ptsout[128];	/* storage wasted for idiotic bindings */

int work_in[11];	/* Input to GSX parameter array */
int work_out[57];	/* Output from GSX parameter array */
int pxyarray[10];	/* input point array */

int	menu_id;

xesparmblk	xparmblk;

int	current_type=0;	/* Defaults to C code	*/

OBJECT	*treeptr;

typedef	struct{
		char	dta_res[21];
		char	dta_attrib[1];
		int		dta_time[1];
		int		dta_date[1];
		long	dta_size[1];
		char	dta_name[14];
}newdta;

#include	"D:\\WNX_DEV\\WERCS\\CONVERT.H"
#include	"D:\\WNX_DEV\\WERCS\\CONVERT.C"

void	wnx_button()
{
		/* Finds out what was clicked on and does what's necessary */
		xes_xformdo(-1,treeptr,xparmblk.WNX_out[5],&xparmblk);
		
		switch(xparmblk.WNX_out[5]){
			case	_C:	{
				current_type=0;
				break;
			}
			
			case	_devpac:	{
				current_type=1;
				break;
			}
			
			case	_list:	{
				current_type=2;
				break;
			}
			
			case	convertit:	{
					program();
					treeptr[convertit].ob_state &= ~SELECTED;
					xes_draw(convertit,1,&xparmblk);
					break;
			}
		}
}

open_vwork()
{
int i;
	for(i=0;i<10;work_in[i++]=1);
	work_in[10]=2;
	handle=phys_handle;
	v_opnvwk(work_in,&handle,work_out);
}


void prg_acc_startup()
{
		appl_init();
		phys_handle=graf_handle(&gl_wchar,&gl_hchar,&gl_wbox,&gl_hbox);
		open_vwork();
}	


multi()
{
	int event;

      while (TRUE) {
  
  		/*	currently only messages... could change this to an
  				evnt_mesag call, but I like to keep it open... */
				event = evnt_multi(MU_MESAG,
				1,1,1,
				0,0,0,0,0,
				0,0,0,0,0,
				msgbuff,0,0,&mx,&my,&ret,&ret,&keycode,&ret);

				switch(msgbuff[0]){
					case	AC_OPEN:
								program();
								break;
				}
			}
}


fix_path_name(p_ptr,n_ptr,f_ptr)
		register char	*p_ptr,*n_ptr,*f_ptr;
{
				while	(*p_ptr)
					{
						*f_ptr=*p_ptr;
						++f_ptr;
						++p_ptr;
					}
					
				while	(*f_ptr!='\\')
					--f_ptr;
					
				++f_ptr;
				
				while	(*n_ptr)
					{
						*f_ptr=*n_ptr;
						++n_ptr;
						++f_ptr;
					}
		
				*f_ptr=0;
}

stopme()
{		
		if	(_app==-1){
			xparmblk.WNX_tcount=10;
			xes_submit(&xparmblk);
		}
}

uc2str(in,begin1)
	unsigned char *in,begin1[];
{
	int	i;
	unsigned char	c[4];

		sprintf(c,"%d",*in);
		strcat(begin1,c);
}


convert2C(f_name,name,size)
char	*f_name;
char	*name[14];
long	size;
{
	char	*in;
	char	filename[144],arrayname[11];
	
	if	(in=(char *)Malloc(size)){
			/* Do the conversion here	*/
			int	i,j,inhandle,outhandle;
			long	temp1,temp2,fseekptr=0;
			char	begin1[80],begin2[80],begin3[80],begin4[80];
			
			inhandle	=	Fopen(f_name,0);
			if	(inhandle<0)
				return	FALSE;
			temp1	=	Fread(inhandle,size,in);
			if	(temp1!=size)	return	FALSE;
			Fclose(inhandle);			

			strcpy(filename,f_name);
			
			i=0;
			while	(filename[i])
				i++;
	
			while	(filename[i]!='\\')
				i--;
			
			while (filename[i]!='.' & filename[i]!=0)
				i++;
		
			if	(filename[i+1]!='C' & (current_type==0)){
					filename[i+1]='C';
					filename[i+2]=0;
			}
			else if(filename[i+1]!='S' & (current_type==1)){
					filename[i+1]='S';
					filename[i+2]=0;
			}
			else{
					filename[i+1]='O';
					filename[i+2]='O';
					filename[i+3]='P';
					filename[i+4]=0;
			}


			outhandle	=	Fcreate(filename,0);

			if	(outhandle<0)
				return	FALSE;
										
			/* fix the array name up... */
			strcpy(arrayname,name);
			
			i=0;
			while (arrayname[i]!='.' & arrayname[i]!=0)
				i++;
			
			arrayname[i]=0;
		
			switch	(current_type){
				case	0:{
					/* C Declaration	*/

					strcpy(begin1,"/* This is a C code representation of the file:\n");
					
					strcpy(begin2,"   ");
					strcat(begin2,f_name);
					strcat(begin2,"\n");
					
					strcpy(begin3,"   ...as an array of unsigned character (bytes) */\n\n");
					
					strcpy(begin4,"unsigned char ");
					strcat(begin4,arrayname);
					strcat(begin4,"[] = {");
					
					break;
					}
				case	1:{
					/* Assembler (DevPac ST) */
				
					strcpy(begin1,"; This is an assembler representation of the file:\n");
					
					strcpy(begin2,"; ");
					strcat(begin2,f_name);
					strcat(begin2,"\n");
					
					strcpy(begin3,"; ...as an array of bytes.\n\n");
					
					strcpy(begin4,arrayname);
					strcat(begin4,":");
					
					break;
					}
				case	3:{
				
					begin1[0]=0;
					begin2[0]=0;
					begin3[0]=0;
					begin4[0]=0;
				
					break;
					}
				}

			temp2=(long)strlen(begin1);
			if	(temp2!=(Fwrite(outhandle,temp2,begin1)))
				return FALSE;
			Fclose(outhandle);
			fseekptr	+=	temp2;

			outhandle	=	Fopen(filename,2);
			if	(outhandle<0)	return	FALSE;
			temp2=(long)strlen(begin2);
			Fseek(fseekptr,outhandle,0);
			Fwrite(outhandle,temp2,begin2);
			Fclose(outhandle);
			fseekptr	+=	temp2;
			
			outhandle	=	Fopen(filename,2);
			if	(outhandle<0)	return	FALSE;
			temp2=(long)strlen(begin3);
			Fseek(fseekptr,outhandle,0);
			Fwrite(outhandle,temp2,begin3);
			Fclose(outhandle);
			fseekptr	+=	temp2;

			outhandle	=	Fopen(filename,2);
			if	(outhandle<0)	return	FALSE;
			temp2=(long)strlen(begin4);
			Fseek(fseekptr,outhandle,0);
			Fwrite(outhandle,temp2,begin4);			
			Fclose(outhandle);
			fseekptr	+=	temp2;
			
			j=0;
			for(i=0;i<size;i++){

				/* Every 18 entries, skip to a new line...	*/			

				if	((i%18)==0){
					if(i){

						outhandle	=	Fopen(filename,2);
						if	(outhandle<0)	return	FALSE;
						temp2=(long)strlen(begin1);
						Fseek(fseekptr,outhandle,0);
						Fwrite(outhandle,temp2,begin1);
						Fclose(outhandle);
						fseekptr	+=	temp2;
					
						stopme();
					}
					
					switch(current_type){
						case	0:{
							strcpy(begin1,"\n\t");
							uc2str(in,begin1);
							break;
						}
						case	1:{
							strcpy(begin1,"\n\tdc.b   ");
							uc2str(in,begin1);
							break;
						}
						case	2:{
							strcpy(begin1,"\n\t");
							uc2str(in,begin1);
							break;
						}
					}

						stopme();
				}
				else{	
							uc2str(in,begin1);
				}
				
				if	(i<size-1){
					switch(current_type){
						case	0:{
							strcat(begin1,",");
							break;
						}
						default:{
							if (i%18!=17)
								strcat(begin1,",");
							break;
						}
					}
				}
				++in;
			}
			/*	End declaration	*/

			outhandle	=	Fopen(filename,2);
			if	(outhandle<0)	return	FALSE;
			temp2=(long)strlen(begin1);
			Fseek(fseekptr,outhandle,0);
			Fwrite(outhandle,temp2,begin1);
			Fclose(outhandle);
			fseekptr	+=	temp2;
			
			if	(current_type==0){
				strcpy(begin1,"\n};");

				outhandle	=	Fopen(filename,2);
				if	(outhandle<0)	return	FALSE;
				temp2=(long)strlen(begin1);
				Fseek(fseekptr,outhandle,0);
				Fwrite(outhandle,temp2,begin1);
				Fclose(outhandle);
				fseekptr	+=	temp2;
			}
			return	TRUE;
	}
	else	return FALSE;		
}



program()
{
		int	button;
		static unsigned char	drive;
		static char	path[128],name[14];
		char	fullname[144];
		long	size;
		newdta	*dta_ptr;

/*	code for program's "guts" goes here... this is only called if
		we're an .ACC or .PRG.  A .WNX has to call this itself.			*/

		if	(!path[0]){
			/* path has not been set before... */
			
			strcpy(path,"A:\\");
			drive	=	Dgetdrv();
			path[0]+=drive;
			Dgetpath(&path[3],drive+1);
			strcat(path,"\\*.*");
			}

		button	=	TRUE;

		/* loops until the user doesn't pick a file	*/
		while(button){

			/* pop goes the fsel cuz the fsel goes pop!	*/
			fsel_input(path,name,&button);
			
			if	(button){
				fix_path_name(path,name,fullname);
				dta_ptr=(newdta *)Fgetdta();
				
				if (!Fsfirst(fullname,3)){
					size	=	*(dta_ptr->dta_size);
					
					xparmblk.WNX_flags=XES_LINK|XES_TIMER;			

					convert2C(fullname,name,size);

					xparmblk.WNX_flags=XES_BUTTON;			

					/* If not a program, don't keep looping like this */
					if	(_app!=1)
						button = FALSE;
				}
			}
		}		
}

/****************************************************************/
/*		Accessory Init. Until First Event_Multi		*/
/****************************************************************/
main()
{
	switch(_app)
	{
		case	-1:
		{
			/*	WNX	*/
			strcpy(xparmblk.WNX_idname,"File Converter");
			xes_init(&xparmblk);

			/*	A .WNX doesn't have the variable __c, which is
					its AES parameter block, fixed up during an
					appl_init, so I have to do it myself.	*/
			
			_c.cb_pcontrol=control;
			_c.cb_pglobal=global;
			_c.cb_pintin=int_in;
			_c.cb_pintout=int_out;
			_c.cb_padrin=addr_in;
			_c.cb_padrout=addr_out;
			
			_ad_c=&_c;

			xes_genv(&xparmblk);
			xes_pwindow(&xparmblk);

			xes_rsrcinit(-1,2,CONVERT,&xparmblk);
			treeptr=*(OBJECT **)&xparmblk.WNX_out[1];
			treeptr[_C].ob_state = SELECTED;
		
			phys_handle=xparmblk.WNX_grafhandle;
			
			xparmblk.WNX_flags=XES_BUTTON;			/* button events only */
			xparmblk.WNX_broutine=wnx_button;
			
			xes_submit(&xparmblk);
			break;
		}

#ifdef LASERC
		default:
		{
			_app=1;
			prg_acc_startup();
			program();
		}	
#else
		case	1:
		{
		/*	PROGRAM	*/
			prg_acc_startup();
			program();
			break;
		}
		case	0:
		{
		/* ACCESSORY */
			prg_acc_startup();
			menu_id=menu_register(gl_apid,ACCNAME);
			multi();
			break;
		}
#endif	
 	}
}
