static char TiburonApp_id[] = "$Header: /u/oreilly/rov/tmacs/RCS/TiburonApp.cc,v 1.9 1997/09/10 08:53:43 oreilly Exp $";


/*
$Log: TiburonApp.cc,v $
Revision 1.9  1997/09/10 08:53:43  oreilly
*** empty log message ***

Revision 1.8  97/08/12  14:58:41  14:58:41  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.7  97/06/16  16:00:00  16:00:00  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.6  97/05/06  16:32:19  16:32:19  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.5  97/03/20  12:31:09  12:31:09  oreilly (Thomas C. O'Reilly)
..

Revision 1.4  96/11/04  15:23:04  15:23:04  oreilly (Thomas C. O'Reilly)
Removed system micros object

Revision 1.3  96/10/28  09:12:43  09:12:43  oreilly (Thomas C. O'Reilly)
*** empty log message ***

Revision 1.2  96/07/22  10:42:14  10:42:14  oreilly (Thomas C. O'Reilly)
First external release

*/
#include <stdlib.h>
#include <xpm.h>
#include "smallTiburon.xpm"
#include "TiburonApp.h"
#include "tiburon.h"

XtResource TiburonApp::_resources[] = 
{
  {  
    "priority1Background",
    "Priority1Background",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _priority1Background),
    XmRString,
    "Red"
  },

  {  
    "priority1Foreground",
    "Priority1Foreground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _priority1Foreground),
    XmRString,
    "White"
  },

  {  
    "priority2Background",
    "Priority2Background",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _priority2Background),
    XmRString,
    "Yellow"
  },

  {  
    "priority2Foreground",
    "Priority2Foreground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _priority2Foreground),
    XmRString,
    "Black"
  },

  {  
    "priority3Background",
    "Priority3Background",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _priority3Background),
    XmRString,
    "Green"
  },

  {  
    "priority3Foreground",
    "Priority3Foreground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _priority3Foreground),
    XmRString,
    "Black"
  },

  {  
    "okForeground",
    "OkForeground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _okForeground),
    XmRString,
    "Black"
  },

  {  
    "okBackground",
    "OkBackground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _okBackground),
    XmRString,
    "Green"
  },

  {  
    "onForeground",
    "OnForeground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _onForeground),
    XmRString,
    "black"
  },

  {  
    "onBackground",
    "OnBackground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _onBackground),
    XmRString,
    "Green"
  },

  {  
    "offForeground",
    "OffForeground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _offForeground),
    XmRString,
    "white"
  },

  {  
    "offBackground",
    "OffBackground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _offBackground),
    XmRString,
    "black"
  },

  {  
    "faultForeground",
    "FaultForeground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _faultForeground),
    XmRString,
    "white"
  },

  {  
    "faultBackground",
    "FaultBackground",
    XtRPixel,
    sizeof(Pixel),
    XtOffset(TiburonApp *, _faultBackground),
    XmRString,
    "red"
  }
};


XtActionsRec TiburonApp::_actions[] = 
{
};


TiburonApp::TiburonApp(char *appClassName,
		       int *argc,
		       char **argv,
		       XrmOptionDescRec *optionList,
		       int sizeOfOptionList) : DmGuiApp(appClassName,
							argc, argv,
							optionList,
							sizeOfOptionList)

{
  getResources(_resources, XtNumber(_resources));
  
  XtAppAddActions(appContext(), _actions, XtNumber(_actions));


  _platformName = 0;

  // Set platform name to "TIBURON" by default
  setPlatformName(TIBURON_DM_PREFIX);

  // Set icon pixmap
  XpmAttributes xpmAttributes;

  xpmAttributes.closeness = 40000;
  xpmAttributes.valuemask = XpmSize | XpmCloseness;
  
  Pixmap mask;
  
  if (XpmCreatePixmapFromData(display(), 
			      DefaultRootWindow(display()),
			      smallTiburon_xpm,
			      &_iconPixmap,
			      &mask,
			      &xpmAttributes) != XpmSuccess)
  {
    _iconPixmap = NULL;
  }

  char errorBuf[512];
  
  if (defaultInputs(errorBuf) == -1)
  {
    fprintf(stderr, "%s\n", errorBuf);
    this->exit(1);
  }

  getOptions(*argc, argv);
  
  systemAlarms = new SystemAlarms(_systemAlarmFile);
  if (systemAlarms->error())
  {
    systemAlarms->prtErrorMsg();
    this->exit(1);
  }

  vehicleConfig = new VehicleConfiguration(_vehicleConfigFile);
  if (vehicleConfig->error())
  {
    vehicleConfig->prtErrorMsg();
    this->exit(1);
  }
}


TiburonApp::~TiburonApp()
{
  if (_iconPixmap)
    XFreePixmap(theApplication->display(), _iconPixmap);

}


RovMainWindow *TiburonApp::createRovMainWindow()
{
  int screenNum = DefaultScreen(display());
  XWindowAttributes windowAttr;
  
  if (XGetWindowAttributes(display(), RootWindow(display(), screenNum),
			   &windowAttr) == 0)
  {
    fprintf(stderr, 
	    "TiburonApp::TiburonApp() - XGetWindowAttributes failed\n");
    exit(1);
  }
  const int sideWindowMargin = 20;
  const int topWindowMargin = 30;
  
  int width = windowAttr.width - sideWindowMargin;
  int height = (int )(0.95 * windowAttr.height);
  int viewPortWidth = (int )(0.65 * width);
  int viewPortHeight = (int )(0.8 * height);

  _rovMainWindow = new RovMainWindow("TMACS", width, height, 
				     viewPortWidth, viewPortHeight);

  setIconPixmap(_rovMainWindow->baseWidget());
  
  return _rovMainWindow;
}


int TiburonApp::defaultInputs(char *errorBuf)
{
  char *ptr;
  if ((ptr = getenv(rovEnvVar)) == NULL)
  {
    sprintf(errorBuf, "Environment variable \"%s\" not set\n", rovEnvVar);
    return -1;
  }

  sprintf(_systemAlarmFile, "%s/%s", ptr, defaultAlarmDefFile);
  sprintf(_vehicleConfigFile, "%s/%s", ptr, defaultConfigFile);
  sprintf(_preferencesFile, "%s/%s", ptr, defaultPreferencesFile);
  return 0;
}


void TiburonApp::getOptions(int argc, char **argv)
{
  MBool error = FALSE;
  _debug = FALSE;
  
  for (int i = 1; i < argc; i++)
  {
    if (!strcmp(argv[i], "-alarms") && i < argc - 1)
    {
      strcpy(_systemAlarmFile, argv[++i]);
    }
    else if (!strcmp(argv[i], "-config") && i < argc - 1)
    {
      strcpy(_vehicleConfigFile, argv[++i]);
    }
    else if (!strcmp(argv[i], "-pref") && i < argc - 1)
    {
      strcpy(_preferencesFile, argv[++i]);
    }
    else if (!strcmp(argv[i], "-platform") && i < argc - 1) {
      setPlatformName(argv[++i]);
    }
    else if (!strcmp(argv[i], "-debug")) {
      _debug = TRUE;
    }
    else
    {
      fprintf(stderr, "\"%s\" - invalid or incomplete option\n", argv[i]);
      error = TRUE;
    }
  }

  if (error)
  {
    fprintf(stderr, 
	    "usage: %s "
	    "[-config file][-alarms file][-pref file][-platform name]"
	    "[-debug]\n",
	    argv[0]);

    this->exit(1);
  }
}


void TiburonApp::systemAlarmFile(char *filename)
{
  strcpy(filename, _systemAlarmFile);
  return;
}


void TiburonApp::vehicleConfigFile(char *filename)
{
  strcpy(filename, _vehicleConfigFile);
  return;
}


void TiburonApp::setIconPixmap(Widget shell)
{
  // Set icon pixmap
  if (_iconPixmap)
  {  
    XtVaSetValues(shell,
		  XmNiconPixmap, _iconPixmap,
		  NULL);
  }
}


void TiburonApp::showAlarm(Alarm *alarm)
{
  AlarmSetupWindow *window = AlarmSetupWindow::instance();
  
  window->open();
  window->show();
  window->raise();
  window->alarmEditor->selectItem(alarm->name());
}


void TiburonApp::setPlatformName(const char *platformName)
{
  free((void *)_platformName);
  _platformName = strdup(platformName);
}


const char *TiburonApp::platformName()
{
  return _platformName;
}

