#include "Control_Include.h"

/* Start of Location Function Prototypes And Data 

  Also Want To Include Somewhere Support For Commoditys, That is
   The Ability To Switch off And On Commoditys From Within Control
*/

char ** __saveds __asm Task_Construct_Code(register __a2 APTR,register __a1 char *);	// The Construct Hook
void   __saveds __asm Task_Destruct_Code(register __a2 APTR,register __a1 char **);    // The Destruct Hook
void   __saveds __asm Task_Display_Code(register __a2 char **,register __a1 char **);   // The Display Hook
int 	Memory;
int	Display_Count;

struct Library *MUIMasterBase;

struct Hook Task_Construct_CodeHook = {
            {NULL, NULL},
            (void *)Task_Construct_Code,
            NULL, NULL
           };
           
struct Hook Task_Destruct_CodeHook = {
            {NULL, NULL},
            (void *)Task_Destruct_Code,
            NULL, NULL
           };
           
struct Hook Task_Display_CodeHook = {
            {NULL, NULL},
            (void *)Task_Display_Code,
            NULL, NULL
           };


/* End Of Local Function Prototypes and Data */

extern struct Task * Find_Task( char *);


__asm __saveds char ** Task_Construct_Code(register __a2 APTR *Memory_Pool, register __a1 char *Task_Name_Input)  
{
               char  **Col_Array;
       struct Process *Current_Task;
               char   *Stack_Size;
               char   *Task_Name;
               char   *Task_Type;
               char   *Signals;
               char   *State;
               char   *Task_Location;
               char   *Sig_Rec;
               char   *Task_Pri;
               Display_Count++;
               if(!Memory_Pool)
               {
                Display_Count--;
                return(0);
               }
       
  Current_Task = (struct Process *)Find_Task(Task_Name_Input);
  
  if(Current_Task)
  {
       Col_Array       =   AllocPooled(Memory_Pool,32);
       Signals         =   AllocPooled(Memory_Pool,45);
       Task_Name       =   AllocPooled(Memory_Pool,45);
       State           =   AllocPooled(Memory_Pool,45);
       Sig_Rec         =   AllocPooled(Memory_Pool,45);
       Stack_Size      =   AllocPooled(Memory_Pool,45);
       Task_Type       =   AllocPooled(Memory_Pool,45);
       Task_Location   =   AllocPooled(Memory_Pool,45);
       Task_Pri        =   AllocPooled(Memory_Pool,45);
       Memory          =   Memory + 32+45+45+45+45+45+45+45+45;
      if(Current_Task->pr_Task.tc_State==8)
       {
          sprintf(Task_Type,"\33I[6:20]");
          sprintf(Stack_Size,"%d",(int)Current_Task->pr_Task.tc_SPUpper-(int)Current_Task->pr_Task.tc_SPLower);
        }
        else
        {
         if(Current_Task->pr_Task.tc_Node.ln_Type==NT_TASK)
            {
             sprintf(Stack_Size,"%d",(int)Current_Task->pr_Task.tc_SPUpper-(int)Current_Task->pr_Task.tc_SPLower);      
             sprintf(Task_Type,"\33I[6:25]");
            }
           else
            {
             sprintf(Stack_Size,"%d",Current_Task->pr_StackSize);
             sprintf(Task_Type,"\33I[6:23]");
            }
        }
      sprintf(Task_Location,"$%p",&Current_Task->pr_Task);
      sprintf(Task_Name,    "%s",Task_Name_Input);
      sprintf(Signals,   "%08lx",Current_Task->pr_Task.tc_SigWait);
      sprintf(State,     "%08lx",Current_Task->pr_Task.tc_SigAlloc);
      sprintf(Sig_Rec,   "%08lx",Current_Task->pr_Task.tc_SigRecvd);
      sprintf(Task_Pri,  "%d",Current_Task->pr_Task.tc_Node.ln_Pri);           
      
      Col_Array[0]=Task_Type;
      Col_Array[1]=Task_Name;
      Col_Array[2]=Task_Location;
      Col_Array[3]=Signals;
      Col_Array[4]=State;
      Col_Array[5]=Sig_Rec;
      Col_Array[6]=Task_Pri;
      Col_Array[7]=Stack_Size;
      Col_Array[8]=0;
      return(Col_Array);
  }
  else
  {
       Col_Array       =   AllocPooled(Memory_Pool,32);
       Task_Name       =   AllocPooled(Memory_Pool,45);
       Task_Type       =   AllocPooled(Memory_Pool,45);
       Memory          =   Memory + 32+45+45;
      sprintf(Task_Type,"\33I[6:12]");
      sprintf(Task_Name,    "INVALID TASKNAME OR STRUCTURE");
      Col_Array[0]=Task_Type;
      Col_Array[1]=Task_Name;
      Col_Array[2]=0;
      Col_Array[3]=0;
      Col_Array[4]=0;
      Col_Array[5]=0;
      Col_Array[6]=0;
      Col_Array[7]=0;
      Col_Array[8]=0;
      return(Col_Array);
  }



}



__asm void __saveds  Task_Destruct_Code(register __a2 APTR Memory_Pool, register __a1 char **String)   
{
    //Right Sum Nice Code Coming Up!
    int x=0;
    Display_Count--;
    while(String[x]!=0)
     {
        
        FreePooled(Memory_Pool,String[x],45);
        Memory=Memory-45;
        x++;
     }
   
    FreePooled(Memory_Pool,String,32);
    Memory=Memory-32;
    
}


__asm void __saveds Task_Display_Code(register __a2 char **Insert_Me,register __a1 char **String)
{
   
    int x=0;
   while(String[x]!=0)
   {
    Insert_Me[x]=String[x];
    x++;
   }
   Insert_Me[x]=0;
    
}

void Open_Librarys( void )
{ 
         if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
              printf("Failed to open %s.\n",MUIMASTER_NAME); // Some Smart Routine Here
}
 
 
 
void Close_Librarys( void )
{
              if (MUIMasterBase)
              CloseLibrary(MUIMasterBase);
}


struct Task * Find_Task( char *Name)
{
    struct Task *Bob;
    struct Node *execnode;
    struct List *exectasklist;
    
     exectasklist = &(SysBase->TaskReady);
          for(execnode = exectasklist->lh_Head; execnode->ln_Succ; execnode = execnode->ln_Succ)
              {
                //printf("Searching In Ready_List\n");
                  if(!strcmp(Name,execnode->ln_Name));
                    {
                        return((struct Task*)execnode);
                    }
                     
              }
             
     exectasklist = &(SysBase->TaskWait);
          for(execnode = exectasklist->lh_Head; execnode->ln_Succ; execnode = execnode->ln_Succ)
              {
                //printf("Searching In Wait_List\n");
                  if(!strcmp(Name,execnode->ln_Name));
                    {
                        return((struct Task*)execnode);
                    }
                     
              }
      exectasklist = &(SysBase->IntrList);
          for(execnode = exectasklist->lh_Head; execnode->ln_Succ; execnode = execnode->ln_Succ)
              {
                //printf("Searching In Interrupt List\n");
                  if(!strcmp(Name,execnode->ln_Name));
                    {
                        return((struct Task*)execnode);
                    }
                     
              }

   return(NULL);
}