diff options
| author | 2017-08-07 00:04:06 +0300 | |
|---|---|---|
| committer | 2017-08-11 11:05:08 +0300 | |
| commit | 188194908c2785bd1e03485941b9148777cdddd7 (patch) | |
| tree | ab6cd04195f5e18bd1e7dd21a7c2896066827a6f /src/input_common | |
| parent | HID: use MotionDevice for Accelerometer and Gyroscope (diff) | |
| download | yuzu-188194908c2785bd1e03485941b9148777cdddd7.tar.gz yuzu-188194908c2785bd1e03485941b9148777cdddd7.tar.xz yuzu-188194908c2785bd1e03485941b9148777cdddd7.zip | |
move MotionEmu from core/frontend to input_common as a InputDevice
Diffstat (limited to 'src/input_common')
| -rw-r--r-- | src/input_common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/input_common/main.cpp | 15 | ||||
| -rw-r--r-- | src/input_common/main.h | 5 | ||||
| -rw-r--r-- | src/input_common/motion_emu.cpp | 159 | ||||
| -rw-r--r-- | src/input_common/motion_emu.h | 47 |
5 files changed, 226 insertions, 2 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 | ||
| 7 | set(HEADERS | 8 | set(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 | ||
| 13 | if(SDL2_FOUND) | 15 | if(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 @@ | |||
| 14 | namespace InputCommon { | 15 | namespace InputCommon { |
| 15 | 16 | ||
| 16 | static std::shared_ptr<Keyboard> keyboard; | 17 | static std::shared_ptr<Keyboard> keyboard; |
| 18 | static std::shared_ptr<MotionEmu> motion_emu; | ||
| 17 | 19 | ||
| 18 | void Init() { | 20 | void 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 | ||
| 49 | MotionEmu* GetMotionEmu() { | ||
| 50 | return motion_emu.get(); | ||
| 51 | } | ||
| 52 | |||
| 42 | std::string GenerateKeyboardParam(int key_code) { | 53 | std::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..74283f7c6 100644 --- a/src/input_common/main.h +++ b/src/input_common/main.h | |||
| @@ -19,6 +19,11 @@ class Keyboard; | |||
| 19 | /// Gets the keyboard button device factory. | 19 | /// Gets the keyboard button device factory. |
| 20 | Keyboard* GetKeyboard(); | 20 | Keyboard* GetKeyboard(); |
| 21 | 21 | ||
| 22 | class MotionEmu; | ||
| 23 | |||
| 24 | /// Gets the motion emulation factory. | ||
| 25 | MotionEmu* 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 |
| 23 | std::string GenerateKeyboardParam(int key_code); | 28 | std::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..65e80529d --- /dev/null +++ b/src/input_common/motion_emu.cpp | |||
| @@ -0,0 +1,159 @@ | |||
| 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 "common/math_util.h" | ||
| 6 | #include "common/quaternion.h" | ||
| 7 | #include "input_common/motion_emu.h" | ||
| 8 | |||
| 9 | namespace InputCommon { | ||
| 10 | |||
| 11 | // Implementation class of the motion emulation device | ||
| 12 | class MotionEmuDevice { | ||
| 13 | public: | ||
| 14 | MotionEmuDevice(int update_millisecond, float sensitivity) | ||
| 15 | : update_millisecond(update_millisecond), | ||
| 16 | update_duration(std::chrono::duration_cast<std::chrono::steady_clock::duration>( | ||
| 17 | std::chrono::milliseconds(update_millisecond))), | ||
| 18 | sensitivity(sensitivity), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {} | ||
| 19 | |||
| 20 | ~MotionEmuDevice() { | ||
| 21 | if (motion_emu_thread.joinable()) { | ||
| 22 | shutdown_event.Set(); | ||
| 23 | motion_emu_thread.join(); | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | void BeginTilt(int x, int y) { | ||
| 28 | mouse_origin = Math::MakeVec(x, y); | ||
| 29 | is_tilting = true; | ||
| 30 | } | ||
| 31 | |||
| 32 | void Tilt(int x, int y) { | ||
| 33 | auto mouse_move = Math::MakeVec(x, y) - mouse_origin; | ||
| 34 | if (is_tilting) { | ||
| 35 | std::lock_guard<std::mutex> guard(tilt_mutex); | ||
| 36 | if (mouse_move.x == 0 && mouse_move.y == 0) { | ||
| 37 | tilt_angle = 0; | ||
| 38 | } else { | ||
| 39 | tilt_direction = mouse_move.Cast<float>(); | ||
| 40 | tilt_angle = MathUtil::Clamp(tilt_direction.Normalize() * sensitivity, 0.0f, | ||
| 41 | MathUtil::PI * 0.5f); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | void EndTilt() { | ||
| 47 | std::lock_guard<std::mutex> guard(tilt_mutex); | ||
| 48 | tilt_angle = 0; | ||
| 49 | is_tilting = false; | ||
| 50 | } | ||
| 51 | |||
| 52 | std::tuple<Math::Vec3<float>, Math::Vec3<float>> GetStatus() { | ||
| 53 | std::lock_guard<std::mutex> guard(status_mutex); | ||
| 54 | return status; | ||
| 55 | } | ||
| 56 | |||
| 57 | private: | ||
| 58 | const int update_millisecond; | ||
| 59 | const std::chrono::steady_clock::duration update_duration; | ||
| 60 | const float sensitivity; | ||
| 61 | |||
| 62 | Math::Vec2<int> mouse_origin; | ||
| 63 | |||
| 64 | std::mutex tilt_mutex; | ||
| 65 | Math::Vec2<float> tilt_direction; | ||
| 66 | float tilt_angle = 0; | ||
| 67 | |||
| 68 | bool is_tilting = false; | ||
| 69 | |||
| 70 | Common::Event shutdown_event; | ||
| 71 | std::thread motion_emu_thread; | ||
| 72 | |||
| 73 | std::tuple<Math::Vec3<float>, Math::Vec3<float>> status; | ||
| 74 | std::mutex status_mutex; | ||
| 75 | |||
| 76 | void MotionEmuThread() { | ||
| 77 | auto update_time = std::chrono::steady_clock::now(); | ||
| 78 | Math::Quaternion<float> q = MakeQuaternion(Math::Vec3<float>(), 0); | ||
| 79 | Math::Quaternion<float> old_q; | ||
| 80 | |||
| 81 | while (!shutdown_event.WaitUntil(update_time)) { | ||
| 82 | update_time += update_duration; | ||
| 83 | old_q = q; | ||
| 84 | |||
| 85 | { | ||
| 86 | std::lock_guard<std::mutex> guard(tilt_mutex); | ||
| 87 | |||
| 88 | // Find the quaternion describing current 3DS tilting | ||
| 89 | q = MakeQuaternion(Math::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), | ||
| 90 | tilt_angle); | ||
| 91 | } | ||
| 92 | |||
| 93 | auto inv_q = q.Inverse(); | ||
| 94 | |||
| 95 | // Set the gravity vector in world space | ||
| 96 | auto gravity = Math::MakeVec(0.0f, -1.0f, 0.0f); | ||
| 97 | |||
| 98 | // Find the angular rate vector in world space | ||
| 99 | auto angular_rate = ((q - old_q) * inv_q).xyz * 2; | ||
| 100 | angular_rate *= 1000 / update_millisecond / MathUtil::PI * 180; | ||
| 101 | |||
| 102 | // Transform the two vectors from world space to 3DS space | ||
| 103 | gravity = QuaternionRotate(inv_q, gravity); | ||
| 104 | angular_rate = QuaternionRotate(inv_q, angular_rate); | ||
| 105 | |||
| 106 | // Update the sensor state | ||
| 107 | { | ||
| 108 | std::lock_guard<std::mutex> guard(status_mutex); | ||
| 109 | status = std::make_tuple(gravity, angular_rate); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | // Interface wrapper held by input receiver as a unique_ptr. It holds the implementation class as | ||
| 116 | // a shared_ptr, which is also observed by the factory class as a weak_ptr. In this way the factory | ||
| 117 | // can forward all the inputs to the implementation only when it is valid. | ||
| 118 | class MotionEmuDeviceWrapper : public Input::MotionDevice { | ||
| 119 | public: | ||
| 120 | MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) { | ||
| 121 | device = std::make_shared<MotionEmuDevice>(update_millisecond, sensitivity); | ||
| 122 | } | ||
| 123 | |||
| 124 | std::tuple<Math::Vec3<float>, Math::Vec3<float>> GetStatus() const { | ||
| 125 | return device->GetStatus(); | ||
| 126 | } | ||
| 127 | |||
| 128 | std::shared_ptr<MotionEmuDevice> device; | ||
| 129 | }; | ||
| 130 | |||
| 131 | std::unique_ptr<Input::MotionDevice> MotionEmu::Create(const Common::ParamPackage& params) { | ||
| 132 | int update_period = params.Get("update_period", 100); | ||
| 133 | float sensitivity = params.Get("sensitivity", 0.01f); | ||
| 134 | auto device_wrapper = std::make_unique<MotionEmuDeviceWrapper>(update_period, sensitivity); | ||
| 135 | // Previously created device is disconnected here. Having two motion devices for 3DS is not | ||
| 136 | // expected. | ||
| 137 | current_device = device_wrapper->device; | ||
| 138 | return std::move(device_wrapper); | ||
| 139 | } | ||
| 140 | |||
| 141 | void MotionEmu::BeginTilt(int x, int y) { | ||
| 142 | if (auto ptr = current_device.lock()) { | ||
| 143 | ptr->BeginTilt(x, y); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | void MotionEmu::Tilt(int x, int y) { | ||
| 148 | if (auto ptr = current_device.lock()) { | ||
| 149 | ptr->Tilt(x, y); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | void MotionEmu::EndTilt() { | ||
| 154 | if (auto ptr = current_device.lock()) { | ||
| 155 | ptr->EndTilt(); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | } // namespace InputCommon | ||
diff --git a/src/input_common/motion_emu.h b/src/input_common/motion_emu.h new file mode 100644 index 000000000..195fb69d2 --- /dev/null +++ b/src/input_common/motion_emu.h | |||
| @@ -0,0 +1,47 @@ | |||
| 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 | #include "common/thread.h" | ||
| 7 | #include "common/vector_math.h" | ||
| 8 | #include "core/frontend/input.h" | ||
| 9 | |||
| 10 | namespace InputCommon { | ||
| 11 | |||
| 12 | class MotionEmuDevice; | ||
| 13 | |||
| 14 | class MotionEmu : public Input::Factory<Input::MotionDevice> { | ||
| 15 | public: | ||
| 16 | /** | ||
| 17 | * Creates a motion device emulated from mouse input | ||
| 18 | * @param params contains parameters for creating the device: | ||
| 19 | * - "update_period": update period in milliseconds | ||
| 20 | * - "sensitivity": the coefficient converting mouse movement to tilting angle | ||
| 21 | */ | ||
| 22 | std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override; | ||
| 23 | |||
| 24 | /** | ||
| 25 | * Signals that a motion sensor tilt has begun. | ||
| 26 | * @param x the x-coordinate of the cursor | ||
| 27 | * @param y the y-coordinate of the cursor | ||
| 28 | */ | ||
| 29 | void BeginTilt(int x, int y); | ||
| 30 | |||
| 31 | /** | ||
| 32 | * Signals that a motion sensor tilt is occurring. | ||
| 33 | * @param x the x-coordinate of the cursor | ||
| 34 | * @param y the y-coordinate of the cursor | ||
| 35 | */ | ||
| 36 | void Tilt(int x, int y); | ||
| 37 | |||
| 38 | /** | ||
| 39 | * Signals that a motion sensor tilt has ended. | ||
| 40 | */ | ||
| 41 | void EndTilt(); | ||
| 42 | |||
| 43 | private: | ||
| 44 | std::weak_ptr<MotionEmuDevice> current_device; | ||
| 45 | }; | ||
| 46 | |||
| 47 | } // namespace InputCommon | ||