hotkeys.h 3.8 KB

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