RFDInput.h

8 views
Skip to first unread message

Matt Meinen

unread,
Oct 3, 2009, 1:17:50 AM10/3/09
to More Explosive Than Poetry
/*
Author: Corporeal
Default File Location: Root of project
File: RFDInput.h
Scope: Event Handling for Reusable File Dialogues
System: Sony PSP 2000
Goals:
Provide single function call for setup of threaded user input
handling.
Provide methods for attaching callback functions as needed.
Event-driven architecture
Example: {
#include <RFDInput.h>

void EHButtonX() {
pspDebugScreenClear();
printf("Oh no you didn't!");
}

void EHButtonO() {
pspDebugScreenClear();
printf("Oh yes I did!");
}

int main (int argc, char *argv[]) {

RFDISetup();
RFDISetFunc(PSP_CTRL_CROSS, &EHButtonX);
RFDISetFunc(PSP_CTRL_CIRCLE, &EHButtonO);

return 0;
}

} Sets up the thread and assigns EHButtonX to CROSS and EHButtonO to
CIRCLE

*/

#ifndef __RFDINPUT_H__
#define __RFDINPUT_H__

#include <pspctrl.h>
#include <main.h>

#ifdef __cplusplus
extern "C" {
#endif

//ID of thread for RFDIInputProcessor() Allows for clean exit.
SceUID RFDIThreadID;

//Pointer to X button event handler
void (*RFDI_X)();
//Pointer to O button event handler
void (*RFDI_O)();
//Pointer to Triangle button event handler
void (*RFDI_T)();
//Pointer to Square button event handler
void (*RFDI_S)();
//Pointer to Left Trigger button event handler
void (*RFDI_L)();
//Pointer to Right Trigger button event handler
void (*RFDI_R)();
//Pointer to D-Up button event handler
void (*RFDI_DU)();
//Pointer to D-Down button event handler
void (*RFDI_DD)();
//Pointer to D-Left button event handler
void (*RFDI_DL)();
//Pointer to D-Right button event handler
void (*RFDI_DR)();
//Pointer to Start button event handler
void (*RFDI_Start)();
//Pointer to Select button event handler
void (*RFDI_Select)();

//Function for thread which catches and passes input. Once setup it
runs until stopped.
void RFDIInputProcessor(SceSize args, void *argp);
//Setup thread for input handling which calls RFDIInputProcessor.
returns -1 if runtime error; else 0.
int RFDISetup();
//Stop input handling
void RFDIStop();
//Set handle function for button event
void RFDISetFunc(int RFDI_BUTTON_LETTER_, void (*functionPointer)());

/*
Create & start input handling thread
*/
int RFDISetup() {
RFDIThreadID = sceKernelCreateThread("RFDIMain", RFDIInputProcessor,
100, 0x10000, PSP_THREAD_ATTR_CLEAR_STACK, NULL);
sceKernelStartThread(RFDIThreadID, 0, 0);
}

/*
Pass detected input to functions if pointer is assigned and delay.
This is not a game input function. Continous press is irrelevant.
*/
void RFDIInputProcessor(SceSize args, void *argp) {
SceCtrlData input;
while (RFDIThreadID != 0) {
sceCtrlReadBufferPositive(&input, 1);
if (RFDI_X != 0)
if (input.Buttons & PSP_CTRL_CROSS) {
(*RFDI_X)();
sceKernelDelayThread(150000);
}
if (RFDI_O != 0)
if (input.Buttons & PSP_CTRL_CIRCLE) {
(*RFDI_O)();
sceKernelDelayThread(150000);
}
if (RFDI_T != 0)
if (input.Buttons & PSP_CTRL_TRIANGLE) {
(*RFDI_T)();
sceKernelDelayThread(150000);
}
if (RFDI_S != 0)
if (input.Buttons & PSP_CTRL_SQUARE) {
(*RFDI_S)();
sceKernelDelayThread(150000);
}
if (RFDI_L != 0)
if (input.Buttons & PSP_CTRL_LTRIGGER) {
(*RFDI_L)();
sceKernelDelayThread(150000);
}
if (RFDI_R != 0)
if (input.Buttons & PSP_CTRL_RTRIGGER) {
(*RFDI_R)();
sceKernelDelayThread(150000);
}
if (RFDI_DU != 0)
if (input.Buttons & PSP_CTRL_UP) {
(*RFDI_DU)();
sceKernelDelayThread(150000);
}
if (RFDI_DD != 0)
if (input.Buttons & PSP_CTRL_DOWN) {
(*RFDI_DD)();
sceKernelDelayThread(150000);
}
if (RFDI_DL != 0)
if (input.Buttons & PSP_CTRL_LEFT) {
(*RFDI_DL)();
sceKernelDelayThread(150000);
}
if (RFDI_DR != 0)
if (input.Buttons & PSP_CTRL_RIGHT) {
(*RFDI_DR)();
sceKernelDelayThread(150000);
}
if (RFDI_Start != 0)
if (input.Buttons & PSP_CTRL_START) {
(*RFDI_Start)();
sceKernelDelayThread(150000);
}
if (RFDI_Select != 0)
if (input.Buttons & PSP_CTRL_SELECT) {
(*RFDI_Select)();
sceKernelDelayThread(150000);
}
}
}

/*
Stop the input handling thread and NULL its pointer
*/
void RFDIStop() {
sceKernelTerminateDeleteThread(RFDIThreadID);
RFDIThreadID = 0;
}

/*
Assigns the appropriate function adress to it's button
Parameter 1: value from 'enum PspCtrlButton' in 'pspctrl.h'
Parameter 2: function to assign
Example: RFDISetFunc(PSP_CTRL_CROSS, &myCrossEventHandler);
*/
void RFDISetFunc(int num, void (*functionPointer)()) {
if ( num & PSP_CTRL_CROSS )
RFDI_X = functionPointer;
else if ( num & PSP_CTRL_CIRCLE )
RFDI_O = functionPointer;
else if ( num & PSP_CTRL_TRIANGLE )
RFDI_T = functionPointer;
else if ( num & PSP_CTRL_SQUARE )
RFDI_S = functionPointer;
else if ( num & PSP_CTRL_LTRIGGER )
RFDI_L = functionPointer;
else if ( num & PSP_CTRL_RTRIGGER )
RFDI_R = functionPointer;
else if ( num & PSP_CTRL_UP )
RFDI_DU = functionPointer;
else if ( num & PSP_CTRL_DOWN )
RFDI_DD = functionPointer;
else if ( num & PSP_CTRL_LEFT )
RFDI_DL = functionPointer;
else if ( num & PSP_CTRL_RIGHT )
RFDI_DR = functionPointer;
else if ( num & PSP_CTRL_START )
RFDI_Start = functionPointer;
else if ( num & PSP_CTRL_SELECT )
RFDI_Select = functionPointer;
}

#ifdef __cplusplus
}
#endif

#endif


Reply all
Reply to author
Forward
0 new messages