emulated_console.cpp 7.1 KB

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