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

/*
 *      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 "define.h"
#include "gtk_callbacks.h"
#include "remote.h"

void switch_player_mode_with_remote(INFO *info)
{
  switch(info->bDiscType)
    {
    case LOCALFILE:
#ifdef _VOD_
      toggle_player_mode(RTSPURL);
#else
      toggle_player_mode(DRIVE_NUMBER);
#endif
      break;
    case RTSPURL:
      toggle_player_mode(DRIVE_NUMBER);
      break;
    default:
      toggle_player_mode(LOCALFILE);
      break;
    }
}
//-------------------------------------------------------------------------------------//
// ReadLibrary - This function is a Library of behaviours attached to the symbols read //
//-------------------------------------------------------------------------------------//
void ReadLibrary(INFO *InfoBuffer)
{
	InfoBuffer->test = TRUE;	// value returned by the function
	
	gdk_threads_enter();
	switch(InfoBuffer->symbol)			// identify symbol
	{
	case 13:		// 7
	  G_MESSAGE("7");
	  
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(7));
	  break;
	case 17:		// 8
	  G_MESSAGE("8");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(8));
	  break;
	case 21:
	  G_MESSAGE("VGA/TV");
	  break;
	case 25:		// 0
	  G_MESSAGE("0");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(0));
	  break;
	case 33:		
	  G_MESSAGE("ENT");
	  keypad_ent_clicked_cb(NULL, NULL);
	  break;
	case 37:		// L-R
	  G_MESSAGE("L-R");
	  keypad_del_clicked_cb(NULL, NULL);
	  break;
	case 141:		// Computer: FMPClose()
	  G_MESSAGE("Computer");
	  
	    break;
	case 142:		// Player : FMPOpen()
	    G_MESSAGE("Player");
	    switch_player_mode_with_remote(InfoBuffer);
	  break;
	case 143:		// UP
	  G_MESSAGE("UP");
	  dvd_navigation_cb(NULL, UP, NULL);	
	    break;
	case 145:		// Left
		G_MESSAGE("LEFT");
	dvd_navigation_cb(NULL, LEFT, NULL);	
		break;
	case 146:		// Sel
	  G_MESSAGE("SEL");
	  dvd_navigation_cb(NULL, SEL, NULL);
		break;
	case 147:		// Down
	  G_MESSAGE("DOWN");
	  dvd_navigation_cb(NULL, DOWN, NULL);
	  break;
	case 148:		// Menu: ReturnPGSearch
	  G_MESSAGE("Menu");
	  dvd_menu_call_cb(NULL, DVD_ROOT_ID, NULL); 
		break;
	case 149:		// Title
	  G_MESSAGE("Title");
	  dvd_menu_call_cb(NULL, DVD_TITLE_ID, NULL);
		break;
	case 150:		// OSD
	  G_MESSAGE("Osd");	
	  break;
	case 151:		// Language
	  G_MESSAGE("Language");
	  dvd_menu_call_cb(NULL, DVD_AUDIO_ID, NULL);
		break;
	case 152:		// Angle
	  G_MESSAGE("Angle");
	  dvd_menu_call_cb(NULL, DVD_ANGLE_ID, NULL);
		break;
	case 153:		// Sub-title
	  G_MESSAGE("Subtitle");
	  dvd_menu_call_cb(NULL, DVD_SUBPICTURE_ID, NULL);
	  break;
	case 154:		// Play/Pause: if playing: FMPPause(), if not playing, FMPPlay()
	  G_MESSAGE("Play/Pause");
	  play_cb(NULL, NULL);
		break;
	case 155:		// Stop: FMPStop()
	  G_MESSAGE("Stop");
	  if(InfoBuffer->playing == STOPPED)
	    close_cb(NULL, NULL);
	  stop_cb(NULL, NULL);
		break;
	case 156:		// Step play
	  G_MESSAGE("Step play");
		break;
	case 157:		// Eject
	  G_MESSAGE("Eject");
	  eject_cb(NULL, NULL);
		break;
	case 158:		// Rewind
	  G_MESSAGE("Rewind");
	  frewind_cb(NULL, NULL);
		break;
	case 159:		// Fast Forward
	  G_MESSAGE("Fast Forward");
	  fforward_cb(NULL, NULL);
		break;
	case 160:		// Last track: PrevPGSearch()
	  G_MESSAGE("Prev track");
	  prev_cb(NULL, NULL);
		break;
	case 161:		// Next track: NextPGSearch()
	  G_MESSAGE("Next track");
	  next_cb(NULL, NULL);
		break;
	case 162:		// Vol+
	  G_MESSAGE("Vol+");
		break;
	case 163:		// 1: VCDNumericSelection()
	  G_MESSAGE("1");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(1));
		break;
	case 164:		// 2
	  G_MESSAGE("2");
	   keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(2));
		break;
	case 165:		// 6
	  G_MESSAGE("6");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(6));
		break;
	case 166:		// Vol-
	G_MESSAGE("Vol-");
		break;
	case 167:		// 4
	  G_MESSAGE("4");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(4));
		break;
	case 168:		// 5
	  G_MESSAGE("5");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(5));
		break;
	case 169:		// 9
	  G_MESSAGE("9");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(9));
		break;
	case 171:		// Mute		
	  G_MESSAGE("Mute");	
		break;
	case 172:		// 3		
	  G_MESSAGE("3");
	  keypad_digit_clicked_cb(NULL, GUINT_TO_POINTER(3));
		break;
	case 190:		// Right
	  G_MESSAGE("RIGHT");
	  dvd_navigation_cb(NULL, RIGHT, NULL);
		break;
	default:
	  InfoBuffer->test = FALSE;			// no character recognized... return 0.
	  break;
	}
	gdk_threads_leave();
}


gint handle_listening_to_remote_timeout(gpointer data)
{
  INFO *local_info = (INFO*) data;
  
  GetSymbol(local_info);  /* Reads until a good sequence is found */
  ReadLibrary(local_info);	/* Identifies sequence with a known key */
  if (local_info->symbol == 141)
    {	
      OSClosePort (local_info->hPort); 
      return FALSE;
    }
  if(local_info->test) 
    OSPurge(local_info->hPort); /* If a known key was found, empties Com port */
  return TRUE;
}
