/*****************************************************************************/
/*
 *      main.c -- REALmagic simple windowing application in GTK
 *
 *      Copyright (C) 2000 Sigma Designs
 *                    written by Nicolas Vignal <nicolas_vignal@sdesigns.com>
 *                    modified by Emmanuel Michon <emmanuel_michon@sdesigns.com>
 *                                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"
#include "manageconfig.h"

DWORD g_Pass;
DWORD g_OutputMode = 0;

GuiElement GUI;
INFO Info;
FMPOVERLAYVDO ovl;

void treatSIGINT()
{
  signal(SIGINT,treatSIGINT);
  gtk_main_quit();
}

void initialize_gui_element(GuiElement *gui)
{
  memset (gui,0,sizeof(GuiElement));
  gui->window_width = 640;
  gui->window_height = 480;
}

int main (int argc, char *argv[])
{  
	GtkWidget *drawingarea;
	GdkColor keycolor;
	GdkColormap *colormap;
	// DWORD temp;
	
	//DWORD FMPreturn;
	
	signal(SIGINT,treatSIGINT);
	if (!g_thread_supported())
	  {
	    g_thread_init(NULL);
	    g_print("\nMultithreading mode support is enabled.\n\n");
	  }
	else
	  g_print("\nYour GLib version is not thread safe !!\n\n");
	
	gtk_init (&argc, &argv);
	
	/* initializing the GUI element */
	initialize_gui_element(&GUI);
	/* initializing the Gui state structure */
	initialize_gui_state(&ovl);
	/* initializing media structure */
	initialize_info_structure_disc(&Info);
	if(load_fmp_config(&ovl) &&
	   load_localfile_config(&Info))
	  g_print("\nConfig file succesfully loaded !\n");
	else
	  g_print("\nConfig file loading has failed !\n");
	
	MPEGon(&Info);
	
	GUI.window = create_window ();
	
	gtk_widget_show (GUI.window);
	
	set_check_item_state();
	
	drawingarea = GTK_WIDGET(gtk_object_get_data (GTK_OBJECT (GUI.window), "drawingarea"));

	// Get drawingarea colormap
	colormap = gdk_window_get_colormap (drawingarea->window);
	keycolor.red = 0;
	keycolor.green = 10280;
	keycolor.blue = 0;

	// Allocate color
	gdk_color_alloc (colormap, &keycolor);
	
	// Set drawingarea background color
	gdk_window_set_background (drawingarea->window, &keycolor);

	// Clear drawingarea
	gdk_window_clear (drawingarea->window);

	g_OutputMode = (WORD)FMPGetProfileWord(RMREG_OUTPUTMODE);
	
	// Spawning a playing process
	// temp = (WORD)FMPGetProfileWord(RMREG_AUTOPLAY);
	//if(temp == RM_AUTOPLAY_ON)

	gdk_threads_enter();
	gtk_main ();
	gdk_threads_leave();
	
	if(save_fmp_config(ovl) && 
	   save_localfile_config(Info))
	  g_print("\nConfig file succesfully saved !\n");
	else
	  g_print("\nConfig file saving has failed !\n");
	if(GUI.playlistwin != NULL)
	  save_playlist(NULL);
	if(ovl.b_overlay_access == TRUE)
	  {
	    switch_overlay_back(&ovl);
	  }
	G_WARNING("Exiting app !");
	MPEGoff(&Info);
	// FMPDestroyMSGQUEUE();
	return 0;
}
