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-01-02 03:37:29 +01:00
|
|
|
#include "core/frontend/input.h"
|
2018-10-05 16:23:21 +02:00
|
|
|
#include "core/hle/service/hid/controllers/controller_base.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
// Called when input devices should be loaded
|
|
|
|
void OnLoadInputDevices() override;
|
|
|
|
|
|
|
|
private:
|
2021-01-02 03:37:29 +01:00
|
|
|
static constexpr size_t MAX_FINGERS = 16;
|
|
|
|
static constexpr size_t MAX_POINTS = 4;
|
|
|
|
|
|
|
|
enum class TouchType : u32 {
|
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class Direction : u32 {
|
|
|
|
None,
|
|
|
|
Left,
|
|
|
|
Up,
|
|
|
|
Right,
|
|
|
|
Down,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Attribute {
|
|
|
|
union {
|
|
|
|
u32_le raw{};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|
|
|
|
static_assert(sizeof(Attribute) == 4, "Attribute is an invalid size");
|
|
|
|
|
|
|
|
struct Points {
|
2018-10-05 16:23:21 +02:00
|
|
|
s32_le x;
|
|
|
|
s32_le y;
|
|
|
|
};
|
2021-01-02 03:37:29 +01:00
|
|
|
static_assert(sizeof(Points) == 8, "Points is an invalid size");
|
2018-10-05 16:23:21 +02:00
|
|
|
|
|
|
|
struct GestureState {
|
|
|
|
s64_le sampling_number;
|
|
|
|
s64_le sampling_number2;
|
|
|
|
s64_le detection_count;
|
2021-01-02 03:37:29 +01:00
|
|
|
TouchType type;
|
2021-05-02 00:02:35 +02:00
|
|
|
Direction direction;
|
2018-10-05 16:23:21 +02:00
|
|
|
s32_le x;
|
|
|
|
s32_le y;
|
|
|
|
s32_le delta_x;
|
|
|
|
s32_le delta_y;
|
|
|
|
f32 vel_x;
|
|
|
|
f32 vel_y;
|
2021-01-02 03:37:29 +01:00
|
|
|
Attribute attributes;
|
2021-05-02 00:02:35 +02:00
|
|
|
f32 scale;
|
|
|
|
f32 rotation_angle;
|
2021-01-02 03:37:29 +01:00
|
|
|
s32_le point_count;
|
|
|
|
std::array<Points, 4> points;
|
2018-10-05 16:23:21 +02:00
|
|
|
};
|
|
|
|
static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size");
|
|
|
|
|
|
|
|
struct SharedMemory {
|
|
|
|
CommonHeader header;
|
|
|
|
std::array<GestureState, 17> gesture_states;
|
|
|
|
};
|
2021-01-02 03:37:29 +01:00
|
|
|
static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size");
|
|
|
|
|
|
|
|
struct Finger {
|
|
|
|
f32 x{};
|
|
|
|
f32 y{};
|
|
|
|
bool pressed{};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GestureProperties {
|
|
|
|
std::array<Points, MAX_POINTS> points{};
|
|
|
|
std::size_t active_points{};
|
|
|
|
Points mid_point{};
|
|
|
|
s64_le detection_count{};
|
|
|
|
u64_le 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
|
|
|
|
void NewGesture(GestureProperties& gesture, TouchType& type, Attribute& attributes);
|
|
|
|
|
|
|
|
// Updates existing gesture state
|
|
|
|
void UpdateExistingGesture(GestureProperties& gesture, TouchType& type, f32 time_difference);
|
|
|
|
|
|
|
|
// Terminates exiting gesture
|
|
|
|
void EndGesture(GestureProperties& gesture, GestureProperties& last_gesture, TouchType& type,
|
|
|
|
Attribute& attributes, f32 time_difference);
|
|
|
|
|
|
|
|
// Set current event to a tap event
|
|
|
|
void SetTapEvent(GestureProperties& gesture, GestureProperties& last_gesture, TouchType& type,
|
|
|
|
Attribute& attributes);
|
|
|
|
|
|
|
|
// Calculates and set the extra parameters related to a pan event
|
|
|
|
void UpdatePanEvent(GestureProperties& gesture, GestureProperties& last_gesture,
|
|
|
|
TouchType& type, f32 time_difference);
|
|
|
|
|
|
|
|
// Terminates the pan event
|
|
|
|
void EndPanEvent(GestureProperties& gesture, GestureProperties& last_gesture, TouchType& type,
|
|
|
|
f32 time_difference);
|
|
|
|
|
|
|
|
// Set current event to a swipe event
|
|
|
|
void SetSwipeEvent(GestureProperties& gesture, GestureProperties& last_gesture,
|
|
|
|
TouchType& type);
|
|
|
|
|
2021-01-02 03:37:29 +01:00
|
|
|
// Returns an unused finger id, if there is no fingers avaliable MAX_FINGERS will be returned
|
|
|
|
std::optional<size_t> GetUnusedFingerID() const;
|
|
|
|
|
|
|
|
/** If the touch is new it tries to assing a new finger id, if there is no fingers avaliable no
|
|
|
|
* changes will be made. Updates the coordinates if the finger id it's already set. If the touch
|
|
|
|
* ends delays the output by one frame to set the end_touch flag before finally freeing the
|
|
|
|
* finger id */
|
|
|
|
size_t UpdateTouchInputEvent(const std::tuple<float, float, bool>& touch_input,
|
|
|
|
size_t finger_id);
|
|
|
|
|
|
|
|
// Returns the average distance, angle and middle point of the active fingers
|
|
|
|
GestureProperties GetGestureProperties();
|
|
|
|
|
2018-10-05 16:23:21 +02:00
|
|
|
SharedMemory shared_memory{};
|
2021-01-02 03:37:29 +01:00
|
|
|
std::unique_ptr<Input::TouchDevice> touch_mouse_device;
|
|
|
|
std::unique_ptr<Input::TouchDevice> touch_udp_device;
|
|
|
|
std::unique_ptr<Input::TouchDevice> touch_btn_device;
|
|
|
|
std::array<size_t, MAX_FINGERS> mouse_finger_id;
|
|
|
|
std::array<size_t, MAX_FINGERS> keyboard_finger_id;
|
|
|
|
std::array<size_t, MAX_FINGERS> udp_finger_id;
|
|
|
|
std::array<Finger, MAX_POINTS> fingers;
|
2021-05-02 00:02:35 +02:00
|
|
|
GestureProperties last_gesture{};
|
|
|
|
s64_le last_update_timestamp{};
|
|
|
|
s64_le last_tap_timestamp{};
|
|
|
|
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
|