hid_core.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/assert.h"
  5. #include "core/hid/emulated_console.h"
  6. #include "core/hid/emulated_controller.h"
  7. #include "core/hid/emulated_devices.h"
  8. #include "core/hid/hid_core.h"
  9. namespace Core::HID {
  10. HIDCore::HIDCore()
  11. : player_1{std::make_unique<EmulatedController>(NpadIdType::Player1)},
  12. player_2{std::make_unique<EmulatedController>(NpadIdType::Player2)},
  13. player_3{std::make_unique<EmulatedController>(NpadIdType::Player3)},
  14. player_4{std::make_unique<EmulatedController>(NpadIdType::Player4)},
  15. player_5{std::make_unique<EmulatedController>(NpadIdType::Player5)},
  16. player_6{std::make_unique<EmulatedController>(NpadIdType::Player6)},
  17. player_7{std::make_unique<EmulatedController>(NpadIdType::Player7)},
  18. player_8{std::make_unique<EmulatedController>(NpadIdType::Player8)},
  19. other{std::make_unique<EmulatedController>(NpadIdType::Other)},
  20. handheld{std::make_unique<EmulatedController>(NpadIdType::Handheld)},
  21. console{std::make_unique<EmulatedConsole>()}, devices{std::make_unique<EmulatedDevices>()} {}
  22. HIDCore::~HIDCore() = default;
  23. EmulatedController* HIDCore::GetEmulatedController(NpadIdType npad_id_type) {
  24. switch (npad_id_type) {
  25. case NpadIdType::Player1:
  26. return player_1.get();
  27. case NpadIdType::Player2:
  28. return player_2.get();
  29. case NpadIdType::Player3:
  30. return player_3.get();
  31. case NpadIdType::Player4:
  32. return player_4.get();
  33. case NpadIdType::Player5:
  34. return player_5.get();
  35. case NpadIdType::Player6:
  36. return player_6.get();
  37. case NpadIdType::Player7:
  38. return player_7.get();
  39. case NpadIdType::Player8:
  40. return player_8.get();
  41. case NpadIdType::Other:
  42. return other.get();
  43. case NpadIdType::Handheld:
  44. return handheld.get();
  45. case NpadIdType::Invalid:
  46. default:
  47. UNREACHABLE_MSG("Invalid NpadIdType={}", npad_id_type);
  48. return nullptr;
  49. }
  50. }
  51. const EmulatedController* HIDCore::GetEmulatedController(NpadIdType npad_id_type) const {
  52. switch (npad_id_type) {
  53. case NpadIdType::Player1:
  54. return player_1.get();
  55. case NpadIdType::Player2:
  56. return player_2.get();
  57. case NpadIdType::Player3:
  58. return player_3.get();
  59. case NpadIdType::Player4:
  60. return player_4.get();
  61. case NpadIdType::Player5:
  62. return player_5.get();
  63. case NpadIdType::Player6:
  64. return player_6.get();
  65. case NpadIdType::Player7:
  66. return player_7.get();
  67. case NpadIdType::Player8:
  68. return player_8.get();
  69. case NpadIdType::Other:
  70. return other.get();
  71. case NpadIdType::Handheld:
  72. return handheld.get();
  73. case NpadIdType::Invalid:
  74. default:
  75. UNREACHABLE_MSG("Invalid NpadIdType={}", npad_id_type);
  76. return nullptr;
  77. }
  78. }
  79. EmulatedConsole* HIDCore::GetEmulatedConsole() {
  80. return console.get();
  81. }
  82. const EmulatedConsole* HIDCore::GetEmulatedConsole() const {
  83. return console.get();
  84. }
  85. EmulatedDevices* HIDCore::GetEmulatedDevices() {
  86. return devices.get();
  87. }
  88. const EmulatedDevices* HIDCore::GetEmulatedDevices() const {
  89. return devices.get();
  90. }
  91. EmulatedController* HIDCore::GetEmulatedControllerByIndex(std::size_t index) {
  92. return GetEmulatedController(IndexToNpadIdType(index));
  93. }
  94. const EmulatedController* HIDCore::GetEmulatedControllerByIndex(std::size_t index) const {
  95. return GetEmulatedController(IndexToNpadIdType(index));
  96. }
  97. void HIDCore::SetSupportedStyleTag(NpadStyleTag style_tag) {
  98. supported_style_tag.raw = style_tag.raw;
  99. player_1->SetSupportedNpadStyleTag(supported_style_tag);
  100. player_2->SetSupportedNpadStyleTag(supported_style_tag);
  101. player_3->SetSupportedNpadStyleTag(supported_style_tag);
  102. player_4->SetSupportedNpadStyleTag(supported_style_tag);
  103. player_5->SetSupportedNpadStyleTag(supported_style_tag);
  104. player_6->SetSupportedNpadStyleTag(supported_style_tag);
  105. player_7->SetSupportedNpadStyleTag(supported_style_tag);
  106. player_8->SetSupportedNpadStyleTag(supported_style_tag);
  107. other->SetSupportedNpadStyleTag(supported_style_tag);
  108. handheld->SetSupportedNpadStyleTag(supported_style_tag);
  109. }
  110. NpadStyleTag HIDCore::GetSupportedStyleTag() const {
  111. return supported_style_tag;
  112. }
  113. s8 HIDCore::GetPlayerCount() const {
  114. s8 active_players = 0;
  115. for (std::size_t player_index = 0; player_index < available_controllers - 2; ++player_index) {
  116. const auto* const controller = GetEmulatedControllerByIndex(player_index);
  117. if (controller->IsConnected()) {
  118. active_players++;
  119. }
  120. }
  121. return active_players;
  122. }
  123. NpadIdType HIDCore::GetFirstNpadId() const {
  124. for (std::size_t player_index = 0; player_index < available_controllers; ++player_index) {
  125. const auto* const controller = GetEmulatedControllerByIndex(player_index);
  126. if (controller->IsConnected()) {
  127. return controller->GetNpadIdType();
  128. }
  129. }
  130. return NpadIdType::Player1;
  131. }
  132. NpadIdType HIDCore::GetFirstDisconnectedNpadId() const {
  133. for (std::size_t player_index = 0; player_index < available_controllers; ++player_index) {
  134. const auto* const controller = GetEmulatedControllerByIndex(player_index);
  135. if (!controller->IsConnected()) {
  136. return controller->GetNpadIdType();
  137. }
  138. }
  139. return NpadIdType::Player1;
  140. }
  141. void HIDCore::EnableAllControllerConfiguration() {
  142. player_1->EnableConfiguration();
  143. player_2->EnableConfiguration();
  144. player_3->EnableConfiguration();
  145. player_4->EnableConfiguration();
  146. player_5->EnableConfiguration();
  147. player_6->EnableConfiguration();
  148. player_7->EnableConfiguration();
  149. player_8->EnableConfiguration();
  150. other->EnableConfiguration();
  151. handheld->EnableConfiguration();
  152. }
  153. void HIDCore::DisableAllControllerConfiguration() {
  154. player_1->DisableConfiguration();
  155. player_2->DisableConfiguration();
  156. player_3->DisableConfiguration();
  157. player_4->DisableConfiguration();
  158. player_5->DisableConfiguration();
  159. player_6->DisableConfiguration();
  160. player_7->DisableConfiguration();
  161. player_8->DisableConfiguration();
  162. other->DisableConfiguration();
  163. handheld->DisableConfiguration();
  164. }
  165. void HIDCore::ReloadInputDevices() {
  166. player_1->ReloadFromSettings();
  167. player_2->ReloadFromSettings();
  168. player_3->ReloadFromSettings();
  169. player_4->ReloadFromSettings();
  170. player_5->ReloadFromSettings();
  171. player_6->ReloadFromSettings();
  172. player_7->ReloadFromSettings();
  173. player_8->ReloadFromSettings();
  174. other->ReloadFromSettings();
  175. handheld->ReloadFromSettings();
  176. console->ReloadFromSettings();
  177. devices->ReloadFromSettings();
  178. }
  179. void HIDCore::UnloadInputDevices() {
  180. player_1->UnloadInput();
  181. player_2->UnloadInput();
  182. player_3->UnloadInput();
  183. player_4->UnloadInput();
  184. player_5->UnloadInput();
  185. player_6->UnloadInput();
  186. player_7->UnloadInput();
  187. player_8->UnloadInput();
  188. other->UnloadInput();
  189. handheld->UnloadInput();
  190. console->UnloadInput();
  191. devices->UnloadInput();
  192. }
  193. } // namespace Core::HID