/***************************************************************************

	psp.c

	Initialize PSP and control threads.

***************************************************************************/

#include "ncdz.h"

/* Define the module info section */
PSP_MODULE_INFO("NCDZPSP", VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD);

/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);


/***************************************************************************
	Global variables
 ***************************************************************************/

volatile int bLoop;
volatile int bSleep;


/*--------------------------------------------------------
	Get application status
 -------------------------------------------------------*/

int get_app_status(void)
{
	return (bLoop == LOOP_EXIT);
}


/*--------------------------------------------------------
	Exit callback
 -------------------------------------------------------*/

SceKernelCallbackFunction ExitCallback(int arg1, int arg2, void *arg)
{
	bLoop = LOOP_EXIT;
	return 0;
}


/*--------------------------------------------------------
	Power callback
 -------------------------------------------------------*/

static SceKernelCallbackFunction PowerCallback(int unknown, int pwrflags, void *arg)
{
	int cbid;

	if (pwrflags & POWER_CB_POWER)
	{
		bSleep = 1;
		mp3_set_sleep_flag();
	}
	else if (pwrflags & POWER_CB_RESCOMP)
	{
		bSleep = 0;
	}

	cbid = sceKernelCreateCallback("Power Callback", (void *)PowerCallback, NULL);

	scePowerRegisterCallback(0, cbid);
}

/*--------------------------------------------------------
	Create callback threads
 -------------------------------------------------------*/

static int CallbackThread(SceSize args, void *argp)
{
	int cbid;

	cbid = sceKernelCreateCallback("Exit Callback", (void *)ExitCallback, NULL);
	sceKernelRegisterExitCallback(cbid);

	cbid = sceKernelCreateCallback("Power Callback", (void *)PowerCallback, NULL);
	scePowerRegisterCallback(0, cbid);

	sceKernelSleepThreadCB();
}


/*--------------------------------------------------------
	Setup callback threads
 -------------------------------------------------------*/

static int SetupCallbacks(void)
{
	SceUID thread_id = 0;

	thread_id = sceKernelCreateThread("Update Thread", CallbackThread, 0x9, 0x1000, 0, NULL);
	if (thread_id >= 0)
	{
		sceKernelStartThread(thread_id, 0, 0);
	}

	return thread_id;
}


/*--------------------------------------------------------
	Show error message
 -------------------------------------------------------*/

void ErrorMsg(char *msg, ...)
{
	char buf[256];
	va_list arg;

	va_start(arg, msg);
	vsprintf(buf, msg, arg);
	va_end(arg);

	video_clear_frame(draw_frame);
	small_font_print_center((SCR_HEIGHT - 8) >> 1, COLOR_WHITE, buf, 1);
	video_flip_screen(0);
	pad_wait_press(-1);

	blit_clear_cache();
}


/*--------------------------------------------------------
	main()
 -------------------------------------------------------*/

int main(int argc, char **argv)
{
	bLoop = 0;
	bSleep = 0;

	SetupCallbacks();

	scePowerSetClockFrequency(222, 222, 111);

	pad_init();
	video_init();

	file_browser(argv[0]);

	video_exit();
	sceKernelExitGame();

	return 0;
}
