/*
 * This file contains routines to draw and update the screen.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Services
 * 8 August 1990
 *
 *    $Source: /afs/net.mit.edu/tools/src/xport/RCS/xstuff.c,v $
 *    $Author: tom $
 *    $Locker: tom $
 *    $Log:	xstuff.c,v $
 * Revision 1.2  90/08/19  16:19:18  tom
 * *** empty log message ***
 * 
 * Revision 1.1  90/08/15  01:13:17  tom
 * Initial revision
 * 
 */

#ifndef lint
static char *rcsid = "$Header: /afs/net.mit.edu/tools/src/xport/RCS/xstuff.c,v 1.2 90/08/19 16:19:18 tom Exp Locker: tom $";
#endif

#include <X11/Xatom.h>
#include <X11/IntrinsicP.h>
#include <X11/CompositeP.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/StripChart.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/cursorfont.h>
#include <X11/Xaw/Dialog.h>
#include <X11/cursorfont.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/Sme.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/Repeater.h>
#include <X11/Xaw/ScrollbarP.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/ViewportP.h>
#include <X11/Quarks.h>

#include "xrmonwatch.h"

extern Widget headerW;

static int capture = 0;
static int start   = 1;
extern int s;              /* socket from snmp layer */
static Widget last;
static Widget list[100];
char *buf[100];
char *headerP;
int len[100];

int dataFile = 0;
int outputFile = 0;

static char header[300][300];
static int color[300];
static int list_ptr = 0;
static int size = 0;

extern XtAppContext app_con;

static XtIntervalId int_id = 0;
static XtInputId input_id = 0;

static void cb_header();

static char packetTranslations[] =
    "<EnterWindow>:     highlight() header() ";

static XtActionsRec packet_actions[] = {
    { "header", cb_header },
};

void
cb_dostuff(w, source, id)
     XtPointer w;
     int *source;
     XtInputId *id;
{
  Arg args[5];
  char *data;
  char *text;

  if(int_id)
    XtRemoveTimeOut(int_id);


  if(!outputFile)
	outputFile = open("data.out", O_RDWR);

  if((data = (char *) buffer_get_response(&len[list_ptr])))
    {
      bcopy(data, buf[list_ptr], len[list_ptr]); 
      if(outputFile)
        {
          write(outputFile, "\01", 1);
          write(outputFile, &len[list_ptr], 4);
          write(outputFile, "\01", 1);
          write(outputFile, data, len[list_ptr]);
        }
 
      headerP = header[list_ptr];
      *headerP = '\0';
      text = (char *) do_packet(data, len[list_ptr], 1, &color[list_ptr]);
      if(!dataFile)
	free(data);
      if(text)
	{	 
	  XtSetArg(args[0], XtNlabel, (XtArgVal) text);
	  XtSetArg(args[1], XtNforeground, (XtArgVal) color[list_ptr]);
	  XtSetValues(list[list_ptr], args, 2);
	/*  if(!list_ptr || strncmp(header[list_ptr], header[list_ptr-1], 
				  strlen(header[list_ptr])))
	     {
	       XtSetArg(args[0], XtNlabel, (XtArgVal) header[list_ptr]);
	       XtSetValues(headerW, args, 2);
	     }*/
	  ++list_ptr;
	  if(list_ptr >= size)
	    list_ptr = 0;
	}
    }

  if(!dataFile)
    XtRemoveInput(*id);
  if(!start)
    XtAppAddTimeOut(app_con, 1, cb_send, w);
  return;
}


void
cb_readFile(w, data, closure)
     Widget w;
     XtPointer data;
     XtPointer closure;
{
  if(dataFile)
    close(dataFile);
  
  if((dataFile = open("pout", O_RDONLY, 0644)) < 0)
    return;

  return;
}


void
cb_dpypacket(w, data, closure)
     Widget w;
     XtPointer data;
     XtPointer closure;
{
  XrmValue v;
  char *text;
  int color;
  int i;

  i = (int) data;
  text = (char *) do_packet(buf[i], len[i], 0, &color);
  make_text_popup(w, "packet", text, color);
}


static void
cb_header(w, event, params, n)
     Widget w;
     XEvent *event;
     String *params;
     Cardinal n;
{
  Arg args[2];
  static int last_ptr;
  register int i = 0;

  /*
   * I don't know how to pass data via a translation.. so I'm cheating
   */ 

  while((i < size) && (list[i] != w))
    ++i;
  if(i == size)
    return;

   if(!last_ptr ||  strcmp(header[i], header[last_ptr]))
     {
       XtSetArg(args[0], XtNlabel, (XtArgVal) header[i]);
       XtSetArg(args[1], XtNforeground, (XtArgVal) color[i]);
       XtSetValues(headerW, args, 2);
     }
  last_ptr = i;
}


void
cb_send(w, id)
     XtPointer w;
     XtIntervalId *id;
{
  if(!start && !dataFile)
    {
      buffer_send_query();
      input_id = XtAppAddInput(app_con, s, XtInputReadMask, cb_dostuff,  w);
      int_id = XtAppAddTimeOut(app_con, 1000, cb_retry, w); 
    }
  else
    int_id = XtAppAddTimeOut(app_con, 1, cb_dostuff, w); 

  return;
}


void cb_retry(w, id)
     XtPointer w;
     XtIntervalId *id;
{
  XtRemoveInput(input_id);
  printf("retry\n");
  cb_send(w, NULL);
  return;
}



void 
cb_atNBPUpdate(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  at_nbpUpdate();
}


void
cb_capture(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  Arg arg;

  if(capture)
    capture = 0;
  else 
    capture = 1;

  XtSetArg(arg, XtNlabel, (XtArgVal) capture ? "Capture" : "Scroll");
  XtSetValues(w, &arg, 1);
}


void
cb_start(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  Arg args[5];
  int i;
  Widget widget;
  XtTranslations translations;

  if(start)
    start = 0;
  else 
    start = 1;

  XtSetArg(args[0], XtNlabel, (XtArgVal) start ? "Start" : "Stop");
  XtSetValues(w, args, 1);
  if(!start)
    {
      bzero(header, sizeof(header));
      if(!size)
	{
	  size = 20;
	  XtAppAddActions (app_con, packet_actions, XtNumber(packet_actions));
	  translations = XtParseTranslationTable(packetTranslations);

	  XtSetArg(args[0], XtNwidth, (XtArgVal) 580);
	  list[0] = widget = 
	    XtCreateManagedWidget("packet", commandWidgetClass, 
					 data, args, 1);
	  XtOverrideTranslations(widget, translations);
	  for(i = 1; i < size; i++)
	    {
	      XtSetArg(args[1], XtNfromVert,   (XtArgVal) widget);
	       list[i] = widget = 
		 XtCreateManagedWidget("packet", commandWidgetClass, 
					 data, args, 2);
	      XtOverrideTranslations(widget, translations);
	      buf[i-1] = (char *) malloc(2048);
	      XtAddCallback(list[i - 1], XtNcallback, cb_dpypacket, i - 1);
	    }
	  buf[i-1] = (char *) malloc(2048);
	  XtAddCallback(list[i - 1], XtNcallback, cb_dpypacket, i - 1);
	}

      if(dataFile)
	int_id   = XtAppAddTimeOut(app_con, 1, cb_dostuff, w); 
      else
	{
	  buffer_send_query();
	  input_id = XtAppAddInput(app_con, s, XtInputReadMask, 
				   cb_dostuff, data);
	  int_id   = XtAppAddTimeOut(app_con, 1000, cb_retry, w); 
	}
    }
  else
    XtRemoveTimeOut(int_id);
  return;
}



void
cb_chdisplay(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{  
  char *t;

  t = (char *)(data);
  if(*t == (char) 1)
    *t = (char) 0;
  else
    *t = (char) 1;
}



void cb_filter(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  filter_ip_panel(w);
}


void cb_filterList(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  filter_list_panel(w);
}


void
cb_IPdisplay(w, data, closure)
{
  display_ip_panel(w);
}


void
cb_ATdisplay(w, data, closure)
{
  display_at_panel(w);
}


void
cb_quit(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  exit(0);
}


void
cb_destroy_widget(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  XtDestroyWidget((Widget) data);
}

void
cb_close_popup(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  XtUnmapWidget((Widget) data);
}


void
cb_buffer(w, data, closure)
     Widget w;
     caddr_t data;
     caddr_t closure;
{
  draw_buffer();
}
