configure_input.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <thread>
  7. #include <QSignalBlocker>
  8. #include <QTimer>
  9. #include "core/core.h"
  10. #include "core/hid/emulated_controller.h"
  11. #include "core/hid/hid_core.h"
  12. #include "core/hle/service/am/am.h"
  13. #include "core/hle/service/am/applet_ae.h"
  14. #include "core/hle/service/am/applet_oe.h"
  15. #include "core/hle/service/sm/sm.h"
  16. #include "ui_configure_input.h"
  17. #include "ui_configure_input_advanced.h"
  18. #include "ui_configure_input_player.h"
  19. #include "yuzu/configuration/configure_debug_controller.h"
  20. #include "yuzu/configuration/configure_input.h"
  21. #include "yuzu/configuration/configure_input_advanced.h"
  22. #include "yuzu/configuration/configure_input_player.h"
  23. #include "yuzu/configuration/configure_motion_touch.h"
  24. #include "yuzu/configuration/configure_mouse_advanced.h"
  25. #include "yuzu/configuration/configure_touchscreen_advanced.h"
  26. #include "yuzu/configuration/configure_vibration.h"
  27. #include "yuzu/configuration/input_profiles.h"
  28. namespace {
  29. template <typename Dialog, typename... Args>
  30. void CallConfigureDialog(ConfigureInput& parent, Args&&... args) {
  31. Dialog dialog(&parent, std::forward<Args>(args)...);
  32. const auto res = dialog.exec();
  33. if (res == QDialog::Accepted) {
  34. dialog.ApplyConfiguration();
  35. }
  36. }
  37. } // Anonymous namespace
  38. void OnDockedModeChanged(bool last_state, bool new_state, Core::System& system) {
  39. if (last_state == new_state) {
  40. return;
  41. }
  42. if (!system.IsPoweredOn()) {
  43. return;
  44. }
  45. Service::SM::ServiceManager& sm = system.ServiceManager();
  46. // Message queue is shared between these services, we just need to signal an operation
  47. // change to one and it will handle both automatically
  48. auto applet_oe = sm.GetService<Service::AM::AppletOE>("appletOE");
  49. auto applet_ae = sm.GetService<Service::AM::AppletAE>("appletAE");
  50. bool has_signalled = false;
  51. if (applet_oe != nullptr) {
  52. applet_oe->GetMessageQueue()->OperationModeChanged();
  53. has_signalled = true;
  54. }
  55. if (applet_ae != nullptr && !has_signalled) {
  56. applet_ae->GetMessageQueue()->OperationModeChanged();
  57. }
  58. }
  59. ConfigureInput::ConfigureInput(Core::System& system_, QWidget* parent)
  60. : QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
  61. profiles(std::make_unique<InputProfiles>(system_)), system{system_} {
  62. ui->setupUi(this);
  63. }
  64. ConfigureInput::~ConfigureInput() = default;
  65. void ConfigureInput::Initialize(InputCommon::InputSubsystem* input_subsystem,
  66. std::size_t max_players) {
  67. const bool is_powered_on = system.IsPoweredOn();
  68. auto& hid_core = system.HIDCore();
  69. player_controllers = {
  70. new ConfigureInputPlayer(this, 0, ui->consoleInputSettings, input_subsystem, profiles.get(),
  71. hid_core, is_powered_on),
  72. new ConfigureInputPlayer(this, 1, ui->consoleInputSettings, input_subsystem, profiles.get(),
  73. hid_core, is_powered_on),
  74. new ConfigureInputPlayer(this, 2, ui->consoleInputSettings, input_subsystem, profiles.get(),
  75. hid_core, is_powered_on),
  76. new ConfigureInputPlayer(this, 3, ui->consoleInputSettings, input_subsystem, profiles.get(),
  77. hid_core, is_powered_on),
  78. new ConfigureInputPlayer(this, 4, ui->consoleInputSettings, input_subsystem, profiles.get(),
  79. hid_core, is_powered_on),
  80. new ConfigureInputPlayer(this, 5, ui->consoleInputSettings, input_subsystem, profiles.get(),
  81. hid_core, is_powered_on),
  82. new ConfigureInputPlayer(this, 6, ui->consoleInputSettings, input_subsystem, profiles.get(),
  83. hid_core, is_powered_on),
  84. new ConfigureInputPlayer(this, 7, ui->consoleInputSettings, input_subsystem, profiles.get(),
  85. hid_core, is_powered_on),
  86. };
  87. player_tabs = {
  88. ui->tabPlayer1, ui->tabPlayer2, ui->tabPlayer3, ui->tabPlayer4,
  89. ui->tabPlayer5, ui->tabPlayer6, ui->tabPlayer7, ui->tabPlayer8,
  90. };
  91. player_connected = {
  92. ui->checkboxPlayer1Connected, ui->checkboxPlayer2Connected, ui->checkboxPlayer3Connected,
  93. ui->checkboxPlayer4Connected, ui->checkboxPlayer5Connected, ui->checkboxPlayer6Connected,
  94. ui->checkboxPlayer7Connected, ui->checkboxPlayer8Connected,
  95. };
  96. std::array<QLabel*, 8> player_connected_labels = {
  97. ui->label, ui->label_3, ui->label_4, ui->label_5,
  98. ui->label_6, ui->label_7, ui->label_8, ui->label_9,
  99. };
  100. for (std::size_t i = 0; i < player_tabs.size(); ++i) {
  101. player_tabs[i]->setLayout(new QHBoxLayout(player_tabs[i]));
  102. player_tabs[i]->layout()->addWidget(player_controllers[i]);
  103. connect(player_controllers[i], &ConfigureInputPlayer::Connected, [&, i](bool is_connected) {
  104. // Ensures that the controllers are always connected in sequential order
  105. if (is_connected) {
  106. for (std::size_t index = 0; index <= i; ++index) {
  107. player_connected[index]->setChecked(is_connected);
  108. }
  109. } else {
  110. for (std::size_t index = i; index < player_tabs.size(); ++index) {
  111. player_connected[index]->setChecked(is_connected);
  112. }
  113. }
  114. });
  115. connect(player_controllers[i], &ConfigureInputPlayer::RefreshInputDevices, this,
  116. &ConfigureInput::UpdateAllInputDevices);
  117. connect(player_controllers[i], &ConfigureInputPlayer::RefreshInputProfiles, this,
  118. &ConfigureInput::UpdateAllInputProfiles, Qt::QueuedConnection);
  119. connect(player_connected[i], &QCheckBox::stateChanged, [this, i](int state) {
  120. player_controllers[i]->ConnectPlayer(state == Qt::Checked);
  121. });
  122. // Remove/hide all the elements that exceed max_players, if applicable.
  123. if (i >= max_players) {
  124. ui->tabWidget->removeTab(static_cast<int>(max_players));
  125. player_connected[i]->hide();
  126. player_connected_labels[i]->hide();
  127. }
  128. }
  129. // Only the first player can choose handheld mode so connect the signal just to player 1
  130. connect(player_controllers[0], &ConfigureInputPlayer::HandheldStateChanged,
  131. [this](bool is_handheld) { UpdateDockedState(is_handheld); });
  132. advanced = new ConfigureInputAdvanced(this);
  133. ui->tabAdvanced->setLayout(new QHBoxLayout(ui->tabAdvanced));
  134. ui->tabAdvanced->layout()->addWidget(advanced);
  135. connect(advanced, &ConfigureInputAdvanced::CallDebugControllerDialog,
  136. [this, input_subsystem, &hid_core, is_powered_on] {
  137. CallConfigureDialog<ConfigureDebugController>(
  138. *this, input_subsystem, profiles.get(), hid_core, is_powered_on);
  139. });
  140. connect(advanced, &ConfigureInputAdvanced::CallMouseConfigDialog, [this, input_subsystem] {
  141. CallConfigureDialog<ConfigureMouseAdvanced>(*this, input_subsystem);
  142. });
  143. connect(advanced, &ConfigureInputAdvanced::CallTouchscreenConfigDialog,
  144. [this] { CallConfigureDialog<ConfigureTouchscreenAdvanced>(*this); });
  145. connect(advanced, &ConfigureInputAdvanced::CallMotionTouchConfigDialog,
  146. [this, input_subsystem] {
  147. CallConfigureDialog<ConfigureMotionTouch>(*this, input_subsystem);
  148. });
  149. connect(ui->vibrationButton, &QPushButton::clicked,
  150. [this] { CallConfigureDialog<ConfigureVibration>(*this); });
  151. connect(ui->motionButton, &QPushButton::clicked, [this, input_subsystem] {
  152. CallConfigureDialog<ConfigureMotionTouch>(*this, input_subsystem);
  153. });
  154. connect(ui->buttonClearAll, &QPushButton::clicked, [this] { ClearAll(); });
  155. connect(ui->buttonRestoreDefaults, &QPushButton::clicked, [this] { RestoreDefaults(); });
  156. RetranslateUI();
  157. LoadConfiguration();
  158. }
  159. QList<QWidget*> ConfigureInput::GetSubTabs() const {
  160. return {
  161. ui->tabPlayer1, ui->tabPlayer2, ui->tabPlayer3, ui->tabPlayer4, ui->tabPlayer5,
  162. ui->tabPlayer6, ui->tabPlayer7, ui->tabPlayer8, ui->tabAdvanced,
  163. };
  164. }
  165. void ConfigureInput::ApplyConfiguration() {
  166. for (auto* controller : player_controllers) {
  167. controller->ApplyConfiguration();
  168. }
  169. advanced->ApplyConfiguration();
  170. const bool pre_docked_mode = Settings::values.use_docked_mode.GetValue();
  171. Settings::values.use_docked_mode.SetValue(ui->radioDocked->isChecked());
  172. OnDockedModeChanged(pre_docked_mode, Settings::values.use_docked_mode.GetValue(), system);
  173. Settings::values.vibration_enabled.SetValue(ui->vibrationGroup->isChecked());
  174. Settings::values.motion_enabled.SetValue(ui->motionGroup->isChecked());
  175. }
  176. void ConfigureInput::changeEvent(QEvent* event) {
  177. if (event->type() == QEvent::LanguageChange) {
  178. RetranslateUI();
  179. }
  180. QWidget::changeEvent(event);
  181. }
  182. void ConfigureInput::RetranslateUI() {
  183. ui->retranslateUi(this);
  184. }
  185. void ConfigureInput::LoadConfiguration() {
  186. const auto* handheld = system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Handheld);
  187. LoadPlayerControllerIndices();
  188. UpdateDockedState(handheld->IsConnected());
  189. ui->vibrationGroup->setChecked(Settings::values.vibration_enabled.GetValue());
  190. ui->motionGroup->setChecked(Settings::values.motion_enabled.GetValue());
  191. }
  192. void ConfigureInput::LoadPlayerControllerIndices() {
  193. for (std::size_t i = 0; i < player_connected.size(); ++i) {
  194. if (i == 0) {
  195. auto* handheld =
  196. system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Handheld);
  197. if (handheld->IsConnected()) {
  198. player_connected[i]->setChecked(true);
  199. continue;
  200. }
  201. }
  202. const auto* controller = system.HIDCore().GetEmulatedControllerByIndex(i);
  203. player_connected[i]->setChecked(controller->IsConnected());
  204. }
  205. }
  206. void ConfigureInput::ClearAll() {
  207. // We don't have a good way to know what tab is active, but we can find out by getting the
  208. // parent of the consoleInputSettings
  209. auto* player_tab = static_cast<ConfigureInputPlayer*>(ui->consoleInputSettings->parent());
  210. player_tab->ClearAll();
  211. }
  212. void ConfigureInput::RestoreDefaults() {
  213. // We don't have a good way to know what tab is active, but we can find out by getting the
  214. // parent of the consoleInputSettings
  215. auto* player_tab = static_cast<ConfigureInputPlayer*>(ui->consoleInputSettings->parent());
  216. player_tab->RestoreDefaults();
  217. ui->radioDocked->setChecked(true);
  218. ui->radioUndocked->setChecked(false);
  219. ui->vibrationGroup->setChecked(true);
  220. ui->motionGroup->setChecked(true);
  221. }
  222. void ConfigureInput::UpdateDockedState(bool is_handheld) {
  223. // Disallow changing the console mode if the controller type is handheld.
  224. ui->radioDocked->setEnabled(!is_handheld);
  225. ui->radioUndocked->setEnabled(!is_handheld);
  226. ui->radioDocked->setChecked(Settings::values.use_docked_mode.GetValue());
  227. ui->radioUndocked->setChecked(!Settings::values.use_docked_mode.GetValue());
  228. // Also force into undocked mode if the controller type is handheld.
  229. if (is_handheld) {
  230. ui->radioUndocked->setChecked(true);
  231. }
  232. }
  233. void ConfigureInput::UpdateAllInputDevices() {
  234. for (const auto& player : player_controllers) {
  235. player->UpdateInputDeviceCombobox();
  236. }
  237. }
  238. void ConfigureInput::UpdateAllInputProfiles(std::size_t player_index) {
  239. for (std::size_t i = 0; i < player_controllers.size(); ++i) {
  240. if (i == player_index) {
  241. continue;
  242. }
  243. player_controllers[i]->UpdateInputProfiles();
  244. }
  245. }