/* **********************************************************************
 * *                                                                    *
 * *                                                                    *
 * *                                                                    *
 * **********************************************************************
 * File: utils/custom/runwayutil.c
 *
 *                        COPYRIGHT (c) 1998  BY
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED.
 */

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <Xm/MainW.h>
#include <Xm/TextF.h>
#include <Xm/Form.h>
#include <Xm/Label.h>

#define ACCESS (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP )

struct runwayutil
{
  int  runway_flag;
  char input_agl_server[10];  
  char input_runway_I_rep[10];
  char output_runway_II_cmd[10];
  char input_runway_II_rep[10];
  char output_runway_I_cmd[10];
} *runwayutil_data;
  
int runway_to = 0;

/********************************
 * local stuff               
 ********************************/
static Widget  agl_server_txtf_wgt_c;
static Widget  runway_I_rep_txtf_wgt_c; 
static Widget  runway_II_rep_txtf_wgt_c; 
static XtAppContext app_context_c;

static void create_runway_layout(Widget runway_form_a); 
static void runway_I_cmd_om_cbf(Widget wgt_a, int idx_a,
    XmPushButtonCallbackStruct* cbd_a );

static void runway_II_cmd_om_cbf(Widget wgt_a, int idx_a,
    XmPushButtonCallbackStruct* cbd_a );

static void runway_update_timer_cbf( XtPointer cdata_a, XtIntervalId *id_a )
{
   XmTextFieldSetString(agl_server_txtf_wgt_c, 
       runwayutil_data->input_agl_server);
   XmTextFieldSetString(runway_I_rep_txtf_wgt_c, 
       runwayutil_data->input_runway_I_rep);
   XmTextFieldSetString(runway_II_rep_txtf_wgt_c, 
       runwayutil_data->input_runway_II_rep);


   if (runwayutil_data->runway_flag == 0) runway_to++;
   else
   {
     runwayutil_data->runway_flag = 0;
     runway_to = 0;
   }
   if (runway_to >= 10)
   {
     runway_to = 0;
     runwayutil_data->input_agl_server[0]    = '\0' ;
     runwayutil_data->input_runway_I_rep[0]  = '\0' ;
     runwayutil_data->input_runway_II_rep[0] = '\0' ;
   }
   XtAppAddTimeOut( app_context_c, 1000, runway_update_timer_cbf, NULL );
}


int main( int argc_a, char** argv_a )
{
  char    *sptr;
  char    menu_title[100];
  Widget  runway_top;
  Widget  runway_main;
  Widget  runway_form;
  char    hostname[MAXHOSTNAMELEN];
  int     runway_key;

  if ( (runway_key = shmget(0x01020e00, sizeof(struct runwayutil), 
         IPC_CREAT | ACCESS)) <= 0 )
        
  {
    perror("shmget");
    exit(EXIT_FAILURE);
  }
  if ( (runwayutil_data = (struct runwayutil *)shmat(runway_key, NULL, 0))
         == (struct runwayutil *)-1 )
  {
    perror("shmat");
    exit(EXIT_FAILURE);
  }

  strcpy(runwayutil_data->output_runway_I_cmd, "AGL");
  strcpy(runwayutil_data->output_runway_II_cmd, "AGL");

  runwayutil_data->input_agl_server[0]    = '\0' ;
  runwayutil_data->input_runway_I_rep[0]  = '\0' ;
  runwayutil_data->input_runway_II_rep[0] = '\0' ;

  gethostname(hostname, MAXHOSTNAMELEN);
  if ((sptr = strchr(hostname, '.')) != NULL)
     *sptr = 0;
  sprintf(menu_title, "%s,  Runway Utility", hostname);

  setlocale(LC_ALL, "");
  if (XSupportsLocale())
    XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL);

  /* Top level and Main window
   */

  runway_top = XtVaAppInitialize( &app_context_c,"runway",NULL,0,
				   (int *) &argc_a, argv_a,NULL,NULL );

  XtVaSetValues(runway_top, XmNtitle, menu_title,
                XmNwidth, 310, XmNheight, 180, NULL );

  
  runway_main = XmCreateMainWindow( runway_top, (char *)"runwaymain", NULL, 0 );

  runway_form = XmCreateForm ( runway_main, NULL, NULL, 0);

  create_runway_layout(runway_form); 


  XtManageChild( runway_main );
  XtManageChild( runway_form );
  

  XtRealizeWidget( runway_top );  

  XtAppAddTimeOut( app_context_c, 100, runway_update_timer_cbf, NULL );

  XtAppMainLoop( app_context_c );

  exit(EXIT_SUCCESS) ;
}


static void create_runway_layout(Widget runway_form_a)
{
  int icnt;
  Arg arglist[20];
  Widget   agl_server_lab_wgt;
  Widget   runway_I_cmd_lab_wgt,  runway_I_cmd_om_wgt; 
  Widget   runway_II_cmd_lab_wgt,  runway_II_cmd_om_wgt; 
  Widget   runway_I_rep_lab_wgt; 
  Widget   runway_II_rep_lab_wgt; 
  XmString xsitem;
  XmString xsitems[3];

/* AGL SERVER */
  xsitem = XmStringCreateLocalized((char *)"AGL Server");
  icnt=0;
  XtSetArg(arglist[icnt], XmNlabelString, xsitem ); icnt++;
  XtSetArg(arglist[icnt], XmNalignment, XmALIGNMENT_END ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 180 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  agl_server_lab_wgt = XmCreateLabel(runway_form_a, NULL, arglist, icnt );
  XmStringFree(xsitem);

  icnt=0;
  XtSetArg(arglist[icnt], XmNcursorPositionVisible, FALSE ); icnt++;
  XtSetArg(arglist[icnt], XmNeditable, FALSE ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 100 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  agl_server_txtf_wgt_c = XmCreateTextField(runway_form_a, NULL, arglist, icnt );

  XtVaSetValues(agl_server_lab_wgt, 
                XmNtopAttachment, XmATTACH_FORM,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_FORM,
                XmNleftOffset, 5, NULL );

  XtVaSetValues(agl_server_txtf_wgt_c,
                XmNtopAttachment, XmATTACH_FORM,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_WIDGET,
                XmNleftWidget, agl_server_lab_wgt,
                XmNleftOffset, 10, NULL );

  XtManageChild(agl_server_lab_wgt);
  XtManageChild(agl_server_txtf_wgt_c);

/* RUNWAY I COMMAND */
  xsitem = XmStringCreateLocalized((char *)"Runway I Commanded");
  icnt=0;
  XtSetArg(arglist[icnt], XmNlabelString, xsitem ); icnt++;
  XtSetArg(arglist[icnt], XmNalignment, XmALIGNMENT_END ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 180 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  runway_I_cmd_lab_wgt = XmCreateLabel(runway_form_a, NULL, arglist, icnt );
  XmStringFree (xsitem);

  xsitem = XmStringCreateLocalized((char *)"\0");
  xsitems[0] = XmStringCreateLocalized((char *)"AGL");
  xsitems[1] = XmStringCreateLocalized((char *)"32R");
  xsitems[2] = XmStringCreateLocalized((char *)"14L");

  icnt=0;
  XtSetArg(arglist[icnt], XmNspacing, 0); icnt++;
  XtSetArg(arglist[icnt], XmNmarginWidth, 0); icnt++;
  XtSetArg(arglist[icnt], XmNmarginHeight, 0); icnt++;
  XtSetArg(arglist[icnt], XmNlabelString, xsitem ); icnt++;
  XtSetArg(arglist[icnt], XmNbuttonCount, 3); icnt++;
  XtSetArg(arglist[icnt], XmNbuttons, xsitems); icnt++;
  XtSetArg(arglist[icnt], XmNsimpleCallback, runway_I_cmd_om_cbf); icnt++;
  runway_I_cmd_om_wgt = XmCreateSimpleOptionMenu(runway_form_a, NULL, arglist, icnt);

  XtVaSetValues(runway_I_cmd_lab_wgt, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget, agl_server_lab_wgt,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_FORM,
                XmNleftOffset, 5, NULL );

  XtVaSetValues(runway_I_cmd_om_wgt, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget,agl_server_lab_wgt, 
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_WIDGET,
                XmNleftWidget, runway_I_cmd_lab_wgt,
                XmNleftOffset, 5, NULL );

  XtManageChild(runway_I_cmd_lab_wgt);
  XtManageChild(runway_I_cmd_om_wgt);

/* RUNWAY II COMMAND */
  xsitem = XmStringCreateLocalized((char *)"Runway II Commanded");
  icnt=0;
  XtSetArg(arglist[icnt], XmNlabelString, xsitem ); icnt++;
  XtSetArg(arglist[icnt], XmNalignment, XmALIGNMENT_END ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 180 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  runway_II_cmd_lab_wgt = XmCreateLabel(runway_form_a, NULL, arglist, icnt );
  XmStringFree (xsitem);

  xsitem = XmStringCreateLocalized((char *)"\0");
  xsitems[0] = XmStringCreateLocalized((char *)"AGL");
  xsitems[1] = XmStringCreateLocalized((char *)"32L");
  xsitems[2] = XmStringCreateLocalized((char *)"14R");

  icnt=0;
  XtSetArg(arglist[icnt], XmNspacing, 0); icnt++;
  XtSetArg(arglist[icnt], XmNmarginWidth, 0); icnt++;
  XtSetArg(arglist[icnt], XmNmarginHeight, 0); icnt++;
  XtSetArg(arglist[icnt], XmNlabelString, xsitem ); icnt++;
  XtSetArg(arglist[icnt], XmNbuttonCount, 3); icnt++;
  XtSetArg(arglist[icnt], XmNbuttons, xsitems); icnt++;
  XtSetArg(arglist[icnt], XmNsimpleCallback, runway_II_cmd_om_cbf); icnt++;
  runway_II_cmd_om_wgt = XmCreateSimpleOptionMenu(runway_form_a, NULL, arglist, icnt);

  XtVaSetValues(runway_II_cmd_lab_wgt, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget, runway_I_cmd_lab_wgt,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_FORM,
                XmNleftOffset, 5, NULL );

  XtVaSetValues(runway_II_cmd_om_wgt, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget, runway_I_cmd_lab_wgt, 
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_WIDGET,
                XmNleftWidget, runway_II_cmd_lab_wgt,
                XmNleftOffset, 5, NULL );

  XtManageChild(runway_II_cmd_lab_wgt);
  XtManageChild(runway_II_cmd_om_wgt);

/* Runway I Reported */
  xsitem = XmStringCreateLocalized((char *)"Runway I Reported");
  icnt=0;
  XtSetArg(arglist[icnt], XmNlabelString, xsitem ); icnt++;
  XtSetArg(arglist[icnt], XmNalignment, XmALIGNMENT_END ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 180 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  runway_I_rep_lab_wgt = XmCreateLabel(runway_form_a, NULL, arglist, icnt );
  XmStringFree(xsitem);

  icnt=0;
  XtSetArg(arglist[icnt], XmNcursorPositionVisible, FALSE ); icnt++;
  XtSetArg(arglist[icnt], XmNeditable, FALSE ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 100 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  runway_I_rep_txtf_wgt_c = XmCreateTextField(runway_form_a, NULL, arglist, icnt );

  XtVaSetValues(runway_I_rep_lab_wgt, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget, runway_II_cmd_lab_wgt,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_FORM,
                XmNleftOffset, 5, NULL );

  XtVaSetValues(runway_I_rep_txtf_wgt_c, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget, runway_II_cmd_lab_wgt,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_WIDGET,
                XmNleftWidget, runway_I_rep_lab_wgt,
                XmNleftOffset, 10, NULL );

  XtManageChild(runway_I_rep_lab_wgt);
  XtManageChild(runway_I_rep_txtf_wgt_c);

/* Runway II Reported */
  xsitem = XmStringCreateLocalized((char *)"Runway II Reported");
  icnt=0;
  XtSetArg(arglist[icnt], XmNlabelString, xsitem ); icnt++;
  XtSetArg(arglist[icnt], XmNalignment, XmALIGNMENT_END ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 180 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  runway_II_rep_lab_wgt = XmCreateLabel(runway_form_a, NULL, arglist, icnt );
  XmStringFree(xsitem);

  icnt=0;
  XtSetArg(arglist[icnt], XmNcursorPositionVisible, FALSE ); icnt++;
  XtSetArg(arglist[icnt], XmNeditable, FALSE ); icnt++;
  XtSetArg(arglist[icnt], XmNwidth, 100 ); icnt++;
  XtSetArg(arglist[icnt], XmNheight, 30 ); icnt++;
  runway_II_rep_txtf_wgt_c = XmCreateTextField(runway_form_a, NULL, arglist, icnt );

  XtVaSetValues(runway_II_rep_lab_wgt, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget, runway_I_rep_lab_wgt,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_FORM,
                XmNleftOffset, 5, NULL );

  XtVaSetValues(runway_II_rep_txtf_wgt_c, XmNtopAttachment, XmATTACH_WIDGET,
                XmNtopWidget, runway_I_rep_lab_wgt,
                XmNtopOffset, 5,
                XmNleftAttachment, XmATTACH_WIDGET,
                XmNleftWidget, runway_II_rep_lab_wgt,
                XmNleftOffset, 10, NULL );

  XtManageChild(runway_II_rep_lab_wgt);
  XtManageChild(runway_II_rep_txtf_wgt_c);

}

static void runway_I_cmd_om_cbf(Widget wgt_a, int idx_a,
    XmPushButtonCallbackStruct* cbd_a )
{

  char         *sptr;
  XmString     xsitem;

  XtVaGetValues( wgt_a, XmNlabelString, &xsitem, NULL );
  XmStringGetLtoR( xsitem, XmFONTLIST_DEFAULT_TAG, &sptr );

  strncpy(runwayutil_data->output_runway_I_cmd, sptr, 10);
  runwayutil_data->output_runway_I_cmd[9] = 0;

  XmStringFree(xsitem);
  XtFree(sptr);

}

static void runway_II_cmd_om_cbf(Widget wgt_a, int idx_a,
    XmPushButtonCallbackStruct* cbd_a )
{
  char         *sptr;
  XmString     xsitem;

  XtVaGetValues( wgt_a, XmNlabelString, &xsitem, NULL );
  XmStringGetLtoR( xsitem, XmFONTLIST_DEFAULT_TAG, &sptr );

  strncpy(runwayutil_data->output_runway_II_cmd, sptr, 10);
  runwayutil_data->output_runway_II_cmd[9] = 0;

  XmStringFree(xsitem);
  XtFree(sptr);


}
