From 188194908c2785bd1e03485941b9148777cdddd7 Mon Sep 17 00:00:00 2001 From: wwylele Date: Mon, 7 Aug 2017 00:04:06 +0300 Subject: move MotionEmu from core/frontend to input_common as a InputDevice --- src/input_common/CMakeLists.txt | 2 + src/input_common/main.cpp | 15 +++- src/input_common/main.h | 5 ++ src/input_common/motion_emu.cpp | 159 ++++++++++++++++++++++++++++++++++++++++ src/input_common/motion_emu.h | 47 ++++++++++++ 5 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 src/input_common/motion_emu.cpp create mode 100644 src/input_common/motion_emu.h (limited to 'src/input_common') 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 analog_from_button.cpp keyboard.cpp main.cpp + motion_emu.cpp ) set(HEADERS analog_from_button.h keyboard.h main.h + motion_emu.h ) 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 @@ #include "input_common/analog_from_button.h" #include "input_common/keyboard.h" #include "input_common/main.h" +#include "input_common/motion_emu.h" #ifdef HAVE_SDL2 #include "input_common/sdl/sdl.h" #endif @@ -14,12 +15,16 @@ namespace InputCommon { static std::shared_ptr keyboard; +static std::shared_ptr motion_emu; void Init() { - keyboard = std::make_shared(); + keyboard = std::make_shared(); Input::RegisterFactory("keyboard", keyboard); Input::RegisterFactory("analog_from_button", - std::make_shared()); + std::make_shared()); + motion_emu = std::make_shared(); + Input::RegisterFactory("motion_emu", motion_emu); + #ifdef HAVE_SDL2 SDL::Init(); #endif @@ -29,6 +34,8 @@ void Shutdown() { Input::UnregisterFactory("keyboard"); keyboard.reset(); Input::UnregisterFactory("analog_from_button"); + Input::UnregisterFactory("motion_emu"); + motion_emu.reset(); #ifdef HAVE_SDL2 SDL::Shutdown(); @@ -39,6 +46,10 @@ Keyboard* GetKeyboard() { return keyboard.get(); } +MotionEmu* GetMotionEmu() { + return motion_emu.get(); +} + std::string GenerateKeyboardParam(int key_code) { Common::ParamPackage param{ {"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; /// Gets the keyboard button device factory. Keyboard* GetKeyboard(); +class MotionEmu; + +/// Gets the motion emulation factory. +MotionEmu* GetMotionEmu(); + /// Generates a serialized param package for creating a keyboard button device std::string GenerateKeyboardParam(int key_code); 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 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "common/math_util.h" +#include "common/quaternion.h" +#include "input_common/motion_emu.h" + +namespace InputCommon { + +// Implementation class of the motion emulation device +class MotionEmuDevice { +public: + MotionEmuDevice(int update_millisecond, float sensitivity) + : update_millisecond(update_millisecond), + update_duration(std::chrono::duration_cast( + std::chrono::milliseconds(update_millisecond))), + sensitivity(sensitivity), motion_emu_thread(&MotionEmuDevice::MotionEmuThread, this) {} + + ~MotionEmuDevice() { + if (motion_emu_thread.joinable()) { + shutdown_event.Set(); + motion_emu_thread.join(); + } + } + + void BeginTilt(int x, int y) { + mouse_origin = Math::MakeVec(x, y); + is_tilting = true; + } + + void Tilt(int x, int y) { + auto mouse_move = Math::MakeVec(x, y) - mouse_origin; + if (is_tilting) { + std::lock_guard guard(tilt_mutex); + if (mouse_move.x == 0 && mouse_move.y == 0) { + tilt_angle = 0; + } else { + tilt_direction = mouse_move.Cast(); + tilt_angle = MathUtil::Clamp(tilt_direction.Normalize() * sensitivity, 0.0f, + MathUtil::PI * 0.5f); + } + } + } + + void EndTilt() { + std::lock_guard guard(tilt_mutex); + tilt_angle = 0; + is_tilting = false; + } + + std::tuple, Math::Vec3> GetStatus() { + std::lock_guard guard(status_mutex); + return status; + } + +private: + const int update_millisecond; + const std::chrono::steady_clock::duration update_duration; + const float sensitivity; + + Math::Vec2 mouse_origin; + + std::mutex tilt_mutex; + Math::Vec2 tilt_direction; + float tilt_angle = 0; + + bool is_tilting = false; + + Common::Event shutdown_event; + std::thread motion_emu_thread; + + std::tuple, Math::Vec3> status; + std::mutex status_mutex; + + void MotionEmuThread() { + auto update_time = std::chrono::steady_clock::now(); + Math::Quaternion q = MakeQuaternion(Math::Vec3(), 0); + Math::Quaternion old_q; + + while (!shutdown_event.WaitUntil(update_time)) { + update_time += update_duration; + old_q = q; + + { + std::lock_guard guard(tilt_mutex); + + // Find the quaternion describing current 3DS tilting + q = MakeQuaternion(Math::MakeVec(-tilt_direction.y, 0.0f, tilt_direction.x), + tilt_angle); + } + + auto inv_q = q.Inverse(); + + // Set the gravity vector in world space + auto gravity = Math::MakeVec(0.0f, -1.0f, 0.0f); + + // Find the angular rate vector in world space + auto angular_rate = ((q - old_q) * inv_q).xyz * 2; + angular_rate *= 1000 / update_millisecond / MathUtil::PI * 180; + + // Transform the two vectors from world space to 3DS space + gravity = QuaternionRotate(inv_q, gravity); + angular_rate = QuaternionRotate(inv_q, angular_rate); + + // Update the sensor state + { + std::lock_guard guard(status_mutex); + status = std::make_tuple(gravity, angular_rate); + } + } + } +}; + +// Interface wrapper held by input receiver as a unique_ptr. It holds the implementation class as +// a shared_ptr, which is also observed by the factory class as a weak_ptr. In this way the factory +// can forward all the inputs to the implementation only when it is valid. +class MotionEmuDeviceWrapper : public Input::MotionDevice { +public: + MotionEmuDeviceWrapper(int update_millisecond, float sensitivity) { + device = std::make_shared(update_millisecond, sensitivity); + } + + std::tuple, Math::Vec3> GetStatus() const { + return device->GetStatus(); + } + + std::shared_ptr device; +}; + +std::unique_ptr MotionEmu::Create(const Common::ParamPackage& params) { + int update_period = params.Get("update_period", 100); + float sensitivity = params.Get("sensitivity", 0.01f); + auto device_wrapper = std::make_unique(update_period, sensitivity); + // Previously created device is disconnected here. Having two motion devices for 3DS is not + // expected. + current_device = device_wrapper->device; + return std::move(device_wrapper); +} + +void MotionEmu::BeginTilt(int x, int y) { + if (auto ptr = current_device.lock()) { + ptr->BeginTilt(x, y); + } +} + +void MotionEmu::Tilt(int x, int y) { + if (auto ptr = current_device.lock()) { + ptr->Tilt(x, y); + } +} + +void MotionEmu::EndTilt() { + if (auto ptr = current_device.lock()) { + ptr->EndTilt(); + } +} + +} // 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 @@ +// Copyright 2017 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once +#include "common/thread.h" +#include "common/vector_math.h" +#include "core/frontend/input.h" + +namespace InputCommon { + +class MotionEmuDevice; + +class MotionEmu : public Input::Factory { +public: + /** + * Creates a motion device emulated from mouse input + * @param params contains parameters for creating the device: + * - "update_period": update period in milliseconds + * - "sensitivity": the coefficient converting mouse movement to tilting angle + */ + std::unique_ptr Create(const Common::ParamPackage& params) override; + + /** + * Signals that a motion sensor tilt has begun. + * @param x the x-coordinate of the cursor + * @param y the y-coordinate of the cursor + */ + void BeginTilt(int x, int y); + + /** + * Signals that a motion sensor tilt is occurring. + * @param x the x-coordinate of the cursor + * @param y the y-coordinate of the cursor + */ + void Tilt(int x, int y); + + /** + * Signals that a motion sensor tilt has ended. + */ + void EndTilt(); + +private: + std::weak_ptr current_device; +}; + +} // namespace InputCommon -- cgit v1.2.3