2020-07-14 19:01:36 +02:00
|
|
|
// Copyright 2018 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <array>
|
2020-11-18 05:16:29 +01:00
|
|
|
#include <sstream>
|
2020-07-14 19:01:36 +02:00
|
|
|
#include <QCloseEvent>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPushButton>
|
2020-11-18 05:16:29 +01:00
|
|
|
#include <QStringListModel>
|
2020-07-14 19:01:36 +02:00
|
|
|
#include <QVBoxLayout>
|
2020-08-29 20:56:51 +02:00
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "core/settings.h"
|
2020-07-14 19:01:36 +02:00
|
|
|
#include "input_common/main.h"
|
2020-08-29 20:56:51 +02:00
|
|
|
#include "input_common/udp/client.h"
|
|
|
|
#include "input_common/udp/udp.h"
|
2020-07-14 19:01:36 +02:00
|
|
|
#include "ui_configure_motion_touch.h"
|
|
|
|
#include "yuzu/configuration/configure_motion_touch.h"
|
|
|
|
#include "yuzu/configuration/configure_touch_from_button.h"
|
|
|
|
|
|
|
|
CalibrationConfigurationDialog::CalibrationConfigurationDialog(QWidget* parent,
|
|
|
|
const std::string& host, u16 port,
|
|
|
|
u8 pad_index, u16 client_id)
|
|
|
|
: QDialog(parent) {
|
|
|
|
layout = new QVBoxLayout;
|
|
|
|
status_label = new QLabel(tr("Communicating with the server..."));
|
|
|
|
cancel_button = new QPushButton(tr("Cancel"));
|
|
|
|
connect(cancel_button, &QPushButton::clicked, this, [this] {
|
2020-08-29 20:56:51 +02:00
|
|
|
if (!completed) {
|
2020-07-14 19:01:36 +02:00
|
|
|
job->Stop();
|
2020-08-29 20:56:51 +02:00
|
|
|
}
|
2020-07-14 19:01:36 +02:00
|
|
|
accept();
|
|
|
|
});
|
|
|
|
layout->addWidget(status_label);
|
|
|
|
layout->addWidget(cancel_button);
|
|
|
|
setLayout(layout);
|
|
|
|
|
|
|
|
using namespace InputCommon::CemuhookUDP;
|
|
|
|
job = std::make_unique<CalibrationConfigurationJob>(
|
|
|
|
host, port, pad_index, client_id,
|
|
|
|
[this](CalibrationConfigurationJob::Status status) {
|
|
|
|
QString text;
|
|
|
|
switch (status) {
|
|
|
|
case CalibrationConfigurationJob::Status::Ready:
|
|
|
|
text = tr("Touch the top left corner <br>of your touchpad.");
|
|
|
|
break;
|
|
|
|
case CalibrationConfigurationJob::Status::Stage1Completed:
|
|
|
|
text = tr("Now touch the bottom right corner <br>of your touchpad.");
|
|
|
|
break;
|
|
|
|
case CalibrationConfigurationJob::Status::Completed:
|
|
|
|
text = tr("Configuration completed!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
QMetaObject::invokeMethod(this, "UpdateLabelText", Q_ARG(QString, text));
|
|
|
|
if (status == CalibrationConfigurationJob::Status::Completed) {
|
|
|
|
QMetaObject::invokeMethod(this, "UpdateButtonText", Q_ARG(QString, tr("OK")));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[this](u16 min_x_, u16 min_y_, u16 max_x_, u16 max_y_) {
|
|
|
|
completed = true;
|
|
|
|
min_x = min_x_;
|
|
|
|
min_y = min_y_;
|
|
|
|
max_x = max_x_;
|
|
|
|
max_y = max_y_;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CalibrationConfigurationDialog::~CalibrationConfigurationDialog() = default;
|
|
|
|
|
2020-08-29 20:56:51 +02:00
|
|
|
void CalibrationConfigurationDialog::UpdateLabelText(const QString& text) {
|
2020-07-14 19:01:36 +02:00
|
|
|
status_label->setText(text);
|
|
|
|
}
|
|
|
|
|
2020-08-29 20:56:51 +02:00
|
|
|
void CalibrationConfigurationDialog::UpdateButtonText(const QString& text) {
|
2020-07-14 19:01:36 +02:00
|
|
|
cancel_button->setText(text);
|
|
|
|
}
|
|
|
|
|
2020-08-29 20:56:51 +02:00
|
|
|
constexpr std::array<std::pair<const char*, const char*>, 2> TouchProviders = {{
|
|
|
|
{"emu_window", QT_TRANSLATE_NOOP("ConfigureMotionTouch", "Emulator Window")},
|
|
|
|
{"cemuhookudp", QT_TRANSLATE_NOOP("ConfigureMotionTouch", "CemuhookUDP")},
|
|
|
|
}};
|
2020-07-14 19:01:36 +02:00
|
|
|
|
2020-08-29 20:56:51 +02:00
|
|
|
ConfigureMotionTouch::ConfigureMotionTouch(QWidget* parent,
|
|
|
|
InputCommon::InputSubsystem* input_subsystem_)
|
|
|
|
: QDialog(parent), input_subsystem{input_subsystem_},
|
|
|
|
ui(std::make_unique<Ui::ConfigureMotionTouch>()) {
|
2020-07-14 19:01:36 +02:00
|
|
|
ui->setupUi(this);
|
2020-08-30 00:07:38 +02:00
|
|
|
for (const auto& [provider, name] : TouchProviders) {
|
2020-07-14 19:01:36 +02:00
|
|
|
ui->touch_provider->addItem(tr(name), QString::fromUtf8(provider));
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->udp_learn_more->setOpenExternalLinks(true);
|
|
|
|
ui->udp_learn_more->setText(
|
|
|
|
tr("<a "
|
2020-08-29 20:56:51 +02:00
|
|
|
"href='https://yuzu-emu.org/wiki/"
|
2020-07-14 19:01:36 +02:00
|
|
|
"using-a-controller-or-android-phone-for-motion-or-touch-input'><span "
|
|
|
|
"style=\"text-decoration: underline; color:#039be5;\">Learn More</span></a>"));
|
|
|
|
|
|
|
|
SetConfiguration();
|
|
|
|
UpdateUiDisplay();
|
|
|
|
ConnectEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfigureMotionTouch::~ConfigureMotionTouch() = default;
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::SetConfiguration() {
|
2020-08-30 00:07:38 +02:00
|
|
|
const Common::ParamPackage motion_param(Settings::values.motion_device);
|
|
|
|
const Common::ParamPackage touch_param(Settings::values.touch_device);
|
|
|
|
const std::string motion_engine = motion_param.Get("engine", "motion_emu");
|
|
|
|
const std::string touch_engine = touch_param.Get("engine", "emu_window");
|
2020-07-14 19:01:36 +02:00
|
|
|
|
|
|
|
ui->touch_provider->setCurrentIndex(
|
|
|
|
ui->touch_provider->findData(QString::fromStdString(touch_engine)));
|
|
|
|
ui->touch_from_button_checkbox->setChecked(Settings::values.use_touch_from_button);
|
|
|
|
touch_from_button_maps = Settings::values.touch_from_button_maps;
|
|
|
|
for (const auto& touch_map : touch_from_button_maps) {
|
|
|
|
ui->touch_from_button_map->addItem(QString::fromStdString(touch_map.name));
|
|
|
|
}
|
|
|
|
ui->touch_from_button_map->setCurrentIndex(Settings::values.touch_from_button_map_index);
|
|
|
|
ui->motion_sensitivity->setValue(motion_param.Get("sensitivity", 0.01f));
|
|
|
|
|
|
|
|
min_x = touch_param.Get("min_x", 100);
|
|
|
|
min_y = touch_param.Get("min_y", 50);
|
|
|
|
max_x = touch_param.Get("max_x", 1800);
|
|
|
|
max_y = touch_param.Get("max_y", 850);
|
|
|
|
|
2020-11-18 05:16:29 +01:00
|
|
|
ui->udp_server->setText(QString::fromStdString("127.0.0.1"));
|
|
|
|
ui->udp_port->setText(QString::number(26760));
|
|
|
|
|
|
|
|
udp_server_list_model = new QStringListModel(this);
|
|
|
|
udp_server_list_model->setStringList({});
|
|
|
|
ui->udp_server_list->setModel(udp_server_list_model);
|
|
|
|
|
|
|
|
std::stringstream ss(Settings::values.udp_input_servers);
|
|
|
|
std::string token;
|
|
|
|
|
|
|
|
while (std::getline(ss, token, ',')) {
|
|
|
|
const int row = udp_server_list_model->rowCount();
|
|
|
|
udp_server_list_model->insertRows(row, 1);
|
|
|
|
const QModelIndex index = udp_server_list_model->index(row);
|
|
|
|
udp_server_list_model->setData(index, QString::fromStdString(token));
|
|
|
|
}
|
2020-07-14 19:01:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::UpdateUiDisplay() {
|
2020-08-29 20:56:51 +02:00
|
|
|
const QString touch_engine = ui->touch_provider->currentData().toString();
|
2020-08-30 00:07:38 +02:00
|
|
|
const QString cemuhook_udp = QStringLiteral("cemuhookudp");
|
2020-07-14 19:01:36 +02:00
|
|
|
|
2020-11-18 05:16:29 +01:00
|
|
|
ui->motion_sensitivity_label->setVisible(true);
|
|
|
|
ui->motion_sensitivity->setVisible(true);
|
2020-07-14 19:01:36 +02:00
|
|
|
|
2020-08-29 20:56:51 +02:00
|
|
|
if (touch_engine == cemuhook_udp) {
|
2020-07-14 19:01:36 +02:00
|
|
|
ui->touch_calibration->setVisible(true);
|
|
|
|
ui->touch_calibration_config->setVisible(true);
|
|
|
|
ui->touch_calibration_label->setVisible(true);
|
2020-08-29 20:56:51 +02:00
|
|
|
ui->touch_calibration->setText(
|
|
|
|
QStringLiteral("(%1, %2) - (%3, %4)").arg(min_x).arg(min_y).arg(max_x).arg(max_y));
|
2020-07-14 19:01:36 +02:00
|
|
|
} else {
|
|
|
|
ui->touch_calibration->setVisible(false);
|
|
|
|
ui->touch_calibration_config->setVisible(false);
|
|
|
|
ui->touch_calibration_label->setVisible(false);
|
|
|
|
}
|
|
|
|
|
2020-11-18 05:16:29 +01:00
|
|
|
ui->udp_config_group_box->setVisible(true);
|
2020-07-14 19:01:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::ConnectEvents() {
|
2020-08-29 20:56:51 +02:00
|
|
|
connect(ui->touch_provider, qOverload<int>(&QComboBox::currentIndexChanged), this,
|
2020-07-14 19:01:36 +02:00
|
|
|
[this](int index) { UpdateUiDisplay(); });
|
|
|
|
connect(ui->udp_test, &QPushButton::clicked, this, &ConfigureMotionTouch::OnCemuhookUDPTest);
|
2020-11-18 05:16:29 +01:00
|
|
|
connect(ui->udp_add, &QPushButton::clicked, this, &ConfigureMotionTouch::OnUDPAddServer);
|
|
|
|
connect(ui->udp_remove, &QPushButton::clicked, this, &ConfigureMotionTouch::OnUDPDeleteServer);
|
2020-07-14 19:01:36 +02:00
|
|
|
connect(ui->touch_calibration_config, &QPushButton::clicked, this,
|
|
|
|
&ConfigureMotionTouch::OnConfigureTouchCalibration);
|
|
|
|
connect(ui->touch_from_button_config_btn, &QPushButton::clicked, this,
|
|
|
|
&ConfigureMotionTouch::OnConfigureTouchFromButton);
|
|
|
|
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, [this] {
|
2020-08-29 20:56:51 +02:00
|
|
|
if (CanCloseDialog()) {
|
2020-07-14 19:01:36 +02:00
|
|
|
reject();
|
2020-08-29 20:56:51 +02:00
|
|
|
}
|
2020-07-14 19:01:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-18 05:16:29 +01:00
|
|
|
void ConfigureMotionTouch::OnUDPAddServer() {
|
|
|
|
QRegExp re(tr("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?["
|
|
|
|
"0-9][0-9]?)$")); // a valid ip address
|
|
|
|
bool ok;
|
|
|
|
QString port_text = ui->udp_port->text();
|
|
|
|
QString server_text = ui->udp_server->text();
|
|
|
|
const QString server_string = tr("%1:%2").arg(server_text, port_text);
|
|
|
|
int port_number = port_text.toInt(&ok, 10);
|
|
|
|
int row = udp_server_list_model->rowCount();
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
QMessageBox::warning(this, tr("yuzu"), tr("Port number has invalid characters"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (port_number < 0 || port_number > 65353) {
|
|
|
|
QMessageBox::warning(this, tr("yuzu"), tr("Port has to be in range 0 and 65353"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!re.exactMatch(server_text)) {
|
|
|
|
QMessageBox::warning(this, tr("yuzu"), tr("IP address is not valid"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Search for duplicates
|
|
|
|
for (const auto& item : udp_server_list_model->stringList()) {
|
|
|
|
if (item == server_string) {
|
|
|
|
QMessageBox::warning(this, tr("yuzu"), tr("This UDP server already exists"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Limit server count to 8
|
|
|
|
if (row == 8) {
|
|
|
|
QMessageBox::warning(this, tr("yuzu"), tr("Unable to add more than 8 servers"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
udp_server_list_model->insertRows(row, 1);
|
|
|
|
QModelIndex index = udp_server_list_model->index(row);
|
|
|
|
udp_server_list_model->setData(index, server_string);
|
|
|
|
ui->udp_server_list->setCurrentIndex(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::OnUDPDeleteServer() {
|
|
|
|
udp_server_list_model->removeRows(ui->udp_server_list->currentIndex().row(), 1);
|
|
|
|
}
|
|
|
|
|
2020-07-14 19:01:36 +02:00
|
|
|
void ConfigureMotionTouch::OnCemuhookUDPTest() {
|
|
|
|
ui->udp_test->setEnabled(false);
|
|
|
|
ui->udp_test->setText(tr("Testing"));
|
|
|
|
udp_test_in_progress = true;
|
|
|
|
InputCommon::CemuhookUDP::TestCommunication(
|
2020-11-18 05:16:29 +01:00
|
|
|
ui->udp_server->text().toStdString(), static_cast<u16>(ui->udp_port->text().toInt()), 0,
|
|
|
|
24872,
|
2020-07-14 19:01:36 +02:00
|
|
|
[this] {
|
|
|
|
LOG_INFO(Frontend, "UDP input test success");
|
|
|
|
QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, true));
|
|
|
|
},
|
|
|
|
[this] {
|
|
|
|
LOG_ERROR(Frontend, "UDP input test failed");
|
|
|
|
QMetaObject::invokeMethod(this, "ShowUDPTestResult", Q_ARG(bool, false));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::OnConfigureTouchCalibration() {
|
|
|
|
ui->touch_calibration_config->setEnabled(false);
|
|
|
|
ui->touch_calibration_config->setText(tr("Configuring"));
|
2020-11-18 05:16:29 +01:00
|
|
|
CalibrationConfigurationDialog dialog(this, ui->udp_server->text().toStdString(),
|
|
|
|
static_cast<u16>(ui->udp_port->text().toUInt()), 0,
|
|
|
|
24872);
|
2020-08-29 20:56:51 +02:00
|
|
|
dialog.exec();
|
|
|
|
if (dialog.completed) {
|
|
|
|
min_x = dialog.min_x;
|
|
|
|
min_y = dialog.min_y;
|
|
|
|
max_x = dialog.max_x;
|
|
|
|
max_y = dialog.max_y;
|
2020-07-14 19:01:36 +02:00
|
|
|
LOG_INFO(Frontend,
|
|
|
|
"UDP touchpad calibration config success: min_x={}, min_y={}, max_x={}, max_y={}",
|
|
|
|
min_x, min_y, max_x, max_y);
|
|
|
|
UpdateUiDisplay();
|
|
|
|
} else {
|
|
|
|
LOG_ERROR(Frontend, "UDP touchpad calibration config failed");
|
|
|
|
}
|
|
|
|
ui->touch_calibration_config->setEnabled(true);
|
|
|
|
ui->touch_calibration_config->setText(tr("Configure"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::closeEvent(QCloseEvent* event) {
|
2020-08-29 20:56:51 +02:00
|
|
|
if (CanCloseDialog()) {
|
2020-07-14 19:01:36 +02:00
|
|
|
event->accept();
|
2020-08-29 20:56:51 +02:00
|
|
|
} else {
|
2020-07-14 19:01:36 +02:00
|
|
|
event->ignore();
|
2020-08-29 20:56:51 +02:00
|
|
|
}
|
2020-07-14 19:01:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::ShowUDPTestResult(bool result) {
|
|
|
|
udp_test_in_progress = false;
|
|
|
|
if (result) {
|
|
|
|
QMessageBox::information(this, tr("Test Successful"),
|
|
|
|
tr("Successfully received data from the server."));
|
|
|
|
} else {
|
|
|
|
QMessageBox::warning(this, tr("Test Failed"),
|
|
|
|
tr("Could not receive valid data from the server.<br>Please verify "
|
|
|
|
"that the server is set up correctly and "
|
|
|
|
"the address and port are correct."));
|
|
|
|
}
|
|
|
|
ui->udp_test->setEnabled(true);
|
|
|
|
ui->udp_test->setText(tr("Test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::OnConfigureTouchFromButton() {
|
2020-08-29 20:56:51 +02:00
|
|
|
ConfigureTouchFromButton dialog{this, touch_from_button_maps, input_subsystem,
|
2020-07-14 19:01:36 +02:00
|
|
|
ui->touch_from_button_map->currentIndex()};
|
|
|
|
if (dialog.exec() != QDialog::Accepted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
touch_from_button_maps = dialog.GetMaps();
|
|
|
|
|
|
|
|
while (ui->touch_from_button_map->count() > 0) {
|
|
|
|
ui->touch_from_button_map->removeItem(0);
|
|
|
|
}
|
|
|
|
for (const auto& touch_map : touch_from_button_maps) {
|
|
|
|
ui->touch_from_button_map->addItem(QString::fromStdString(touch_map.name));
|
|
|
|
}
|
|
|
|
ui->touch_from_button_map->setCurrentIndex(dialog.GetSelectedIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConfigureMotionTouch::CanCloseDialog() {
|
|
|
|
if (udp_test_in_progress) {
|
2020-11-18 05:16:29 +01:00
|
|
|
QMessageBox::warning(this, tr("yuzu"),
|
2020-07-14 19:01:36 +02:00
|
|
|
tr("UDP Test or calibration configuration is in progress.<br>Please "
|
|
|
|
"wait for them to finish."));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMotionTouch::ApplyConfiguration() {
|
2020-08-29 20:56:51 +02:00
|
|
|
if (!CanCloseDialog()) {
|
2020-07-14 19:01:36 +02:00
|
|
|
return;
|
2020-08-29 20:56:51 +02:00
|
|
|
}
|
2020-07-14 19:01:36 +02:00
|
|
|
|
|
|
|
std::string touch_engine = ui->touch_provider->currentData().toString().toStdString();
|
|
|
|
|
2020-11-18 05:16:29 +01:00
|
|
|
Common::ParamPackage touch_param{};
|
2020-08-30 00:07:38 +02:00
|
|
|
touch_param.Set("engine", std::move(touch_engine));
|
2020-07-14 19:01:36 +02:00
|
|
|
|
|
|
|
if (touch_engine == "cemuhookudp") {
|
|
|
|
touch_param.Set("min_x", min_x);
|
|
|
|
touch_param.Set("min_y", min_y);
|
|
|
|
touch_param.Set("max_x", max_x);
|
|
|
|
touch_param.Set("max_y", max_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings::values.touch_device = touch_param.Serialize();
|
|
|
|
Settings::values.use_touch_from_button = ui->touch_from_button_checkbox->isChecked();
|
|
|
|
Settings::values.touch_from_button_map_index = ui->touch_from_button_map->currentIndex();
|
|
|
|
Settings::values.touch_from_button_maps = touch_from_button_maps;
|
2020-11-18 05:16:29 +01:00
|
|
|
Settings::values.udp_input_servers = GetUDPServerString();
|
2020-08-29 20:56:51 +02:00
|
|
|
input_subsystem->ReloadInputDevices();
|
2020-07-14 19:01:36 +02:00
|
|
|
|
|
|
|
accept();
|
|
|
|
}
|
2020-11-18 05:16:29 +01:00
|
|
|
|
|
|
|
std::string ConfigureMotionTouch::GetUDPServerString() const {
|
|
|
|
QString input_servers;
|
|
|
|
|
|
|
|
for (const auto& item : udp_server_list_model->stringList()) {
|
|
|
|
input_servers += item;
|
|
|
|
input_servers += QLatin1Char{','};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove last comma
|
|
|
|
input_servers.chop(1);
|
|
|
|
return input_servers.toStdString();
|
|
|
|
}
|