summaryrefslogtreecommitdiff
path: root/src/input_common/motion_emu.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/motion_emu.cpp')
-rw-r--r--src/input_common/motion_emu.cpp178
1 files changed, 0 insertions, 178 deletions
diff --git a/src/input_common/motion_emu.cpp b/src/input_common/motion_emu.cpp
deleted file mode 100644
index 69fd3c1d2..000000000
--- a/src/input_common/motion_emu.cpp
+++ /dev/null
@@ -1,178 +0,0 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <algorithm>
6#include <chrono>
7#include <mutex>
8#include <thread>
9#include <tuple>
10#include "common/math_util.h"
11#include "common/quaternion.h"
12#include "common/thread.h"
13#include "common/vector_math.h"
14#include "input_common/motion_emu.h"
15
16namespace InputCommon {
17
18// Implementation class of the motion emulation device
19class MotionEmuDevice {
20public:
21 MotionEmuDevice(int update_millisecond, float sensitivity)
22 : update_millisecond(update_millisecond),
23 update_duration(std::chrono::duration_cast<std::chrono::steady_clock::duration>(
24 std::chrono::milliseconds(update_millisecond))),
25 sensitivity(sensitivity), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {}
26
27 ~MotionEmuDevice() {
28 if (motion_emu_thread.joinable()) {
29 shutdown_event.Set();
30 motion_emu_thread.join();
31 }
32 }
33
34 void BeginTilt(int x, int y) {
35 mouse_origin = Common::MakeVec(x, y);
36 is_tilting = true;
37 }
38
39 void Tilt(int x, int y) {
40 auto mouse_move = Common::MakeVec(x, y) - mouse_origin;
41 if (is_tilting) {
42 std::lock_guard guard{tilt_mutex};
43 if (mouse_move.x == 0 && mouse_move.y == 0) {
44 tilt_angle = 0;
45 } else {
46 tilt_direction = mouse_move.Cast<float>();
47 tilt_angle =
48 std::clamp(tilt_direction.Normalize() * sensitivity, 0.0f, Common::PI * 0.5f);
49 }
50 }
51 }
52
53 void EndTilt() {
54 std::lock_guard guard{tilt_mutex};
55 tilt_angle = 0;
56 is_tilting = false;
57 }
58
59 Input::MotionStatus GetStatus() {
60 std::lock_guard guard{status_mutex};
61 return status;
62 }
63
64private:
65 const int update_millisecond;
66 const std::chrono::steady_clock::duration update_duration;
67 const float sensitivity;
68
69 Common::Vec2<int> mouse_origin;
70
71 std::mutex tilt_mutex;
72 Common::Vec2<float> tilt_direction;
73 float tilt_angle = 0;
74
75 bool is_tilting = false;
76
77 Common::Event shutdown_event;
78
79 Input::MotionStatus status;
80 std::mutex status_mutex;
81
82 // Note: always keep the thread declaration at the end so that other objects are initialized
83 // before this!
84 std::thread motion_emu_thread;
85
86 void MotionEmuThread() {
87 auto update_time = std::chrono::steady_clock::now();
88 Common::Quaternion<float> q = Common::MakeQuaternion(Common::Vec3<float>(), 0);
89 Common::Quaternion<float> old_q;
90
91 while (!shutdown_event.WaitUntil(update_time)) {
92 update_time += update_duration;
93 old_q = q;
94
95 {
96 std::lock_guard guard{tilt_mutex};
97
98 // Find the quaternion describing current 3DS tilting
99 q = Common::MakeQuaternion(
100 Common::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), tilt_angle);
101 }
102
103 auto inv_q = q.Inverse();
104
105 // Set the gravity vector in world space
106 auto gravity = Common::MakeVec(0.0f, -1.0f, 0.0f);
107
108 // Find the angular rate vector in world space
109 auto angular_rate = ((q - old_q) * inv_q).xyz * 2;
110 angular_rate *= 1000 / update_millisecond / Common::PI * 180;
111
112 // Transform the two vectors from world space to 3DS space
113 gravity = QuaternionRotate(inv_q, gravity);
114 angular_rate = QuaternionRotate(inv_q, angular_rate);
115
116 // TODO: Calculate the correct rotation vector and orientation matrix
117 const auto matrix4x4 = q.ToMatrix();
118 const auto rotation = Common::MakeVec(0.0f, 0.0f, 0.0f);
119 const std::array orientation{
120 Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
121 Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
122 Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10]),
123 };
124
125 // Update the sensor state
126 {
127 std::lock_guard guard{status_mutex};
128 status = std::make_tuple(gravity, angular_rate, rotation, orientation);
129 }
130 }
131 }
132};
133
134// Interface wrapper held by input receiver as a unique_ptr. It holds the implementation class as
135// a shared_ptr, which is also observed by the factory class as a weak_ptr. In this way the factory
136// can forward all the inputs to the implementation only when it is valid.
137class MotionEmuDeviceWrapper : public Input::MotionDevice {
138public:
139 MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) {
140 device = std::make_shared<MotionEmuDevice>(update_millisecond, sensitivity);
141 }
142
143 Input::MotionStatus GetStatus() const override {
144 return device->GetStatus();
145 }
146
147 std::shared_ptr<MotionEmuDevice> device;
148};
149
150std::unique_ptr<Input::MotionDevice> MotionEmu::Create(const Common::ParamPackage& params) {
151 int update_period = params.Get("update_period", 100);
152 float sensitivity = params.Get("sensitivity", 0.01f);
153 auto device_wrapper = std::make_unique<MotionEmuDeviceWrapper>(update_period, sensitivity);
154 // Previously created device is disconnected here. Having two motion devices for 3DS is not
155 // expected.
156 current_device = device_wrapper->device;
157 return device_wrapper;
158}
159
160void MotionEmu::BeginTilt(int x, int y) {
161 if (auto ptr = current_device.lock()) {
162 ptr->BeginTilt(x, y);
163 }
164}
165
166void MotionEmu::Tilt(int x, int y) {
167 if (auto ptr = current_device.lock()) {
168 ptr->Tilt(x, y);
169 }
170}
171
172void MotionEmu::EndTilt() {
173 if (auto ptr = current_device.lock()) {
174 ptr->EndTilt();
175 }
176}
177
178} // namespace InputCommon