#include "DCDswi.h"
#include <string.h>

#include "swis.h"

static int Player = -1;
static int PlugIn = -1;

int DCDUtils_GetFullScreenPlugIn(void)
{
	int val;

	// DCDUtils_GetFullScreenPlugIn
	if (_swix(0x52788, _OUT(0), &val)) return -1;

	return val;
}

void DCDUtils_SetPlayer(int player)
{
	Player = player;
}

const _kernel_oserror* DCDUtils_RegisterPlugIn(int task, bool bFullScreen)
{
	// DCDUtils_RegisterPlugIn
	return _swix(0x052783, _INR(1,3)|_OUT(0),
			Player, task, (bFullScreen) ? 1: 0,
			&PlugIn);
}

const _kernel_oserror* DCDUtils_DeregisterPlugIn(void)
{
	// DCDUtils_DeregisterPlugIn
	return _swix(0x052784, _IN(0), PlugIn);
}

void DCDUtils_GetInfo(int* window, int* count, int* volume, const char** title)
{
	// DCDUtils_GetPlayerInfo
	if (_swix(0x052786, _IN(0)|_OUTR(1,4), Player,
		window, count, volume, title))
	{
		if (window) *window = -1;
		if (count)  *count  = 0;
		if (volume) *volume = 256;
		if (title)  title   = NULL;
	}
}

int DCDUtils_GetBuffer(char2* buffer, char2* buffer_end)
{
	// DCDUtils_FillABuffer
	if (_swix(0x05278E, _INR(0,3)|_OUT(2),
		Player, buffer, buffer_end, 4 /* 8-bit, volume scaled */,
		&buffer_end))
		memset(buffer, 0, sizeof(char2)*(buffer_end - buffer));

	return buffer_end - buffer;
}
