configure_input.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <memory>
  6. #include <utility>
  7. #include <QMessageBox>
  8. #include <QTimer>
  9. #include "common/param_package.h"
  10. #include "input_common/main.h"
  11. #include "yuzu/configuration/config.h"
  12. #include "yuzu/configuration/configure_input.h"
  13. const std::array<std::string, ConfigureInput::ANALOG_SUB_BUTTONS_NUM>
  14. ConfigureInput::analog_sub_buttons{{
  15. "up", "down", "left", "right", "modifier",
  16. }};
  17. static QString getKeyName(int key_code) {
  18. switch (key_code) {
  19. case Qt::Key_Shift:
  20. return QObject::tr("Shift");
  21. case Qt::Key_Control:
  22. return QObject::tr("Ctrl");
  23. case Qt::Key_Alt:
  24. return QObject::tr("Alt");
  25. case Qt::Key_Meta:
  26. return "";
  27. default:
  28. return QKeySequence(key_code).toString();
  29. }
  30. }
  31. static void SetAnalogButton(const Common::ParamPackage& input_param,
  32. Common::ParamPackage& analog_param, const std::string& button_name) {
  33. if (analog_param.Get("engine", "") != "analog_from_button") {
  34. analog_param = {
  35. {"engine", "analog_from_button"}, {"modifier_scale", "0.5"},
  36. };
  37. }
  38. analog_param.Set(button_name, input_param.Serialize());
  39. }
  40. ConfigureInput::ConfigureInput(QWidget* parent)
  41. : QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
  42. timeout_timer(std::make_unique<QTimer>()), poll_timer(std::make_unique<QTimer>()) {
  43. ui->setupUi(this);
  44. setFocusPolicy(Qt::ClickFocus);
  45. button_map = {
  46. ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY,
  47. ui->buttonLStick, ui->buttonRStick, ui->buttonL, ui->buttonR,
  48. ui->buttonZL, ui->buttonZR, ui->buttonPlus, ui->buttonMinus,
  49. ui->buttonDpadLeft, ui->buttonDpadUp, ui->buttonDpadRight, ui->buttonDpadDown,
  50. ui->buttonLStickLeft, ui->buttonLStickUp, ui->buttonLStickRight, ui->buttonLStickDown,
  51. ui->buttonRStickLeft, ui->buttonRStickUp, ui->buttonRStickRight, ui->buttonRStickDown,
  52. ui->buttonSL, ui->buttonSR, ui->buttonHome, ui->buttonScreenshot,
  53. };
  54. analog_map_buttons = {{
  55. {
  56. ui->buttonLStickUp, ui->buttonLStickDown, ui->buttonLStickLeft, ui->buttonLStickRight,
  57. ui->buttonLStickMod,
  58. },
  59. {
  60. ui->buttonRStickUp, ui->buttonRStickDown, ui->buttonRStickLeft, ui->buttonRStickRight,
  61. ui->buttonRStickMod,
  62. },
  63. }};
  64. analog_map_stick = {ui->buttonLStickAnalog, ui->buttonRStickAnalog};
  65. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
  66. if (button_map[button_id])
  67. connect(button_map[button_id], &QPushButton::released, [=]() {
  68. handleClick(
  69. button_map[button_id],
  70. [=](const Common::ParamPackage& params) { buttons_param[button_id] = params; },
  71. InputCommon::Polling::DeviceType::Button);
  72. });
  73. }
  74. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
  75. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
  76. if (analog_map_buttons[analog_id][sub_button_id] != nullptr) {
  77. connect(analog_map_buttons[analog_id][sub_button_id], &QPushButton::released,
  78. [=]() {
  79. handleClick(analog_map_buttons[analog_id][sub_button_id],
  80. [=](const Common::ParamPackage& params) {
  81. SetAnalogButton(params, analogs_param[analog_id],
  82. analog_sub_buttons[sub_button_id]);
  83. },
  84. InputCommon::Polling::DeviceType::Button);
  85. });
  86. }
  87. }
  88. connect(analog_map_stick[analog_id], &QPushButton::released, [=]() {
  89. QMessageBox::information(
  90. this, "Information",
  91. "After pressing OK, first move your joystick horizontally, and then vertically.");
  92. handleClick(
  93. analog_map_stick[analog_id],
  94. [=](const Common::ParamPackage& params) { analogs_param[analog_id] = params; },
  95. InputCommon::Polling::DeviceType::Analog);
  96. });
  97. }
  98. connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); });
  99. timeout_timer->setSingleShot(true);
  100. connect(timeout_timer.get(), &QTimer::timeout, [this]() { setPollingResult({}, true); });
  101. connect(poll_timer.get(), &QTimer::timeout, [this]() {
  102. Common::ParamPackage params;
  103. for (auto& poller : device_pollers) {
  104. params = poller->GetNextInput();
  105. if (params.Has("engine")) {
  106. setPollingResult(params, false);
  107. return;
  108. }
  109. }
  110. });
  111. this->loadConfiguration();
  112. // TODO(wwylele): enable this when we actually emulate it
  113. ui->buttonHome->setEnabled(false);
  114. }
  115. void ConfigureInput::applyConfiguration() {
  116. std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(),
  117. [](const Common::ParamPackage& param) { return param.Serialize(); });
  118. std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(),
  119. [](const Common::ParamPackage& param) { return param.Serialize(); });
  120. Settings::Apply();
  121. }
  122. void ConfigureInput::loadConfiguration() {
  123. std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(),
  124. buttons_param.begin(),
  125. [](const std::string& str) { return Common::ParamPackage(str); });
  126. std::transform(Settings::values.analogs.begin(), Settings::values.analogs.end(),
  127. analogs_param.begin(),
  128. [](const std::string& str) { return Common::ParamPackage(str); });
  129. updateButtonLabels();
  130. }
  131. void ConfigureInput::restoreDefaults() {
  132. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
  133. buttons_param[button_id] = Common::ParamPackage{
  134. InputCommon::GenerateKeyboardParam(Config::default_buttons[button_id])};
  135. }
  136. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
  137. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
  138. Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
  139. Config::default_analogs[analog_id][sub_button_id])};
  140. SetAnalogButton(params, analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
  141. }
  142. }
  143. updateButtonLabels();
  144. applyConfiguration();
  145. }
  146. void ConfigureInput::updateButtonLabels() {
  147. QString non_keyboard(tr("[non-keyboard]"));
  148. auto KeyToText = [&non_keyboard](const Common::ParamPackage& param) {
  149. if (!param.Has("engine")) {
  150. return QString("[not set]");
  151. } else if (param.Get("engine", "") != "keyboard") {
  152. return non_keyboard;
  153. } else {
  154. return getKeyName(param.Get("code", 0));
  155. }
  156. };
  157. for (int button = 0; button < Settings::NativeButton::NumButtons; button++) {
  158. button_map[button]->setText(KeyToText(buttons_param[button]));
  159. }
  160. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
  161. if (analogs_param[analog_id].Get("engine", "") != "analog_from_button") {
  162. for (QPushButton* button : analog_map_buttons[analog_id]) {
  163. if (button)
  164. button->setText(non_keyboard);
  165. }
  166. } else {
  167. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
  168. Common::ParamPackage param(
  169. analogs_param[analog_id].Get(analog_sub_buttons[sub_button_id], ""));
  170. if (analog_map_buttons[analog_id][sub_button_id])
  171. analog_map_buttons[analog_id][sub_button_id]->setText(KeyToText(param));
  172. }
  173. }
  174. analog_map_stick[analog_id]->setText("Set Analog Stick");
  175. }
  176. }
  177. void ConfigureInput::handleClick(QPushButton* button,
  178. std::function<void(const Common::ParamPackage&)> new_input_setter,
  179. InputCommon::Polling::DeviceType type) {
  180. button->setText(tr("[press key]"));
  181. button->setFocus();
  182. input_setter = new_input_setter;
  183. device_pollers = InputCommon::Polling::GetPollers(type);
  184. // Keyboard keys can only be used as button devices
  185. want_keyboard_keys = type == InputCommon::Polling::DeviceType::Button;
  186. for (auto& poller : device_pollers) {
  187. poller->Start();
  188. }
  189. grabKeyboard();
  190. grabMouse();
  191. timeout_timer->start(5000); // Cancel after 5 seconds
  192. poll_timer->start(200); // Check for new inputs every 200ms
  193. }
  194. void ConfigureInput::setPollingResult(const Common::ParamPackage& params, bool abort) {
  195. releaseKeyboard();
  196. releaseMouse();
  197. timeout_timer->stop();
  198. poll_timer->stop();
  199. for (auto& poller : device_pollers) {
  200. poller->Stop();
  201. }
  202. if (!abort) {
  203. (*input_setter)(params);
  204. }
  205. updateButtonLabels();
  206. input_setter = boost::none;
  207. }
  208. void ConfigureInput::keyPressEvent(QKeyEvent* event) {
  209. if (!input_setter || !event)
  210. return;
  211. if (event->key() != Qt::Key_Escape) {
  212. if (want_keyboard_keys) {
  213. setPollingResult(Common::ParamPackage{InputCommon::GenerateKeyboardParam(event->key())},
  214. false);
  215. } else {
  216. // Escape key wasn't pressed and we don't want any keyboard keys, so don't stop polling
  217. return;
  218. }
  219. }
  220. setPollingResult({}, true);
  221. }