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

/*
 *      remote.c -- REALmagic remote control client os independent functions
 *
 *      Copyright (C) 1999-2000 Sigma Designs
 *                   written by Julien Veyssiere
 *
 *      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"

#define SCREEN_WIDTH	640
#define SCREEN_HEIGHT	480

//----------------------------------------------------------------------------//
// Event_Notifications - This function receives events from the graph manager //
//----------------------------------------------------------------------------//
DWORD Event_Notifications (DWORD dwContext, DWORD dwMsg, DWORD dwValue)
{
	switch (dwMsg)
	{
	case FMPM_ERROR:	
		break;

	case FMPM_STARVATION:
		break;

	case FMPM_EOS:
		break;

	case FMPM_DVD_ANGLE_CHANGE:
		printf("Event: DVD Angle Change\n");
		break;

	case FMPM_DVD_ANGLES_BLOCK:
		printf("Event: DVD Angles Block Change\n");
		break;

	case FMPM_DVD_AUDIO_STREAM_CHANGE:
		printf("Event: DVD Audio Stream Change\n");
		break;
	case FMPM_DVD_BUTTONS_CHANGE:
		printf("Event: DVD Buttons Change\n");
		break;

	case FMPM_DVD_BUTTON_CHANGE:
		printf("Event: DVD Button Change\n");
		break;

	case FMPM_DVD_PROGRAM_START:
		printf("Event: DVD Program Start\n");
		break;

	case FMPM_DVD_CURRENT_TIME:
		printf("Event: DVD Current Time %2x:%2x:%2x:%x\r\n",
				 ((unsigned char *)(&dwValue))[0],
				 ((unsigned char *)(&dwValue))[1],
				 ((unsigned char *)(&dwValue))[2],
				 ((unsigned char *)(&dwValue))[3]);
		break;

	case FMPM_DVD_DOMAIN_CHANGE:
		printf("Event: DVD Domain Change\n");
		break;

	case FMPM_DVD_NO_FP_PGC:
		printf("Event: DVD No FP PGC\n");
		break;

	case FMPM_DVD_PARENTAL_LEVEL_CHANGE:
		printf("Event: DVD Parental Level Change\n");
		break;

	case FMPM_DVD_PLAYBACK_STOPPED:
		printf("Event: DVD Playback Stopped\n");
		break;

	case FMPM_DVD_STILL_OFF:
		printf("Event: DVD Still Off\n");
		break;

	case FMPM_DVD_STILL_ON:
		printf("Event: DVD Still On\n");
		break;

	case FMPM_DVD_SUBPICTURE_STREAM_CHANGE:
		printf("Event: DVD Subpic Stream Change\n");
		break;

	case FMPM_DVD_TITLE_CHANGE:
		printf("Event: DVD Title Change\n");
		break;

	case FMPM_DVD_VALID_UOPS_CHANGE:
		printf("Event: DVD VALID UOPS Change\n");
		break;

	default:
		break;
	}
	return 0;
}
									
//-------------------------------------------------------------------------//
// GetDigitEntry - This function bufferizes digit input						 //
//-------------------------------------------------------------------------//
void GetDigitEntry(int digit, PORTHANDLE hPort)
{
	int key;
	int num_digits = 1;
	int newdigit[5];
	int buffer, i, tens;
	BYTE figure = 0;

	newdigit[0] = digit;
	
	while( num_digits < 5)
	{
		
		OSPurge(hPort);
		key = GetSymbol(hPort);
		
		if (key == 33)
		{
			tens = 1;
			for (i=1; i<=num_digits; i++)
			{
				figure += tens * newdigit[num_digits - i];
				tens *=10;
		
			}
			NumericSelections(figure);
			break;
		}

		else if ((buffer = ReadDigit(key)) != -1)
		{
			newdigit[num_digits] = buffer;
			num_digits++;
		}
	}
}


//-------------------------------------------------------------------------//
// ReadDigit - This function identifies input digits and returns the digit //
//-------------------------------------------------------------------------//
int ReadDigit(int entry)
{
	int value;
	
	switch(entry)
	{
	case 163:			// 1
		value = 1;
		break;
	case 164:			// 2
		value = 2;
		break;
	case 172:			// 3
		value = 3;
		break;
	case 167:			// 4
		value = 4;
		break;
	case 168:			// 5
		value = 5;
		break;
	case 165:			// 6
		value = 6;
		break;
	case 13:			// 7
		value = 7;
		break;
	case 17:			// 8
		value = 8;
		break;
	case 169:			// 9
		value = 9;
		break;
	case 25:			// 0
		value = 0;
		break;
	default:
		value = -1;
		break;
	}
	return value;
}

//-------------------------------------------------------------------------------------//
// ReadLibrary - This function is a Library of behaviours attached to the symbols read //
//-------------------------------------------------------------------------------------//
INFO ReadLibrary(INFO InfoBuffer)
{
	InfoBuffer.test = TRUE;	// value returned by the function
	
	switch(InfoBuffer.symbol)			// identify symbol
	{
	case 13:		// 7
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(7, InfoBuffer.hPort);
		printf("7\n");
		break;
	case 17:		// 8
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(8, InfoBuffer.hPort);
		printf("8\n");
		break;
	case 21:
		// VGA/TV
		if ( (InfoBuffer.bVideoOut & FMPV_VIDEOOUT_MODE_MASK) == FMPV_VIDEOOUT_TV )
		{
		        InfoBuffer.bVideoOut = FMPV_VIDEOOUT_VGA;
			FMPSet( FMPI_VIDEOOUT, InfoBuffer.bVideoOut );
		}
		else
		{
			InfoBuffer.bVideoOut = FMPV_VIDEOOUT_TV;
			FMPSet( FMPI_VIDEOOUT, InfoBuffer.bVideoOut );
		}
		break;
	case 25:		// 0
		FMPSetDestination (0, 0, 100, 100);
		printf("0\n");
		break;
	case 33:		
		printf("ENT\n");	
		break;
	case 37:		// L-R
		printf("L-R\n");
		InfoBuffer.bPosAction++;
		InfoBuffer.bPosAction%=POS_MAX;
		break;
	case 141:		// Computer: FMPClose()
		FMPEnableOverlay (0);
		FMPStop();
		FMPClose();

		#ifdef _PLAYWARE_
			OSCloseWindow();
		#endif

		InfoBuffer.playing = CLOSED;
		break;
	case 142:		// Player : FMPOpen()
		StartPlaying( &InfoBuffer );

		break;
	case 143:		// UP
		printf("UP\n");
		if (InfoBuffer.bPosAction == POS_MOVE)
		{
			if (InfoBuffer.dwY > InfoBuffer.bStep)
			{
				InfoBuffer.dwY -= InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		if (InfoBuffer.bPosAction == POS_RESIZE)
		{
			if (InfoBuffer.dwcY > InfoBuffer.bStep)
			{
				InfoBuffer.dwcY -= InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		{
			if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
				UpperButtonSelect ();
		}
		break;
	case 145:		// Left
		printf("LEFT\n");
		if (InfoBuffer.bPosAction == POS_MOVE)
		{
			if (InfoBuffer.dwX > InfoBuffer.bStep)
			{
				InfoBuffer.dwX -= InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		if (InfoBuffer.bPosAction == POS_RESIZE)
		{
			if (InfoBuffer.dwcX > InfoBuffer.bStep)
			{
				InfoBuffer.dwcX -= InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		{
			if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
				LeftButtonSelect ();
		}
		break;
	case 146:		// Sel
		printf("SEL\n");
		if (InfoBuffer.bPosAction == POS_NO_ACTION)
		{
			if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
				ButtonActivate ();
		}
		else
		{
			switch (InfoBuffer.bStep)
			{
			case 1:
				InfoBuffer.bStep = 5;
				break;
			case 5:
				InfoBuffer.bStep = 10;
				break;
			case 10:
				InfoBuffer.bStep = 1;
				break;
			default:
				InfoBuffer.bStep = 10;
				break;
			}
		}
		break;
	case 147:		// Down
		printf("DOWN\n");
		if (InfoBuffer.bPosAction == POS_MOVE)
		{
			if (InfoBuffer.dwY + InfoBuffer.dwcY + InfoBuffer.bStep <=  SCREEN_HEIGHT)
			{
				InfoBuffer.dwY += InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		if (InfoBuffer.bPosAction == POS_RESIZE)
		{
			if (InfoBuffer.dwY + InfoBuffer.dwcY + InfoBuffer.bStep <=  SCREEN_HEIGHT)
			{
				InfoBuffer.dwcY += InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		{
			if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
				LowerButtonSelect ();
		}
		break;
	case 148:		// Menu: ReturnPGSearch
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			ReturnPGSearch();
		if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
			MenuCall (3);	// 3 for root menu
		break;
	case 149:		// Title
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			DefaultPGSearch();
		printf("TITLE\n");
		break;
	case 150:		// OSD
		printf("OSD\n");	
		FMPShowBitmap (100, 127, 495, 380, 145 | (580 << 16), 100 | (400 << 16));
		break;
	case 151:		// Language
		if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
		{
			printf("DVD LANGUAGE %d\n", InfoBuffer.bAudioStream);
			AudioStreamChange (InfoBuffer.bAudioStream++);
			if (InfoBuffer.bAudioStream > InfoBuffer.bMaxAudioStream)
				InfoBuffer.bAudioStream = 0;
		}
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
		{
			AudioStreamChange(0);		
			printf("VCD LANGUAGE\n");
		}
		break;
	case 152:		// Angle
		if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
		{
			if(!InfoBuffer.bAngleNumber) 
			{
				InfoBuffer.bAngleNumber = (BYTE) FMPGet(FMPI_CURRENT_ANGLE) + 1;
				if (InfoBuffer.bAngleNumber > (BYTE) FMPGet(FMPI_ANGLES_AVAILABLE))
					InfoBuffer.bAngleNumber = 1;
			}
			printf("DVD ANGLE %d\n", InfoBuffer.bAngleNumber);
			AngleChange(InfoBuffer.bAngleNumber);
			InfoBuffer.bAngleNumber = 0;
		}		
		break;
	case 153:		// Sub-title
		if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
		{
			if (InfoBuffer.bSPStream > InfoBuffer.bMaxSPStream)
			{
				InfoBuffer.bSPStream = 0;
				printf("DVD SUB-TITLE off\n");
				SubPictureStreamChange (InfoBuffer.bSPStream, 0);				
			}
			else
			{
				printf("DVD SUB-TITLE %d\n", InfoBuffer.bSPStream);
				SubPictureStreamChange (InfoBuffer.bSPStream++, 1);
			}
		}		
		break;
	case 154:		// Play/Pause: if playing: FMPPause(), if not playing, FMPPlay()
		if ((InfoBuffer.playing == STOPPED)||(InfoBuffer.playing == PAUSED))
		{
			printf("PLAY\n");
			FMPPlay();
			InfoBuffer.playing = PLAYING;
		}	
		else if (InfoBuffer.playing == TRICK_MODE)
		{
			printf("Resume from trick mode\n");
			StopForResume ();
			Resume ();
			InfoBuffer.playing = PLAYING;			
		}
		else if (InfoBuffer.playing == STOP_FOR_RESUME)
		{
			printf("Resume\n");
			if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
				Resume();
			if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
				Resume();
			InfoBuffer.playing = PLAYING;
		}
		else
		{
			printf("PAUSE\n");
				FMPPause();
			InfoBuffer.playing = PAUSED;
		}
		break;
	case 155:		// Stop: FMPStop()
		if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
				StopForResume();
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
				StopForResume();
		InfoBuffer.playing = STOP_FOR_RESUME;
		break;
	case 156:		// Step play
		{
			int key, digit;
			int power = 0, i;
			int time[4];
			ULONG time_return;

			if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			{
				printf("Time Search: ");
				OSPurge(InfoBuffer.hPort);
				key = GetSymbol(InfoBuffer.hPort);	// get first digit
				while (power <= 4)	// while not cancelled or finished
				{
					if ( (key == 158) && (power > 0))	// if rewind pressed .. delete last digit entered
					{	
						power--;
						printf("\n");
						for (i = 0; i < power; i++)
							printf("%d", time[i]);
						
					}
					else if ((key == 33)&&(power < 4))
						break;					
					else if ((key == 33)&&(power == 4))
					{
						time_return = 1000 * time[0] + 100 * time[1] + 10 * time[2] + time[3];
						break;
					}
					else if (power < 4)
					{
						digit = ReadDigit(key);		// get the digit
						if (digit != -1 && (((power != 0)&&(power != 2)) || digit < 6))
							// if digit correct
						{
							printf("%d", digit);
							time[power] = digit;
							power++;
						}
					}	
					
					if ((power <= 4)	&& (key != 33))			// if not four digits are entered
					{	
						OSPurge(InfoBuffer.hPort);
						key = GetSymbol(InfoBuffer.hPort);
					}
				}
			}		
			printf("\n");
		}
		break;
	case 157:		// Eject

		if( FMPIsDiscLoaded() == TRUE )
		{
			FMPStop();
			FMPTrayOperation( FMPT_TRAYOPEN );
			FMPClose();
			InfoBuffer.playing = CLOSED;
		}
		else
		{
			FMPTrayOperation( FMPT_TRAYCLOSE );
		}
		
		break;
	case 158:		// Rewind
		if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
		{
			printf("DVD REVERSE: %dx\n", InfoBuffer.wBSpeed);
			BackwardScan (InfoBuffer.wBSpeed, 1);
			InfoBuffer.wBSpeed += 2;
			if (InfoBuffer.wBSpeed > 8)
				InfoBuffer.wBSpeed = 0;
		}
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
		{
			BYTE	speed;

			printf("VCD REWIND\n");

			speed = (BYTE)FMPGet(FMPI_SVCD_TRICK_MODE);
			if ((speed >0) && (speed < 4))
				speed = 4;
			else
			{
				speed = speed? speed-3 : 0;
				if (++speed == 4)
					speed = 0;
			}
			Rewind(speed);
			InfoBuffer.playing = TRICK_MODE;
		}
		
		break;
	case 159:		// Fast Forward
		if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
		{
			printf("DVD FAST FORWARD: %dx\n", InfoBuffer.wFSpeed);
			ForwardScan (InfoBuffer.wFSpeed, 1);
			InfoBuffer.wFSpeed += 2;
			if (InfoBuffer.wFSpeed > 8)
				InfoBuffer.wFSpeed = 0;
		}
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
		{
			BYTE	speed;

			printf("VCD FAST FORWARD\n");

			speed = (BYTE)FMPGet(FMPI_SVCD_TRICK_MODE);
			if (speed>3)
				speed = 4;
			else if (++speed == 4)
				speed = 0;
			FastForward(speed);			
			InfoBuffer.playing = TRICK_MODE;
		}
		break;
	case 160:		// Last track: PrevPGSearch()
		switch( InfoBuffer.bDiscType )
		{
			case FMPC_DVD_VIDEO :
				PrevPGSearch();	
				break;
			case FMPC_VCD :
			case FMPC_SVCD :
				PrevPGSearch();
				break;
			case FMPC_CDDA :
				CDDAPrevTrack();
				break;
		}
		break;
	case 161:		// Next track: NextPGSearch()
		switch( InfoBuffer.bDiscType )
		{
			case FMPC_DVD_VIDEO :
				NextPGSearch();	
				break;
			case FMPC_VCD :
			case FMPC_SVCD :
				NextPGSearch();
				break;
			case FMPC_CDDA :
				CDDANextTrack();
				break;
		}
		break;
	case 162:		// Vol+
		{
			DWORD l, r;			
			l = FMPGet (FMPI_LEFT_VOLUME);
			r = FMPGet (FMPI_RIGHT_VOLUME);
			l++;
			r++;
			FMPSet (FMPI_LEFT_VOLUME, l);
			FMPSet (FMPI_RIGHT_VOLUME, r);
			l = FMPGet (FMPI_LEFT_VOLUME);
			r = FMPGet (FMPI_RIGHT_VOLUME);
			printf("VOL+ (%ld, %ld)\n", r, l);

		}
		break;
	case 163:		// 1: VCDNumericSelection()
/*		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(1, InfoBuffer.hPort);
*/
		{
			unsigned long value;

			value = FMPGet(FMPI_CONTRAST);//,1000-(unsigned int)adj->value);
			value = value + 50;
			FMPSet(FMPI_CONTRAST,value);
		}
		
		break;
	case 164:		// 2
/*		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(2, InfoBuffer.hPort);
		printf("2\n");
*/
		{
			unsigned long value;
			value = FMPGet(FMPI_BRIGHTNESS);//,1000-(unsigned int)adj->value);
			value = value + 50;
			FMPSet(FMPI_BRIGHTNESS,value);
		}

		break;
	case 165:		// 6
/*		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(6, InfoBuffer.hPort);
		printf("6\n");
*/
		
		{	
			unsigned long value;

			value = FMPGet(FMPI_SATURATION);//,1000-(unsigned int)adj->value);
			value = value - 50;
			FMPSet(FMPI_SATURATION,value);
			}

		
		break;
	case 166:		// Vol-
		{
			DWORD l, r;			
			l = FMPGet (FMPI_LEFT_VOLUME);
			r = FMPGet (FMPI_RIGHT_VOLUME);
			l--;
			r--;
			FMPSet (FMPI_LEFT_VOLUME, l);
			FMPSet (FMPI_RIGHT_VOLUME, r);
			l = FMPGet (FMPI_LEFT_VOLUME);
			r = FMPGet (FMPI_RIGHT_VOLUME);
			printf("VOL- (%ld, %ld)\n", r, l);
		
		}		
		break;
	case 167:		// 4
/*		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(4, InfoBuffer.hPort);
		printf("4\n");
*/
				{
			unsigned long value;

			value = FMPGet(FMPI_CONTRAST);//,1000-(unsigned int)adj->value);
			value = value - 50;
			FMPSet(FMPI_CONTRAST,value);
		}

		break;
	case 168:		// 5
/*		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(5, InfoBuffer.hPort);
		printf("5\n");
*/		{
			unsigned long value;
			value = FMPGet(FMPI_BRIGHTNESS);//,1000-(unsigned int)adj->value);
			value = value - 50;
			FMPSet(FMPI_BRIGHTNESS,value);
		}

		break;
	case 169:		// 9
		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(9, InfoBuffer.hPort);
		printf("9\n");
		break;
	case 171:		// Mute		
		if (FMPGet (FMPI_MUTE))
		{
			printf("MUTE off\n");
			FMPSet (FMPI_MUTE, 0);
		}
		else
		{
			printf("MUTE on\n");
			FMPSet (FMPI_MUTE, 1);
		}

		break;
	case 172:		// 3
/*		if ((InfoBuffer.bDiscType == FMPC_VCD) || (InfoBuffer.bDiscType == FMPC_SVCD))
			GetDigitEntry(3, InfoBuffer.hPort);
		printf("3\n");
*/
			{	
			unsigned long value;

			value = FMPGet(FMPI_SATURATION);//,1000-(unsigned int)adj->value);
			value = value + 50;
			FMPSet(FMPI_SATURATION,value);
			}

		break;
	case 190:		// Right
		printf("RIGHT\n");
		if (InfoBuffer.bPosAction == POS_MOVE)
		{
			if (InfoBuffer.dwX + InfoBuffer.dwcX + InfoBuffer.bStep <=  SCREEN_WIDTH)
			{
				InfoBuffer.dwX += InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		if (InfoBuffer.bPosAction == POS_RESIZE)
		{
			if (InfoBuffer.dwX + InfoBuffer.dwcX + InfoBuffer.bStep <=  SCREEN_WIDTH)
			{
				InfoBuffer.dwcX += InfoBuffer.bStep;
				FMPSetDestination (InfoBuffer.dwX, InfoBuffer.dwY, InfoBuffer.dwcX, InfoBuffer.dwcY);
			}
		}
		else
		{
			if (InfoBuffer.bDiscType == FMPC_DVD_VIDEO)
				RightButtonSelect ();
		}
		break;
	default:
		InfoBuffer.test = FALSE;			// no character recognized... return 0.
		break;
	}

	return InfoBuffer;
}


void StartPlaying(INFO* pInfo)
{
		DWORD dwError;
		FMP_OPENSTRUCT FMPOpenStruct;
			printf("Player\n");

		if (pInfo->playing == CLOSED)
		{
			printf("Begin of FMPOpen\n");			
			FMPOpenStruct.dwStructSize		= sizeof (FMP_OPENSTRUCT);
			FMPOpenStruct.bDriverNumber		= 'F' - 'A';
#if defined(_BARBADOS_)&!defined(_PLAYWARE_)
			FMPOpenStruct.dwSize			= 32*1024;
			FMPOpenStruct.dwCount			= 8;
			FMPOpenStruct.pCallback			= NULL;
#elif defined(_PLAYWARE_)
			FMPOpenStruct.dwSize			= 32*1024;
			FMPOpenStruct.dwCount			= 8;
			FMPOpenStruct.pCallback			= NULL;
#else
			FMPOpenStruct.dwSize			= 32*1024;
			FMPOpenStruct.dwCount			= 10;
			FMPOpenStruct.pCallback			= Event_Notifications;
#endif
			FMPOpenStruct.dwContext			= 0;
			FMPOpenStruct.sFileName			= NULL;
			FMPOpenStruct.dwFlags			= /*FMPF_SVCD;*/FMPF_DVD;
			FMPOpenStruct.bDiscContent		= FMPC_UNKNOWN;
			FMPOpenStruct.bFileSystem		= 0x00;



#ifdef _ANTIGUA_
			//OutputDebugString (TEXT("Startplaying\n"));
#endif
			dwError = FMPOpenDiscPlayback (&FMPOpenStruct);
			if ( dwError != FMPE_OK )
			{
				pInfo->playing = CLOSED;
				printf("StartPlaying Failed !...");
				return;
			}

			pInfo->bDiscType = FMPOpenStruct.bDiscContent;
#ifdef _BARBADOS_
			// apply command line settings if present
			{
				DWORD oldsetting,newsetting;
				oldsetting=FMPGet(FMPI_VIDEOOUT);
				newsetting=oldsetting;
				
				if (command_line_switch_YUV) {
					newsetting=newsetting&(~FMPV_VIDEOOUT_COMPONENT_MASK);
					newsetting|=FMPV_VIDEOOUT_COMPONENT_YUV;
				}
				if (command_line_switch_RGB) {
					newsetting=newsetting&(~FMPV_VIDEOOUT_COMPONENT_MASK);
					newsetting|=FMPV_VIDEOOUT_COMPONENT_RGB;
				}
				if (command_line_switch_NTSC) {
					newsetting=newsetting&(~FMPV_VIDEOOUT_STANDARDTV_MASK);
					newsetting|=FMPV_VIDEOOUT_NTSC;
				}
				if (command_line_switch_PAL) {
					newsetting=newsetting&(~FMPV_VIDEOOUT_STANDARDTV_MASK);
					newsetting|=FMPV_VIDEOOUT_PAL;
				}
				
				if (newsetting!=oldsetting) FMPSet(FMPI_VIDEOOUT,newsetting);
			}
#endif
			
			// go full screen on TV now.
			FMPEnableOverlay (0);
			FMPEnableFullScreen	(1);
			printf("MUTE off\n");
			FMPSet (FMPI_MUTE, 0);
			
			pInfo->playing = STOPPED;
	
			printf("End of FMPOpen\n");

#ifdef _PLAYWARE_
			OSrunning();
#endif
			FMPPlay();
			

			pInfo->playing = PLAYING;
			
			printf("End of FMPPlay\n");
		}
}

//-------------------------------------------------------------------------------//
//	GetSymbol - Reads the port until a valid sequence is detected - then returns //
//   the symbol read															 //
//-------------------------------------------------------------------------------//
int GetSymbol(PORTHANDLE hPort)
{
	int test;
	int symbol, symbol_2;

	do
	{
		test = 0;
		symbol = OSReadPort(hPort);	// read one character
		if  (symbol == 131)
		{
			symbol = OSReadPort(hPort);
			symbol_2 = OSReadPort(hPort);
			if (symbol == symbol_2)
				test = 1;
		}	
	}
	while (test == 0);

	return symbol;
}

//-----------------------------------------------------------------------------//
//	MAIN FUNCTION - IDENTICAL IN QNX AND CE, EXCEPT FOR "int WINAPI WinMain..."//
//-----------------------------------------------------------------------------//
void OSMain(BYTE bDrive)
{						
	INFO Info;					

	printf ("remote application start\n");
	// Initialize driver
	MPEGDriverEntry(bDrive);

	printf ("mpeg driver initialized\n");

	// initialize the info structure
	Info.symbol	= 0;
	Info.bDiscType  = FMPC_UNKNOWN;
	Info.playing	= CLOSED;
	Info.test	= FALSE;
	Info.wBSpeed	= 2;
	Info.wFSpeed	= 2;
	// should get this info from navigator
	Info.bAngleNumber    = 0;
	Info.bMaxAngleNumber = 0;
	Info.bAudioStream    = 0;
	Info.bMaxAudioStream = 7;
	Info.bSPStream       = 0;
	Info.bMaxSPStream    = 7;
	Info.bFullScreen     = 1;
	Info.bVideoOut 	     = FMPV_VIDEOOUT_TV;
	FMPSet( FMPI_VIDEOOUT, Info.bVideoOut );

	// Position and Size of the window
	Info.dwX	= 320;
	Info.dwY	= 0;
	Info.dwcX	= 320;
	Info.dwcY	= 240;
	Info.bStep	= 10;
	Info.bPosAction = POS_NO_ACTION;

	printf ("open com port\n");
	Info.hPort = OSOpenPort(NULL); // Opens and initialize default COM port
	if (Info.hPort == FALSE) return;
	OSSetPortHandle(Info.hPort);

	printf ("purge com port\n");
	OSPurge(Info.hPort); // empties COM port before first reading

	// main loop, maybe we can think of a way of exiting...
	while (1) {
		Info.symbol = GetSymbol(Info.hPort); // Reads until a good sequence is found
		Info = ReadLibrary(Info);	// Identifies sequence with a known key
		if (Info.symbol == 141)	break; //allow open/close/open/close/open/close stress test
		if (Info.test) OSPurge(Info.hPort); // If a known key was found, empties Com port
	}
	MPEGDriverUnload();
	OSClosePort (Info.hPort);

#ifndef _BARBADOS_	
	exit(0);
#else
	// exit() causes segmentation fault on Linux (bug in thread library?)
	pthread_exit(0);
#endif _BARBADOS_	
}
