configure_input_player.cpp 59 KB

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