configure_input_player.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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/core.h"
  15. #include "core/hle/service/hid/controllers/npad.h"
  16. #include "core/hle/service/hid/hid.h"
  17. #include "core/hle/service/sm/sm.h"
  18. #include "input_common/gcadapter/gc_poller.h"
  19. #include "input_common/main.h"
  20. #include "input_common/mouse/mouse_poller.h"
  21. #include "input_common/udp/udp.h"
  22. #include "ui_configure_input_player.h"
  23. #include "yuzu/configuration/config.h"
  24. #include "yuzu/configuration/configure_input_player.h"
  25. #include "yuzu/configuration/configure_vibration.h"
  26. #include "yuzu/configuration/input_profiles.h"
  27. #include "yuzu/util/limitable_input_dialog.h"
  28. using namespace Service::HID;
  29. const std::array<std::string, ConfigureInputPlayer::ANALOG_SUB_BUTTONS_NUM>
  30. ConfigureInputPlayer::analog_sub_buttons{{
  31. "up",
  32. "down",
  33. "left",
  34. "right",
  35. }};
  36. namespace {
  37. constexpr std::size_t HANDHELD_INDEX = 8;
  38. void UpdateController(Settings::ControllerType controller_type, std::size_t npad_index,
  39. bool connected) {
  40. Core::System& system{Core::System::GetInstance()};
  41. if (!system.IsPoweredOn()) {
  42. return;
  43. }
  44. Service::SM::ServiceManager& sm = system.ServiceManager();
  45. auto& npad = sm.GetService<Hid>("hid")->GetAppletResource()->GetController<Controller_NPad>(
  46. HidController::NPad);
  47. npad.UpdateControllerAt(npad.MapSettingsTypeToNPad(controller_type), npad_index, connected);
  48. }
  49. QString GetKeyName(int key_code) {
  50. switch (key_code) {
  51. case Qt::LeftButton:
  52. return QObject::tr("Click 0");
  53. case Qt::RightButton:
  54. return QObject::tr("Click 1");
  55. case Qt::MiddleButton:
  56. return QObject::tr("Click 2");
  57. case Qt::BackButton:
  58. return QObject::tr("Click 3");
  59. case Qt::ForwardButton:
  60. return QObject::tr("Click 4");
  61. case Qt::Key_Shift:
  62. return QObject::tr("Shift");
  63. case Qt::Key_Control:
  64. return QObject::tr("Ctrl");
  65. case Qt::Key_Alt:
  66. return QObject::tr("Alt");
  67. case Qt::Key_Meta:
  68. return {};
  69. default:
  70. return QKeySequence(key_code).toString();
  71. }
  72. }
  73. void SetAnalogParam(const Common::ParamPackage& input_param, Common::ParamPackage& analog_param,
  74. const std::string& button_name) {
  75. // The poller returned a complete axis, so set all the buttons
  76. if (input_param.Has("axis_x") && input_param.Has("axis_y")) {
  77. analog_param = input_param;
  78. return;
  79. }
  80. // Check if the current configuration has either no engine or an axis binding.
  81. // Clears out the old binding and adds one with analog_from_button.
  82. if (!analog_param.Has("engine") || analog_param.Has("axis_x") || analog_param.Has("axis_y")) {
  83. analog_param = {
  84. {"engine", "analog_from_button"},
  85. };
  86. }
  87. analog_param.Set(button_name, input_param.Serialize());
  88. }
  89. QString ButtonToText(const Common::ParamPackage& param) {
  90. if (!param.Has("engine")) {
  91. return QObject::tr("[not set]");
  92. }
  93. if (param.Get("engine", "") == "keyboard") {
  94. return GetKeyName(param.Get("code", 0));
  95. }
  96. if (param.Get("engine", "") == "gcpad") {
  97. if (param.Has("axis")) {
  98. const QString axis_str = QString::fromStdString(param.Get("axis", ""));
  99. const QString direction_str = QString::fromStdString(param.Get("direction", ""));
  100. return QObject::tr("GC Axis %1%2").arg(axis_str, direction_str);
  101. }
  102. if (param.Has("button")) {
  103. const QString button_str = QString::number(int(std::log2(param.Get("button", 0))));
  104. return QObject::tr("GC Button %1").arg(button_str);
  105. }
  106. return GetKeyName(param.Get("code", 0));
  107. }
  108. if (param.Get("engine", "") == "cemuhookudp") {
  109. if (param.Has("pad_index")) {
  110. const QString motion_str = QString::fromStdString(param.Get("pad_index", ""));
  111. return QObject::tr("Motion %1").arg(motion_str);
  112. }
  113. return GetKeyName(param.Get("code", 0));
  114. }
  115. if (param.Get("engine", "") == "sdl") {
  116. if (param.Has("hat")) {
  117. const QString hat_str = QString::fromStdString(param.Get("hat", ""));
  118. const QString direction_str = QString::fromStdString(param.Get("direction", ""));
  119. return QObject::tr("Hat %1 %2").arg(hat_str, direction_str);
  120. }
  121. if (param.Has("axis")) {
  122. const QString axis_str = QString::fromStdString(param.Get("axis", ""));
  123. const QString direction_str = QString::fromStdString(param.Get("direction", ""));
  124. return QObject::tr("Axis %1%2").arg(axis_str, direction_str);
  125. }
  126. if (param.Has("button")) {
  127. const QString button_str = QString::fromStdString(param.Get("button", ""));
  128. return QObject::tr("Button %1").arg(button_str);
  129. }
  130. return {};
  131. }
  132. if (param.Get("engine", "") == "mouse") {
  133. if (param.Has("button")) {
  134. const QString button_str = QString::number(int(param.Get("button", 0)));
  135. return QObject::tr("Click %1").arg(button_str);
  136. }
  137. return GetKeyName(param.Get("code", 0));
  138. }
  139. return QObject::tr("[unknown]");
  140. }
  141. QString AnalogToText(const Common::ParamPackage& param, const std::string& dir) {
  142. if (!param.Has("engine")) {
  143. return QObject::tr("[not set]");
  144. }
  145. if (param.Get("engine", "") == "analog_from_button") {
  146. return ButtonToText(Common::ParamPackage{param.Get(dir, "")});
  147. }
  148. const auto engine_str = param.Get("engine", "");
  149. const QString axis_x_str = QString::fromStdString(param.Get("axis_x", ""));
  150. const QString axis_y_str = QString::fromStdString(param.Get("axis_y", ""));
  151. const bool invert_x = param.Get("invert_x", "+") == "-";
  152. const bool invert_y = param.Get("invert_y", "+") == "-";
  153. if (engine_str == "sdl" || engine_str == "gcpad" || engine_str == "mouse") {
  154. if (dir == "modifier") {
  155. return QObject::tr("[unused]");
  156. }
  157. if (dir == "left") {
  158. const QString invert_x_str = QString::fromStdString(invert_x ? "+" : "-");
  159. return QObject::tr("Axis %1%2").arg(axis_x_str, invert_x_str);
  160. }
  161. if (dir == "right") {
  162. const QString invert_x_str = QString::fromStdString(invert_x ? "-" : "+");
  163. return QObject::tr("Axis %1%2").arg(axis_x_str, invert_x_str);
  164. }
  165. if (dir == "up") {
  166. const QString invert_y_str = QString::fromStdString(invert_y ? "-" : "+");
  167. return QObject::tr("Axis %1%2").arg(axis_y_str, invert_y_str);
  168. }
  169. if (dir == "down") {
  170. const QString invert_y_str = QString::fromStdString(invert_y ? "+" : "-");
  171. return QObject::tr("Axis %1%2").arg(axis_y_str, invert_y_str);
  172. }
  173. return {};
  174. }
  175. return QObject::tr("[unknown]");
  176. }
  177. } // namespace
  178. ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_index,
  179. QWidget* bottom_row,
  180. InputCommon::InputSubsystem* input_subsystem_,
  181. InputProfiles* profiles_, bool debug)
  182. : QWidget(parent), ui(std::make_unique<Ui::ConfigureInputPlayer>()), player_index(player_index),
  183. debug(debug), input_subsystem{input_subsystem_}, profiles(profiles_),
  184. timeout_timer(std::make_unique<QTimer>()), poll_timer(std::make_unique<QTimer>()),
  185. bottom_row(bottom_row) {
  186. ui->setupUi(this);
  187. setFocusPolicy(Qt::ClickFocus);
  188. button_map = {
  189. ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY,
  190. ui->buttonLStick, ui->buttonRStick, ui->buttonL, ui->buttonR,
  191. ui->buttonZL, ui->buttonZR, ui->buttonPlus, ui->buttonMinus,
  192. ui->buttonDpadLeft, ui->buttonDpadUp, ui->buttonDpadRight, ui->buttonDpadDown,
  193. ui->buttonSL, ui->buttonSR, ui->buttonHome, ui->buttonScreenshot,
  194. };
  195. analog_map_buttons = {{
  196. {
  197. ui->buttonLStickUp,
  198. ui->buttonLStickDown,
  199. ui->buttonLStickLeft,
  200. ui->buttonLStickRight,
  201. },
  202. {
  203. ui->buttonRStickUp,
  204. ui->buttonRStickDown,
  205. ui->buttonRStickLeft,
  206. ui->buttonRStickRight,
  207. },
  208. }};
  209. motion_map = {
  210. ui->buttonMotionLeft,
  211. ui->buttonMotionRight,
  212. };
  213. analog_map_deadzone_label = {ui->labelLStickDeadzone, ui->labelRStickDeadzone};
  214. analog_map_deadzone_slider = {ui->sliderLStickDeadzone, ui->sliderRStickDeadzone};
  215. analog_map_modifier_groupbox = {ui->buttonLStickModGroup, ui->buttonRStickModGroup};
  216. analog_map_modifier_button = {ui->buttonLStickMod, ui->buttonRStickMod};
  217. analog_map_modifier_label = {ui->labelLStickModifierRange, ui->labelRStickModifierRange};
  218. analog_map_modifier_slider = {ui->sliderLStickModifierRange, ui->sliderRStickModifierRange};
  219. analog_map_range_groupbox = {ui->buttonLStickRangeGroup, ui->buttonRStickRangeGroup};
  220. analog_map_range_spinbox = {ui->spinboxLStickRange, ui->spinboxRStickRange};
  221. const auto ConfigureButtonClick = [&](QPushButton* button, Common::ParamPackage* param,
  222. int default_val, InputCommon::Polling::DeviceType type) {
  223. connect(button, &QPushButton::clicked, [=, this] {
  224. HandleClick(
  225. button,
  226. [=, this](Common::ParamPackage params) {
  227. // Workaround for ZL & ZR for analog triggers like on XBOX
  228. // controllers. Analog triggers (from controllers like the XBOX
  229. // controller) would not work due to a different range of their
  230. // signals (from 0 to 255 on analog triggers instead of -32768 to
  231. // 32768 on analog joysticks). The SDL driver misinterprets analog
  232. // triggers as analog joysticks.
  233. // TODO: reinterpret the signal range for analog triggers to map the
  234. // values correctly. This is required for the correct emulation of
  235. // the analog triggers of the GameCube controller.
  236. if (button == ui->buttonZL || button == ui->buttonZR) {
  237. params.Set("direction", "+");
  238. params.Set("threshold", "0.5");
  239. }
  240. *param = std::move(params);
  241. },
  242. type);
  243. });
  244. };
  245. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  246. auto* const button = button_map[button_id];
  247. if (button == nullptr) {
  248. continue;
  249. }
  250. ConfigureButtonClick(button_map[button_id], &buttons_param[button_id],
  251. Config::default_buttons[button_id],
  252. InputCommon::Polling::DeviceType::Button);
  253. button->setContextMenuPolicy(Qt::CustomContextMenu);
  254. connect(button, &QPushButton::customContextMenuRequested,
  255. [=, this](const QPoint& menu_location) {
  256. QMenu context_menu;
  257. context_menu.addAction(tr("Clear"), [&] {
  258. buttons_param[button_id].Clear();
  259. button_map[button_id]->setText(tr("[not set]"));
  260. });
  261. context_menu.exec(button_map[button_id]->mapToGlobal(menu_location));
  262. });
  263. }
  264. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  265. auto* const button = motion_map[motion_id];
  266. if (button == nullptr) {
  267. continue;
  268. }
  269. ConfigureButtonClick(motion_map[motion_id], &motions_param[motion_id],
  270. Config::default_motions[motion_id],
  271. InputCommon::Polling::DeviceType::Motion);
  272. button->setContextMenuPolicy(Qt::CustomContextMenu);
  273. connect(button, &QPushButton::customContextMenuRequested,
  274. [=, this](const QPoint& menu_location) {
  275. QMenu context_menu;
  276. context_menu.addAction(tr("Clear"), [&] {
  277. motions_param[motion_id].Clear();
  278. motion_map[motion_id]->setText(tr("[not set]"));
  279. });
  280. context_menu.exec(motion_map[motion_id]->mapToGlobal(menu_location));
  281. });
  282. }
  283. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  284. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  285. auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  286. if (analog_button == nullptr) {
  287. continue;
  288. }
  289. connect(analog_button, &QPushButton::clicked, [=, this] {
  290. if (!map_analog_stick_accepted) {
  291. map_analog_stick_accepted =
  292. QMessageBox::information(
  293. this, tr("Map Analog Stick"),
  294. tr("After pressing OK, first move your joystick horizontally, and then "
  295. "vertically.\nTo invert the axes, first move your joystick "
  296. "vertically, and then horizontally."),
  297. QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok;
  298. if (!map_analog_stick_accepted) {
  299. return;
  300. }
  301. }
  302. HandleClick(
  303. analog_map_buttons[analog_id][sub_button_id],
  304. [=, this](const Common::ParamPackage& params) {
  305. SetAnalogParam(params, analogs_param[analog_id],
  306. analog_sub_buttons[sub_button_id]);
  307. },
  308. InputCommon::Polling::DeviceType::AnalogPreferred);
  309. });
  310. analog_button->setContextMenuPolicy(Qt::CustomContextMenu);
  311. connect(analog_button, &QPushButton::customContextMenuRequested,
  312. [=, this](const QPoint& menu_location) {
  313. QMenu context_menu;
  314. context_menu.addAction(tr("Clear"), [&] {
  315. analogs_param[analog_id].Clear();
  316. analog_map_buttons[analog_id][sub_button_id]->setText(tr("[not set]"));
  317. });
  318. context_menu.addAction(tr("Invert axis"), [&] {
  319. if (sub_button_id == 2 || sub_button_id == 3) {
  320. const bool invert_value =
  321. analogs_param[analog_id].Get("invert_x", "+") == "-";
  322. const std::string invert_str = invert_value ? "+" : "-";
  323. analogs_param[analog_id].Set("invert_x", invert_str);
  324. }
  325. if (sub_button_id == 0 || sub_button_id == 1) {
  326. const bool invert_value =
  327. analogs_param[analog_id].Get("invert_y", "+") == "-";
  328. const std::string invert_str = invert_value ? "+" : "-";
  329. analogs_param[analog_id].Set("invert_y", invert_str);
  330. }
  331. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM;
  332. ++sub_button_id) {
  333. analog_map_buttons[analog_id][sub_button_id]->setText(AnalogToText(
  334. analogs_param[analog_id], analog_sub_buttons[sub_button_id]));
  335. }
  336. });
  337. context_menu.exec(analog_map_buttons[analog_id][sub_button_id]->mapToGlobal(
  338. menu_location));
  339. });
  340. }
  341. // Handle clicks for the modifier buttons as well.
  342. connect(analog_map_modifier_button[analog_id], &QPushButton::clicked, [=, this] {
  343. HandleClick(
  344. analog_map_modifier_button[analog_id],
  345. [=, this](const Common::ParamPackage& params) {
  346. analogs_param[analog_id].Set("modifier", params.Serialize());
  347. },
  348. InputCommon::Polling::DeviceType::Button);
  349. });
  350. analog_map_modifier_button[analog_id]->setContextMenuPolicy(Qt::CustomContextMenu);
  351. connect(analog_map_modifier_button[analog_id], &QPushButton::customContextMenuRequested,
  352. [=, this](const QPoint& menu_location) {
  353. QMenu context_menu;
  354. context_menu.addAction(tr("Clear"), [&] {
  355. analogs_param[analog_id].Set("modifier", "");
  356. analog_map_modifier_button[analog_id]->setText(tr("[not set]"));
  357. });
  358. context_menu.exec(
  359. analog_map_modifier_button[analog_id]->mapToGlobal(menu_location));
  360. });
  361. connect(analog_map_range_spinbox[analog_id], qOverload<int>(&QSpinBox::valueChanged),
  362. [=, this] {
  363. const auto spinbox_value = analog_map_range_spinbox[analog_id]->value();
  364. analogs_param[analog_id].Set("range", spinbox_value / 100.0f);
  365. });
  366. connect(analog_map_deadzone_slider[analog_id], &QSlider::valueChanged, [=, this] {
  367. const auto slider_value = analog_map_deadzone_slider[analog_id]->value();
  368. analog_map_deadzone_label[analog_id]->setText(tr("Deadzone: %1%").arg(slider_value));
  369. analogs_param[analog_id].Set("deadzone", slider_value / 100.0f);
  370. });
  371. connect(analog_map_modifier_slider[analog_id], &QSlider::valueChanged, [=, this] {
  372. const auto slider_value = analog_map_modifier_slider[analog_id]->value();
  373. analog_map_modifier_label[analog_id]->setText(
  374. tr("Modifier Range: %1%").arg(slider_value));
  375. analogs_param[analog_id].Set("modifier_scale", slider_value / 100.0f);
  376. });
  377. }
  378. // Player Connected checkbox
  379. connect(ui->groupConnectedController, &QGroupBox::toggled,
  380. [this](bool checked) { emit Connected(checked); });
  381. if (player_index == 0) {
  382. connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged),
  383. [this](int index) {
  384. emit HandheldStateChanged(GetControllerTypeFromIndex(index) ==
  385. Settings::ControllerType::Handheld);
  386. });
  387. }
  388. if (debug || player_index == 9) {
  389. ui->groupConnectedController->setCheckable(false);
  390. }
  391. // The Debug Controller can only choose the Pro Controller.
  392. if (debug) {
  393. ui->buttonScreenshot->setEnabled(false);
  394. ui->buttonHome->setEnabled(false);
  395. ui->comboControllerType->addItem(tr("Pro Controller"));
  396. } else {
  397. SetConnectableControllers();
  398. }
  399. UpdateControllerIcon();
  400. UpdateControllerAvailableButtons();
  401. UpdateMotionButtons();
  402. connect(ui->comboControllerType, qOverload<int>(&QComboBox::currentIndexChanged), [this](int) {
  403. UpdateControllerIcon();
  404. UpdateControllerAvailableButtons();
  405. UpdateMotionButtons();
  406. });
  407. connect(ui->comboDevices, qOverload<int>(&QComboBox::activated), this,
  408. &ConfigureInputPlayer::UpdateMappingWithDefaults);
  409. ui->comboDevices->setCurrentIndex(-1);
  410. ui->buttonRefreshDevices->setIcon(QIcon::fromTheme(QStringLiteral("view-refresh")));
  411. connect(ui->buttonRefreshDevices, &QPushButton::clicked,
  412. [this] { emit RefreshInputDevices(); });
  413. timeout_timer->setSingleShot(true);
  414. connect(timeout_timer.get(), &QTimer::timeout, [this] { SetPollingResult({}, true); });
  415. connect(poll_timer.get(), &QTimer::timeout, [this] {
  416. Common::ParamPackage params;
  417. if (input_subsystem->GetGCButtons()->IsPolling()) {
  418. params = input_subsystem->GetGCButtons()->GetNextInput();
  419. if (params.Has("engine") && IsInputAcceptable(params)) {
  420. SetPollingResult(params, false);
  421. return;
  422. }
  423. }
  424. if (input_subsystem->GetGCAnalogs()->IsPolling()) {
  425. params = input_subsystem->GetGCAnalogs()->GetNextInput();
  426. if (params.Has("engine") && IsInputAcceptable(params)) {
  427. SetPollingResult(params, false);
  428. return;
  429. }
  430. }
  431. if (input_subsystem->GetUDPMotions()->IsPolling()) {
  432. params = input_subsystem->GetUDPMotions()->GetNextInput();
  433. if (params.Has("engine")) {
  434. SetPollingResult(params, false);
  435. return;
  436. }
  437. }
  438. if (input_subsystem->GetMouseButtons()->IsPolling()) {
  439. params = input_subsystem->GetMouseButtons()->GetNextInput();
  440. if (params.Has("engine") && IsInputAcceptable(params)) {
  441. SetPollingResult(params, false);
  442. return;
  443. }
  444. }
  445. if (input_subsystem->GetMouseAnalogs()->IsPolling()) {
  446. params = input_subsystem->GetMouseAnalogs()->GetNextInput();
  447. if (params.Has("engine") && IsInputAcceptable(params)) {
  448. SetPollingResult(params, false);
  449. return;
  450. }
  451. }
  452. if (input_subsystem->GetMouseMotions()->IsPolling()) {
  453. params = input_subsystem->GetMouseMotions()->GetNextInput();
  454. if (params.Has("engine") && IsInputAcceptable(params)) {
  455. SetPollingResult(params, false);
  456. return;
  457. }
  458. }
  459. if (input_subsystem->GetMouseTouch()->IsPolling()) {
  460. params = input_subsystem->GetMouseTouch()->GetNextInput();
  461. if (params.Has("engine") && IsInputAcceptable(params)) {
  462. SetPollingResult(params, false);
  463. return;
  464. }
  465. }
  466. for (auto& poller : device_pollers) {
  467. params = poller->GetNextInput();
  468. if (params.Has("engine") && IsInputAcceptable(params)) {
  469. SetPollingResult(params, false);
  470. return;
  471. }
  472. }
  473. });
  474. UpdateInputProfiles();
  475. connect(ui->buttonProfilesNew, &QPushButton::clicked, this,
  476. &ConfigureInputPlayer::CreateProfile);
  477. connect(ui->buttonProfilesDelete, &QPushButton::clicked, this,
  478. &ConfigureInputPlayer::DeleteProfile);
  479. connect(ui->comboProfiles, qOverload<int>(&QComboBox::activated), this,
  480. &ConfigureInputPlayer::LoadProfile);
  481. connect(ui->buttonProfilesSave, &QPushButton::clicked, this,
  482. &ConfigureInputPlayer::SaveProfile);
  483. LoadConfiguration();
  484. // TODO(wwylele): enable this when we actually emulate it
  485. ui->buttonHome->setEnabled(false);
  486. }
  487. ConfigureInputPlayer::~ConfigureInputPlayer() = default;
  488. void ConfigureInputPlayer::ApplyConfiguration() {
  489. auto& player = Settings::values.players.GetValue()[player_index];
  490. auto& buttons = debug ? Settings::values.debug_pad_buttons : player.buttons;
  491. auto& analogs = debug ? Settings::values.debug_pad_analogs : player.analogs;
  492. std::transform(buttons_param.begin(), buttons_param.end(), buttons.begin(),
  493. [](const Common::ParamPackage& param) { return param.Serialize(); });
  494. std::transform(analogs_param.begin(), analogs_param.end(), analogs.begin(),
  495. [](const Common::ParamPackage& param) { return param.Serialize(); });
  496. if (debug) {
  497. return;
  498. }
  499. auto& motions = player.motions;
  500. std::transform(motions_param.begin(), motions_param.end(), motions.begin(),
  501. [](const Common::ParamPackage& param) { return param.Serialize(); });
  502. // Apply configuration for handheld
  503. if (player_index == 0) {
  504. auto& handheld = Settings::values.players.GetValue()[HANDHELD_INDEX];
  505. const auto handheld_connected = handheld.connected;
  506. handheld = player;
  507. handheld.connected = handheld_connected;
  508. }
  509. }
  510. void ConfigureInputPlayer::TryConnectSelectedController() {
  511. auto& player = Settings::values.players.GetValue()[player_index];
  512. const auto controller_type =
  513. GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  514. const auto player_connected = ui->groupConnectedController->isChecked() &&
  515. controller_type != Settings::ControllerType::Handheld;
  516. // Connect Handheld depending on Player 1's controller configuration.
  517. if (player_index == 0) {
  518. auto& handheld = Settings::values.players.GetValue()[HANDHELD_INDEX];
  519. const auto handheld_connected = ui->groupConnectedController->isChecked() &&
  520. controller_type == Settings::ControllerType::Handheld;
  521. // Connect only if handheld is going from disconnected to connected
  522. if (!handheld.connected && handheld_connected) {
  523. UpdateController(controller_type, HANDHELD_INDEX, true);
  524. }
  525. handheld.connected = handheld_connected;
  526. }
  527. if (player.controller_type == controller_type && player.connected == player_connected) {
  528. // Set vibration devices in the event that the input device has changed.
  529. ConfigureVibration::SetVibrationDevices(player_index);
  530. return;
  531. }
  532. player.controller_type = controller_type;
  533. player.connected = player_connected;
  534. ConfigureVibration::SetVibrationDevices(player_index);
  535. if (!player.connected) {
  536. return;
  537. }
  538. UpdateController(controller_type, player_index, true);
  539. }
  540. void ConfigureInputPlayer::TryDisconnectSelectedController() {
  541. const auto& player = Settings::values.players.GetValue()[player_index];
  542. const auto controller_type =
  543. GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  544. const auto player_connected = ui->groupConnectedController->isChecked() &&
  545. controller_type != Settings::ControllerType::Handheld;
  546. // Disconnect Handheld depending on Player 1's controller configuration.
  547. if (player_index == 0 && player.controller_type == Settings::ControllerType::Handheld) {
  548. const auto& handheld = Settings::values.players.GetValue()[HANDHELD_INDEX];
  549. const auto handheld_connected = ui->groupConnectedController->isChecked() &&
  550. controller_type == Settings::ControllerType::Handheld;
  551. // Disconnect only if handheld is going from connected to disconnected
  552. if (handheld.connected && !handheld_connected) {
  553. UpdateController(controller_type, HANDHELD_INDEX, false);
  554. }
  555. return;
  556. }
  557. // Do not do anything if the controller configuration has not changed.
  558. if (player.controller_type == controller_type && player.connected == player_connected) {
  559. return;
  560. }
  561. // Do not disconnect if the controller is already disconnected
  562. if (!player.connected) {
  563. return;
  564. }
  565. // Disconnect the controller first.
  566. UpdateController(controller_type, player_index, false);
  567. }
  568. void ConfigureInputPlayer::showEvent(QShowEvent* event) {
  569. if (bottom_row == nullptr) {
  570. return;
  571. }
  572. QWidget::showEvent(event);
  573. ui->main->addWidget(bottom_row);
  574. }
  575. void ConfigureInputPlayer::changeEvent(QEvent* event) {
  576. if (event->type() == QEvent::LanguageChange) {
  577. RetranslateUI();
  578. }
  579. QWidget::changeEvent(event);
  580. }
  581. void ConfigureInputPlayer::RetranslateUI() {
  582. ui->retranslateUi(this);
  583. UpdateUI();
  584. }
  585. void ConfigureInputPlayer::LoadConfiguration() {
  586. auto& player = Settings::values.players.GetValue()[player_index];
  587. if (debug) {
  588. std::transform(Settings::values.debug_pad_buttons.begin(),
  589. Settings::values.debug_pad_buttons.end(), buttons_param.begin(),
  590. [](const std::string& str) { return Common::ParamPackage(str); });
  591. std::transform(Settings::values.debug_pad_analogs.begin(),
  592. Settings::values.debug_pad_analogs.end(), analogs_param.begin(),
  593. [](const std::string& str) { return Common::ParamPackage(str); });
  594. } else {
  595. std::transform(player.buttons.begin(), player.buttons.end(), buttons_param.begin(),
  596. [](const std::string& str) { return Common::ParamPackage(str); });
  597. std::transform(player.analogs.begin(), player.analogs.end(), analogs_param.begin(),
  598. [](const std::string& str) { return Common::ParamPackage(str); });
  599. std::transform(player.motions.begin(), player.motions.end(), motions_param.begin(),
  600. [](const std::string& str) { return Common::ParamPackage(str); });
  601. }
  602. UpdateUI();
  603. UpdateInputDeviceCombobox();
  604. if (debug) {
  605. return;
  606. }
  607. ui->comboControllerType->setCurrentIndex(GetIndexFromControllerType(player.controller_type));
  608. ui->groupConnectedController->setChecked(
  609. player.connected ||
  610. (player_index == 0 && Settings::values.players.GetValue()[HANDHELD_INDEX].connected));
  611. }
  612. void ConfigureInputPlayer::ConnectPlayer(bool connected) {
  613. ui->groupConnectedController->setChecked(connected);
  614. }
  615. void ConfigureInputPlayer::UpdateInputDeviceCombobox() {
  616. // Skip input device persistence if "Input Devices" is set to "Any".
  617. if (ui->comboDevices->currentIndex() == 0) {
  618. UpdateInputDevices();
  619. return;
  620. }
  621. // Find the first button that isn't empty.
  622. const auto button_param =
  623. std::find_if(buttons_param.begin(), buttons_param.end(),
  624. [](const Common::ParamPackage param) { return param.Has("engine"); });
  625. const bool buttons_empty = button_param == buttons_param.end();
  626. const auto current_engine = button_param->Get("engine", "");
  627. const auto current_guid = button_param->Get("guid", "");
  628. const auto current_port = button_param->Get("port", "");
  629. const bool is_keyboard_mouse = current_engine == "keyboard" || current_engine == "mouse";
  630. UpdateInputDevices();
  631. if (buttons_empty) {
  632. return;
  633. }
  634. const bool all_one_device =
  635. std::all_of(buttons_param.begin(), buttons_param.end(),
  636. [current_engine, current_guid, current_port,
  637. is_keyboard_mouse](const Common::ParamPackage param) {
  638. if (is_keyboard_mouse) {
  639. return !param.Has("engine") || param.Get("engine", "") == "keyboard" ||
  640. param.Get("engine", "") == "mouse";
  641. }
  642. return !param.Has("engine") || (param.Get("engine", "") == current_engine &&
  643. param.Get("guid", "") == current_guid &&
  644. param.Get("port", "") == current_port);
  645. });
  646. if (all_one_device) {
  647. if (is_keyboard_mouse) {
  648. ui->comboDevices->setCurrentIndex(1);
  649. return;
  650. }
  651. const auto devices_it = std::find_if(
  652. input_devices.begin(), input_devices.end(),
  653. [current_engine, current_guid, current_port](const Common::ParamPackage param) {
  654. return param.Get("class", "") == current_engine &&
  655. param.Get("guid", "") == current_guid &&
  656. param.Get("port", "") == current_port;
  657. });
  658. const int device_index =
  659. devices_it != input_devices.end()
  660. ? static_cast<int>(std::distance(input_devices.begin(), devices_it))
  661. : 0;
  662. ui->comboDevices->setCurrentIndex(device_index);
  663. } else {
  664. ui->comboDevices->setCurrentIndex(0);
  665. }
  666. }
  667. void ConfigureInputPlayer::RestoreDefaults() {
  668. UpdateMappingWithDefaults();
  669. }
  670. void ConfigureInputPlayer::ClearAll() {
  671. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  672. const auto* const button = button_map[button_id];
  673. if (button == nullptr) {
  674. continue;
  675. }
  676. buttons_param[button_id].Clear();
  677. }
  678. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  679. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  680. const auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  681. if (analog_button == nullptr) {
  682. continue;
  683. }
  684. analogs_param[analog_id].Clear();
  685. }
  686. }
  687. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  688. const auto* const motion_button = motion_map[motion_id];
  689. if (motion_button == nullptr) {
  690. continue;
  691. }
  692. motions_param[motion_id].Clear();
  693. }
  694. UpdateUI();
  695. UpdateInputDevices();
  696. }
  697. void ConfigureInputPlayer::UpdateUI() {
  698. for (int button = 0; button < Settings::NativeButton::NumButtons; ++button) {
  699. button_map[button]->setText(ButtonToText(buttons_param[button]));
  700. }
  701. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  702. motion_map[motion_id]->setText(ButtonToText(motions_param[motion_id]));
  703. }
  704. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  705. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  706. auto* const analog_button = analog_map_buttons[analog_id][sub_button_id];
  707. if (analog_button == nullptr) {
  708. continue;
  709. }
  710. analog_button->setText(
  711. AnalogToText(analogs_param[analog_id], analog_sub_buttons[sub_button_id]));
  712. }
  713. analog_map_modifier_button[analog_id]->setText(
  714. ButtonToText(Common::ParamPackage{analogs_param[analog_id].Get("modifier", "")}));
  715. const auto deadzone_label = analog_map_deadzone_label[analog_id];
  716. const auto deadzone_slider = analog_map_deadzone_slider[analog_id];
  717. const auto modifier_groupbox = analog_map_modifier_groupbox[analog_id];
  718. const auto modifier_label = analog_map_modifier_label[analog_id];
  719. const auto modifier_slider = analog_map_modifier_slider[analog_id];
  720. const auto range_groupbox = analog_map_range_groupbox[analog_id];
  721. const auto range_spinbox = analog_map_range_spinbox[analog_id];
  722. int slider_value;
  723. auto& param = analogs_param[analog_id];
  724. const bool is_controller = param.Get("engine", "") == "sdl" ||
  725. param.Get("engine", "") == "gcpad" ||
  726. param.Get("engine", "") == "mouse";
  727. if (is_controller) {
  728. if (!param.Has("deadzone")) {
  729. param.Set("deadzone", 0.1f);
  730. }
  731. slider_value = static_cast<int>(param.Get("deadzone", 0.1f) * 100);
  732. deadzone_label->setText(tr("Deadzone: %1%").arg(slider_value));
  733. deadzone_slider->setValue(slider_value);
  734. if (!param.Has("range")) {
  735. param.Set("range", 1.0f);
  736. }
  737. range_spinbox->setValue(static_cast<int>(param.Get("range", 1.0f) * 100));
  738. } else {
  739. if (!param.Has("modifier_scale")) {
  740. param.Set("modifier_scale", 0.5f);
  741. }
  742. slider_value = static_cast<int>(param.Get("modifier_scale", 0.5f) * 100);
  743. modifier_label->setText(tr("Modifier Range: %1%").arg(slider_value));
  744. modifier_slider->setValue(slider_value);
  745. }
  746. deadzone_label->setVisible(is_controller);
  747. deadzone_slider->setVisible(is_controller);
  748. modifier_groupbox->setVisible(!is_controller);
  749. modifier_label->setVisible(!is_controller);
  750. modifier_slider->setVisible(!is_controller);
  751. range_groupbox->setVisible(is_controller);
  752. }
  753. }
  754. void ConfigureInputPlayer::SetConnectableControllers() {
  755. const auto add_controllers = [this](bool enable_all,
  756. Controller_NPad::NpadStyleSet npad_style_set = {}) {
  757. index_controller_type_pairs.clear();
  758. ui->comboControllerType->clear();
  759. if (enable_all || npad_style_set.fullkey == 1) {
  760. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  761. Settings::ControllerType::ProController);
  762. ui->comboControllerType->addItem(tr("Pro Controller"));
  763. }
  764. if (enable_all || npad_style_set.joycon_dual == 1) {
  765. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  766. Settings::ControllerType::DualJoyconDetached);
  767. ui->comboControllerType->addItem(tr("Dual Joycons"));
  768. }
  769. if (enable_all || npad_style_set.joycon_left == 1) {
  770. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  771. Settings::ControllerType::LeftJoycon);
  772. ui->comboControllerType->addItem(tr("Left Joycon"));
  773. }
  774. if (enable_all || npad_style_set.joycon_right == 1) {
  775. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  776. Settings::ControllerType::RightJoycon);
  777. ui->comboControllerType->addItem(tr("Right Joycon"));
  778. }
  779. if (player_index == 0 && (enable_all || npad_style_set.handheld == 1)) {
  780. index_controller_type_pairs.emplace_back(ui->comboControllerType->count(),
  781. Settings::ControllerType::Handheld);
  782. ui->comboControllerType->addItem(tr("Handheld"));
  783. }
  784. };
  785. Core::System& system{Core::System::GetInstance()};
  786. if (!system.IsPoweredOn()) {
  787. add_controllers(true);
  788. return;
  789. }
  790. Service::SM::ServiceManager& sm = system.ServiceManager();
  791. auto& npad = sm.GetService<Hid>("hid")->GetAppletResource()->GetController<Controller_NPad>(
  792. HidController::NPad);
  793. add_controllers(false, npad.GetSupportedStyleSet());
  794. }
  795. Settings::ControllerType ConfigureInputPlayer::GetControllerTypeFromIndex(int index) const {
  796. const auto it =
  797. std::find_if(index_controller_type_pairs.begin(), index_controller_type_pairs.end(),
  798. [index](const auto& pair) { return pair.first == index; });
  799. if (it == index_controller_type_pairs.end()) {
  800. return Settings::ControllerType::ProController;
  801. }
  802. return it->second;
  803. }
  804. int ConfigureInputPlayer::GetIndexFromControllerType(Settings::ControllerType type) const {
  805. const auto it =
  806. std::find_if(index_controller_type_pairs.begin(), index_controller_type_pairs.end(),
  807. [type](const auto& pair) { return pair.second == type; });
  808. if (it == index_controller_type_pairs.end()) {
  809. return -1;
  810. }
  811. return it->first;
  812. }
  813. void ConfigureInputPlayer::UpdateInputDevices() {
  814. input_devices = input_subsystem->GetInputDevices();
  815. ui->comboDevices->clear();
  816. for (auto device : input_devices) {
  817. ui->comboDevices->addItem(QString::fromStdString(device.Get("display", "Unknown")), {});
  818. }
  819. }
  820. void ConfigureInputPlayer::UpdateControllerIcon() {
  821. // We aren't using Qt's built in theme support here since we aren't drawing an icon (and its
  822. // "nonstandard" to use an image through the icon support)
  823. const QString stylesheet = [this] {
  824. switch (GetControllerTypeFromIndex(ui->comboControllerType->currentIndex())) {
  825. case Settings::ControllerType::ProController:
  826. return QStringLiteral("image: url(:/controller/pro_controller%0)");
  827. case Settings::ControllerType::DualJoyconDetached:
  828. return QStringLiteral("image: url(:/controller/dual_joycon%0)");
  829. case Settings::ControllerType::LeftJoycon:
  830. return QStringLiteral("image: url(:/controller/single_joycon_left_vertical%0)");
  831. case Settings::ControllerType::RightJoycon:
  832. return QStringLiteral("image: url(:/controller/single_joycon_right_vertical%0)");
  833. case Settings::ControllerType::Handheld:
  834. return QStringLiteral("image: url(:/controller/handheld%0)");
  835. default:
  836. return QString{};
  837. }
  838. }();
  839. const QString theme = [] {
  840. if (QIcon::themeName().contains(QStringLiteral("dark"))) {
  841. return QStringLiteral("_dark");
  842. } else if (QIcon::themeName().contains(QStringLiteral("midnight"))) {
  843. return QStringLiteral("_midnight");
  844. } else {
  845. return QString{};
  846. }
  847. }();
  848. ui->controllerFrame->setStyleSheet(stylesheet.arg(theme));
  849. }
  850. void ConfigureInputPlayer::UpdateControllerAvailableButtons() {
  851. auto layout = GetControllerTypeFromIndex(ui->comboControllerType->currentIndex());
  852. if (debug) {
  853. layout = Settings::ControllerType::ProController;
  854. }
  855. // List of all the widgets that will be hidden by any of the following layouts that need
  856. // "unhidden" after the controller type changes
  857. const std::array<QWidget*, 9> layout_show = {
  858. ui->buttonShoulderButtonsSLSR,
  859. ui->horizontalSpacerShoulderButtonsWidget,
  860. ui->horizontalSpacerShoulderButtonsWidget2,
  861. ui->buttonShoulderButtonsLeft,
  862. ui->buttonMiscButtonsMinusScreenshot,
  863. ui->bottomLeft,
  864. ui->buttonShoulderButtonsRight,
  865. ui->buttonMiscButtonsPlusHome,
  866. ui->bottomRight,
  867. };
  868. for (auto* widget : layout_show) {
  869. widget->show();
  870. }
  871. std::vector<QWidget*> layout_hidden;
  872. switch (layout) {
  873. case Settings::ControllerType::ProController:
  874. case Settings::ControllerType::DualJoyconDetached:
  875. case Settings::ControllerType::Handheld:
  876. layout_hidden = {
  877. ui->buttonShoulderButtonsSLSR,
  878. ui->horizontalSpacerShoulderButtonsWidget2,
  879. };
  880. break;
  881. case Settings::ControllerType::LeftJoycon:
  882. layout_hidden = {
  883. ui->horizontalSpacerShoulderButtonsWidget2,
  884. ui->buttonShoulderButtonsRight,
  885. ui->buttonMiscButtonsPlusHome,
  886. ui->bottomRight,
  887. };
  888. break;
  889. case Settings::ControllerType::RightJoycon:
  890. layout_hidden = {
  891. ui->horizontalSpacerShoulderButtonsWidget,
  892. ui->buttonShoulderButtonsLeft,
  893. ui->buttonMiscButtonsMinusScreenshot,
  894. ui->bottomLeft,
  895. };
  896. break;
  897. }
  898. for (auto* widget : layout_hidden) {
  899. widget->hide();
  900. }
  901. }
  902. void ConfigureInputPlayer::UpdateMotionButtons() {
  903. if (debug) {
  904. // Motion isn't used with the debug controller, hide both groupboxes.
  905. ui->buttonMotionLeftGroup->hide();
  906. ui->buttonMotionRightGroup->hide();
  907. return;
  908. }
  909. // Show/hide the "Motion 1/2" groupboxes depending on the currently selected controller.
  910. switch (GetControllerTypeFromIndex(ui->comboControllerType->currentIndex())) {
  911. case Settings::ControllerType::ProController:
  912. case Settings::ControllerType::LeftJoycon:
  913. case Settings::ControllerType::Handheld:
  914. // Show "Motion 1" and hide "Motion 2".
  915. ui->buttonMotionLeftGroup->show();
  916. ui->buttonMotionRightGroup->hide();
  917. break;
  918. case Settings::ControllerType::RightJoycon:
  919. // Show "Motion 2" and hide "Motion 1".
  920. ui->buttonMotionLeftGroup->hide();
  921. ui->buttonMotionRightGroup->show();
  922. break;
  923. case Settings::ControllerType::DualJoyconDetached:
  924. default:
  925. // Show both "Motion 1/2".
  926. ui->buttonMotionLeftGroup->show();
  927. ui->buttonMotionRightGroup->show();
  928. break;
  929. }
  930. }
  931. void ConfigureInputPlayer::UpdateMappingWithDefaults() {
  932. if (ui->comboDevices->currentIndex() == 0) {
  933. return;
  934. }
  935. if (ui->comboDevices->currentIndex() == 1) {
  936. // Reset keyboard bindings
  937. for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; ++button_id) {
  938. buttons_param[button_id] = Common::ParamPackage{
  939. InputCommon::GenerateKeyboardParam(Config::default_buttons[button_id])};
  940. }
  941. for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; ++analog_id) {
  942. for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; ++sub_button_id) {
  943. Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
  944. Config::default_analogs[analog_id][sub_button_id])};
  945. SetAnalogParam(params, analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
  946. }
  947. analogs_param[analog_id].Set("modifier", InputCommon::GenerateKeyboardParam(
  948. Config::default_stick_mod[analog_id]));
  949. }
  950. for (int motion_id = 0; motion_id < Settings::NativeMotion::NumMotions; ++motion_id) {
  951. motions_param[motion_id] = Common::ParamPackage{
  952. InputCommon::GenerateKeyboardParam(Config::default_motions[motion_id])};
  953. }
  954. UpdateUI();
  955. return;
  956. }
  957. // Reset controller bindings
  958. const auto& device = input_devices[ui->comboDevices->currentIndex()];
  959. auto button_mapping = input_subsystem->GetButtonMappingForDevice(device);
  960. auto analog_mapping = input_subsystem->GetAnalogMappingForDevice(device);
  961. for (std::size_t i = 0; i < buttons_param.size(); ++i) {
  962. buttons_param[i] = button_mapping[static_cast<Settings::NativeButton::Values>(i)];
  963. }
  964. for (std::size_t i = 0; i < analogs_param.size(); ++i) {
  965. analogs_param[i] = analog_mapping[static_cast<Settings::NativeAnalog::Values>(i)];
  966. }
  967. UpdateUI();
  968. }
  969. void ConfigureInputPlayer::HandleClick(
  970. QPushButton* button, std::function<void(const Common::ParamPackage&)> new_input_setter,
  971. InputCommon::Polling::DeviceType type) {
  972. if (button == ui->buttonMotionLeft || button == ui->buttonMotionRight) {
  973. button->setText(tr("Shake!"));
  974. } else {
  975. button->setText(tr("[waiting]"));
  976. }
  977. button->setFocus();
  978. // The first two input devices are always Any and Keyboard/Mouse. If the user filtered to a
  979. // controller, then they don't want keyboard/mouse input
  980. want_keyboard_mouse = ui->comboDevices->currentIndex() < 2;
  981. input_setter = new_input_setter;
  982. device_pollers = input_subsystem->GetPollers(type);
  983. for (auto& poller : device_pollers) {
  984. poller->Start();
  985. }
  986. QWidget::grabMouse();
  987. QWidget::grabKeyboard();
  988. if (type == InputCommon::Polling::DeviceType::Button) {
  989. input_subsystem->GetGCButtons()->BeginConfiguration();
  990. } else {
  991. input_subsystem->GetGCAnalogs()->BeginConfiguration();
  992. }
  993. if (type == InputCommon::Polling::DeviceType::Motion) {
  994. input_subsystem->GetUDPMotions()->BeginConfiguration();
  995. }
  996. if (type == InputCommon::Polling::DeviceType::Button) {
  997. input_subsystem->GetMouseButtons()->BeginConfiguration();
  998. } else if (type == InputCommon::Polling::DeviceType::AnalogPreferred) {
  999. input_subsystem->GetMouseAnalogs()->BeginConfiguration();
  1000. } else if (type == InputCommon::Polling::DeviceType::Motion) {
  1001. input_subsystem->GetMouseMotions()->BeginConfiguration();
  1002. } else {
  1003. input_subsystem->GetMouseTouch()->BeginConfiguration();
  1004. }
  1005. timeout_timer->start(2500); // Cancel after 2.5 seconds
  1006. poll_timer->start(50); // Check for new inputs every 50ms
  1007. }
  1008. void ConfigureInputPlayer::SetPollingResult(const Common::ParamPackage& params, bool abort) {
  1009. timeout_timer->stop();
  1010. poll_timer->stop();
  1011. for (auto& poller : device_pollers) {
  1012. poller->Stop();
  1013. }
  1014. QWidget::releaseMouse();
  1015. QWidget::releaseKeyboard();
  1016. input_subsystem->GetGCButtons()->EndConfiguration();
  1017. input_subsystem->GetGCAnalogs()->EndConfiguration();
  1018. input_subsystem->GetUDPMotions()->EndConfiguration();
  1019. input_subsystem->GetMouseButtons()->EndConfiguration();
  1020. input_subsystem->GetMouseAnalogs()->EndConfiguration();
  1021. input_subsystem->GetMouseMotions()->EndConfiguration();
  1022. input_subsystem->GetMouseTouch()->EndConfiguration();
  1023. if (!abort) {
  1024. (*input_setter)(params);
  1025. }
  1026. UpdateUI();
  1027. UpdateInputDeviceCombobox();
  1028. input_setter = std::nullopt;
  1029. }
  1030. bool ConfigureInputPlayer::IsInputAcceptable(const Common::ParamPackage& params) const {
  1031. if (ui->comboDevices->currentIndex() == 0) {
  1032. return true;
  1033. }
  1034. // Keyboard/Mouse
  1035. if (ui->comboDevices->currentIndex() == 1) {
  1036. return params.Get("engine", "") == "keyboard" || params.Get("engine", "") == "mouse";
  1037. }
  1038. const auto current_input_device = input_devices[ui->comboDevices->currentIndex()];
  1039. return params.Get("engine", "") == current_input_device.Get("class", "") &&
  1040. params.Get("guid", "") == current_input_device.Get("guid", "") &&
  1041. params.Get("port", "") == current_input_device.Get("port", "");
  1042. }
  1043. void ConfigureInputPlayer::mousePressEvent(QMouseEvent* event) {
  1044. if (!input_setter || !event) {
  1045. return;
  1046. }
  1047. input_subsystem->GetMouse()->PressButton(0, 0, event->button());
  1048. }
  1049. void ConfigureInputPlayer::keyPressEvent(QKeyEvent* event) {
  1050. if (!input_setter || !event) {
  1051. return;
  1052. }
  1053. if (event->key() != Qt::Key_Escape) {
  1054. if (want_keyboard_mouse) {
  1055. SetPollingResult(Common::ParamPackage{InputCommon::GenerateKeyboardParam(event->key())},
  1056. false);
  1057. } else {
  1058. // Escape key wasn't pressed and we don't want any keyboard keys, so don't stop polling
  1059. return;
  1060. }
  1061. }
  1062. SetPollingResult({}, true);
  1063. }
  1064. void ConfigureInputPlayer::CreateProfile() {
  1065. const auto profile_name =
  1066. LimitableInputDialog::GetText(this, tr("New Profile"), tr("Enter a profile name:"), 1, 20);
  1067. if (profile_name.isEmpty()) {
  1068. return;
  1069. }
  1070. if (!InputProfiles::IsProfileNameValid(profile_name.toStdString())) {
  1071. QMessageBox::critical(this, tr("Create Input Profile"),
  1072. tr("The given profile name is not valid!"));
  1073. return;
  1074. }
  1075. ApplyConfiguration();
  1076. if (!profiles->CreateProfile(profile_name.toStdString(), player_index)) {
  1077. QMessageBox::critical(this, tr("Create Input Profile"),
  1078. tr("Failed to create the input profile \"%1\"").arg(profile_name));
  1079. UpdateInputProfiles();
  1080. emit RefreshInputProfiles(player_index);
  1081. return;
  1082. }
  1083. emit RefreshInputProfiles(player_index);
  1084. ui->comboProfiles->addItem(profile_name);
  1085. ui->comboProfiles->setCurrentIndex(ui->comboProfiles->count() - 1);
  1086. }
  1087. void ConfigureInputPlayer::DeleteProfile() {
  1088. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1089. if (profile_name.isEmpty()) {
  1090. return;
  1091. }
  1092. if (!profiles->DeleteProfile(profile_name.toStdString())) {
  1093. QMessageBox::critical(this, tr("Delete Input Profile"),
  1094. tr("Failed to delete the input profile \"%1\"").arg(profile_name));
  1095. UpdateInputProfiles();
  1096. emit RefreshInputProfiles(player_index);
  1097. return;
  1098. }
  1099. emit RefreshInputProfiles(player_index);
  1100. ui->comboProfiles->removeItem(ui->comboProfiles->currentIndex());
  1101. ui->comboProfiles->setCurrentIndex(-1);
  1102. }
  1103. void ConfigureInputPlayer::LoadProfile() {
  1104. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1105. if (profile_name.isEmpty()) {
  1106. return;
  1107. }
  1108. ApplyConfiguration();
  1109. if (!profiles->LoadProfile(profile_name.toStdString(), player_index)) {
  1110. QMessageBox::critical(this, tr("Load Input Profile"),
  1111. tr("Failed to load the input profile \"%1\"").arg(profile_name));
  1112. UpdateInputProfiles();
  1113. emit RefreshInputProfiles(player_index);
  1114. return;
  1115. }
  1116. LoadConfiguration();
  1117. }
  1118. void ConfigureInputPlayer::SaveProfile() {
  1119. const QString profile_name = ui->comboProfiles->itemText(ui->comboProfiles->currentIndex());
  1120. if (profile_name.isEmpty()) {
  1121. return;
  1122. }
  1123. ApplyConfiguration();
  1124. if (!profiles->SaveProfile(profile_name.toStdString(), player_index)) {
  1125. QMessageBox::critical(this, tr("Save Input Profile"),
  1126. tr("Failed to save the input profile \"%1\"").arg(profile_name));
  1127. UpdateInputProfiles();
  1128. emit RefreshInputProfiles(player_index);
  1129. return;
  1130. }
  1131. }
  1132. void ConfigureInputPlayer::UpdateInputProfiles() {
  1133. ui->comboProfiles->clear();
  1134. for (const auto& profile_name : profiles->GetInputProfileNames()) {
  1135. ui->comboProfiles->addItem(QString::fromStdString(profile_name));
  1136. }
  1137. ui->comboProfiles->setCurrentIndex(-1);
  1138. }