emulated_console.cpp 7.3 KB

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