2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-09-21 00:19:55 +02:00
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
#include <fmt/format.h>
|
2023-03-05 18:00:00 +01:00
|
|
|
#include <math.h>
|
2021-09-21 00:19:55 +02:00
|
|
|
|
|
|
|
#include "common/param_package.h"
|
|
|
|
#include "common/settings.h"
|
|
|
|
#include "common/thread.h"
|
|
|
|
#include "input_common/drivers/mouse.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
2023-02-22 04:46:42 +01:00
|
|
|
constexpr int update_time = 10;
|
2023-03-05 18:00:00 +01:00
|
|
|
constexpr float default_stick_sensitivity = 0.0044f;
|
|
|
|
constexpr float default_motion_sensitivity = 0.0003f;
|
|
|
|
constexpr float maximum_rotation_speed = 2.0f;
|
2021-11-14 21:09:29 +01:00
|
|
|
constexpr int mouse_axis_x = 0;
|
|
|
|
constexpr int mouse_axis_y = 1;
|
|
|
|
constexpr int wheel_axis_x = 2;
|
|
|
|
constexpr int wheel_axis_y = 3;
|
2021-11-01 21:17:53 +01:00
|
|
|
constexpr PadIdentifier identifier = {
|
2022-02-05 18:35:39 +01:00
|
|
|
.guid = Common::UUID{},
|
2021-11-01 21:17:53 +01:00
|
|
|
.port = 0,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
2021-09-21 00:19:55 +02:00
|
|
|
|
2023-02-22 04:46:42 +01:00
|
|
|
constexpr PadIdentifier motion_identifier = {
|
|
|
|
.guid = Common::UUID{},
|
|
|
|
.port = 0,
|
|
|
|
.pad = 1,
|
|
|
|
};
|
|
|
|
|
2023-02-16 20:38:50 +01:00
|
|
|
constexpr PadIdentifier real_mouse_identifier = {
|
|
|
|
.guid = Common::UUID{},
|
|
|
|
.port = 1,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
constexpr PadIdentifier touch_identifier = {
|
|
|
|
.guid = Common::UUID{},
|
|
|
|
.port = 2,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
|
|
|
|
2021-12-13 15:05:23 +01:00
|
|
|
Mouse::Mouse(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
|
2021-09-21 00:19:55 +02:00
|
|
|
PreSetController(identifier);
|
2023-02-16 20:38:50 +01:00
|
|
|
PreSetController(real_mouse_identifier);
|
|
|
|
PreSetController(touch_identifier);
|
2023-02-22 04:46:42 +01:00
|
|
|
PreSetController(motion_identifier);
|
2023-02-16 20:38:50 +01:00
|
|
|
|
|
|
|
// Initialize all mouse axis
|
2021-11-14 21:09:29 +01:00
|
|
|
PreSetAxis(identifier, mouse_axis_x);
|
|
|
|
PreSetAxis(identifier, mouse_axis_y);
|
|
|
|
PreSetAxis(identifier, wheel_axis_x);
|
|
|
|
PreSetAxis(identifier, wheel_axis_y);
|
2023-02-16 20:38:50 +01:00
|
|
|
PreSetAxis(real_mouse_identifier, mouse_axis_x);
|
|
|
|
PreSetAxis(real_mouse_identifier, mouse_axis_y);
|
|
|
|
PreSetAxis(touch_identifier, mouse_axis_x);
|
|
|
|
PreSetAxis(touch_identifier, mouse_axis_y);
|
2023-02-22 04:46:42 +01:00
|
|
|
|
|
|
|
// Initialize variables
|
|
|
|
mouse_origin = {};
|
|
|
|
last_mouse_position = {};
|
|
|
|
wheel_position = {};
|
|
|
|
last_mouse_change = {};
|
|
|
|
last_motion_change = {};
|
|
|
|
|
2021-09-21 00:19:55 +02:00
|
|
|
update_thread = std::jthread([this](std::stop_token stop_token) { UpdateThread(stop_token); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::UpdateThread(std::stop_token stop_token) {
|
2022-10-04 00:43:56 +02:00
|
|
|
Common::SetCurrentThreadName("Mouse");
|
2021-09-21 00:19:55 +02:00
|
|
|
|
2023-02-22 04:46:42 +01:00
|
|
|
while (!stop_token.stop_requested()) {
|
|
|
|
UpdateStickInput();
|
|
|
|
UpdateMotionInput();
|
2022-01-17 01:03:11 +01:00
|
|
|
|
2021-09-21 00:19:55 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(update_time));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 04:46:42 +01:00
|
|
|
void Mouse::UpdateStickInput() {
|
|
|
|
if (!Settings::values.mouse_panning) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-29 14:51:56 +02:00
|
|
|
const float length = last_mouse_change.Length();
|
2023-02-22 04:46:42 +01:00
|
|
|
|
2023-05-29 14:51:56 +02:00
|
|
|
// Prevent input from exceeding the max range (1.0f) too much,
|
|
|
|
// but allow some room to make it easier to sustain
|
|
|
|
if (length > 1.2f) {
|
|
|
|
last_mouse_change /= length;
|
|
|
|
last_mouse_change *= 1.2f;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto mouse_change = last_mouse_change;
|
|
|
|
|
|
|
|
// Bind the mouse change to [0 <= deadzone_counterweight <= 1,1]
|
|
|
|
if (length < 1.0f) {
|
|
|
|
const float deadzone_h_counterweight =
|
|
|
|
Settings::values.mouse_panning_deadzone_x_counterweight.GetValue();
|
|
|
|
const float deadzone_v_counterweight =
|
|
|
|
Settings::values.mouse_panning_deadzone_y_counterweight.GetValue();
|
|
|
|
mouse_change /= length;
|
|
|
|
mouse_change.x *= length + (1 - length) * deadzone_h_counterweight * 0.01f;
|
|
|
|
mouse_change.y *= length + (1 - length) * deadzone_v_counterweight * 0.01f;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetAxis(identifier, mouse_axis_x, mouse_change.x);
|
|
|
|
SetAxis(identifier, mouse_axis_y, -mouse_change.y);
|
|
|
|
|
|
|
|
// Decay input over time
|
|
|
|
const float clamped_length = std::min(1.0f, length);
|
|
|
|
const float decay_strength = Settings::values.mouse_panning_decay_strength.GetValue();
|
|
|
|
const float decay = 1 - clamped_length * clamped_length * decay_strength * 0.01f;
|
|
|
|
const float min_decay = Settings::values.mouse_panning_min_decay.GetValue();
|
|
|
|
const float clamped_decay = std::min(1 - min_decay / 100.0f, decay);
|
|
|
|
last_mouse_change *= clamped_decay;
|
2023-02-22 04:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::UpdateMotionInput() {
|
2023-05-29 14:51:56 +02:00
|
|
|
// This may need its own sensitivity instead of using the average
|
|
|
|
const float sensitivity = (Settings::values.mouse_panning_x_sensitivity.GetValue() +
|
|
|
|
Settings::values.mouse_panning_y_sensitivity.GetValue()) /
|
|
|
|
2.0f * default_motion_sensitivity;
|
2023-02-22 04:46:42 +01:00
|
|
|
|
2023-03-05 18:00:00 +01:00
|
|
|
const float rotation_velocity = std::sqrt(last_motion_change.x * last_motion_change.x +
|
|
|
|
last_motion_change.y * last_motion_change.y);
|
|
|
|
|
|
|
|
if (rotation_velocity > maximum_rotation_speed / sensitivity) {
|
|
|
|
const float multiplier = maximum_rotation_speed / rotation_velocity / sensitivity;
|
|
|
|
last_motion_change.x = last_motion_change.x * multiplier;
|
|
|
|
last_motion_change.y = last_motion_change.y * multiplier;
|
2023-02-22 04:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const BasicMotion motion_data{
|
|
|
|
.gyro_x = last_motion_change.x * sensitivity,
|
|
|
|
.gyro_y = last_motion_change.y * sensitivity,
|
|
|
|
.gyro_z = last_motion_change.z * sensitivity,
|
|
|
|
.accel_x = 0,
|
|
|
|
.accel_y = 0,
|
|
|
|
.accel_z = 0,
|
|
|
|
.delta_timestamp = update_time * 1000,
|
|
|
|
};
|
|
|
|
|
2023-03-05 18:00:00 +01:00
|
|
|
if (Settings::values.mouse_panning) {
|
|
|
|
last_motion_change.x = 0;
|
|
|
|
last_motion_change.y = 0;
|
|
|
|
}
|
|
|
|
last_motion_change.z = 0;
|
|
|
|
|
2023-02-22 04:46:42 +01:00
|
|
|
SetMotion(motion_identifier, 0, motion_data);
|
|
|
|
}
|
|
|
|
|
2023-02-16 20:38:50 +01:00
|
|
|
void Mouse::Move(int x, int y, int center_x, int center_y) {
|
2021-09-21 00:19:55 +02:00
|
|
|
if (Settings::values.mouse_panning) {
|
2023-05-29 14:51:56 +02:00
|
|
|
const auto mouse_change =
|
2021-09-21 00:19:55 +02:00
|
|
|
(Common::MakeVec(x, y) - Common::MakeVec(center_x, center_y)).Cast<float>();
|
2023-05-29 14:51:56 +02:00
|
|
|
const float x_sensitivity =
|
|
|
|
Settings::values.mouse_panning_x_sensitivity.GetValue() * default_stick_sensitivity;
|
|
|
|
const float y_sensitivity =
|
|
|
|
Settings::values.mouse_panning_y_sensitivity.GetValue() * default_stick_sensitivity;
|
2021-09-21 00:19:55 +02:00
|
|
|
|
2023-05-29 14:51:56 +02:00
|
|
|
last_motion_change += {-mouse_change.y, -mouse_change.x, 0};
|
|
|
|
last_mouse_change.x += mouse_change.x * x_sensitivity * 0.09f;
|
|
|
|
last_mouse_change.y += mouse_change.y * y_sensitivity * 0.09f;
|
2021-09-21 00:19:55 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (button_pressed) {
|
|
|
|
const auto mouse_move = Common::MakeVec<int>(x, y) - mouse_origin;
|
2023-05-29 14:51:56 +02:00
|
|
|
const float x_sensitivity = Settings::values.mouse_panning_x_sensitivity.GetValue();
|
|
|
|
const float y_sensitivity = Settings::values.mouse_panning_y_sensitivity.GetValue();
|
|
|
|
SetAxis(identifier, mouse_axis_x,
|
|
|
|
static_cast<float>(mouse_move.x) * x_sensitivity * 0.0012f);
|
|
|
|
SetAxis(identifier, mouse_axis_y,
|
|
|
|
static_cast<float>(-mouse_move.y) * y_sensitivity * 0.0012f);
|
2023-02-22 04:46:42 +01:00
|
|
|
|
|
|
|
last_motion_change = {
|
|
|
|
static_cast<float>(-mouse_move.y) / 50.0f,
|
|
|
|
static_cast<float>(-mouse_move.x) / 50.0f,
|
|
|
|
last_motion_change.z,
|
|
|
|
};
|
2021-09-21 00:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-16 20:38:50 +01:00
|
|
|
void Mouse::MouseMove(f32 touch_x, f32 touch_y) {
|
|
|
|
SetAxis(real_mouse_identifier, mouse_axis_x, touch_x);
|
|
|
|
SetAxis(real_mouse_identifier, mouse_axis_y, touch_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::TouchMove(f32 touch_x, f32 touch_y) {
|
|
|
|
SetAxis(touch_identifier, mouse_axis_x, touch_x);
|
|
|
|
SetAxis(touch_identifier, mouse_axis_y, touch_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::PressButton(int x, int y, MouseButton button) {
|
2021-09-21 00:19:55 +02:00
|
|
|
SetButton(identifier, static_cast<int>(button), true);
|
2023-02-16 20:38:50 +01:00
|
|
|
|
2021-09-21 00:19:55 +02:00
|
|
|
// Set initial analog parameters
|
|
|
|
mouse_origin = {x, y};
|
|
|
|
last_mouse_position = {x, y};
|
|
|
|
button_pressed = true;
|
|
|
|
}
|
|
|
|
|
2023-02-16 20:38:50 +01:00
|
|
|
void Mouse::PressMouseButton(MouseButton button) {
|
|
|
|
SetButton(real_mouse_identifier, static_cast<int>(button), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mouse::PressTouchButton(f32 touch_x, f32 touch_y, MouseButton button) {
|
|
|
|
SetAxis(touch_identifier, mouse_axis_x, touch_x);
|
|
|
|
SetAxis(touch_identifier, mouse_axis_y, touch_y);
|
|
|
|
SetButton(touch_identifier, static_cast<int>(button), true);
|
|
|
|
}
|
|
|
|
|
2021-09-21 00:19:55 +02:00
|
|
|
void Mouse::ReleaseButton(MouseButton button) {
|
|
|
|
SetButton(identifier, static_cast<int>(button), false);
|
2023-02-16 20:38:50 +01:00
|
|
|
SetButton(real_mouse_identifier, static_cast<int>(button), false);
|
|
|
|
SetButton(touch_identifier, static_cast<int>(button), false);
|
2021-09-21 00:19:55 +02:00
|
|
|
|
2023-02-16 20:38:50 +01:00
|
|
|
if (!Settings::values.mouse_panning) {
|
2021-11-14 21:09:29 +01:00
|
|
|
SetAxis(identifier, mouse_axis_x, 0);
|
|
|
|
SetAxis(identifier, mouse_axis_y, 0);
|
2021-09-21 00:19:55 +02:00
|
|
|
}
|
2023-02-22 04:46:42 +01:00
|
|
|
|
|
|
|
last_motion_change.x = 0;
|
|
|
|
last_motion_change.y = 0;
|
|
|
|
|
2021-09-21 00:19:55 +02:00
|
|
|
button_pressed = false;
|
|
|
|
}
|
|
|
|
|
2021-11-14 21:09:29 +01:00
|
|
|
void Mouse::MouseWheelChange(int x, int y) {
|
2021-11-15 04:28:38 +01:00
|
|
|
wheel_position.x += x;
|
|
|
|
wheel_position.y += y;
|
2023-02-22 04:46:42 +01:00
|
|
|
last_motion_change.z += static_cast<f32>(y) / 100.0f;
|
2021-11-15 04:28:38 +01:00
|
|
|
SetAxis(identifier, wheel_axis_x, static_cast<f32>(wheel_position.x));
|
|
|
|
SetAxis(identifier, wheel_axis_y, static_cast<f32>(wheel_position.y));
|
2021-11-14 21:09:29 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 00:19:55 +02:00
|
|
|
void Mouse::ReleaseAllButtons() {
|
|
|
|
ResetButtonState();
|
|
|
|
button_pressed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Common::ParamPackage> Mouse::GetInputDevices() const {
|
|
|
|
std::vector<Common::ParamPackage> devices;
|
|
|
|
devices.emplace_back(Common::ParamPackage{
|
2021-10-24 18:22:20 +02:00
|
|
|
{"engine", GetEngineName()},
|
2021-09-21 00:19:55 +02:00
|
|
|
{"display", "Keyboard/Mouse"},
|
|
|
|
});
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
2021-10-24 18:22:20 +02:00
|
|
|
AnalogMapping Mouse::GetAnalogMappingForDevice(
|
|
|
|
[[maybe_unused]] const Common::ParamPackage& params) {
|
|
|
|
// Only overwrite different buttons from default
|
|
|
|
AnalogMapping mapping = {};
|
|
|
|
Common::ParamPackage right_analog_params;
|
|
|
|
right_analog_params.Set("engine", GetEngineName());
|
|
|
|
right_analog_params.Set("axis_x", 0);
|
|
|
|
right_analog_params.Set("axis_y", 1);
|
|
|
|
right_analog_params.Set("threshold", 0.5f);
|
|
|
|
right_analog_params.Set("range", 1.0f);
|
|
|
|
right_analog_params.Set("deadzone", 0.0f);
|
|
|
|
mapping.insert_or_assign(Settings::NativeAnalog::RStick, std::move(right_analog_params));
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
|
2022-01-17 01:03:11 +01:00
|
|
|
Common::Input::ButtonNames Mouse::GetUIButtonName(const Common::ParamPackage& params) const {
|
|
|
|
const auto button = static_cast<MouseButton>(params.Get("button", 0));
|
|
|
|
switch (button) {
|
|
|
|
case MouseButton::Left:
|
|
|
|
return Common::Input::ButtonNames::ButtonLeft;
|
|
|
|
case MouseButton::Right:
|
|
|
|
return Common::Input::ButtonNames::ButtonRight;
|
|
|
|
case MouseButton::Wheel:
|
|
|
|
return Common::Input::ButtonNames::ButtonMouseWheel;
|
|
|
|
case MouseButton::Backward:
|
|
|
|
return Common::Input::ButtonNames::ButtonBackward;
|
|
|
|
case MouseButton::Forward:
|
|
|
|
return Common::Input::ButtonNames::ButtonForward;
|
|
|
|
case MouseButton::Task:
|
|
|
|
return Common::Input::ButtonNames::ButtonTask;
|
|
|
|
case MouseButton::Extra:
|
|
|
|
return Common::Input::ButtonNames::ButtonExtra;
|
|
|
|
case MouseButton::Undefined:
|
|
|
|
default:
|
|
|
|
return Common::Input::ButtonNames::Undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 21:12:01 +01:00
|
|
|
Common::Input::ButtonNames Mouse::GetUIName(const Common::ParamPackage& params) const {
|
2021-09-21 00:19:55 +02:00
|
|
|
if (params.Has("button")) {
|
2022-01-17 01:03:11 +01:00
|
|
|
return GetUIButtonName(params);
|
2021-11-21 21:12:01 +01:00
|
|
|
}
|
|
|
|
if (params.Has("axis")) {
|
|
|
|
return Common::Input::ButtonNames::Value;
|
2021-09-21 00:19:55 +02:00
|
|
|
}
|
2022-01-17 01:03:11 +01:00
|
|
|
if (params.Has("axis_x") && params.Has("axis_y") && params.Has("axis_z")) {
|
|
|
|
return Common::Input::ButtonNames::Engine;
|
|
|
|
}
|
2023-02-22 04:46:42 +01:00
|
|
|
if (params.Has("motion")) {
|
|
|
|
return Common::Input::ButtonNames::Engine;
|
|
|
|
}
|
2021-09-21 00:19:55 +02:00
|
|
|
|
2021-11-21 21:12:01 +01:00
|
|
|
return Common::Input::ButtonNames::Invalid;
|
2021-09-21 00:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace InputCommon
|