configure_hotkeys.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QMenu>
  5. #include <QMessageBox>
  6. #include <QStandardItemModel>
  7. #include <QTimer>
  8. #include "core/hid/emulated_controller.h"
  9. #include "core/hid/hid_core.h"
  10. #include "ui_configure_hotkeys.h"
  11. #include "yuzu/configuration/config.h"
  12. #include "yuzu/configuration/configure_hotkeys.h"
  13. #include "yuzu/hotkeys.h"
  14. #include "yuzu/util/sequence_dialog/sequence_dialog.h"
  15. constexpr int name_column = 0;
  16. constexpr int hotkey_column = 1;
  17. constexpr int controller_column = 2;
  18. ConfigureHotkeys::ConfigureHotkeys(Core::HID::HIDCore& hid_core, QWidget* parent)
  19. : QWidget(parent), ui(std::make_unique<Ui::ConfigureHotkeys>()),
  20. timeout_timer(std::make_unique<QTimer>()), poll_timer(std::make_unique<QTimer>()) {
  21. ui->setupUi(this);
  22. setFocusPolicy(Qt::ClickFocus);
  23. model = new QStandardItemModel(this);
  24. model->setColumnCount(3);
  25. connect(ui->hotkey_list, &QTreeView::doubleClicked, this, &ConfigureHotkeys::Configure);
  26. connect(ui->hotkey_list, &QTreeView::customContextMenuRequested, this,
  27. &ConfigureHotkeys::PopupContextMenu);
  28. ui->hotkey_list->setContextMenuPolicy(Qt::CustomContextMenu);
  29. ui->hotkey_list->setModel(model);
  30. ui->hotkey_list->setColumnWidth(name_column, 200);
  31. ui->hotkey_list->resizeColumnToContents(hotkey_column);
  32. connect(ui->button_restore_defaults, &QPushButton::clicked, this,
  33. &ConfigureHotkeys::RestoreDefaults);
  34. connect(ui->button_clear_all, &QPushButton::clicked, this, &ConfigureHotkeys::ClearAll);
  35. controller = hid_core.GetEmulatedController(Core::HID::NpadIdType::Player1);
  36. connect(timeout_timer.get(), &QTimer::timeout, [this] { SetPollingResult({}, true); });
  37. connect(poll_timer.get(), &QTimer::timeout, [this] {
  38. const auto buttons = controller->GetNpadButtons();
  39. if (buttons.raw != Core::HID::NpadButton::None) {
  40. SetPollingResult(buttons.raw, false);
  41. return;
  42. }
  43. });
  44. RetranslateUI();
  45. }
  46. ConfigureHotkeys::~ConfigureHotkeys() = default;
  47. void ConfigureHotkeys::Populate(const HotkeyRegistry& registry) {
  48. for (const auto& group : registry.hotkey_groups) {
  49. auto* parent_item = new QStandardItem(group.first);
  50. parent_item->setEditable(false);
  51. for (const auto& hotkey : group.second) {
  52. auto* action = new QStandardItem(hotkey.first);
  53. auto* keyseq =
  54. new QStandardItem(hotkey.second.keyseq.toString(QKeySequence::NativeText));
  55. auto* controller_keyseq = new QStandardItem(hotkey.second.controller_keyseq);
  56. action->setEditable(false);
  57. keyseq->setEditable(false);
  58. controller_keyseq->setEditable(false);
  59. parent_item->appendRow({action, keyseq, controller_keyseq});
  60. }
  61. model->appendRow(parent_item);
  62. }
  63. ui->hotkey_list->expandAll();
  64. ui->hotkey_list->resizeColumnToContents(name_column);
  65. ui->hotkey_list->resizeColumnToContents(hotkey_column);
  66. }
  67. void ConfigureHotkeys::changeEvent(QEvent* event) {
  68. if (event->type() == QEvent::LanguageChange) {
  69. RetranslateUI();
  70. }
  71. QWidget::changeEvent(event);
  72. }
  73. void ConfigureHotkeys::RetranslateUI() {
  74. ui->retranslateUi(this);
  75. model->setHorizontalHeaderLabels({tr("Action"), tr("Hotkey"), tr("Controller Hotkey")});
  76. }
  77. void ConfigureHotkeys::Configure(QModelIndex index) {
  78. if (!index.parent().isValid()) {
  79. return;
  80. }
  81. // Controller configuration is selected
  82. if (index.column() == controller_column) {
  83. ConfigureController(index);
  84. return;
  85. }
  86. // Swap to the hotkey column
  87. index = index.sibling(index.row(), hotkey_column);
  88. const auto previous_key = model->data(index);
  89. SequenceDialog hotkey_dialog{this};
  90. const int return_code = hotkey_dialog.exec();
  91. const auto key_sequence = hotkey_dialog.GetSequence();
  92. if (return_code == QDialog::Rejected || key_sequence.isEmpty()) {
  93. return;
  94. }
  95. const auto [key_sequence_used, used_action] = IsUsedKey(key_sequence);
  96. if (key_sequence_used && key_sequence != QKeySequence(previous_key.toString())) {
  97. QMessageBox::warning(
  98. this, tr("Conflicting Key Sequence"),
  99. tr("The entered key sequence is already assigned to: %1").arg(used_action));
  100. } else {
  101. model->setData(index, key_sequence.toString(QKeySequence::NativeText));
  102. }
  103. }
  104. void ConfigureHotkeys::ConfigureController(QModelIndex index) {
  105. if (timeout_timer->isActive()) {
  106. return;
  107. }
  108. const auto previous_key = model->data(index);
  109. input_setter = [this, index, previous_key](const Core::HID::NpadButton button,
  110. const bool cancel) {
  111. if (cancel) {
  112. model->setData(index, previous_key);
  113. return;
  114. }
  115. const QString button_string = tr("Home+%1").arg(GetButtonName(button));
  116. const auto [key_sequence_used, used_action] = IsUsedControllerKey(button_string);
  117. if (key_sequence_used) {
  118. QMessageBox::warning(
  119. this, tr("Conflicting Key Sequence"),
  120. tr("The entered key sequence is already assigned to: %1").arg(used_action));
  121. model->setData(index, previous_key);
  122. } else {
  123. model->setData(index, button_string);
  124. }
  125. };
  126. model->setData(index, tr("[waiting]"));
  127. timeout_timer->start(2500); // Cancel after 2.5 seconds
  128. poll_timer->start(200); // Check for new inputs every 200ms
  129. // We need to disable configuration to be able to read npad buttons
  130. controller->DisableConfiguration();
  131. controller->DisableSystemButtons();
  132. }
  133. void ConfigureHotkeys::SetPollingResult(Core::HID::NpadButton button, const bool cancel) {
  134. timeout_timer->stop();
  135. poll_timer->stop();
  136. // Re-Enable configuration
  137. controller->EnableConfiguration();
  138. controller->EnableSystemButtons();
  139. (*input_setter)(button, cancel);
  140. input_setter = std::nullopt;
  141. }
  142. QString ConfigureHotkeys::GetButtonName(Core::HID::NpadButton button) const {
  143. Core::HID::NpadButtonState state{button};
  144. if (state.a) {
  145. return tr("A");
  146. }
  147. if (state.b) {
  148. return tr("B");
  149. }
  150. if (state.x) {
  151. return tr("X");
  152. }
  153. if (state.y) {
  154. return tr("Y");
  155. }
  156. if (state.l || state.right_sl || state.left_sl) {
  157. return tr("L");
  158. }
  159. if (state.r || state.right_sr || state.left_sr) {
  160. return tr("R");
  161. }
  162. if (state.zl) {
  163. return tr("ZL");
  164. }
  165. if (state.zr) {
  166. return tr("ZR");
  167. }
  168. if (state.left) {
  169. return tr("Dpad_Left");
  170. }
  171. if (state.right) {
  172. return tr("Dpad_Right");
  173. }
  174. if (state.up) {
  175. return tr("Dpad_Up");
  176. }
  177. if (state.down) {
  178. return tr("Dpad_Down");
  179. }
  180. if (state.stick_l) {
  181. return tr("Left_Stick");
  182. }
  183. if (state.stick_r) {
  184. return tr("Right_Stick");
  185. }
  186. if (state.minus) {
  187. return tr("Minus");
  188. }
  189. if (state.plus) {
  190. return tr("Plus");
  191. }
  192. return tr("Invalid");
  193. }
  194. std::pair<bool, QString> ConfigureHotkeys::IsUsedKey(QKeySequence key_sequence) const {
  195. for (int r = 0; r < model->rowCount(); ++r) {
  196. const QStandardItem* const parent = model->item(r, 0);
  197. for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
  198. const QStandardItem* const key_seq_item = parent->child(r2, hotkey_column);
  199. const auto key_seq_str = key_seq_item->text();
  200. const auto key_seq = QKeySequence::fromString(key_seq_str, QKeySequence::NativeText);
  201. if (key_sequence == key_seq) {
  202. return std::make_pair(true, parent->child(r2, 0)->text());
  203. }
  204. }
  205. }
  206. return std::make_pair(false, QString());
  207. }
  208. std::pair<bool, QString> ConfigureHotkeys::IsUsedControllerKey(const QString& key_sequence) const {
  209. for (int r = 0; r < model->rowCount(); ++r) {
  210. const QStandardItem* const parent = model->item(r, 0);
  211. for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
  212. const QStandardItem* const key_seq_item = parent->child(r2, controller_column);
  213. const auto key_seq_str = key_seq_item->text();
  214. if (key_sequence == key_seq_str) {
  215. return std::make_pair(true, parent->child(r2, 0)->text());
  216. }
  217. }
  218. }
  219. return std::make_pair(false, QString());
  220. }
  221. void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) {
  222. for (int key_id = 0; key_id < model->rowCount(); key_id++) {
  223. const QStandardItem* parent = model->item(key_id, 0);
  224. for (int key_column_id = 0; key_column_id < parent->rowCount(); key_column_id++) {
  225. const QStandardItem* action = parent->child(key_column_id, name_column);
  226. const QStandardItem* keyseq = parent->child(key_column_id, hotkey_column);
  227. const QStandardItem* controller_keyseq =
  228. parent->child(key_column_id, controller_column);
  229. for (auto& [group, sub_actions] : registry.hotkey_groups) {
  230. if (group != parent->text())
  231. continue;
  232. for (auto& [action_name, hotkey] : sub_actions) {
  233. if (action_name != action->text())
  234. continue;
  235. hotkey.keyseq = QKeySequence(keyseq->text());
  236. hotkey.controller_keyseq = controller_keyseq->text();
  237. }
  238. }
  239. }
  240. }
  241. registry.SaveHotkeys();
  242. }
  243. void ConfigureHotkeys::RestoreDefaults() {
  244. for (int r = 0; r < model->rowCount(); ++r) {
  245. const QStandardItem* parent = model->item(r, 0);
  246. for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
  247. model->item(r, 0)
  248. ->child(r2, hotkey_column)
  249. ->setText(Config::default_hotkeys[r2].shortcut.keyseq);
  250. model->item(r, 0)
  251. ->child(r2, controller_column)
  252. ->setText(Config::default_hotkeys[r2].shortcut.controller_keyseq);
  253. }
  254. }
  255. }
  256. void ConfigureHotkeys::ClearAll() {
  257. for (int r = 0; r < model->rowCount(); ++r) {
  258. const QStandardItem* parent = model->item(r, 0);
  259. for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
  260. model->item(r, 0)->child(r2, hotkey_column)->setText(QString{});
  261. model->item(r, 0)->child(r2, controller_column)->setText(QString{});
  262. }
  263. }
  264. }
  265. void ConfigureHotkeys::PopupContextMenu(const QPoint& menu_location) {
  266. QModelIndex index = ui->hotkey_list->indexAt(menu_location);
  267. if (!index.parent().isValid()) {
  268. return;
  269. }
  270. // Swap to the hotkey column if the controller hotkey column is not selected
  271. if (index.column() != controller_column) {
  272. index = index.sibling(index.row(), hotkey_column);
  273. }
  274. QMenu context_menu;
  275. QAction* restore_default = context_menu.addAction(tr("Restore Default"));
  276. QAction* clear = context_menu.addAction(tr("Clear"));
  277. connect(restore_default, &QAction::triggered, [this, index] {
  278. if (index.column() == controller_column) {
  279. RestoreControllerHotkey(index);
  280. return;
  281. }
  282. RestoreHotkey(index);
  283. });
  284. connect(clear, &QAction::triggered, [this, index] { model->setData(index, QString{}); });
  285. context_menu.exec(ui->hotkey_list->viewport()->mapToGlobal(menu_location));
  286. }
  287. void ConfigureHotkeys::RestoreControllerHotkey(QModelIndex index) {
  288. const QString& default_key_sequence =
  289. Config::default_hotkeys[index.row()].shortcut.controller_keyseq;
  290. const auto [key_sequence_used, used_action] = IsUsedControllerKey(default_key_sequence);
  291. if (key_sequence_used && default_key_sequence != model->data(index).toString()) {
  292. QMessageBox::warning(
  293. this, tr("Conflicting Button Sequence"),
  294. tr("The default button sequence is already assigned to: %1").arg(used_action));
  295. } else {
  296. model->setData(index, default_key_sequence);
  297. }
  298. }
  299. void ConfigureHotkeys::RestoreHotkey(QModelIndex index) {
  300. const QKeySequence& default_key_sequence = QKeySequence::fromString(
  301. Config::default_hotkeys[index.row()].shortcut.keyseq, QKeySequence::NativeText);
  302. const auto [key_sequence_used, used_action] = IsUsedKey(default_key_sequence);
  303. if (key_sequence_used && default_key_sequence != QKeySequence(model->data(index).toString())) {
  304. QMessageBox::warning(
  305. this, tr("Conflicting Key Sequence"),
  306. tr("The default key sequence is already assigned to: %1").arg(used_action));
  307. } else {
  308. model->setData(index, default_key_sequence.toString(QKeySequence::NativeText));
  309. }
  310. }