Skip to content

Commit

Permalink
padscore: Simulate queue behaviour for KPADRead
Browse files Browse the repository at this point in the history
  • Loading branch information
Exzap committed Aug 15, 2024
1 parent c49296a commit b0bab27
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Cafe/OS/libs/padscore/padscore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
enum class KPAD_ERROR : sint32
{
NONE = 0,
NO_SAMPLE_DATA = -1,
NO_CONTROLLER = -2,
NOT_INITIALIZED = -5,
};
Expand Down Expand Up @@ -106,6 +107,9 @@ void padscoreExport_WPADProbe(PPCInterpreter_t* hCPU)
}
else
{
if(type)
*type = 253;

osLib_returnFromFunction(hCPU, WPAD_ERR_NO_CONTROLLER);
}
}
Expand Down Expand Up @@ -420,9 +424,12 @@ void padscoreExport_KPADSetConnectCallback(PPCInterpreter_t* hCPU)
osLib_returnFromFunction(hCPU, old_callback.GetMPTR());
}

uint64 g_kpadLastRead[InputManager::kMaxWPADControllers] = {0};
bool g_kpadIsInited = true;

sint32 _KPADRead(uint32 channel, KPADStatus_t* samplingBufs, uint32 length, betype<KPAD_ERROR>* errResult)
{

if (channel >= InputManager::kMaxWPADControllers)
{
debugBreakpoint();
Expand All @@ -446,6 +453,19 @@ sint32 _KPADRead(uint32 channel, KPADStatus_t* samplingBufs, uint32 length, bety
return 0;
}

// On console new input samples are only received every few ms and calling KPADRead(Ex) clears the internal queue regardless of length value
// thus calling KPADRead(Ex) again too soon on the same channel will result in no data being returned
// Games that depend on this: Affordable Space Adventures
uint64 currentTime = coreinit::OSGetTime();
uint64 timeDif = currentTime - g_kpadLastRead[channel];
if(length == 0 || timeDif < coreinit::EspressoTime::ConvertNsToTimerTicks(1000000))
{
if (errResult)
*errResult = KPAD_ERROR::NO_SAMPLE_DATA;
return 0;
}
g_kpadLastRead[channel] = currentTime;

memset(samplingBufs, 0x00, sizeof(KPADStatus_t));
samplingBufs->wpadErr = WPAD_ERR_NONE;
samplingBufs->data_format = controller->get_data_format();
Expand Down

0 comments on commit b0bab27

Please sign in to comment.