/*
 * 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 "xrmonwatch.h"

extern XtAppContext app_con;
extern Widget toplevel;
static Widget IPpopup = (Widget) NULL;
static Widget ATpopup = (Widget) NULL;

char header[100][12];

struct DISPLAY_SETTING dpy_settings[] =
{
  {Off,    0,            0,  "",              ""}, 
  {Off,    0,            4,  "Eth",           "Ethernet"},
  {Off,    R_Ethernet,   8,  "type",          "Ethernet type"},
  {Off,    R_Ethernet,   4,  "len",           "Ethernet length"},
  {On,     R_Ethernet,  12,  "src Addr",      "Ethernet src address"},
  {On,     R_Ethernet,  12,  "dst Addr",      "Ethernet dst address"},
  {Off,    R_Ethernet,   4,  "ARP",           "Address Resolution Protocol"},
  {Off,    R_ARP,       14,  "src phys addr", "ARP src phys addr"},
  {Off,    R_ARP,       14,  "dst phys addr", "ARP dst phys addr"},
  {On,     R_ARP,       16,  "src addr",      "ARP src proto addr"},
  {On,     R_ARP,       16,  "dst addr",      "ARP dst proto addr"},
  {Off,    R_Ethernet,   4,  "IP",            "Internet Protocol"},
  {Off,    R_IP,         4,  "vers",          "IP version"},
  {Off,    R_IP,         4,  "hlen",          "IP header length"}, 
  {Off,    R_IP,         4,  "type",          "IP type"},
  {Off,    R_IP,         5,  "len",           "IP length"},
  {Off,    R_IP,         4,  "frag",          "IP fragment"},
  {Off,    R_IP,         6,  "offset",        "IP fragment offset"}, 
  {Off,    R_IP,         5,  "ident",         "IP identification"}, 
  {Off,    R_IP,         3,  "ttl",           "IP ttl"},
  {On,     R_IP,         5,  "proto",         "IP protocol"}, 
  {Off,    R_IP,         5,  "xsum",          "IP checksum"}, 
  {On,     R_IP,        16,  "src addr"       "IP src address"},
  {On,     R_IP,        16,  "dst addr",      "IP dst address"},
  {Off,    R_IP,         4,  "ICMP",          "Internet Control Management Protocol"}, 
  {On,     R_ICMP,       4,  "type",          "ICMP type"},
  {Off,    R_ICMP,       4,  "code",          "ICMP code"},
  {Off,    R_ICMP,       5,  "xsum",          "ICMP checksum"},
  {Off,    R_ICMP,       0,  "",              "ICMP IP header"},
  {Off,    R_ICMP,       3,  "ptr",           "ICMP pointer"},
  {Off,    R_ICMP,      16, "gateway",        "ICMP gateway"},
  {Off,    R_ICMP,       5,  "ident",         "ICMP identifier"},
  {Off,    R_ICMP,       5,  "seq",           "ICMP sequence"},
  {Off,    R_ICMP,      12, "orig time",      "ICMP orig timestamp"},
  {Off,    R_ICMP,      12, "recv time",      "ICMP recv timestamp"},
  {Off,    R_ICMP,      12, "trans time",     "ICMP trans timestamp"},
  {Off,    R_ICMP,       4,  "data",          "ICMP data"},
  {Off,    R_IP,         4,  "UDP",           "Universal Datagram Protocol"},
  {Off,    R_UDP,        5,  "xsum",          "UDP checksum"},
  {Off,    R_UDP,        5,  "len",           "UDP length"},
  {On,     R_UDP,       12, "src port",       "UDP src port"},
  {On,     R_UDP,       12, "dst port"        "UDP dst port"},
  {Off,    R_IP,         4,  "TCP",           "Transmission Control Protocol"},
  {Off,    R_IP,         8,  "seq",           "TCP sequence"},
  {Off,    R_IP,         8,  "ack",           "TCP ack"},
  {On,     R_IP,        12, "src port",       "TCP src port"},
  {On,     R_IP,        12, "dst port",       "TCP dst port"},
  {Off,    R_Ethernet,   4,  "AARP",          "Apple Address Resolution Protocol"},
  {Off,    R_AARP,       5,  "htype",         "AARP hardware type"},
  {Off,    R_AARP,       5,  "ptype",         "AARP proto type"},
  {Off,    R_AARP,       4,  "hlen",          "AARP hardware length"},
  {Off,    R_AARP,       4,  "plen",          "AARP proto length"},
  {Off,    R_AARP,       5,  "haddr",         "AARP hardware addr"},
  {Off,    R_AARP,       5,  "paddr",         "AARP proto addr"},
  {Off,    R_Ethernet,   4,  "ELAP",          "Ethertalk Link Access Protocol"},
  {Off,    R_ELAP,       8,  "src addr",      "ELAP src address"},
  {Off,    R_ELAP,       8,  "dst addr",      "ELAP dst address"},
  {Off,    R_ELAP,       5,  "type",          "ELAP type"},
  {Off,    R_ELAP,       4,  "DDP",           "Datagram Delivery Protocol"},
  {Off,    R_DDP,        5,  "len",           "DDP length"},
  {Off,    R_DDP,        4,  "hops",          "DDP hop count"},
  {Off,    R_DDP,        5,  "type",          "DDP type"},
  {Off,    R_DDP,        5,  "xsum",          "DDP checksum"},
  {Off,    R_DDP,        7,  "src net",       "DDP src network"},
  {Off,    R_DDP,        7,  "dst net",       "DDP dst network"},
  {On,     R_DDP,        7,  "src node",      "DDP src node"},
  {On,     R_DDP,        7,  "dst node",      "DDP dst node"},
  {On,     R_DDP,        7,  "src sock",      "DDP src socket"},
  {On,     R_DDP,        7,  "dst sock",      "DDP dst socket"},
  {Off,    R_DDP,        4,  "RTMP",          "Routing Table Maintainance Protocol"},
  {Off,    R_DDP,        4,  "AEP",           "Appletalk Echo Protocol"}, 
  {Off,    R_AEP,        4,  "type",          "AEP type"},
  {Off,    R_DDP,        4,  "NBP",           "Name Binding Protocol"},
  {Off,    R_NBP,        4,  "type",          "NBP type"},
  {Off,    R_NBP,        5,  "ident",         "NBP identification"},
  {End,    0,            3,  "end",           "End of list"},
};


/*
 * macros for creating widgets
 */

#define create_row(leftname, lval, rightname, rval)                           \
  cbox    = XtCreateManagedWidget(leftname"Form", formWidgetClass,            \
				  grpForm, NULL, 0);                          \
  XtCreateManagedWidget(leftname"Label",   labelWidgetClass,  cbox, NULL, 0); \
  XtSetArg(carg, XtNstate, lval);                                             \
  XtCreateManagedWidget(leftname"Toggle",  toggleWidgetClass, cbox,&carg, 1); \
  XtSetArg(carg, XtNstate, rval);                                             \
  XtCreateManagedWidget(rightname"Toggle", toggleWidgetClass, cbox,&carg, 1); \
  XtCreateManagedWidget(rightname"Label",  labelWidgetClass,  cbox, NULL, 0)
  

#define create_box(name)                                                      \
  grpForm = XtCreateManagedWidget(name"Form",formWidgetClass,cform, NULL, 0); \
  XtCreateManagedWidget(name"Label", labelWidgetClass,  grpForm, NULL, 0) 


#define create_form(toplevel, name)                                           \
  cwidget = XtCreateManagedWidget(name"Viewport", viewportWidgetClass,        \
				  toplevel, NULL, 0);                         \
  cform  = XtCreateManagedWidget(name"PopupForm", formWidgetClass,            \
				 cwidget, NULL, 0);                           \
  XtCreateManagedWidget(name"PopupLabel", labelWidgetClass, cform, NULL, 0) 



/* 
 * variables used by macros
 */

Widget grpForm;
Widget cbox;
Widget cform;
Widget cwidget;
Arg    carg;


display_ip_panel(w)
{

  if(IPpopup)
    {
      XtMapWidget(IPpopup);
      XRaiseWindow(XtDisplay(IPpopup), XtWindow(IPpopup));
      return;
    }

  IPpopup= XtCreatePopupShell("dpyPopup", 
			      topLevelShellWidgetClass, 
			      toplevel, NULL, 0);

  create_form(IPpopup, "dpyTCPIP");

  /*
   * Ethernet box
   */

  create_box("dpyEther");
  create_row("dpyEtherType",       dpy_settings[R_EthernetType].d, 
	     "dpyEtherLen",        dpy_settings[R_EthernetLength].d);
  create_row("dpyEtherSrcAddr",    dpy_settings[R_EthernetSrcAddress].d, 
	     "dpyEtherDstAddr",    dpy_settings[R_EthernetDstAddress].d);
  
  
  /*
   * ARP
   */

  create_box("dpyARP");
  create_row("dpyARPSrcHAddr",     dpy_settings[R_ARPSrcPhysicalAddress].d,
	     "dpyARPDstHAddr",     dpy_settings[R_ARPTargetPhysicalAddress].d);
  create_row("dpyARPSrcPAddr",     dpy_settings[R_ARPSrcProtocolAddress].d,
	     "dpyARPDstPAddr",     dpy_settings[R_ARPTargetProtocolAddress].d);


  /*
   * IP
   */

  create_box("dpyIP");
  create_row("dpyIPVersion",       dpy_settings[R_IPVersion].d,
	     "dpyIPIHL",           dpy_settings[R_IPHeaderLength].d);
  create_row("dpyIPType",          dpy_settings[R_IPType].d, 
	     "dpyIPLen",           dpy_settings[R_IPLength].d);
  create_row("dpyIPFragment",      dpy_settings[R_IPFragment].d,
	     "dpyIPFragOffset",    dpy_settings[R_IPFragmentOffset].d);
  create_row("dpyIPIdent",         dpy_settings[R_IPIdentfication].d,
	     "dpyIPTTL",           dpy_settings[R_IPTTL].d);
  create_row("dpyIPProto",         dpy_settings[R_IPProtocol].d,
	     "dpyIPXSum",          dpy_settings[R_IPChecksum].d);
  create_row("dpyIPSrcAddr",       dpy_settings[R_IPSrcAddress].d,
	     "dpyIPDstAddr",       dpy_settings[R_IPDstAddress].d);


  /*
   *  ICMP
   */
  
  create_box("dpyICMP");
  create_row("dpyICMPType",        dpy_settings[R_ICMPType].d,
	     "dpyICMPCode",        dpy_settings[R_ICMPCode].d);
  create_row("dpyICMPXSum",        dpy_settings[R_ICMPChecksum].d,
	     "dpyICMPIPData",      dpy_settings[R_ICMPIPHeader].d);
  create_row("dpyICMPPtr",         dpy_settings[R_ICMPPointer].d,
	     "dpyICMPGw",          dpy_settings[R_ICMPGateway].d);
  create_row("dpyICMPIdent",       dpy_settings[R_ICMPIdentifier].d,
	     "dpyICMPSeq",         dpy_settings[R_ICMPSequence].d);
  create_row("dpyICMPTimeOrig",    dpy_settings[R_ICMPOriginateTimestamp].d,
	     "dpyICMPTimeRecv",    dpy_settings[R_ICMPReceiveTimestamp].d);
  create_row("dpyICMPTimeTrans",   dpy_settings[R_ICMPTransmitTimestamp].d,
	     "dpyICMPData",        dpy_settings[R_ICMPData].d);


  /*
   *  UDP
   */

  create_box("dpyUDP");
  create_row("dpyUDPXSum",         dpy_settings[R_UDPChecksum].d,
	     "dpyUDPLen",          dpy_settings[R_UCPLength].d);
  create_row("dpyUDPSrcPort",      dpy_settings[R_UCPSrcPort].d,
	     "dpyUDPDstPort",      dpy_settings[R_UDPDstPort].d);

  
  /*
   *  TCP
   */

  create_box("dpyTCP");
  create_row("dpyTCPSeq",          dpy_settings[R_TCPSequence].d,
	     "dpyTCPAck",          dpy_settings[R_TCPAck].d);
  create_row("dpyTCPSrcPort",      dpy_settings[R_TCPSrcPort].d,
	     "dpyTCPDstPort",      dpy_settings[R_TCPDstPort].d);

  XtPopup(IPpopup, XtGrabNone);
  return;
}



display_at_panel(w)
{

  if(ATpopup)
    {
      XtMapWidget(ATpopup);
      XRaiseWindow(XtDisplay(ATpopup), XtWindow(ATpopup));
      return;
    }

  ATpopup= XtCreatePopupShell("dpyPopup", 
			      topLevelShellWidgetClass, 
			      toplevel, NULL, 0);
  
  create_form(ATpopup, "dpyAT");


  /*
   * Ethernet box
   */

  create_box("dpyEther");
  create_row("dpyEtherType",       dpy_settings[R_EthernetType].d,
	     "dpyEtherLen",        dpy_settings[R_EthernetLength].d);
  create_row("dpyEtherSrcAddr",    dpy_settings[R_EthernetSrcAddress].d,
	     "dpyEtherDstAddr",    dpy_settings[R_EthernetDstAddress].d);

  
  /*
   * AARP
   */

  create_box("dpyAARP");
  create_row("dpyAARPHType",       dpy_settings[R_AARPHardwareType].d,
	     "dpyAARPPType",       dpy_settings[R_AARPProtocolType].d);
  create_row("dpyAARPHLength",     dpy_settings[R_AARPHardwareLength].d,
	     "dpyAARPPLength",     dpy_settings[R_AARPProtocolLength].d);
  create_row("dpyAARPHAddr",       dpy_settings[R_AARPHardwareAddress].d,
	     "dpyAARPPAddr",       dpy_settings[R_AARPProtocolAddress].d);


  /*
   * ELAP
   */

  create_box("dpyELAP"); 
  create_row("dpyELAPSrcAddr",     dpy_settings[R_ELAPSrcAddress].d,
	     "dpyELAPDstAddr",     dpy_settings[R_ELAPDstAddress].d);
  create_row("dpyELAPType",        dpy_settings[R_ELAPType].d,
	     "dpyEmpty",           0);


  /*
   * DDP
   */

  create_box("dpyDDP");
  create_row("dpyDDPLength",       dpy_settings[R_DDPLength].d,
	     "dpyDDPHop",          dpy_settings[R_DDPHopCount].d);
  create_row("dpyDDPType",         dpy_settings[R_DDPType].d,
	     "dpyDDPXSum",         dpy_settings[R_DDPChecksum].d);
  create_row("dpyDDPSrcNet",       dpy_settings[R_DDPSrcNetwork].d,
	     "dpyDDPDstNet",       dpy_settings[R_DDPDstNetwork].d);
  create_row("dpyDDPSrcNode",      dpy_settings[R_DDPSrcNode].d,
	     "dpyDDPDstNode",      dpy_settings[R_DDPDstNode].d);
  create_row("dpyDDPSrcSocket",    dpy_settings[R_DDPSrcSocket].d,
	     "dpyDDPDstSocket",    dpy_settings[R_DDPDstSocket].d);


  /*
   *  RTMP
   */

  create_box("dpyRTMP");


  /*
   *  AEP
   */

  create_box("dpyAEP");
  create_row("dpyAEPType",         dpy_settings[R_AEPType].d,
	     "dpyEmpty",           0);


  /*
   *  NBP
   */

  create_box("dpyNBP"); 
  create_row("dpyNBPType",        dpy_settings[R_NBPType].d,
	     "dpyNBPId",          dpy_settings[R_NBPIdentification].d);
  

  XtPopup(ATpopup, XtGrabNone);
  return;
}


new_header()
{
  

}
