configure_hotkeys.cpp 15 KB

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