/*********************************************************************/
/* 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 LASERC
#define TRUE 1
#define FALSE 0

#define	ACCNAME	"Flip's .ACC"

#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					   	     */
/*********************************************************************/

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;

/****************************************************************/
/* open virtual workstation					*/
/****************************************************************/
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);
}

/*	This is some stuff common to both .ACC and .PRG executables
		which I do whenever they start up...											*/
void prg_acc_startup()
{
		appl_init();
		phys_handle=graf_handle(&gl_wchar,&gl_hchar,&gl_wbox,&gl_hbox);
		open_vwork();

		wi_handle=NO_WINDOW;
		hidden=FALSE;
		fulled=FALSE;
		butdown=TRUE;
}	

/****************************************************************/
/* dispatches all accessory tasks				*/
/****************************************************************/
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;
				}
			}
}

program()
{
/*	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.			*/
}
/****************************************************************/
/*		Accessory Init. Until First Event_Multi		*/
/****************************************************************/
main()
{
	switch(_app)
	{
		case	-1:
			{
				/*	WNX	*/
		
				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);

				/* Fixes the WNX' i.d. name field */
				strcpy(xparmblk.WNX_idname,"I AM A WNX");

				xes_submit(&xparmblk);
				break;
			}

#ifdef LASERC
/*	This is needed since LASER C doesn't make A0 = 0, but does
	make p_parent null.  I just make it so if LASERC == TRUE,
	we know this is being run from the LASER C shell, and so
	it will be a program...									*/
		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	
	}
}
