summaryrefslogtreecommitdiff
path: root/src/input_common
diff options
context:
space:
mode:
authorGravatar bunnei2017-10-09 23:56:20 -0400
committerGravatar bunnei2017-10-09 23:56:20 -0400
commitb1d5db1cf60344b6b081c9d03cb6ccc3264326cd (patch)
treefde377c4ba3c0f92c032e6f5ec8627aae37270ef /src/input_common
parentloader: Various improvements for NSO/NRO loaders. (diff)
parentMerge pull request #2996 from MerryMage/split-travis (diff)
downloadyuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.tar.gz
yuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.tar.xz
yuzu-b1d5db1cf60344b6b081c9d03cb6ccc3264326cd.zip
Merge remote-tracking branch 'upstream/master' into nx
# Conflicts: # src/core/CMakeLists.txt # src/core/arm/dynarmic/arm_dynarmic.cpp # src/core/arm/dyncom/arm_dyncom.cpp # src/core/hle/kernel/process.cpp # src/core/hle/kernel/thread.cpp # src/core/hle/kernel/thread.h # src/core/hle/kernel/vm_manager.cpp # src/core/loader/3dsx.cpp # src/core/loader/elf.cpp # src/core/loader/ncch.cpp # src/core/memory.cpp # src/core/memory.h # src/core/memory_setup.h
Diffstat (limited to 'src/input_common')
-rw-r--r--src/input_common/CMakeLists.txt2
-rw-r--r--src/input_common/main.cpp15
-rw-r--r--src/input_common/main.h7
-rw-r--r--src/input_common/motion_emu.cpp168
-rw-r--r--src/input_common/motion_emu.h46
-rw-r--r--src/input_common/sdl/sdl.cpp2
6 files changed, 236 insertions, 4 deletions
diff --git a/src/input_common/CMakeLists.txt b/src/input_common/CMakeLists.txt
index e3e36ada7..92792a702 100644
--- a/src/input_common/CMakeLists.txt
+++ b/src/input_common/CMakeLists.txt
@@ -2,12 +2,14 @@ set(SRCS
2 analog_from_button.cpp 2 analog_from_button.cpp
3 keyboard.cpp 3 keyboard.cpp
4 main.cpp 4 main.cpp
5 motion_emu.cpp
5 ) 6 )
6 7
7set(HEADERS 8set(HEADERS
8 analog_from_button.h 9 analog_from_button.h
9 keyboard.h 10 keyboard.h
10 main.h 11 main.h
12 motion_emu.h
11 ) 13 )
12 14
13if(SDL2_FOUND) 15if(SDL2_FOUND)
diff --git a/src/input_common/main.cpp b/src/input_common/main.cpp
index 699f41e6b..557353740 100644
--- a/src/input_common/main.cpp
+++ b/src/input_common/main.cpp
@@ -7,6 +7,7 @@
7#include "input_common/analog_from_button.h" 7#include "input_common/analog_from_button.h"
8#include "input_common/keyboard.h" 8#include "input_common/keyboard.h"
9#include "input_common/main.h" 9#include "input_common/main.h"
10#include "input_common/motion_emu.h"
10#ifdef HAVE_SDL2 11#ifdef HAVE_SDL2
11#include "input_common/sdl/sdl.h" 12#include "input_common/sdl/sdl.h"
12#endif 13#endif
@@ -14,12 +15,16 @@
14namespace InputCommon { 15namespace InputCommon {
15 16
16static std::shared_ptr<Keyboard> keyboard; 17static std::shared_ptr<Keyboard> keyboard;
18static std::shared_ptr<MotionEmu> motion_emu;
17 19
18void Init() { 20void Init() {
19 keyboard = std::make_shared<InputCommon::Keyboard>(); 21 keyboard = std::make_shared<Keyboard>();
20 Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard); 22 Input::RegisterFactory<Input::ButtonDevice>("keyboard", keyboard);
21 Input::RegisterFactory<Input::AnalogDevice>("analog_from_button", 23 Input::RegisterFactory<Input::AnalogDevice>("analog_from_button",
22 std::make_shared<InputCommon::AnalogFromButton>()); 24 std::make_shared<AnalogFromButton>());
25 motion_emu = std::make_shared<MotionEmu>();
26 Input::RegisterFactory<Input::MotionDevice>("motion_emu", motion_emu);
27
23#ifdef HAVE_SDL2 28#ifdef HAVE_SDL2
24 SDL::Init(); 29 SDL::Init();
25#endif 30#endif
@@ -29,6 +34,8 @@ void Shutdown() {
29 Input::UnregisterFactory<Input::ButtonDevice>("keyboard"); 34 Input::UnregisterFactory<Input::ButtonDevice>("keyboard");
30 keyboard.reset(); 35 keyboard.reset();
31 Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button"); 36 Input::UnregisterFactory<Input::AnalogDevice>("analog_from_button");
37 Input::UnregisterFactory<Input::MotionDevice>("motion_emu");
38 motion_emu.reset();
32 39
33#ifdef HAVE_SDL2 40#ifdef HAVE_SDL2
34 SDL::Shutdown(); 41 SDL::Shutdown();
@@ -39,6 +46,10 @@ Keyboard* GetKeyboard() {
39 return keyboard.get(); 46 return keyboard.get();
40} 47}
41 48
49MotionEmu* GetMotionEmu() {
50 return motion_emu.get();
51}
52
42std::string GenerateKeyboardParam(int key_code) { 53std::string GenerateKeyboardParam(int key_code) {
43 Common::ParamPackage param{ 54 Common::ParamPackage param{
44 {"engine", "keyboard"}, {"code", std::to_string(key_code)}, 55 {"engine", "keyboard"}, {"code", std::to_string(key_code)},
diff --git a/src/input_common/main.h b/src/input_common/main.h
index 140bbd014..5604f0fa8 100644
--- a/src/input_common/main.h
+++ b/src/input_common/main.h
@@ -11,7 +11,7 @@ namespace InputCommon {
11/// Initializes and registers all built-in input device factories. 11/// Initializes and registers all built-in input device factories.
12void Init(); 12void Init();
13 13
14/// Unresisters all build-in input device factories and shut them down. 14/// Deregisters all built-in input device factories and shuts them down.
15void Shutdown(); 15void Shutdown();
16 16
17class Keyboard; 17class Keyboard;
@@ -19,6 +19,11 @@ class Keyboard;
19/// Gets the keyboard button device factory. 19/// Gets the keyboard button device factory.
20Keyboard* GetKeyboard(); 20Keyboard* GetKeyboard();
21 21
22class MotionEmu;
23
24/// Gets the motion emulation factory.
25MotionEmu* GetMotionEmu();
26
22/// Generates a serialized param package for creating a keyboard button device 27/// Generates a serialized param package for creating a keyboard button device
23std::string GenerateKeyboardParam(int key_code); 28std::string GenerateKeyboardParam(int key_code);
24 29
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
diff --git a/src/input_common/motion_emu.h b/src/input_common/motion_emu.h
new file mode 100644
index 000000000..7a7e22467
--- /dev/null
+++ b/src/input_common/motion_emu.h
@@ -0,0 +1,46 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include "core/frontend/input.h"
8
9namespace InputCommon {
10
11class MotionEmuDevice;
12
13class MotionEmu : public Input::Factory<Input::MotionDevice> {
14public:
15 /**
16 * Creates a motion device emulated from mouse input
17 * @param params contains parameters for creating the device:
18 * - "update_period": update period in milliseconds
19 * - "sensitivity": the coefficient converting mouse movement to tilting angle
20 */
21 std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override;
22
23 /**
24 * Signals that a motion sensor tilt has begun.
25 * @param x the x-coordinate of the cursor
26 * @param y the y-coordinate of the cursor
27 */
28 void BeginTilt(int x, int y);
29
30 /**
31 * Signals that a motion sensor tilt is occurring.
32 * @param x the x-coordinate of the cursor
33 * @param y the y-coordinate of the cursor
34 */
35 void Tilt(int x, int y);
36
37 /**
38 * Signals that a motion sensor tilt has ended.
39 */
40 void EndTilt();
41
42private:
43 std::weak_ptr<MotionEmuDevice> current_device;
44};
45
46} // namespace InputCommon
diff --git a/src/input_common/sdl/sdl.cpp b/src/input_common/sdl/sdl.cpp
index 756ee58b7..d404afa89 100644
--- a/src/input_common/sdl/sdl.cpp
+++ b/src/input_common/sdl/sdl.cpp
@@ -159,7 +159,7 @@ public:
159 * - "axis"(optional): the index of the axis to bind 159 * - "axis"(optional): the index of the axis to bind
160 * - "direction"(only used for hat): the direction name of the hat to bind. Can be "up", 160 * - "direction"(only used for hat): the direction name of the hat to bind. Can be "up",
161 * "down", "left" or "right" 161 * "down", "left" or "right"
162 * - "threshould"(only used for axis): a float value in (-1.0, 1.0) which the button is 162 * - "threshold"(only used for axis): a float value in (-1.0, 1.0) which the button is
163 * triggered if the axis value crosses 163 * triggered if the axis value crosses
164 * - "direction"(only used for axis): "+" means the button is triggered when the axis value 164 * - "direction"(only used for axis): "+" means the button is triggered when the axis value
165 * is greater than the threshold; "-" means the button is triggered when the axis value 165 * is greater than the threshold; "-" means the button is triggered when the axis value