/*
** Amster - Datatype sound player
** Copyright © 1999-2000 by Gürer Özen
**
** 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "amster.h"

#include <proto/exec.h>
#include <proto/datatypes.h>
#include <proto/dtclass.h>
#include <clib/datatypes_protos.h>
#include <clib/alib_protos.h>
#include <datatypes/datatypes.h>
#include <datatypes/datatypesclass.h>
#include <datatypes/soundclass.h>

#include "thread.h"

THREAD_DECL(snd_player);


void snd_play(char *fname)
{
	if (!fname) return;
	th_spawn(NULL, "Amster sound player", &snd_player, 0, fname);
}


THREAD(snd_player)
{
	thread t;
	struct Library *DosBase;
	struct Library *DataTypesBase;
	Object *sample;
	struct dtTrigger trigger;
	long ssig, tsig;

	t = thr_init();
	if (!t) return;
	tsig = (1L << (t->port->mp_SigBit));

	DosBase = OpenLibrary("dos.library", 36);
	if (!DosBase) {
		thr_exit(t, 1);
		return;
	}
	DataTypesBase = OpenLibrary("datatypes.library", 39);
	if (!DataTypesBase) {
		CloseLibrary(DosBase);
		thr_exit(t, 1);
		return;
	}

	ssig = AllocSignal(-1);
	if (ssig == -1) {
		CloseLibrary(DataTypesBase);
		CloseLibrary(DosBase);
		thr_exit(t, 1);
		return;
	}

	sample = NewDTObject((char*)t->data, DTA_GroupID, GID_SOUND, TAG_DONE);
	if (!sample) {
		FreeSignal(ssig);
		CloseLibrary(DataTypesBase);
		CloseLibrary(DosBase);
		thr_exit(t, 2);
		return;
	}

	SetDTAttrs(sample,NULL,NULL,SDTA_SignalBit,(1L << ssig),SDTA_SignalTask,t->task,TAG_DONE);

	trigger.MethodID = DTM_TRIGGER;
	trigger.dtt_GInfo = NULL;
	trigger.dtt_Function = STM_PLAY;
	trigger.dtt_Data = NULL;
	DoDTMethodA(sample, 0L, 0L, (Msg)&trigger);

	Wait((1L<<ssig)|tsig|SIGBREAKF_CTRL_C);

	DisposeDTObject(sample);
	FreeSignal(ssig);
	CloseLibrary(DataTypesBase);
	CloseLibrary(DosBase);
	thr_exit(t, 0);
}
