mouse.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included
  4. #include <stop_token>
  5. #include <thread>
  6. #include <fmt/format.h>
  7. #include "common/param_package.h"
  8. #include "common/settings.h"
  9. #include "common/thread.h"
  10. #include "input_common/drivers/mouse.h"
  11. namespace InputCommon {
  12. constexpr int mouse_axis_x = 0;
  13. constexpr int mouse_axis_y = 1;
  14. constexpr int wheel_axis_x = 2;
  15. constexpr int wheel_axis_y = 3;
  16. constexpr int motion_wheel_y = 4;
  17. constexpr int touch_axis_x = 10;
  18. constexpr int touch_axis_y = 11;
  19. constexpr PadIdentifier identifier = {
  20. .guid = Common::UUID{},
  21. .port = 0,
  22. .pad = 0,
  23. };
  24. Mouse::Mouse(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
  25. PreSetController(identifier);
  26. PreSetAxis(identifier, mouse_axis_x);
  27. PreSetAxis(identifier, mouse_axis_y);
  28. PreSetAxis(identifier, wheel_axis_x);
  29. PreSetAxis(identifier, wheel_axis_y);
  30. PreSetAxis(identifier, motion_wheel_y);
  31. PreSetAxis(identifier, touch_axis_x);
  32. PreSetAxis(identifier, touch_axis_y);
  33. update_thread = std::jthread([this](std::stop_token stop_token) { UpdateThread(stop_token); });
  34. }
  35. void Mouse::UpdateThread(std::stop_token stop_token) {
  36. Common::SetCurrentThreadName("yuzu:input:Mouse");
  37. constexpr int update_time = 10;
  38. while (!stop_token.stop_requested()) {
  39. if (Settings::values.mouse_panning && !Settings::values.mouse_enabled) {
  40. // Slow movement by 4%
  41. last_mouse_change *= 0.96f;
  42. const float sensitivity =
  43. Settings::values.mouse_panning_sensitivity.GetValue() * 0.022f;
  44. SetAxis(identifier, mouse_axis_x, last_mouse_change.x * sensitivity);
  45. SetAxis(identifier, mouse_axis_y, -last_mouse_change.y * sensitivity);
  46. }
  47. SetAxis(identifier, motion_wheel_y, 0.0f);
  48. if (mouse_panning_timout++ > 20) {
  49. StopPanning();
  50. }
  51. std::this_thread::sleep_for(std::chrono::milliseconds(update_time));
  52. }
  53. }
  54. void Mouse::MouseMove(int x, int y, f32 touch_x, f32 touch_y, int center_x, int center_y) {
  55. // If native mouse is enabled just set the screen coordinates
  56. if (Settings::values.mouse_enabled) {
  57. SetAxis(identifier, mouse_axis_x, touch_x);
  58. SetAxis(identifier, mouse_axis_y, touch_y);
  59. return;
  60. }
  61. SetAxis(identifier, touch_axis_x, touch_x);
  62. SetAxis(identifier, touch_axis_y, touch_y);
  63. if (Settings::values.mouse_panning) {
  64. auto mouse_change =
  65. (Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y)).Cast<float>();
  66. mouse_panning_timout = 0;
  67. const auto move_distance = mouse_change.Length();
  68. if (move_distance == 0) {
  69. return;
  70. }
  71. // Make slow movements at least 3 units on lenght
  72. if (move_distance < 3.0f) {
  73. // Normalize value
  74. mouse_change /= move_distance;
  75. mouse_change *= 3.0f;
  76. }
  77. // Average mouse movements
  78. last_mouse_change = (last_mouse_change * 0.91f) + (mouse_change * 0.09f);
  79. const auto last_move_distance = last_mouse_change.Length();
  80. // Make fast movements clamp to 8 units on lenght
  81. if (last_move_distance > 8.0f) {
  82. // Normalize value
  83. last_mouse_change /= last_move_distance;
  84. last_mouse_change *= 8.0f;
  85. }
  86. // Ignore average if it's less than 1 unit and use current movement value
  87. if (last_move_distance < 1.0f) {
  88. last_mouse_change = mouse_change / mouse_change.Length();
  89. }
  90. return;
  91. }
  92. if (button_pressed) {
  93. const auto mouse_move = Common::MakeVec<int>(x, y) - mouse_origin;
  94. const float sensitivity = Settings::values.mouse_panning_sensitivity.GetValue() * 0.0012f;
  95. SetAxis(identifier, mouse_axis_x, static_cast<float>(mouse_move.x) * sensitivity);
  96. SetAxis(identifier, mouse_axis_y, static_cast<float>(-mouse_move.y) * sensitivity);
  97. }
  98. }
  99. void Mouse::PressButton(int x, int y, f32 touch_x, f32 touch_y, MouseButton button) {
  100. SetAxis(identifier, touch_axis_x, touch_x);
  101. SetAxis(identifier, touch_axis_y, touch_y);
  102. SetButton(identifier, static_cast<int>(button), true);
  103. // Set initial analog parameters
  104. mouse_origin = {x, y};
  105. last_mouse_position = {x, y};
  106. button_pressed = true;
  107. }
  108. void Mouse::ReleaseButton(MouseButton button) {
  109. SetButton(identifier, static_cast<int>(button), false);
  110. if (!Settings::values.mouse_panning && !Settings::values.mouse_enabled) {
  111. SetAxis(identifier, mouse_axis_x, 0);
  112. SetAxis(identifier, mouse_axis_y, 0);
  113. }
  114. button_pressed = false;
  115. }
  116. void Mouse::MouseWheelChange(int x, int y) {
  117. wheel_position.x += x;
  118. wheel_position.y += y;
  119. SetAxis(identifier, wheel_axis_x, static_cast<f32>(wheel_position.x));
  120. SetAxis(identifier, wheel_axis_y, static_cast<f32>(wheel_position.y));
  121. SetAxis(identifier, motion_wheel_y, static_cast<f32>(y) / 100.0f);
  122. }
  123. void Mouse::ReleaseAllButtons() {
  124. ResetButtonState();
  125. button_pressed = false;
  126. }
  127. void Mouse::StopPanning() {
  128. last_mouse_change = {};
  129. }
  130. std::vector<Common::ParamPackage> Mouse::GetInputDevices() const {
  131. std::vector<Common::ParamPackage> devices;
  132. devices.emplace_back(Common::ParamPackage{
  133. {"engine", GetEngineName()},
  134. {"display", "Keyboard/Mouse"},
  135. });
  136. return devices;
  137. }
  138. AnalogMapping Mouse::GetAnalogMappingForDevice(
  139. [[maybe_unused]] const Common::ParamPackage& params) {
  140. // Only overwrite different buttons from default
  141. AnalogMapping mapping = {};
  142. Common::ParamPackage right_analog_params;
  143. right_analog_params.Set("engine", GetEngineName());
  144. right_analog_params.Set("axis_x", 0);
  145. right_analog_params.Set("axis_y", 1);
  146. right_analog_params.Set("threshold", 0.5f);
  147. right_analog_params.Set("range", 1.0f);
  148. right_analog_params.Set("deadzone", 0.0f);
  149. mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params));
  150. return mapping;
  151. }
  152. Common::Input::ButtonNames Mouse::GetUIButtonName(const Common::ParamPackage& params) const {
  153. const auto button = static_cast<MouseButton>(params.Get("button", 0));
  154. switch (button) {
  155. case MouseButton::Left:
  156. return Common::Input::ButtonNames::ButtonLeft;
  157. case MouseButton::Right:
  158. return Common::Input::ButtonNames::ButtonRight;
  159. case MouseButton::Wheel:
  160. return Common::Input::ButtonNames::ButtonMouseWheel;
  161. case MouseButton::Backward:
  162. return Common::Input::ButtonNames::ButtonBackward;
  163. case MouseButton::Forward:
  164. return Common::Input::ButtonNames::ButtonForward;
  165. case MouseButton::Task:
  166. return Common::Input::ButtonNames::ButtonTask;
  167. case MouseButton::Extra:
  168. return Common::Input::ButtonNames::ButtonExtra;
  169. case MouseButton::Undefined:
  170. default:
  171. return Common::Input::ButtonNames::Undefined;
  172. }
  173. }
  174. Common::Input::ButtonNames Mouse::GetUIName(const Common::ParamPackage& params) const {
  175. if (params.Has("button")) {
  176. return GetUIButtonName(params);
  177. }
  178. if (params.Has("axis")) {
  179. return Common::Input::ButtonNames::Value;
  180. }
  181. if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
  182. return Common::Input::ButtonNames::Engine;
  183. }
  184. return Common::Input::ButtonNames::Invalid;
  185. }
  186. } // namespace InputCommon