hotkeys.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-FileCopyrightText: 2014 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <sstream>
  4. #include <QShortcut>
  5. #include <QTreeWidgetItem>
  6. #include <QtGlobal>
  7. #include "core/hid/emulated_controller.h"
  8. #include "yuzu/hotkeys.h"
  9. #include "yuzu/uisettings.h"
  10. HotkeyRegistry::HotkeyRegistry() = default;
  11. HotkeyRegistry::~HotkeyRegistry() = default;
  12. void HotkeyRegistry::SaveHotkeys() {
  13. UISettings::values.shortcuts.clear();
  14. for (const auto& group : hotkey_groups) {
  15. for (const auto& hotkey : group.second) {
  16. UISettings::values.shortcuts.push_back(
  17. {hotkey.first, group.first,
  18. UISettings::ContextualShortcut({hotkey.second.keyseq.toString(),
  19. hotkey.second.controller_keyseq,
  20. hotkey.second.context})});
  21. }
  22. }
  23. }
  24. void HotkeyRegistry::LoadHotkeys() {
  25. // Make sure NOT to use a reference here because it would become invalid once we call
  26. // beginGroup()
  27. for (auto shortcut : UISettings::values.shortcuts) {
  28. Hotkey& hk = hotkey_groups[shortcut.group][shortcut.name];
  29. if (!shortcut.shortcut.keyseq.isEmpty()) {
  30. hk.keyseq =
  31. QKeySequence::fromString(shortcut.shortcut.keyseq, QKeySequence::NativeText);
  32. hk.context = static_cast<Qt::ShortcutContext>(shortcut.shortcut.context);
  33. }
  34. if (!shortcut.shortcut.controller_keyseq.isEmpty()) {
  35. hk.controller_keyseq = shortcut.shortcut.controller_keyseq;
  36. }
  37. if (hk.shortcut) {
  38. hk.shortcut->disconnect();
  39. hk.shortcut->setKey(hk.keyseq);
  40. }
  41. if (hk.controller_shortcut) {
  42. hk.controller_shortcut->disconnect();
  43. hk.controller_shortcut->SetKey(hk.controller_keyseq);
  44. }
  45. }
  46. }
  47. QShortcut* HotkeyRegistry::GetHotkey(const QString& group, const QString& action, QWidget* widget) {
  48. Hotkey& hk = hotkey_groups[group][action];
  49. if (!hk.shortcut) {
  50. hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
  51. }
  52. hk.shortcut->setAutoRepeat(false);
  53. return hk.shortcut;
  54. }
  55. ControllerShortcut* HotkeyRegistry::GetControllerHotkey(const QString& group, const QString& action,
  56. Core::HID::EmulatedController* controller) {
  57. Hotkey& hk = hotkey_groups[group][action];
  58. if (!hk.controller_shortcut) {
  59. hk.controller_shortcut = new ControllerShortcut(controller);
  60. hk.controller_shortcut->SetKey(hk.controller_keyseq);
  61. }
  62. return hk.controller_shortcut;
  63. }
  64. QKeySequence HotkeyRegistry::GetKeySequence(const QString& group, const QString& action) {
  65. return hotkey_groups[group][action].keyseq;
  66. }
  67. Qt::ShortcutContext HotkeyRegistry::GetShortcutContext(const QString& group,
  68. const QString& action) {
  69. return hotkey_groups[group][action].context;
  70. }
  71. ControllerShortcut::ControllerShortcut(Core::HID::EmulatedController* controller) {
  72. emulated_controller = controller;
  73. Core::HID::ControllerUpdateCallback engine_callback{
  74. .on_change = [this](Core::HID::ControllerTriggerType type) { ControllerUpdateEvent(type); },
  75. .is_npad_service = false,
  76. };
  77. callback_key = emulated_controller->SetCallback(engine_callback);
  78. is_enabled = true;
  79. }
  80. ControllerShortcut::~ControllerShortcut() {
  81. emulated_controller->DeleteCallback(callback_key);
  82. }
  83. void ControllerShortcut::SetKey(const ControllerButtonSequence& buttons) {
  84. button_sequence = buttons;
  85. }
  86. void ControllerShortcut::SetKey(const QString& buttons_shortcut) {
  87. ControllerButtonSequence sequence{};
  88. name = buttons_shortcut.toStdString();
  89. std::istringstream command_line(buttons_shortcut.toStdString());
  90. std::string line;
  91. while (std::getline(command_line, line, '+')) {
  92. if (line.empty()) {
  93. continue;
  94. }
  95. if (line == "A") {
  96. sequence.npad.a.Assign(1);
  97. }
  98. if (line == "B") {
  99. sequence.npad.b.Assign(1);
  100. }
  101. if (line == "X") {
  102. sequence.npad.x.Assign(1);
  103. }
  104. if (line == "Y") {
  105. sequence.npad.y.Assign(1);
  106. }
  107. if (line == "L") {
  108. sequence.npad.l.Assign(1);
  109. }
  110. if (line == "R") {
  111. sequence.npad.r.Assign(1);
  112. }
  113. if (line == "ZL") {
  114. sequence.npad.zl.Assign(1);
  115. }
  116. if (line == "ZR") {
  117. sequence.npad.zr.Assign(1);
  118. }
  119. if (line == "Dpad_Left") {
  120. sequence.npad.left.Assign(1);
  121. }
  122. if (line == "Dpad_Right") {
  123. sequence.npad.right.Assign(1);
  124. }
  125. if (line == "Dpad_Up") {
  126. sequence.npad.up.Assign(1);
  127. }
  128. if (line == "Dpad_Down") {
  129. sequence.npad.down.Assign(1);
  130. }
  131. if (line == "Left_Stick") {
  132. sequence.npad.stick_l.Assign(1);
  133. }
  134. if (line == "Right_Stick") {
  135. sequence.npad.stick_r.Assign(1);
  136. }
  137. if (line == "Minus") {
  138. sequence.npad.minus.Assign(1);
  139. }
  140. if (line == "Plus") {
  141. sequence.npad.plus.Assign(1);
  142. }
  143. if (line == "Home") {
  144. sequence.home.home.Assign(1);
  145. }
  146. if (line == "Screenshot") {
  147. sequence.capture.capture.Assign(1);
  148. }
  149. }
  150. button_sequence = sequence;
  151. }
  152. ControllerButtonSequence ControllerShortcut::ButtonSequence() const {
  153. return button_sequence;
  154. }
  155. void ControllerShortcut::SetEnabled(bool enable) {
  156. is_enabled = enable;
  157. }
  158. bool ControllerShortcut::IsEnabled() const {
  159. return is_enabled;
  160. }
  161. void ControllerShortcut::ControllerUpdateEvent(Core::HID::ControllerTriggerType type) {
  162. if (!is_enabled) {
  163. return;
  164. }
  165. if (type != Core::HID::ControllerTriggerType::Button) {
  166. return;
  167. }
  168. if (!Settings::values.controller_navigation) {
  169. return;
  170. }
  171. if (button_sequence.npad.raw == Core::HID::NpadButton::None &&
  172. button_sequence.capture.raw == 0 && button_sequence.home.raw == 0) {
  173. return;
  174. }
  175. const auto player_npad_buttons =
  176. emulated_controller->GetNpadButtons().raw & button_sequence.npad.raw;
  177. const u64 player_capture_buttons =
  178. emulated_controller->GetCaptureButtons().raw & button_sequence.capture.raw;
  179. const u64 player_home_buttons =
  180. emulated_controller->GetHomeButtons().raw & button_sequence.home.raw;
  181. if (player_npad_buttons == button_sequence.npad.raw &&
  182. player_capture_buttons == button_sequence.capture.raw &&
  183. player_home_buttons == button_sequence.home.raw && !active) {
  184. // Force user to press the home or capture button again
  185. active = true;
  186. emit Activated();
  187. return;
  188. }
  189. active = false;
  190. }