/**
 * receiver_controller.c: Controller for softdevice receiver
 * 
 * Copyright: See COPYING file that comes with this distribution
 *
 * Author: Matthias Feistel <hitman_47@users.sourceforge.net>, (C) 2006, 2007
 */

#ifdef LASTFM_USE_SOFTDEVICE_RECEIVER

#include "receiver_controller.h"

cReceiverController::cReceiverController( void )
{
	Receiver_ = 0;

	SearchSoftdevice();
}

cReceiverController::~cReceiverController( )
{
	if( Receiver_ )
		DELETENULL( Receiver_ );
}

int cReceiverController::SearchSoftdevice( void )
{
	softdevicePlugin_ = cPluginManager::GetPlugin( "softdevice" );

	if ( !softdevicePlugin_ ||
			!softdevicePlugin_->Service( GET_PACKET_HANDEL_IDV100,
					(void *)&SoftHandles ) )
	{
		return 1;
	}

	// from vdr-softplay; used modified (SwitchChannel):
	const cChannel *channel=Channels.GetByNumber(cDevice::CurrentChannel());

	if( channel )
	{
		Receiver_=new cSoftplayReceiver(channel,
				cDevice::PrimaryDevice(),SoftHandles);
#if VDRVERSNUM >= 10500 
		cDevice *receiveDev=cDevice::GetDevice(channel, 1, true);
#else
		cDevice *receiveDev=cDevice::GetDevice(channel,1);
#endif
		if (receiveDev)
		{
			receiveDev->AttachReceiver(Receiver_);
#if VDRVERSNUM >= 10500 
			receiveDev->SwitchChannel( channel, true );
#endif
		}

	}

	return 0;
}

// ---- Receiver controls ------------------------------
// from vdr-softplay; used unmodified

void cReceiverController::ChannelUpDown( int i )
{
	if (!Receiver_ )
	return;

	i = i>0 ? +1 : -1;

	int currChannelNo=Receiver_->currChannel.Number();
	const cChannel *channel = Channels.GetByNumber( currChannelNo + i, i );
	if (!channel)
	{
		printf("Didn't get channel!\n");
		return;
	};

	if( Receiver_ )
		DELETENULL( Receiver_ );

#if VDRVERSNUM >= 10500 
	cDevice *receiveDev=cDevice::GetDevice(channel,1,true);
#else
	cDevice *receiveDev=cDevice::GetDevice(channel,1);
#endif
	
	if (!receiveDev)
	{
		printf("Didn't get device!\n");
		Skins.Message(mtError, tr("Channel not available!"));
		channel = Channels.GetByNumber( currChannelNo );
#if VDRVERSNUM >= 10500 
		receiveDev=cDevice::GetDevice(channel,0,true);
#else
		receiveDev=cDevice::GetDevice(channel,1);
#endif
		if (!receiveDev)
		{
			printf("Jetzt bin ich angeschmiert\n");
			return;
		};
	};
	receiveDev->SwitchChannel(channel,false);
	Receiver_=new cSoftplayReceiver(channel,
			cDevice::PrimaryDevice(),SoftHandles);
	if (!receiveDev->AttachReceiver(Receiver_) )
	{
		printf("Could not attach to device!\n");
	};

}

#endif // LASTFM_USE_SOFTDEVICE_RECEIVER
