/****************************************************************************
*$RevHead_Start                                                             *
*                                                                           *
* Project Details                                                           *
* ---------------                                                           *
* Project Name:    MaxCON                                                   *
* Project Version: 0.01                                                     *
* Copyright:       © David M. Balean 1992                                   *
* Date:            Saturday 5th September 1992                              *
* Start DateStamp: 04-Aug-1992 20.46.21.46                                  *
* Project Manager: DMB, 44 Wyong Rd, Killarney Vale, NSW 2261               *
*                                                                           *
*****************************************************************************
*                                                                           *
* This File:       MaxCON.c                                                 *
*$Author:          David M. Balean                                          *
*$File Version:    0.01                                                     *
*$DateStamp:       05-Sep-1992 14.37.43.05                                  *
*                                                                           *
*****************************************************************************
* File Summary:                                                             *
* Obtain screen & window pointers of current CLI/Shell window.              *
* I have chosen the long way rather than take the current active window     *
* in this program.  Use the screen height and width to re-size the window.  *
*****************************************************************************
*                                                                           *
* Date        Time         Version Author                                   *
*                                                                           *
* 04-Aug-1992 20.36.27.46  0.00    David M. Balean                          *
*                                                                           *
* 05-Sep-1992 14.37.43.05  0.01    David M. Balean                          *
* Modified to include strlen() function.                                    *
* Now uses TinyStart.                                                       *
*                                                                           *
*$RevHead_End                                                               *
****************************************************************************/


#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <devices/conunit.h>
#include <intuition/intuition.h>
#include <proto/all.h>


/* prototypes */
LONG sendpkt(struct MsgPort *pid,LONG action,LONG args[],LONG nargs);
struct Window *FindWindow(VOID);
VOID cleanexit(char *s,LONG err);
VOID main(VOID);

VOID exit(LONG);
ULONG strlen(char *);





struct IntuitionBase *IntuitionBase;



/************************************************
 * Strlen version by DMB.			*
 * NB: Only one variable has to be incremented.	*
 ************************************************/

ULONG strlen(s1)

register char *s1;

{
register char *s2 = s1;

while (*s2)
	s2++;

return(s2 - s1);
}




VOID cleanexit(s,err)
char	*s;
LONG	err;

{
BPTR	fh;

if (fh = Output())
	if(*s)
		Write(fh,s,strlen(s));
exit(err);
}



/* sendpkt code - A. Finkel, P. Lindsay, C. Scheppner  CBM */

LONG sendpkt(pid,action,args,nargs)
struct MsgPort *pid;  /* process indentifier ... (handlers message port ) */
LONG action,          /* packet type ... (what you want handler to do )   */
     args[],          /* a pointer to a argument list */
     nargs;           /* number of arguments in list  */
   {
   struct MsgPort        *replyport;
   struct StandardPacket *packet;
 
   LONG  count, *pargs, res1;

   replyport = (struct MsgPort *) CreatePort(NULL,0);
   if(!replyport) return(NULL);

   packet = (struct StandardPacket *) 
      AllocMem((long)sizeof(struct StandardPacket),MEMF_PUBLIC|MEMF_CLEAR);
   if(!packet) 
      {
      DeletePort(replyport);
      return(NULL);
      }

   packet->sp_Msg.mn_Node.ln_Name = (char *)&(packet->sp_Pkt);
   packet->sp_Pkt.dp_Link         = &(packet->sp_Msg);
   packet->sp_Pkt.dp_Port         = replyport;
   packet->sp_Pkt.dp_Type         = action;

   /* copy the args into the packet */
   pargs = &(packet->sp_Pkt.dp_Arg1);       /* address of first argument */
   for(count=0;count < nargs;count++) 
      pargs[count]=args[count];
 
   PutMsg(pid,(struct Message *)packet); /* send packet */

   WaitPort(replyport);
   GetMsg(replyport); 

   res1 = packet->sp_Pkt.dp_Res1;

   FreeMem(packet,(long)sizeof(struct StandardPacket));
   DeletePort(replyport); 

   return(res1);
   }


struct Window *FindWindow()

{
struct InfoData	*id;
struct MsgPort	*conid;
struct Process	*me;
struct Window	*wp;
LONG		myargs[8];

/* Alloc to insure longword alignment */
id = (struct InfoData *)AllocMem(sizeof(struct InfoData),
                                       MEMF_PUBLIC|MEMF_CLEAR);
if(! id) return(0);
me = (struct Process *) FindTask(NULL);
conid = (struct MsgPort *) me->pr_ConsoleTask;

myargs[0]=((ULONG)id) >> 2;


if ((LONG)sendpkt(conid,ACTION_DISK_INFO,myargs,1))
	wp = (struct Window *)id->id_VolumeNode;
else wp = NULL;

FreeMem(id,sizeof(struct InfoData));
return(wp);
}


VOID main()
{


struct Screen *ScrnPtr;
struct Window *WinPtr;

if (!(IntuitionBase =
	    (struct IntuitionBase *) OpenLibrary ("intuition.library", 0L)))
	cleanexit("\nCouldn't open intuition!\n",100);

if (!(WinPtr = FindWindow()))
	cleanexit("\nCan't find window!\n",200);

ScrnPtr = WinPtr->WScreen;
MoveWindow (WinPtr, -(WinPtr->LeftEdge), -(WinPtr->TopEdge));

SizeWindow (WinPtr, (ScrnPtr->Width - WinPtr->Width),
				(ScrnPtr->Height - WinPtr->Height));

CloseLibrary(IntuitionBase);

}


