//  
//  Copyright (c) 1994 by Mike Romberg (  romberg@md.fsl.noaa.gov )
//  Debugged and Featurized by Michael Hemy
//
//  This file may be distributed under terms of the GPL
//

#include "xosview.h"
#include "cpumeter.h"
#include "memmeter.h"
#include "swapmeter.h"
#include "netinmeter.h"
#include "netoutmeter.h"
#include <iostream.h>
#include <unistd.h>
#include <string.h>


#define NAME "xosview@"
int disable_free_legend_ = 0;


XOSView::XOSView( int argc, char *argv[] )
:XWin( argc, argv, 230, 200, winname(), 
       "cyan", "navy", "8x13bold", "yellow", "orange", "red", "black" ){
  parseargs( argc, argv );
  if ( netmax_ > 0 )
    nummeters_ = 5;
  else
    nummeters_ = 3;
  cpu_ = new CPUMeter( this, 40, 15, width_ - 10, 10 );
  mem_ = new MemMeter( this, 40, 40, width_ - 10, 10 );
  swap_ = new SwapMeter( this, 40, 65, width_ - 10, 10 );
  if ( netmax_ > 0 ){
    netin_ = new NetInMeter( this, 40, 90, width_ - 10, 10, netmax_ );
    netout_ = new NetOutMeter( this, 40, 115, width_ - 10, 10, netmax_ );
  }
  cpu_->dolegends( legend_ );
  mem_->dolegends( legend_ );
  swap_->dolegends( legend_ );
  if ( netmax_ > 0 ){
    netin_->dolegends( legend_ );
    netout_->dolegends( legend_ );
  }
  done_ = 0;
  resize();
}

void XOSView::parseargs( int argc, char *argv[] ){
  int i = 1;
  netmax_ = 0;
  legend_ = 0;
  while ( i <= argc - 1 ){
    if ( !strcmp( argv[i], "-l" ) ){
      legend_ = 1;
    }
    if ( !strcmp( argv[i], "-nf" ) ){
      disable_free_legend_ = 1;
    }
    else if ( !strcmp( argv[i], "-net" ) ){
      i++;
      if ( i >= argc ){
	cerr <<"XOSView::parseargs() : no parameter for option -net" <<endl;
	exit( 1 );
      }
      netmax_ = atof( argv[i] );
    }
    i++;
  }
}

const char *XOSView::winname( void ){
  static char name[100];
  char host[100];
  strcpy( name, NAME );
  gethostname( host, 99 );
  strcat( name, host );
  return name;
}


void  XOSView::resize( void ){
  int newwidth = width_ - (xoff_right_ + xoff_left_); 
  int font_space = yoff_above_ + yoff_below_;
  int newheight = (height_ - 6)/nummeters_ - font_space;
  cpu_->resize(xoff_left_,    font_space,               newwidth, newheight );
  mem_->resize(xoff_left_,  2*font_space +   newheight, newwidth, newheight );
  swap_->resize(xoff_left_, 3*font_space + 2*newheight, newwidth, newheight );
  if ( netmax_ > 0 ){
    netin_->resize(xoff_left_, 4*font_space + 3*newheight,newwidth, newheight);
    netout_->resize(xoff_left_, 5*font_space +4*newheight,newwidth, newheight);
  }
  XWin::resize();
}

XOSView::~XOSView( void ){
  delete cpu_;
  delete mem_;
  delete swap_;
  if ( netmax_ > 0 ){
    delete netin_;
    delete netout_;
  }
}

void XOSView::draw( void ){
  cpu_->draw();
  mem_->draw();
  swap_->draw();
  if ( netmax_ > 0 ){
    netin_->draw();
    netout_->draw();
  }
}

void XOSView::keyrelease( char *ch ){
  if ( (*ch == 'q') || (*ch == 'Q') )
    done_ = 1;
}

void XOSView::run( void ){
  int counter = 0;

  while( !done_ ){
    checkevent();
    cpu_->checkevent();
    if ( netmax_ > 0 ){
      NetMeter::checkevent();
      netin_->checkevent();
      netout_->checkevent();
    }
    if ( !counter ) {
      mem_->checkevent();
      swap_->checkevent();
    }

    flush();
    usleep( 200000 );
    counter = (counter + 1) % 5;    
  }
}

void XOSView::visnotify( void ){
  draw();
}
