diff options
Diffstat (limited to '')
| -rw-r--r-- | src/input_common/mouse/mouse_poller.cpp | 299 |
1 files changed, 0 insertions, 299 deletions
diff --git a/src/input_common/mouse/mouse_poller.cpp b/src/input_common/mouse/mouse_poller.cpp deleted file mode 100644 index 090b26972..000000000 --- a/src/input_common/mouse/mouse_poller.cpp +++ /dev/null | |||
| @@ -1,299 +0,0 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <memory> | ||
| 7 | #include <mutex> | ||
| 8 | #include <utility> | ||
| 9 | |||
| 10 | #include "common/settings.h" | ||
| 11 | #include "common/threadsafe_queue.h" | ||
| 12 | #include "input_common/mouse/mouse_input.h" | ||
| 13 | #include "input_common/mouse/mouse_poller.h" | ||
| 14 | |||
| 15 | namespace InputCommon { | ||
| 16 | |||
| 17 | class MouseButton final : public Input::ButtonDevice { | ||
| 18 | public: | ||
| 19 | explicit MouseButton(u32 button_, bool toggle_, MouseInput::Mouse* mouse_input_) | ||
| 20 | : button(button_), toggle(toggle_), mouse_input(mouse_input_) {} | ||
| 21 | |||
| 22 | bool GetStatus() const override { | ||
| 23 | const bool button_state = mouse_input->GetMouseState(button).pressed; | ||
| 24 | if (!toggle) { | ||
| 25 | return button_state; | ||
| 26 | } | ||
| 27 | |||
| 28 | if (button_state) { | ||
| 29 | return mouse_input->ToggleButton(button); | ||
| 30 | } | ||
| 31 | return mouse_input->UnlockButton(button); | ||
| 32 | } | ||
| 33 | |||
| 34 | private: | ||
| 35 | const u32 button; | ||
| 36 | const bool toggle; | ||
| 37 | MouseInput::Mouse* mouse_input; | ||
| 38 | }; | ||
| 39 | |||
| 40 | MouseButtonFactory::MouseButtonFactory(std::shared_ptr<MouseInput::Mouse> mouse_input_) | ||
| 41 | : mouse_input(std::move(mouse_input_)) {} | ||
| 42 | |||
| 43 | std::unique_ptr<Input::ButtonDevice> MouseButtonFactory::Create( | ||
| 44 | const Common::ParamPackage& params) { | ||
| 45 | const auto button_id = params.Get("button", 0); | ||
| 46 | const auto toggle = params.Get("toggle", false); | ||
| 47 | |||
| 48 | return std::make_unique<MouseButton>(button_id, toggle, mouse_input.get()); | ||
| 49 | } | ||
| 50 | |||
| 51 | Common::ParamPackage MouseButtonFactory::GetNextInput() const { | ||
| 52 | MouseInput::MouseStatus pad; | ||
| 53 | Common::ParamPackage params; | ||
| 54 | auto& queue = mouse_input->GetMouseQueue(); | ||
| 55 | while (queue.Pop(pad)) { | ||
| 56 | // This while loop will break on the earliest detected button | ||
| 57 | if (pad.button != MouseInput::MouseButton::Undefined) { | ||
| 58 | params.Set("engine", "mouse"); | ||
| 59 | params.Set("button", static_cast<u16>(pad.button)); | ||
| 60 | params.Set("toggle", false); | ||
| 61 | return params; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return params; | ||
| 65 | } | ||
| 66 | |||
| 67 | void MouseButtonFactory::BeginConfiguration() { | ||
| 68 | polling = true; | ||
| 69 | mouse_input->BeginConfiguration(); | ||
| 70 | } | ||
| 71 | |||
| 72 | void MouseButtonFactory::EndConfiguration() { | ||
| 73 | polling = false; | ||
| 74 | mouse_input->EndConfiguration(); | ||
| 75 | } | ||
| 76 | |||
| 77 | class MouseAnalog final : public Input::AnalogDevice { | ||
| 78 | public: | ||
| 79 | explicit MouseAnalog(u32 port_, u32 axis_x_, u32 axis_y_, bool invert_x_, bool invert_y_, | ||
| 80 | float deadzone_, float range_, const MouseInput::Mouse* mouse_input_) | ||
| 81 | : button(port_), axis_x(axis_x_), axis_y(axis_y_), invert_x(invert_x_), invert_y(invert_y_), | ||
| 82 | deadzone(deadzone_), range(range_), mouse_input(mouse_input_) {} | ||
| 83 | |||
| 84 | float GetAxis(u32 axis) const { | ||
| 85 | std::lock_guard lock{mutex}; | ||
| 86 | const auto axis_value = | ||
| 87 | static_cast<float>(mouse_input->GetMouseState(button).axis.at(axis)); | ||
| 88 | const float sensitivity = Settings::values.mouse_panning_sensitivity.GetValue() * 0.10f; | ||
| 89 | return axis_value * sensitivity / (100.0f * range); | ||
| 90 | } | ||
| 91 | |||
| 92 | std::pair<float, float> GetAnalog(u32 analog_axis_x, u32 analog_axis_y) const { | ||
| 93 | float x = GetAxis(analog_axis_x); | ||
| 94 | float y = GetAxis(analog_axis_y); | ||
| 95 | if (invert_x) { | ||
| 96 | x = -x; | ||
| 97 | } | ||
| 98 | if (invert_y) { | ||
| 99 | y = -y; | ||
| 100 | } | ||
| 101 | |||
| 102 | // Make sure the coordinates are in the unit circle, | ||
| 103 | // otherwise normalize it. | ||
| 104 | float r = x * x + y * y; | ||
| 105 | if (r > 1.0f) { | ||
| 106 | r = std::sqrt(r); | ||
| 107 | x /= r; | ||
| 108 | y /= r; | ||
| 109 | } | ||
| 110 | |||
| 111 | return {x, y}; | ||
| 112 | } | ||
| 113 | |||
| 114 | std::tuple<float, float> GetStatus() const override { | ||
| 115 | const auto [x, y] = GetAnalog(axis_x, axis_y); | ||
| 116 | const float r = std::sqrt((x * x) + (y * y)); | ||
| 117 | if (r > deadzone) { | ||
| 118 | return {x / r * (r - deadzone) / (1 - deadzone), | ||
| 119 | y / r * (r - deadzone) / (1 - deadzone)}; | ||
| 120 | } | ||
| 121 | return {0.0f, 0.0f}; | ||
| 122 | } | ||
| 123 | |||
| 124 | std::tuple<float, float> GetRawStatus() const override { | ||
| 125 | const float x = GetAxis(axis_x); | ||
| 126 | const float y = GetAxis(axis_y); | ||
| 127 | return {x, y}; | ||
| 128 | } | ||
| 129 | |||
| 130 | Input::AnalogProperties GetAnalogProperties() const override { | ||
| 131 | return {deadzone, range, 0.5f}; | ||
| 132 | } | ||
| 133 | |||
| 134 | private: | ||
| 135 | const u32 button; | ||
| 136 | const u32 axis_x; | ||
| 137 | const u32 axis_y; | ||
| 138 | const bool invert_x; | ||
| 139 | const bool invert_y; | ||
| 140 | const float deadzone; | ||
| 141 | const float range; | ||
| 142 | const MouseInput::Mouse* mouse_input; | ||
| 143 | mutable std::mutex mutex; | ||
| 144 | }; | ||
| 145 | |||
| 146 | /// An analog device factory that creates analog devices from GC Adapter | ||
| 147 | MouseAnalogFactory::MouseAnalogFactory(std::shared_ptr<MouseInput::Mouse> mouse_input_) | ||
| 148 | : mouse_input(std::move(mouse_input_)) {} | ||
| 149 | |||
| 150 | /** | ||
| 151 | * Creates analog device from joystick axes | ||
| 152 | * @param params contains parameters for creating the device: | ||
| 153 | * - "port": the nth gcpad on the adapter | ||
| 154 | * - "axis_x": the index of the axis to be bind as x-axis | ||
| 155 | * - "axis_y": the index of the axis to be bind as y-axis | ||
| 156 | */ | ||
| 157 | std::unique_ptr<Input::AnalogDevice> MouseAnalogFactory::Create( | ||
| 158 | const Common::ParamPackage& params) { | ||
| 159 | const auto port = static_cast<u32>(params.Get("port", 0)); | ||
| 160 | const auto axis_x = static_cast<u32>(params.Get("axis_x", 0)); | ||
| 161 | const auto axis_y = static_cast<u32>(params.Get("axis_y", 1)); | ||
| 162 | const auto deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, 1.0f); | ||
| 163 | const auto range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); | ||
| 164 | const std::string invert_x_value = params.Get("invert_x", "+"); | ||
| 165 | const std::string invert_y_value = params.Get("invert_y", "+"); | ||
| 166 | const bool invert_x = invert_x_value == "-"; | ||
| 167 | const bool invert_y = invert_y_value == "-"; | ||
| 168 | |||
| 169 | return std::make_unique<MouseAnalog>(port, axis_x, axis_y, invert_x, invert_y, deadzone, range, | ||
| 170 | mouse_input.get()); | ||
| 171 | } | ||
| 172 | |||
| 173 | void MouseAnalogFactory::BeginConfiguration() { | ||
| 174 | polling = true; | ||
| 175 | mouse_input->BeginConfiguration(); | ||
| 176 | } | ||
| 177 | |||
| 178 | void MouseAnalogFactory::EndConfiguration() { | ||
| 179 | polling = false; | ||
| 180 | mouse_input->EndConfiguration(); | ||
| 181 | } | ||
| 182 | |||
| 183 | Common::ParamPackage MouseAnalogFactory::GetNextInput() const { | ||
| 184 | MouseInput::MouseStatus pad; | ||
| 185 | Common::ParamPackage params; | ||
| 186 | auto& queue = mouse_input->GetMouseQueue(); | ||
| 187 | while (queue.Pop(pad)) { | ||
| 188 | // This while loop will break on the earliest detected button | ||
| 189 | if (pad.button != MouseInput::MouseButton::Undefined) { | ||
| 190 | params.Set("engine", "mouse"); | ||
| 191 | params.Set("port", static_cast<u16>(pad.button)); | ||
| 192 | params.Set("axis_x", 0); | ||
| 193 | params.Set("axis_y", 1); | ||
| 194 | params.Set("invert_x", "+"); | ||
| 195 | params.Set("invert_y", "+"); | ||
| 196 | return params; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | return params; | ||
| 200 | } | ||
| 201 | |||
| 202 | class MouseMotion final : public Input::MotionDevice { | ||
| 203 | public: | ||
| 204 | explicit MouseMotion(u32 button_, const MouseInput::Mouse* mouse_input_) | ||
| 205 | : button(button_), mouse_input(mouse_input_) {} | ||
| 206 | |||
| 207 | Input::MotionStatus GetStatus() const override { | ||
| 208 | return mouse_input->GetMouseState(button).motion; | ||
| 209 | } | ||
| 210 | |||
| 211 | private: | ||
| 212 | const u32 button; | ||
| 213 | const MouseInput::Mouse* mouse_input; | ||
| 214 | }; | ||
| 215 | |||
| 216 | MouseMotionFactory::MouseMotionFactory(std::shared_ptr<MouseInput::Mouse> mouse_input_) | ||
| 217 | : mouse_input(std::move(mouse_input_)) {} | ||
| 218 | |||
| 219 | std::unique_ptr<Input::MotionDevice> MouseMotionFactory::Create( | ||
| 220 | const Common::ParamPackage& params) { | ||
| 221 | const auto button_id = params.Get("button", 0); | ||
| 222 | |||
| 223 | return std::make_unique<MouseMotion>(button_id, mouse_input.get()); | ||
| 224 | } | ||
| 225 | |||
| 226 | Common::ParamPackage MouseMotionFactory::GetNextInput() const { | ||
| 227 | MouseInput::MouseStatus pad; | ||
| 228 | Common::ParamPackage params; | ||
| 229 | auto& queue = mouse_input->GetMouseQueue(); | ||
| 230 | while (queue.Pop(pad)) { | ||
| 231 | // This while loop will break on the earliest detected button | ||
| 232 | if (pad.button != MouseInput::MouseButton::Undefined) { | ||
| 233 | params.Set("engine", "mouse"); | ||
| 234 | params.Set("button", static_cast<u16>(pad.button)); | ||
| 235 | return params; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | return params; | ||
| 239 | } | ||
| 240 | |||
| 241 | void MouseMotionFactory::BeginConfiguration() { | ||
| 242 | polling = true; | ||
| 243 | mouse_input->BeginConfiguration(); | ||
| 244 | } | ||
| 245 | |||
| 246 | void MouseMotionFactory::EndConfiguration() { | ||
| 247 | polling = false; | ||
| 248 | mouse_input->EndConfiguration(); | ||
| 249 | } | ||
| 250 | |||
| 251 | class MouseTouch final : public Input::TouchDevice { | ||
| 252 | public: | ||
| 253 | explicit MouseTouch(u32 button_, const MouseInput::Mouse* mouse_input_) | ||
| 254 | : button(button_), mouse_input(mouse_input_) {} | ||
| 255 | |||
| 256 | Input::TouchStatus GetStatus() const override { | ||
| 257 | return mouse_input->GetMouseState(button).touch; | ||
| 258 | } | ||
| 259 | |||
| 260 | private: | ||
| 261 | const u32 button; | ||
| 262 | const MouseInput::Mouse* mouse_input; | ||
| 263 | }; | ||
| 264 | |||
| 265 | MouseTouchFactory::MouseTouchFactory(std::shared_ptr<MouseInput::Mouse> mouse_input_) | ||
| 266 | : mouse_input(std::move(mouse_input_)) {} | ||
| 267 | |||
| 268 | std::unique_ptr<Input::TouchDevice> MouseTouchFactory::Create(const Common::ParamPackage& params) { | ||
| 269 | const auto button_id = params.Get("button", 0); | ||
| 270 | |||
| 271 | return std::make_unique<MouseTouch>(button_id, mouse_input.get()); | ||
| 272 | } | ||
| 273 | |||
| 274 | Common::ParamPackage MouseTouchFactory::GetNextInput() const { | ||
| 275 | MouseInput::MouseStatus pad; | ||
| 276 | Common::ParamPackage params; | ||
| 277 | auto& queue = mouse_input->GetMouseQueue(); | ||
| 278 | while (queue.Pop(pad)) { | ||
| 279 | // This while loop will break on the earliest detected button | ||
| 280 | if (pad.button != MouseInput::MouseButton::Undefined) { | ||
| 281 | params.Set("engine", "mouse"); | ||
| 282 | params.Set("button", static_cast<u16>(pad.button)); | ||
| 283 | return params; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | return params; | ||
| 287 | } | ||
| 288 | |||
| 289 | void MouseTouchFactory::BeginConfiguration() { | ||
| 290 | polling = true; | ||
| 291 | mouse_input->BeginConfiguration(); | ||
| 292 | } | ||
| 293 | |||
| 294 | void MouseTouchFactory::EndConfiguration() { | ||
| 295 | polling = false; | ||
| 296 | mouse_input->EndConfiguration(); | ||
| 297 | } | ||
| 298 | |||
| 299 | } // namespace InputCommon | ||