configure_input_player.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  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").arg(toggle, 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](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("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 toggle_value = !param.Get("inverted", false);
  321. param.Set("inverted", toggle_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](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. context_menu.addAction(tr("Clear"), [&] {
  370. emulated_controller->SetMotionParam(motion_id, {});
  371. motion_map[motion_id]->setText(tr("[not set]"));
  372. });
  373. context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location));
  374. });
  375. }
  376. connect(ui->sliderZLThreshold, &QSlider::valueChanged, [=, this] {
  377. Common::ParamPackage param =
  378. emulated_controller->GetButtonParam(Settings::NativeButton::ZL);
  379. if (param.Has("threshold")) {
  380. const auto slider_value = ui->sliderZLThreshold->value();
  381. param.Set("threshold", slider_value / 100.0f);
  382. emulated_controller->SetButtonParam(Settings::NativeButton::ZL, param);
  383. }
  384. });
  385. connect(ui->sliderZRThreshold, &QSlider::valueChanged, [=, this] {
  386. Common::ParamPackage param =
  387. emulated_controller->GetButtonParam(Settings::NativeButton::ZR);
  388. if (param.Has("threshold")) {
  389. const auto slider_value = ui->sliderZRThreshold->value();
  390. param.Set("threshold", slider_value / 100.0f);
  391. emulated_controller->SetButtonParam(Settings::NativeButton::ZR, param);
  392. }
  393. });
  394. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  395. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  396. auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  397. if (analog_button == nullptr) {
  398. continue;
  399. }
  400. connect(analog_button, &QPushButton::clicked, [=, this] {
  401. if (!map_analog_stick_accepted) {
  402. map_analog_stick_accepted =
  403. QMessageBox::information(
  404. this, tr("Map Analog Stick"),
  405. tr("After pressing OK, first move your joystick horizontally, and then "
  406. "vertically.\nTo invert the axes, first move your joystick "
  407. "vertically, and then horizontally."),
  408. QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok;
  409. if (!map_analog_stick_accepted) {
  410. return;
  411. }
  412. }
  413. HandleClick(
  414. analog_map_buttons[analog_id][sub_button_id], analog_id,
  415. [=, this](const Common::ParamPackage& params) {
  416. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  417. SetAnalogParam(params, param, analog_sub_buttons[sub_button_id]);
  418. emulated_controller->SetStickParam(analog_id, param);
  419. },
  420. InputCommon::Polling::InputType::Stick);
  421. });
  422. analog_button->setContextMenuPolicy(Qt::CustomContextMenu);
  423. connect(analog_button, &QPushButton::customContextMenuRequested,
  424. [=, this](const QPoint& menu_location) {
  425. QMenu context_menu;
  426. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  427. context_menu.addAction(tr("Clear"), [&] {
  428. emulated_controller->SetStickParam(analog_id, {});
  429. analog_map_buttons[analog_id][sub_button_id]->setText(tr("[not set]"));
  430. });
  431. context_menu.addAction(tr("Invert axis"), [&] {
  432. if (sub_button_id == 2 || sub_button_id == 3) {
  433. const bool invert_value = param.Get("invert_x", "+") == "-";
  434. const std::string invert_str = invert_value ? "+" : "-";
  435. param.Set("invert_x", invert_str);
  436. emulated_controller->SetStickParam(analog_id, param);
  437. }
  438. if (sub_button_id == 0 || sub_button_id == 1) {
  439. const bool invert_value = param.Get("invert_y", "+") == "-";
  440. const std::string invert_str = invert_value ? "+" : "-";
  441. param.Set("invert_y", invert_str);
  442. emulated_controller->SetStickParam(analog_id, param);
  443. }
  444. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM;
  445. ++sub_button_id) {
  446. analog_map_buttons[analog_id][sub_button_id]->setText(
  447. AnalogToText(param, analog_sub_buttons[sub_button_id]));
  448. }
  449. });
  450. context_menu.exec(analog_map_buttons[analog_id][sub_button_id]->mapToGlobal(
  451. menu_location));
  452. });
  453. }
  454. // Handle clicks for the modifier buttons as well.
  455. connect(analog_map_modifier_button[analog_id], &QPushButton::clicked, [=, this] {
  456. HandleClick(
  457. analog_map_modifier_button[analog_id], analog_id,
  458. [=, this](const Common::ParamPackage& params) {
  459. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  460. param.Set("modifier", params.Serialize());
  461. emulated_controller->SetStickParam(analog_id, param);
  462. },
  463. InputCommon::Polling::InputType::Button);
  464. });
  465. analog_map_modifier_button[analog_id]->setContextMenuPolicy(Qt::CustomContextMenu);
  466. connect(analog_map_modifier_button[analog_id], &QPushButton::customContextMenuRequested,
  467. [=, this](const QPoint& menu_location) {
  468. QMenu context_menu;
  469. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  470. context_menu.addAction(tr("Clear"), [&] {
  471. param.Set("modifier", "");
  472. analog_map_modifier_button[analog_id]->setText(tr("[not set]"));
  473. emulated_controller->SetStickParam(analog_id, param);
  474. });
  475. context_menu.addAction(tr("Toggle button"), [&] {
  476. Common::ParamPackage modifier_param =
  477. Common::ParamPackage{param.Get("modifier", "")};
  478. const bool toggle_value = !modifier_param.Get("toggle", false);
  479. modifier_param.Set("toggle", toggle_value);
  480. param.Set("modifier", modifier_param.Serialize());
  481. analog_map_modifier_button[analog_id]->setText(
  482. ButtonToText(modifier_param));
  483. emulated_controller->SetStickParam(analog_id, param);
  484. });
  485. context_menu.exec(
  486. analog_map_modifier_button[analog_id]->mapToGlobal(menu_location));
  487. });
  488. connect(analog_map_range_spinbox[analog_id], qOverload<int>(&QSpinBox::valueChanged),
  489. [=, this] {
  490. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  491. const auto spinbox_value = analog_map_range_spinbox[analog_id]->value();
  492. param.Set("range", spinbox_value / 100.0f);
  493. emulated_controller->SetStickParam(analog_id, param);
  494. });
  495. connect(analog_map_deadzone_slider[analog_id], &QSlider::valueChanged, [=, this] {
  496. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  497. const auto slider_value = analog_map_deadzone_slider[analog_id]->value();
  498. analog_map_deadzone_label[analog_id]->setText(tr("Deadzone: %1%").arg(slider_value));
  499. param.Set("deadzone", slider_value / 100.0f);
  500. emulated_controller->SetStickParam(analog_id, param);
  501. });
  502. connect(analog_map_modifier_slider[analog_id], &QSlider::valueChanged, [=, this] {
  503. Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  504. const auto slider_value = analog_map_modifier_slider[analog_id]->value();
  505. analog_map_modifier_label[analog_id]->setText(
  506. tr("Modifier Range: %1%").arg(slider_value));
  507. param.Set("modifier_scale", slider_value / 100.0f);
  508. emulated_controller->SetStickParam(analog_id, param);
  509. });
  510. }
  511. // Player Connected checkbox
  512. connect(ui->groupConnectedController, &QGroupBox::toggled,
  513. [this](bool checked) { emit Connected(checked); });
  514. if (player_index == 0) {
  515. connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged),
  516. [this](int index) {
  517. emit HandheldStateChanged(GetControllerTypeFromIndex(index) ==
  518. Core::HID::NpadStyleIndex::Handheld);
  519. });
  520. }
  521. if (debug || player_index == 9) {
  522. ui->groupConnectedController->setCheckable(false);
  523. }
  524. // The Debug Controller can only choose the Pro Controller.
  525. if (debug) {
  526. ui->buttonScreenshot->setEnabled(false);
  527. ui->buttonHome->setEnabled(false);
  528. ui->comboControllerType->addItem(tr("Pro Controller"));
  529. } else {
  530. SetConnectableControllers();
  531. }
  532. UpdateControllerAvailableButtons();
  533. UpdateControllerEnabledButtons();
  534. UpdateControllerButtonNames();
  535. UpdateMotionButtons();
  536. connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged),
  537. [this, player_index](int) {
  538. UpdateControllerAvailableButtons();
  539. UpdateControllerEnabledButtons();
  540. UpdateControllerButtonNames();
  541. UpdateMotionButtons();
  542. const Core::HID::NpadStyleIndex type =
  543. GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  544. if (player_index == 0) {
  545. auto* emulated_controller_p1 =
  546. hid_core.GetEmulatedController(Core::HID::NpadIdType::Player1);
  547. auto* emulated_controller_handheld =
  548. hid_core.GetEmulatedController(Core::HID::NpadIdType::Handheld);
  549. bool is_connected = emulated_controller->IsConnected(true);
  550. emulated_controller_p1->SetNpadStyleIndex(type);
  551. emulated_controller_handheld->SetNpadStyleIndex(type);
  552. if (is_connected) {
  553. if (type == Core::HID::NpadStyleIndex::Handheld) {
  554. emulated_controller_p1->Disconnect();
  555. emulated_controller_handheld->Connect(true);
  556. emulated_controller = emulated_controller_handheld;
  557. } else {
  558. emulated_controller_handheld->Disconnect();
  559. emulated_controller_p1->Connect(true);
  560. emulated_controller = emulated_controller_p1;
  561. }
  562. }
  563. ui->controllerFrame->SetController(emulated_controller);
  564. }
  565. emulated_controller->SetNpadStyleIndex(type);
  566. });
  567. connect(ui->comboDevices, qOverload<int>(&QComboBox::activated), this,
  568. &ConfigureInputPlayer::UpdateMappingWithDefaults);
  569. ui->comboDevices->setCurrentIndex(-1);
  570. ui->buttonRefreshDevices->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
  571. connect(ui->buttonRefreshDevices, &QPushButton::clicked,
  572. [this] { emit RefreshInputDevices(); });
  573. timeout_timer->setSingleShot(true);
  574. connect(timeout_timer.get(), &QTimer::timeout, [this] { SetPollingResult({}, true); });
  575. connect(poll_timer.get(), &QTimer::timeout, [this] {
  576. const auto& params = input_subsystem->GetNextInput();
  577. if (params.Has("engine") && IsInputAcceptable(params)) {
  578. SetPollingResult(params, false);
  579. return;
  580. }
  581. });
  582. UpdateInputProfiles();
  583. connect(ui->buttonProfilesNew, &QPushButton::clicked, this,
  584. &ConfigureInputPlayer::CreateProfile);
  585. connect(ui->buttonProfilesDelete, &QPushButton::clicked, this,
  586. &ConfigureInputPlayer::DeleteProfile);
  587. connect(ui->comboProfiles, qOverload<int>(&QComboBox::activated), this,
  588. &ConfigureInputPlayer::LoadProfile);
  589. connect(ui->buttonProfilesSave, &QPushButton::clicked, this,
  590. &ConfigureInputPlayer::SaveProfile);
  591. LoadConfiguration();
  592. }
  593. ConfigureInputPlayer::~ConfigureInputPlayer() {
  594. if (player_index == 0) {
  595. auto* emulated_controller_p1 =
  596. hid_core.GetEmulatedController(Core::HID::NpadIdType::Player1);
  597. auto* emulated_controller_handheld =
  598. hid_core.GetEmulatedController(Core::HID::NpadIdType::Handheld);
  599. emulated_controller_p1->DisableConfiguration();
  600. emulated_controller_handheld->DisableConfiguration();
  601. } else {
  602. emulated_controller->DisableConfiguration();
  603. }
  604. }
  605. void ConfigureInputPlayer::ApplyConfiguration() {
  606. if (player_index == 0) {
  607. auto* emulated_controller_p1 =
  608. hid_core.GetEmulatedController(Core::HID::NpadIdType::Player1);
  609. auto* emulated_controller_handheld =
  610. hid_core.GetEmulatedController(Core::HID::NpadIdType::Handheld);
  611. emulated_controller_p1->DisableConfiguration();
  612. emulated_controller_p1->SaveCurrentConfig();
  613. emulated_controller_p1->EnableConfiguration();
  614. emulated_controller_handheld->DisableConfiguration();
  615. emulated_controller_handheld->SaveCurrentConfig();
  616. emulated_controller_handheld->EnableConfiguration();
  617. return;
  618. }
  619. emulated_controller->DisableConfiguration();
  620. emulated_controller->SaveCurrentConfig();
  621. emulated_controller->EnableConfiguration();
  622. }
  623. void ConfigureInputPlayer::showEvent(QShowEvent* event) {
  624. if (bottom_row == nullptr) {
  625. return;
  626. }
  627. QWidget::showEvent(event);
  628. ui->main->addWidget(bottom_row);
  629. }
  630. void ConfigureInputPlayer::changeEvent(QEvent* event) {
  631. if (event->type() == QEvent::LanguageChange) {
  632. RetranslateUI();
  633. }
  634. QWidget::changeEvent(event);
  635. }
  636. void ConfigureInputPlayer::RetranslateUI() {
  637. ui->retranslateUi(this);
  638. UpdateUI();
  639. }
  640. void ConfigureInputPlayer::LoadConfiguration() {
  641. emulated_controller->ReloadFromSettings();
  642. UpdateUI();
  643. UpdateInputDeviceCombobox();
  644. if (debug) {
  645. return;
  646. }
  647. const int comboBoxIndex =
  648. GetIndexFromControllerType(emulated_controller->GetNpadStyleIndex(true));
  649. ui->comboControllerType->setCurrentIndex(comboBoxIndex);
  650. ui->groupConnectedController->setChecked(emulated_controller->IsConnected(true));
  651. }
  652. void ConfigureInputPlayer::ConnectPlayer(bool connected) {
  653. ui->groupConnectedController->setChecked(connected);
  654. if (connected) {
  655. emulated_controller->Connect(true);
  656. } else {
  657. emulated_controller->Disconnect();
  658. }
  659. }
  660. void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
  661. // Skip input device persistence if "Input Devices" is set to "Any".
  662. if (ui->comboDevices->currentIndex() == 0) {
  663. UpdateInputDevices();
  664. return;
  665. }
  666. const auto devices =
  667. emulated_controller->GetMappedDevices(Core::HID::EmulatedDeviceIndex::AllDevices);
  668. UpdateInputDevices();
  669. if (devices.empty()) {
  670. return;
  671. }
  672. if (devices.size() > 2) {
  673. ui->comboDevices->setCurrentIndex(0);
  674. return;
  675. }
  676. const auto first_engine = devices[0].Get("engine", "");
  677. const auto first_guid = devices[0].Get("guid", "");
  678. const auto first_port = devices[0].Get("port", 0);
  679. const auto first_pad = devices[0].Get("pad", 0);
  680. if (devices.size() == 1) {
  681. const auto devices_it = std::find_if(
  682. input_devices.begin(), input_devices.end(),
  683. [first_engine, first_guid, first_port, first_pad](const Common::ParamPackage param) {
  684. return param.Get("engine", "") == first_engine &&
  685. param.Get("guid", "") == first_guid && param.Get("port", 0) == first_port &&
  686. param.Get("pad", 0) == first_pad;
  687. });
  688. const int device_index =
  689. devices_it != input_devices.end()
  690. ? static_cast<int>(std::distance(input_devices.begin(), devices_it))
  691. : 0;
  692. ui->comboDevices->setCurrentIndex(device_index);
  693. return;
  694. }
  695. const auto second_engine = devices[1].Get("engine", "");
  696. const auto second_guid = devices[1].Get("guid", "");
  697. const auto second_port = devices[1].Get("port", 0);
  698. const bool is_keyboard_mouse = (first_engine == "keyboard" || first_engine == "mouse") &&
  699. (second_engine == "keyboard" || second_engine == "mouse");
  700. if (is_keyboard_mouse) {
  701. ui->comboDevices->setCurrentIndex(2);
  702. return;
  703. }
  704. const bool is_engine_equal = first_engine == second_engine;
  705. const bool is_port_equal = first_port == second_port;
  706. if (is_engine_equal && is_port_equal) {
  707. const auto devices_it = std::find_if(
  708. input_devices.begin(), input_devices.end(),
  709. [first_engine, first_guid, second_guid, first_port](const Common::ParamPackage param) {
  710. const bool is_guid_valid =
  711. (param.Get("guid", "") == first_guid &&
  712. param.Get("guid2", "") == second_guid) ||
  713. (param.Get("guid", "") == second_guid && param.Get("guid2", "") == first_guid);
  714. return param.Get("engine", "") == first_engine && is_guid_valid &&
  715. param.Get("port", 0) == first_port;
  716. });
  717. const int device_index =
  718. devices_it != input_devices.end()
  719. ? static_cast<int>(std::distance(input_devices.begin(), devices_it))
  720. : 0;
  721. ui->comboDevices->setCurrentIndex(device_index);
  722. } else {
  723. ui->comboDevices->setCurrentIndex(0);
  724. }
  725. }
  726. void ConfigureInputPlayer::RestoreDefaults() {
  727. UpdateMappingWithDefaults();
  728. }
  729. void ConfigureInputPlayer::ClearAll() {
  730. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  731. const auto* const button = button_map[button_id];
  732. if (button == nullptr) {
  733. continue;
  734. }
  735. emulated_controller->SetButtonParam(button_id, {});
  736. }
  737. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  738. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  739. const auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  740. if (analog_button == nullptr) {
  741. continue;
  742. }
  743. emulated_controller->SetStickParam(analog_id, {});
  744. }
  745. }
  746. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  747. const auto* const motion_button = motion_map[motion_id];
  748. if (motion_button == nullptr) {
  749. continue;
  750. }
  751. emulated_controller->SetMotionParam(motion_id, {});
  752. }
  753. UpdateUI();
  754. UpdateInputDevices();
  755. }
  756. void ConfigureInputPlayer::UpdateUI() {
  757. for (int button = 0; button < Settings::NativeButton::NumButtons; ++button) {
  758. const Common::ParamPackage param = emulated_controller->GetButtonParam(button);
  759. button_map[button]->setText(ButtonToText(param));
  760. }
  761. const Common::ParamPackage ZL_param =
  762. emulated_controller->GetButtonParam(Settings::NativeButton::ZL);
  763. if (ZL_param.Has("threshold")) {
  764. const int button_threshold = static_cast<int>(ZL_param.Get("threshold", 0.5f) * 100.0f);
  765. ui->sliderZLThreshold->setValue(button_threshold);
  766. }
  767. const Common::ParamPackage ZR_param =
  768. emulated_controller->GetButtonParam(Settings::NativeButton::ZR);
  769. if (ZR_param.Has("threshold")) {
  770. const int button_threshold = static_cast<int>(ZR_param.Get("threshold", 0.5f) * 100.0f);
  771. ui->sliderZRThreshold->setValue(button_threshold);
  772. }
  773. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  774. const Common::ParamPackage param = emulated_controller->GetMotionParam(motion_id);
  775. motion_map[motion_id]->setText(ButtonToText(param));
  776. }
  777. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  778. const Common::ParamPackage param = emulated_controller->GetStickParam(analog_id);
  779. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  780. auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  781. if (analog_button == nullptr) {
  782. continue;
  783. }
  784. analog_button->setText(AnalogToText(param, analog_sub_buttons[sub_button_id]));
  785. }
  786. analog_map_modifier_button[analog_id]->setText(
  787. ButtonToText(Common::ParamPackage{param.Get("modifier", "")}));
  788. const auto deadzone_label = analog_map_deadzone_label[analog_id];
  789. const auto deadzone_slider = analog_map_deadzone_slider[analog_id];
  790. const auto modifier_groupbox = analog_map_modifier_groupbox[analog_id];
  791. const auto modifier_label = analog_map_modifier_label[analog_id];
  792. const auto modifier_slider = analog_map_modifier_slider[analog_id];
  793. const auto range_groupbox = analog_map_range_groupbox[analog_id];
  794. const auto range_spinbox = analog_map_range_spinbox[analog_id];
  795. int slider_value;
  796. const bool is_controller = input_subsystem->IsController(param);
  797. if (is_controller) {
  798. slider_value = static_cast<int>(param.Get("deadzone", 0.15f) * 100);
  799. deadzone_label->setText(tr("Deadzone: %1%").arg(slider_value));
  800. deadzone_slider->setValue(slider_value);
  801. range_spinbox->setValue(static_cast<int>(param.Get("range", 1.0f) * 100));
  802. } else {
  803. slider_value = static_cast<int>(param.Get("modifier_scale", 0.5f) * 100);
  804. modifier_label->setText(tr("Modifier Range: %1%").arg(slider_value));
  805. modifier_slider->setValue(slider_value);
  806. }
  807. deadzone_label->setVisible(is_controller);
  808. deadzone_slider->setVisible(is_controller);
  809. modifier_groupbox->setVisible(!is_controller);
  810. modifier_label->setVisible(!is_controller);
  811. modifier_slider->setVisible(!is_controller);
  812. range_groupbox->setVisible(is_controller);
  813. }
  814. }
  815. void ConfigureInputPlayer::SetConnectableControllers() {
  816. const auto npad_style_set = hid_core.GetSupportedStyleTag();
  817. index_controller_type_pairs.clear();
  818. ui->comboControllerType->clear();
  819. const auto add_item = [&](Core::HID::NpadStyleIndex controller_type,
  820. const QString& controller_name) {
  821. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(), controller_type);
  822. ui->comboControllerType->addItem(controller_name);
  823. };
  824. if (npad_style_set.fullkey == 1) {
  825. add_item(Core::HID::NpadStyleIndex::ProController, tr("Pro Controller"));
  826. }
  827. if (npad_style_set.joycon_dual == 1) {
  828. add_item(Core::HID::NpadStyleIndex::JoyconDual, tr("Dual Joycons"));
  829. }
  830. if (npad_style_set.joycon_left == 1) {
  831. add_item(Core::HID::NpadStyleIndex::JoyconLeft, tr("Left Joycon"));
  832. }
  833. if (npad_style_set.joycon_right == 1) {
  834. add_item(Core::HID::NpadStyleIndex::JoyconRight, tr("Right Joycon"));
  835. }
  836. if (player_index == 0 && npad_style_set.handheld == 1) {
  837. add_item(Core::HID::NpadStyleIndex::Handheld, tr("Handheld"));
  838. }
  839. if (npad_style_set.gamecube == 1) {
  840. add_item(Core::HID::NpadStyleIndex::GameCube, tr("GameCube Controller"));
  841. }
  842. // Disable all unsupported controllers
  843. if (!Settings::values.enable_all_controllers) {
  844. return;
  845. }
  846. if (npad_style_set.palma == 1) {
  847. add_item(Core::HID::NpadStyleIndex::Pokeball, tr("Poke Ball Plus"));
  848. }
  849. if (npad_style_set.lark == 1) {
  850. add_item(Core::HID::NpadStyleIndex::NES, tr("NES Controller"));
  851. }
  852. if (npad_style_set.lucia == 1) {
  853. add_item(Core::HID::NpadStyleIndex::SNES, tr("SNES Controller"));
  854. }
  855. if (npad_style_set.lagoon == 1) {
  856. add_item(Core::HID::NpadStyleIndex::N64, tr("N64 Controller"));
  857. }
  858. if (npad_style_set.lager == 1) {
  859. add_item(Core::HID::NpadStyleIndex::SegaGenesis, tr("Sega Genesis"));
  860. }
  861. }
  862. Core::HID::NpadStyleIndex ConfigureInputPlayer::GetControllerTypeFromIndex(int index) const {
  863. const auto it =
  864. std::find_if(index_controller_type_pairs.begin(), index_controller_type_pairs.end(),
  865. [index](const auto& pair) { return pair.first == index; });
  866. if (it == index_controller_type_pairs.end()) {
  867. return Core::HID::NpadStyleIndex::ProController;
  868. }
  869. return it->second;
  870. }
  871. int ConfigureInputPlayer::GetIndexFromControllerType(Core::HID::NpadStyleIndex type) const {
  872. const auto it =
  873. std::find_if(index_controller_type_pairs.begin(), index_controller_type_pairs.end(),
  874. [type](const auto& pair) { return pair.second == type; });
  875. if (it == index_controller_type_pairs.end()) {
  876. return -1;
  877. }
  878. return it->first;
  879. }
  880. void ConfigureInputPlayer::UpdateInputDevices() {
  881. input_devices = input_subsystem->GetInputDevices();
  882. ui->comboDevices->clear();
  883. for (auto device : input_devices) {
  884. ui->comboDevices->addItem(QString::fromStdString(device.Get("display", "Unknown")), {});
  885. }
  886. }
  887. void ConfigureInputPlayer::UpdateControllerAvailableButtons() {
  888. auto layout = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  889. if (debug) {
  890. layout = Core::HID::NpadStyleIndex::ProController;
  891. }
  892. // List of all the widgets that will be hidden by any of the following layouts that need
  893. // "unhidden" after the controller type changes
  894. const std::array<QWidget*, 11> layout_show = {
  895. ui->buttonShoulderButtonsSLSR,
  896. ui->horizontalSpacerShoulderButtonsWidget,
  897. ui->horizontalSpacerShoulderButtonsWidget2,
  898. ui->buttonShoulderButtonsLeft,
  899. ui->buttonMiscButtonsMinusScreenshot,
  900. ui->bottomLeft,
  901. ui->buttonShoulderButtonsRight,
  902. ui->buttonMiscButtonsPlusHome,
  903. ui->bottomRight,
  904. ui->buttonMiscButtonsMinusGroup,
  905. ui->buttonMiscButtonsScreenshotGroup,
  906. };
  907. for (auto* widget : layout_show) {
  908. widget->show();
  909. }
  910. std::vector<QWidget*> layout_hidden;
  911. switch (layout) {
  912. case Core::HID::NpadStyleIndex::ProController:
  913. case Core::HID::NpadStyleIndex::JoyconDual:
  914. case Core::HID::NpadStyleIndex::Handheld:
  915. layout_hidden = {
  916. ui->buttonShoulderButtonsSLSR,
  917. ui->horizontalSpacerShoulderButtonsWidget2,
  918. };
  919. break;
  920. case Core::HID::NpadStyleIndex::JoyconLeft:
  921. layout_hidden = {
  922. ui->horizontalSpacerShoulderButtonsWidget2,
  923. ui->buttonShoulderButtonsRight,
  924. ui->buttonMiscButtonsPlusHome,
  925. ui->bottomRight,
  926. };
  927. break;
  928. case Core::HID::NpadStyleIndex::JoyconRight:
  929. layout_hidden = {
  930. ui->horizontalSpacerShoulderButtonsWidget,
  931. ui->buttonShoulderButtonsLeft,
  932. ui->buttonMiscButtonsMinusScreenshot,
  933. ui->bottomLeft,
  934. };
  935. break;
  936. case Core::HID::NpadStyleIndex::GameCube:
  937. layout_hidden = {
  938. ui->buttonShoulderButtonsSLSR,
  939. ui->horizontalSpacerShoulderButtonsWidget2,
  940. ui->buttonMiscButtonsMinusGroup,
  941. ui->buttonMiscButtonsScreenshotGroup,
  942. };
  943. break;
  944. default:
  945. break;
  946. }
  947. for (auto* widget : layout_hidden) {
  948. widget->hide();
  949. }
  950. }
  951. void ConfigureInputPlayer::UpdateControllerEnabledButtons() {
  952. auto layout = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  953. if (debug) {
  954. layout = Core::HID::NpadStyleIndex::ProController;
  955. }
  956. // List of all the widgets that will be disabled by any of the following layouts that need
  957. // "enabled" after the controller type changes
  958. const std::array<QWidget*, 3> layout_enable = {
  959. ui->buttonLStickPressedGroup,
  960. ui->groupRStickPressed,
  961. ui->buttonShoulderButtonsButtonLGroup,
  962. };
  963. for (auto* widget : layout_enable) {
  964. widget->setEnabled(true);
  965. }
  966. std::vector<QWidget*> layout_disable;
  967. switch (layout) {
  968. case Core::HID::NpadStyleIndex::ProController:
  969. case Core::HID::NpadStyleIndex::JoyconDual:
  970. case Core::HID::NpadStyleIndex::Handheld:
  971. case Core::HID::NpadStyleIndex::JoyconLeft:
  972. case Core::HID::NpadStyleIndex::JoyconRight:
  973. break;
  974. case Core::HID::NpadStyleIndex::GameCube:
  975. layout_disable = {
  976. ui->buttonHome,
  977. ui->buttonLStickPressedGroup,
  978. ui->groupRStickPressed,
  979. ui->buttonShoulderButtonsButtonLGroup,
  980. };
  981. break;
  982. default:
  983. break;
  984. }
  985. for (auto* widget : layout_disable) {
  986. widget->setEnabled(false);
  987. }
  988. }
  989. void ConfigureInputPlayer::UpdateMotionButtons() {
  990. if (debug) {
  991. // Motion isn't used with the debug controller, hide both groupboxes.
  992. ui->buttonMotionLeftGroup->hide();
  993. ui->buttonMotionRightGroup->hide();
  994. return;
  995. }
  996. // Show/hide the "Motion 1/2" groupboxes depending on the currently selected controller.
  997. switch (GetControllerTypeFromIndex(ui->comboControllerType->currentIndex())) {
  998. case Core::HID::NpadStyleIndex::ProController:
  999. case Core::HID::NpadStyleIndex::JoyconLeft:
  1000. case Core::HID::NpadStyleIndex::Handheld:
  1001. // Show "Motion 1" and hide "Motion 2".
  1002. ui->buttonMotionLeftGroup->show();
  1003. ui->buttonMotionRightGroup->hide();
  1004. break;
  1005. case Core::HID::NpadStyleIndex::JoyconRight:
  1006. // Show "Motion 2" and hide "Motion 1".
  1007. ui->buttonMotionLeftGroup->hide();
  1008. ui->buttonMotionRightGroup->show();
  1009. break;
  1010. case Core::HID::NpadStyleIndex::GameCube:
  1011. // Hide both "Motion 1/2".
  1012. ui->buttonMotionLeftGroup->hide();
  1013. ui->buttonMotionRightGroup->hide();
  1014. break;
  1015. case Core::HID::NpadStyleIndex::JoyconDual:
  1016. default:
  1017. // Show both "Motion 1/2".
  1018. ui->buttonMotionLeftGroup->show();
  1019. ui->buttonMotionRightGroup->show();
  1020. break;
  1021. }
  1022. }
  1023. void ConfigureInputPlayer::UpdateControllerButtonNames() {
  1024. auto layout = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  1025. if (debug) {
  1026. layout = Core::HID::NpadStyleIndex::ProController;
  1027. }
  1028. switch (layout) {
  1029. case Core::HID::NpadStyleIndex::ProController:
  1030. case Core::HID::NpadStyleIndex::JoyconDual:
  1031. case Core::HID::NpadStyleIndex::Handheld:
  1032. case Core::HID::NpadStyleIndex::JoyconLeft:
  1033. case Core::HID::NpadStyleIndex::JoyconRight:
  1034. ui->buttonMiscButtonsPlusGroup->setTitle(tr("Plus"));
  1035. ui->buttonShoulderButtonsButtonZLGroup->setTitle(tr("ZL"));
  1036. ui->buttonShoulderButtonsZRGroup->setTitle(tr("ZR"));
  1037. ui->buttonShoulderButtonsRGroup->setTitle(tr("R"));
  1038. ui->LStick->setTitle(tr("Left Stick"));
  1039. ui->RStick->setTitle(tr("Right Stick"));
  1040. break;
  1041. case Core::HID::NpadStyleIndex::GameCube:
  1042. ui->buttonMiscButtonsPlusGroup->setTitle(tr("Start / Pause"));
  1043. ui->buttonShoulderButtonsButtonZLGroup->setTitle(tr("L"));
  1044. ui->buttonShoulderButtonsZRGroup->setTitle(tr("R"));
  1045. ui->buttonShoulderButtonsRGroup->setTitle(tr("Z"));
  1046. ui->LStick->setTitle(tr("Control Stick"));
  1047. ui->RStick->setTitle(tr("C-Stick"));
  1048. break;
  1049. default:
  1050. break;
  1051. }
  1052. }
  1053. void ConfigureInputPlayer::UpdateMappingWithDefaults() {
  1054. if (ui->comboDevices->currentIndex() == 0) {
  1055. return;
  1056. }
  1057. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  1058. const auto* const button = button_map[button_id];
  1059. if (button == nullptr) {
  1060. continue;
  1061. }
  1062. emulated_controller->SetButtonParam(button_id, {});
  1063. }
  1064. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  1065. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  1066. const auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  1067. if (analog_button == nullptr) {
  1068. continue;
  1069. }
  1070. emulated_controller->SetStickParam(analog_id, {});
  1071. }
  1072. }
  1073. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  1074. const auto* const motion_button = motion_map[motion_id];
  1075. if (motion_button == nullptr) {
  1076. continue;
  1077. }
  1078. emulated_controller->SetMotionParam(motion_id, {});
  1079. }
  1080. // Reset keyboard or mouse bindings
  1081. if (ui->comboDevices->currentIndex() == 1 || ui->comboDevices->currentIndex() == 2) {
  1082. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  1083. emulated_controller->SetButtonParam(
  1084. button_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
  1085. Config::default_buttons[button_id])});
  1086. }
  1087. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  1088. Common::ParamPackage analog_param{};
  1089. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  1090. Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
  1091. Config::default_analogs[analog_id][sub_button_id])};
  1092. SetAnalogParam(params, analog_param, analog_sub_buttons[sub_button_id]);
  1093. }
  1094. analog_param.Set("modifier", InputCommon::GenerateKeyboardParam(
  1095. Config::default_stick_mod[analog_id]));
  1096. emulated_controller->SetStickParam(analog_id, analog_param);
  1097. }
  1098. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  1099. emulated_controller->SetMotionParam(
  1100. motion_id, Common::ParamPackage{InputCommon::GenerateKeyboardParam(
  1101. Config::default_motions[motion_id])});
  1102. }
  1103. // If mouse is selected we want to override with mappings from the driver
  1104. if (ui->comboDevices->currentIndex() == 1) {
  1105. UpdateUI();
  1106. return;
  1107. }
  1108. }
  1109. // Reset controller bindings
  1110. const auto& device = input_devices[ui->comboDevices->currentIndex()];
  1111. auto button_mappings = input_subsystem->GetButtonMappingForDevice(device);
  1112. auto analog_mappings = input_subsystem->GetAnalogMappingForDevice(device);
  1113. auto motion_mappings = input_subsystem->GetMotionMappingForDevice(device);
  1114. for (const auto& button_mapping : button_mappings) {
  1115. const std::size_t index = button_mapping.first;
  1116. emulated_controller->SetButtonParam(index, button_mapping.second);
  1117. }
  1118. for (const auto& analog_mapping : analog_mappings) {
  1119. const std::size_t index = analog_mapping.first;
  1120. emulated_controller->SetStickParam(index, analog_mapping.second);
  1121. }
  1122. for (const auto& motion_mapping : motion_mappings) {
  1123. const std::size_t index = motion_mapping.first;
  1124. emulated_controller->SetMotionParam(index, motion_mapping.second);
  1125. }
  1126. UpdateUI();
  1127. }
  1128. void ConfigureInputPlayer::HandleClick(
  1129. QPushButton* button, std::size_t button_id,
  1130. std::function<void(const Common::ParamPackage&)> new_input_setter,
  1131. InputCommon::Polling::InputType type) {
  1132. if (button == ui->buttonMotionLeft || button == ui->buttonMotionRight) {
  1133. button->setText(tr("Shake!"));
  1134. } else {
  1135. button->setText(tr("[waiting]"));
  1136. }
  1137. button->setFocus();
  1138. input_setter = new_input_setter;
  1139. input_subsystem->BeginMapping(type);
  1140. QWidget::grabMouse();
  1141. QWidget::grabKeyboard();
  1142. if (type == InputCommon::Polling::InputType::Button) {
  1143. ui->controllerFrame->BeginMappingButton(button_id);
  1144. } else if (type == InputCommon::Polling::InputType::Stick) {
  1145. ui->controllerFrame->BeginMappingAnalog(button_id);
  1146. }
  1147. timeout_timer->start(2500); // Cancel after 2.5 seconds
  1148. poll_timer->start(25); // Check for new inputs every 25ms
  1149. }
  1150. void ConfigureInputPlayer::SetPollingResult(const Common::ParamPackage& params, bool abort) {
  1151. timeout_timer->stop();
  1152. poll_timer->stop();
  1153. input_subsystem->StopMapping();
  1154. QWidget::releaseMouse();
  1155. QWidget::releaseKeyboard();
  1156. if (!abort) {
  1157. (*input_setter)(params);
  1158. }
  1159. UpdateUI();
  1160. UpdateInputDeviceCombobox();
  1161. ui->controllerFrame->EndMapping();
  1162. input_setter = std::nullopt;
  1163. }
  1164. bool ConfigureInputPlayer::IsInputAcceptable(const Common::ParamPackage& params) const {
  1165. if (ui->comboDevices->currentIndex() == 0) {
  1166. return true;
  1167. }
  1168. if (params.Has("motion")) {
  1169. return true;
  1170. }
  1171. // Keyboard/Mouse
  1172. if (ui->comboDevices->currentIndex() == 1 || ui->comboDevices->currentIndex() == 2) {
  1173. return params.Get("engine", "") == "keyboard" || params.Get("engine", "") == "mouse";
  1174. }
  1175. const auto current_input_device = input_devices[ui->comboDevices->currentIndex()];
  1176. return params.Get("engine", "") == current_input_device.Get("engine", "") &&
  1177. (params.Get("guid", "") == current_input_device.Get("guid", "") ||
  1178. params.Get("guid", "") == current_input_device.Get("guid2", "")) &&
  1179. params.Get("port", 0) == current_input_device.Get("port", 0);
  1180. }
  1181. void ConfigureInputPlayer::mousePressEvent(QMouseEvent* event) {
  1182. if (!input_setter || !event) {
  1183. return;
  1184. }
  1185. const auto button = GRenderWindow::QtButtonToMouseButton(event->button());
  1186. input_subsystem->GetMouse()->PressButton(0, 0, 0, 0, button);
  1187. }
  1188. void ConfigureInputPlayer::keyPressEvent(QKeyEvent* event) {
  1189. event->ignore();
  1190. if (!input_setter || !event) {
  1191. return;
  1192. }
  1193. if (event->key() != Qt::Key_Escape) {
  1194. input_subsystem->GetKeyboard()->PressKey(event->key());
  1195. }
  1196. }
  1197. void ConfigureInputPlayer::CreateProfile() {
  1198. const auto profile_name =
  1199. LimitableInputDialog::GetText(this, tr("New Profile"), tr("Enter a profile name:"), 1, 20,
  1200. LimitableInputDialog::InputLimiter::Filesystem);
  1201. if (profile_name.isEmpty()) {
  1202. return;
  1203. }
  1204. if (!InputProfiles::IsProfileNameValid(profile_name.toStdString())) {
  1205. QMessageBox::critical(this, tr("Create Input Profile"),
  1206. tr("The given profile name is not valid!"));
  1207. return;
  1208. }
  1209. ApplyConfiguration();
  1210. if (!profiles->CreateProfile(profile_name.toStdString(), player_index)) {
  1211. QMessageBox::critical(this, tr("Create Input Profile"),
  1212. tr("Failed to create the input profile \"%1\"").arg(profile_name));
  1213. UpdateInputProfiles();
  1214. emit RefreshInputProfiles(player_index);
  1215. return;
  1216. }
  1217. emit RefreshInputProfiles(player_index);
  1218. ui->comboProfiles->addItem(profile_name);
  1219. ui->comboProfiles->setCurrentIndex(ui->comboProfiles->count() - 1);
  1220. }
  1221. void ConfigureInputPlayer::DeleteProfile() {
  1222. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1223. if (profile_name.isEmpty()) {
  1224. return;
  1225. }
  1226. if (!profiles->DeleteProfile(profile_name.toStdString())) {
  1227. QMessageBox::critical(this, tr("Delete Input Profile"),
  1228. tr("Failed to delete the input profile \"%1\"").arg(profile_name));
  1229. UpdateInputProfiles();
  1230. emit RefreshInputProfiles(player_index);
  1231. return;
  1232. }
  1233. emit RefreshInputProfiles(player_index);
  1234. ui->comboProfiles->removeItem(ui->comboProfiles->currentIndex());
  1235. ui->comboProfiles->setCurrentIndex(-1);
  1236. }
  1237. void ConfigureInputPlayer::LoadProfile() {
  1238. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1239. if (profile_name.isEmpty()) {
  1240. return;
  1241. }
  1242. ApplyConfiguration();
  1243. if (!profiles->LoadProfile(profile_name.toStdString(), player_index)) {
  1244. QMessageBox::critical(this, tr("Load Input Profile"),
  1245. tr("Failed to load the input profile \"%1\"").arg(profile_name));
  1246. UpdateInputProfiles();
  1247. emit RefreshInputProfiles(player_index);
  1248. return;
  1249. }
  1250. LoadConfiguration();
  1251. }
  1252. void ConfigureInputPlayer::SaveProfile() {
  1253. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1254. if (profile_name.isEmpty()) {
  1255. return;
  1256. }
  1257. ApplyConfiguration();
  1258. if (!profiles->SaveProfile(profile_name.toStdString(), player_index)) {
  1259. QMessageBox::critical(this, tr("Save Input Profile"),
  1260. tr("Failed to save the input profile \"%1\"").arg(profile_name));
  1261. UpdateInputProfiles();
  1262. emit RefreshInputProfiles(player_index);
  1263. return;
  1264. }
  1265. }
  1266. void ConfigureInputPlayer::UpdateInputProfiles() {
  1267. ui->comboProfiles->clear();
  1268. for (const auto& profile_name : profiles->GetInputProfileNames()) {
  1269. ui->comboProfiles->addItem(QString::fromStdString(profile_name));
  1270. }
  1271. ui->comboProfiles->setCurrentIndex(-1);
  1272. }