2017-08-06 23:04:06 +02:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-04-20 05:01:50 +02:00
|
|
|
#include <algorithm>
|
2017-08-09 02:05:03 +02:00
|
|
|
#include <chrono>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <tuple>
|
2017-08-06 23:04:06 +02:00
|
|
|
#include "common/math_util.h"
|
|
|
|
#include "common/quaternion.h"
|
2017-08-09 02:05:03 +02:00
|
|
|
#include "common/thread.h"
|
|
|
|
#include "common/vector_math.h"
|
2017-08-06 23:04:06 +02:00
|
|
|
#include "input_common/motion_emu.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
|
|
|
// Implementation class of the motion emulation device
|
|
|
|
class MotionEmuDevice {
|
|
|
|
public:
|
|
|
|
MotionEmuDevice(int update_millisecond, float sensitivity)
|
|
|
|
: update_millisecond(update_millisecond),
|
|
|
|
update_duration(std::chrono::duration_cast<std::chrono::steady_clock::duration>(
|
|
|
|
std::chrono::milliseconds(update_millisecond))),
|
|
|
|
sensitivity(sensitivity), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {}
|
|
|
|
|
|
|
|
~MotionEmuDevice() {
|
|
|
|
if (motion_emu_thread.joinable()) {
|
|
|
|
shutdown_event.Set();
|
|
|
|
motion_emu_thread.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BeginTilt(int x, int y) {
|
2019-02-27 04:38:34 +01:00
|
|
|
mouse_origin = Common::MakeVec(x, y);
|
2017-08-06 23:04:06 +02:00
|
|
|
is_tilting = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tilt(int x, int y) {
|
2019-02-27 04:38:34 +01:00
|
|
|
auto mouse_move = Common::MakeVec(x, y) - mouse_origin;
|
2017-08-06 23:04:06 +02:00
|
|
|
if (is_tilting) {
|
2019-04-01 18:29:59 +02:00
|
|
|
std::lock_guard guard{tilt_mutex};
|
2017-08-06 23:04:06 +02:00
|
|
|
if (mouse_move.x == 0 && mouse_move.y == 0) {
|
|
|
|
tilt_angle = 0;
|
|
|
|
} else {
|
|
|
|
tilt_direction = mouse_move.Cast<float>();
|
2018-04-20 05:01:50 +02:00
|
|
|
tilt_angle =
|
2019-02-27 04:47:49 +01:00
|
|
|
std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, Common::PI * 0.5f);
|
2017-08-06 23:04:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndTilt() {
|
2019-04-01 18:29:59 +02:00
|
|
|
std::lock_guard guard{tilt_mutex};
|
2017-08-06 23:04:06 +02:00
|
|
|
tilt_angle = 0;
|
|
|
|
is_tilting = false;
|
|
|
|
}
|
|
|
|
|
2019-02-27 04:38:34 +01:00
|
|
|
std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() {
|
2019-04-01 18:29:59 +02:00
|
|
|
std::lock_guard guard{status_mutex};
|
2017-08-06 23:04:06 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int update_millisecond;
|
|
|
|
const std::chrono::steady_clock::duration update_duration;
|
|
|
|
const float sensitivity;
|
|
|
|
|
2019-02-27 04:38:34 +01:00
|
|
|
Common::Vec2<int> mouse_origin;
|
2017-08-06 23:04:06 +02:00
|
|
|
|
|
|
|
std::mutex tilt_mutex;
|
2019-02-27 04:38:34 +01:00
|
|
|
Common::Vec2<float> tilt_direction;
|
2017-08-06 23:04:06 +02:00
|
|
|
float tilt_angle = 0;
|
|
|
|
|
|
|
|
bool is_tilting = false;
|
|
|
|
|
|
|
|
Common::Event shutdown_event;
|
|
|
|
|
2019-02-27 04:38:34 +01:00
|
|
|
std::tuple<Common::Vec3<float>, Common::Vec3<float>> status;
|
2017-08-06 23:04:06 +02:00
|
|
|
std::mutex status_mutex;
|
|
|
|
|
2017-08-22 10:43:44 +02:00
|
|
|
// Note: always keep the thread declaration at the end so that other objects are initialized
|
|
|
|
// before this!
|
|
|
|
std::thread motion_emu_thread;
|
|
|
|
|
2017-08-06 23:04:06 +02:00
|
|
|
void MotionEmuThread() {
|
|
|
|
auto update_time = std::chrono::steady_clock::now();
|
2019-02-27 04:38:34 +01:00
|
|
|
Common::Quaternion<float> q = Common::MakeQuaternion(Common::Vec3<float>(), 0);
|
2019-02-27 04:31:15 +01:00
|
|
|
Common::Quaternion<float> old_q;
|
2017-08-06 23:04:06 +02:00
|
|
|
|
|
|
|
while (!shutdown_event.WaitUntil(update_time)) {
|
|
|
|
update_time += update_duration;
|
|
|
|
old_q = q;
|
|
|
|
|
|
|
|
{
|
2019-04-01 18:29:59 +02:00
|
|
|
std::lock_guard guard{tilt_mutex};
|
2017-08-06 23:04:06 +02:00
|
|
|
|
|
|
|
// Find the quaternion describing current 3DS tilting
|
2019-02-27 04:38:34 +01:00
|
|
|
q = Common::MakeQuaternion(
|
|
|
|
Common::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), tilt_angle);
|
2017-08-06 23:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto inv_q = q.Inverse();
|
|
|
|
|
|
|
|
// Set the gravity vector in world space
|
2019-02-27 04:38:34 +01:00
|
|
|
auto gravity = Common::MakeVec(0.0f, -1.0f, 0.0f);
|
2017-08-06 23:04:06 +02:00
|
|
|
|
|
|
|
// Find the angular rate vector in world space
|
|
|
|
auto angular_rate = ((q - old_q) * inv_q).xyz * 2;
|
2019-02-27 04:47:49 +01:00
|
|
|
angular_rate *= 1000 / update_millisecond / Common::PI * 180;
|
2017-08-06 23:04:06 +02:00
|
|
|
|
|
|
|
// Transform the two vectors from world space to 3DS space
|
|
|
|
gravity = QuaternionRotate(inv_q, gravity);
|
|
|
|
angular_rate = QuaternionRotate(inv_q, angular_rate);
|
|
|
|
|
|
|
|
// Update the sensor state
|
|
|
|
{
|
2019-04-01 18:29:59 +02:00
|
|
|
std::lock_guard guard{status_mutex};
|
2017-08-06 23:04:06 +02:00
|
|
|
status = std::make_tuple(gravity, angular_rate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Interface wrapper held by input receiver as a unique_ptr. It holds the implementation class as
|
|
|
|
// a shared_ptr, which is also observed by the factory class as a weak_ptr. In this way the factory
|
|
|
|
// can forward all the inputs to the implementation only when it is valid.
|
|
|
|
class MotionEmuDeviceWrapper : public Input::MotionDevice {
|
|
|
|
public:
|
|
|
|
MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) {
|
|
|
|
device = std::make_shared<MotionEmuDevice>(update_millisecond, sensitivity);
|
|
|
|
}
|
|
|
|
|
2019-02-27 04:38:34 +01:00
|
|
|
std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override {
|
2017-08-06 23:04:06 +02:00
|
|
|
return device->GetStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<MotionEmuDevice> device;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Input::MotionDevice> MotionEmu::Create(const Common::ParamPackage& params) {
|
|
|
|
int update_period = params.Get("update_period", 100);
|
|
|
|
float sensitivity = params.Get("sensitivity", 0.01f);
|
|
|
|
auto device_wrapper = std::make_unique<MotionEmuDeviceWrapper>(update_period, sensitivity);
|
|
|
|
// Previously created device is disconnected here. Having two motion devices for 3DS is not
|
|
|
|
// expected.
|
|
|
|
current_device = device_wrapper->device;
|
|
|
|
return std::move(device_wrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionEmu::BeginTilt(int x, int y) {
|
|
|
|
if (auto ptr = current_device.lock()) {
|
|
|
|
ptr->BeginTilt(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionEmu::Tilt(int x, int y) {
|
|
|
|
if (auto ptr = current_device.lock()) {
|
|
|
|
ptr->Tilt(x, y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MotionEmu::EndTilt() {
|
|
|
|
if (auto ptr = current_device.lock()) {
|
|
|
|
ptr->EndTilt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace InputCommon
|