#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include <string.h>
#include "../sdk/inputdriver.h"
#include "../sdk/inputframework.h"

PSP_MODULE_INFO("piKeySio", 0x1000, 1, 1);
PSP_NO_CREATE_MAIN_THREAD();

#define	DRIVERVER	"1"
#define REQUIREDVER	1

SceUID siothread;

int getDriverInfo(DRIVERINFO *infostruct) {
	strcpy(infostruct->driverVersion, DRIVERVER);
	if(infostruct->apiVersion < REQUIREDVER) return PIKEY_ERROR_VERSION;
	else {
		strcpy(infostruct->driverName, "SIO");
		infostruct->apiVersion = PIKEY_THIS_VERSION;
		infostruct->unicodeSupported = FALSE;
		return PIKEY_SUCCESS;
	}
}

int main_thread(SceSize args, void *argp) {
	int ch;

	sioPrint("SIO in ready.\r\n");

	for(;;) {	
		ch = pspDebugSioGetchar();

		if(ch > 0)
		{
			putNextCharW(ch);
		}

    sceKernelDelayThread(1000);
	}
	return 0;
}

// called by module_start
int main(void) {

	siothread = sceKernelCreateThread("sio", main_thread, 16, 0x800, 0, NULL);
	if (siothread >= 0) sceKernelStartThread(siothread, 0, NULL);
	else Kprintf("Failed to create thread");

	return 0;
}

void module_stop(void) {
}

int module_start(SceSize args, void *argp) __attribute__((alias("_start")));

int _start(SceSize args, void *argp)
{
	return main();
}
