configure_input.cpp 11 KB

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