configure_hotkeys.cpp 13 KB

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