//#define	D(x)	x;
#define	D(x)	;

#include	<exec/memory.h>
#include <intuition/screens.h>
#include <graphics/rastport.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/imageclass.h>
#include <intuition/icclass.h>
#include <intuition/gadgetclass.h>

#include <libraries/p4specials.h>

#include <proto/exec.h>
#include <proto/graphics.h>
#include <proto/utility.h>
#include <proto/intuition.h>
#include <proto/gadtools.h>
#include <proto/dos.h>
#include <proto/rtg.h>
#include <proto/xsl.h>
#include	<proto/picasso96.h>

#include	<stdio.h>
#include	<math.h>

#include "TV.h"
#include "cs_sup.h"

extern int ChannelIndex;

struct List ChannelList;

int NumChannels = 0;

void initAVModule(void)
{
	p96PIP_SetTags(wd, 
		P4PALO_Audio_Init, TRUE, 
		P4PALO_TV_Volume, 0xffffffff, 
		P4PALO_TV_Mute, FALSE, 
		TAG_END);
}

void shutdownAVModule(void)
{
	p96PIP_SetTags(wd, 
		P4PALO_TV_Mute, TRUE, 
		TAG_END);
}

void ActivateTuner(void)
{
	p96PIP_SetTags(wd,
		P4PALO_Video_Source, P4PALO_VideoSource_Tuner,
		TAG_DONE);

	Mode = MODE_TV;
}

void ActivateFBAS(void)
{
	p96PIP_SetTags(wd,
		P4PALO_Video_Source, P4PALO_VideoSource_CVBS,
		TAG_DONE);

	Mode = MODE_FBAS;
}

void ActivateSVHS(void)
{
	p96PIP_SetTags(wd,
		P4PALO_Video_Source, P4PALO_VideoSource_YC,
		TAG_DONE);

	Mode = MODE_SVHS;
}

void ActivatePABLO(void)
{
	p96PIP_SetTags(wd,
		P4PALO_Video_Source, P4PALO_VideoSource_Pablo,
		TAG_DONE);

	Mode = MODE_PABLO;
}

void SetChannel(int index)
{
	struct TVchannelHandle *Channel;

	p96PIP_SetTags(wd, 
		P4PALO_TV_Mute, TRUE, 
		TAG_END);

	Channel = (struct TVchannelHandle *)FindNumber(&ChannelList, index);
	p96PIP_SetTags(wd, 
		P4PALO_Tuner_Frequency, Channel->Channel, 
		TAG_DONE);
	Delay(3);

	p96PIP_SetTags(wd, 
		P4PALO_TV_Mute, FALSE, 
		TAG_END);
}

int TuneNext(int index)
{
	if(NumChannels){
		index++;
		index %= NumChannels;

		SetChannel(index);
	}
	return(index);
}

int TunePrev(int index)
{
	if(NumChannels){
		index += NumChannels-1;
		index %= NumChannels;

		SetChannel(index);
	}
	return(index);
}

int Autoscan(void)
{
	struct TVchannelHandle *Channel;
	struct Node *n;
	ULONG	Frequency = 1;

	while(n = RemHead(&TempList)){
		FreeVec(n->ln_Name);
		FreeVec(n);
	}
	TempNumChannels = 0;

	p96PIP_SetTags(wd,
		P4PALO_TV_Mute, TRUE,
		TAG_END);
	p96PIP_SetTags(wd,
		P4PALO_Tuner_Frequency, Frequency,
		TAG_DONE);

	while(Frequency != 0){
		p96PIP_SetTags(wd,
			P4PALO_Tuner_NextStationUp, TRUE,
			TAG_DONE);

		p96PIP_GetTags(wd,
			P4PALO_Tuner_Frequency, &Frequency,
			TAG_DONE);

		D(printf("\t{ %d},\n", Frequency));
		if(Channel = AllocVec(sizeof(struct TVchannelHandle), MEMF_CLEAR)){
			Channel->Node.ln_Name = CloneStr("unset");
			Channel->Channel.Frequency = Frequency;
			AddTail(&TempList,(struct Node *)Channel);
			TempNumChannels++;
		}
	}

	p96PIP_SetTags(wd,
		P4PALO_TV_Mute, FALSE,
		TAG_END);

	return(1);
}

int TuneUp(void)
{
	ULONG Frequency;

	p96PIP_SetTags(wd,
		P4PALO_Tuner_NextStationUp, TRUE,
		TAG_DONE);

	p96PIP_GetTags(wd,
		P4PALO_Tuner_Frequency, &Frequency,
		TAG_DONE);

	D(printf("\t{ %ld },\n", Frequency));

	((struct TVchannelHandle *)TempNode)->Channel.Frequency = Frequency;

	return(1);
}

int TuneDown(void)
{
	ULONG Frequency;

	p96PIP_SetTags(wd,
		P4PALO_Tuner_NextStationDown, TRUE,
		TAG_DONE);

	p96PIP_GetTags(wd,
		P4PALO_Tuner_Frequency, &Frequency,
		TAG_DONE);

	D(printf("\t{ %ld },\n", Frequency));

	((struct TVchannelHandle *)TempNode)->Channel.Frequency = Frequency;

	return(1);
}

int AdjustUp(void)
{
	struct TVchannelHandle *Channel;
	ULONG Frequency;
	ULONG	MaxFrequency, Granularity;

	Channel = (struct TVchannelHandle *)TempNode;
	Frequency = Channel->Channel.Frequency;

	p96PIP_GetTags(wd, P4PALO_Tuner_MaxFrequency, &MaxFrequency, P4PALO_Tuner_Granularity, &Granularity, TAG_END);

	Frequency += Granularity;
	if(Frequency <= MaxFrequency){
		Channel->Channel.Frequency = Frequency;
		p96PIP_SetTags(wd, 
			P4PALO_Tuner_Frequency, Frequency, 
			TAG_DONE);
		Delay(5);
	}

	return(1);
}

int AdjustDown(void)
{
	struct TVchannelHandle *Channel;
	ULONG Frequency;
	ULONG	MinFrequency, Granularity;

	Channel = (struct TVchannelHandle *)TempNode;
	Frequency = Channel->Channel.Frequency;

	p96PIP_GetTags(wd, 
		P4PALO_Tuner_MinFrequency, &MinFrequency, 
		P4PALO_Tuner_Granularity, &Granularity, 
		TAG_END);

	Frequency -= Granularity;
	if(Frequency >= MinFrequency){
		Channel->Channel.Frequency = Frequency;
		p96PIP_SetTags(wd, 
			P4PALO_Tuner_Frequency, Frequency, 
			TAG_DONE);
		Delay(5);
	}

	return(1);
}
