configure_input_player.cpp 67 KB

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