Windows Port: Optimize input handling, reducing CPU usage when the emulator is idle. (Related to commit 8fb0d6d.)
This commit is contained in:
parent
8fb0d6ddc5
commit
8d011f8344
|
@ -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
|
if(KeyIdent == 0 || KeyIdent == 0xFF || KeyIdent == VK_ESCAPE) // if it's the 'disabled' key, it's never pressed
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if(!allowBackgroundInput && MainWindow->getHWnd() != GetForegroundWindow())
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (KeyIdent & 0x8000) // if it's a joystick 'key':
|
if (KeyIdent & 0x8000) // if it's a joystick 'key':
|
||||||
{
|
{
|
||||||
int j = (KeyIdent >> 8) & 15;
|
int j = (KeyIdent >> 8) & 15;
|
||||||
|
@ -2439,13 +2436,15 @@ bool S9xGetState (WORD KeyIdent)
|
||||||
return ((gks & 0x80) == 0);
|
return ((gks & 0x80) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void S9xWinScanJoypads ()
|
void S9xWinScanJoypads(const bool willAcceptInput)
|
||||||
{
|
{
|
||||||
S9xUpdateJoyState();
|
S9xUpdateJoyState();
|
||||||
|
|
||||||
|
if (willAcceptInput)
|
||||||
|
{
|
||||||
for (int J = 0; J < 8; J++)
|
for (int J = 0; J < 8; J++)
|
||||||
{
|
{
|
||||||
if (Joypad [J].Enabled)
|
if (Joypad[J].Enabled)
|
||||||
{
|
{
|
||||||
int PadState = 0;
|
int PadState = 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].R)) ? R_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].R)) ? R_MASK : 0;
|
||||||
|
@ -2453,11 +2452,11 @@ void S9xWinScanJoypads ()
|
||||||
PadState |= (!S9xGetState(Joypad[J].X)) ? X_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].X)) ? X_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].A)) ? A_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].A)) ? A_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Right)) ? RIGHT_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Right)) ? RIGHT_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Right_Up)) ? RIGHT_MASK|UP_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Right_Up)) ? RIGHT_MASK | UP_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Right_Down)) ? RIGHT_MASK|DOWN_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Right_Down)) ? RIGHT_MASK | DOWN_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Left)) ? LEFT_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Left)) ? LEFT_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Left_Up)) ? LEFT_MASK|UP_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Left_Up)) ? LEFT_MASK | UP_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Left_Down)) ? LEFT_MASK|DOWN_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Left_Down)) ? LEFT_MASK | DOWN_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Down)) ? DOWN_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Down)) ? DOWN_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Up)) ? UP_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Up)) ? UP_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Start)) ? START_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Start)) ? START_MASK : 0;
|
||||||
|
@ -2466,7 +2465,18 @@ void S9xWinScanJoypads ()
|
||||||
PadState |= (!S9xGetState(Joypad[J].B)) ? B_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].B)) ? B_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Lid)) ? LID_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Lid)) ? LID_MASK : 0;
|
||||||
PadState |= (!S9xGetState(Joypad[J].Debug)) ? DEBUG_MASK : 0;
|
PadState |= (!S9xGetState(Joypad[J].Debug)) ? DEBUG_MASK : 0;
|
||||||
joypads [J] = PadState | 0x80000000;
|
joypads[J] = PadState | 0x80000000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int J = 0; J < 8; J++)
|
||||||
|
{
|
||||||
|
if (Joypad[J].Enabled)
|
||||||
|
{
|
||||||
|
joypads[J] = 0x80000000;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2706,9 +2716,10 @@ static void RunAntipodalRestriction(const buttonstruct<bool>& pad);
|
||||||
// and updates input-related state that needs to update even while paused.
|
// and updates input-related state that needs to update even while paused.
|
||||||
void input_acquire()
|
void input_acquire()
|
||||||
{
|
{
|
||||||
|
const bool willAcceptInput = ( (MainWindow->getHWnd() == GetForegroundWindow()) || allowBackgroundInput );
|
||||||
u32 oldInput = joypads[0];
|
u32 oldInput = joypads[0];
|
||||||
|
|
||||||
S9xWinScanJoypads();
|
S9xWinScanJoypads(willAcceptInput);
|
||||||
|
|
||||||
buttonstruct<bool> buttons = {};
|
buttonstruct<bool> buttons = {};
|
||||||
buttons.R = (joypads[0] & RIGHT_MASK)!=0;
|
buttons.R = (joypads[0] & RIGHT_MASK)!=0;
|
||||||
|
@ -2766,35 +2777,36 @@ void input_acquire()
|
||||||
buttons.Y, buttons.X, buttons.W, buttons.E,
|
buttons.Y, buttons.X, buttons.W, buttons.E,
|
||||||
buttons.G, buttons.F);
|
buttons.G, buttons.F);
|
||||||
|
|
||||||
|
if (willAcceptInput)
|
||||||
|
{
|
||||||
// TODO: this part hasn't been revised yet,
|
// TODO: this part hasn't been revised yet,
|
||||||
// but guitarGrip_setKey should only request a change (like NDS_setPad does)
|
// but guitarGrip_setKey should only request a change (like NDS_setPad does)
|
||||||
if (Guitar.Enabled)
|
if (Guitar.Enabled)
|
||||||
{
|
{
|
||||||
bool gG=!S9xGetState(Guitar.GREEN);
|
bool gG = !S9xGetState(Guitar.GREEN);
|
||||||
bool gR=!S9xGetState(Guitar.RED);
|
bool gR = !S9xGetState(Guitar.RED);
|
||||||
bool gY=!S9xGetState(Guitar.YELLOW);
|
bool gY = !S9xGetState(Guitar.YELLOW);
|
||||||
bool gB=!S9xGetState(Guitar.BLUE);
|
bool gB = !S9xGetState(Guitar.BLUE);
|
||||||
guitarGrip_setKey(gG, gR, gY, gB);
|
guitarGrip_setKey(gG, gR, gY, gB);
|
||||||
}
|
}
|
||||||
|
|
||||||
//etc. same as above
|
//etc. same as above
|
||||||
if (Piano.Enabled)
|
if (Piano.Enabled)
|
||||||
{
|
{
|
||||||
bool c=!S9xGetState(Piano.C);
|
bool c = !S9xGetState(Piano.C);
|
||||||
bool cs=!S9xGetState(Piano.CS);
|
bool cs = !S9xGetState(Piano.CS);
|
||||||
bool d=!S9xGetState(Piano.D);
|
bool d = !S9xGetState(Piano.D);
|
||||||
bool ds=!S9xGetState(Piano.DS);
|
bool ds = !S9xGetState(Piano.DS);
|
||||||
bool e=!S9xGetState(Piano.E);
|
bool e = !S9xGetState(Piano.E);
|
||||||
bool f=!S9xGetState(Piano.F);
|
bool f = !S9xGetState(Piano.F);
|
||||||
bool fs=!S9xGetState(Piano.FS);
|
bool fs = !S9xGetState(Piano.FS);
|
||||||
bool g=!S9xGetState(Piano.G);
|
bool g = !S9xGetState(Piano.G);
|
||||||
bool gs=!S9xGetState(Piano.GS);
|
bool gs = !S9xGetState(Piano.GS);
|
||||||
bool a=!S9xGetState(Piano.A);
|
bool a = !S9xGetState(Piano.A);
|
||||||
bool as=!S9xGetState(Piano.AS);
|
bool as = !S9xGetState(Piano.AS);
|
||||||
bool b=!S9xGetState(Piano.B);
|
bool b = !S9xGetState(Piano.B);
|
||||||
bool hic=!S9xGetState(Piano.HIC);
|
bool hic = !S9xGetState(Piano.HIC);
|
||||||
piano_setKey(c,cs,d,ds,e,f,fs,g,gs,a,as,b,hic);
|
piano_setKey(c, cs, d, ds, e, f, fs, g, gs, a, as, b, hic);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Paddle.Enabled)
|
if (Paddle.Enabled)
|
||||||
|
@ -2804,6 +2816,19 @@ void input_acquire()
|
||||||
if (inc) nds.paddle += 5;
|
if (inc) nds.paddle += 5;
|
||||||
if (dec) nds.paddle -= 5;
|
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).
|
// only runs once per frame (always after input_acquire has been called at least once).
|
||||||
|
|
|
@ -597,18 +597,24 @@ static void InputTimer()
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nloops = JOYSTICK?16:1;
|
const int nloops = (JOYSTICK) ? 16 : 1;
|
||||||
for(int j=0;j<nloops;j++)
|
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);
|
int i = z | (JOYSTICK?0x8000:0);
|
||||||
i |= (j<<8);
|
i |= (j<<8);
|
||||||
int n = i&0xFFF;
|
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;
|
bool keyRepeat = (currentTime - joyState[n].firstPressedTime) >= (DWORD)KeyInDelayMSec;
|
||||||
if (!joyState[n].wasPressed || keyRepeat) {
|
if (!joyState[n].wasPressed || keyRepeat)
|
||||||
|
{
|
||||||
if (!joyState[n].wasPressed)
|
if (!joyState[n].wasPressed)
|
||||||
joyState[n].firstPressedTime = currentTime;
|
joyState[n].firstPressedTime = currentTime;
|
||||||
joyState[n].lastPressedTime = currentTime;
|
joyState[n].lastPressedTime = currentTime;
|
||||||
|
@ -616,16 +622,17 @@ static void InputTimer()
|
||||||
joyState[n].repeatCount++;
|
joyState[n].repeatCount++;
|
||||||
int mods = GetInitialModifiers(i);
|
int mods = GetInitialModifiers(i);
|
||||||
WPARAM wparam = i | (mods << 16) | (j<<8);
|
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;
|
joyState[n].repeatCount = 1;
|
||||||
if (joyState[n].wasPressed)
|
if (joyState[n].wasPressed)
|
||||||
{
|
{
|
||||||
int mods = GetInitialModifiers(i);
|
int mods = GetInitialModifiers(i);
|
||||||
WPARAM wparam = i | (mods << 16) | (j<<8);
|
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;
|
joyState[n].wasPressed = active;
|
||||||
|
|
Loading…
Reference in New Issue