configure_input_player.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 <QGridLayout>
  8. #include <QInputDialog>
  9. #include <QKeyEvent>
  10. #include <QMenu>
  11. #include <QMessageBox>
  12. #include <QTimer>
  13. #include "common/param_package.h"
  14. #include "core/core.h"
  15. #include "core/hid/emulated_controller.h"
  16. #include "core/hid/hid_core.h"
  17. #include "core/hid/hid_types.h"
  18. #include "input_common/drivers/keyboard.h"
  19. #include "input_common/drivers/mouse.h"
  20. #include "input_common/main.h"
  21. #include "ui_configure_input_player.h"
  22. #include "yuzu/bootmanager.h"
  23. #include "yuzu/configuration/config.h"
  24. #include "yuzu/configuration/configure_input_player.h"
  25. #include "yuzu/configuration/configure_input_player_widget.h"
  26. #include "yuzu/configuration/configure_vibration.h"
  27. #include "yuzu/configuration/input_profiles.h"
  28. #include "yuzu/util/limitable_input_dialog.h"
  29. const std::array<std::string, ConfigureInputPlayer::ANALOG_SUB_BUTTONS_NUM>
  30. ConfigureInputPlayer::analog_sub_buttons{{
  31. "up",
  32. "down",
  33. "left",
  34. "right",
  35. }};
  36. namespace {
  37. constexpr std::size_t HANDHELD_INDEX = 8;
  38. QString GetKeyName(int key_code) {
  39. switch (key_code) {
  40. case Qt::Key_Shift:
  41. return QObject::tr("Shift");
  42. case Qt::Key_Control:
  43. return QObject::tr("Ctrl");
  44. case Qt::Key_Alt:
  45. return QObject::tr("Alt");
  46. case Qt::Key_Meta:
  47. return {};
  48. default:
  49. return QKeySequence(key_code).toString();
  50. }
  51. }
  52. void SetAnalogParam(const Common::ParamPackage& input_param, Common::ParamPackage& analog_param,
  53. const std::string& button_name) {
  54. // The poller returned a complete axis, so set all the buttons
  55. if (input_param.Has("axis_x") && input_param.Has("axis_y")) {
  56. analog_param = input_param;
  57. return;
  58. }
  59. // Check if the current configuration has either no engine or an axis binding.
  60. // Clears out the old binding and adds one with analog_from_button.
  61. if (!analog_param.Has("engine") || analog_param.Has("axis_x") || analog_param.Has("axis_y")) {
  62. analog_param = {
  63. {"engine", "analog_from_button"},
  64. };
  65. }
  66. analog_param.Set(button_name, input_param.Serialize());
  67. }
  68. } // namespace
  69. QString ConfigureInputPlayer::ButtonToText(const Common::ParamPackage& param) {
  70. if (!param.Has("engine")) {
  71. return QObject::tr("[not set]");
  72. }
  73. // Retrieve the names from Qt
  74. if (param.Get("engine", "") == "keyboard") {
  75. const QString button_str = GetKeyName(param.Get("code", 0));
  76. const QString toggle = QString::fromStdString(param.Get("toggle", false) ? "~" : "");
  77. return QObject::tr("%1%2").arg(toggle, button_str);
  78. }
  79. std::string button_name = input_subsystem->GetButtonName(param);
  80. return QString::fromStdString(button_name);
  81. }
  82. QString ConfigureInputPlayer::AnalogToText(const Common::ParamPackage& param,
  83. const std::string& dir) {
  84. if (!param.Has("engine")) {
  85. return QObject::tr("[not set]");
  86. }
  87. if (param.Get("engine", "") == "analog_from_button") {
  88. return ButtonToText(Common::ParamPackage{param.Get(dir, "")});
  89. }
  90. if (!param.Has("axis_x") || !param.Has("axis_y")) {
  91. return QObject::tr("[unknown]");
  92. }
  93. const auto engine_str = param.Get("engine", "");
  94. const QString axis_x_str = QString::fromStdString(param.Get("axis_x", ""));
  95. const QString axis_y_str = QString::fromStdString(param.Get("axis_y", ""));
  96. const bool invert_x = param.Get("invert_x", "+") == "-";
  97. const bool invert_y = param.Get("invert_y", "+") == "-";
  98. if (dir == "modifier") {
  99. return QObject::tr("[unused]");
  100. }
  101. if (dir == "left") {
  102. const QString invert_x_str = QString::fromStdString(invert_x ? "+" : "-");
  103. return QObject::tr("Axis %1%2").arg(axis_x_str, invert_x_str);
  104. }
  105. if (dir == "right") {
  106. const QString invert_x_str = QString::fromStdString(invert_x ? "-" : "+");
  107. return QObject::tr("Axis %1%2").arg(axis_x_str, invert_x_str);
  108. }
  109. if (dir == "up") {
  110. const QString invert_y_str = QString::fromStdString(invert_y ? "-" : "+");
  111. return QObject::tr("Axis %1%2").arg(axis_y_str, invert_y_str);
  112. }
  113. if (dir == "down") {
  114. const QString invert_y_str = QString::fromStdString(invert_y ? "+" : "-");
  115. return QObject::tr("Axis %1%2").arg(axis_y_str, invert_y_str);
  116. }
  117. return QObject::tr("[unknown]");
  118. }
  119. ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_index,
  120. QWidget* bottom_row,
  121. InputCommon::InputSubsystem* input_subsystem_,
  122. InputProfiles* profiles_, Core::System& system_,
  123. bool debug)
  124. : QWidget(parent), ui(std::make_unique<Ui::ConfigureInputPlayer>()), player_index(player_index),
  125. debug(debug), input_subsystem{input_subsystem_}, profiles(profiles_),
  126. timeout_timer(std::make_unique<QTimer>()), poll_timer(std::make_unique<QTimer>()),
  127. bottom_row(bottom_row), system{system_} {
  128. emulated_controller = system_.HIDCore().GetEmulatedControllerByIndex(player_index);
  129. emulated_controller->EnableConfiguration();
  130. ui->setupUi(this);
  131. setFocusPolicy(Qt::ClickFocus);
  132. button_map = {
  133. ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY,
  134. ui->buttonLStick, ui->buttonRStick, ui->buttonL, ui->buttonR,
  135. ui->buttonZL, ui->buttonZR, ui->buttonPlus, ui->buttonMinus,
  136. ui->buttonDpadLeft, ui->buttonDpadUp, ui->buttonDpadRight, ui->buttonDpadDown,
  137. ui->buttonSL, ui->buttonSR, ui->buttonHome, ui->buttonScreenshot,
  138. };
  139. analog_map_buttons = {{
  140. {
  141. ui->buttonLStickUp,
  142. ui->buttonLStickDown,
  143. ui->buttonLStickLeft,
  144. ui->buttonLStickRight,
  145. },
  146. {
  147. ui->buttonRStickUp,
  148. ui->buttonRStickDown,
  149. ui->buttonRStickLeft,
  150. ui->buttonRStickRight,
  151. },
  152. }};
  153. motion_map = {
  154. ui->buttonMotionLeft,
  155. ui->buttonMotionRight,
  156. };
  157. analog_map_deadzone_label = {ui->labelLStickDeadzone, ui->labelRStickDeadzone};
  158. analog_map_deadzone_slider = {ui->sliderLStickDeadzone, ui->sliderRStickDeadzone};
  159. analog_map_modifier_groupbox = {ui->buttonLStickModGroup, ui->buttonRStickModGroup};
  160. analog_map_modifier_button = {ui->buttonLStickMod, ui->buttonRStickMod};
  161. analog_map_modifier_label = {ui->labelLStickModifierRange, ui->labelRStickModifierRange};
  162. analog_map_modifier_slider = {ui->sliderLStickModifierRange, ui->sliderRStickModifierRange};
  163. analog_map_range_groupbox = {ui->buttonLStickRangeGroup, ui->buttonRStickRangeGroup};
  164. analog_map_range_spinbox = {ui->spinboxLStickRange, ui->spinboxRStickRange};
  165. ui->controllerFrame->SetController(emulated_controller);
  166. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  167. auto* const button = button_map[button_id];
  168. if (button == nullptr) {
  169. continue;
  170. }
  171. connect(button, &QPushButton::clicked, [=, this] {
  172. HandleClick(
  173. button, button_id,
  174. [=, this](Common::ParamPackage params) {
  175. emulated_controller->SetButtonParam(button_id, params);
  176. },
  177. InputCommon::Polling::InputType::Button);
  178. });
  179. button->setContextMenuPolicy(Qt::CustomContextMenu);
  180. connect(button, &QPushButton::customContextMenuRequested,
  181. [=, this](const QPoint& menu_location) {
  182. QMenu context_menu;
  183. Common::ParamPackage param = emulated_controller->GetButtonParam(button_id);
  184. context_menu.addAction(tr("Clear"), [&] {
  185. emulated_controller->SetButtonParam(button_id, {});
  186. button_map[button_id]->setText(tr("[not set]"));
  187. });
  188. if (param.Has("button") || param.Has("hat")) {
  189. context_menu.addAction(tr("Toggle button"), [&] {
  190. const bool toggle_value = !param.Get("toggle", false);
  191. param.Set("toggle", toggle_value);
  192. button_map[button_id]->setText(ButtonToText(param));
  193. emulated_controller->SetButtonParam(button_id, param);
  194. });
  195. context_menu.addAction(tr("Invert button"), [&] {
  196. const bool toggle_value = !param.Get("inverted", false);
  197. param.Set("inverted", toggle_value);
  198. button_map[button_id]->setText(ButtonToText(param));
  199. emulated_controller->SetButtonParam(button_id, param);
  200. });
  201. }
  202. if (param.Has("axis")) {
  203. context_menu.addAction(tr("Invert axis"), [&] {
  204. const bool toggle_value = !(param.Get("invert", "+") == "-");
  205. param.Set("invert", toggle_value ? "-" : "+");
  206. button_map[button_id]->setText(ButtonToText(param));
  207. emulated_controller->SetButtonParam(button_id, param);
  208. });
  209. context_menu.addAction(tr("Set threshold"), [&] {
  210. const int button_threshold =
  211. static_cast<int>(param.Get("threshold", 0.5f) * 100.0f);
  212. const int new_threshold = QInputDialog::getInt(
  213. this, tr("Set threshold"), tr("Choose a value between 0% and 100%"),
  214. button_threshold, 0, 100);
  215. param.Set("threshold", new_threshold / 100.0f);
  216. if (button_id == Settings::NativeButton::ZL) {
  217. ui->sliderZLThreshold->setValue(new_threshold);
  218. }
  219. if (button_id == Settings::NativeButton::ZR) {
  220. ui->sliderZRThreshold->setValue(new_threshold);
  221. }
  222. emulated_controller->SetButtonParam(button_id, param);
  223. });
  224. }
  225. context_menu.exec(button_map[button_id]->mapToGlobal(menu_location));
  226. });
  227. }
  228. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  229. auto* const button = motion_map[motion_id];
  230. if (button == nullptr) {
  231. continue;
  232. }
  233. connect(button, &QPushButton::clicked, [=, this] {
  234. HandleClick(
  235. button, motion_id,
  236. [=, this](Common::ParamPackage params) {
  237. emulated_controller->SetMotionParam(motion_id, params);
  238. },
  239. InputCommon::Polling::InputType::Motion);
  240. });
  241. button->setContextMenuPolicy(Qt::CustomContextMenu);
  242. connect(button, &QPushButton::customContextMenuRequested,
  243. [=, this](const QPoint& menu_location) {
  244. QMenu context_menu;
  245. context_menu.addAction(tr("Clear"), [&] {
  246. emulated_controller->SetMotionParam(motion_id, {});
  247. motion_map[motion_id]->setText(tr("[not set]"));
  248. });
  249. context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location));
  250. });
  251. }
  252. connect(ui->sliderZLThreshold, &QSlider::valueChanged, [=, this] {
  253. Common::ParamPackage param =
  254. emulated_controller->GetButtonParam(Settings::NativeButton::ZL);
  255. if (param.Has("threshold")) {
  256. const auto slider_value = ui->sliderZLThreshold->value();
  257. param.Set("threshold", slider_value / 100.0f);
  258. emulated_controller->SetButtonParam(Settings::NativeButton::ZL, param);
  259. }
  260. });
  261. connect(ui->sliderZRThreshold, &QSlider::valueChanged, [=, this] {
  262. Common::ParamPackage param =
  263. emulated_controller->GetButtonParam(Settings::NativeButton::ZR);
  264. if (param.Has("threshold")) {
  265. const auto slider_value = ui->sliderZRThreshold->value();
  266. param.Set("threshold", slider_value / 100.0f);
  267. emulated_controller->SetButtonParam(Settings::NativeButton::ZR, param);
  268. }
  269. });
  270. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  271. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  272. auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  273. if (analog_button == nullptr) {
  274. continue;
  275. }
  276. connect(analog_button, &QPushButton::clicked, [=, this] {
  277. if (!map_analog_stick_accepted) {
  278. map_analog_stick_accepted =
  279. QMessageBox::information(
  280. this, tr("Map Analog Stick"),
  281. tr("After pressing OK, first move your joystick horizontally, and then "
  282. "vertically.\nTo invert the axes, first move your joystick "
  283. "vertically, and then horizontally."),
  284. QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok;
  285. if (!map_analog_stick_accepted) {
  286. return;
  287. }
  288. }
  289. HandleClick(
  290. analog_map_buttons[analog_id][sub_button_id], analog_id,
  291. [=, this](const Common::ParamPackage& params) {
  292. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  293. SetAnalogParam(params, param, analog_sub_buttons[sub_button_id]);
  294. emulated_controller->SetStickParam(analog_id, param);
  295. },
  296. InputCommon::Polling::InputType::Stick);
  297. });
  298. analog_button->setContextMenuPolicy(Qt::CustomContextMenu);
  299. connect(analog_button, &QPushButton::customContextMenuRequested,
  300. [=, this](const QPoint& menu_location) {
  301. QMenu context_menu;
  302. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  303. context_menu.addAction(tr("Clear"), [&] {
  304. emulated_controller->SetStickParam(analog_id, {});
  305. analog_map_buttons[analog_id][sub_button_id]->setText(tr("[not set]"));
  306. });
  307. context_menu.addAction(tr("Invert axis"), [&] {
  308. if (sub_button_id == 2 || sub_button_id == 3) {
  309. const bool invert_value = param.Get("invert_x", "+") == "-";
  310. const std::string invert_str = invert_value ? "+" : "-";
  311. param.Set("invert_x", invert_str);
  312. emulated_controller->SetStickParam(analog_id, param);
  313. }
  314. if (sub_button_id == 0 || sub_button_id == 1) {
  315. const bool invert_value = param.Get("invert_y", "+") == "-";
  316. const std::string invert_str = invert_value ? "+" : "-";
  317. param.Set("invert_y", invert_str);
  318. emulated_controller->SetStickParam(analog_id, param);
  319. }
  320. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM;
  321. ++sub_button_id) {
  322. analog_map_buttons[analog_id][sub_button_id]->setText(
  323. AnalogToText(param, analog_sub_buttons[sub_button_id]));
  324. }
  325. });
  326. context_menu.exec(analog_map_buttons[analog_id][sub_button_id]->mapToGlobal(
  327. menu_location));
  328. });
  329. }
  330. // Handle clicks for the modifier buttons as well.
  331. connect(analog_map_modifier_button[analog_id], &QPushButton::clicked, [=, this] {
  332. HandleClick(
  333. analog_map_modifier_button[analog_id], analog_id,
  334. [=, this](const Common::ParamPackage& params) {
  335. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  336. param.Set("modifier", params.Serialize());
  337. emulated_controller->SetStickParam(analog_id, param);
  338. },
  339. InputCommon::Polling::InputType::Button);
  340. });
  341. analog_map_modifier_button[analog_id]->setContextMenuPolicy(Qt::CustomContextMenu);
  342. connect(analog_map_modifier_button[analog_id], &QPushButton::customContextMenuRequested,
  343. [=, this](const QPoint& menu_location) {
  344. QMenu context_menu;
  345. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  346. context_menu.addAction(tr("Clear"), [&] {
  347. param.Set("modifier", "");
  348. analog_map_modifier_button[analog_id]->setText(tr("[not set]"));
  349. emulated_controller->SetStickParam(analog_id, param);
  350. });
  351. context_menu.addAction(tr("Toggle button"), [&] {
  352. Common::ParamPackage modifier_param =
  353. Common::ParamPackage{param.Get("modifier", "")};
  354. const bool toggle_value = !modifier_param.Get("toggle", false);
  355. modifier_param.Set("toggle", toggle_value);
  356. param.Set("modifier", modifier_param.Serialize());
  357. analog_map_modifier_button[analog_id]->setText(
  358. ButtonToText(modifier_param));
  359. emulated_controller->SetStickParam(analog_id, param);
  360. });
  361. context_menu.exec(
  362. analog_map_modifier_button[analog_id]->mapToGlobal(menu_location));
  363. });
  364. connect(analog_map_range_spinbox[analog_id], qOverload<int>(&QSpinBox::valueChanged),
  365. [=, this] {
  366. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  367. const auto spinbox_value = analog_map_range_spinbox[analog_id]->value();
  368. param.Set("range", spinbox_value / 100.0f);
  369. emulated_controller->SetStickParam(analog_id, param);
  370. });
  371. connect(analog_map_deadzone_slider[analog_id], &QSlider::valueChanged, [=, this] {
  372. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  373. const auto slider_value = analog_map_deadzone_slider[analog_id]->value();
  374. analog_map_deadzone_label[analog_id]->setText(tr("Deadzone: %1%").arg(slider_value));
  375. param.Set("deadzone", slider_value / 100.0f);
  376. emulated_controller->SetStickParam(analog_id, param);
  377. });
  378. connect(analog_map_modifier_slider[analog_id], &QSlider::valueChanged, [=, this] {
  379. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  380. const auto slider_value = analog_map_modifier_slider[analog_id]->value();
  381. analog_map_modifier_label[analog_id]->setText(
  382. tr("Modifier Range: %1%").arg(slider_value));
  383. param.Set("modifier_scale", slider_value / 100.0f);
  384. emulated_controller->SetStickParam(analog_id, param);
  385. });
  386. }
  387. // Player Connected checkbox
  388. connect(ui->groupConnectedController, &QGroupBox::toggled,
  389. [this](bool checked) { emit Connected(checked); });
  390. if (player_index == 0) {
  391. connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged),
  392. [this](int index) {
  393. emit HandheldStateChanged(GetControllerTypeFromIndex(index) ==
  394. Core::HID::NpadType::Handheld);
  395. });
  396. }
  397. if (debug || player_index == 9) {
  398. ui->groupConnectedController->setCheckable(false);
  399. }
  400. // The Debug Controller can only choose the Pro Controller.
  401. if (debug) {
  402. ui->buttonScreenshot->setEnabled(false);
  403. ui->buttonHome->setEnabled(false);
  404. ui->comboControllerType->addItem(tr("Pro Controller"));
  405. } else {
  406. SetConnectableControllers();
  407. }
  408. UpdateControllerAvailableButtons();
  409. UpdateControllerEnabledButtons();
  410. UpdateControllerButtonNames();
  411. UpdateMotionButtons();
  412. connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged), [this](int) {
  413. UpdateControllerAvailableButtons();
  414. UpdateControllerEnabledButtons();
  415. UpdateControllerButtonNames();
  416. UpdateMotionButtons();
  417. emulated_controller->SetNpadType(
  418. GetControllerTypeFromIndex(ui->comboControllerType->currentIndex()));
  419. });
  420. connect(ui->comboDevices, qOverload<int>(&QComboBox::activated), this,
  421. &ConfigureInputPlayer::UpdateMappingWithDefaults);
  422. ui->comboDevices->setCurrentIndex(-1);
  423. ui->buttonRefreshDevices->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
  424. connect(ui->buttonRefreshDevices, &QPushButton::clicked,
  425. [this] { emit RefreshInputDevices(); });
  426. timeout_timer->setSingleShot(true);
  427. connect(timeout_timer.get(), &QTimer::timeout, [this] { SetPollingResult({}, true); });
  428. connect(poll_timer.get(), &QTimer::timeout, [this] {
  429. const auto& params = input_subsystem->GetNextInput();
  430. if (params.Has("engine") && IsInputAcceptable(params)) {
  431. SetPollingResult(params, false);
  432. return;
  433. }
  434. });
  435. UpdateInputProfiles();
  436. connect(ui->buttonProfilesNew, &QPushButton::clicked, this,
  437. &ConfigureInputPlayer::CreateProfile);
  438. connect(ui->buttonProfilesDelete, &QPushButton::clicked, this,
  439. &ConfigureInputPlayer::DeleteProfile);
  440. connect(ui->comboProfiles, qOverload<int>(&QComboBox::activated), this,
  441. &ConfigureInputPlayer::LoadProfile);
  442. connect(ui->buttonProfilesSave, &QPushButton::clicked, this,
  443. &ConfigureInputPlayer::SaveProfile);
  444. LoadConfiguration();
  445. }
  446. ConfigureInputPlayer::~ConfigureInputPlayer() {
  447. emulated_controller->DisableConfiguration();
  448. };
  449. void ConfigureInputPlayer::ApplyConfiguration() {
  450. emulated_controller->SaveCurrentConfig();
  451. }
  452. void ConfigureInputPlayer::showEvent(QShowEvent* event) {
  453. if (bottom_row == nullptr) {
  454. return;
  455. }
  456. QWidget::showEvent(event);
  457. ui->main->addWidget(bottom_row);
  458. }
  459. void ConfigureInputPlayer::changeEvent(QEvent* event) {
  460. if (event->type() == QEvent::LanguageChange) {
  461. RetranslateUI();
  462. }
  463. QWidget::changeEvent(event);
  464. }
  465. void ConfigureInputPlayer::RetranslateUI() {
  466. ui->retranslateUi(this);
  467. UpdateUI();
  468. }
  469. void ConfigureInputPlayer::LoadConfiguration() {
  470. UpdateUI();
  471. UpdateInputDeviceCombobox();
  472. const int comboBoxIndex = GetIndexFromControllerType(emulated_controller->GetNpadType());
  473. ui->comboControllerType->setCurrentIndex(comboBoxIndex);
  474. ui->groupConnectedController->setChecked(emulated_controller->IsConnected());
  475. }
  476. void ConfigureInputPlayer::ConnectPlayer(bool connected) {
  477. ui->groupConnectedController->setChecked(connected);
  478. if (connected) {
  479. emulated_controller->Connect();
  480. } else {
  481. emulated_controller->Disconnect();
  482. }
  483. }
  484. void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
  485. // Skip input device persistence if "Input Devices" is set to "Any".
  486. if (ui->comboDevices->currentIndex() == 0) {
  487. UpdateInputDevices();
  488. return;
  489. }
  490. const auto devices = emulated_controller->GetMappedDevices();
  491. UpdateInputDevices();
  492. if (devices.empty()) {
  493. return;
  494. }
  495. if (devices.size() > 2) {
  496. ui->comboDevices->setCurrentIndex(0);
  497. return;
  498. }
  499. const auto first_engine = devices[0].Get("engine", "");
  500. const auto first_guid = devices[0].Get("guid", "");
  501. const auto first_port = devices[0].Get("port", "");
  502. if (devices.size() == 1) {
  503. const auto devices_it =
  504. std::find_if(input_devices.begin(), input_devices.end(),
  505. [first_engine, first_guid, first_port](const Common::ParamPackage param) {
  506. return param.Get("engine", "") == first_engine &&
  507. param.Get("guid", "") == first_guid &&
  508. param.Get("port", "") == first_port;
  509. });
  510. const int device_index =
  511. devices_it != input_devices.end()
  512. ? static_cast<int>(std::distance(input_devices.begin(), devices_it))
  513. : 0;
  514. ui->comboDevices->setCurrentIndex(device_index);
  515. return;
  516. }
  517. const auto second_engine = devices[1].Get("engine", "");
  518. const auto second_guid = devices[1].Get("guid", "");
  519. const auto second_port = devices[1].Get("port", "");
  520. const bool is_keyboard_mouse = (first_engine == "keyboard" || first_engine == "mouse") &&
  521. (second_engine == "keyboard" || second_engine == "mouse");
  522. if (is_keyboard_mouse) {
  523. ui->comboDevices->setCurrentIndex(2);
  524. return;
  525. }
  526. const bool is_engine_equal = first_engine == second_engine;
  527. const bool is_port_equal = first_port == second_port;
  528. if (is_engine_equal && is_port_equal) {
  529. const auto devices_it = std::find_if(
  530. input_devices.begin(), input_devices.end(),
  531. [first_engine, first_guid, second_guid, first_port](const Common::ParamPackage param) {
  532. const bool is_guid_valid =
  533. (param.Get("guid", "") == first_guid &&
  534. param.Get("guid2", "") == second_guid) ||
  535. (param.Get("guid", "") == second_guid && param.Get("guid2", "") == first_guid);
  536. return param.Get("engine", "") == first_engine && is_guid_valid &&
  537. param.Get("port", "") == first_port;
  538. });
  539. const int device_index =
  540. devices_it != input_devices.end()
  541. ? static_cast<int>(std::distance(input_devices.begin(), devices_it))
  542. : 0;
  543. ui->comboDevices->setCurrentIndex(device_index);
  544. } else {
  545. ui->comboDevices->setCurrentIndex(0);
  546. }
  547. }
  548. void ConfigureInputPlayer::RestoreDefaults() {
  549. UpdateMappingWithDefaults();
  550. }
  551. void ConfigureInputPlayer::ClearAll() {
  552. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  553. const auto* const button = button_map[button_id];
  554. if (button == nullptr) {
  555. continue;
  556. }
  557. emulated_controller->SetButtonParam(button_id, {});
  558. }
  559. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  560. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  561. const auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  562. if (analog_button == nullptr) {
  563. continue;
  564. }
  565. emulated_controller->SetStickParam(analog_id, {});
  566. }
  567. }
  568. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  569. const auto* const motion_button = motion_map[motion_id];
  570. if (motion_button == nullptr) {
  571. continue;
  572. }
  573. emulated_controller->SetMotionParam(motion_id, {});
  574. }
  575. UpdateUI();
  576. UpdateInputDevices();
  577. }
  578. void ConfigureInputPlayer::UpdateUI() {
  579. for (int button = 0; button < Settings::NativeButton::NumButtons; ++button) {
  580. const Common::ParamPackage param = emulated_controller->GetButtonParam(button);
  581. button_map[button]->setText(ButtonToText(param));
  582. }
  583. const Common::ParamPackage ZL_param =
  584. emulated_controller->GetButtonParam(Settings::NativeButton::ZL);
  585. if (ZL_param.Has("threshold")) {
  586. const int button_threshold = static_cast<int>(ZL_param.Get("threshold", 0.5f) * 100.0f);
  587. ui->sliderZLThreshold->setValue(button_threshold);
  588. }
  589. const Common::ParamPackage ZR_param =
  590. emulated_controller->GetButtonParam(Settings::NativeButton::ZR);
  591. if (ZR_param.Has("threshold")) {
  592. const int button_threshold = static_cast<int>(ZR_param.Get("threshold", 0.5f) * 100.0f);
  593. ui->sliderZRThreshold->setValue(button_threshold);
  594. }
  595. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  596. const Common::ParamPackage param = emulated_controller->GetMotionParam(motion_id);
  597. motion_map[motion_id]->setText(ButtonToText(param));
  598. }
  599. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  600. const Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  601. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  602. auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  603. if (analog_button == nullptr) {
  604. continue;
  605. }
  606. analog_button->setText(AnalogToText(param, analog_sub_buttons[sub_button_id]));
  607. }
  608. analog_map_modifier_button[analog_id]->setText(
  609. ButtonToText(Common::ParamPackage{param.Get("modifier", "")}));
  610. const auto deadzone_label = analog_map_deadzone_label[analog_id];
  611. const auto deadzone_slider = analog_map_deadzone_slider[analog_id];
  612. const auto modifier_groupbox = analog_map_modifier_groupbox[analog_id];
  613. const auto modifier_label = analog_map_modifier_label[analog_id];
  614. const auto modifier_slider = analog_map_modifier_slider[analog_id];
  615. const auto range_groupbox = analog_map_range_groupbox[analog_id];
  616. const auto range_spinbox = analog_map_range_spinbox[analog_id];
  617. int slider_value;
  618. const bool is_controller = input_subsystem->IsController(param);
  619. if (is_controller) {
  620. slider_value = static_cast<int>(param.Get("deadzone", 0.15f) * 100);
  621. deadzone_label->setText(tr("Deadzone: %1%").arg(slider_value));
  622. deadzone_slider->setValue(slider_value);
  623. range_spinbox->setValue(static_cast<int>(param.Get("range", 1.0f) * 100));
  624. } else {
  625. slider_value = static_cast<int>(param.Get("modifier_scale", 0.5f) * 100);
  626. modifier_label->setText(tr("Modifier Range: %1%").arg(slider_value));
  627. modifier_slider->setValue(slider_value);
  628. }
  629. deadzone_label->setVisible(is_controller);
  630. deadzone_slider->setVisible(is_controller);
  631. modifier_groupbox->setVisible(!is_controller);
  632. modifier_label->setVisible(!is_controller);
  633. modifier_slider->setVisible(!is_controller);
  634. range_groupbox->setVisible(is_controller);
  635. }
  636. }
  637. void ConfigureInputPlayer::SetConnectableControllers() {
  638. const auto add_controllers = [this](bool enable_all,
  639. Core::HID::NpadStyleTag npad_style_set = {}) {
  640. index_controller_type_pairs.clear();
  641. ui->comboControllerType->clear();
  642. if (enable_all || npad_style_set.fullkey == 1) {
  643. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  644. Core::HID::NpadType::ProController);
  645. ui->comboControllerType->addItem(tr("Pro Controller"));
  646. }
  647. if (enable_all || npad_style_set.joycon_dual == 1) {
  648. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  649. Core::HID::NpadType::JoyconDual);
  650. ui->comboControllerType->addItem(tr("Dual Joycons"));
  651. }
  652. if (enable_all || npad_style_set.joycon_left == 1) {
  653. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  654. Core::HID::NpadType::JoyconLeft);
  655. ui->comboControllerType->addItem(tr("Left Joycon"));
  656. }
  657. if (enable_all || npad_style_set.joycon_right == 1) {
  658. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  659. Core::HID::NpadType::JoyconRight);
  660. ui->comboControllerType->addItem(tr("Right Joycon"));
  661. }
  662. if (player_index == 0 && (enable_all || npad_style_set.handheld == 1)) {
  663. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  664. Core::HID::NpadType::Handheld);
  665. ui->comboControllerType->addItem(tr("Handheld"));
  666. }
  667. if (enable_all || npad_style_set.gamecube == 1) {
  668. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  669. Core::HID::NpadType::GameCube);
  670. ui->comboControllerType->addItem(tr("GameCube Controller"));
  671. }
  672. };
  673. if (!system.IsPoweredOn()) {
  674. add_controllers(true);
  675. return;
  676. }
  677. add_controllers(false, system.HIDCore().GetSupportedStyleTag());
  678. }
  679. Core::HID::NpadType ConfigureInputPlayer::GetControllerTypeFromIndex(int index) const {
  680. const auto it =
  681. std::find_if(index_controller_type_pairs.begin(), index_controller_type_pairs.end(),
  682. [index](const auto& pair) { return pair.first == index; });
  683. if (it == index_controller_type_pairs.end()) {
  684. return Core::HID::NpadType::ProController;
  685. }
  686. return it->second;
  687. }
  688. int ConfigureInputPlayer::GetIndexFromControllerType(Core::HID::NpadType type) const {
  689. const auto it =
  690. std::find_if(index_controller_type_pairs.begin(), index_controller_type_pairs.end(),
  691. [type](const auto& pair) { return pair.second == type; });
  692. if (it == index_controller_type_pairs.end()) {
  693. return -1;
  694. }
  695. return it->first;
  696. }
  697. void ConfigureInputPlayer::UpdateInputDevices() {
  698. input_devices = input_subsystem->GetInputDevices();
  699. ui->comboDevices->clear();
  700. for (auto device : input_devices) {
  701. ui->comboDevices->addItem(QString::fromStdString(device.Get("display", "Unknown")), {});
  702. }
  703. }
  704. void ConfigureInputPlayer::UpdateControllerAvailableButtons() {
  705. auto layout = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  706. if (debug) {
  707. layout = Core::HID::NpadType::ProController;
  708. }
  709. // List of all the widgets that will be hidden by any of the following layouts that need
  710. // "unhidden" after the controller type changes
  711. const std::array<QWidget*, 11> layout_show = {
  712. ui->buttonShoulderButtonsSLSR,
  713. ui->horizontalSpacerShoulderButtonsWidget,
  714. ui->horizontalSpacerShoulderButtonsWidget2,
  715. ui->buttonShoulderButtonsLeft,
  716. ui->buttonMiscButtonsMinusScreenshot,
  717. ui->bottomLeft,
  718. ui->buttonShoulderButtonsRight,
  719. ui->buttonMiscButtonsPlusHome,
  720. ui->bottomRight,
  721. ui->buttonMiscButtonsMinusGroup,
  722. ui->buttonMiscButtonsScreenshotGroup,
  723. };
  724. for (auto* widget : layout_show) {
  725. widget->show();
  726. }
  727. std::vector<QWidget*> layout_hidden;
  728. switch (layout) {
  729. case Core::HID::NpadType::ProController:
  730. case Core::HID::NpadType::JoyconDual:
  731. case Core::HID::NpadType::Handheld:
  732. layout_hidden = {
  733. ui->buttonShoulderButtonsSLSR,
  734. ui->horizontalSpacerShoulderButtonsWidget2,
  735. };
  736. break;
  737. case Core::HID::NpadType::JoyconLeft:
  738. layout_hidden = {
  739. ui->horizontalSpacerShoulderButtonsWidget2,
  740. ui->buttonShoulderButtonsRight,
  741. ui->buttonMiscButtonsPlusHome,
  742. ui->bottomRight,
  743. };
  744. break;
  745. case Core::HID::NpadType::JoyconRight:
  746. layout_hidden = {
  747. ui->horizontalSpacerShoulderButtonsWidget,
  748. ui->buttonShoulderButtonsLeft,
  749. ui->buttonMiscButtonsMinusScreenshot,
  750. ui->bottomLeft,
  751. };
  752. break;
  753. case Core::HID::NpadType::GameCube:
  754. layout_hidden = {
  755. ui->buttonShoulderButtonsSLSR,
  756. ui->horizontalSpacerShoulderButtonsWidget2,
  757. ui->buttonMiscButtonsMinusGroup,
  758. ui->buttonMiscButtonsScreenshotGroup,
  759. };
  760. break;
  761. default:
  762. break;
  763. }
  764. for (auto* widget : layout_hidden) {
  765. widget->hide();
  766. }
  767. }
  768. void ConfigureInputPlayer::UpdateControllerEnabledButtons() {
  769. auto layout = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  770. if (debug) {
  771. layout = Core::HID::NpadType::ProController;
  772. }
  773. // List of all the widgets that will be disabled by any of the following layouts that need
  774. // "enabled" after the controller type changes
  775. const std::array<QWidget*, 3> layout_enable = {
  776. ui->buttonLStickPressedGroup,
  777. ui->groupRStickPressed,
  778. ui->buttonShoulderButtonsButtonLGroup,
  779. };
  780. for (auto* widget : layout_enable) {
  781. widget->setEnabled(true);
  782. }
  783. std::vector<QWidget*> layout_disable;
  784. switch (layout) {
  785. case Core::HID::NpadType::ProController:
  786. case Core::HID::NpadType::JoyconDual:
  787. case Core::HID::NpadType::Handheld:
  788. case Core::HID::NpadType::JoyconLeft:
  789. case Core::HID::NpadType::JoyconRight:
  790. break;
  791. case Core::HID::NpadType::GameCube:
  792. layout_disable = {
  793. ui->buttonHome,
  794. ui->buttonLStickPressedGroup,
  795. ui->groupRStickPressed,
  796. ui->buttonShoulderButtonsButtonLGroup,
  797. };
  798. break;
  799. default:
  800. break;
  801. }
  802. for (auto* widget : layout_disable) {
  803. widget->setEnabled(false);
  804. }
  805. }
  806. void ConfigureInputPlayer::UpdateMotionButtons() {
  807. if (debug) {
  808. // Motion isn't used with the debug controller, hide both groupboxes.
  809. ui->buttonMotionLeftGroup->hide();
  810. ui->buttonMotionRightGroup->hide();
  811. return;
  812. }
  813. // Show/hide the "Motion 1/2" groupboxes depending on the currently selected controller.
  814. switch (GetControllerTypeFromIndex(ui->comboControllerType->currentIndex())) {
  815. case Core::HID::NpadType::ProController:
  816. case Core::HID::NpadType::JoyconLeft:
  817. case Core::HID::NpadType::Handheld:
  818. // Show "Motion 1" and hide "Motion 2".
  819. ui->buttonMotionLeftGroup->show();
  820. ui->buttonMotionRightGroup->hide();
  821. break;
  822. case Core::HID::NpadType::JoyconRight:
  823. // Show "Motion 2" and hide "Motion 1".
  824. ui->buttonMotionLeftGroup->hide();
  825. ui->buttonMotionRightGroup->show();
  826. break;
  827. case Core::HID::NpadType::GameCube:
  828. // Hide both "Motion 1/2".
  829. ui->buttonMotionLeftGroup->hide();
  830. ui->buttonMotionRightGroup->hide();
  831. break;
  832. case Core::HID::NpadType::JoyconDual:
  833. default:
  834. // Show both "Motion 1/2".
  835. ui->buttonMotionLeftGroup->show();
  836. ui->buttonMotionRightGroup->show();
  837. break;
  838. }
  839. }
  840. void ConfigureInputPlayer::UpdateControllerButtonNames() {
  841. auto layout = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  842. if (debug) {
  843. layout = Core::HID::NpadType::ProController;
  844. }
  845. switch (layout) {
  846. case Core::HID::NpadType::ProController:
  847. case Core::HID::NpadType::JoyconDual:
  848. case Core::HID::NpadType::Handheld:
  849. case Core::HID::NpadType::JoyconLeft:
  850. case Core::HID::NpadType::JoyconRight:
  851. ui->buttonMiscButtonsPlusGroup->setTitle(tr("Plus"));
  852. ui->buttonShoulderButtonsButtonZLGroup->setTitle(tr("ZL"));
  853. ui->buttonShoulderButtonsZRGroup->setTitle(tr("ZR"));
  854. ui->buttonShoulderButtonsRGroup->setTitle(tr("R"));
  855. ui->LStick->setTitle(tr("Left Stick"));
  856. ui->RStick->setTitle(tr("Right Stick"));
  857. break;
  858. case Core::HID::NpadType::GameCube:
  859. ui->buttonMiscButtonsPlusGroup->setTitle(tr("Start / Pause"));
  860. ui->buttonShoulderButtonsButtonZLGroup->setTitle(tr("L"));
  861. ui->buttonShoulderButtonsZRGroup->setTitle(tr("R"));
  862. ui->buttonShoulderButtonsRGroup->setTitle(tr("Z"));
  863. ui->LStick->setTitle(tr("Control Stick"));
  864. ui->RStick->setTitle(tr("C-Stick"));
  865. break;
  866. default:
  867. break;
  868. }
  869. }
  870. void ConfigureInputPlayer::UpdateMappingWithDefaults() {
  871. if (ui->comboDevices->currentIndex() == 0) {
  872. return;
  873. }
  874. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  875. const auto* const button = button_map[button_id];
  876. if (button == nullptr) {
  877. continue;
  878. }
  879. emulated_controller->SetButtonParam(button_id, {});
  880. }
  881. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  882. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  883. const auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  884. if (analog_button == nullptr) {
  885. continue;
  886. }
  887. emulated_controller->SetStickParam(analog_id, {});
  888. }
  889. }
  890. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  891. const auto* const motion_button = motion_map[motion_id];
  892. if (motion_button == nullptr) {
  893. continue;
  894. }
  895. emulated_controller->SetMotionParam(motion_id, {});
  896. }
  897. // Reset keyboard bindings
  898. if (ui->comboDevices->currentIndex() == 1) {
  899. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  900. emulated_controller->SetButtonParam(
  901. button_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
  902. Config::default_buttons[button_id])});
  903. }
  904. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  905. Common::ParamPackage analog_param{};
  906. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  907. Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
  908. Config::default_analogs[analog_id][sub_button_id])};
  909. SetAnalogParam(params, analog_param, analog_sub_buttons[sub_button_id]);
  910. }
  911. analog_param.Set("modifier", InputCommon::GenerateKeyboardParam(
  912. Config::default_stick_mod[analog_id]));
  913. emulated_controller->SetStickParam(analog_id, analog_param);
  914. }
  915. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  916. emulated_controller->SetMotionParam(
  917. motion_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
  918. Config::default_motions[motion_id])});
  919. }
  920. UpdateUI();
  921. return;
  922. }
  923. // Reset keyboard with mouse bindings
  924. if (ui->comboDevices->currentIndex() == 2) {
  925. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  926. emulated_controller->SetButtonParam(
  927. button_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
  928. Config::default_buttons[button_id])});
  929. }
  930. Common::ParamPackage left_analog_param{};
  931. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  932. Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
  933. Config::default_analogs[Settings::NativeAnalog::LStick][sub_button_id])};
  934. SetAnalogParam(params, left_analog_param, analog_sub_buttons[sub_button_id]);
  935. }
  936. left_analog_param.Set("modifier",
  937. InputCommon::GenerateKeyboardParam(
  938. Config::default_stick_mod[Settings::NativeAnalog::LStick]));
  939. emulated_controller->SetStickParam(Settings::NativeAnalog::LStick, left_analog_param);
  940. Common::ParamPackage right_analog_param{};
  941. right_analog_param.Set("engine", "mouse");
  942. right_analog_param.Set("port", 0);
  943. right_analog_param.Set("axis_x", 0);
  944. right_analog_param.Set("axis_y", 1);
  945. emulated_controller->SetStickParam(Settings::NativeAnalog::RStick,
  946. std::move(right_analog_param));
  947. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  948. emulated_controller->SetMotionParam(
  949. motion_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
  950. Config::default_motions[motion_id])});
  951. }
  952. UpdateUI();
  953. return;
  954. }
  955. // Reset controller bindings
  956. const auto& device = input_devices[ui->comboDevices->currentIndex()];
  957. auto button_mapping = input_subsystem->GetButtonMappingForDevice(device);
  958. auto analog_mapping = input_subsystem->GetAnalogMappingForDevice(device);
  959. auto motion_mapping = input_subsystem->GetMotionMappingForDevice(device);
  960. for (std::size_t i = 0; i < button_mapping.size(); ++i) {
  961. emulated_controller->SetButtonParam(
  962. i, button_mapping[static_cast<Settings::NativeButton::Values>(i)]);
  963. }
  964. for (std::size_t i = 0; i < analog_mapping.size(); ++i) {
  965. emulated_controller->SetStickParam(
  966. i, analog_mapping[static_cast<Settings::NativeAnalog::Values>(i)]);
  967. }
  968. for (std::size_t i = 0; i < motion_mapping.size(); ++i) {
  969. emulated_controller->SetMotionParam(
  970. i, motion_mapping[static_cast<Settings::NativeMotion::Values>(i)]);
  971. }
  972. UpdateUI();
  973. }
  974. void ConfigureInputPlayer::HandleClick(
  975. QPushButton* button, std::size_t button_id,
  976. std::function<void(const Common::ParamPackage&)> new_input_setter,
  977. InputCommon::Polling::InputType type) {
  978. if (button == ui->buttonMotionLeft || button == ui->buttonMotionRight) {
  979. button->setText(tr("Shake!"));
  980. } else {
  981. button->setText(tr("[waiting]"));
  982. }
  983. button->setFocus();
  984. input_setter = new_input_setter;
  985. input_subsystem->BeginMapping(type);
  986. QWidget::grabMouse();
  987. QWidget::grabKeyboard();
  988. if (type == InputCommon::Polling::InputType::Button) {
  989. ui->controllerFrame->BeginMappingButton(button_id);
  990. } else if (type == InputCommon::Polling::InputType::Stick) {
  991. ui->controllerFrame->BeginMappingAnalog(button_id);
  992. }
  993. timeout_timer->start(2500); // Cancel after 2.5 seconds
  994. poll_timer->start(50); // Check for new inputs every 50ms
  995. }
  996. void ConfigureInputPlayer::SetPollingResult(const Common::ParamPackage& params, bool abort) {
  997. timeout_timer->stop();
  998. poll_timer->stop();
  999. input_subsystem->StopMapping();
  1000. QWidget::releaseMouse();
  1001. QWidget::releaseKeyboard();
  1002. if (!abort) {
  1003. (*input_setter)(params);
  1004. }
  1005. UpdateUI();
  1006. UpdateInputDeviceCombobox();
  1007. ui->controllerFrame->EndMapping();
  1008. input_setter = std::nullopt;
  1009. }
  1010. bool ConfigureInputPlayer::IsInputAcceptable(const Common::ParamPackage& params) const {
  1011. if (ui->comboDevices->currentIndex() == 0) {
  1012. return true;
  1013. }
  1014. // Keyboard/Mouse
  1015. if (ui->comboDevices->currentIndex() == 2) {
  1016. return params.Get("engine", "") == "keyboard" || params.Get("engine", "") == "mouse";
  1017. }
  1018. const auto current_input_device = input_devices[ui->comboDevices->currentIndex()];
  1019. return params.Get("engine", "") == current_input_device.Get("engine", "") &&
  1020. (params.Get("guid", "") == current_input_device.Get("guid", "") ||
  1021. params.Get("guid", "") == current_input_device.Get("guid2", "")) &&
  1022. params.Get("port", "") == current_input_device.Get("port", "");
  1023. }
  1024. void ConfigureInputPlayer::mousePressEvent(QMouseEvent* event) {
  1025. if (!input_setter || !event) {
  1026. return;
  1027. }
  1028. const auto button = GRenderWindow::QtButtonToMouseButton(event->button());
  1029. input_subsystem->GetMouse()->PressButton(0, 0, 0, 0, button);
  1030. }
  1031. void ConfigureInputPlayer::keyPressEvent(QKeyEvent* event) {
  1032. event->ignore();
  1033. if (!input_setter || !event) {
  1034. return;
  1035. }
  1036. if (event->key() != Qt::Key_Escape) {
  1037. input_subsystem->GetKeyboard()->PressKey(event->key());
  1038. }
  1039. }
  1040. void ConfigureInputPlayer::CreateProfile() {
  1041. const auto profile_name =
  1042. LimitableInputDialog::GetText(this, tr("New Profile"), tr("Enter a profile name:"), 1, 20,
  1043. LimitableInputDialog::InputLimiter::Filesystem);
  1044. if (profile_name.isEmpty()) {
  1045. return;
  1046. }
  1047. if (!InputProfiles::IsProfileNameValid(profile_name.toStdString())) {
  1048. QMessageBox::critical(this, tr("Create Input Profile"),
  1049. tr("The given profile name is not valid!"));
  1050. return;
  1051. }
  1052. ApplyConfiguration();
  1053. if (!profiles->CreateProfile(profile_name.toStdString(), player_index)) {
  1054. QMessageBox::critical(this, tr("Create Input Profile"),
  1055. tr("Failed to create the input profile \"%1\"").arg(profile_name));
  1056. UpdateInputProfiles();
  1057. emit RefreshInputProfiles(player_index);
  1058. return;
  1059. }
  1060. emit RefreshInputProfiles(player_index);
  1061. ui->comboProfiles->addItem(profile_name);
  1062. ui->comboProfiles->setCurrentIndex(ui->comboProfiles->count() - 1);
  1063. }
  1064. void ConfigureInputPlayer::DeleteProfile() {
  1065. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1066. if (profile_name.isEmpty()) {
  1067. return;
  1068. }
  1069. if (!profiles->DeleteProfile(profile_name.toStdString())) {
  1070. QMessageBox::critical(this, tr("Delete Input Profile"),
  1071. tr("Failed to delete the input profile \"%1\"").arg(profile_name));
  1072. UpdateInputProfiles();
  1073. emit RefreshInputProfiles(player_index);
  1074. return;
  1075. }
  1076. emit RefreshInputProfiles(player_index);
  1077. ui->comboProfiles->removeItem(ui->comboProfiles->currentIndex());
  1078. ui->comboProfiles->setCurrentIndex(-1);
  1079. }
  1080. void ConfigureInputPlayer::LoadProfile() {
  1081. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1082. if (profile_name.isEmpty()) {
  1083. return;
  1084. }
  1085. ApplyConfiguration();
  1086. if (!profiles->LoadProfile(profile_name.toStdString(), player_index)) {
  1087. QMessageBox::critical(this, tr("Load Input Profile"),
  1088. tr("Failed to load the input profile \"%1\"").arg(profile_name));
  1089. UpdateInputProfiles();
  1090. emit RefreshInputProfiles(player_index);
  1091. return;
  1092. }
  1093. LoadConfiguration();
  1094. }
  1095. void ConfigureInputPlayer::SaveProfile() {
  1096. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1097. if (profile_name.isEmpty()) {
  1098. return;
  1099. }
  1100. ApplyConfiguration();
  1101. if (!profiles->SaveProfile(profile_name.toStdString(), player_index)) {
  1102. QMessageBox::critical(this, tr("Save Input Profile"),
  1103. tr("Failed to save the input profile \"%1\"").arg(profile_name));
  1104. UpdateInputProfiles();
  1105. emit RefreshInputProfiles(player_index);
  1106. return;
  1107. }
  1108. }
  1109. void ConfigureInputPlayer::UpdateInputProfiles() {
  1110. ui->comboProfiles->clear();
  1111. for (const auto& profile_name : profiles->GetInputProfileNames()) {
  1112. ui->comboProfiles->addItem(QString::fromStdString(profile_name));
  1113. }
  1114. ui->comboProfiles->setCurrentIndex(-1);
  1115. }