hotkeys.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <map>
  5. #include <QKeySequence>
  6. #include <QString>
  7. #include <QWidget>
  8. #include "core/hid/hid_types.h"
  9. class QDialog;
  10. class QSettings;
  11. class QShortcut;
  12. class ControllerShortcut;
  13. namespace Core::HID {
  14. enum class ControllerTriggerType;
  15. class EmulatedController;
  16. } // namespace Core::HID
  17. struct ControllerButtonSequence {
  18. Core::HID::CaptureButtonState capture{};
  19. Core::HID::HomeButtonState home{};
  20. Core::HID::NpadButtonState npad{};
  21. };
  22. class ControllerShortcut : public QObject {
  23. Q_OBJECT
  24. public:
  25. explicit ControllerShortcut(Core::HID::EmulatedController* controller);
  26. ~ControllerShortcut();
  27. void SetKey(const ControllerButtonSequence& buttons);
  28. void SetKey(const std::string& buttons_shortcut);
  29. ControllerButtonSequence ButtonSequence() const;
  30. void SetEnabled(bool enable);
  31. bool IsEnabled() const;
  32. Q_SIGNALS:
  33. void Activated();
  34. private:
  35. void ControllerUpdateEvent(Core::HID::ControllerTriggerType type);
  36. bool is_enabled{};
  37. bool active{};
  38. int callback_key{};
  39. ControllerButtonSequence button_sequence{};
  40. std::string name{};
  41. Core::HID::EmulatedController* emulated_controller = nullptr;
  42. };
  43. class HotkeyRegistry final {
  44. public:
  45. friend class ConfigureHotkeys;
  46. explicit HotkeyRegistry();
  47. ~HotkeyRegistry();
  48. /**
  49. * Loads hotkeys from the settings file.
  50. *
  51. * @note Yet unregistered hotkeys which are present in the settings will automatically be
  52. * registered.
  53. */
  54. void LoadHotkeys();
  55. /**
  56. * Saves all registered hotkeys to the settings file.
  57. *
  58. * @note Each hotkey group will be stored a settings group; For each hotkey inside that group, a
  59. * settings group will be created to store the key sequence and the hotkey context.
  60. */
  61. void SaveHotkeys();
  62. /**
  63. * Returns a QShortcut object whose activated() signal can be connected to other QObjects'
  64. * slots.
  65. *
  66. * @param group General group this hotkey belongs to (e.g. "Main Window", "Debugger").
  67. * @param action Name of the action (e.g. "Start Emulation", "Load Image").
  68. * @param widget Parent widget of the returned QShortcut.
  69. * @warning If multiple QWidgets' call this function for the same action, the returned QShortcut
  70. * will be the same. Thus, you shouldn't rely on the caller really being the
  71. * QShortcut's parent.
  72. */
  73. QShortcut* GetHotkey(const std::string& group, const std::string& action, QWidget* widget);
  74. ControllerShortcut* GetControllerHotkey(const std::string& group, const std::string& action,
  75. Core::HID::EmulatedController* controller);
  76. /**
  77. * Returns a QKeySequence object whose signal can be connected to QAction::setShortcut.
  78. *
  79. * @param group General group this hotkey belongs to (e.g. "Main Window", "Debugger").
  80. * @param action Name of the action (e.g. "Start Emulation", "Load Image").
  81. */
  82. QKeySequence GetKeySequence(const std::string& group, const std::string& action);
  83. /**
  84. * Returns a Qt::ShortcutContext object who can be connected to other
  85. * QAction::setShortcutContext.
  86. *
  87. * @param group General group this shortcut context belongs to (e.g. "Main Window",
  88. * "Debugger").
  89. * @param action Name of the action (e.g. "Start Emulation", "Load Image").
  90. */
  91. Qt::ShortcutContext GetShortcutContext(const std::string& group, const std::string& action);
  92. private:
  93. struct Hotkey {
  94. QKeySequence keyseq;
  95. std::string controller_keyseq;
  96. QShortcut* shortcut = nullptr;
  97. ControllerShortcut* controller_shortcut = nullptr;
  98. Qt::ShortcutContext context = Qt::WindowShortcut;
  99. bool repeat;
  100. };
  101. using HotkeyMap = std::map<std::string, Hotkey>;
  102. using HotkeyGroupMap = std::map<std::string, HotkeyMap>;
  103. HotkeyGroupMap hotkey_groups;
  104. };