emulated_console.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/settings.h"
  5. #include "core/hid/emulated_console.h"
  6. #include "core/hid/input_converter.h"
  7. namespace Core::HID {
  8. EmulatedConsole::EmulatedConsole() = default;
  9. EmulatedConsole::~EmulatedConsole() = default;
  10. void EmulatedConsole::ReloadFromSettings() {
  11. // Using first motion device from player 1. No need to assign any unique 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::SetTouchParams() {
  17. // TODO(german77): Support any number of fingers
  18. std::size_t index = 0;
  19. // Hardcode mouse, touchscreen and cemuhook parameters
  20. touch_params[index++] = Common::ParamPackage{"engine:mouse,axis_x:10,axis_y:11,button:0"};
  21. touch_params[index++] = Common::ParamPackage{"engine:touch,axis_x:0,axis_y:1,button:0"};
  22. touch_params[index++] = Common::ParamPackage{"engine:touch,axis_x:2,axis_y:3,button:1"};
  23. touch_params[index++] = Common::ParamPackage{"engine:cemuhookudp,axis_x:0,axis_y:1,button:0"};
  24. touch_params[index++] = Common::ParamPackage{"engine:cemuhookudp,axis_x:2,axis_y:3,button:1"};
  25. const auto button_index =
  26. static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue());
  27. const auto& touch_buttons = Settings::values.touch_from_button_maps[button_index].buttons;
  28. // Map the rest of the fingers from touch from button configuration
  29. for (const auto& config_entry : touch_buttons) {
  30. Common::ParamPackage params{config_entry};
  31. Common::ParamPackage touch_button_params;
  32. const int x = params.Get("x", 0);
  33. const int y = params.Get("y", 0);
  34. params.Erase("x");
  35. params.Erase("y");
  36. touch_button_params.Set("engine", "touch_from_button");
  37. touch_button_params.Set("button", params.Serialize());
  38. touch_button_params.Set("x", x);
  39. touch_button_params.Set("y", y);
  40. touch_button_params.Set("touch_id", static_cast<int>(index));
  41. touch_params[index] = touch_button_params;
  42. index++;
  43. if (index >= touch_params.size()) {
  44. return;
  45. }
  46. }
  47. }
  48. void EmulatedConsole::ReloadInput() {
  49. // If you load any device here add the equivalent to the UnloadInput() function
  50. SetTouchParams();
  51. motion_devices = Common::Input::CreateDevice<Common::Input::InputDevice>(motion_params);
  52. if (motion_devices) {
  53. Common::Input::InputCallback motion_callback{
  54. [this](Common::Input::CallbackStatus callback) { SetMotion(callback); }};
  55. motion_devices->SetCallback(motion_callback);
  56. }
  57. // Unique index for identifying touch device source
  58. std::size_t index = 0;
  59. for (auto& touch_device : touch_devices) {
  60. touch_device = Common::Input::CreateDevice<Common::Input::InputDevice>(touch_params[index]);
  61. if (!touch_device) {
  62. continue;
  63. }
  64. Common::Input::InputCallback touch_callback{
  65. [this, index](Common::Input::CallbackStatus callback) { SetTouch(callback, index); }};
  66. touch_device->SetCallback(touch_callback);
  67. index++;
  68. }
  69. }
  70. void EmulatedConsole::UnloadInput() {
  71. motion_devices.reset();
  72. for (auto& touch : touch_devices) {
  73. touch.reset();
  74. }
  75. }
  76. void EmulatedConsole::EnableConfiguration() {
  77. is_configuring = true;
  78. SaveCurrentConfig();
  79. }
  80. void EmulatedConsole::DisableConfiguration() {
  81. is_configuring = false;
  82. }
  83. bool EmulatedConsole::IsConfiguring() const {
  84. return is_configuring;
  85. }
  86. void EmulatedConsole::SaveCurrentConfig() {
  87. if (!is_configuring) {
  88. return;
  89. }
  90. }
  91. void EmulatedConsole::RestoreConfig() {
  92. if (!is_configuring) {
  93. return;
  94. }
  95. ReloadFromSettings();
  96. }
  97. Common::ParamPackage EmulatedConsole::GetMotionParam() const {
  98. return motion_params;
  99. }
  100. void EmulatedConsole::SetMotionParam(Common::ParamPackage param) {
  101. motion_params = param;
  102. ReloadInput();
  103. }
  104. void EmulatedConsole::SetMotion(Common::Input::CallbackStatus callback) {
  105. std::lock_guard lock{mutex};
  106. auto& raw_status = console.motion_values.raw_status;
  107. auto& emulated = console.motion_values.emulated;
  108. raw_status = TransformToMotion(callback);
  109. emulated.SetAcceleration(Common::Vec3f{
  110. raw_status.accel.x.value,
  111. raw_status.accel.y.value,
  112. raw_status.accel.z.value,
  113. });
  114. emulated.SetGyroscope(Common::Vec3f{
  115. raw_status.gyro.x.value,
  116. raw_status.gyro.y.value,
  117. raw_status.gyro.z.value,
  118. });
  119. emulated.UpdateRotation(raw_status.delta_timestamp);
  120. emulated.UpdateOrientation(raw_status.delta_timestamp);
  121. if (is_configuring) {
  122. TriggerOnChange(ConsoleTriggerType::Motion);
  123. return;
  124. }
  125. auto& motion = console.motion_state;
  126. motion.accel = emulated.GetAcceleration();
  127. motion.gyro = emulated.GetGyroscope();
  128. motion.rotation = emulated.GetGyroscope();
  129. motion.orientation = emulated.GetOrientation();
  130. motion.quaternion = emulated.GetQuaternion();
  131. motion.is_at_rest = !emulated.IsMoving(motion_sensitivity);
  132. TriggerOnChange(ConsoleTriggerType::Motion);
  133. }
  134. void EmulatedConsole::SetTouch(Common::Input::CallbackStatus callback,
  135. [[maybe_unused]] std::size_t index) {
  136. if (index >= console.touch_values.size()) {
  137. return;
  138. }
  139. std::lock_guard lock{mutex};
  140. console.touch_values[index] = TransformToTouch(callback);
  141. if (is_configuring) {
  142. TriggerOnChange(ConsoleTriggerType::Touch);
  143. return;
  144. }
  145. // TODO(german77): Remap touch id in sequential order
  146. console.touch_state[index] = {
  147. .position = {console.touch_values[index].x.value, console.touch_values[index].y.value},
  148. .id = static_cast<u32>(console.touch_values[index].id),
  149. .pressed = console.touch_values[index].pressed.value,
  150. };
  151. TriggerOnChange(ConsoleTriggerType::Touch);
  152. }
  153. ConsoleMotionValues EmulatedConsole::GetMotionValues() const {
  154. return console.motion_values;
  155. }
  156. TouchValues EmulatedConsole::GetTouchValues() const {
  157. return console.touch_values;
  158. }
  159. ConsoleMotion EmulatedConsole::GetMotion() const {
  160. return console.motion_state;
  161. }
  162. TouchFingerState EmulatedConsole::GetTouch() const {
  163. return console.touch_state;
  164. }
  165. void EmulatedConsole::TriggerOnChange(ConsoleTriggerType type) {
  166. for (const auto& poller_pair : callback_list) {
  167. const ConsoleUpdateCallback& poller = poller_pair.second;
  168. if (poller.on_change) {
  169. poller.on_change(type);
  170. }
  171. }
  172. }
  173. int EmulatedConsole::SetCallback(ConsoleUpdateCallback update_callback) {
  174. std::lock_guard lock{mutex};
  175. callback_list.insert_or_assign(last_callback_key, update_callback);
  176. return last_callback_key++;
  177. }
  178. void EmulatedConsole::DeleteCallback(int key) {
  179. std::lock_guard lock{mutex};
  180. const auto& iterator = callback_list.find(key);
  181. if (iterator == callback_list.end()) {
  182. LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
  183. return;
  184. }
  185. callback_list.erase(iterator);
  186. }
  187. } // namespace Core::HID