input_engine.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/logging/log.h"
  5. #include "common/param_package.h"
  6. #include "input_common/input_engine.h"
  7. namespace InputCommon {
  8. void InputEngine::PreSetController(const PadIdentifier& identifier) {
  9. std::lock_guard lock{mutex};
  10. controller_list.try_emplace(identifier);
  11. }
  12. void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) {
  13. std::lock_guard lock{mutex};
  14. ControllerData& controller = controller_list.at(identifier);
  15. controller.buttons.try_emplace(button, false);
  16. }
  17. void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) {
  18. std::lock_guard lock{mutex};
  19. ControllerData& controller = controller_list.at(identifier);
  20. controller.hat_buttons.try_emplace(button, u8{0});
  21. }
  22. void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) {
  23. std::lock_guard lock{mutex};
  24. ControllerData& controller = controller_list.at(identifier);
  25. controller.axes.try_emplace(axis, 0.0f);
  26. }
  27. void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) {
  28. std::lock_guard lock{mutex};
  29. ControllerData& controller = controller_list.at(identifier);
  30. controller.motions.try_emplace(motion);
  31. }
  32. void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) {
  33. {
  34. std::lock_guard lock{mutex};
  35. ControllerData& controller = controller_list.at(identifier);
  36. if (!configuring) {
  37. controller.buttons.insert_or_assign(button, value);
  38. }
  39. }
  40. TriggerOnButtonChange(identifier, button, value);
  41. }
  42. void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 value) {
  43. {
  44. std::lock_guard lock{mutex};
  45. ControllerData& controller = controller_list.at(identifier);
  46. if (!configuring) {
  47. controller.hat_buttons.insert_or_assign(button, value);
  48. }
  49. }
  50. TriggerOnHatButtonChange(identifier, button, value);
  51. }
  52. void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) {
  53. {
  54. std::lock_guard lock{mutex};
  55. ControllerData& controller = controller_list.at(identifier);
  56. if (!configuring) {
  57. controller.axes.insert_or_assign(axis, value);
  58. }
  59. }
  60. TriggerOnAxisChange(identifier, axis, value);
  61. }
  62. void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::BatteryLevel value) {
  63. {
  64. std::lock_guard lock{mutex};
  65. ControllerData& controller = controller_list.at(identifier);
  66. if (!configuring) {
  67. controller.battery = value;
  68. }
  69. }
  70. TriggerOnBatteryChange(identifier, value);
  71. }
  72. void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value) {
  73. {
  74. std::lock_guard lock{mutex};
  75. ControllerData& controller = controller_list.at(identifier);
  76. if (!configuring) {
  77. controller.motions.insert_or_assign(motion, value);
  78. }
  79. }
  80. TriggerOnMotionChange(identifier, motion, value);
  81. }
  82. bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const {
  83. std::lock_guard lock{mutex};
  84. const auto controller_iter = controller_list.find(identifier);
  85. if (controller_iter == controller_list.cend()) {
  86. LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
  87. identifier.pad, identifier.port);
  88. return false;
  89. }
  90. const ControllerData& controller = controller_iter->second;
  91. const auto button_iter = controller.buttons.find(button);
  92. if (button_iter == controller.buttons.cend()) {
  93. LOG_ERROR(Input, "Invalid button {}", button);
  94. return false;
  95. }
  96. return button_iter->second;
  97. }
  98. bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const {
  99. std::lock_guard lock{mutex};
  100. const auto controller_iter = controller_list.find(identifier);
  101. if (controller_iter == controller_list.cend()) {
  102. LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
  103. identifier.pad, identifier.port);
  104. return false;
  105. }
  106. const ControllerData& controller = controller_iter->second;
  107. const auto hat_iter = controller.hat_buttons.find(button);
  108. if (hat_iter == controller.hat_buttons.cend()) {
  109. LOG_ERROR(Input, "Invalid hat button {}", button);
  110. return false;
  111. }
  112. return (hat_iter->second & direction) != 0;
  113. }
  114. f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const {
  115. std::lock_guard lock{mutex};
  116. const auto controller_iter = controller_list.find(identifier);
  117. if (controller_iter == controller_list.cend()) {
  118. LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
  119. identifier.pad, identifier.port);
  120. return 0.0f;
  121. }
  122. const ControllerData& controller = controller_iter->second;
  123. const auto axis_iter = controller.axes.find(axis);
  124. if (axis_iter == controller.axes.cend()) {
  125. LOG_ERROR(Input, "Invalid axis {}", axis);
  126. return 0.0f;
  127. }
  128. return axis_iter->second;
  129. }
  130. Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const {
  131. std::lock_guard lock{mutex};
  132. const auto controller_iter = controller_list.find(identifier);
  133. if (controller_iter == controller_list.cend()) {
  134. LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
  135. identifier.pad, identifier.port);
  136. return Common::Input::BatteryLevel::Charging;
  137. }
  138. const ControllerData& controller = controller_iter->second;
  139. return controller.battery;
  140. }
  141. BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const {
  142. std::lock_guard lock{mutex};
  143. const auto controller_iter = controller_list.find(identifier);
  144. if (controller_iter == controller_list.cend()) {
  145. LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(),
  146. identifier.pad, identifier.port);
  147. return {};
  148. }
  149. const ControllerData& controller = controller_iter->second;
  150. return controller.motions.at(motion);
  151. }
  152. void InputEngine::ResetButtonState() {
  153. for (const auto& controller : controller_list) {
  154. for (const auto& button : controller.second.buttons) {
  155. SetButton(controller.first, button.first, false);
  156. }
  157. for (const auto& button : controller.second.hat_buttons) {
  158. SetHatButton(controller.first, button.first, 0);
  159. }
  160. }
  161. }
  162. void InputEngine::ResetAnalogState() {
  163. for (const auto& controller : controller_list) {
  164. for (const auto& axis : controller.second.axes) {
  165. SetAxis(controller.first, axis.first, 0.0);
  166. }
  167. }
  168. }
  169. void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) {
  170. std::lock_guard lock{mutex_callback};
  171. for (const auto& poller_pair : callback_list) {
  172. const InputIdentifier& poller = poller_pair.second;
  173. if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) {
  174. continue;
  175. }
  176. if (poller.callback.on_change) {
  177. poller.callback.on_change();
  178. }
  179. }
  180. if (!configuring || !mapping_callback.on_data) {
  181. return;
  182. }
  183. PreSetButton(identifier, button);
  184. if (value == GetButton(identifier, button)) {
  185. return;
  186. }
  187. mapping_callback.on_data(MappingData{
  188. .engine = GetEngineName(),
  189. .pad = identifier,
  190. .type = EngineInputType::Button,
  191. .index = button,
  192. .button_value = value,
  193. });
  194. }
  195. void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) {
  196. std::lock_guard lock{mutex_callback};
  197. for (const auto& poller_pair : callback_list) {
  198. const InputIdentifier& poller = poller_pair.second;
  199. if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) {
  200. continue;
  201. }
  202. if (poller.callback.on_change) {
  203. poller.callback.on_change();
  204. }
  205. }
  206. if (!configuring || !mapping_callback.on_data) {
  207. return;
  208. }
  209. for (std::size_t index = 1; index < 0xff; index <<= 1) {
  210. bool button_value = (value & index) != 0;
  211. if (button_value == GetHatButton(identifier, button, static_cast<u8>(index))) {
  212. continue;
  213. }
  214. mapping_callback.on_data(MappingData{
  215. .engine = GetEngineName(),
  216. .pad = identifier,
  217. .type = EngineInputType::HatButton,
  218. .index = button,
  219. .hat_name = GetHatButtonName(static_cast<u8>(index)),
  220. });
  221. }
  222. }
  223. void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) {
  224. std::lock_guard lock{mutex_callback};
  225. for (const auto& poller_pair : callback_list) {
  226. const InputIdentifier& poller = poller_pair.second;
  227. if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) {
  228. continue;
  229. }
  230. if (poller.callback.on_change) {
  231. poller.callback.on_change();
  232. }
  233. }
  234. if (!configuring || !mapping_callback.on_data) {
  235. return;
  236. }
  237. if (std::abs(value - GetAxis(identifier, axis)) < 0.5f) {
  238. return;
  239. }
  240. mapping_callback.on_data(MappingData{
  241. .engine = GetEngineName(),
  242. .pad = identifier,
  243. .type = EngineInputType::Analog,
  244. .index = axis,
  245. .axis_value = value,
  246. });
  247. }
  248. void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier,
  249. [[maybe_unused]] Common::Input::BatteryLevel value) {
  250. std::lock_guard lock{mutex_callback};
  251. for (const auto& poller_pair : callback_list) {
  252. const InputIdentifier& poller = poller_pair.second;
  253. if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) {
  254. continue;
  255. }
  256. if (poller.callback.on_change) {
  257. poller.callback.on_change();
  258. }
  259. }
  260. }
  261. void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion,
  262. const BasicMotion& value) {
  263. std::lock_guard lock{mutex_callback};
  264. for (const auto& poller_pair : callback_list) {
  265. const InputIdentifier& poller = poller_pair.second;
  266. if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) {
  267. continue;
  268. }
  269. if (poller.callback.on_change) {
  270. poller.callback.on_change();
  271. }
  272. }
  273. if (!configuring || !mapping_callback.on_data) {
  274. return;
  275. }
  276. bool is_active = false;
  277. if (std::abs(value.accel_x) > 1.5f || std::abs(value.accel_y) > 1.5f ||
  278. std::abs(value.accel_z) > 1.5f) {
  279. is_active = true;
  280. }
  281. if (std::abs(value.gyro_x) > 0.6f || std::abs(value.gyro_y) > 0.6f ||
  282. std::abs(value.gyro_z) > 0.6f) {
  283. is_active = true;
  284. }
  285. if (!is_active) {
  286. return;
  287. }
  288. mapping_callback.on_data(MappingData{
  289. .engine = GetEngineName(),
  290. .pad = identifier,
  291. .type = EngineInputType::Motion,
  292. .index = motion,
  293. .motion_value = value,
  294. });
  295. }
  296. bool InputEngine::IsInputIdentifierEqual(const InputIdentifier& input_identifier,
  297. const PadIdentifier& identifier, EngineInputType type,
  298. int index) const {
  299. if (input_identifier.type != type) {
  300. return false;
  301. }
  302. if (input_identifier.index != index) {
  303. return false;
  304. }
  305. if (input_identifier.identifier != identifier) {
  306. return false;
  307. }
  308. return true;
  309. }
  310. void InputEngine::BeginConfiguration() {
  311. configuring = true;
  312. }
  313. void InputEngine::EndConfiguration() {
  314. configuring = false;
  315. }
  316. const std::string& InputEngine::GetEngineName() const {
  317. return input_engine;
  318. }
  319. int InputEngine::SetCallback(InputIdentifier input_identifier) {
  320. std::lock_guard lock{mutex_callback};
  321. callback_list.insert_or_assign(last_callback_key, std::move(input_identifier));
  322. return last_callback_key++;
  323. }
  324. void InputEngine::SetMappingCallback(MappingCallback callback) {
  325. std::lock_guard lock{mutex_callback};
  326. mapping_callback = std::move(callback);
  327. }
  328. void InputEngine::DeleteCallback(int key) {
  329. std::lock_guard lock{mutex_callback};
  330. const auto& iterator = callback_list.find(key);
  331. if (iterator == callback_list.end()) {
  332. LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
  333. return;
  334. }
  335. callback_list.erase(iterator);
  336. }
  337. } // namespace InputCommon