/* 
    XTVscreen for Bt848 frame grabber driver

    Copyright (C) 1996,97 Marcus Metzler (mocm@thp.uni-koeln.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "xtvscreen.h"
#include "TVscreen.h"
#include "channel.h"
#include "callbacks.h"
#include "allwidgets.h"
#include "visual.h"
#include "sound.h"
#include "snap.h"

/*Global Variables */
channel head;
int channel::cnt=0;

Widget ChanW,dialog,conwid,pane,rowcol;
Widget list,freq,chan,fine,text;
Widget toplevel,screen,OptW,OptF,seqs;
Widget input1[4];
Widget input2[4];
Widget norm[3];
Widget Message;
Widget Message2;
Visual *visual;
AppData app_data;

XtAppContext app_context;


/*                      */
/* Command Line Options */
/*                      */
static XrmOptionDescRec options[] = {
  {"-norm",  "*norm" , XrmoptionSepArg,NULL},
  {"-in", "*inpsel", XrmoptionSepArg,NULL},
  {"-bpp", "*bpp", XrmoptionSepArg,NULL},
  {"-dev",    "*devname",        XrmoptionSepArg,NULL},
  {"-v",    "*visname",        XrmoptionSepArg,NULL},
  {"-rw", "*realw", XrmoptionSepArg,NULL},
  {"-asp", "*aspect", XrmoptionSepArg,NULL},
  {"-capon","*capture",XrmoptionNoArg,(XPointer)"true"},
  {"-disableSaver","*disableSaver",XrmoptionNoArg,(XPointer)"true"},
  {"-dontDisableSaver","*disableSaver",XrmoptionNoArg,(XPointer)"false"},
  {"-country",    "*countryFile",        XrmoptionSepArg,NULL},
  {"-full","*fullScreen",XrmoptionNoArg,(XPointer)"true"},
  {"-snap",    "*snapName",  XrmoptionSepArg,NULL},
  {"-seq",    "*sequName",  XrmoptionSepArg,NULL},
  {"-sn",    "*sequNum",  XrmoptionSepArg,NULL},
};

/*                         */
/* Set Channels for Europe */
/*                         */
void set_country()
{
  switch(DCOUNTRY){
  case EUROPE:
    CountryList.init(8,"Europe");
    CountryList.iv[0].set(48.25,    66.25,   2,   4,  7.0);   
    CountryList.iv[1].set(112.25,  172.25,  72,  80,  7.0);  
    CountryList.iv[2].set(175.25,  228.25,   5,  12,  7.0);
    CountryList.iv[3].set(231.25,  298.25,  81,  90,  7.0);
    CountryList.iv[4].set(303.25,  451.25,  91, 109,  8.0);
    CountryList.iv[5].set(455.25,  471.25, 110, 112,  8.0); 
    CountryList.iv[6].set(471.25,  859.25,  21,  69,  8.0); 
    CountryList.iv[7].set(859.25, 1175.25, 161, 200,  8.0);
    break;
    
  case USA:
    CountryList.init(7,"USA");
    CountryList.iv[0].set( 55.25,   73.25,   2,   4, 6.0);
    CountryList.iv[1].set( 77.25,   87.25,   5,   6, 6.0);
    CountryList.iv[2].set(175.25,  215.25,   7,  13, 6.0);
    CountryList.iv[3].set(471.25,  805.25,  14,  69, 6.0);
    CountryList.iv[4].set(121.25,  173.25, 114, 122, 6.0);
    CountryList.iv[5].set(217.25,  299.25, 123, 136, 6.0);
    CountryList.iv[6].set(301.25, 1031.25, 137, 258, 6.0);
    break;
  }
  
}

/*                               */
/* switch channel  via keyboard  */
/*                               */
void Channel(Widget w,XEvent *ev, String *params, Cardinal *num_params)
{
  if (*num_params != 1)
    return;
  if (0 == strcasecmp(params[0],"up"))
      chan_ud(NULL,(XtPointer)CHANUP,NULL);
  else if (0 == strcasecmp(params[0],"down"))
      chan_ud(NULL,(XtPointer)CHANDOWN,NULL);
}


/*                      */
/* restore screensaver  */
/*                      */
int timeout,interval,prefer_blanking,allow_exposures;
void restoresaver(Widget,XtPointer,XtPointer)
{
  int t,i,p,a;
  XGetScreenSaver(XtDisplay(toplevel),
		  &t,&i,&p,&a);
  if (t == 0 && i == interval &&
      p == prefer_blanking && a == allow_exposures)
    XSetScreenSaver(XtDisplay(toplevel),
		    timeout,interval,prefer_blanking,allow_exposures);
}


/*                 */
/* Create toplevel */
/*                 */
void SetTop(XtAppContext &app_context,char *name,int argc,char **argv)
{
  XtToolkitInitialize();
  app_context = XtCreateApplicationContext();
  XtAppAddActions(app_context,actionTable,XtNumber(actionTable));
  XtAppSetFallbackResources(app_context,fallback_resources);
  Display *dpy = XtOpenDisplay(app_context,NULL,NULL,name,
			       options,XtNumber(options),&argc,argv);
  
  toplevel = XtVaAppCreateShell(NULL,name,applicationShellWidgetClass,dpy
				,XmNminWidth,    12
				,XmNminHeight ,  12
				,XmNmaxWidth,    960
				,XmNmaxHeight ,  680
				,XmNwidthInc,  4
				,XmNheightInc, 4
				,XmNinput,(XtArgVal)True
				,NULL);
  
  XtVaGetApplicationResources(toplevel,&app_data,resources,
			      XtNumber(resources),
			      NULL);
  if (app_data.dasp == NOASP) app_data.dasp=A4_3;
  if (app_data.disable_saver)
    {
      XGetScreenSaver(XtDisplay(toplevel),
		      &timeout,&interval,&prefer_blanking,&allow_exposures);
      XSetScreenSaver(XtDisplay(toplevel),
		      0,interval,prefer_blanking,allow_exposures);
      XtAddCallback(toplevel,XtNdestroyCallback,restoresaver,NULL);  
    }
  
  if (app_data.countryFile) CountryList.init(app_data.countryFile);
  else set_country();

  if (!app_data.snapname) app_data.snapname="pic";
  if (!app_data.sequname) app_data.sequname="seq";
  if (!app_data.seqnum) app_data.seqnum=DEFSEQ;


  // syntax check
  if (argc > 1) {
    cerr << "usage: "<< argv[0] <<" [-options]"<< endl<<help_message << endl;
    exit(1);
  }

  sound_init();
#ifdef LesstifVersion
  cerr << "Using Lesstif" << endl;
#endif
}


/*      */
/* MAIN */
/*      */
void main(int argc, char **argv)
{
  Widget win;
  Widget cw[2];

  SetTop(app_context,"XTvscreen",argc,argv);
  MainWin(win,"Window");
  XtRealizeWidget(toplevel);

  cw[0]=screen;
  cw[1]=toplevel;
  XtSetWMColormapWindows(toplevel,cw,2);
  XtSetWMColormapWindows(screen,cw,2);

  XtAppMainLoop(app_context);
}
