configure_input_player.cpp 64 KB

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