// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: CC-BY-NC-ND-4.0

#include "achievements.h"
#include "core.h"
#include "cpu_code_cache.h"
#include "cpu_core.h"
#include "cpu_pgxp.h"
#include "fullscreenui.h"
#include "gpu.h"
#include "gpu_hw_texture_cache.h"
#include "gte.h"
#include "host.h"
#include "imgui_overlays.h"
#include "settings.h"
#include "spu.h"
#include "system.h"
#include "video_presenter.h"
#include "video_thread.h"

#include "util/gpu_device.h"
#include "util/input_manager.h"
#include "util/postprocessing.h"
#include "util/translation.h"

#include "common/error.h"
#include "common/file_system.h"
#include "common/timer.h"

#include "IconsEmoji.h"
#include "IconsFontAwesome.h"
#include "fmt/format.h"

#include <cmath>

void Settings::SetDefaultHotkeyConfig(SettingsInterface& si)
{
  si.ClearSection("Hotkeys");

#ifndef __ANDROID__
  si.SetStringValue("Hotkeys", "FastForward", "Keyboard/Tab");
  si.SetStringValue("Hotkeys", "TogglePause", "Keyboard/Space");
  si.SetStringValue("Hotkeys", "Screenshot", "Keyboard/F10");
  si.SetStringValue("Hotkeys", "ToggleFullscreen", "Keyboard/F11");

  si.SetStringValue("Hotkeys", "OpenPauseMenu", "Keyboard/Escape");
  si.SetStringValue("Hotkeys", "LoadSelectedSaveState", "Keyboard/F1");
  si.SetStringValue("Hotkeys", "SaveSelectedSaveState", "Keyboard/F2");
  si.SetStringValue("Hotkeys", "SelectPreviousSaveStateSlot", "Keyboard/F3");
  si.SetStringValue("Hotkeys", "SelectNextSaveStateSlot", "Keyboard/F4");
#endif
}

static void HotkeyModifyResolutionScale(s32 increment)
{
  const u32 new_resolution_scale = std::clamp<u32>(
    static_cast<u32>(static_cast<s32>(g_settings.gpu_resolution_scale) + increment), 1, GPU::MAX_RESOLUTION_SCALE);
  if (new_resolution_scale == g_settings.gpu_resolution_scale)
    return;

  const Settings old_settings = g_settings;
  g_settings.gpu_resolution_scale = Truncate8(new_resolution_scale);

  if (System::IsValid())
  {
    VideoThread::UpdateSettings(true, false, false);
    System::ClearMemorySaveStates(true, false);
  }
}

static void HotkeyToggleOSD()
{
  g_settings.display_show_fps ^= Core::GetBoolSettingValue("Display", "ShowFPS", false);
  g_settings.display_show_speed ^= Core::GetBoolSettingValue("Display", "ShowSpeed", false);
  g_settings.display_show_gpu_stats ^= Core::GetBoolSettingValue("Display", "ShowGPUStatistics", false);
  g_settings.display_show_resolution ^= Core::GetBoolSettingValue("Display", "ShowResolution", false);
  g_settings.display_show_latency_stats ^= Core::GetBoolSettingValue("Display", "ShowLatencyStatistics", false);
  g_settings.display_show_cpu_usage ^= Core::GetBoolSettingValue("Display", "ShowCPU", false);
  g_settings.display_show_gpu_usage ^= Core::GetBoolSettingValue("Display", "ShowGPU", false);
  g_settings.display_show_frame_times ^= Core::GetBoolSettingValue("Display", "ShowFrameTimes", false);
  g_settings.display_show_status_indicators ^= Core::GetBoolSettingValue("Display", "ShowStatusIndicators", true);
  g_settings.display_show_inputs ^= Core::GetBoolSettingValue("Display", "ShowInputs", false);
  g_settings.display_show_enhancements ^= Core::GetBoolSettingValue("Display", "ShowEnhancements", false);

  VideoThread::UpdateSettings(true, false, false);
}

static bool HotkeyCheckRewindAvailability(bool enabled)
{
  if (g_settings.rewind_enable)
    return true;

  // don't do anything on key release
  if (!enabled)
    return false;

  // figure out why it's disabled...
  std::string summary;
  const bool rewind_actually_enabled = Core::GetBoolSettingValue("Main", "RewindEnable", false);
  if (rewind_actually_enabled)
  {
    if (g_settings.IsRunaheadEnabled())
    {
      summary = TRANSLATE_STR("OSDMessage", "Rewinding is disabled while runahead is enabled.");
    }
    else if (g_settings.disable_all_enhancements)
    {
      summary = TRANSLATE_STR("OSDMessage", "Rewinding is disabled while safe mode is enabled.");
    }
    else if (Achievements::IsHardcoreModeActive())
    {
      // because this is keypress-based, don't enable it on accept
      Achievements::ConfirmHardcoreModeDisableAsync("Rewinding", {});
      return false;
    }
  }
  else
  {
    summary = TRANSLATE_STR("OSDMessage", "Rewinding is not enabled.");
  }

  Host::AddIconOSDMessage(OSDMessageType::Warning, "SetRewindState", ICON_EMOJI_WARNING,
                          TRANSLATE_STR("OSDMessage", "Rewinding unavailable."), std::move(summary));
  return false;
}

static constexpr const HotkeyInfo s_hotkey_list[] = {

#ifndef __ANDROID__

  {"OpenPauseMenu", TRANSLATE_NOOP("Hotkeys", "Interface"), TRANSLATE_NOOP("Hotkeys", "Open Pause Menu"),
   [](s32 pressed) {
     if (!pressed && System::CanPauseSystem(true))
       FullscreenUI::OpenPauseMenu();
   }},

  {"OpenCheatsMenu", TRANSLATE_NOOP("Hotkeys", "Interface"), TRANSLATE_NOOP("Hotkeys", "Open Cheat Settings"),
   [](s32 pressed) {
     if (!pressed && System::CanPauseSystem(true))
       FullscreenUI::OpenCheatsMenu();
   }},

  {"OpenAchievements", TRANSLATE_NOOP("Hotkeys", "Interface"), TRANSLATE_NOOP("Hotkeys", "Open Achievement List"),
   [](s32 pressed) {
     if (!pressed && System::CanPauseSystem(true))
       FullscreenUI::OpenAchievementsWindow();
   }},

  {"OpenLeaderboards", TRANSLATE_NOOP("Hotkeys", "Interface"), TRANSLATE_NOOP("Hotkeys", "Open Leaderboard List"),
   [](s32 pressed) {
     if (!pressed && System::CanPauseSystem(true))
       FullscreenUI::OpenLeaderboardsWindow();
   }},

  {"Screenshot", TRANSLATE_NOOP("Hotkeys", "Interface"), TRANSLATE_NOOP("Hotkeys", "Save Screenshot"),
   [](s32 pressed) {
     if (!pressed)
       System::SaveScreenshot();
   }},

  {"TogglePause", TRANSLATE_NOOP("Hotkeys", "Interface"), TRANSLATE_NOOP("Hotkeys", "Toggle Pause"),
   [](s32 pressed) {
     if (!pressed && System::CanPauseSystem(true))
       System::PauseSystem(!System::IsPaused());
   }},

  {"ToggleFullscreen", TRANSLATE_NOOP("Hotkeys", "Interface"), TRANSLATE_NOOP("Hotkeys", "Toggle Fullscreen"),
   [](s32 pressed) {
     if (!pressed)
       VideoThread::SetFullscreen(!VideoThread::IsFullscreen());
   }},

#endif // __ANDROID__

  {"FastForward", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Fast Forward (Hold)"),
   [](s32 pressed) {
     if (pressed < 0)
       return;
     System::SetFastForwardEnabled(pressed > 0);
   }},

  {"ToggleFastForward", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Fast Forward (Toggle)"),
   [](s32 pressed) {
     if (!pressed)
       System::SetFastForwardEnabled(!System::IsFastForwardEnabled());
   }},

  {"Turbo", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Turbo (Hold)"),
   [](s32 pressed) {
     if (pressed < 0)
       return;
     System::SetTurboEnabled(pressed > 0);
   }},

  {"ToggleTurbo", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Turbo (Toggle)"),
   [](s32 pressed) {
     if (!pressed)
       System::SetTurboEnabled(!System::IsTurboEnabled());
   }},

  {"PowerOff", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Power Off System"),
   [](s32 pressed) {
     if (!pressed)
       Host::RequestSystemShutdown(true, g_settings.save_state_on_exit, true);
   }},

  {"Reset", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Restart Game"),
   [](s32 pressed) {
     if (!pressed)
       Host::RunOnCoreThread(System::ResetSystem);
   }},

#ifndef __ANDROID__
  {"ChangeDisc", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Change Disc"),
   [](s32 pressed) {
     if (!pressed)
       FullscreenUI::OpenDiscChangeMenu();
   }},
#endif // __ANDROID__

  {"SwitchToPreviousDisc", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Switch to Previous Disc"),
   [](s32 pressed) {
     // Defer because otherwise the hotkey might be invalidated by config change.
     if (!pressed)
       Host::RunOnCoreThread([]() { System::SwitchToPreviousDisc(true); });
   }},

  {"SwitchToNextDisc", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Switch to Next Disc"),
   [](s32 pressed) {
     // Defer because otherwise the hotkey might be invalidated by config change.
     if (!pressed)
       Host::RunOnCoreThread([]() { System::SwitchToNextDisc(true); });
   }},

  {"Rewind", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Rewind"),
   [](s32 pressed) {
     if (pressed < 0)
       return;

     const bool enabled = (pressed > 0);
     if (HotkeyCheckRewindAvailability(enabled))
       System::SetRewindState(enabled);
   }},

#ifndef __ANDROID__
  {"FrameStep", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Frame Step"),
   [](s32 pressed) {
     if (!pressed)
       System::FrameStep();
   }},

  {"ToggleMediaCapture", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Toggle Media Capture"),
   [](s32 pressed) {
     if (!pressed)
     {
       if (System::GetMediaCapture())
         System::StopMediaCapture();
       else
         System::StartMediaCapture();
     }
   }},

#endif // __ANDROID__

  {"SwapMemoryCards", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Swap Memory Card Slots"),
   [](s32 pressed) {
     if (!pressed)
       System::SwapMemoryCards();
   }},

  {"ToggleOverclocking", TRANSLATE_NOOP("Hotkeys", "System"),
   TRANSLATE_NOOP("Hotkeys", "Toggle Clock Speed Control (Overclocking)"),
   [](s32 pressed) {
     if (!pressed && System::IsValid() && !Achievements::IsHardcoreModeActive())
     {
       g_settings.cpu_overclock_enable = !g_settings.cpu_overclock_enable;
       g_settings.UpdateOverclockActive();
       System::UpdateOverclock();

       if (g_settings.cpu_overclock_enable)
       {
         const u32 percent = g_settings.GetCPUOverclockPercent();
         const double clock_speed =
           ((static_cast<double>(System::MASTER_CLOCK) * static_cast<double>(percent)) / 100.0) / 1000000.0;
         Host::AddIconOSDMessage(
           OSDMessageType::Quick, "ToggleOverclocking", ICON_FA_GAUGE_SIMPLE_HIGH,
           fmt::format(TRANSLATE_FS("OSDMessage", "CPU clock speed control enabled ({:.3f} MHz)."), clock_speed));
       }
       else
       {
         Host::AddIconOSDMessage(
           OSDMessageType::Quick, "ToggleOverclocking", ICON_FA_GAUGE_SIMPLE_HIGH,
           fmt::format(TRANSLATE_FS("OSDMessage", "CPU clock speed control disabled ({:.3f} MHz)."),
                       static_cast<double>(System::MASTER_CLOCK) / 1000000.0));
       }
     }
   }},

  {"IncreaseEmulationSpeed", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Increase Emulation Speed"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.emulation_speed += 0.1f;
       System::UpdateSpeedLimiterState();
       Host::AddIconOSDMessage(OSDMessageType::Quick, "EmulationSpeedChange", ICON_FA_GAUGE,
                               fmt::format(TRANSLATE_FS("OSDMessage", "Emulation speed set to {}%."),
                                           static_cast<u32>(std::lround(g_settings.emulation_speed * 100.0f))));
     }
   }},

  {"DecreaseEmulationSpeed", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Decrease Emulation Speed"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.emulation_speed =
         std::max(g_settings.emulation_speed - 0.1f, Achievements::IsHardcoreModeActive() ? 1.0f : 0.1f);
       System::UpdateSpeedLimiterState();
       Host::AddIconOSDMessage(OSDMessageType::Quick, "EmulationSpeedChange", ICON_FA_GAUGE,
                               fmt::format(TRANSLATE_FS("OSDMessage", "Emulation speed set to {}%."),
                                           static_cast<u32>(std::lround(g_settings.emulation_speed * 100.0f))));
     }
   }},

  {"ResetEmulationSpeed", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP("Hotkeys", "Reset Emulation Speed"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.emulation_speed = std::max(Core::GetFloatSettingValue("Main", "EmulationSpeed", 1.0f),
                                             Achievements::IsHardcoreModeActive() ? 1.0f : 0.1f);
       System::UpdateSpeedLimiterState();
       Host::AddIconOSDMessage(OSDMessageType::Quick, "EmulationSpeedChange", ICON_FA_GAUGE,
                               fmt::format(TRANSLATE_FS("OSDMessage", "Emulation speed set to {}%."),
                                           static_cast<u32>(std::lround(g_settings.emulation_speed * 100.0f))));
     }
   }},

  {"RotateClockwise", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_NOOP("Hotkeys", "Rotate Display Clockwise"),
   [](s32 pressed) {
     if (!pressed)
     {
       g_settings.display_rotation = static_cast<DisplayRotation>((static_cast<u8>(g_settings.display_rotation) + 1) %
                                                                  static_cast<u8>(DisplayRotation::Count));
       VideoThread::UpdateSettings(true, false, false);
     }
   }},

  {"RotateCounterclockwise", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Rotate Display Counterclockwise"),
   [](s32 pressed) {
     if (!pressed)
     {
       g_settings.display_rotation = (g_settings.display_rotation > static_cast<DisplayRotation>(0)) ?
                                       static_cast<DisplayRotation>((static_cast<u8>(g_settings.display_rotation) - 1) %
                                                                    static_cast<u8>(DisplayRotation::Count)) :
                                       static_cast<DisplayRotation>(static_cast<u8>(DisplayRotation::Count) - 1);
       VideoThread::UpdateSettings(true, false, false);
     }
   }},

  {"ToggleOSD", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_NOOP("Hotkeys", "Toggle On-Screen Display"),
   [](s32 pressed) {
     if (!pressed)
       HotkeyToggleOSD();
   }},

  {"ToggleSoftwareRendering", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Toggle Software Rendering"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
       System::ToggleSoftwareRendering();
   }},

  {"TogglePGXP", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_NOOP("Hotkeys", "Toggle PGXP"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       System::ClearMemorySaveStates(true, true);

       // This is pretty yikes, if PGXP was off we'll have already disabled all the dependent settings.
       g_settings.gpu_pgxp_enable = !g_settings.gpu_pgxp_enable;
       {
         const auto lock = Core::GetSettingsLock();
         const SettingsInterface& si = *Core::GetSettingsInterface();
         if (g_settings.gpu_pgxp_enable)
           g_settings.LoadPGXPSettings(si);
         g_settings.FixIncompatibleSettings(si, false);
       }

       VideoThread::UpdateSettings(true, false, false);

       Host::AddKeyedOSDMessage(OSDMessageType::Quick, "TogglePGXP",
                                g_settings.gpu_pgxp_enable ? TRANSLATE_STR("OSDMessage", "PGXP is now enabled.") :
                                                             TRANSLATE_STR("OSDMessage", "PGXP is now disabled."));

       if (g_settings.gpu_pgxp_enable)
         CPU::PGXP::Initialize();
       else
         CPU::PGXP::Shutdown();

       // we need to recompile all blocks if pgxp is toggled on/off
       CPU::CodeCache::Reset();

       // need to swap interpreters
       System::InterruptExecution();
     }
   }},

  {"TogglePGXPDepth", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_NOOP("Hotkeys", "Toggle PGXP Depth Buffer"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       if (!g_settings.gpu_pgxp_enable)
         return;

       System::ClearMemorySaveStates(true, true);

       g_settings.gpu_pgxp_depth_buffer = !g_settings.gpu_pgxp_depth_buffer;
       VideoThread::UpdateSettings(true, false, false);

       Host::AddIconOSDMessage(OSDMessageType::Quick, "TogglePGXPDepth", ICON_FA_SITEMAP,
                               g_settings.gpu_pgxp_depth_buffer ?
                                 TRANSLATE_STR("OSDMessage", "PGXP Depth Buffer is now enabled.") :
                                 TRANSLATE_STR("OSDMessage", "PGXP Depth Buffer is now disabled."));
     }
   }},

  {"ToggleWidescreen", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_NOOP("Hotkeys", "Toggle Widescreen"),
   [](s32 pressed) {
     if (!pressed)
       System::ToggleWidescreen();
   }},

  {"ToggleModulationCrop", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Toggle Texture Modulation Cropping"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.gpu_modulation_crop = !g_settings.gpu_modulation_crop;
       VideoThread::UpdateSettings(true, false, false);
       Host::AddIconOSDMessage(OSDMessageType::Quick, "ToggleModulationCrop", ICON_FA_SWATCHBOOK,
                               g_settings.gpu_modulation_crop ?
                                 TRANSLATE_STR("OSDMessage", "Texture modulation cropping is now enabled.") :
                                 TRANSLATE_STR("OSDMessage", "Texture modulation cropping is now disabled."));
     }
   }},

  {"TogglePostProcessing", TRANSLATE_NOOP("Hotkeys", "Graphics"), TRANSLATE_NOOP("Hotkeys", "Toggle Post-Processing"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
       VideoPresenter::TogglePostProcessing();
   }},

  {"ReloadPostProcessingShaders", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Reload Post Processing Shaders"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
       VideoPresenter::ReloadPostProcessingSettings(true, true, true);
   }},

  {"ReloadTextureReplacements", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Reload Texture Replacements"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
       VideoThread::RunOnThread([]() { GPUTextureCache::ReloadTextureReplacements(true, true); });
   }},

  {"IncreaseResolutionScale", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Increase Resolution Scale"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
       HotkeyModifyResolutionScale(1);
   }},

  {"DecreaseResolutionScale", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Decrease Resolution Scale"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
       HotkeyModifyResolutionScale(-1);
   }},

  {"RecordSingleFrameGPUDump", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Record Single Frame GPU Trace"),
   [](s32 pressed) {
     if (!pressed)
       System::StartRecordingGPUDump(nullptr, 1);
   }},

  {"RecordMultiFrameGPUDump", TRANSLATE_NOOP("Hotkeys", "Graphics"),
   TRANSLATE_NOOP("Hotkeys", "Record Multi-Frame GPU Trace"),
   [](s32 pressed) {
     if (pressed > 0)
       System::StartRecordingGPUDump(nullptr, 0);
     else
       System::StopRecordingGPUDump();
   }},

// See gte.cpp.
#ifndef __ANDROID__

  {"FreecamToggle", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Toggle"),
   [](s32 pressed) {
     if (!pressed && !Achievements::IsHardcoreModeActive())
       GTE::SetFreecamEnabled(!GTE::IsFreecamEnabled());
   }},

  {"FreecamReset", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Reset"),
   [](s32 pressed) {
     if (!pressed && !Achievements::IsHardcoreModeActive())
       GTE::ResetFreecam();
   }},

  {"FreecamMoveLeft", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Move Left"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamMoveAxis(0, std::max(static_cast<float>(pressed), 0.0f));
   }},

  {"FreecamMoveRight", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Move Right"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamMoveAxis(0, std::min(static_cast<float>(-pressed), 0.0f));
   }},

  {"FreecamMoveUp", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Move Up"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamMoveAxis(1, std::max(static_cast<float>(pressed), 0.0f));
   }},

  {"FreecamMoveDown", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Move Down"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamMoveAxis(1, std::min(static_cast<float>(-pressed), 0.0f));
   }},

  {"FreecamMoveForward", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Move Forward"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamMoveAxis(2, std::min(static_cast<float>(-pressed), 0.0f));
   }},

  {"FreecamMoveBackward", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Move Backward"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamMoveAxis(2, std::max(static_cast<float>(pressed), 0.0f));
   }},

  {"FreecamRotateLeft", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Rotate Left"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamRotateAxis(1, std::max(static_cast<float>(pressed), 0.0f));
   }},

  {"FreecamRotateRight", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Rotate Right"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamRotateAxis(1, std::min(static_cast<float>(-pressed), 0.0f));
   }},

  {"FreecamRotateForward", TRANSLATE_NOOP("Hotkeys", "Free Camera"),
   TRANSLATE_NOOP("Hotkeys", "Freecam Rotate Forward"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamRotateAxis(0, std::min(static_cast<float>(-pressed), 0.0f));
   }},

  {"FreecamRotateBackward", TRANSLATE_NOOP("Hotkeys", "Free Camera"),
   TRANSLATE_NOOP("Hotkeys", "Freecam Rotate Backward"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamRotateAxis(0, std::max(static_cast<float>(pressed), 0.0f));
   }},

  {"FreecamRollLeft", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Roll Left"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamRotateAxis(2, std::min(static_cast<float>(-pressed), 0.0f));
   }},

  {"FreecamRollRight", TRANSLATE_NOOP("Hotkeys", "Free Camera"), TRANSLATE_NOOP("Hotkeys", "Freecam Roll Right"),
   [](s32 pressed) {
     if (Achievements::IsHardcoreModeActive())
       return;

     GTE::SetFreecamRotateAxis(2, std::max(static_cast<float>(pressed), 0.0f));
   }},

#endif // __ANDROID__

  {"AudioMute", TRANSLATE_NOOP("Hotkeys", "Audio"), TRANSLATE_NOOP("Hotkeys", "Toggle Mute"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.audio_output_muted = !g_settings.audio_output_muted;
       const s32 volume = System::GetAudioOutputVolume();
       SPU::GetOutputStream().SetOutputVolume(volume);
       if (g_settings.audio_output_muted)
       {
         Host::AddIconOSDMessage(OSDMessageType::Quick, "AudioControlHotkey", ICON_EMOJI_MUTED_SPEAKER,
                                 TRANSLATE_STR("OSDMessage", "Volume: Muted"));
       }
       else
       {
         Host::AddIconOSDMessage(OSDMessageType::Quick, "AudioControlHotkey", ICON_EMOJI_MEDIUM_VOLUME_SPEAKER,
                                 fmt::format(TRANSLATE_FS("OSDMessage", "Volume: {}%"), volume));
       }
     }
   }},

  {"AudioCDAudioMute", TRANSLATE_NOOP("Hotkeys", "Audio"), TRANSLATE_NOOP("Hotkeys", "Toggle CD Audio Mute"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.cdrom_mute_cd_audio = !g_settings.cdrom_mute_cd_audio;
       Host::AddIconOSDMessage(OSDMessageType::Quick, "AudioControlHotkey",
                               g_settings.cdrom_mute_cd_audio ? ICON_EMOJI_MUTED_SPEAKER :
                                                                ICON_EMOJI_MEDIUM_VOLUME_SPEAKER,
                               g_settings.cdrom_mute_cd_audio ? TRANSLATE_STR("OSDMessage", "CD Audio Muted.") :
                                                                TRANSLATE_STR("OSDMessage", "CD Audio Unmuted."));
     }
   }},

  {"AudioVolumeUp", TRANSLATE_NOOP("Hotkeys", "Audio"), TRANSLATE_NOOP("Hotkeys", "Volume Up"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.audio_output_muted = false;

       const u8 volume = Truncate8(std::min<s32>(static_cast<s32>(System::GetAudioOutputVolume()) + 10, 200));
       g_settings.audio_output_volume = volume;
       g_settings.audio_fast_forward_volume = volume;
       SPU::GetOutputStream().SetOutputVolume(volume);
       Host::AddIconOSDMessage(OSDMessageType::Quick, "AudioControlHotkey", ICON_EMOJI_HIGH_VOLUME_SPEAKER,
                               fmt::format(TRANSLATE_FS("OSDMessage", "Volume: {}%"), volume));
     }
   }},

  {"AudioVolumeDown", TRANSLATE_NOOP("Hotkeys", "Audio"), TRANSLATE_NOOP("Hotkeys", "Volume Down"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       g_settings.audio_output_muted = false;

       const u8 volume = Truncate8(std::max<s32>(static_cast<s32>(System::GetAudioOutputVolume()) - 10, 0));
       g_settings.audio_output_volume = volume;
       g_settings.audio_fast_forward_volume = volume;
       SPU::GetOutputStream().SetOutputVolume(volume);
       Host::AddIconOSDMessage(OSDMessageType::Quick, "AudioControlHotkey", ICON_EMOJI_MEDIUM_VOLUME_SPEAKER,
                               fmt::format(TRANSLATE_FS("OSDMessage", "Volume: {}%"), volume));
     }
   }},

  // NOTE: All save/load state hotkeys are deferred, because it can trigger setting reapply, which reloads bindings.
  {"LoadSelectedSaveState", TRANSLATE_NOOP("Hotkeys", "Save States"),
   TRANSLATE_NOOP("Hotkeys", "Load From Selected Slot"),
   [](s32 pressed) {
     if (!pressed)
       VideoThread::RunOnThread(SaveStateSelectorUI::LoadCurrentSlot);
   }},

  {"SaveSelectedSaveState", TRANSLATE_NOOP("Hotkeys", "Save States"),
   TRANSLATE_NOOP("Hotkeys", "Save To Selected Slot"),
   [](s32 pressed) {
     if (!pressed)
       VideoThread::RunOnThread(SaveStateSelectorUI::SaveCurrentSlot);
   }},

  {"SelectPreviousSaveStateSlot", TRANSLATE_NOOP("Hotkeys", "Save States"),
   TRANSLATE_NOOP("Hotkeys", "Select Previous Save Slot"),
   [](s32 pressed) {
     if (!pressed)
       VideoThread::RunOnThread([]() { SaveStateSelectorUI::SelectPreviousSlot(true); });
   }},

  {"SelectNextSaveStateSlot", TRANSLATE_NOOP("Hotkeys", "Save States"),
   TRANSLATE_NOOP("Hotkeys", "Select Next Save Slot"),
   [](s32 pressed) {
     if (!pressed)
       VideoThread::RunOnThread([]() { SaveStateSelectorUI::SelectNextSlot(true); });
   }},

  {"SaveStateAndSelectNextSlot", TRANSLATE_NOOP("Hotkeys", "Save States"),
   TRANSLATE_NOOP("Hotkeys", "Save State and Select Next Slot"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       VideoThread::RunOnThread([]() {
         SaveStateSelectorUI::SaveCurrentSlot();
         SaveStateSelectorUI::SelectNextSlot(false);
       });
     }
   }},

  {"SelectNextSlotSaveState", TRANSLATE_NOOP("Hotkeys", "Save States"),
   TRANSLATE_NOOP("Hotkeys", "Select Next Slot and Save State"),
   [](s32 pressed) {
     if (!pressed && System::IsValid())
     {
       VideoThread::RunOnThread([]() {
         SaveStateSelectorUI::SelectNextSlot(false);
         SaveStateSelectorUI::SaveCurrentSlot();
       });
     }
   }},

  {"UndoLoadState", TRANSLATE_NOOP("Hotkeys", "Save States"), TRANSLATE_NOOP("Hotkeys", "Undo Load State"),
   [](s32 pressed) {
     if (!pressed)
       Host::RunOnCoreThread(System::UndoLoadState);
   }},

  {"TogglePGXPCPU", TRANSLATE_NOOP("Hotkeys", "Debugging"), TRANSLATE_NOOP("Hotkeys", "Toggle PGXP CPU Mode"),
   [](s32 pressed) {
     if (pressed && System::IsValid())
     {
       if (!g_settings.gpu_pgxp_enable)
         return;

       System::ClearMemorySaveStates(true, true);

       // GPU thread is unchanged
       g_settings.gpu_pgxp_cpu = !g_settings.gpu_pgxp_cpu;

       Host::AddIconOSDMessage(OSDMessageType::Quick, "TogglePGXPCPU", ICON_FA_BEZIER_CURVE,
                               g_settings.gpu_pgxp_cpu ? TRANSLATE_STR("OSDMessage", "PGXP CPU mode is now enabled.") :
                                                         TRANSLATE_STR("OSDMessage", "PGXP CPU mode is now disabled."));

       CPU::PGXP::Shutdown();
       CPU::PGXP::Initialize();

       // we need to recompile all blocks if pgxp is
       // toggled on/off
       CPU::CodeCache::Reset();

       // need to swap interpreters
       System::InterruptExecution();
     }
   }},

  {"TogglePGXPPreserveProjPrecision", TRANSLATE_NOOP("Hotkeys", "Debugging"),
   TRANSLATE_NOOP("Hotkeys", "Toggle PGXP Preserve Projection Precision"),
   [](s32 pressed) {
     if (pressed && System::IsValid())
     {
       if (!g_settings.gpu_pgxp_enable)
         return;

       // GPU thread is unchanged
       g_settings.gpu_pgxp_preserve_proj_fp = !g_settings.gpu_pgxp_preserve_proj_fp;

       Host::AddIconOSDMessage(OSDMessageType::Quick, "TogglePGXPPreserveProjPrecision", ICON_FA_DRAW_POLYGON,
                               g_settings.gpu_pgxp_preserve_proj_fp ?
                                 TRANSLATE_STR("OSDMessage", "PGXP Preserve Projection Precision is now enabled.") :
                                 TRANSLATE_STR("OSDMessage", "PGXP Preserve Projection Precision is now disabled."));
     }
   }},

  {"ToggleVRAMView", TRANSLATE_NOOP("Hotkeys", "Debugging"), TRANSLATE_NOOP("Hotkeys", "Toggle VRAM View"),
   [](s32 pressed) {
     if (pressed && System::IsValid())
     {
       if (!g_settings.gpu_pgxp_enable || Achievements::IsHardcoreModeActive())
         return;

       g_settings.gpu_show_vram = !g_settings.gpu_show_vram;
       VideoThread::UpdateSettings(true, false, false);

       Host::AddIconOSDMessage(OSDMessageType::Quick, "ToggleVRAMView", ICON_FA_FILE,
                               g_settings.gpu_show_vram ? TRANSLATE_STR("OSDMessage", "Now showing VRAM.") :
                                                          TRANSLATE_STR("OSDMessage", "Now showing display."));
     }
   }},

#define MAKE_LOAD_STATE_HOTKEY(global, slot, name)                                                                     \
  {global ? "LoadGameState" #slot : "LoadGlobalState" #slot, TRANSLATE_NOOP("Hotkeys", "Save States"), name,           \
   [](s32 pressed) {                                                                                                   \
     if (!pressed)                                                                                                     \
       Host::RunOnCoreThread([]() { System::LoadStateFromSlot(global, slot); });                                       \
   }}
#define MAKE_SAVE_STATE_HOTKEY(global, slot, name)                                                                     \
  {global ? "SaveGameState" #slot : "SaveGlobalState" #slot, TRANSLATE_NOOP("Hotkeys", "Save States"), name,           \
   [](s32 pressed) {                                                                                                   \
     if (!pressed)                                                                                                     \
       Host::RunOnCoreThread([]() { System::SaveStateToSlot(global, slot); });                                         \
   }}

  MAKE_LOAD_STATE_HOTKEY(false, 1, TRANSLATE_NOOP("Hotkeys", "Load Game State 1")),
  MAKE_SAVE_STATE_HOTKEY(false, 1, TRANSLATE_NOOP("Hotkeys", "Save Game State 1")),
  MAKE_LOAD_STATE_HOTKEY(false, 2, TRANSLATE_NOOP("Hotkeys", "Load Game State 2")),
  MAKE_SAVE_STATE_HOTKEY(false, 2, TRANSLATE_NOOP("Hotkeys", "Save Game State 2")),
  MAKE_LOAD_STATE_HOTKEY(false, 3, TRANSLATE_NOOP("Hotkeys", "Load Game State 3")),
  MAKE_SAVE_STATE_HOTKEY(false, 3, TRANSLATE_NOOP("Hotkeys", "Save Game State 3")),
  MAKE_LOAD_STATE_HOTKEY(false, 4, TRANSLATE_NOOP("Hotkeys", "Load Game State 4")),
  MAKE_SAVE_STATE_HOTKEY(false, 4, TRANSLATE_NOOP("Hotkeys", "Save Game State 4")),
  MAKE_LOAD_STATE_HOTKEY(false, 5, TRANSLATE_NOOP("Hotkeys", "Load Game State 5")),
  MAKE_SAVE_STATE_HOTKEY(false, 5, TRANSLATE_NOOP("Hotkeys", "Save Game State 5")),
  MAKE_LOAD_STATE_HOTKEY(false, 6, TRANSLATE_NOOP("Hotkeys", "Load Game State 6")),
  MAKE_SAVE_STATE_HOTKEY(false, 6, TRANSLATE_NOOP("Hotkeys", "Save Game State 6")),
  MAKE_LOAD_STATE_HOTKEY(false, 7, TRANSLATE_NOOP("Hotkeys", "Load Game State 7")),
  MAKE_SAVE_STATE_HOTKEY(false, 7, TRANSLATE_NOOP("Hotkeys", "Save Game State 7")),
  MAKE_LOAD_STATE_HOTKEY(false, 8, TRANSLATE_NOOP("Hotkeys", "Load Game State 8")),
  MAKE_SAVE_STATE_HOTKEY(false, 8, TRANSLATE_NOOP("Hotkeys", "Save Game State 8")),
  MAKE_LOAD_STATE_HOTKEY(false, 9, TRANSLATE_NOOP("Hotkeys", "Load Game State 9")),
  MAKE_SAVE_STATE_HOTKEY(false, 9, TRANSLATE_NOOP("Hotkeys", "Save Game State 9")),
  MAKE_LOAD_STATE_HOTKEY(false, 10, TRANSLATE_NOOP("Hotkeys", "Load Game State 10")),
  MAKE_SAVE_STATE_HOTKEY(false, 10, TRANSLATE_NOOP("Hotkeys", "Save Game State 10")),

  MAKE_LOAD_STATE_HOTKEY(true, 1, TRANSLATE_NOOP("Hotkeys", "Load Global State 1")),
  MAKE_SAVE_STATE_HOTKEY(true, 1, TRANSLATE_NOOP("Hotkeys", "Save Global State 1")),
  MAKE_LOAD_STATE_HOTKEY(true, 2, TRANSLATE_NOOP("Hotkeys", "Load Global State 2")),
  MAKE_SAVE_STATE_HOTKEY(true, 2, TRANSLATE_NOOP("Hotkeys", "Save Global State 2")),
  MAKE_LOAD_STATE_HOTKEY(true, 3, TRANSLATE_NOOP("Hotkeys", "Load Global State 3")),
  MAKE_SAVE_STATE_HOTKEY(true, 3, TRANSLATE_NOOP("Hotkeys", "Save Global State 3")),
  MAKE_LOAD_STATE_HOTKEY(true, 4, TRANSLATE_NOOP("Hotkeys", "Load Global State 4")),
  MAKE_SAVE_STATE_HOTKEY(true, 4, TRANSLATE_NOOP("Hotkeys", "Save Global State 4")),
  MAKE_LOAD_STATE_HOTKEY(true, 5, TRANSLATE_NOOP("Hotkeys", "Load Global State 5")),
  MAKE_SAVE_STATE_HOTKEY(true, 5, TRANSLATE_NOOP("Hotkeys", "Save Global State 5")),
  MAKE_LOAD_STATE_HOTKEY(true, 6, TRANSLATE_NOOP("Hotkeys", "Load Global State 6")),
  MAKE_SAVE_STATE_HOTKEY(true, 6, TRANSLATE_NOOP("Hotkeys", "Save Global State 6")),
  MAKE_LOAD_STATE_HOTKEY(true, 7, TRANSLATE_NOOP("Hotkeys", "Load Global State 7")),
  MAKE_SAVE_STATE_HOTKEY(true, 7, TRANSLATE_NOOP("Hotkeys", "Save Global State 7")),
  MAKE_LOAD_STATE_HOTKEY(true, 8, TRANSLATE_NOOP("Hotkeys", "Load Global State 8")),
  MAKE_SAVE_STATE_HOTKEY(true, 8, TRANSLATE_NOOP("Hotkeys", "Save Global State 8")),
  MAKE_LOAD_STATE_HOTKEY(true, 9, TRANSLATE_NOOP("Hotkeys", "Load Global State 9")),
  MAKE_SAVE_STATE_HOTKEY(true, 9, TRANSLATE_NOOP("Hotkeys", "Save Global State 9")),
  MAKE_LOAD_STATE_HOTKEY(true, 10, TRANSLATE_NOOP("Hotkeys", "Load Global State 10")),
  MAKE_SAVE_STATE_HOTKEY(true, 10, TRANSLATE_NOOP("Hotkeys", "Save Global State 10")),

#undef MAKE_SAVE_STATE_HOTKEY
#undef MAKE_LOAD_STATE_HOTKEY
};

std::span<const HotkeyInfo> Core::GetHotkeyList()
{
  return s_hotkey_list;
}
