| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/HotkeyScheduler.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cmath> |
| 8 | #include <thread> |
| 9 | |
| 10 | #include <fmt/format.h> |
| 11 | |
| 12 | #include <QApplication> |
| 13 | #include <QCoreApplication> |
| 14 | |
| 15 | #include "AudioCommon/AudioCommon.h" |
| 16 | |
| 17 | #include "Common/Config/Config.h" |
| 18 | #include "Common/Thread.h" |
| 19 | |
| 20 | #include "Core/AchievementManager.h" |
| 21 | #include "Core/Config/FreeLookSettings.h" |
| 22 | #include "Core/Config/GraphicsSettings.h" |
| 23 | #include "Core/Config/MainSettings.h" |
| 24 | #include "Core/Config/UISettings.h" |
| 25 | #include "Core/Core.h" |
| 26 | #include "Core/FreeLookManager.h" |
| 27 | #include "Core/HotkeyManager.h" |
| 28 | #include "Core/IOS/IOS.h" |
| 29 | #include "Core/State.h" |
| 30 | #include "Core/System.h" |
| 31 | #include "Core/WiiUtils.h" |
| 32 | |
| 33 | #ifdef HAS_LIBMGBA |
| 34 | #include "DolphinQt/GBAWidget.h" |
| 35 | #include "DolphinQt/QtUtils/QueueOnObject.h" |
| 36 | #endif |
| 37 | #include "DolphinQt/Settings.h" |
| 38 | |
| 39 | #include "InputCommon/ControlReference/ControlReference.h" |
| 40 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 41 | |
| 42 | #include "VideoCommon/OnScreenDisplay.h" |
| 43 | #include "VideoCommon/VideoConfig.h" |
| 44 | |
| 45 | constexpr const char* DUBOIS_ALGORITHM_SHADER = "dubois"; |
| 46 | |
| 47 | HotkeyScheduler::HotkeyScheduler() : m_stop_requested(false) |
| 48 | { |
| 49 | HotkeyManagerEmu::Enable(true); |
| 50 | } |
| 51 | |
| 52 | HotkeyScheduler::~HotkeyScheduler() |
| 53 | { |
| 54 | Stop(); |
| 55 | } |
| 56 | |
| 57 | void HotkeyScheduler::Start() |
| 58 | { |
| 59 | m_stop_requested.Set(false); |
| 60 | m_thread = std::thread(&HotkeyScheduler::Run, this); |
| 61 | } |
| 62 | |
| 63 | void HotkeyScheduler::Stop() |
| 64 | { |
| 65 | m_stop_requested.Set(true); |
| 66 | |
| 67 | if (m_thread.joinable()) |
| 68 | m_thread.join(); |
| 69 | } |
| 70 | |
| 71 | static bool IsHotkey(int id, bool held = false) |
| 72 | { |
| 73 | return HotkeyManagerEmu::IsPressed(id, held); |
| 74 | } |
| 75 | |
| 76 | static void HandleFrameStepHotkeys() |
| 77 | { |
| 78 | constexpr int MAX_FRAME_STEP_DELAY = 60; |
| 79 | constexpr int FRAME_STEP_DELAY = 30; |
| 80 | |
| 81 | static int frame_step_count = 0; |
| 82 | static int frame_step_delay = 1; |
| 83 | static int frame_step_delay_count = 0; |
| 84 | static bool frame_step_hold = false; |
| 85 | |
| 86 | if (IsHotkey(HK_FRAME_ADVANCE_INCREASE_SPEED)) |
| 87 | { |
| 88 | frame_step_delay = std::max(frame_step_delay - 1, 0); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | if (IsHotkey(HK_FRAME_ADVANCE_DECREASE_SPEED)) |
| 93 | { |
| 94 | frame_step_delay = std::min(frame_step_delay + 1, MAX_FRAME_STEP_DELAY); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (IsHotkey(HK_FRAME_ADVANCE_RESET_SPEED)) |
| 99 | { |
| 100 | frame_step_delay = 1; |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (IsHotkey(HK_FRAME_ADVANCE, true)) |
| 105 | { |
| 106 | if (frame_step_delay_count < frame_step_delay && frame_step_hold) |
| 107 | frame_step_delay_count++; |
| 108 | |
| 109 | if ((frame_step_count == 0 || frame_step_count == FRAME_STEP_DELAY) && !frame_step_hold) |
| 110 | { |
| 111 | if (frame_step_count > 0) |
| 112 | Settings::Instance().SetIsContinuouslyFrameStepping(true); |
| 113 | Core::QueueHostJob([](auto& system) { Core::DoFrameStep(system); }); |
| 114 | frame_step_hold = true; |
| 115 | } |
| 116 | |
| 117 | if (frame_step_count < FRAME_STEP_DELAY) |
| 118 | { |
| 119 | frame_step_count++; |
| 120 | frame_step_hold = false; |
| 121 | } |
| 122 | |
| 123 | if (frame_step_count == FRAME_STEP_DELAY && frame_step_hold && |
| 124 | frame_step_delay_count >= frame_step_delay) |
| 125 | { |
| 126 | frame_step_hold = false; |
| 127 | frame_step_delay_count = 0; |
| 128 | } |
| 129 | |
| 130 | return; |
| 131 | } |
| 132 | else if (frame_step_count > 0) |
| 133 | { |
| 134 | // Reset frame advance |
| 135 | frame_step_count = 0; |
| 136 | frame_step_hold = false; |
| 137 | frame_step_delay_count = 0; |
| 138 | Settings::Instance().SetIsContinuouslyFrameStepping(false); |
| 139 | emit Settings::Instance().EmulationStateChanged(Core::GetState(Core::System::GetInstance())); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void HotkeyScheduler::Run() |
| 144 | { |
| 145 | Common::SetCurrentThreadName("HotkeyScheduler"); |
| 146 | |
| 147 | while (!m_stop_requested.IsSet()) |
| 148 | { |
| 149 | Common::SleepCurrentThread(5); |
| 150 | |
| 151 | g_controller_interface.SetCurrentInputChannel(ciface::InputChannel::FreeLook); |
| 152 | g_controller_interface.UpdateInput(); |
| 153 | FreeLook::UpdateInput(); |
| 154 | |
| 155 | g_controller_interface.SetCurrentInputChannel(ciface::InputChannel::Host); |
| 156 | g_controller_interface.UpdateInput(); |
| 157 | |
| 158 | if (!HotkeyManagerEmu::IsEnabled()) |
| 159 | continue; |
| 160 | |
| 161 | Core::System& system = Core::System::GetInstance(); |
| 162 | if (Core::GetState(system) != Core::State::Stopping) |
| 163 | { |
| 164 | // Obey window focus (config permitting) before checking hotkeys. |
| 165 | Core::UpdateInputGate(Config::Get(Config::MAIN_FOCUSED_HOTKEYS)); |
| 166 | |
| 167 | HotkeyManagerEmu::GetStatus(false); |
| 168 | |
| 169 | // Everything else on the host thread (controller config dialog) should always get input. |
| 170 | ControlReference::SetInputGate(true); |
| 171 | |
| 172 | HotkeyManagerEmu::GetStatus(true); |
| 173 | |
| 174 | // Open |
| 175 | if (IsHotkey(HK_OPEN)) |
| 176 | emit Open(); |
| 177 | |
| 178 | // Refresh Game List |
| 179 | if (IsHotkey(HK_REFRESH_LIST)) |
| 180 | emit RefreshGameListHotkey(); |
| 181 | |
| 182 | // Recording |
| 183 | if (IsHotkey(HK_START_RECORDING)) |
| 184 | emit StartRecording(); |
| 185 | |
| 186 | // Exit |
| 187 | if (IsHotkey(HK_EXIT)) |
| 188 | emit ExitHotkey(); |
| 189 | |
| 190 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 191 | if (IsHotkey(HK_OPEN_ACHIEVEMENTS)) |
| 192 | emit OpenAchievements(); |
| 193 | #endif // USE_RETRO_ACHIEVEMENTS |
| 194 | |
| 195 | if (Core::IsUninitialized(system)) |
| 196 | { |
| 197 | // Only check for Play Recording hotkey when no game is running |
| 198 | if (IsHotkey(HK_PLAY_RECORDING)) |
| 199 | emit PlayRecording(); |
| 200 | |
| 201 | continue; |
| 202 | } |
| 203 | |
| 204 | // Disc |
| 205 | |
| 206 | if (IsHotkey(HK_EJECT_DISC)) |
| 207 | emit EjectDisc(); |
| 208 | |
| 209 | if (IsHotkey(HK_CHANGE_DISC)) |
| 210 | emit ChangeDisc(); |
| 211 | |
| 212 | // Fullscreen |
| 213 | if (IsHotkey(HK_FULLSCREEN)) |
| 214 | { |
| 215 | emit FullScreenHotkey(); |
| 216 | |
| 217 | // Prevent fullscreen from getting toggled too often |
| 218 | Common::SleepCurrentThread(100); |
| 219 | } |
| 220 | |
| 221 | // Pause and Unpause |
| 222 | if (IsHotkey(HK_PLAY_PAUSE)) |
| 223 | emit TogglePauseHotkey(); |
| 224 | |
| 225 | // Stop |
| 226 | if (IsHotkey(HK_STOP)) |
| 227 | emit StopHotkey(); |
| 228 | |
| 229 | // Reset |
| 230 | if (IsHotkey(HK_RESET)) |
| 231 | emit ResetHotkey(); |
| 232 | |
| 233 | // Frame advance |
| 234 | HandleFrameStepHotkeys(); |
| 235 | |
| 236 | // Screenshot |
| 237 | if (IsHotkey(HK_SCREENSHOT)) |
| 238 | emit ScreenShotHotkey(); |
| 239 | |
| 240 | // Unlock Cursor |
| 241 | if (IsHotkey(HK_UNLOCK_CURSOR)) |
| 242 | emit UnlockCursor(); |
| 243 | |
| 244 | if (IsHotkey(HK_CENTER_MOUSE, true)) |
| 245 | g_controller_interface.SetMouseCenteringRequested(true); |
| 246 | |
| 247 | auto& settings = Settings::Instance(); |
| 248 | |
| 249 | // Toggle Chat |
| 250 | if (IsHotkey(HK_ACTIVATE_CHAT)) |
| 251 | emit ActivateChat(); |
| 252 | |
| 253 | if (IsHotkey(HK_REQUEST_GOLF_CONTROL)) |
| 254 | emit RequestGolfControl(); |
| 255 | |
| 256 | if (IsHotkey(HK_EXPORT_RECORDING)) |
| 257 | emit ExportRecording(); |
| 258 | |
| 259 | if (IsHotkey(HK_READ_ONLY_MODE)) |
| 260 | emit ToggleReadOnlyMode(); |
| 261 | |
| 262 | // Wiimote |
| 263 | if (auto bt = WiiUtils::GetBluetoothRealDevice()) |
| 264 | bt->UpdateSyncButtonState(IsHotkey(HK_TRIGGER_SYNC_BUTTON, true)); |
| 265 | |
| 266 | if (Config::IsDebuggingEnabled()) |
| 267 | { |
| 268 | CheckDebuggingHotkeys(); |
| 269 | } |
| 270 | |
| 271 | // TODO: HK_MBP_ADD |
| 272 | |
| 273 | if (Core::System::GetInstance().IsWii()) |
| 274 | { |
| 275 | if (IsHotkey(HK_WIIMOTE1_CONNECT)) |
| 276 | emit ConnectWiiRemote(0); |
| 277 | if (IsHotkey(HK_WIIMOTE2_CONNECT)) |
| 278 | emit ConnectWiiRemote(1); |
| 279 | if (IsHotkey(HK_WIIMOTE3_CONNECT)) |
| 280 | emit ConnectWiiRemote(2); |
| 281 | if (IsHotkey(HK_WIIMOTE4_CONNECT)) |
| 282 | emit ConnectWiiRemote(3); |
| 283 | if (IsHotkey(HK_BALANCEBOARD_CONNECT)) |
| 284 | emit ConnectWiiRemote(4); |
| 285 | |
| 286 | if (IsHotkey(HK_TOGGLE_SD_CARD)) |
| 287 | Settings::Instance().SetSDCardInserted(!Settings::Instance().IsSDCardInserted()); |
| 288 | |
| 289 | if (IsHotkey(HK_TOGGLE_USB_KEYBOARD)) |
| 290 | { |
| 291 | Settings::Instance().SetUSBKeyboardConnected( |
| 292 | !Settings::Instance().IsUSBKeyboardConnected()); |
| 293 | } |
| 294 | |
| 295 | if (IsHotkey(HK_TOGGLE_WII_SPEAK_MUTE)) |
| 296 | { |
| 297 | const bool muted = !Settings::Instance().IsWiiSpeakMuted(); |
| 298 | Settings::Instance().SetWiiSpeakMuted(muted); |
| 299 | OSD::AddMessage(muted ? "Wii Speak muted" : "Wii Speak unmuted"); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if (IsHotkey(HK_PREV_WIIMOTE_PROFILE_1)) |
| 304 | m_profile_cycler.PreviousWiimoteProfile(0); |
| 305 | else if (IsHotkey(HK_NEXT_WIIMOTE_PROFILE_1)) |
| 306 | m_profile_cycler.NextWiimoteProfile(0); |
| 307 | |
| 308 | if (IsHotkey(HK_PREV_WIIMOTE_PROFILE_2)) |
| 309 | m_profile_cycler.PreviousWiimoteProfile(1); |
| 310 | else if (IsHotkey(HK_NEXT_WIIMOTE_PROFILE_2)) |
| 311 | m_profile_cycler.NextWiimoteProfile(1); |
| 312 | |
| 313 | if (IsHotkey(HK_PREV_WIIMOTE_PROFILE_3)) |
| 314 | m_profile_cycler.PreviousWiimoteProfile(2); |
| 315 | else if (IsHotkey(HK_NEXT_WIIMOTE_PROFILE_3)) |
| 316 | m_profile_cycler.NextWiimoteProfile(2); |
| 317 | |
| 318 | if (IsHotkey(HK_PREV_WIIMOTE_PROFILE_4)) |
| 319 | m_profile_cycler.PreviousWiimoteProfile(3); |
| 320 | else if (IsHotkey(HK_NEXT_WIIMOTE_PROFILE_4)) |
| 321 | m_profile_cycler.NextWiimoteProfile(3); |
| 322 | |
| 323 | if (IsHotkey(HK_PREV_GAME_WIIMOTE_PROFILE_1)) |
| 324 | m_profile_cycler.PreviousWiimoteProfileForGame(0); |
| 325 | else if (IsHotkey(HK_NEXT_GAME_WIIMOTE_PROFILE_1)) |
| 326 | m_profile_cycler.NextWiimoteProfileForGame(0); |
| 327 | |
| 328 | if (IsHotkey(HK_PREV_GAME_WIIMOTE_PROFILE_2)) |
| 329 | m_profile_cycler.PreviousWiimoteProfileForGame(1); |
| 330 | else if (IsHotkey(HK_NEXT_GAME_WIIMOTE_PROFILE_2)) |
| 331 | m_profile_cycler.NextWiimoteProfileForGame(1); |
| 332 | |
| 333 | if (IsHotkey(HK_PREV_GAME_WIIMOTE_PROFILE_3)) |
| 334 | m_profile_cycler.PreviousWiimoteProfileForGame(2); |
| 335 | else if (IsHotkey(HK_NEXT_GAME_WIIMOTE_PROFILE_3)) |
| 336 | m_profile_cycler.NextWiimoteProfileForGame(2); |
| 337 | |
| 338 | if (IsHotkey(HK_PREV_GAME_WIIMOTE_PROFILE_4)) |
| 339 | m_profile_cycler.PreviousWiimoteProfileForGame(3); |
| 340 | else if (IsHotkey(HK_NEXT_GAME_WIIMOTE_PROFILE_4)) |
| 341 | m_profile_cycler.NextWiimoteProfileForGame(3); |
| 342 | |
| 343 | auto ShowVolume = [] { |
| 344 | OSD::AddMessage(std::string("Volume: ") + |
| 345 | (Config::Get(Config::MAIN_AUDIO_MUTED) ? |
| 346 | "Muted" : |
| 347 | std::to_string(Config::Get(Config::MAIN_AUDIO_VOLUME)) + "%")); |
| 348 | }; |
| 349 | |
| 350 | // Volume |
| 351 | if (IsHotkey(HK_VOLUME_DOWN)) |
| 352 | { |
| 353 | settings.DecreaseVolume(3); |
| 354 | ShowVolume(); |
| 355 | } |
| 356 | |
| 357 | if (IsHotkey(HK_VOLUME_UP)) |
| 358 | { |
| 359 | settings.IncreaseVolume(3); |
| 360 | ShowVolume(); |
| 361 | } |
| 362 | |
| 363 | if (IsHotkey(HK_VOLUME_TOGGLE_MUTE)) |
| 364 | { |
| 365 | AudioCommon::ToggleMuteVolume(system); |
| 366 | ShowVolume(); |
| 367 | } |
| 368 | |
| 369 | // Graphics |
| 370 | const auto efb_scale = Config::Get(Config::GFX_EFB_SCALE); |
| 371 | const auto ShowEFBScale = [](int new_efb_scale) { |
| 372 | switch (new_efb_scale) |
| 373 | { |
| 374 | case EFB_SCALE_AUTO_INTEGRAL: |
| 375 | OSD::AddMessage("Internal Resolution: Auto (integral)"); |
| 376 | break; |
| 377 | case 1: |
| 378 | OSD::AddMessage("Internal Resolution: Native"); |
| 379 | break; |
| 380 | default: |
| 381 | OSD::AddMessage(fmt::format("Internal Resolution: {}x", new_efb_scale)); |
| 382 | break; |
| 383 | } |
| 384 | }; |
| 385 | |
| 386 | if (IsHotkey(HK_INCREASE_IR)) |
| 387 | { |
| 388 | Config::SetCurrent(Config::GFX_EFB_SCALE, efb_scale + 1); |
| 389 | ShowEFBScale(efb_scale + 1); |
| 390 | } |
| 391 | if (IsHotkey(HK_DECREASE_IR)) |
| 392 | { |
| 393 | if (efb_scale > EFB_SCALE_AUTO_INTEGRAL) |
| 394 | { |
| 395 | Config::SetCurrent(Config::GFX_EFB_SCALE, efb_scale - 1); |
| 396 | ShowEFBScale(efb_scale - 1); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (IsHotkey(HK_TOGGLE_CROP_TO_ASPECT_RATIO)) |
| 401 | { |
| 402 | Config::SetCurrent(Config::GFX_CROP_TO_ASPECT_RATIO, |
| 403 | !Config::Get(Config::GFX_CROP_TO_ASPECT_RATIO)); |
| 404 | } |
| 405 | |
| 406 | if (IsHotkey(HK_TOGGLE_CROP_CUSTOM)) |
| 407 | Config::SetCurrent(Config::GFX_CROP_CUSTOM, !Config::Get(Config::GFX_CROP_CUSTOM)); |
| 408 | |
| 409 | if (IsHotkey(HK_TOGGLE_AR)) |
| 410 | { |
| 411 | const int aspect_ratio = (static_cast<int>(Config::Get(Config::GFX_ASPECT_RATIO)) + 1) & 3; |
| 412 | Config::SetCurrent(Config::GFX_ASPECT_RATIO, static_cast<AspectMode>(aspect_ratio)); |
| 413 | switch (static_cast<AspectMode>(aspect_ratio)) |
| 414 | { |
| 415 | case AspectMode::Stretch: |
| 416 | OSD::AddMessage("Stretch"); |
| 417 | break; |
| 418 | case AspectMode::ForceStandard: |
| 419 | OSD::AddMessage("Force 4:3"); |
| 420 | break; |
| 421 | case AspectMode::ForceWide: |
| 422 | OSD::AddMessage("Force 16:9"); |
| 423 | break; |
| 424 | case AspectMode::Custom: |
| 425 | OSD::AddMessage("Custom"); |
| 426 | break; |
| 427 | case AspectMode::CustomStretch: |
| 428 | OSD::AddMessage("Custom (Stretch)"); |
| 429 | break; |
| 430 | case AspectMode::Raw: |
| 431 | OSD::AddMessage("Raw (Square Pixels)"); |
| 432 | break; |
| 433 | case AspectMode::Auto: |
| 434 | default: |
| 435 | OSD::AddMessage("Auto"); |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (IsHotkey(HK_TOGGLE_SKIP_EFB_ACCESS)) |
| 441 | { |
| 442 | const bool new_value = !Config::Get(Config::GFX_HACK_EFB_ACCESS_ENABLE); |
| 443 | Config::SetCurrent(Config::GFX_HACK_EFB_ACCESS_ENABLE, new_value); |
| 444 | OSD::AddMessage(fmt::format("{} EFB Access from CPU", new_value ? "Skip" : "Don't skip")); |
| 445 | } |
| 446 | |
| 447 | if (IsHotkey(HK_TOGGLE_EFBCOPIES)) |
| 448 | { |
| 449 | const bool new_value = !Config::Get(Config::GFX_HACK_SKIP_EFB_COPY_TO_RAM); |
| 450 | Config::SetCurrent(Config::GFX_HACK_SKIP_EFB_COPY_TO_RAM, new_value); |
| 451 | OSD::AddMessage(fmt::format("Copy EFB: {}", new_value ? "to Texture" : "to RAM")); |
| 452 | } |
| 453 | |
| 454 | auto ShowXFBCopies = [] { |
| 455 | OSD::AddMessage(fmt::format( |
| 456 | "Copy XFB: {}{}", Config::Get(Config::GFX_HACK_IMMEDIATE_XFB) ? " (Immediate)" : "", |
| 457 | Config::Get(Config::GFX_HACK_SKIP_XFB_COPY_TO_RAM) ? "to Texture" : "to RAM")); |
| 458 | }; |
| 459 | |
| 460 | if (IsHotkey(HK_TOGGLE_XFBCOPIES)) |
| 461 | { |
| 462 | Config::SetCurrent(Config::GFX_HACK_SKIP_XFB_COPY_TO_RAM, |
| 463 | !Config::Get(Config::GFX_HACK_SKIP_XFB_COPY_TO_RAM)); |
| 464 | ShowXFBCopies(); |
| 465 | } |
| 466 | if (IsHotkey(HK_TOGGLE_IMMEDIATE_XFB)) |
| 467 | { |
| 468 | Config::SetCurrent(Config::GFX_HACK_IMMEDIATE_XFB, |
| 469 | !Config::Get(Config::GFX_HACK_IMMEDIATE_XFB)); |
| 470 | ShowXFBCopies(); |
| 471 | } |
| 472 | if (IsHotkey(HK_TOGGLE_FOG)) |
| 473 | { |
| 474 | const bool new_value = !Config::Get(Config::GFX_DISABLE_FOG); |
| 475 | Config::SetCurrent(Config::GFX_DISABLE_FOG, new_value); |
| 476 | OSD::AddMessage(fmt::format("Fog: {}", new_value ? "Enabled" : "Disabled")); |
| 477 | } |
| 478 | |
| 479 | if (IsHotkey(HK_TOGGLE_DUMPTEXTURES)) |
| 480 | { |
| 481 | const bool enable_dumping = !Config::Get(Config::GFX_DUMP_TEXTURES); |
| 482 | Config::SetCurrent(Config::GFX_DUMP_TEXTURES, enable_dumping); |
| 483 | OSD::AddMessage( |
| 484 | fmt::format("Texture Dumping {}", |
| 485 | enable_dumping ? "enabled. This will reduce performance." : "disabled."), |
| 486 | OSD::Duration::NORMAL); |
| 487 | } |
| 488 | |
| 489 | if (IsHotkey(HK_TOGGLE_TEXTURES)) |
| 490 | Config::SetCurrent(Config::GFX_HIRES_TEXTURES, !Config::Get(Config::GFX_HIRES_TEXTURES)); |
| 491 | |
| 492 | Core::SetIsThrottlerTempDisabled(IsHotkey(HK_TOGGLE_THROTTLE, true)); |
| 493 | |
| 494 | if (IsHotkey(HK_TOGGLE_THROTTLE, true) && !Config::Get(Config::MAIN_AUDIO_MUTED) && |
| 495 | Config::Get(Config::MAIN_AUDIO_MUTE_ON_DISABLED_SPEED_LIMIT)) |
| 496 | { |
| 497 | Config::SetCurrent(Config::MAIN_AUDIO_MUTED, true); |
| 498 | AudioCommon::UpdateSoundStream(system); |
| 499 | } |
| 500 | else if (!IsHotkey(HK_TOGGLE_THROTTLE, true) && Config::Get(Config::MAIN_AUDIO_MUTED) && |
| 501 | Config::GetActiveLayerForConfig(Config::MAIN_AUDIO_MUTED) == |
| 502 | Config::LayerType::CurrentRun) |
| 503 | { |
| 504 | Config::DeleteKey(Config::LayerType::CurrentRun, Config::MAIN_AUDIO_MUTED); |
| 505 | AudioCommon::UpdateSoundStream(system); |
| 506 | } |
| 507 | |
| 508 | auto ShowEmulationSpeed = [] { |
| 509 | const float emulation_speed = Config::Get(Config::MAIN_EMULATION_SPEED); |
| 510 | if (!AchievementManager::GetInstance().IsHardcoreModeActive() || |
| 511 | Config::Get(Config::MAIN_EMULATION_SPEED) >= 1.0f || |
| 512 | Config::Get(Config::MAIN_EMULATION_SPEED) <= 0.0f) |
| 513 | { |
| 514 | OSD::AddMessage(emulation_speed <= 0 ? "Speed Limit: Unlimited" : |
| 515 | fmt::format("Speed Limit: {}%", |
| 516 | std::lround(emulation_speed * 100.f))); |
| 517 | } |
| 518 | }; |
| 519 | |
| 520 | if (IsHotkey(HK_DECREASE_EMULATION_SPEED)) |
| 521 | { |
| 522 | auto speed = Config::Get(Config::MAIN_EMULATION_SPEED) - 0.1; |
| 523 | if (speed > 0) |
| 524 | { |
| 525 | speed = (speed >= 0.95 && speed <= 1.05) ? 1.0 : speed; |
| 526 | Config::SetCurrent(Config::MAIN_EMULATION_SPEED, speed); |
| 527 | } |
| 528 | ShowEmulationSpeed(); |
| 529 | } |
| 530 | |
| 531 | if (IsHotkey(HK_INCREASE_EMULATION_SPEED)) |
| 532 | { |
| 533 | auto speed = Config::Get(Config::MAIN_EMULATION_SPEED) + 0.1; |
| 534 | speed = (speed >= 0.95 && speed <= 1.05) ? 1.0 : speed; |
| 535 | Config::SetCurrent(Config::MAIN_EMULATION_SPEED, speed); |
| 536 | ShowEmulationSpeed(); |
| 537 | } |
| 538 | |
| 539 | // USB Device Emulation |
| 540 | if (IsHotkey(HK_SKYLANDERS_PORTAL)) |
| 541 | emit SkylandersPortalHotkey(); |
| 542 | |
| 543 | if (IsHotkey(HK_INFINITY_BASE)) |
| 544 | emit InfinityBaseHotkey(); |
| 545 | |
| 546 | // Slot Saving / Loading |
| 547 | if (IsHotkey(HK_SAVE_STATE_SLOT_SELECTED)) |
| 548 | emit StateSaveSlotHotkey(); |
| 549 | |
| 550 | if (IsHotkey(HK_LOAD_STATE_SLOT_SELECTED)) |
| 551 | emit StateLoadSlotHotkey(); |
| 552 | |
| 553 | if (IsHotkey(HK_INCREMENT_SELECTED_STATE_SLOT)) |
| 554 | emit IncrementSelectedStateSlotHotkey(); |
| 555 | |
| 556 | if (IsHotkey(HK_DECREMENT_SELECTED_STATE_SLOT)) |
| 557 | emit DecrementSelectedStateSlotHotkey(); |
| 558 | |
| 559 | // Stereoscopy |
| 560 | if (IsHotkey(HK_TOGGLE_STEREO_SIDE_BY_SIDE)) |
| 561 | { |
| 562 | if (Config::Get(Config::GFX_STEREO_MODE) != StereoMode::SideBySide) |
| 563 | { |
| 564 | // Disable post-processing shader, as stereoscopy itself is currently a shader |
| 565 | if (Config::Get(Config::GFX_ENHANCE_POST_SHADER) == DUBOIS_ALGORITHM_SHADER) |
| 566 | Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); |
| 567 | |
| 568 | Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::SideBySide); |
| 569 | } |
| 570 | else |
| 571 | { |
| 572 | Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if (IsHotkey(HK_TOGGLE_STEREO_TOP_AND_BOTTOM)) |
| 577 | { |
| 578 | if (Config::Get(Config::GFX_STEREO_MODE) != StereoMode::TopAndBottom) |
| 579 | { |
| 580 | // Disable post-processing shader, as stereoscopy itself is currently a shader |
| 581 | if (Config::Get(Config::GFX_ENHANCE_POST_SHADER) == DUBOIS_ALGORITHM_SHADER) |
| 582 | Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); |
| 583 | |
| 584 | Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::TopAndBottom); |
| 585 | } |
| 586 | else |
| 587 | { |
| 588 | Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (IsHotkey(HK_TOGGLE_STEREO_ANAGLYPH)) |
| 593 | { |
| 594 | if (Config::Get(Config::GFX_STEREO_MODE) != StereoMode::Anaglyph) |
| 595 | { |
| 596 | Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Anaglyph); |
| 597 | Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, DUBOIS_ALGORITHM_SHADER); |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); |
| 602 | Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | CheckGBAHotkeys(); |
| 607 | } |
| 608 | |
| 609 | const auto stereo_depth = Config::Get(Config::GFX_STEREO_DEPTH); |
| 610 | |
| 611 | if (IsHotkey(HK_DECREASE_DEPTH, true)) |
| 612 | Config::SetCurrent(Config::GFX_STEREO_DEPTH, std::max(stereo_depth - 1, 0.0f)); |
| 613 | |
| 614 | if (IsHotkey(HK_INCREASE_DEPTH, true)) |
| 615 | Config::SetCurrent(Config::GFX_STEREO_DEPTH, |
| 616 | std::min(stereo_depth + 1, Config::GFX_STEREO_DEPTH_MAXIMUM)); |
| 617 | |
| 618 | const auto stereo_convergence = Config::Get(Config::GFX_STEREO_CONVERGENCE); |
| 619 | |
| 620 | if (IsHotkey(HK_DECREASE_CONVERGENCE, true)) |
| 621 | Config::SetCurrent(Config::GFX_STEREO_CONVERGENCE, std::max(stereo_convergence - 5, 0.0f)); |
| 622 | |
| 623 | if (IsHotkey(HK_INCREASE_CONVERGENCE, true)) |
| 624 | Config::SetCurrent(Config::GFX_STEREO_CONVERGENCE, |
| 625 | std::min(stereo_convergence + 5, Config::GFX_STEREO_CONVERGENCE_MAXIMUM)); |
| 626 | |
| 627 | // Free Look |
| 628 | if (IsHotkey(HK_FREELOOK_TOGGLE)) |
| 629 | { |
| 630 | const bool new_value = !Config::Get(Config::FREE_LOOK_ENABLED); |
| 631 | Config::SetCurrent(Config::FREE_LOOK_ENABLED, new_value); |
| 632 | |
| 633 | const bool hardcore = AchievementManager::GetInstance().IsHardcoreModeActive(); |
| 634 | if (hardcore) |
| 635 | OSD::AddMessage("Free Look is Disabled in Hardcore Mode"); |
| 636 | else |
| 637 | OSD::AddMessage(fmt::format("Free Look: {}", new_value ? "Enabled" : "Disabled")); |
| 638 | } |
| 639 | |
| 640 | // Savestates |
| 641 | for (u32 i = 0; i < State::NUM_STATES; i++) |
| 642 | { |
| 643 | if (IsHotkey(HK_LOAD_STATE_SLOT_1 + i)) |
| 644 | emit StateLoadSlot(i + 1); |
| 645 | |
| 646 | if (IsHotkey(HK_SAVE_STATE_SLOT_1 + i)) |
| 647 | emit StateSaveSlot(i + 1); |
| 648 | |
| 649 | if (IsHotkey(HK_LOAD_LAST_STATE_1 + i)) |
| 650 | emit StateLoadLastSaved(i + 1); |
| 651 | |
| 652 | if (IsHotkey(HK_SELECT_STATE_SLOT_1 + i)) |
| 653 | emit SetStateSlotHotkey(i + 1); |
| 654 | } |
| 655 | |
| 656 | if (IsHotkey(HK_SAVE_FIRST_STATE)) |
| 657 | emit StateSaveOldest(); |
| 658 | |
| 659 | if (IsHotkey(HK_UNDO_LOAD_STATE)) |
| 660 | emit StateLoadUndo(); |
| 661 | |
| 662 | if (IsHotkey(HK_UNDO_SAVE_STATE)) |
| 663 | emit StateSaveUndo(); |
| 664 | |
| 665 | if (IsHotkey(HK_LOAD_STATE_FILE)) |
| 666 | emit StateLoadFile(); |
| 667 | |
| 668 | if (IsHotkey(HK_SAVE_STATE_FILE)) |
| 669 | emit StateSaveFile(); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | void HotkeyScheduler::CheckDebuggingHotkeys() |
| 674 | { |
| 675 | if (IsHotkey(HK_STEP)) |
| 676 | emit Step(); |
| 677 | |
| 678 | if (IsHotkey(HK_STEP_OVER)) |
| 679 | emit StepOver(); |
| 680 | |
| 681 | if (IsHotkey(HK_STEP_OUT)) |
| 682 | emit StepOut(); |
| 683 | |
| 684 | if (IsHotkey(HK_SKIP)) |
| 685 | emit Skip(); |
| 686 | |
| 687 | if (IsHotkey(HK_SHOW_PC)) |
| 688 | emit ShowPC(); |
| 689 | |
| 690 | if (IsHotkey(HK_SET_PC)) |
| 691 | emit Skip(); |
| 692 | |
| 693 | if (IsHotkey(HK_BP_TOGGLE)) |
| 694 | emit ToggleBreakpoint(); |
| 695 | |
| 696 | if (IsHotkey(HK_BP_ADD)) |
| 697 | emit AddBreakpoint(); |
| 698 | } |
| 699 | |
| 700 | void HotkeyScheduler::CheckGBAHotkeys() |
| 701 | { |
| 702 | #ifdef HAS_LIBMGBA |
| 703 | GBAWidget* gba_widget = qobject_cast<GBAWidget*>(QApplication::activeWindow()); |
| 704 | if (!gba_widget) |
| 705 | return; |
| 706 | |
| 707 | if (IsHotkey(HK_GBA_LOAD)) |
| 708 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->LoadROM(); }); |
| 709 | |
| 710 | if (IsHotkey(HK_GBA_UNLOAD)) |
| 711 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->UnloadROM(); }); |
| 712 | |
| 713 | if (IsHotkey(HK_GBA_RESET)) |
| 714 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->ResetCore(); }); |
| 715 | |
| 716 | if (IsHotkey(HK_GBA_VOLUME_DOWN)) |
| 717 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->VolumeDown(); }); |
| 718 | |
| 719 | if (IsHotkey(HK_GBA_VOLUME_UP)) |
| 720 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->VolumeUp(); }); |
| 721 | |
| 722 | if (IsHotkey(HK_GBA_TOGGLE_MUTE)) |
| 723 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->ToggleMute(); }); |
| 724 | |
| 725 | if (IsHotkey(HK_GBA_1X)) |
| 726 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->Resize(1); }); |
| 727 | |
| 728 | if (IsHotkey(HK_GBA_2X)) |
| 729 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->Resize(2); }); |
| 730 | |
| 731 | if (IsHotkey(HK_GBA_3X)) |
| 732 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->Resize(3); }); |
| 733 | |
| 734 | if (IsHotkey(HK_GBA_4X)) |
| 735 | QueueOnObject(gba_widget, [gba_widget] { gba_widget->Resize(4); }); |
| 736 | #endif |
| 737 | } |