hotkeys.cpp 6.8 KB

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