2016-09-02 19:36:37 -04:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "ShortcutKeyHandler.h"
|
|
|
|
#include "EmulationSettings.h"
|
|
|
|
#include "ControlManager.h"
|
|
|
|
#include "VideoDecoder.h"
|
|
|
|
#include "VsControlManager.h"
|
|
|
|
#include "FDS.h"
|
|
|
|
#include "SaveStateManager.h"
|
2017-04-28 19:54:58 -04:00
|
|
|
#include "RewindManager.h"
|
2016-09-02 19:36:37 -04:00
|
|
|
|
|
|
|
ShortcutKeyHandler::ShortcutKeyHandler()
|
|
|
|
{
|
2017-09-08 10:38:41 -04:00
|
|
|
_keySetIndex = 0;
|
2016-09-02 19:36:37 -04:00
|
|
|
_stopThread = false;
|
|
|
|
_thread = std::thread([=]() {
|
|
|
|
while(!_stopThread) {
|
2017-09-08 10:38:41 -04:00
|
|
|
ProcessKeys();
|
2016-09-02 19:36:37 -04:00
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(50));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ShortcutKeyHandler::~ShortcutKeyHandler()
|
|
|
|
{
|
|
|
|
_stopThread = true;
|
|
|
|
_thread.join();
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
bool ShortcutKeyHandler::IsKeyPressed(EmulatorShortcut shortcut)
|
2016-09-02 19:36:37 -04:00
|
|
|
{
|
2017-09-08 10:38:41 -04:00
|
|
|
KeyCombination comb = EmulationSettings::GetShortcutKey(shortcut, _keySetIndex);
|
2016-09-02 19:36:37 -04:00
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
int keyCount = (comb.Key1 ? 1 : 0) + (comb.Key2 ? 1 : 0) + (comb.Key3 ? 1 : 0);
|
|
|
|
|
|
|
|
if(keyCount == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_pressedKeys.size() != keyCount) {
|
|
|
|
//Only allow shortcuts that use as many keys as the number of keys pressed
|
|
|
|
//e.g: Needed to prevent Shift-F1 from triggering a shortcut for F1
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ControlManager::IsKeyPressed(comb.Key1) &&
|
|
|
|
(comb.Key2 == 0 || ControlManager::IsKeyPressed(comb.Key2)) &&
|
|
|
|
(comb.Key3 == 0 || ControlManager::IsKeyPressed(comb.Key3));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShortcutKeyHandler::DetectKeyPress(EmulatorShortcut shortcut)
|
|
|
|
{
|
|
|
|
if(IsKeyPressed(shortcut)) {
|
|
|
|
_keysDown[_keySetIndex].emplace(shortcut);
|
|
|
|
|
|
|
|
if(_prevKeysDown[_keySetIndex].find(shortcut) == _prevKeysDown[_keySetIndex].end()) {
|
2016-09-02 19:36:37 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
bool ShortcutKeyHandler::DetectKeyRelease(EmulatorShortcut shortcut)
|
2016-09-03 10:49:54 -04:00
|
|
|
{
|
2017-09-08 10:38:41 -04:00
|
|
|
if(!IsKeyPressed(shortcut)) {
|
|
|
|
if(_prevKeysDown[_keySetIndex].find(shortcut) != _prevKeysDown[_keySetIndex].end()) {
|
2016-09-03 10:49:54 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
void ShortcutKeyHandler::CheckMappedKeys()
|
2016-09-02 19:36:37 -04:00
|
|
|
{
|
2017-04-28 19:54:58 -04:00
|
|
|
bool isNetplayClient = GameClient::Connected();
|
|
|
|
bool isMovieActive = MovieManager::Playing() || MovieManager::Recording();
|
2017-08-12 18:58:38 -04:00
|
|
|
bool needConfirm = EmulationSettings::CheckFlag(ConfirmExitResetPower);
|
2017-04-28 19:54:58 -04:00
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
//Let the UI handle these shortcuts
|
|
|
|
for(uint64_t i = (uint64_t)EmulatorShortcut::SwitchDiskSide; i <= (uint64_t)EmulatorShortcut::OpenTraceLogger; i++) {
|
|
|
|
if(DetectKeyPress((EmulatorShortcut)i)) {
|
|
|
|
void* param = (void*)i;
|
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::ExecuteShortcut, param);
|
|
|
|
}
|
2016-09-11 08:29:34 -04:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::FastForward)) {
|
|
|
|
EmulationSettings::SetFlags(EmulationFlags::Turbo);
|
|
|
|
} else if(DetectKeyRelease(EmulatorShortcut::FastForward)) {
|
|
|
|
EmulationSettings::ClearFlags(EmulationFlags::Turbo);
|
2016-09-02 19:36:37 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:54:58 -04:00
|
|
|
if(VsControlManager::GetInstance() && !isNetplayClient && !isMovieActive) {
|
2016-09-03 10:49:54 -04:00
|
|
|
VsControlManager* manager = VsControlManager::GetInstance();
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::VsServiceButton)) {
|
2016-09-03 10:49:54 -04:00
|
|
|
manager->SetServiceButtonState(true);
|
|
|
|
}
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyRelease(EmulatorShortcut::VsServiceButton)) {
|
2016-09-03 10:49:54 -04:00
|
|
|
manager->SetServiceButtonState(false);
|
2016-09-02 19:36:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::InsertNextDisk) && !isNetplayClient && !isMovieActive) {
|
2016-09-02 19:36:37 -04:00
|
|
|
FDS::InsertNextDisk();
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::MoveToNextStateSlot)) {
|
2016-09-02 19:36:37 -04:00
|
|
|
SaveStateManager::MoveToNextSlot();
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::MoveToPreviousStateSlot)) {
|
2016-09-02 19:36:37 -04:00
|
|
|
SaveStateManager::MoveToPreviousSlot();
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::SaveState)) {
|
2016-09-02 19:36:37 -04:00
|
|
|
SaveStateManager::SaveState();
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::LoadState) && !isNetplayClient) {
|
2016-09-02 19:36:37 -04:00
|
|
|
SaveStateManager::LoadState();
|
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::ToggleCheats) && !isNetplayClient && !isMovieActive) {
|
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::ExecuteShortcut, (void*)EmulatorShortcut::ToggleCheats);
|
2017-03-19 14:21:53 -04:00
|
|
|
}
|
2017-04-22 18:08:38 -04:00
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::ToggleAudio)) {
|
|
|
|
MessageManager::SendNotification(ConsoleNotificationType::ExecuteShortcut, (void*)EmulatorShortcut::ToggleAudio);
|
2017-05-25 20:31:20 -04:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
if(IsKeyPressed(EmulatorShortcut::RunSingleFrame)) {
|
2017-04-22 18:08:38 -04:00
|
|
|
if(EmulationSettings::CheckFlag(EmulationFlags::Paused)) {
|
|
|
|
EmulationSettings::ClearFlags(EmulationFlags::Paused);
|
|
|
|
Console::Pause();
|
|
|
|
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(50));
|
|
|
|
EmulationSettings::SetFlags(EmulationFlags::Paused);
|
|
|
|
Console::Resume();
|
|
|
|
} else {
|
|
|
|
EmulationSettings::SetFlags(EmulationFlags::Paused);
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 19:54:58 -04:00
|
|
|
|
|
|
|
if(!isNetplayClient && !isMovieActive && !EmulationSettings::CheckFlag(NsfPlayerEnabled)) {
|
2017-09-08 10:38:41 -04:00
|
|
|
if(DetectKeyPress(EmulatorShortcut::Rewind)) {
|
2017-04-28 19:54:58 -04:00
|
|
|
RewindManager::StartRewinding();
|
2017-09-08 10:38:41 -04:00
|
|
|
} else if(DetectKeyRelease(EmulatorShortcut::Rewind)) {
|
2017-04-28 19:54:58 -04:00
|
|
|
RewindManager::StopRewinding();
|
2017-09-08 10:38:41 -04:00
|
|
|
} else if(DetectKeyPress(EmulatorShortcut::RewindTenSecs)) {
|
2017-04-28 19:54:58 -04:00
|
|
|
RewindManager::RewindSeconds(10);
|
2017-09-08 10:38:41 -04:00
|
|
|
} else if(DetectKeyPress(EmulatorShortcut::RewindOneMin)) {
|
2017-04-28 19:54:58 -04:00
|
|
|
RewindManager::RewindSeconds(60);
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 19:36:37 -04:00
|
|
|
}
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
void ShortcutKeyHandler::ProcessKeys()
|
2016-09-02 19:36:37 -04:00
|
|
|
{
|
2017-09-08 10:38:41 -04:00
|
|
|
auto lock = _lock.AcquireSafe();
|
2016-09-02 19:36:37 -04:00
|
|
|
ControlManager::RefreshKeyState();
|
|
|
|
|
2017-09-08 10:38:41 -04:00
|
|
|
_pressedKeys = ControlManager::GetPressedKeys();
|
|
|
|
|
|
|
|
for(int i = 0; i < 2; i++) {
|
|
|
|
_keysDown[i].clear();
|
|
|
|
_keySetIndex = i;
|
|
|
|
CheckMappedKeys();
|
|
|
|
_prevKeysDown[i] = _keysDown[i];
|
|
|
|
}
|
2016-09-02 19:36:37 -04:00
|
|
|
}
|