/* 
 * Thomas Pantzer: khep_main.cc, v0.1b  23.9.97 22:29 
 * Thomas Pantzer: khep_main.cc, v0.4.7  8.6.99 16:32 
 * 
 * 
 * Copyright 1997 by Thomas Pantzer
 * 
 * Permission to use, copy, modify, and distribute this software for noncommercial 
 * use and without fee is hereby granted, provided that the above copyright notice 
 * appear in all copies and that both that copyright notice and this permission 
 * notice appear in supporting documentation, and that the name of the Author not be 
 * used in advertising or publicity pertaining to distribution of the software 
 * without specific, written prior permission. The Author makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is" 
 * without expressed or implied warranty. 
 * 
 * 
 * pantec@aix520.informatik.uni-leipzig.de   (Thomas Pantzer) 
 * 
 */
 

#include "forms.h"
#include "fft.h"


#define MAIN_G
#include "copyright.h"

#include "new_panel.h"
#include "slBounds.h"

#include "stuff.h"
#include "graphix.h"
// #include "param.h"
#include "timer.h"
#include "user.h"
#include "netutils.h"
#include "mkopts.h"



#ifndef KCONTROL_PORT
#define KCONTROL_PORT 	55555
#endif

#ifndef KCONTROL_HOST
#define KCONTROL_HOST 	"localhost\0"
#endif

#ifndef KCONTROL_SERVICE
#define KCONTROL_SERVICE 	"khepsimd\0"
#endif


#if (SunOS || HPUX || AIX)

#ifndef KCONTROL_DEVICE
#define KCONTROL_DEVICE "/dev/tty"
#endif

#else	// not SunOS nor HPUX nor AIX

#ifdef Linux

#ifndef KCONTROL_DEVICE
#define KCONTROL_DEVICE "/dev/ttyS"
#endif

#else	// not Linux nor SunOS nor HPUX	nor AIX

	#error default serial device not defined

#endif	// Linux
#endif	// SunOS 


#ifndef DEFAULT_VISUAL_ARG
#define DEFAULT_VISUAL_ARG	3
#endif


int socket_mode = 0;

void clientflag_handler(int i, float f, char *s)
{
	socket_mode = 1;
}


t_opts opts[] = {
	{clientflag_handler,e_integer,'p',"port","use port number\t\t\t",false,KCONTROL_PORT,0.0,"\0"},
#define PORT_NUM opts[0].i
	{clientflag_handler,e_string ,'h',"host","connect to this host\t\t",false,0,0.0,KCONTROL_HOST"\0                                                                                                                           \0"},
#define HOST_NAME opts[1].s
	{empty_func,e_string ,'d',"device","use named serial device\t\t",false,0,0.0,KCONTROL_DEVICE"\0                                                                                                                               \0"},
#define DEVICE_NAME opts[2].s
	{empty_func,e_integer,'v',"visual","define visual for 8bit depth diplays",false,DEFAULT_VISUAL_ARG,0.0,"\0"},
#define VISUAL_ARG opts[3].i
	{empty_func,e_integer,'i',"index","serial device index number\t",false,1,0.0,"\0                                                                                                                                        \0"},
#define D_IDX opts[4].i


// 	{empty_func,e_string,'x',"xorkey","character for first xor operation",false,0,0.0,"AA\0\0\0\0\0\0"},
// #define XOR_STARTKEY opts[5].s[0]
// 	{empty_func,e_boolean,'d',"debug","log to foreground and do not fork",true,0,0.0,"\0"},
// #define DEBUG_MODE opts[6].b
// 	{empty_func,e_string,'o',"options","alternate configuration file\t",false,0,0.0,"\0                                                                                                                                        \0"},
// #define OPTIONS_FILE opts[7].s
// 	{empty_func,e_string,'l',"logfile","log file name\t\t\t",false,0,0.0,LOGFILE_PATH"\0                                                                                                                                        \0"},
// #define LOG_FILE opts[9].s

	{empty_func,e_unknown,'\0',"\0","\0",false,0,0.0,"\0"}
		};







void DoneKhepera(void)
{
//	Callbacks.Done();
//  int i;
//  for(i=0; i<used_robots; i++)
	user_cb_done();	
	Robot_.Done();

//  VScr.Done();
}


int main(int argc, char *argv[])
{
	PORT_NUM = GetTCPPort(KCONTROL_SERVICE,KCONTROL_PORT);
	scan_args(argc,argv,opts);

	fl_initialize(&argc, argv, 0, 0, 0);
	fd_panel = create_form_panel();
	fd_slBounds_Dialog = create_form_slBounds_Dialog();
   
	/* fill-in form initialization code */
//	fl_set_idle_delta(0);

	VScr.Init(VISUAL_ARG);
	VScr.Show("kcontrol canvas");	

	fd_panel->canvas = VScr.fd_xfbgi->canvas;
	
   fl_add_canvas_handler(fd_panel->canvas, Expose, xfgc_canvas_expose, VScr.fd_xfbgi);
   fl_add_canvas_handler(fd_panel->canvas, ButtonPress, canvas_expose, VScr.fd_xfbgi);
   fl_add_canvas_handler(fd_panel->canvas, KeyRelease, canvas_expose, VScr.fd_xfbgi);

//   fl_add_canvas_handler(fd_xfbgi->canvas, ButtonPress, xfgc_canvas_event, fd_xfbgi);
   fl_add_canvas_handler(fd_panel->canvas, ButtonRelease, xfgc_canvas_event, VScr.fd_xfbgi);
   fl_add_canvas_handler(fd_panel->canvas, KeyPress, xfgc_canvas_event, VScr.fd_xfbgi);
//   fl_add_canvas_handler(fd_xfbgi->canvas, KeyRelease, xfgc_canvas_event, fd_xfbgi);
   


	char st[256];

        if (socket_mode == 1)
           sprintf(st,"%s %d",HOST_NAME,PORT_NUM);
         else  
#ifdef Linux
           sprintf(st,"%s%d",DEVICE_NAME,D_IDX);
#else
#if (SunOS || HPUX || AIX)
           sprintf(st,"%s%c",DEVICE_NAME,97+D_IDX);
#else
	#error no serial device naming convention known for your system
#endif
#endif
	Robot_.Init(st);
	user_cb_init();

	for (int k=0; k<Callbacks.number; k++)
          {
	   if ((Callbacks.RunObj[k]->props & _p_control) > 0)
	      {
	       char st[256];
	       sprintf(st,"%s%%x%d",Callbacks.RunObj[k]->Name,k+1);	  	
#ifdef DEBUG
	       fprintf(stderr,"making %d. callback(%s) menu-entry\n",k+1,st);
#endif
	       fl_addto_choice(fd_panel->control_ch,st);
	      }
          }	

//   InitializeFFT(64);
//   	InitTimer();

	fprintf(stderr,"show the first form\n");
	fl_show_form(fd_panel->panel,FL_PLACE_POSITION,FL_FULLBORDER,"Khepera Control");
//   fl_show_form(fd_param_fm->param_fm,FL_PLACE_CENTER,FL_FULLBORDER,"param form");

	VScr.Hide();

	fl_set_idle_callback(idle_cb,0);

	while (fl_do_forms() != fd_panel->exit_bt);
      
	DoneKhepera(); 
           
	return 0;
}







/**/
/**/
/**/
/**/
/**/
/**/
/**/
/**/
/*****  End Of File  ****************************/
