emulated_console.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included
  4. #include <fmt/format.h>
  5. #include "core/hid/emulated_console.h"
  6. #include "core/hid/input_converter.h"
  7. namespace Core::HID {
  8. EmulatedConsole::EmulatedConsole() {}
  9. EmulatedConsole::~EmulatedConsole() = default;
  10. void EmulatedConsole::ReloadFromSettings() {
  11. // Using first motion device from player 1. No need to assign a special config at the moment
  12. const auto& player = Settings::values.players.GetValue()[0];
  13. motion_params = Common::ParamPackage(player.motions[0]);
  14. ReloadInput();
  15. }
  16. void EmulatedConsole::ReloadInput() {
  17. motion_devices = Input::CreateDevice<Input::InputDevice>(motion_params);
  18. if (motion_devices) {
  19. Input::InputCallback motion_callback{
  20. [this](Input::CallbackStatus callback) { SetMotion(callback); }};
  21. motion_devices->SetCallback(motion_callback);
  22. }
  23. // TODO: Fix this mess
  24. std::size_t index = 0;
  25. const std::string mouse_device_string =
  26. fmt::format("engine:mouse,axis_x:10,axis_y:11,button:{}", index);
  27. touch_devices[index] = Input::CreateDeviceFromString<Input::InputDevice>(mouse_device_string);
  28. Input::InputCallback trigger_callbackk{
  29. [this, index](Input::CallbackStatus callback) { SetTouch(callback, index); }};
  30. touch_devices[index]->SetCallback(trigger_callbackk);
  31. index++;
  32. const auto button_index =
  33. static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue());
  34. const auto& touch_buttons = Settings::values.touch_from_button_maps[button_index].buttons;
  35. for (const auto& config_entry : touch_buttons) {
  36. Common::ParamPackage params{config_entry};
  37. Common::ParamPackage touch_button_params;
  38. const int x = params.Get("x", 0);
  39. const int y = params.Get("y", 0);
  40. params.Erase("x");
  41. params.Erase("y");
  42. touch_button_params.Set("engine", "touch_from_button");
  43. touch_button_params.Set("button", params.Serialize());
  44. touch_button_params.Set("x", x);
  45. touch_button_params.Set("y", y);
  46. touch_button_params.Set("touch_id", static_cast<int>(index));
  47. LOG_ERROR(Common, "{} ", touch_button_params.Serialize());
  48. touch_devices[index] =
  49. Input::CreateDeviceFromString<Input::InputDevice>(touch_button_params.Serialize());
  50. if (!touch_devices[index]) {
  51. continue;
  52. }
  53. Input::InputCallback trigger_callback{
  54. [this, index](Input::CallbackStatus callback) { SetTouch(callback, index); }};
  55. touch_devices[index]->SetCallback(trigger_callback);
  56. index++;
  57. }
  58. }
  59. void EmulatedConsole::UnloadInput() {
  60. motion_devices.reset();
  61. for (auto& touch : touch_devices) {
  62. touch.reset();
  63. }
  64. }
  65. void EmulatedConsole::EnableConfiguration() {
  66. is_configuring = true;
  67. SaveCurrentConfig();
  68. }
  69. void EmulatedConsole::DisableConfiguration() {
  70. is_configuring = false;
  71. }
  72. bool EmulatedConsole::IsConfiguring() const {
  73. return is_configuring;
  74. }
  75. void EmulatedConsole::SaveCurrentConfig() {
  76. if (!is_configuring) {
  77. return;
  78. }
  79. }
  80. void EmulatedConsole::RestoreConfig() {
  81. if (!is_configuring) {
  82. return;
  83. }
  84. ReloadFromSettings();
  85. }
  86. Common::ParamPackage EmulatedConsole::GetMotionParam() const {
  87. return motion_params;
  88. }
  89. void EmulatedConsole::SetMotionParam(Common::ParamPackage param) {
  90. motion_params = param;
  91. ReloadInput();
  92. }
  93. void EmulatedConsole::SetMotion(Input::CallbackStatus callback) {
  94. std::lock_guard lock{mutex};
  95. auto& raw_status = console.motion_values.raw_status;
  96. auto& emulated = console.motion_values.emulated;
  97. raw_status = TransformToMotion(callback);
  98. emulated.SetAcceleration(Common::Vec3f{
  99. raw_status.accel.x.value,
  100. raw_status.accel.y.value,
  101. raw_status.accel.z.value,
  102. });
  103. emulated.SetGyroscope(Common::Vec3f{
  104. raw_status.gyro.x.value,
  105. raw_status.gyro.y.value,
  106. raw_status.gyro.z.value,
  107. });
  108. emulated.UpdateRotation(raw_status.delta_timestamp);
  109. emulated.UpdateOrientation(raw_status.delta_timestamp);
  110. if (is_configuring) {
  111. TriggerOnChange(ConsoleTriggerType::Motion);
  112. return;
  113. }
  114. auto& motion = console.motion_state;
  115. motion.accel = emulated.GetAcceleration();
  116. motion.gyro = emulated.GetGyroscope();
  117. motion.rotation = emulated.GetGyroscope();
  118. motion.orientation = emulated.GetOrientation();
  119. motion.quaternion = emulated.GetQuaternion();
  120. motion.is_at_rest = emulated.IsMoving(motion_sensitivity);
  121. TriggerOnChange(ConsoleTriggerType::Motion);
  122. }
  123. void EmulatedConsole::SetTouch(Input::CallbackStatus callback, [[maybe_unused]] std::size_t index) {
  124. if (index >= console.touch_values.size()) {
  125. return;
  126. }
  127. std::lock_guard lock{mutex};
  128. console.touch_values[index] = TransformToTouch(callback);
  129. if (is_configuring) {
  130. TriggerOnChange(ConsoleTriggerType::Touch);
  131. return;
  132. }
  133. console.touch_state[index] = {
  134. .position = {console.touch_values[index].x.value, console.touch_values[index].y.value},
  135. .id = console.touch_values[index].id,
  136. .pressed = console.touch_values[index].pressed.value,
  137. };
  138. TriggerOnChange(ConsoleTriggerType::Touch);
  139. }
  140. ConsoleMotionValues EmulatedConsole::GetMotionValues() const {
  141. return console.motion_values;
  142. }
  143. TouchValues EmulatedConsole::GetTouchValues() const {
  144. return console.touch_values;
  145. }
  146. ConsoleMotion EmulatedConsole::GetMotion() const {
  147. return console.motion_state;
  148. }
  149. TouchFingerState EmulatedConsole::GetTouch() const {
  150. return console.touch_state;
  151. }
  152. void EmulatedConsole::TriggerOnChange(ConsoleTriggerType type) {
  153. for (const std::pair<int, ConsoleUpdateCallback> poller_pair : callback_list) {
  154. const ConsoleUpdateCallback& poller = poller_pair.second;
  155. if (poller.on_change) {
  156. poller.on_change(type);
  157. }
  158. }
  159. }
  160. int EmulatedConsole::SetCallback(ConsoleUpdateCallback update_callback) {
  161. std::lock_guard lock{mutex};
  162. callback_list.insert_or_assign(last_callback_key, update_callback);
  163. return last_callback_key++;
  164. }
  165. void EmulatedConsole::DeleteCallback(int key) {
  166. std::lock_guard lock{mutex};
  167. if (!callback_list.contains(key)) {
  168. LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
  169. return;
  170. }
  171. callback_list.erase(key);
  172. }
  173. } // namespace Core::HID