service/hid: Fix gesture input

This commit is contained in:
german77 2021-10-20 23:18:04 -05:00 committed by Narr the Reg
parent af55dd1935
commit 85052b8662
8 changed files with 158 additions and 90 deletions

View file

@ -20,27 +20,21 @@ void EmulatedConsole::ReloadFromSettings() {
ReloadInput(); ReloadInput();
} }
void EmulatedConsole::ReloadInput() { void EmulatedConsole::SetTouchParams() {
motion_devices = Input::CreateDevice<Input::InputDevice>(motion_params); // TODO(german77): Support any number of fingers
if (motion_devices) {
Input::InputCallback motion_callback{
[this](Input::CallbackStatus callback) { SetMotion(callback); }};
motion_devices->SetCallback(motion_callback);
}
// TODO: Fix this mess
std::size_t index = 0; std::size_t index = 0;
const std::string mouse_device_string =
fmt::format("engine:mouse,axis_x:10,axis_y:11,button:{}", index);
touch_devices[index] = Input::CreateDeviceFromString<Input::InputDevice>(mouse_device_string);
Input::InputCallback trigger_callbackk{
[this, index](Input::CallbackStatus callback) { SetTouch(callback, index); }};
touch_devices[index]->SetCallback(trigger_callbackk);
index++; // Hardcode mouse, touchscreen and cemuhook parameters
touch_params[index++] = Common::ParamPackage{"engine:mouse,axis_x:10,axis_y:11,button:0"};
touch_params[index++] = Common::ParamPackage{"engine:touch,axis_x:0,axis_y:1,button:0"};
touch_params[index++] = Common::ParamPackage{"engine:touch,axis_x:2,axis_y:3,button:1"};
touch_params[index++] = Common::ParamPackage{"engine:cemuhookudp,axis_x:0,axis_y:1,button:0"};
touch_params[index++] = Common::ParamPackage{"engine:cemuhookudp,axis_x:2,axis_y:3,button:1"};
const auto button_index = const auto button_index =
static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue()); static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue());
const auto& touch_buttons = Settings::values.touch_from_button_maps[button_index].buttons; const auto& touch_buttons = Settings::values.touch_from_button_maps[button_index].buttons;
for (const auto& config_entry : touch_buttons) { for (const auto& config_entry : touch_buttons) {
Common::ParamPackage params{config_entry}; Common::ParamPackage params{config_entry};
Common::ParamPackage touch_button_params; Common::ParamPackage touch_button_params;
@ -53,15 +47,32 @@ void EmulatedConsole::ReloadInput() {
touch_button_params.Set("x", x); touch_button_params.Set("x", x);
touch_button_params.Set("y", y); touch_button_params.Set("y", y);
touch_button_params.Set("touch_id", static_cast<int>(index)); touch_button_params.Set("touch_id", static_cast<int>(index));
touch_devices[index] = touch_params[index] = touch_button_params;
Input::CreateDeviceFromString<Input::InputDevice>(touch_button_params.Serialize()); index++;
if (!touch_devices[index]) { if (index >= touch_params.size()) {
continue; return;
}
}
} }
Input::InputCallback trigger_callback{ void EmulatedConsole::ReloadInput() {
SetTouchParams();
motion_devices = Input::CreateDevice<Input::InputDevice>(motion_params);
if (motion_devices) {
Input::InputCallback motion_callback{
[this](Input::CallbackStatus callback) { SetMotion(callback); }};
motion_devices->SetCallback(motion_callback);
}
std::size_t index = 0;
for (auto& touch_device : touch_devices) {
touch_device = Input::CreateDevice<Input::InputDevice>(touch_params[index]);
if (!touch_device) {
continue;
}
Input::InputCallback touch_callback{
[this, index](Input::CallbackStatus callback) { SetTouch(callback, index); }}; [this, index](Input::CallbackStatus callback) { SetTouch(callback, index); }};
touch_devices[index]->SetCallback(trigger_callback); touch_device->SetCallback(touch_callback);
index++; index++;
} }
} }

View file

@ -144,6 +144,9 @@ public:
void DeleteCallback(int key); void DeleteCallback(int key);
private: private:
/// Creates and stores the touch params
void SetTouchParams();
/** /**
* Updates the motion status of the console * Updates the motion status of the console
* @param A CallbackStatus containing gyro and accelerometer data * @param A CallbackStatus containing gyro and accelerometer data

View file

@ -705,7 +705,6 @@ void EmulatedController::Connect() {
} }
is_connected = true; is_connected = true;
} }
LOG_ERROR(Service_HID, "Connected controller {}", NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Connected, true); TriggerOnChange(ControllerTriggerType::Connected, true);
} }
@ -714,8 +713,6 @@ void EmulatedController::Disconnect() {
std::lock_guard lock{mutex}; std::lock_guard lock{mutex};
if (is_configuring) { if (is_configuring) {
temporary_is_connected = false; temporary_is_connected = false;
LOG_ERROR(Service_HID, "Disconnected temporal controller {}",
NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Disconnected, false); TriggerOnChange(ControllerTriggerType::Disconnected, false);
return; return;
} }
@ -725,7 +722,6 @@ void EmulatedController::Disconnect() {
} }
is_connected = false; is_connected = false;
} }
LOG_ERROR(Service_HID, "Disconnected controller {}", NpadIdTypeToIndex(npad_id_type));
TriggerOnChange(ControllerTriggerType::Disconnected, true); TriggerOnChange(ControllerTriggerType::Disconnected, true);
} }

View file

@ -28,12 +28,10 @@ constexpr f32 Square(s32 num) {
Controller_Gesture::Controller_Gesture(Core::System& system_) : ControllerBase(system_) { Controller_Gesture::Controller_Gesture(Core::System& system_) : ControllerBase(system_) {
console = system.HIDCore().GetEmulatedConsole(); console = system.HIDCore().GetEmulatedConsole();
} }
Controller_Gesture::~Controller_Gesture() = default; Controller_Gesture::~Controller_Gesture() = default;
void Controller_Gesture::OnInit() { void Controller_Gesture::OnInit() {
gesture_lifo.entry_count = 0; shared_memory.header.entry_count = 0;
gesture_lifo.last_entry_index = 0;
force_update = true; force_update = true;
} }
@ -41,27 +39,27 @@ void Controller_Gesture::OnRelease() {}
void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
std::size_t size) { std::size_t size) {
// TODO FIND WTF IS WRONG HERE!!!!!!!! shared_memory.header.timestamp = core_timing.GetCPUTicks();
return; shared_memory.header.total_entry_count = 17;
if (!IsControllerActivated()) { if (!IsControllerActivated()) {
gesture_lifo.entry_count = 0; shared_memory.header.entry_count = 0;
gesture_lifo.last_entry_index = 0; shared_memory.header.last_entry_index = 0;
std::memcpy(data, &gesture_lifo, sizeof(gesture_lifo));
return; return;
} }
ReadTouchInput(); ReadTouchInput();
GestureProperties gesture = GetGestureProperties(); GestureProperties gesture = GetGestureProperties();
f32 time_difference = f32 time_difference = static_cast<f32>(shared_memory.header.timestamp - last_update_timestamp) /
static_cast<f32>(gesture_lifo.timestamp - last_update_timestamp) / (1000 * 1000 * 1000); (1000 * 1000 * 1000);
// Only update if necesary // Only update if necesary
if (!ShouldUpdateGesture(gesture, time_difference)) { if (!ShouldUpdateGesture(gesture, time_difference)) {
return; return;
} }
last_update_timestamp = gesture_lifo.timestamp; last_update_timestamp = shared_memory.header.timestamp;
UpdateGestureSharedMemory(data, size, gesture, time_difference); UpdateGestureSharedMemory(data, size, gesture, time_difference);
} }
@ -77,7 +75,7 @@ void Controller_Gesture::ReadTouchInput() {
bool Controller_Gesture::ShouldUpdateGesture(const GestureProperties& gesture, bool Controller_Gesture::ShouldUpdateGesture(const GestureProperties& gesture,
f32 time_difference) { f32 time_difference) {
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; const auto& last_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index];
if (force_update) { if (force_update) {
force_update = false; force_update = false;
return true; return true;
@ -105,16 +103,24 @@ void Controller_Gesture::UpdateGestureSharedMemory(u8* data, std::size_t size,
GestureType type = GestureType::Idle; GestureType type = GestureType::Idle;
GestureAttribute attributes{}; GestureAttribute attributes{};
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; const auto& last_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index];
shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17;
auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index];
// Reset next state to default if (shared_memory.header.entry_count < 16) {
next_state.sampling_number = last_entry.sampling_number + 1; shared_memory.header.entry_count++;
next_state.delta = {}; }
next_state.vel_x = 0;
next_state.vel_y = 0; cur_entry.sampling_number = last_entry.sampling_number + 1;
next_state.direction = GestureDirection::None; cur_entry.sampling_number2 = cur_entry.sampling_number;
next_state.rotation_angle = 0;
next_state.scale = 0; // Reset values to default
cur_entry.delta = {};
cur_entry.vel_x = 0;
cur_entry.vel_y = 0;
cur_entry.direction = GestureDirection::None;
cur_entry.rotation_angle = 0;
cur_entry.scale = 0;
if (gesture.active_points > 0) { if (gesture.active_points > 0) {
if (last_gesture.active_points == 0) { if (last_gesture.active_points == 0) {
@ -127,21 +133,20 @@ void Controller_Gesture::UpdateGestureSharedMemory(u8* data, std::size_t size,
} }
// Apply attributes // Apply attributes
next_state.detection_count = gesture.detection_count; cur_entry.detection_count = gesture.detection_count;
next_state.type = type; cur_entry.type = type;
next_state.attributes = attributes; cur_entry.attributes = attributes;
next_state.pos = gesture.mid_point; cur_entry.pos = gesture.mid_point;
next_state.point_count = static_cast<s32>(gesture.active_points); cur_entry.point_count = static_cast<s32>(gesture.active_points);
next_state.points = gesture.points; cur_entry.points = gesture.points;
last_gesture = gesture; last_gesture = gesture;
gesture_lifo.WriteNextEntry(next_state); std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory));
std::memcpy(data + SHARED_MEMORY_OFFSET, &gesture_lifo, sizeof(gesture_lifo));
} }
void Controller_Gesture::NewGesture(GestureProperties& gesture, GestureType& type, void Controller_Gesture::NewGesture(GestureProperties& gesture, GestureType& type,
GestureAttribute& attributes) { GestureAttribute& attributes) {
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; const auto& last_entry = GetLastGestureEntry();
gesture.detection_count++; gesture.detection_count++;
type = GestureType::Touch; type = GestureType::Touch;
@ -155,7 +160,7 @@ void Controller_Gesture::NewGesture(GestureProperties& gesture, GestureType& typ
void Controller_Gesture::UpdateExistingGesture(GestureProperties& gesture, GestureType& type, void Controller_Gesture::UpdateExistingGesture(GestureProperties& gesture, GestureType& type,
f32 time_difference) { f32 time_difference) {
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; const auto& last_entry = GetLastGestureEntry();
// Promote to pan type if touch moved // Promote to pan type if touch moved
for (size_t id = 0; id < MAX_POINTS; id++) { for (size_t id = 0; id < MAX_POINTS; id++) {
@ -190,7 +195,7 @@ void Controller_Gesture::UpdateExistingGesture(GestureProperties& gesture, Gestu
void Controller_Gesture::EndGesture(GestureProperties& gesture, void Controller_Gesture::EndGesture(GestureProperties& gesture,
GestureProperties& last_gesture_props, GestureType& type, GestureProperties& last_gesture_props, GestureType& type,
GestureAttribute& attributes, f32 time_difference) { GestureAttribute& attributes, f32 time_difference) {
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; const auto& last_entry = GetLastGestureEntry();
if (last_gesture_props.active_points != 0) { if (last_gesture_props.active_points != 0) {
switch (last_entry.type) { switch (last_entry.type) {
@ -240,18 +245,19 @@ void Controller_Gesture::SetTapEvent(GestureProperties& gesture,
void Controller_Gesture::UpdatePanEvent(GestureProperties& gesture, void Controller_Gesture::UpdatePanEvent(GestureProperties& gesture,
GestureProperties& last_gesture_props, GestureType& type, GestureProperties& last_gesture_props, GestureType& type,
f32 time_difference) { f32 time_difference) {
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index];
const auto& last_entry = GetLastGestureEntry();
next_state.delta = gesture.mid_point - last_entry.pos; cur_entry.delta = gesture.mid_point - last_entry.pos;
next_state.vel_x = static_cast<f32>(next_state.delta.x) / time_difference; cur_entry.vel_x = static_cast<f32>(cur_entry.delta.x) / time_difference;
next_state.vel_y = static_cast<f32>(next_state.delta.y) / time_difference; cur_entry.vel_y = static_cast<f32>(cur_entry.delta.y) / time_difference;
last_pan_time_difference = time_difference; last_pan_time_difference = time_difference;
// Promote to pinch type // Promote to pinch type
if (std::abs(gesture.average_distance - last_gesture_props.average_distance) > if (std::abs(gesture.average_distance - last_gesture_props.average_distance) >
pinch_threshold) { pinch_threshold) {
type = GestureType::Pinch; type = GestureType::Pinch;
next_state.scale = gesture.average_distance / last_gesture_props.average_distance; cur_entry.scale = gesture.average_distance / last_gesture_props.average_distance;
} }
const f32 angle_between_two_lines = std::atan((gesture.angle - last_gesture_props.angle) / const f32 angle_between_two_lines = std::atan((gesture.angle - last_gesture_props.angle) /
@ -259,21 +265,22 @@ void Controller_Gesture::UpdatePanEvent(GestureProperties& gesture,
// Promote to rotate type // Promote to rotate type
if (std::abs(angle_between_two_lines) > angle_threshold) { if (std::abs(angle_between_two_lines) > angle_threshold) {
type = GestureType::Rotate; type = GestureType::Rotate;
next_state.scale = 0; cur_entry.scale = 0;
next_state.rotation_angle = angle_between_two_lines * 180.0f / Common::PI; cur_entry.rotation_angle = angle_between_two_lines * 180.0f / Common::PI;
} }
} }
void Controller_Gesture::EndPanEvent(GestureProperties& gesture, void Controller_Gesture::EndPanEvent(GestureProperties& gesture,
GestureProperties& last_gesture_props, GestureType& type, GestureProperties& last_gesture_props, GestureType& type,
f32 time_difference) { f32 time_difference) {
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index];
next_state.vel_x = const auto& last_entry = GetLastGestureEntry();
cur_entry.vel_x =
static_cast<f32>(last_entry.delta.x) / (last_pan_time_difference + time_difference); static_cast<f32>(last_entry.delta.x) / (last_pan_time_difference + time_difference);
next_state.vel_y = cur_entry.vel_y =
static_cast<f32>(last_entry.delta.y) / (last_pan_time_difference + time_difference); static_cast<f32>(last_entry.delta.y) / (last_pan_time_difference + time_difference);
const f32 curr_vel = const f32 curr_vel =
std::sqrt((next_state.vel_x * next_state.vel_x) + (next_state.vel_y * next_state.vel_y)); std::sqrt((cur_entry.vel_x * cur_entry.vel_x) + (cur_entry.vel_y * cur_entry.vel_y));
// Set swipe event with parameters // Set swipe event with parameters
if (curr_vel > swipe_threshold) { if (curr_vel > swipe_threshold) {
@ -283,33 +290,42 @@ void Controller_Gesture::EndPanEvent(GestureProperties& gesture,
// End panning without swipe // End panning without swipe
type = GestureType::Complete; type = GestureType::Complete;
next_state.vel_x = 0; cur_entry.vel_x = 0;
next_state.vel_y = 0; cur_entry.vel_y = 0;
force_update = true; force_update = true;
} }
void Controller_Gesture::SetSwipeEvent(GestureProperties& gesture, void Controller_Gesture::SetSwipeEvent(GestureProperties& gesture,
GestureProperties& last_gesture_props, GestureType& type) { GestureProperties& last_gesture_props, GestureType& type) {
const auto& last_entry = gesture_lifo.ReadCurrentEntry().state; auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index];
const auto& last_entry = GetLastGestureEntry();
type = GestureType::Swipe; type = GestureType::Swipe;
gesture = last_gesture_props; gesture = last_gesture_props;
force_update = true; force_update = true;
next_state.delta = last_entry.delta; cur_entry.delta = last_entry.delta;
if (std::abs(next_state.delta.x) > std::abs(next_state.delta.y)) { if (std::abs(cur_entry.delta.x) > std::abs(cur_entry.delta.y)) {
if (next_state.delta.x > 0) { if (cur_entry.delta.x > 0) {
next_state.direction = GestureDirection::Right; cur_entry.direction = GestureDirection::Right;
return; return;
} }
next_state.direction = GestureDirection::Left; cur_entry.direction = GestureDirection::Left;
return; return;
} }
if (next_state.delta.y > 0) { if (cur_entry.delta.y > 0) {
next_state.direction = GestureDirection::Down; cur_entry.direction = GestureDirection::Down;
return; return;
} }
next_state.direction = GestureDirection::Up; cur_entry.direction = GestureDirection::Up;
}
Controller_Gesture::GestureState& Controller_Gesture::GetLastGestureEntry() {
return shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17];
}
const Controller_Gesture::GestureState& Controller_Gesture::GetLastGestureEntry() const {
return shared_memory.gesture_states[(shared_memory.header.last_entry_index + 16) % 17];
} }
Controller_Gesture::GestureProperties Controller_Gesture::GetGestureProperties() { Controller_Gesture::GestureProperties Controller_Gesture::GetGestureProperties() {

View file

@ -71,6 +71,7 @@ private:
// This is nn::hid::GestureState // This is nn::hid::GestureState
struct GestureState { struct GestureState {
s64_le sampling_number; s64_le sampling_number;
s64_le sampling_number2;
s64_le detection_count; s64_le detection_count;
GestureType type; GestureType type;
GestureDirection direction; GestureDirection direction;
@ -84,7 +85,21 @@ private:
s32_le point_count; s32_le point_count;
std::array<Common::Point<s32_le>, 4> points; std::array<Common::Point<s32_le>, 4> points;
}; };
static_assert(sizeof(GestureState) == 0x60, "GestureState is an invalid size"); static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size");
struct CommonHeader {
s64_le timestamp;
s64_le total_entry_count;
s64_le last_entry_index;
s64_le entry_count;
};
static_assert(sizeof(CommonHeader) == 0x20, "CommonHeader is an invalid size");
struct SharedMemory {
CommonHeader header;
std::array<GestureState, 17> gesture_states;
};
static_assert(sizeof(SharedMemory) == 0x708, "SharedMemory is an invalid size");
struct Finger { struct Finger {
Common::Point<f32> pos{}; Common::Point<f32> pos{};
@ -137,17 +152,17 @@ private:
void SetSwipeEvent(GestureProperties& gesture, GestureProperties& last_gesture_props, void SetSwipeEvent(GestureProperties& gesture, GestureProperties& last_gesture_props,
GestureType& type); GestureType& type);
// Retrieves the last gesture entry, as indicated by shared memory indices.
[[nodiscard]] GestureState& GetLastGestureEntry();
[[nodiscard]] const GestureState& GetLastGestureEntry() const;
// Returns the average distance, angle and middle point of the active fingers // Returns the average distance, angle and middle point of the active fingers
GestureProperties GetGestureProperties(); GestureProperties GetGestureProperties();
// This is nn::hid::detail::GestureLifo SharedMemory shared_memory{};
Lifo<GestureState> gesture_lifo{};
static_assert(sizeof(gesture_lifo) == 0x708, "gesture_lifo is an invalid size");
GestureState next_state{};
std::array<Finger, MAX_POINTS> fingers{};
Core::HID::EmulatedConsole* console; Core::HID::EmulatedConsole* console;
std::array<Finger, MAX_POINTS> fingers{};
GestureProperties last_gesture{}; GestureProperties last_gesture{};
s64_le last_update_timestamp{}; s64_le last_update_timestamp{};
s64_le last_tap_timestamp{}; s64_le last_tap_timestamp{};

View file

@ -209,7 +209,7 @@ void GCAdapter::UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_
pads[port].axis_origin[index] = axis_value; pads[port].axis_origin[index] = axis_value;
pads[port].reset_origin_counter++; pads[port].reset_origin_counter++;
} }
const f32 axis_status = (axis_value - pads[port].axis_origin[index]) / 110.0f; const f32 axis_status = (axis_value - pads[port].axis_origin[index]) / 100.0f;
SetAxis(pads[port].identifier, static_cast<int>(index), axis_status); SetAxis(pads[port].identifier, static_cast<int>(index), axis_status);
} }
} }

View file

@ -243,6 +243,33 @@ void UDPClient::OnPadData(Response::PadData data, std::size_t client) {
}; };
const PadIdentifier identifier = GetPadIdentifier(pad_index); const PadIdentifier identifier = GetPadIdentifier(pad_index);
SetMotion(identifier, 0, motion); SetMotion(identifier, 0, motion);
for (std::size_t id = 0; id < data.touch.size(); ++id) {
const auto touch_pad = data.touch[id];
const int touch_id = static_cast<int>(client * 2 + id);
// TODO: Use custom calibration per device
const Common::ParamPackage touch_param(Settings::values.touch_device.GetValue());
const u16 min_x = static_cast<u16>(touch_param.Get("min_x", 100));
const u16 min_y = static_cast<u16>(touch_param.Get("min_y", 50));
const u16 max_x = static_cast<u16>(touch_param.Get("max_x", 1800));
const u16 max_y = static_cast<u16>(touch_param.Get("max_y", 850));
const f32 x =
static_cast<f32>(std::clamp(static_cast<u16>(touch_pad.x), min_x, max_x) - min_x) /
static_cast<f32>(max_x - min_x);
const f32 y =
static_cast<f32>(std::clamp(static_cast<u16>(touch_pad.y), min_y, max_y) - min_y) /
static_cast<f32>(max_y - min_y);
if (touch_pad.is_active) {
SetAxis(identifier, touch_id * 2, x);
SetAxis(identifier, touch_id * 2 + 1, y);
SetButton(identifier, touch_id, true);
continue;
}
SetButton(identifier, touch_id, false);
}
} }
void UDPClient::StartCommunication(std::size_t client, const std::string& host, u16 port) { void UDPClient::StartCommunication(std::size_t client, const std::string& host, u16 port) {