From 9e8a66a0060df692d0c388f86fdb6f04f2268543 Mon Sep 17 00:00:00 2001 From: gocha Date: Mon, 28 Sep 2009 04:15:07 +0000 Subject: [PATCH] win: upgraded hotkey code (ported from snes9x-rr) --- desmume/src/windows/hotkey.cpp | 104 ++++++++++----------- desmume/src/windows/hotkey.h | 19 ++-- desmume/src/windows/main.cpp | 163 ++++++++++++++++----------------- 3 files changed, 140 insertions(+), 146 deletions(-) diff --git a/desmume/src/windows/hotkey.cpp b/desmume/src/windows/hotkey.cpp index 15c14dd55..4ee8704b2 100644 --- a/desmume/src/windows/hotkey.cpp +++ b/desmume/src/windows/hotkey.cpp @@ -83,8 +83,8 @@ void CopyCustomKeys (SCustomKeys *dst, const SCustomKeys *src) //====================================================================================== //=====================================HANDLERS========================================= //====================================================================================== -void HK_OpenROM(int) {OpenFile();} -void HK_PrintScreen(int param) +void HK_OpenROM(int, bool justPressed) {OpenFile();} +void HK_PrintScreen(int param, bool justPressed) { char outFilename[MAX_PATH]; @@ -130,9 +130,9 @@ void HK_PrintScreen(int param) NDS_WriteBMP(filename.c_str()); } -void HK_StateSaveSlot(int num) +void HK_StateSaveSlot(int num, bool justPressed) { - if (romloaded) + if (romloaded && justPressed) { if (!paused) { @@ -147,9 +147,9 @@ void HK_StateSaveSlot(int num) } } -void HK_StateLoadSlot(int num) +void HK_StateLoadSlot(int num, bool justPressed) { - if (romloaded) + if (romloaded && justPressed) { BOOL wasPaused = paused; NDS_Pause(); @@ -166,7 +166,7 @@ void HK_StateLoadSlot(int num) } } -void HK_StateSetSlot(int num) +void HK_StateSetSlot(int num, bool justPressed) { if (romloaded) { @@ -175,23 +175,23 @@ void HK_StateSetSlot(int num) } } -void HK_StateQuickSaveSlot(int) +void HK_StateQuickSaveSlot(int, bool justPressed) { - HK_StateSaveSlot(lastSaveState); + HK_StateSaveSlot(lastSaveState, justPressed); } -void HK_StateQuickLoadSlot(int) +void HK_StateQuickLoadSlot(int, bool justPressed) { - HK_StateLoadSlot(lastSaveState); + HK_StateLoadSlot(lastSaveState, justPressed); } -void HK_MicrophoneKeyDown(int) { NDS_setMic(1); } +void HK_MicrophoneKeyDown(int, bool justPressed) { NDS_setMic(1); } void HK_MicrophoneKeyUp(int) { NDS_setMic(0); } -void HK_AutoHoldKeyDown(int) {AutoHoldPressed = true;} +void HK_AutoHoldKeyDown(int, bool justPressed) {AutoHoldPressed = true;} void HK_AutoHoldKeyUp(int) {AutoHoldPressed = false;} -void HK_StylusAutoHoldKeyDown(int) { +void HK_StylusAutoHoldKeyDown(int, bool justPressed) { StylusAutoHoldPressed = !StylusAutoHoldPressed; if (StylusAutoHoldPressed) NDS_setTouchPos(winLastTouch.x, winLastTouch.y); @@ -199,29 +199,29 @@ void HK_StylusAutoHoldKeyDown(int) { NDS_releaseTouch(); } -void HK_AutoHoldClearKeyDown(int) { +void HK_AutoHoldClearKeyDown(int, bool justPressed) { ClearAutoHold(); StylusAutoHoldPressed = false; if (!userTouchesScreen) NDS_releaseTouch(); } -void HK_Reset(int) {ResetGame();} +void HK_Reset(int, bool justPressed) {ResetGame();} -void HK_RecordAVI(int) { if (AVI_IsRecording()) AviEnd(); else AviRecordTo(); } -void HK_RecordWAV(int) { if (WAV_IsRecording()) WavEnd(); else WavRecordTo(); } +void HK_RecordAVI(int, bool justPressed) { if (AVI_IsRecording()) AviEnd(); else AviRecordTo(); } +void HK_RecordWAV(int, bool justPressed) { if (WAV_IsRecording()) WavEnd(); else WavRecordTo(); } -void HK_ToggleFrame(int) {CommonSettings.hud.FrameCounterDisplay ^= true;} -void HK_ToggleFPS(int) {CommonSettings.hud.FpsDisplay ^= true;} -void HK_ToggleInput(int) {CommonSettings.hud.ShowInputDisplay ^= true;} -void HK_ToggleLag(int) {CommonSettings.hud.ShowLagFrameCounter ^= true;} -void HK_ResetLagCounter(int) { +void HK_ToggleFrame(int, bool justPressed) {CommonSettings.hud.FrameCounterDisplay ^= true;} +void HK_ToggleFPS(int, bool justPressed) {CommonSettings.hud.FpsDisplay ^= true;} +void HK_ToggleInput(int, bool justPressed) {CommonSettings.hud.ShowInputDisplay ^= true;} +void HK_ToggleLag(int, bool justPressed) {CommonSettings.hud.ShowLagFrameCounter ^= true;} +void HK_ResetLagCounter(int, bool justPressed) { lagframecounter=0; LagFrameFlag=0; lastLag=0; TotalLagFrames=0; } -void HK_ToggleReadOnly(int) { +void HK_ToggleReadOnly(int, bool justPressed) { movie_readonly ^= true; if(movie_readonly) osd->addLine("Read Only"); @@ -229,7 +229,7 @@ void HK_ToggleReadOnly(int) { osd->addLine("Read+Write"); } -void HK_PlayMovie(int) +void HK_PlayMovie(int, bool justPressed) { if (romloaded) { @@ -241,11 +241,11 @@ void HK_PlayMovie(int) bool rewinding = false; -void HK_RewindKeyDown(int) {rewinding = true;} +void HK_RewindKeyDown(int, bool justPressed) {rewinding = true;} void HK_RewindKeyUp(int){rewinding = false;} -void HK_RecordMovie(int) +void HK_RecordMovie(int, bool justPressed) { if (romloaded) { @@ -255,68 +255,68 @@ void HK_RecordMovie(int) } } -void HK_StopMovie(int) +void HK_StopMovie(int, bool justPressed) { FCEUI_StopMovie(); } -void HK_NewLuaScriptDown(int) +void HK_NewLuaScriptDown(int, bool justPressed) { SendMessage(MainWindow->getHWnd(), WM_COMMAND, IDC_NEW_LUA_SCRIPT, 0); } -void HK_CloseLuaScriptsDown(int) +void HK_CloseLuaScriptsDown(int, bool justPressed) { SendMessage(MainWindow->getHWnd(), WM_COMMAND, IDC_CLOSE_LUA_SCRIPTS, 0); } -void HK_MostRecentLuaScriptDown(int) +void HK_MostRecentLuaScriptDown(int, bool justPressed) { SendMessage(MainWindow->getHWnd(), WM_COMMAND, IDD_LUARECENT_RESERVE_START, 0); } -void HK_TurboRightKeyDown(int) { Turbo.R = true; } +void HK_TurboRightKeyDown(int, bool justPressed) { Turbo.R = true; } void HK_TurboRightKeyUp(int) { Turbo.R = false; } -void HK_TurboLeftKeyDown(int) { Turbo.L = true; } +void HK_TurboLeftKeyDown(int, bool justPressed) { Turbo.L = true; } void HK_TurboLeftKeyUp(int) { Turbo.L = false; } -void HK_TurboRKeyDown(int) { Turbo.E = true; } +void HK_TurboRKeyDown(int, bool justPressed) { Turbo.E = true; } void HK_TurboRKeyUp(int) { Turbo.E = false; } -void HK_TurboLKeyDown(int) { Turbo.W = true; } +void HK_TurboLKeyDown(int, bool justPressed) { Turbo.W = true; } void HK_TurboLKeyUp(int) { Turbo.W = false; } -void HK_TurboDownKeyDown(int) { Turbo.D = true; } +void HK_TurboDownKeyDown(int, bool justPressed) { Turbo.D = true; } void HK_TurboDownKeyUp(int) { Turbo.D = false; } -void HK_TurboUpKeyDown(int) { Turbo.U = true; } +void HK_TurboUpKeyDown(int, bool justPressed) { Turbo.U = true; } void HK_TurboUpKeyUp(int) { Turbo.U = false; } -void HK_TurboBKeyDown(int) { Turbo.B = true; } +void HK_TurboBKeyDown(int, bool justPressed) { Turbo.B = true; } void HK_TurboBKeyUp(int) { Turbo.B = false; } -void HK_TurboAKeyDown(int) { Turbo.A = true; } +void HK_TurboAKeyDown(int, bool justPressed) { Turbo.A = true; } void HK_TurboAKeyUp(int) { Turbo.A = false; } -void HK_TurboXKeyDown(int) { Turbo.X = true; } +void HK_TurboXKeyDown(int, bool justPressed) { Turbo.X = true; } void HK_TurboXKeyUp(int) { Turbo.X = false; } -void HK_TurboYKeyDown(int) { Turbo.Y = true; } +void HK_TurboYKeyDown(int, bool justPressed) { Turbo.Y = true; } void HK_TurboYKeyUp(int) { Turbo.Y = false; } -void HK_TurboStartKeyDown(int) { Turbo.S = true; } +void HK_TurboStartKeyDown(int, bool justPressed) { Turbo.S = true; } void HK_TurboStartKeyUp(int) { Turbo.S = false; } -void HK_TurboSelectKeyDown(int) { Turbo.T = true; } +void HK_TurboSelectKeyDown(int, bool justPressed) { Turbo.T = true; } void HK_TurboSelectKeyUp(int) { Turbo.T = false; } -void HK_NextSaveSlot(int) { +void HK_NextSaveSlot(int, bool justPressed) { lastSaveState++; if(lastSaveState>9) lastSaveState=0; osd->addLine("State %i selected", lastSaveState); } -void HK_PreviousSaveSlot(int) { +void HK_PreviousSaveSlot(int, bool justPressed) { if(lastSaveState==0) lastSaveState=9; @@ -325,16 +325,16 @@ void HK_PreviousSaveSlot(int) { osd->addLine("State %i selected", lastSaveState); } -void HK_Pause(int) { Pause(); } -void HK_FastForwardToggle(int) { FastForward ^=1; } -void HK_FastForwardKeyDown(int) { FastForward = 1; } +void HK_Pause(int, bool justPressed) { Pause(); } +void HK_FastForwardToggle(int, bool justPressed) { FastForward ^=1; } +void HK_FastForwardKeyDown(int, bool justPressed) { FastForward = 1; } void HK_FastForwardKeyUp(int) { FastForward = 0; } -void HK_IncreaseSpeed(int) { IncreaseSpeed(); } -void HK_DecreaseSpeed(int) { DecreaseSpeed(); } -void HK_FrameAdvanceKeyDown(int) { FrameAdvance(true); } +void HK_IncreaseSpeed(int, bool justPressed) { IncreaseSpeed(); } +void HK_DecreaseSpeed(int, bool justPressed) { DecreaseSpeed(); } +void HK_FrameAdvanceKeyDown(int, bool justPressed) { FrameAdvance(true); } void HK_FrameAdvanceKeyUp(int) { FrameAdvance(false); } -void HK_ToggleRasterizer(int) { +void HK_ToggleRasterizer(int, bool justPressed) { if(cur3DCore == GPU3D_OPENGL) cur3DCore = GPU3D_SWRAST; else cur3DCore = GPU3D_OPENGL; diff --git a/desmume/src/windows/hotkey.h b/desmume/src/windows/hotkey.h index 03cf0953b..93271dfc3 100644 --- a/desmume/src/windows/hotkey.h +++ b/desmume/src/windows/hotkey.h @@ -48,11 +48,12 @@ static LPCTSTR hotkeyPageTitle[] = { struct SCustomKey { - typedef void (*THandler) (int param); + typedef void (*THandlerDown) (int param, bool justPressed); + typedef void (*THandlerUp) (int param); WORD key; WORD modifiers; - THandler handleKeyDown; - THandler handleKeyUp; + THandlerDown handleKeyDown; + THandlerUp handleKeyUp; HotkeyPage page; std::wstring name; const char* code; @@ -106,12 +107,12 @@ void InitCustomKeys (SCustomKeys *keys); int GetModifiers(int key); //HOTKEY HANDLERS -void HK_PrintScreen(int); -void HK_StateSaveSlot(int); -void HK_StateLoadSlot(int); -void HK_StateSetSlot(int); -void HK_Pause(int); -void HK_FastForward(int); +void HK_PrintScreen(int, bool); +void HK_StateSaveSlot(int, bool); +void HK_StateLoadSlot(int, bool); +void HK_StateSetSlot(int, bool); +void HK_Pause(int, bool); +void HK_FastForward(int, bool); extern bool AutoHoldPressed; diff --git a/desmume/src/windows/main.cpp b/desmume/src/windows/main.cpp index 7d162c032..cd42bef4c 100644 --- a/desmume/src/windows/main.cpp +++ b/desmume/src/windows/main.cpp @@ -324,87 +324,54 @@ LRESULT CALLBACK WifiSettingsDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM // const char *cflash_disk_image_file; //}; -int KeyInDelayInCount=30; - -static int lastTime = timeGetTime(); -int repeattime; +static int KeyInDelayMSec = 0; +static int KeyInRepeatMSec = 16; VOID CALLBACK KeyInputTimer( UINT idEvent, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) { - // -// if(timeGetTime() - lastTime > 5) -// { - bool S9xGetState (WORD KeyIdent); + bool S9xGetState (WORD KeyIdent); - /* if(GUI.JoystickHotkeys) - { - static uint32 joyState [256]; + static DWORD lastTime = timeGetTime(); + DWORD currentTime = timeGetTime(); - for(int i = 0 ; i < 255 ; i++) - { - bool active = !S9xGetState(0x8000|i); + static struct JoyState { + bool wasPressed; + DWORD firstPressedTime; + DWORD lastPressedTime; + WORD repeatCount; + } joyState [256]; + static bool initialized = false; - if(active) - { - if(joyState[i] < ULONG_MAX) // 0xffffffffUL - joyState[i]++; - if(joyState[i] == 1 || joyState[i] >= (unsigned) KeyInDelayInCount) - PostMessage(GUI.hWnd, WM_CUSTKEYDOWN, (WPARAM)(0x8000|i),(LPARAM)(NULL)); + if(!initialized) { + for(int i = 0; i < 256; i++) { + joyState[i].wasPressed = false; + joyState[i].repeatCount = 1; } - else - { - if(joyState[i]) - { - joyState[i] = 0; - PostMessage(GUI.hWnd, WM_CUSTKEYUP, (WPARAM)(0x8000|i),(LPARAM)(NULL)); - } - } - } - }*/ - // if((!GUI.InactivePause || !Settings.ForcedPause) - // || (GUI.BackgroundInput || !(Settings.ForcedPause & (PAUSE_INACTIVE_WINDOW | PAUSE_WINDOW_ICONISED)))) - // { - static u32 joyState [256]; - static bool initialized = false; - if(!initialized) { - memset(joyState,0,sizeof(joyState)); - initialized = true; - } - for(int i = 0 ; i < 256 ; i++) - { - bool active = !S9xGetState(i); - if(active) - { - if(joyState[i] < ULONG_MAX) // 0xffffffffUL - joyState[i]++; - if(joyState[i] == 1 || joyState[i] == (unsigned) KeyInDelayInCount) { + initialized = true; + } + for (int i = 0; i < 256; i++) { + bool active = !S9xGetState(i); - //HEY HEY HEY! this only repeats once. this is what it needs to be like for frame advance. - //if anyone wants a different kind of repeat, then the whole setup will need to be redone - - //sort of fix the auto repeating - //TODO find something better - // repeattime++; - // if(repeattime % 10 == 0) { - //printf("trigger %d\n",i); - PostMessage(MainWindow->getHWnd(), WM_CUSTKEYDOWN, (WPARAM)(i),(LPARAM)(NULL)); - repeattime=0; - // } - } - } - else - { - if(joyState[i]) - { - joyState[i] = 0; - PostMessage(MainWindow->getHWnd(), WM_CUSTKEYUP, (WPARAM)(i),(LPARAM)(NULL)); - } + if (active) { + bool keyRepeat = (currentTime - joyState[i].firstPressedTime) >= (DWORD)KeyInDelayMSec; + if (!joyState[i].wasPressed || keyRepeat) { + if (!joyState[i].wasPressed) + joyState[i].firstPressedTime = currentTime; + joyState[i].lastPressedTime = currentTime; + if (keyRepeat && joyState[i].repeatCount < 0xffff) + joyState[i].repeatCount++; + PostMessage(MainWindow->getHWnd(), WM_CUSTKEYDOWN, (WPARAM)(i),(LPARAM)(joyState[i].repeatCount | (joyState[i].wasPressed ? 0x40000000 : 0))); } } - // } - // lastTime = timeGetTime(); -// } + else { + joyState[i].repeatCount = 1; + if (joyState[i].wasPressed) + PostMessage(MainWindow->getHWnd(), WM_CUSTKEYUP, (WPARAM)(i),(LPARAM)(joyState[i].repeatCount | (joyState[i].wasPressed ? 0x40000000 : 0))); + } + joyState[i].wasPressed = active; + } + lastTime = currentTime; } void ScaleScreen(float factor) @@ -2139,7 +2106,33 @@ int _main() MainWindow->checkMenu(frameskiprate + IDC_FRAMESKIP0, true); } - int KeyInRepeatMSec=20; + DWORD wmTimerRes; + TIMECAPS tc; + if (timeGetDevCaps(&tc, sizeof(TIMECAPS))== TIMERR_NOERROR) + { + wmTimerRes = std::min(std::max(tc.wPeriodMin, (UINT)1), tc.wPeriodMax); + timeBeginPeriod (wmTimerRes); + } + else + { + wmTimerRes = 5; + timeBeginPeriod (wmTimerRes); + } + + if (KeyInDelayMSec == 0) { + DWORD dwKeyboardDelay; + SystemParametersInfo(SPI_GETKEYBOARDDELAY, 0, &dwKeyboardDelay, 0); + KeyInDelayMSec = 250 * (dwKeyboardDelay + 1); + } + if (KeyInRepeatMSec == 0) { + DWORD dwKeyboardSpeed; + SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &dwKeyboardSpeed, 0); + KeyInRepeatMSec = (int)(1000.0/(((30.0-2.5)/31.0)*dwKeyboardSpeed+2.5)); + } + if (KeyInRepeatMSec < (int)wmTimerRes) + KeyInRepeatMSec = (int)wmTimerRes; + if (KeyInDelayMSec < KeyInRepeatMSec) + KeyInDelayMSec = KeyInRepeatMSec; hKeyInputTimer = timeSetEvent (KeyInRepeatMSec, 0, KeyInputTimer, 0, TIME_PERIODIC); @@ -2232,7 +2225,7 @@ int _main() if(cmdline.load_slot != 0) { - HK_StateLoadSlot(cmdline.load_slot); + HK_StateLoadSlot(cmdline.load_slot, true); } MainWindow->Show(SW_NORMAL); @@ -2841,7 +2834,7 @@ int HandleKeyMessage(WPARAM wParam, LPARAM lParam, int modifiers) SCustomKey *key = &CustomKeys.key(0); while (!IsLastCustomKey(key)) { if (wParam == key->key && modifiers == key->modifiers && key->handleKeyDown) { - key->handleKeyDown(key->param); + key->handleKeyDown(key->param, lParam & 0x40000000 ? false : true); hitHotKey = true; } key++; @@ -3547,7 +3540,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM case IDM_CLOSEROM: return CloseRom(),0; case IDM_PRINTSCREEN: - HK_PrintScreen(0); + HK_PrintScreen(0, true); return 0; case IDM_QUICK_PRINTSCREEN: { @@ -3743,38 +3736,38 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM case IDM_STATE_SAVE_F8: case IDM_STATE_SAVE_F9: case IDM_STATE_SAVE_F10: - HK_StateSaveSlot( abs(IDM_STATE_SAVE_F1 - LOWORD(wParam)) +1); + HK_StateSaveSlot( abs(IDM_STATE_SAVE_F1 - LOWORD(wParam)) +1, true); return 0; case IDM_STATE_LOAD_F1: - HK_StateLoadSlot(1); + HK_StateLoadSlot(1, true); return 0; case IDM_STATE_LOAD_F2: - HK_StateLoadSlot(2); + HK_StateLoadSlot(2, true); return 0; case IDM_STATE_LOAD_F3: - HK_StateLoadSlot(3); + HK_StateLoadSlot(3, true); return 0; case IDM_STATE_LOAD_F4: - HK_StateLoadSlot(4); + HK_StateLoadSlot(4, true); return 0; case IDM_STATE_LOAD_F5: - HK_StateLoadSlot(5); + HK_StateLoadSlot(5, true); return 0; case IDM_STATE_LOAD_F6: - HK_StateLoadSlot(6); + HK_StateLoadSlot(6, true); return 0; case IDM_STATE_LOAD_F7: - HK_StateLoadSlot(7); + HK_StateLoadSlot(7, true); return 0; case IDM_STATE_LOAD_F8: - HK_StateLoadSlot(8); + HK_StateLoadSlot(8, true); return 0; case IDM_STATE_LOAD_F9: - HK_StateLoadSlot(9); + HK_StateLoadSlot(9, true); return 0; case IDM_STATE_LOAD_F10: - HK_StateLoadSlot(10); + HK_StateLoadSlot(10, true); return 0; case IDM_IMPORTBACKUPMEMORY: {