hotkeys.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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().toStdString(),
  19. hotkey.second.controller_keyseq,
  20. hotkey.second.context, hotkey.second.repeat})});
  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.empty()) {
  30. hk.keyseq = QKeySequence::fromString(QString::fromStdString(shortcut.shortcut.keyseq),
  31. QKeySequence::NativeText);
  32. hk.context = static_cast<Qt::ShortcutContext>(shortcut.shortcut.context);
  33. }
  34. if (!shortcut.shortcut.controller_keyseq.empty()) {
  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. hk.repeat = shortcut.shortcut.repeat;
  46. }
  47. }
  48. QShortcut* HotkeyRegistry::GetHotkey(const std::string& group, const std::string& action,
  49. QWidget* widget) {
  50. Hotkey& hk = hotkey_groups[group][action];
  51. if (!hk.shortcut) {
  52. hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
  53. }
  54. hk.shortcut->setAutoRepeat(hk.repeat);
  55. return hk.shortcut;
  56. }
  57. ControllerShortcut* HotkeyRegistry::GetControllerHotkey(const std::string& group,
  58. const std::string& action,
  59. Core::HID::EmulatedController* controller) {
  60. Hotkey& hk = hotkey_groups[group][action];
  61. if (!hk.controller_shortcut) {
  62. hk.controller_shortcut = new ControllerShortcut(controller);
  63. hk.controller_shortcut->SetKey(hk.controller_keyseq);
  64. }
  65. return hk.controller_shortcut;
  66. }
  67. QKeySequence HotkeyRegistry::GetKeySequence(const std::string& group, const std::string& action) {
  68. return hotkey_groups[group][action].keyseq;
  69. }
  70. Qt::ShortcutContext HotkeyRegistry::GetShortcutContext(const std::string& group,
  71. const std::string& action) {
  72. return hotkey_groups[group][action].context;
  73. }
  74. ControllerShortcut::ControllerShortcut(Core::HID::EmulatedController* controller) {
  75. emulated_controller = controller;
  76. Core::HID::ControllerUpdateCallback engine_callback{
  77. .on_change = [this](Core::HID::ControllerTriggerType type) { ControllerUpdateEvent(type); },
  78. .is_npad_service = false,
  79. };
  80. callback_key = emulated_controller->SetCallback(engine_callback);
  81. is_enabled = true;
  82. }
  83. ControllerShortcut::~ControllerShortcut() {
  84. emulated_controller->DeleteCallback(callback_key);
  85. }
  86. void ControllerShortcut::SetKey(const ControllerButtonSequence& buttons) {
  87. button_sequence = buttons;
  88. }
  89. void ControllerShortcut::SetKey(const std::string& buttons_shortcut) {
  90. ControllerButtonSequence sequence{};
  91. name = buttons_shortcut;
  92. std::istringstream command_line(buttons_shortcut);
  93. std::string line;
  94. while (std::getline(command_line, line, '+')) {
  95. if (line.empty()) {
  96. continue;
  97. }
  98. if (line == "A") {
  99. sequence.npad.a.Assign(1);
  100. }
  101. if (line == "B") {
  102. sequence.npad.b.Assign(1);
  103. }
  104. if (line == "X") {
  105. sequence.npad.x.Assign(1);
  106. }
  107. if (line == "Y") {
  108. sequence.npad.y.Assign(1);
  109. }
  110. if (line == "L") {
  111. sequence.npad.l.Assign(1);
  112. }
  113. if (line == "R") {
  114. sequence.npad.r.Assign(1);
  115. }
  116. if (line == "ZL") {
  117. sequence.npad.zl.Assign(1);
  118. }
  119. if (line == "ZR") {
  120. sequence.npad.zr.Assign(1);
  121. }
  122. if (line == "Dpad_Left") {
  123. sequence.npad.left.Assign(1);
  124. }
  125. if (line == "Dpad_Right") {
  126. sequence.npad.right.Assign(1);
  127. }
  128. if (line == "Dpad_Up") {
  129. sequence.npad.up.Assign(1);
  130. }
  131. if (line == "Dpad_Down") {
  132. sequence.npad.down.Assign(1);
  133. }
  134. if (line == "Left_Stick") {
  135. sequence.npad.stick_l.Assign(1);
  136. }
  137. if (line == "Right_Stick") {
  138. sequence.npad.stick_r.Assign(1);
  139. }
  140. if (line == "Minus") {
  141. sequence.npad.minus.Assign(1);
  142. }
  143. if (line == "Plus") {
  144. sequence.npad.plus.Assign(1);
  145. }
  146. if (line == "Home") {
  147. sequence.home.home.Assign(1);
  148. }
  149. if (line == "Screenshot") {
  150. sequence.capture.capture.Assign(1);
  151. }
  152. }
  153. button_sequence = sequence;
  154. }
  155. ControllerButtonSequence ControllerShortcut::ButtonSequence() const {
  156. return button_sequence;
  157. }
  158. void ControllerShortcut::SetEnabled(bool enable) {
  159. is_enabled = enable;
  160. }
  161. bool ControllerShortcut::IsEnabled() const {
  162. return is_enabled;
  163. }
  164. void ControllerShortcut::ControllerUpdateEvent(Core::HID::ControllerTriggerType type) {
  165. if (!is_enabled) {
  166. return;
  167. }
  168. if (type != Core::HID::ControllerTriggerType::Button) {
  169. return;
  170. }
  171. if (!Settings::values.controller_navigation) {
  172. return;
  173. }
  174. if (button_sequence.npad.raw == Core::HID::NpadButton::None &&
  175. button_sequence.capture.raw == 0 && button_sequence.home.raw == 0) {
  176. return;
  177. }
  178. const auto player_npad_buttons =
  179. emulated_controller->GetNpadButtons().raw & button_sequence.npad.raw;
  180. const u64 player_capture_buttons =
  181. emulated_controller->GetCaptureButtons().raw & button_sequence.capture.raw;
  182. const u64 player_home_buttons =
  183. emulated_controller->GetHomeButtons().raw & button_sequence.home.raw;
  184. if (player_npad_buttons == button_sequence.npad.raw &&
  185. player_capture_buttons == button_sequence.capture.raw &&
  186. player_home_buttons == button_sequence.home.raw && !active) {
  187. // Force user to press the home or capture button again
  188. active = true;
  189. emit Activated();
  190. return;
  191. }
  192. active = false;
  193. }