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