2022-04-23 01:59:50 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-09-20 15:20:56 -07:00
|
|
|
|
|
|
|
#include "common/param_package.h"
|
|
|
|
#include "input_common/drivers/touch_screen.h"
|
|
|
|
|
|
|
|
namespace InputCommon {
|
|
|
|
|
2021-11-01 13:17:53 -07:00
|
|
|
constexpr PadIdentifier identifier = {
|
2022-02-05 09:35:39 -08:00
|
|
|
.guid = Common::UUID{},
|
2021-11-01 13:17:53 -07:00
|
|
|
.port = 0,
|
|
|
|
.pad = 0,
|
|
|
|
};
|
|
|
|
|
2021-12-13 06:05:23 -08:00
|
|
|
TouchScreen::TouchScreen(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
|
2021-09-20 15:20:56 -07:00
|
|
|
PreSetController(identifier);
|
2022-05-22 18:34:32 -07:00
|
|
|
ReleaseAllTouch();
|
2021-09-20 15:20:56 -07:00
|
|
|
}
|
|
|
|
|
2022-05-22 18:34:32 -07:00
|
|
|
void TouchScreen::TouchMoved(float x, float y, std::size_t finger_id) {
|
|
|
|
const auto index = GetIndexFromFingerId(finger_id);
|
|
|
|
if (!index) {
|
|
|
|
// Touch doesn't exist handle it as a new one
|
|
|
|
TouchPressed(x, y, finger_id);
|
2021-09-20 15:20:56 -07:00
|
|
|
return;
|
|
|
|
}
|
2022-05-22 18:34:32 -07:00
|
|
|
const auto i = index.value();
|
|
|
|
fingers[i].is_active = true;
|
|
|
|
SetButton(identifier, static_cast<int>(i), true);
|
|
|
|
SetAxis(identifier, static_cast<int>(i * 2), x);
|
|
|
|
SetAxis(identifier, static_cast<int>(i * 2 + 1), y);
|
2021-09-20 15:20:56 -07:00
|
|
|
}
|
|
|
|
|
2022-05-22 18:34:32 -07:00
|
|
|
void TouchScreen::TouchPressed(float x, float y, std::size_t finger_id) {
|
|
|
|
if (GetIndexFromFingerId(finger_id)) {
|
|
|
|
// Touch already exist. Just update the data
|
|
|
|
TouchMoved(x, y, finger_id);
|
2021-09-20 15:20:56 -07:00
|
|
|
return;
|
|
|
|
}
|
2022-05-22 18:34:32 -07:00
|
|
|
const auto index = GetNextFreeIndex();
|
|
|
|
if (!index) {
|
|
|
|
// No free entries. Ignore input
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto i = index.value();
|
|
|
|
fingers[i].is_enabled = true;
|
|
|
|
fingers[i].finger_id = finger_id;
|
|
|
|
TouchMoved(x, y, finger_id);
|
2021-09-20 15:20:56 -07:00
|
|
|
}
|
|
|
|
|
2022-05-22 18:34:32 -07:00
|
|
|
void TouchScreen::TouchReleased(std::size_t finger_id) {
|
|
|
|
const auto index = GetIndexFromFingerId(finger_id);
|
|
|
|
if (!index) {
|
2021-09-20 15:20:56 -07:00
|
|
|
return;
|
|
|
|
}
|
2022-05-22 18:34:32 -07:00
|
|
|
const auto i = index.value();
|
|
|
|
fingers[i].is_enabled = false;
|
|
|
|
SetButton(identifier, static_cast<int>(i), false);
|
|
|
|
SetAxis(identifier, static_cast<int>(i * 2), 0.0f);
|
|
|
|
SetAxis(identifier, static_cast<int>(i * 2 + 1), 0.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::size_t> TouchScreen::GetIndexFromFingerId(std::size_t finger_id) const {
|
|
|
|
for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) {
|
|
|
|
const auto& finger = fingers[index];
|
|
|
|
if (!finger.is_enabled) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (finger.finger_id == finger_id) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::size_t> TouchScreen::GetNextFreeIndex() const {
|
|
|
|
for (std::size_t index = 0; index < MAX_FINGER_COUNT; ++index) {
|
|
|
|
if (!fingers[index].is_enabled) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreen::ClearActiveFlag() {
|
|
|
|
for (auto& finger : fingers) {
|
|
|
|
finger.is_active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreen::ReleaseInactiveTouch() {
|
|
|
|
for (const auto& finger : fingers) {
|
|
|
|
if (!finger.is_active) {
|
|
|
|
TouchReleased(finger.finger_id);
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 15:20:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void TouchScreen::ReleaseAllTouch() {
|
2022-05-22 18:34:32 -07:00
|
|
|
for (const auto& finger : fingers) {
|
|
|
|
if (finger.is_enabled) {
|
|
|
|
TouchReleased(finger.finger_id);
|
|
|
|
}
|
2021-09-20 15:20:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace InputCommon
|