diff options
Diffstat (limited to '')
| -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 | 165 | ||||
| -rw-r--r-- | src/input_common/motion_emu.h | 46 |
5 files changed, 231 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 e7fb3890e..5604f0fa8 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..a1761f184 --- /dev/null +++ b/src/input_common/motion_emu.cpp | |||
| @@ -0,0 +1,165 @@ | |||
| 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 | |||
| 15 | namespace InputCommon { | ||
| 16 | |||
| 17 | // Implementation class of the motion emulation device | ||
| 18 | class MotionEmuDevice { | ||
| 19 | public: | ||
| 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 | |||
| 63 | private: | ||
| 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 | std::thread motion_emu_thread; | ||
| 78 | |||
| 79 | std::tuple<Math::Vec3<float>, Math::Vec3<float>> status; | ||
| 80 | std::mutex status_mutex; | ||
| 81 | |||
| 82 | void MotionEmuThread() { | ||
| 83 | auto update_time = std::chrono::steady_clock::now(); | ||
| 84 | Math::Quaternion<float> q = MakeQuaternion(Math::Vec3<float>(), 0); | ||
| 85 | Math::Quaternion<float> old_q; | ||
| 86 | |||
| 87 | while (!shutdown_event.WaitUntil(update_time)) { | ||
| 88 | update_time += update_duration; | ||
| 89 | old_q = q; | ||
| 90 | |||
| 91 | { | ||
| 92 | std::lock_guard<std::mutex> guard(tilt_mutex); | ||
| 93 | |||
| 94 | // Find the quaternion describing current 3DS tilting | ||
| 95 | q = MakeQuaternion(Math::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), | ||
| 96 | tilt_angle); | ||
| 97 | } | ||
| 98 | |||
| 99 | auto inv_q = q.Inverse(); | ||
| 100 | |||
| 101 | // Set the gravity vector in world space | ||
| 102 | auto gravity = Math::MakeVec(0.0f, -1.0f, 0.0f); | ||
| 103 | |||
| 104 | // Find the angular rate vector in world space | ||
| 105 | auto angular_rate = ((q - old_q) * inv_q).xyz * 2; | ||
| 106 | angular_rate *= 1000 / update_millisecond / MathUtil::PI * 180; | ||
| 107 | |||
| 108 | // Transform the two vectors from world space to 3DS space | ||
| 109 | gravity = QuaternionRotate(inv_q, gravity); | ||
| 110 | angular_rate = QuaternionRotate(inv_q, angular_rate); | ||
| 111 | |||
| 112 | // Update the sensor state | ||
| 113 | { | ||
| 114 | std::lock_guard<std::mutex> guard(status_mutex); | ||
| 115 | status = std::make_tuple(gravity, angular_rate); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | }; | ||
| 120 | |||
| 121 | // Interface wrapper held by input receiver as a unique_ptr. It holds the implementation class as | ||
| 122 | // a shared_ptr, which is also observed by the factory class as a weak_ptr. In this way the factory | ||
| 123 | // can forward all the inputs to the implementation only when it is valid. | ||
| 124 | class MotionEmuDeviceWrapper : public Input::MotionDevice { | ||
| 125 | public: | ||
| 126 | MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) { | ||
| 127 | device = std::make_shared<MotionEmuDevice>(update_millisecond, sensitivity); | ||
| 128 | } | ||
| 129 | |||
| 130 | std::tuple<Math::Vec3<float>, Math::Vec3<float>> GetStatus() const { | ||
| 131 | return device->GetStatus(); | ||
| 132 | } | ||
| 133 | |||
| 134 | std::shared_ptr<MotionEmuDevice> device; | ||
| 135 | }; | ||
| 136 | |||
| 137 | std::unique_ptr<Input::MotionDevice> MotionEmu::Create(const Common::ParamPackage& params) { | ||
| 138 | int update_period = params.Get("update_period", 100); | ||
| 139 | float sensitivity = params.Get("sensitivity", 0.01f); | ||
| 140 | auto device_wrapper = std::make_unique<MotionEmuDeviceWrapper>(update_period, sensitivity); | ||
| 141 | // Previously created device is disconnected here. Having two motion devices for 3DS is not | ||
| 142 | // expected. | ||
| 143 | current_device = device_wrapper->device; | ||
| 144 | return std::move(device_wrapper); | ||
| 145 | } | ||
| 146 | |||
| 147 | void MotionEmu::BeginTilt(int x, int y) { | ||
| 148 | if (auto ptr = current_device.lock()) { | ||
| 149 | ptr->BeginTilt(x, y); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | void MotionEmu::Tilt(int x, int y) { | ||
| 154 | if (auto ptr = current_device.lock()) { | ||
| 155 | ptr->Tilt(x, y); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | void MotionEmu::EndTilt() { | ||
| 160 | if (auto ptr = current_device.lock()) { | ||
| 161 | ptr->EndTilt(); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | } // 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 | |||
| 9 | namespace InputCommon { | ||
| 10 | |||
| 11 | class MotionEmuDevice; | ||
| 12 | |||
| 13 | class MotionEmu : public Input::Factory<Input::MotionDevice> { | ||
| 14 | public: | ||
| 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 | |||
| 42 | private: | ||
| 43 | std::weak_ptr<MotionEmuDevice> current_device; | ||
| 44 | }; | ||
| 45 | |||
| 46 | } // namespace InputCommon | ||