2021-05-02 00:02:35 +02:00
|
|
|
// Copyright 2021 yuzu Emulator Project
|
2018-10-05 16:23:21 +02:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
2018-10-06 05:14:42 +02:00
|
|
|
|
2018-10-05 16:23:21 +02:00
|
|
|
#include <array>
|
2021-01-02 03:37:29 +01:00
|
|
|
#include "common/bit_field.h"
|
2018-10-05 16:23:21 +02:00
|
|
|
#include "common/common_types.h"
|
2021-05-28 13:14:14 +02:00
|
|
|
#include "common/point.h"
|
2018-10-05 16:23:21 +02:00
|
|
|
#include "core/hle/service/hid/controllers/controller_base.h"
|
2021-09-21 03:33:50 +02:00
|
|
|
#include "core/hle/service/hid/ring_lifo.h"
|
|
|
|
|
|
|
|
namespace Core::HID {
|
|
|
|
class EmulatedController;
|
|
|
|
} // namespace Core::HID
|
2018-10-05 16:23:21 +02:00
|
|
|
|
|
|
|
namespace Service::HID {
|
|
|
|
class Controller_Gesture final : public ControllerBase {
|
|
|
|
public:
|
2021-05-04 10:04:05 +02:00
|
|
|
explicit Controller_Gesture(Core::System& system_);
|
2018-10-18 02:34:25 +02:00
|
|
|
~Controller_Gesture() override;
|
2018-10-05 16:23:21 +02:00
|
|
|
|
|
|
|
// Called when the controller is initialized
|
2019-09-22 08:41:34 +02:00
|
|
|
void OnInit() override;
|
2018-10-05 16:23:21 +02:00
|
|
|
|
|
|
|
// When the controller is released
|
|
|
|
void OnRelease() override;
|
|
|
|
|
|
|
|
// When the controller is requesting an update for the shared memory
|
2019-02-14 18:42:58 +01:00
|
|
|
void OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, size_t size) override;
|
2018-10-05 16:23:21 +02:00
|
|
|
|
|
|
|
private:
|
2021-01-02 03:37:29 +01:00
|
|
|
static constexpr size_t MAX_FINGERS = 16;
|
|
|
|
static constexpr size_t MAX_POINTS = 4;
|
|
|
|
|
2021-09-21 03:33:50 +02:00
|
|
|
// This is nn::hid::GestureType
|
|
|
|
enum class GestureType : u32 {
|
2021-01-02 03:37:29 +01:00
|
|
|
Idle, // Nothing touching the screen
|
2021-05-02 00:02:35 +02:00
|
|
|
Complete, // Set at the end of a touch event
|
|
|
|
Cancel, // Set when the number of fingers change
|
|
|
|
Touch, // A finger just touched the screen
|
|
|
|
Press, // Set if last type is touch and the finger hasn't moved
|
2021-01-02 03:37:29 +01:00
|
|
|
Tap, // Fast press then release
|
|
|
|
Pan, // All points moving together across the screen
|
|
|
|
Swipe, // Fast press movement and release of a single point
|
|
|
|
Pinch, // All points moving away/closer to the midpoint
|
|
|
|
Rotate, // All points rotating from the midpoint
|
|
|
|
};
|
|
|
|
|
2021-09-21 03:33:50 +02:00
|
|
|
// This is nn::hid::GestureDirection
|
|
|
|
enum class GestureDirection : u32 {
|
2021-01-02 03:37:29 +01:00
|
|
|
None,
|
|
|
|
Left,
|
|
|
|
Up,
|
|
|
|
Right,
|
|
|
|
Down,
|
|
|
|
};
|
|
|
|
|
2021-09-21 03:33:50 +02:00
|
|
|
// This is nn::hid::GestureAttribute
|
|
|
|
struct GestureAttribute {
|
2021-01-02 03:37:29 +01:00
|
|
|
union {
|
2021-10-23 06:04:06 +02:00
|
|
|
u32 raw{};
|
2021-01-02 03:37:29 +01:00
|
|
|
|
2021-05-02 00:02:35 +02:00
|
|
|
BitField<4, 1, u32> is_new_touch;
|
|
|
|
BitField<8, 1, u32> is_double_tap;
|
2021-01-02 03:37:29 +01:00
|
|
|
};
|
|
|
|
};
|
2021-09-21 03:33:50 +02:00
|
|
|
static_assert(sizeof(GestureAttribute) == 4, "GestureAttribute is an invalid size");
|
2021-01-02 03:37:29 +01:00
|
|
|
|
2021-09-21 03:33:50 +02:00
|
|
|
// This is nn::hid::GestureState
|
2018-10-05 16:23:21 +02:00
|
|
|
struct GestureState {
|
2021-10-23 06:04:06 +02:00
|
|
|
s64 sampling_number;
|
|
|
|
s64 detection_count;
|
2021-09-21 03:33:50 +02:00
|
|
|
GestureType type;
|
|
|
|
GestureDirection direction;
|
2021-10-23 06:04:06 +02:00
|
|
|
Common::Point<s32> pos;
|
|
|
|
Common::Point<s32> delta;
|
2018-10-05 16:23:21 +02:00
|
|
|
f32 vel_x;
|
|
|
|
f32 vel_y;
|
2021-09-21 03:33:50 +02:00
|
|
|
GestureAttribute attributes;
|
2021-05-02 00:02:35 +02:00
|
|
|
f32 scale;
|
|
|
|
f32 rotation_angle;
|
2021-10-23 06:04:06 +02:00
|
|
|
s32 point_count;
|
|
|
|
std::array<Common::Point<s32>, 4> points;
|
2018-10-05 16:23:21 +02:00
|
|
|
};
|
2021-10-21 07:49:09 +02:00
|
|
|
static_assert(sizeof(GestureState) == 0x60, "GestureState is an invalid size");
|
2021-01-02 03:37:29 +01:00
|
|
|
|
|
|
|
struct GestureProperties {
|
2021-10-23 06:04:06 +02:00
|
|
|
std::array<Common::Point<s32>, MAX_POINTS> points{};
|
2021-01-02 03:37:29 +01:00
|
|
|
std::size_t active_points{};
|
2021-10-23 06:04:06 +02:00
|
|
|
Common::Point<s32> mid_point{};
|
|
|
|
s64 detection_count{};
|
|
|
|
u64 delta_time{};
|
2021-05-02 00:02:35 +02:00
|
|
|
f32 average_distance{};
|
|
|
|
f32 angle{};
|
2021-01-02 03:37:29 +01:00
|
|
|
};
|
|
|
|
|
2021-05-02 00:02:35 +02:00
|
|
|
// Reads input from all available input engines
|
|
|
|
void ReadTouchInput();
|
|
|
|
|
|
|
|
// Returns true if gesture state needs to be updated
|
|
|
|
bool ShouldUpdateGesture(const GestureProperties& gesture, f32 time_difference);
|
|
|
|
|
|
|
|
// Updates the shared memory to the next state
|
|
|
|
void UpdateGestureSharedMemory(u8* data, std::size_t size, GestureProperties& gesture,
|
|
|
|
f32 time_difference);
|
|
|
|
|
|
|
|
// Initializes new gesture
|
2021-09-21 03:33:50 +02:00
|
|
|
void NewGesture(GestureProperties& gesture, GestureType& type, GestureAttribute& attributes);
|
2021-05-02 00:02:35 +02:00
|
|
|
|
|
|
|
// Updates existing gesture state
|
2021-09-21 03:33:50 +02:00
|
|
|
void UpdateExistingGesture(GestureProperties& gesture, GestureType& type, f32 time_difference);
|
2021-05-02 00:02:35 +02:00
|
|
|
|
|
|
|
// Terminates exiting gesture
|
2021-05-16 07:46:30 +02:00
|
|
|
void EndGesture(GestureProperties& gesture, GestureProperties& last_gesture_props,
|
2021-09-21 03:33:50 +02:00
|
|
|
GestureType& type, GestureAttribute& attributes, f32 time_difference);
|
2021-05-02 00:02:35 +02:00
|
|
|
|
|
|
|
// Set current event to a tap event
|
2021-05-16 07:46:30 +02:00
|
|
|
void SetTapEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
|
2021-09-21 03:33:50 +02:00
|
|
|
GestureType& type, GestureAttribute& attributes);
|
2021-05-02 00:02:35 +02:00
|
|
|
|
|
|
|
// Calculates and set the extra parameters related to a pan event
|
2021-05-16 07:46:30 +02:00
|
|
|
void UpdatePanEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
|
2021-09-21 03:33:50 +02:00
|
|
|
GestureType& type, f32 time_difference);
|
2021-05-02 00:02:35 +02:00
|
|
|
|
|
|
|
// Terminates the pan event
|
2021-05-16 07:46:30 +02:00
|
|
|
void EndPanEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
|
2021-09-21 03:33:50 +02:00
|
|
|
GestureType& type, f32 time_difference);
|
2021-05-02 00:02:35 +02:00
|
|
|
|
|
|
|
// Set current event to a swipe event
|
2021-05-16 07:46:30 +02:00
|
|
|
void SetSwipeEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
|
2021-09-21 03:33:50 +02:00
|
|
|
GestureType& type);
|
2021-01-02 03:37:29 +01:00
|
|
|
|
2021-10-21 06:18:04 +02:00
|
|
|
// Retrieves the last gesture entry, as indicated by shared memory indices.
|
|
|
|
[[nodiscard]] const GestureState& GetLastGestureEntry() const;
|
|
|
|
|
2021-01-02 03:37:29 +01:00
|
|
|
// Returns the average distance, angle and middle point of the active fingers
|
|
|
|
GestureProperties GetGestureProperties();
|
|
|
|
|
2021-10-21 07:49:09 +02:00
|
|
|
// This is nn::hid::detail::GestureLifo
|
|
|
|
Lifo<GestureState> gesture_lifo{};
|
|
|
|
static_assert(sizeof(gesture_lifo) == 0x708, "gesture_lifo is an invalid size");
|
|
|
|
GestureState next_state{};
|
|
|
|
|
2021-09-21 03:33:50 +02:00
|
|
|
Core::HID::EmulatedConsole* console;
|
|
|
|
|
2021-10-23 06:04:06 +02:00
|
|
|
std::array<Core::HID::TouchFinger, MAX_POINTS> fingers{};
|
2021-05-02 00:02:35 +02:00
|
|
|
GestureProperties last_gesture{};
|
2021-10-23 06:04:06 +02:00
|
|
|
s64 last_update_timestamp{};
|
|
|
|
s64 last_tap_timestamp{};
|
2021-05-02 00:02:35 +02:00
|
|
|
f32 last_pan_time_difference{};
|
|
|
|
bool force_update{false};
|
|
|
|
bool enable_press_and_tap{false};
|
2018-10-05 16:23:21 +02:00
|
|
|
};
|
2018-10-06 05:14:42 +02:00
|
|
|
} // namespace Service::HID
|