configure_input_player.cpp 59 KB

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