/* STATUS.C
 *==========================================================================
 * DATE:	February 2, 1990
 * DESCRIPTION:
 */




/* INCLUDE FILES
 *==========================================================================
 */
#include <sys\gemskel.h>
#include <tos.h>
#include <string.h>
#include <stdlib.h>

#include "general.h"
#include "super.h"



/* PROTOTYPES
 *==========================================================================
 */
void do_os( void );
void do_status( void );
void clear_status( void );
void do_memory( int Total_object, int Free_object );
void wait_button( int flag );



/* DEFINES
 *==========================================================================
 */
#define UP 	0				/* wait button up   */
#define DOWN 	1				/* wait button down */
#define STRAM	0

#define VERTICAL	0
#define HORIZONTAL	1
#define SCROLL_OFFSET	8



/* EXTERNALS
 *==========================================================================
 */
extern long rs_trindex[];


/* GLOBALS
 *==========================================================================
 */
int  errno;				/* errno declaration		*/

int  max_stat;				/* max and min y value of status*/
int  min_stat;				/* Note, these are y offsets... */
char tosdate[10];			/* TOS VERSON DATE		*/
char tosvers[10];			/* TOS VERSION NUMBER		*/

char total[8];				/* ST RAM total available       */
char tfree[8];				/* ST RAM free...		*/

int blit_inc;				/* incrementer for scrolling of */
					/* the status box...		*/
					/* see sl_y for implementation. */
int stat_value;				/* y variable of status slider  */
					/* Note: it is an offset	*/
					/* off of the current y value   */

/* FUNCTIONS
 *==========================================================================
 */


/* do_os()
 *==========================================================================
 */
void
do_os( void )
{
  OBJECT *ad_partz    = (OBJECT *)rs_trindex[PARTZ];
  OBJECT *tree;
  char *version;
  char *osdate;
  int j;
  SYSHDR *osheader;
  char  num;
  
  Enter_Super();
  osheader = *((SYSHDR **)0x4f2L);
  Exit_Super();
  
  osheader = osheader->os_base;
  version  = (char *)&osheader->os_version;
  osdate   = (char *)&osheader->os_gendat;
  
  for(j = 0;j<=3;j += 2)
  {
    num = *version++;
    tosvers[j] = ((num & 0xf0) >> 4) + '0';
    tosvers[j+1] = (num & 0x0f) + '0';
  }
  
  for(j = 0;j<=7;j += 2)
  {
     num = *osdate++;
     tosdate[j] = ((num & 0xf0) >> 4) + '0';
     tosdate[j+1] =  (num & 0x0f) +  '0';
  }
  
  ActiveTree( ad_partz );
  
  TedText( OSDATE ) = &tosdate[0];
  TedText( VERSION ) = &tosvers[0];
}




/* do_status()
 *==========================================================================
 */
void
do_status( void )
{
  OBJECT *ad_tree  = (OBJECT *)rs_trindex[GENERAL];
  OBJECT *ad_partz = (OBJECT *)rs_trindex[PARTZ];
  OBJECT *tree;
  int x,y;
  int button;
  int done = FALSE;

  ActiveTree( ad_tree );
  x = ObX( ROOT );
  y = ObY( ROOT );
  Deselect( STATUS );
    
  ActiveTree( ad_partz );
  ObX( ROOT ) = x;
  ObY( ROOT ) = y;
  
  do_os();
  do_memory( STTOTAL, STFREE );

  Objc_draw( tree, ROOT, MAX_DEPTH, NULL );
  do
  {
     button = form_do( tree, 0 );
     
     switch( button )
     {
     	  case STATOK:   done = TRUE;
     	  		 SetNormal( button );	
       	  		 clear_status();
       	  		 break;
          		 
          default:	 done = TRUE;
          		 clear_status();
          	    	 break;
     }
  }while( !done ); 
}



/* clear_status()
 *==========================================================================
 */
void
clear_status( void )
{
  OBJECT *ad_tree  = (OBJECT *)rs_trindex[GENERAL];
  OBJECT *ad_partz = (OBJECT *)rs_trindex[PARTZ];
  OBJECT *tree;

  ActiveTree( ad_partz );
  Deselect( STATOK );
  
  ActiveTree( ad_tree );
  Objc_draw( ad_tree, GENERAL, MAX_DEPTH, NULL );
  wait_button( UP );
}



/* do_memory()
 *==========================================================================
 */
void
do_memory( int Total_object, int Free_object )
{
  OBJECT *ad_partz = (OBJECT *)rs_trindex[PARTZ];
  OBJECT *tree;
 
  long totalnum = 0L;
  long freenum  = 0L;
  long tempnum  = 0L;
  
  long *mptr;
  long *head;
  long *tptr;
  long *xptr;
  
  char *xtotal;
  char *xfree;

  Enter_Super();
  totalnum = *(long *)0x42EL;
  Exit_Super();

  xtotal = (char *)&total;
  xfree  = (char *)&tfree;
  
  ltoa( totalnum, xtotal, 10 );

  head = mptr = tptr = 0L;
  tempnum = ( long )Malloc( -1L );
  while( tempnum > 4L )
  {
     if( !head )
     {
        head  = mptr = Malloc( tempnum );
        *head = 0L;
     }
     else
     {
        tptr  = Malloc( tempnum );
        *tptr = 0L;
        *mptr = ( long )tptr;
        mptr  = tptr;
     }
     freenum += tempnum;
     tempnum = ( long )Malloc( -1L );       
  }
  

  if( head )
  {  
    tptr = 0L;
    while( *head )
    {
       mptr = (long *)*head;
       xptr = head;
       while( mptr )
       {
       	 tptr = mptr;
       	 mptr = ( long *)*mptr;
       	 if( mptr )
       	     xptr = tptr;
       }
       if( tptr )
       {
          Mfree( tptr );
          tptr  = ( long *)0L;
          *xptr = 0L;
       }
    }
    Mfree( head );
  }
  
  ltoa( freenum, xfree, 10 );
  
  ActiveTree( ad_partz );
  ObString( Total_object ) = xtotal;  
  ObString( Free_object )  = xfree;
}


