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

/*
 *      dvdoptionswincb.c -- REALmagic simple windowing application in GTK
 *
 *      Copyright (C) 2001 Sigma Designs
 *                    written by Pascal Cannenterre <pascal_cannenterre@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 "define.h"
#include "gtk_callbacks.h"
#include "interface.h"


gboolean handle_start_region_code_change_dvd(guint region)
{
  BYTE bCounter = 0;
  
  if( (RegionCodeControl(TRUE, 0, 0)) != FMPE_OK )
    {
      gtk_message_box("Error", "Region Code Change",
		      "The change has failed !",
		      TRUE,
		      NULL,
		      NULL);
      return FALSE;
    }
  
  Info.b_boardRegion = RegionCodeControl (0, GET_REGION, 0);
  bCounter = RegionCodeControl (0, GET_COUNTER_A, 0);
  G_WARNING("Region Code : %d, Counter : %d !",Info.b_boardRegion, bCounter);
  FMPClose();
  if (bCounter == 5)
    {
      gtk_message_box("Error", "Region Code Change",
		      "Cannot change the region code anymore !",
		      TRUE,
		      NULL,
		      NULL);
      return FALSE;
    }
  else if (bCounter == 4)
    {
      gtk_quick_question_box("Warning", "Region Code Change",
			     "The region code can be changed ONLY ONE more time !",
			     GTK_SIGNAL_FUNC(handle_confirm_region_code_change_dvd),
			     GUINT_TO_POINTER(region)); 
    }
  else
    {
      gchar str_count[2];
      gchar *msg = NULL;
      sprintf(str_count, "%d", 5-bCounter);
      msg = g_strconcat("You can change the region code ",
			str_count,
			" more times", NULL);
      gtk_quick_question_box("Warning", "Region Code Change",
			     msg,
			     GTK_SIGNAL_FUNC(handle_confirm_region_code_change_dvd),
			     GUINT_TO_POINTER(region));

      g_free(msg);
    }
  return TRUE;
}


gboolean switch_dvdregion_cb(GtkWidget *widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      if(Info.playing == CLOSED)
	{
	  guint region = GPOINTER_TO_UINT(data);
	  handle_start_region_code_change_dvd(region);
	  return TRUE; 
	}
      else
	gtk_message_box("Warning", "Region Code Change",
			"Please, CLOSE the DVD playing then proceed.",
			TRUE,
			NULL,
			NULL);
    }
  return TRUE;
}


gboolean handle_confirm_region_code_change_dvd(GtkWidget *widget, gpointer data)
{
  BYTE bCounter = 0;
  BYTE region = (BYTE) (GPOINTER_TO_UINT(data));
  gchar str_count[2];
  gchar str_region[4];
  gchar *msg = NULL;

  gtk_destroy_message_box(NULL, NULL);

  if( (RegionCodeControl(TRUE, 0, 0)) != FMPE_OK )
    {
      gtk_message_box("Error", "Region Code Change",
		      "The change has failed !",
		      TRUE,
		      NULL,
		      NULL);
      return FALSE;
    }
  
  if (Info.b_boardRegion != region)
    {
      Info.b_boardRegion=RegionCodeControl (0, SET_REGION, region);
    }
  
  Info.b_boardRegion=RegionCodeControl (0, GET_REGION, 0);
  bCounter=RegionCodeControl (0, GET_COUNTER_A, 0);
  G_WARNING("AT LEAST -> Region Code : %d, Counter : %d !",Info.b_boardRegion, bCounter);
  FMPClose();
  
  sprintf(str_count, "%d", 5-bCounter);
  sprintf(str_region, "%d", Info.b_boardRegion);

  msg = g_strconcat("Region code is ",
		    str_region,", ",
		    str_count,
		    " changes remaining.", NULL);
  
  gtk_message_box("Warning", "Region Code Change",
		  msg,
		  TRUE,
		  NULL,
		  NULL);
  g_free(msg);
  return TRUE;
}

gboolean switch_dvdpglevel_cb(GtkWidget *widget, gpointer data)
{


  return TRUE;
}

gboolean dvdpglevel_passwd_cb(GtkWidget *widget, gpointer data)
{


  return TRUE;
}

gboolean getMatchingSPFromAudio(DWORD audio_stream, BYTE *matching_sp)
{
  WORD lc_audio;
  BYTE i;

  lc_audio = Info.AudioStreamLangage[audio_stream];
  
  for(i=0;i<Info.bSPStreamTotal;i++)
    {
      if(lc_audio == Info.SPStreamLangage[i])
	{
	  *matching_sp = i;
	  return TRUE;
	}
    }
  
  *matching_sp = 0;
  return FALSE;
}

void audioStreamChange_cb(GtkWidget *widget, guint action, gpointer data)
{
  DWORD current_audiostream;

  if(Info.playing != CLOSED)
    {
      getFMPProperty(FMPI_CURRENT_AUDIO_STREAM, &current_audiostream);
  
      if( current_audiostream != action )
	{
	  AudioStreamChange( (BYTE)action );
	  Info.bAudioStream = (BYTE)action;
	}
    }
}


void spStreamChange_cb(GtkWidget *widget, guint action, gpointer data)
{
  DWORD current_spstream, current_audiostream;
  BYTE audio_follow_sp;
 
  if(Info.playing != CLOSED) 
    {
      getFMPProperty(FMPI_SUBPICTURE_STREAM_AVAILABLE, &current_spstream);
      getFMPProperty(FMPI_CURRENT_AUDIO_STREAM, &current_audiostream);
     
      switch(action)
	{
	case SUBTITLEOFF:
	  SubPictureStreamChange(0, FALSE);
	  G_WARNING("Asking for SP stream 0 but NO displaying ...");
	  Info.bSPStream = 0;
	  Info.SPStreamOn = FALSE;
	  break;
	 
	case RM_LANGUAGECODE_AUDIOFOLLOW:
	  if(getMatchingSPFromAudio(current_audiostream, &audio_follow_sp))
	    G_PRINT("\n\nAudio Follow succeded !!\n\n");
	  SubPictureStreamChange(audio_follow_sp, TRUE);
	  Info.bSPStream = audio_follow_sp;
	  Info.SPStreamOn = TRUE;
	  break;
	 
	default:
	  if(current_spstream != action)
	    {
	      SubPictureStreamChange((BYTE)action, TRUE);
	      Info.bSPStream = (BYTE)action;
	      Info.SPStreamOn = TRUE;
	    }
	  break;
	}
    }
 
}

void saveCurrentDvdStatus(DvdPlayingStatus *dvd_status)
{
  /* I should use the glib and not directly this standard 
   *'C' function 
   */
  DWORD dvd_format;
  memset(dvd_status, 0, sizeof(DvdPlayingStatus));
  
  getFMPProperty(FMPI_CURRENT_TITLE, (DWORD*)&(dvd_status->current_title) );
  getFMPProperty(FMPI_CURRENT_CHAPTER, (DWORD*)&(dvd_status->current_chapter) );
  getFMPProperty(FMPI_CURRENT_ANGLE, (DWORD*)&(dvd_status->current_angle) );
  getFMPProperty(FMPI_CURRENT_AUDIO_STREAM, (DWORD*)&(dvd_status->current_ast) );
  getFMPProperty(FMPI_CURRENT_SUBPICT_STREAM, (DWORD*)&(dvd_status->current_spst) );
  getFMPProperty(FMPI_SUBPICTURE_ON, (DWORD*)&(dvd_status->subpicture_on));
  getDvdAspectRatio( &(dvd_format), &(dvd_status->display_mode) );
  
}

void restorePreviousDvdStatus(DvdPlayingStatus *dvd_status)
{
  DvdPlayingStatus actual_status;
  
  saveCurrentDvdStatus(&actual_status);
  
  if(actual_status.current_title != dvd_status->current_title)
    {
      if(Info.playing > STOPPED)
	{
	  // stop_cb(NULL, NULL);
	}
      else if(Info.playing == CLOSED)
	{
	  play_cb(NULL, NULL);
	  //stop_cb(NULL, NULL);
	}
      ChapterPlay((BYTE)(dvd_status->current_title), 
		  (WORD)(dvd_status->current_chapter));
    }
  else if(actual_status.current_chapter != dvd_status->current_chapter)
    {
      ChapterSearch((WORD)(dvd_status->current_chapter));
    }
  
  if(actual_status.current_title == dvd_status->current_title && 
     actual_status.current_angle != dvd_status->current_angle)
    AngleChange((BYTE) (dvd_status->current_angle));
  
  if(actual_status.current_title == dvd_status->current_title &&
     actual_status.current_ast != dvd_status->current_ast)
    AudioStreamChange((BYTE) (dvd_status->current_ast) );
  
  if(actual_status.current_title == dvd_status->current_title &&
     actual_status.current_spst != dvd_status->current_spst &&
     dvd_status->subpicture_on)
    SubPictureStreamChange((BYTE)(dvd_status->current_spst), TRUE);

  if(!dvd_status->subpicture_on)
    SubPictureStreamChange((BYTE)(dvd_status->current_spst), FALSE);
    
  /* Video Mode */
}

gboolean dvd_options_Ok_clicked(GtkWidget *widget, gpointer   data )
{
  GtkWidget *window;
  window = GTK_WIDGET(gtk_widget_get_toplevel(widget));
  
  /* if(Info.playing != CLOSED) */
  /*     saveCurrentDvdStatus((DvdPlayingStatus*)data); */
  
  gtk_widget_destroy(window);
  GUI.dvd_options = NULL;
  return TRUE;
}

gboolean dvd_options_Cancel_clicked(GtkWidget *widget, gpointer   data )
{
  GtkWidget *window;
  window = gtk_widget_get_toplevel(widget);
  
  if(Info.playing != CLOSED)
    restorePreviousDvdStatus((DvdPlayingStatus*)data);
  
  gtk_widget_destroy(window);
  GUI.dvd_options = NULL;
  return TRUE;
}

void refresh_language_frame( GtkWidget *widget )
{
  GtkOptionMenu *audio_s, *sub_s;
  DvdPlayingStatus actual_status;

  audio_s = 
    GTK_OPTION_MENU(gtk_object_get_data (GTK_OBJECT (widget), "menu_audio_language"));
  sub_s = 
    GTK_OPTION_MENU(gtk_object_get_data (GTK_OBJECT (widget), "menu_sp_language"));

      saveCurrentDvdStatus(&actual_status);
      
      if(actual_status.subpicture_on)
	gtk_option_menu_set_history(GTK_OPTION_MENU(sub_s), 
				    (guint)(actual_status.current_spst));
      else
	gtk_option_menu_set_history(GTK_OPTION_MENU(sub_s), 
				    (guint)(1+Info.bSPStreamTotal));
      
      gtk_option_menu_set_history(GTK_OPTION_MENU(audio_s), 
				  (guint)(actual_status.current_ast));
    
}

void refresh_dvd_content_frame( GtkWidget *widget )
{
  GtkWidget *language_frame;
  
  language_frame = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (widget), "language_frame"));
  refresh_language_frame(language_frame);
}

void refresh_tv_format_frame( GtkWidget *widget )
{




}

void refresh_aspect_4_3_frame( GtkWidget *widget )
{
  GtkToggleButton * obj1, *obj2, *obj3;
  DWORD ratio; 
  
  obj1 = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioNORMAL"));
  obj2 = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioZOOMOFF"));
  obj3 = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioZOOMON"));
  gtk_widget_set_sensitive(GTK_WIDGET(obj1), TRUE);
  gtk_widget_set_sensitive(GTK_WIDGET(obj2), TRUE);
  gtk_widget_set_sensitive(GTK_WIDGET(obj3), TRUE);
  
  if(ovl.b_overlay_access)
    { 
      getFMPProperty(FMPI_VIDEOASPECT, &ratio);
      
     /*  if(Info.tv_format == TV_4x3) */
/* 	{ */
/* 	  setFMPProperty(FMPI_VIDEOASPECT, FMPV_VIDEOASPECT_4x3_4x3_NORMAL); */
/* 	  gtk_widget_set_sensitive(GTK_WIDGET(obj2), FALSE); */
/* 	  gtk_widget_set_sensitive(GTK_WIDGET(obj3), FALSE); */
/* 	} */
/*       else */
/* 	 { */
/* 	   gtk_widget_set_sensitive(GTK_WIDGET(obj1), FALSE); */
/* 	 } */
      
      switch(ratio)
	{
	case FMPV_VIDEOASPECT_4x3_4x3_NORMAL:
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj1), TRUE);
	  break;
	case FMPV_VIDEOASPECT_4x3_16x9_ZOOM_OFF:
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj2), TRUE);
	  break;
	case FMPV_VIDEOASPECT_4x3_16x9_ZOOM_ON:
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj3), TRUE);
	  break;
	default:
	  G_WARNING("Video aspect seems to be 16:9 !");
	  break;  
	}
    }
}

void refresh_aspect_16_9_frame( GtkWidget *widget )
{
  GtkToggleButton * obj1, *obj2, *obj3;
  DWORD ratio;
  
  obj1 = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioWIDE"));
  obj2 = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioLETTERBOX"));
  obj3 = GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPANSCAN"));
  gtk_widget_set_sensitive(GTK_WIDGET(obj1), TRUE);
  gtk_widget_set_sensitive(GTK_WIDGET(obj2), TRUE);
  gtk_widget_set_sensitive(GTK_WIDGET(obj3), TRUE);

  if(ovl.b_overlay_access)
    {     
      getFMPProperty(FMPI_VIDEOASPECT, &ratio);

      /* if(Info.tv_format == TV_16x9) */
/* 	{ */
/* 	  gtk_widget_set_sensitive(GTK_WIDGET(obj2), FALSE); */
/* 	  gtk_widget_set_sensitive(GTK_WIDGET(obj3), FALSE); */
/* 	} */
/*       else */
/* 	 { */
/* 	   gtk_widget_set_sensitive(GTK_WIDGET(obj1), FALSE); */
/* 	 } */	
      
      switch(ratio)
	{
	case FMPV_VIDEOASPECT_16x9_16x9_NORMAL:
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj1), TRUE);
	  break;
	case FMPV_VIDEOASPECT_16x9_4x3_LETTERBOX:
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj2), TRUE);
	  break;
	case FMPV_VIDEOASPECT_16x9_4x3_PANSCAN:
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (obj3), TRUE);
	  break;
	default:
	  G_WARNING("Video aspect seems to be 4:3 !");
	  break;
	}
    }
}

void refresh_aspect_ratio_frame( GtkWidget *widget )
{
  GtkWidget *aspect_4_3_frame, *aspect_16_9_frame;
  DWORD dvd_aspect_ratio, display_mode;

  aspect_4_3_frame  = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (widget),"aspect_4_3_frame"));
  aspect_16_9_frame = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (widget),"aspect_16_9_frame"));
  
  if(Info.playing != CLOSED)
    {
      getDvdAspectRatio(&dvd_aspect_ratio, &display_mode);
      
      if(dvd_aspect_ratio == ASPECT_RATIO_4_3)
	{
	  gtk_widget_set_sensitive(aspect_16_9_frame, FALSE);
	  refresh_aspect_4_3_frame(aspect_4_3_frame);
	}
      else
	{
	  gtk_widget_set_sensitive(aspect_4_3_frame, FALSE);
	  refresh_aspect_16_9_frame(aspect_16_9_frame);
	}
    }
}


void refresh_pg_frame(GtkWidget *widget)
{
  GtkToggleButton *obj[9];

  obj[0] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioUNLIMITED"));
  obj[1] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioG"));
  obj[2] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioLEVEL2"));
  obj[3] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPG"));
  obj[4] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioPG13"));
  obj[5] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioLEVEL5"));
  obj[6] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioR"));
  obj[7] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioNC17"));
  obj[8] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioLEVEL8"));
  
}


void refresh_pglevel_frame( GtkWidget *widget )
{
  GtkWidget *pglevel_frame;
  
  pglevel_frame = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (widget),"pglevel_frame"));
  
  refresh_pg_frame(pglevel_frame);

}

void refresh_region_code_frame(GtkWidget *widget)
{
  GtkToggleButton *obj[6];
  
  obj[0] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioREGION1"));
  obj[1] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioREGION2"));
  obj[2] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioREGION3"));
  obj[3] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioREGION4"));
  obj[4] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioREGION5"));
  obj[5] =  GTK_TOGGLE_BUTTON(gtk_object_get_data (GTK_OBJECT (widget),"radioREGION6"));
  
  
}


void refresh_dvd_options(GtkWidget *widget, gpointer   data)
{
  GtkWidget *obj[4];
  
  if(data != NULL)
    {
      obj[0] = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"dvd_content_frame"));
      obj[1] = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"aspect_frame"));
      obj[2] = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"pglevel_frame"));
      obj[3] = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (data),"dvdregion_frame"));
      
      gtk_widget_hide(obj[2]);

      if(Info.bDiscType == FMPC_DVD_VIDEO)
	{
	  if(Info.playing != CLOSED)
	    {
	      gtk_widget_set_sensitive(obj[0],TRUE);
	      refresh_dvd_content_frame(obj[0]);
	      gtk_widget_set_sensitive(obj[1],TRUE);
	      refresh_aspect_ratio_frame(obj[1]);
	    }
	  else
	    {
	      gtk_widget_set_sensitive(obj[0],FALSE);
	      gtk_widget_set_sensitive(obj[1],FALSE);
	    }
	  
	  gtk_widget_set_sensitive(obj[2],FALSE);
	  refresh_pglevel_frame(obj[2]);
	  
	}
      else
	{
	  gtk_widget_set_sensitive(obj[0],FALSE);
	  gtk_widget_set_sensitive(obj[1],FALSE);
	  gtk_widget_set_sensitive(obj[2],FALSE);
	}
      
	gtk_widget_set_sensitive(obj[3], TRUE);
    }
  else
    G_WARNING("Trouble with data");
}

gboolean switch_aspect_mode_cb(GtkWidget *widget, gpointer data)
{
  if (GTK_TOGGLE_BUTTON (widget)->active)
    {
      setFMPProperty(FMPI_VIDEOASPECT, (DWORD)(GPOINTER_TO_UINT(data)));
    }
  
  return TRUE;
}


/* gboolean switch_tv_format_cb(GtkWidget *widget, gpointer data) */
/* { */
/*   GtkWidget *top, *aspect_4_3_frame, *aspect_16_9_frame; */
/*   DWORD aspect_ratio, display_mode; */
  
/*   Info.tv_format = (DWORD) GPOINTER_TO_UINT(data); */
/*   top = gtk_widget_get_toplevel(widget); */

/*   aspect_4_3_frame =  */
/*     GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (top),"aspect_4_3_frame")); */
/*   aspect_16_9_frame = */
/*     GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (top),"aspect_16_9_frame")); */
  
/*   getDvdAspectRatio(&aspect_ratio, &display_mode); */
  
/*   g_print("\nDisplay Mode : %lx\n", display_mode); */
  
/*   if(aspect_ratio == ASPECT_RATIO_4_3 && Info.tv_format == TV_4x3) */
/*     { */
/*       setFMPProperty(FMPI_VIDEOASPECT, FMPV_VIDEOASPECT_4x3_4x3_NORMAL); */
/*     } */
/*   else if(aspect_ratio == ASPECT_RATIO_16_9 && Info.tv_format == TV_16x9) */
/*     { */
/*       setFMPProperty(FMPI_VIDEOASPECT, FMPV_VIDEOASPECT_16x9_16x9_NORMAL); */
/*       g_warning("Set to Wide !!"); */
/*     } */
  
/*   if(aspect_ratio == ASPECT_RATIO_4_3) */
/*     refresh_aspect_4_3_frame(aspect_4_3_frame); */
/*   else */
/*     refresh_aspect_16_9_frame(aspect_16_9_frame); */

/*   return TRUE; */
/* } */
