hotkeys.cpp 6.8 KB

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