/********************************************************************
 *																	*
 *	Accessory and process routines									*
 *	by Ken Hollis													*
 *																	*
 *	Find accessory names, find process names (background processes) *
 *	call accessories, and call processes through menubars.			*
 *																	*
 *	Copyright (C) 1994, Bitgate Software and Clever Bits			*
 *	All Rights Reserved.											*
 *																	*
 *	Use of the Accessor's file is dormant as of now.  We don't use	*
 *	it because we don't know how to get the correct names of the	*
 *	accessories.  We know how to call, just not what menu order		*
 *	the menus are in.												*
 *																	*
 ********************************************************************
 *																	*
 *	Update logs:													*
 *	[12.3.94] Ken Hollis											*
 *		WInitProcesses	- added accessory setup from The Accessor	*
 *		UseInternalAccs	- for TOS 1.0 users and under (couldn't		*
 *						  figure out accessory calling method)		*
 *																	*
 ********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "winlib.h"

LOCAL OBJECT menuint[] = {
  /* Tree #0*/
  {  -1,   1,   4,0x0019,0x0000,0x0000, (long)(0x00000000L),   0,   0,  80,  25},
  {   4,   2,   2,0x0014,0x0000,0x0000, (long)(0x00001100L),   0,   0,  80, 513},
  {   1,   3,   3,0x0019,0x0000,0x0000, (long)(0x00000000L),   2,   0,   6, 769},
  {   2,  -1,  -1,0x0020,0x0000,0x0000, (long)(" Desk"),   0,   0,   6, 769},
  {   0,   5,   5,0x0019,0x0000,0x0000, (long)(0x00000000L),   0, 769,  80,  23},
  {   4,   6,  13,0x0014,0x0000,0x0000, (long)(0x00ff1100L),   2,   0,  19,   8},
  {   7,  -1,  -1,0x001c,0x0000,0x0000, (long)("  Back to program  "),   0,   0,  19,   1},
  {   8,  -1,  -1,0x001c,0x0000,0x0008, (long)("-------------------"),   0,   1,  19,   1},
  {   9,  -1,  -1,0x001c,0x0000,0x0000, (long)("  Desk Accessory 1 "),   0,   2,  19,   1},
  {  10,  -1,  -1,0x001c,0x0000,0x0000, (long)("  Desk Accessory 2 "),   0,   3,  19,   1},
  {  11,  -1,  -1,0x001c,0x0000,0x0000, (long)("  Desk Accessory 3 "),   0,   4,  19,   1},
  {  12,  -1,  -1,0x001c,0x0000,0x0000, (long)("  Desk Accessory 4 "),   0,   5,  19,   1},
  {  13,  -1,  -1,0x001c,0x0000,0x0000, (long)("  Desk Accessory 5 "),   0,   6,  19,   1},
  {   5,  -1,  -1,0x001c,0x0020,0x0000, (long)("  Desk Accessory 6 "),   0,   7,  19,   1}
};

OBJECT *INTERNALMENU = menuint;

ACCS Accessories[6];

GLOBAL void WInitProcesses(void)
{
	FILE *fh;
	int i = 1;
	char *fil;

	RFix_ObjectPos(INTERNALMENU);

	if ((fh = (FILE *) fopen(".\\WLACC.INF", "r")) == NULL) {
		if (AES_VERSION<0x0400)
			form_alert(1,"[3][The Accessor must be run|in the AUTO folder|in order to access|accessories from this|program.][ Okay ]");
	} else {
		do {
			Accessories[i].accname = (char *) malloc(8);

			if((fil = fgets(Accessories[i].accname, 9, fh))!=NULL) {
				Accessories[i].acchandle = (appl_find(Accessories[i].accname));
				Accessories[i].accposition = (appl_find(Accessories[i].accname)) - 2;
			}

			i++;
		} while((fil!=NULL) && (i<6));

		fclose(fh);
	}
}

GLOBAL void WOpenAccessory(int buffernum)
{
	int msg_buf[8];

	msg_buf[0] = AC_OPEN;
	msg_buf[1] = Ap_ID;
	msg_buf[2] = 8;
	msg_buf[3] = 0;	/* Reserved */
	msg_buf[4] = Accessories[buffernum].accposition;

	appl_write(Accessories[buffernum].acchandle, 16, msg_buf);
}	

GLOBAL void WCallAccessory(int buffernum)
{
	int menu = 0, i = 0;
	BOOL found = FALSE;

	do {
		i++;
		if (Accessories[i].accposition == buffernum) {
			menu = i;
			found = TRUE;
		}
	} while((i!=6) && !found);

	WOpenAccessory(menu);
}

GLOBAL void WUseInternalAccs(int show)
{
	menu_bar(INTERNALMENU, show);
}