/*---------------------------------------------------------------------------* Project: Revolution SDK Extension File: demokpad.c Copyright 2006 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Log: demokpad.c,v $ Revision 1.11 2008/06/02 02:24:01 seiki_masashi Added support for KPADRead specification changes. Revision 1.10 2008/05/30 02:27:03 seiki_masashi Reverted code consequent upon reverted SDK code. Revision 1.8 2007/10/23 13:42:06 seiki_masashi Added REXDEMOGetAnyKPadHold. Revision 1.7 2007/06/16 06:31:07 hirose_kazuki Handled warnings. Revision 1.6 2007/06/16 06:17:36 hirose_kazuki Added GC Pad-related features. Revision 1.5 2006/09/05 10:56:12 yosizaki Added REXDEMOGetAnyKPadTrigger. Revision 1.4 2006/08/29 07:19:20 adachi_hiroaki Changed prefix. Cleaned up code elsewhere. Revision 1.3 2006/08/11 10:41:15 yasu Ignored the KPADRead return value. Revision 1.2 2006/08/10 12:09:47 adachi_hiroaki Changed the header file position. Revision 1.1 2006/08/09 13:28:49 yasu Added library for using KPAD. $NoKeywords: $ *---------------------------------------------------------------------------*/ #include #include #include #include "rexdemo/demokpad.h" #define SAMPLE_PADHEAP_SIZE ( 128 * 1024 ) static MEMHeapHandle samplePadHeap; static void* AllocFromPadHeap( u32 size ); static u8 FreeToPadHeap( void* ptr ); static u16 PadButtonPrev[PAD_MAX_CONTROLLERS]; KPADStatus REXDEMOKPadStatus[WPAD_MAX_CONTROLLERS]; PADStatus REXDEMOPadStatus[PAD_MAX_CONTROLLERS]; /*---------------------------------------------------------------------------* Name: REXDEMOKPadInit Description: Initialize PAD library and exported status Arguments: None Returns: None *---------------------------------------------------------------------------*/ void REXDEMOKPadInit(void) { void* heapAddress; int channel; /* Initialize heap for WPAD sampling */ heapAddress = OSGetMEM2ArenaLo(); OSSetMEM2ArenaLo( (void*)OSRoundUp32B( (u32)heapAddress + SAMPLE_PADHEAP_SIZE ) ); samplePadHeap = MEMCreateExpHeap( heapAddress, SAMPLE_PADHEAP_SIZE ); if( samplePadHeap == NULL ) { OSHalt( "Could not create heap.\n" ); } WPADRegisterAllocator( AllocFromPadHeap, FreeToPadHeap ); KPADInit(); while( KPADRead( WPAD_CHAN0, &REXDEMOKPadStatus[0], 1 ) > 0 ) {} while( KPADRead( WPAD_CHAN1, &REXDEMOKPadStatus[1], 1 ) > 0 ) {} while( KPADRead( WPAD_CHAN2, &REXDEMOKPadStatus[2], 1 ) > 0 ) {} while( KPADRead( WPAD_CHAN3, &REXDEMOKPadStatus[3], 1 ) > 0 ) {} /* Initialize GameCube pads */ (void)PADInit(); for ( channel = 0 ; channel < PAD_MAX_CONTROLLERS ; ++channel ) { PadButtonPrev[channel] = 0; } } /*---------------------------------------------------------------------------* Name: REXDEMOKPadRead Description: Calls KPADRead(). Arguments: None Returns: None *---------------------------------------------------------------------------*/ void REXDEMOKPadRead( void ) { int channel; (void)KPADRead( WPAD_CHAN0, &REXDEMOKPadStatus[0], 1 ); (void)KPADRead( WPAD_CHAN1, &REXDEMOKPadStatus[1], 1 ); (void)KPADRead( WPAD_CHAN2, &REXDEMOKPadStatus[2], 1 ); (void)KPADRead( WPAD_CHAN3, &REXDEMOKPadStatus[3], 1 ); for ( channel = 0 ; channel < PAD_MAX_CONTROLLERS ; ++channel ) { PadButtonPrev[channel] = REXDEMOPadStatus[channel].button; } (void)PADRead( REXDEMOPadStatus ); PADClamp( REXDEMOPadStatus ); } /*---------------------------------------------------------------------------* Name : REXDEMOGetAnyKPadTrigger Description : Gets the logical sum of the trigger inputs from all pad channels. Arguments : None. Returns : Trigger input logical sum *---------------------------------------------------------------------------*/ u32 REXDEMOGetAnyKPadTrigger( void ) { u32 retval = 0; int channel; for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel) { retval |= REXDEMOKPadStatus[channel].trig; } return retval; } /*---------------------------------------------------------------------------* Name : REXDEMOGetAnyMixedPadTrigger Description : Gets the logical sum of the trigger inputs from all pad channels. (including the GameCube controller port) Arguments : None. Returns : Trigger input logical sum *---------------------------------------------------------------------------*/ u32 REXDEMOGetAnyMixedPadTrigger( void ) { u32 retval = 0; int channel; for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel) { retval |= REXDEMOKPadStatus[channel].trig; } for (channel = 0; channel < PAD_MAX_CONTROLLERS; ++channel) { u16 padDown; padDown = PADButtonDown(PadButtonPrev[channel], REXDEMOPadStatus[channel].button); retval |= ((u32)padDown << 16); } return retval; } /*---------------------------------------------------------------------------* Name : REXDEMOGetAnyKPadHold Description : Gets the logical sum of the inputs from all pad channels. Arguments : None. Returns : Trigger input logical sum *---------------------------------------------------------------------------*/ u32 REXDEMOGetAnyKPadHold( void ) { u32 retval = 0; int channel; for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel) { retval |= REXDEMOKPadStatus[channel].hold; } return retval; } /*---------------------------------------------------------------------------* Name : REXDEMOGetAnyMixedPadHold Description : Gets the logical sum of the inputs from all pad channels. (including the GameCube controller port) Arguments : None. Returns : Trigger input logical sum *---------------------------------------------------------------------------*/ u32 REXDEMOGetAnyMixedPadHold( void ) { u32 retval = 0; int channel; for (channel = 0; channel < WPAD_MAX_CONTROLLERS; ++channel) { retval |= REXDEMOKPadStatus[channel].hold; } for (channel = 0; channel < PAD_MAX_CONTROLLERS; ++channel) { u16 padDown; padDown = REXDEMOPadStatus[channel].button; retval |= ((u32)padDown << 16); } return retval; } /*---------------------------------------------------------------------------* Name : AllocFromPadHeap Description : Dynamically allocates memory for the WPAD library. Arguments : size - Specifies the size of memory to be allocated. Returns : void* - Returns the starting address of the allocated memory. *---------------------------------------------------------------------------*/ static void* AllocFromPadHeap( u32 size ) { return MEMAllocFromExpHeap( samplePadHeap, size ); } /*---------------------------------------------------------------------------* Name : FreeToPadHeap Description : Deallocates memory dynamically allocated for the WPAD library. Arguments : ptr - Specifies the start address of the memory to be deallocated. Returns : u8 - Returns 0 if attempt to deallocate memory fails. *---------------------------------------------------------------------------*/ static u8 FreeToPadHeap( void* ptr ) { if( ptr == NULL ) { return 0; } MEMFreeToExpHeap( samplePadHeap, ptr ); return 1; }