/************************************************************************/
/*                                                                      */
/*       IL_CALL                                                        */
/*                                                                      */
/*       Autor : Christoph Bartholme                                    */
/*       Compiler : Turbo C V2.03  (Tabsize: 4)                         */
/*                                                                      */
/*       Kleines Beispiel zur šbergabe von Dateien an                   */
/*       die in IdeaList (ab V3.3) eingebaute Drucker-Warteschlage      */
/*                                                                      */
/*       Besonderheiten:                                                */
/*       - l„uft als PRG oder ACC                                       */
/*       - voll CALCLOCK-konform.                                       */
/*       - In der Quelle k”nnen beliebig viele CALCLOCK-f„hige          */
/*         Programme angegeben werden, die nacheinander gesucht werden. */
/*                                                                      */
/*       Die Routinen dieses Programms k”nnen und drfen Sie in         */
/*       eigene Projekte einbauen. Bitte geben Sie IL_CALL und die      */
/*       Quellen stets zusammen und unver„ndert weiter.                 */
/*                                                                      */
/************************************************************************/

/* -------------------------------------------------------------------- */
/*       include - Files:                                               */

#include <aes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tos.h>
/* -------------------------------------------------------------------- */
/*       Typendefinition:                                               */

typedef enum {FALSE, TRUE} boolean;
typedef unsigned int word;

/* -------------------------------------------------------------------- */
/*       Globale Variablen:                                             */

int	gl_apid;
int	menu_id;
char path[80] = "A:";
char pathandname[80];


/* RSC-Daten fr das Men: */
#define NUM_OBS 19
#define M_INFO 	 7
#define M_DOIT 	16
#define M_QUIT 	18
static OBJECT menu[] =
{ 
        -1,        1,        5, G_IBOX    ,   /* Object 0  */
  NONE, NORMAL, (long)0x00000000L,
  0x0000, 0x0000, 0x0050, 0x0019,
         5,        2,        2, G_BOX     ,   /* Object 1  */
  NONE, NORMAL, (long)0x00001100L,
  0x0000, 0x0000, 0x0050, 0x0201,
         1,        3,        4, G_IBOX    ,   /* Object 2  */
  NONE, NORMAL, (long)0x00000000L,
  0x0002, 0x0000, 0x0010, 0x0301,
         4,       -1,       -1, G_TITLE   ,   /* Object 3  */
  NONE, NORMAL, (long)" IL_CALL",
  0x0000, 0x0000, 0x0009, 0x0301,
         2,       -1,       -1, G_TITLE   ,   /* Object 4  */
  NONE, NORMAL, (long)" Datei",
  0x0009, 0x0000, 0x0007, 0x0301,
         0,        6,       15, G_IBOX    ,   /* Object 5  */
  NONE, NORMAL, (long)0x00000000L,
  0x0000, 0x0301, 0x0050, 0x0017,
        15,        7,       14, G_BOX     ,   /* Object 6  */
  NONE, NORMAL, (long)0x00FF1100L,
  0x0002, 0x0000, 0x0013, 0x0008,
         8,       -1,       -1, G_STRING  ,   /* Object 7  */
  NONE, NORMAL, (long)"  Programminfo...  ",
  0x0000, 0x0000, 0x0013, 0x0001,
         9,       -1,       -1, G_STRING  ,   /* Object 8  */
  NONE, DISABLED, (long)"-------------------",
  0x0000, 0x0001, 0x0013, 0x0001,
        10,       -1,       -1, G_STRING  ,   /* Object 9  */
  NONE, NORMAL, (long)"  Desk Accessory 1 ",
  0x0000, 0x0002, 0x0013, 0x0001,
        11,       -1,       -1, G_STRING  ,   /* Object 10  */
  NONE, NORMAL, (long)"  Desk Accessory 2 ",
  0x0000, 0x0003, 0x0013, 0x0001,
        12,       -1,       -1, G_STRING  ,   /* Object 11  */
  NONE, NORMAL, (long)"  Desk Accessory 3 ",
  0x0000, 0x0004, 0x0013, 0x0001,
        13,       -1,       -1, G_STRING  ,   /* Object 12  */
  NONE, NORMAL, (long)"  Desk Accessory 4 ",
  0x0000, 0x0005, 0x0013, 0x0001,
        14,       -1,       -1, G_STRING  ,   /* Object 13  */
  NONE, NORMAL, (long)"  Desk Accessory 5 ",
  0x0000, 0x0006, 0x0013, 0x0001,
         6,       -1,       -1, G_STRING  ,   /* Object 14  */
  NONE, NORMAL, (long)"  Desk Accessory 6 ",
  0x0000, 0x0007, 0x0013, 0x0001,
         5,       16,       18, G_BOX     ,   /* Object 15  */
  NONE, NORMAL, (long)0x00FF1100L,
  0x000B, 0x0000, 0x000F, 0x0003,
        17,       -1,       -1, G_STRING  ,   /* Object 16  */
  NONE, NORMAL, (long)"  Mitteilen... ",
  0x0000, 0x0000, 0x000F, 0x0001,
        18,       -1,       -1, G_STRING  ,   /* Object 17  */
  NONE, DISABLED, (long)"---------------",
  0x0000, 0x0001, 0x000F, 0x0001,
        15,       -1,       -1, G_STRING  ,   /* Object 18  */
  LASTOB, NORMAL, (long)"  Ende         ",
  0x0000, 0x0002, 0x000F, 0x0001
};


/* -------------------------------------------------------------------- */
/*       Extern definierte globale Variablen:                           */

extern int _app;

/* -------------------------------------------------------------------- */
/*    Funktionsprototypen:                                              */

void	do_it(void);
void	event_loop(void);

/* -------------------------------------------------------------------- */
void main(void)
{	
	int 	i=0;

	gl_apid = appl_init();
	if (gl_apid >= 0)
	{
		path[0] = Dgetdrv() +'A'; /* Zugriffs-Laufwerk, ...    */
		Dgetpath(path+2,0);       /* ... -Pfad und ...         */
		strcat(path,"\\*.*");	  /* ... -Maske initialisieren */	
		if (!_app)             /* Als Accessory gestartet...   */
    	   	menu_id = menu_register( gl_apid, "  Rufe Warteschlange");
    	else /* Als PRG gestartet... */
    	{
       		while (i<NUM_OBS) rsrc_obfix(menu,i++);
       		menu_bar(menu,1);
       		graf_mouse(0, (void*)0);
       	}
       	event_loop();

		/* Programm terminieren; nur fr PRG (Das ACC verl„žt event_loop nie!) */ 
	    menu_bar(menu,0);
		appl_exit();
   	   	exit(0);
   	}
}

/* -------------------------------------------------------------------- */
void event_loop(void)
{
	int mbuf[8];
	int quit = FALSE;
	do
	{
	    evnt_mesag(mbuf);
		if (mbuf[0] == MN_SELECTED)
		{
			switch (mbuf[4])
			{
				case M_INFO :
				  form_alert(1,"[0][Kleine Demonstration der|CALCLOCK-Schnittstelle|von ILIST.ACC, IDEALIST.PRG |und CALCLOCK-Kompatiblen][ OK ]"); break;
				case M_DOIT : do_it(); break;				
				case M_QUIT : quit = TRUE; break;
			}
			menu_tnormal(menu, mbuf[3], 1);
		}
		if (mbuf[0] == AC_OPEN)
			do_it();
	} while (!quit);
}

/* -------------------------------------------------------------------- */
void do_it(void)
{	
	/* Liste der Prozesse, die das CALCLOCK-Protokoll verstehen */
	/* und nach denen gesucht werden soll;                      */
	/* der erste Match wird verwendet.							*/
	char *Identifierlist[] = {
							"ILIST   ",
							"IDEALIST",
							"1STGUIDE", 
							"1STVIEW ", 
							"CALCLOCK",
	/* Hier k”nnen weitere Namen eingefgt werden	*/
	/* (mit Blanks auf 8 Stellen auffllen)			*/							 
						    ""};	/* Leerstring markiert das Ende der Liste */

	int calclockId; /* Rueckgabewert von von appl_find() */
	int button;     /* Auswahl von Alertbox */
	int isOK;       /* Auswahl von Fileselectbox */
	int i=0;        /* Zaehlvariable */
	int event;      /* Messagetyp */
	int dum; /* Eventschleife */
	int mbuf[8], mess[8]; /* Messagebuffer fr appl_write() und evnt_multi */
	char *end; 
	char filename[12] = ""; /* Dateiname; Return von Fileselectbox */
	char *identifier; /* Prozežname der Druckerschlange */
	char atext11[255] = "[2][Eine Datei|zur šbergabe an  |"; 
	char *atext12= "|ausw„hlen?][Abbruch|OK]";               
	char atext21[255] = "[3][ |Keine Antwort von|"; 
	char *atext22= " |oder Timeout!][ Pech ]";	
	do
		calclockId = appl_find(identifier=Identifierlist[i++]);
	while ((Identifierlist[i] != "") && (calclockId < 0));
	if (calclockId < 0)
		form_alert(1, "[3][Kann kein|Programm fr die|Druckerschlange finden!][Abbruch]");
	else
	{
		strcat(atext11,identifier);
		strcat(atext11,atext12);
		button = form_alert(2, atext11);
		if (button == 2) 
		{
			wind_update(BEG_UPDATE);
			fsel_input(path, filename, &isOK);
			wind_update(END_UPDATE);
			if (isOK)
			{
				strcpy(pathandname,path);
				end = pathandname+strlen(pathandname);
				while(*--end != '\\');
				*++end = 0;
				strcat(pathandname, filename);
				mbuf[0]	=	0x64;				/* Message Nr. 100  */
				mbuf[1] =	gl_apid;
				mbuf[2] =	-1;
				mbuf[3] =	(word)strlen(pathandname);
				mbuf[4] =	(word)((long)(pathandname) >> 16);
				mbuf[5] =	(word)((long)pathandname & 0xffff);
				mbuf[6]	=	1;	/* Anzahl der Kopien 				*/
				mbuf[7] =	0;  /* 0 = nach Drucken nicht l”schen; 	*/ 
				                /* 1 = nach Drucken l”schen 		*/
				appl_write(calclockId, 16, &mbuf);

			    event = evnt_multi( MU_MESAG|MU_TIMER, 	/* Und auf die Antwort... */
							1, 1, 1,
							0, 0, 0, 0, 0,
						    0, 0, 0, 0, 0,
							mess, 2000, 0,      /* ... 2000 ms warten! 	  */
							&dum, &dum, &dum, &dum, &dum, &dum);
				if (((event & MU_TIMER) != 0) && (mess[0] != 0x65)) 
				{
					strcat(atext21,identifier);
					strcat(atext21,atext22);
					form_alert(1, atext21);
				}
			}
		}
	}		
}

/* ---------------------------------- EOF ---------------------------- */