/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  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.
 *
 *
 *                      ********************
 *
 *
 *  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 Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#ifdef  __SOUND_ENABLE

#include <gtk/gtk.h>
#include "Sound.h"
#include "Debug.h"
#include "Gconf.h"
#include "Thread.h"
#include "Animation.h"
#include "Log.h"
#include "Interface/Demarrage_Interface.h"
#include "Interface/Maemo.h"
#include "Macro.h"

gpointer Thread_Sound_Alert (void)
{
	long      ret;
	int       current_section;

	Create_Log(CATEG_THREAD_SOUND,NULL,NULL,TRUE);

	char      pcmout[BUF_SIZE];

	while (1) {
		if (Sound_Alarme_Active) {
			while (1) {
				ret = ov_read (&vf, pcmout, BUF_SIZE, 0, 2, 1, &current_section);
				if (ret == 0)
					break;
				ao_play (device, pcmout, ret);
			}
			ov_raw_seek (&vf, 0);
		}
		else
			sleep (1);
	}


	ao_close (device);
	ao_shutdown ();


	ov_clear (&vf);
	Stop_Thread (0);
}

void Initialisation_Sound_Alert (void)
{

	ao_sample_format format;
	int       default_driver;
	gchar	  *Message_Error = NULL,*device_Select = NULL;


	vorbis_info *vi = NULL;
	FILE     *f = NULL;

	Sound_Alarme_Active = FALSE;


	if ((f = fopen (Lecture_Gconf_Char (CHAR_CHEMIN_SON_ALERT), "r")) == NULL) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Error opening redalert.ogg");
#ifndef         __MAEMO
		gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar_Demarrage), "opening redalert.ogg : FAILED");
#else
		gtk_progress_bar_set_text_maemo(banner_maemo_Demarrage, "opening redalert.ogg : FAILED");
#endif
		refresh ();
		sleep(1);
		return;
	}


	if (ov_open (f, &vf, NULL, 0) < 0) {
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, "Input isn't ogg");
#ifndef         __MAEMO
		gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar_Demarrage), "Ogg : FAILED");
#else
		gtk_progress_bar_set_text_maemo(banner_maemo_Demarrage, "Ogg : FAILED");
#endif
		refresh ();
		sleep(1);
		return;
	}


	vi = ov_info (&vf, -1);
	format.bits = 16;
	format.channels = vi->channels;
	format.rate = vi->rate;
	format.byte_format = AO_FMT_LITTLE;


	ao_initialize ();

	switch (Lecture_Gconf_Int (INT_AUDIO_DEVICE)) {
	    case 0:
			device_Select = "arts";
		    break;
	    case 1:
			device_Select = "alsa";
		    break;
	    case 2:
			device_Select = "alsa09";
		    break;
	    case 3:
			device_Select = "esd";
		    break;
	    case 4:
			device_Select = "irix";
		    break;
	    case 5:
			device_Select = "oss";
		    break;
	    default:
			device_Select = "null";
	}
	default_driver = ao_driver_id (device_Select);

	device = ao_open_live (default_driver, &format, NULL);
	if (device == NULL) {
		switch (errno){
			case AO_ENODRIVER:
				Message_Error = g_strdup_printf("WARNING: AO_ENODRIVER - No driver corresponds to [%s].",device_Select);
				break;
			case AO_ENOTLIVE:
   		 		Message_Error = g_strdup_printf("WARNING: AO_ENOTLIVE - This driver is not a live output device [%s].",device_Select);
				break;
			case AO_EBADOPTION:
			    Message_Error = g_strdup_printf("WARNING: AO_EBADOPTION - A valid option key has an invalid value [%s].",device_Select);
    			break;
			case AO_EOPENDEVICE:
				Message_Error = g_strdup_printf("WARNING: AO_EOPENDEVICE - Cannot open the device [%s]",device_Select);
				break;
    		case AO_EFAIL:
				Message_Error = g_strdup_printf("WARNING: AO_EFAIL - Any other cause of failure [%s].",default_driver);
				break;
			default:
				Message_Error = g_strdup_printf("WARNING: ao_open_live : (failed) [%s]",device_Select);
	}
		
		debug_printf (INTERFACE, D_WARN, __FILE__, __PRETTY_FUNCTION__, __LINE__, Message_Error);
#ifndef         __MAEMO
		gtk_progress_bar_set_text (GTK_PROGRESS_BAR (progressbar_Demarrage), Message_Error);
#else
		gtk_progress_bar_set_text_maemo(banner_maemo_Demarrage, Message_Error);
#endif
		refresh ();
		sleep(3);
		MYFREE(Message_Error);
		return;
	}
	demmarrage_Thread (Thread_Sound_Alert, NULL);
}
#endif
