configure_input_player.cpp 61 KB

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