Windows Port: Optimize input handling, reducing CPU usage when the emulator is idle. (Related to commit 8fb0d6d.)

This commit is contained in:
rogerman 2018-03-04 17:17:26 -08:00
parent 8fb0d6ddc5
commit 8d011f8344
2 changed files with 122 additions and 90 deletions

37
desmume/src/frontend/windows/inputdx.cpp Normal file → Executable file
View File

@ -2379,9 +2379,6 @@ bool S9xGetState (WORD KeyIdent)
if(KeyIdent == 0 || KeyIdent == 0xFF || KeyIdent == VK_ESCAPE) // if it's the 'disabled' key, it's never pressed
return true;
if(!allowBackgroundInput && MainWindow->getHWnd() != GetForegroundWindow())
return true;
if (KeyIdent & 0x8000) // if it's a joystick 'key':
{
int j = (KeyIdent >> 8) & 15;
@ -2439,10 +2436,12 @@ bool S9xGetState (WORD KeyIdent)
return ((gks & 0x80) == 0);
}
void S9xWinScanJoypads ()
void S9xWinScanJoypads(const bool willAcceptInput)
{
S9xUpdateJoyState();
if (willAcceptInput)
{
for (int J = 0; J < 8; J++)
{
if (Joypad[J].Enabled)
@ -2470,6 +2469,17 @@ void S9xWinScanJoypads ()
}
}
}
else
{
for (int J = 0; J < 8; J++)
{
if (Joypad[J].Enabled)
{
joypads[J] = 0x80000000;
}
}
}
}
//void S9xOldAutofireAndStuff ()
//{
@ -2706,9 +2716,10 @@ static void RunAntipodalRestriction(const buttonstruct<bool>& pad);
// and updates input-related state that needs to update even while paused.
void input_acquire()
{
const bool willAcceptInput = ( (MainWindow->getHWnd() == GetForegroundWindow()) || allowBackgroundInput );
u32 oldInput = joypads[0];
S9xWinScanJoypads();
S9xWinScanJoypads(willAcceptInput);
buttonstruct<bool> buttons = {};
buttons.R = (joypads[0] & RIGHT_MASK)!=0;
@ -2766,7 +2777,8 @@ void input_acquire()
buttons.Y, buttons.X, buttons.W, buttons.E,
buttons.G, buttons.F);
if (willAcceptInput)
{
// TODO: this part hasn't been revised yet,
// but guitarGrip_setKey should only request a change (like NDS_setPad does)
if (Guitar.Enabled)
@ -2805,6 +2817,19 @@ void input_acquire()
if (dec) nds.paddle -= 5;
}
}
else
{
if (Guitar.Enabled)
{
guitarGrip_setKey(false, false, false, false);
}
if (Piano.Enabled)
{
piano_setKey(false, false, false, false, false, false, false, false, false, false, false, false, false);
}
}
}
// only runs once per frame (always after input_acquire has been called at least once).
// applies transformations to the user's input,

View File

@ -597,18 +597,24 @@ static void InputTimer()
initialized = true;
}
int nloops = JOYSTICK?16:1;
const int nloops = (JOYSTICK) ? 16 : 1;
const HWND mainWindow = MainWindow->getHWnd();
const bool willAcceptInput = ( (mainWindow == GetForegroundWindow()) || allowBackgroundInput );
for (int j = 0; j < nloops; j++)
{
for (int z = 0; z < 256; z++) {
for (int z = 0; z < 256; z++)
{
int i = z | (JOYSTICK?0x8000:0);
i |= (j<<8);
int n = i&0xFFF;
bool active = !S9xGetState(i);
bool active = willAcceptInput && !S9xGetState(i);
if (active) {
if (active)
{
bool keyRepeat = (currentTime - joyState[n].firstPressedTime) >= (DWORD)KeyInDelayMSec;
if (!joyState[n].wasPressed || keyRepeat) {
if (!joyState[n].wasPressed || keyRepeat)
{
if (!joyState[n].wasPressed)
joyState[n].firstPressedTime = currentTime;
joyState[n].lastPressedTime = currentTime;
@ -616,16 +622,17 @@ static void InputTimer()
joyState[n].repeatCount++;
int mods = GetInitialModifiers(i);
WPARAM wparam = i | (mods << 16) | (j<<8);
PostMessage(MainWindow->getHWnd(), WM_CUSTKEYDOWN, wparam,(LPARAM)(joyState[n].repeatCount | (joyState[n].wasPressed ? 0x40000000 : 0)));
PostMessage(mainWindow, WM_CUSTKEYDOWN, wparam,(LPARAM)(joyState[n].repeatCount | (joyState[n].wasPressed ? 0x40000000 : 0)));
}
}
else {
else
{
joyState[n].repeatCount = 1;
if (joyState[n].wasPressed)
{
int mods = GetInitialModifiers(i);
WPARAM wparam = i | (mods << 16) | (j<<8);
PostMessage(MainWindow->getHWnd(), WM_CUSTKEYUP, wparam,(LPARAM)(joyState[n].repeatCount | (joyState[n].wasPressed ? 0x40000000 : 0)));
PostMessage(mainWindow, WM_CUSTKEYUP, wparam,(LPARAM)(joyState[n].repeatCount | (joyState[n].wasPressed ? 0x40000000 : 0)));
}
}
joyState[n].wasPressed = active;