/*****************************************************************************/

/*
 *      linuxremote.c -- REALmagic remote control client linux specific functions
 *
 *      Copyright (C) 1999-2000 Sigma Designs
 *                   written by Emmanuel Michon <emmanuel_michon@sdesigns.com>
 *
 *      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 "remote.h"

char *SerialPortDevice;

//------------------------------------------------------------------------//
// OSOpenPort - This function Opens and intializes the COM1/2 serial port.//
//------------------------------------------------------------------------//

struct termios oldtermios;

int ttyraw(int fd)
{
        struct termios newtermios;

        if (tcgetattr(fd,&oldtermios)<0) return -1;
	newtermios=oldtermios;

	// all the basic magic for raw mode
	cfmakeraw(&newtermios);

	// input speed setting
	if (cfsetispeed(&newtermios,B1200)<0) return -1;

	// receive byte per byte
	newtermios.c_cc[VMIN] = 1;
        newtermios.c_cc[VTIME] = 0;
	
        /* You tell me why TCSAFLUSH. */
        if(tcsetattr(fd, TCSAFLUSH, &newtermios) < 0) return -1;
	
        return 0;
}

int ttyreset(int fd)
{
        if(tcsetattr(fd, TCSAFLUSH, &oldtermios) < 0) return -1;

        return(0);
}

PORTHANDLE OSOpenPort(const char *name)
{
	int fd;

	// if no name specified use default
	if (name==NULL) name=SerialPortDevice;

	// 0644 is -rw-r--r--
	// NOCTTY because TTY is raw
	fd = open(name,O_RDWR|O_NOCTTY,0644);
	if (fd==-1) {
		perror("Could not open serial port");
		exit(1);
	}

	if (ttyraw(fd)!=0) {
		perror("Cannot set raw mode");
		exit(1);
	}
	
	return (PORTHANDLE)fd;
}

BOOL OSClosePort(PORTHANDLE hPort)
{
	ttyreset((int)hPort);

	return (close((int)hPort)!=-1);
}

//-----------------------------------------------------------------------//
// OSReadPort - Reads 1 character from the Com Port			 //
//-----------------------------------------------------------------------//

int OSReadPort(PORTHANDLE fd)
{
	unsigned char buf;
	
	while (read(fd,&buf,1)==-1) {
		// signal reception cause harmless read failures
		if (errno!=EINTR) {
			perror("Could not read");
			exit(1);
		}
	}

	return (int)buf;   
}

//---------------------------------------------------------//
// OSPurge - When a valid key is found, purge the com port //
//---------------------------------------------------------//

void OSPurge(PORTHANDLE fd)
{

#define MINREPEAT 300000

	// FIXME tcflush should be protected against EINTR too!

	usleep(MINREPEAT);
	if (tcflush(fd,TCIOFLUSH)==-1) {
		perror("Could not flush");
		exit(1);
	}
}

int command_line_switch_YUV=FALSE;
int command_line_switch_NTSC=FALSE;
int command_line_switch_PAL=FALSE;
int command_line_switch_RGB=FALSE;

void parsecommandline(int argc,char **argv)
{
	struct option long_options[] = {
		{"help",                no_argument,       NULL, 'h'},
		{"yuv",                 no_argument,       NULL, 'y'},
		{"ntsc",                no_argument,       NULL, 'n'},
		{"pal",                 no_argument,       NULL, 'p'},
		{"rgb",                 no_argument,       NULL, 'r'},
		{0, 0, 0, 0}
	};

	char *short_options = "hynpr";
	int c,long_option_index;
	
	while (1) {
		c=getopt_long(argc,
			      argv,
			      short_options,
			      long_options,
			      &long_option_index);
		if (c==-1) break;
		
		switch (c) {
		case 'h':
			printf(
			       "Valid options are:\n"
			       " -y --yuv : YUV output\n"
			       " -n --ntsc: NTSC output\n"
			       " -p --pal : PAL output\n"
			       " -r --rgb : RGB output\n"
			       );
			_exit(0);
			break;
		case 'y':
			command_line_switch_YUV=TRUE;
			command_line_switch_RGB=FALSE;
			break;
		case 'n':
			command_line_switch_NTSC=TRUE;
			command_line_switch_PAL=FALSE;
			break;
		case 'p':
			command_line_switch_PAL=TRUE;
			command_line_switch_NTSC=FALSE;
			break;
		case 'r':
			command_line_switch_RGB=TRUE;
			command_line_switch_YUV=FALSE;
			break;
		default:
			break;
		}
	}
}

void OSSetPortHandle(PORTHANDLE p)
{
	// do nothing.
}

//---------------------------------------------------------//
// MAIN FUNCTION - OS DEPENDENT - calls OSMain in remote.c //
//---------------------------------------------------------//

int main(int argc,char **argv)
{
	char defaultSerialPortDevice[]="/dev/ttyS0";

	SerialPortDevice=getenv("REMOTECONTROLPORT");
	if (SerialPortDevice==NULL) 
		SerialPortDevice=defaultSerialPortDevice;
	
	printf("Using serial port %s\n",SerialPortDevice);
	
	parsecommandline(argc,argv);

	// We have to pass the driver number here,
	// but it is unused under Linux
	OSMain(0);

	return 0;
}

void OSCloseWindow()
{
	return ;
}

void OSrunning() 
{
	return ;
}
