2024-05-15 15:44:45 -04:00
|
|
|
#include "backend.h"
|
|
|
|
|
2024-08-04 17:36:55 -04:00
|
|
|
#include <QBuffer>
|
2024-05-24 03:08:17 -04:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDir>
|
2024-05-24 11:37:54 -04:00
|
|
|
#include <QMessageBox>
|
2024-08-02 13:46:16 -04:00
|
|
|
#include <QNetworkDatagram>
|
2024-08-04 13:53:00 -04:00
|
|
|
#include <QtConcurrent/QtConcurrent>
|
2024-05-24 03:08:17 -04:00
|
|
|
|
2024-08-02 13:46:16 -04:00
|
|
|
#include <arpa/inet.h>
|
2024-05-23 22:47:32 -04:00
|
|
|
#include <fcntl.h>
|
2024-08-12 23:22:51 -04:00
|
|
|
#include <signal.h>
|
2024-05-23 22:47:32 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2024-05-15 15:44:45 -04:00
|
|
|
#include <vanilla.h>
|
|
|
|
|
2024-10-14 06:09:20 -04:00
|
|
|
void Backend::vanillaEventHandler()
|
|
|
|
{
|
|
|
|
vanilla_event_t event;
|
|
|
|
|
|
|
|
while (vanilla_wait_event(&event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case VANILLA_EVENT_VIDEO:
|
|
|
|
emit videoAvailable(QByteArray((const char *) event.data, event.size));
|
|
|
|
break;
|
|
|
|
case VANILLA_EVENT_AUDIO:
|
|
|
|
emit audioAvailable(QByteArray((const char *) event.data, event.size));
|
|
|
|
break;
|
|
|
|
case VANILLA_EVENT_VIBRATE:
|
|
|
|
emit vibrate(*event.data);
|
|
|
|
break;
|
|
|
|
}
|
2024-11-15 19:52:19 -05:00
|
|
|
|
|
|
|
vanilla_free_event(&event);
|
2024-05-18 15:55:00 -04:00
|
|
|
}
|
2024-05-15 15:44:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Backend::Backend(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2024-08-02 13:46:16 -04:00
|
|
|
}
|
2024-05-24 03:08:17 -04:00
|
|
|
|
2024-08-02 13:46:16 -04:00
|
|
|
void Backend::init()
|
|
|
|
{
|
2024-10-09 14:42:21 -04:00
|
|
|
initInternal();
|
2024-08-02 13:46:16 -04:00
|
|
|
}
|
2024-05-24 11:37:54 -04:00
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
int Backend::initInternal()
|
2024-08-02 13:46:16 -04:00
|
|
|
{
|
2024-10-09 14:42:21 -04:00
|
|
|
emit ready();
|
|
|
|
return 0;
|
2024-08-02 13:46:16 -04:00
|
|
|
}
|
2024-05-24 11:37:54 -04:00
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::interrupt()
|
2024-06-13 21:43:58 -04:00
|
|
|
{
|
2024-08-02 13:46:16 -04:00
|
|
|
vanilla_stop();
|
2024-06-13 21:43:58 -04:00
|
|
|
}
|
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::requestIDR()
|
2024-08-02 13:46:16 -04:00
|
|
|
{
|
|
|
|
vanilla_request_idr();
|
|
|
|
}
|
2024-05-24 03:08:17 -04:00
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::sync(uint16_t code)
|
2024-09-18 14:57:38 -04:00
|
|
|
{
|
|
|
|
auto watcher = new QFutureWatcher<int>();
|
2024-10-09 14:42:21 -04:00
|
|
|
connect(watcher, &QFutureWatcher<int>::finished, this, &Backend::syncFutureCompleted);
|
|
|
|
watcher->setFuture(QtConcurrent::run(&Backend::syncInternal, this, code));
|
2024-09-18 14:57:38 -04:00
|
|
|
}
|
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
int Backend::syncInternal(uint16_t code)
|
2024-08-02 13:46:16 -04:00
|
|
|
{
|
2024-10-09 14:42:21 -04:00
|
|
|
return vanilla_sync(code, 0);
|
2024-08-04 13:53:00 -04:00
|
|
|
}
|
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::connectToConsole()
|
2024-08-04 13:53:00 -04:00
|
|
|
{
|
2024-10-14 03:52:57 -04:00
|
|
|
connectInternal();
|
2024-10-14 06:09:20 -04:00
|
|
|
QtConcurrent::run(&Backend::vanillaEventHandler, this);
|
2024-08-02 13:46:16 -04:00
|
|
|
}
|
2024-05-24 03:08:17 -04:00
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
int Backend::connectInternal()
|
|
|
|
{
|
2024-10-14 06:09:20 -04:00
|
|
|
return vanilla_start(0);
|
2024-10-09 14:42:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Backend::updateTouch(int x, int y)
|
2024-05-20 19:16:40 -04:00
|
|
|
{
|
2024-08-02 13:46:16 -04:00
|
|
|
vanilla_set_touch(x, y);
|
2024-05-23 21:47:04 -04:00
|
|
|
}
|
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::setButton(int button, int32_t value)
|
2024-06-19 14:58:02 -04:00
|
|
|
{
|
2024-08-02 13:46:16 -04:00
|
|
|
vanilla_set_button(button, value);
|
2024-06-19 14:58:02 -04:00
|
|
|
}
|
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::setRegion(int region)
|
2024-05-23 21:47:04 -04:00
|
|
|
{
|
2024-08-02 13:46:16 -04:00
|
|
|
vanilla_set_region(region);
|
|
|
|
}
|
2024-05-24 03:08:17 -04:00
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::setBatteryStatus(int status)
|
2024-08-02 13:46:16 -04:00
|
|
|
{
|
|
|
|
vanilla_set_battery_status(status);
|
|
|
|
}
|
|
|
|
|
2024-10-09 14:42:21 -04:00
|
|
|
void Backend::syncFutureCompleted()
|
2024-08-04 13:53:00 -04:00
|
|
|
{
|
|
|
|
QFutureWatcher<int> *watcher = static_cast<QFutureWatcher<int>*>(sender());
|
|
|
|
int r = watcher->result();
|
2024-08-02 13:46:16 -04:00
|
|
|
emit syncCompleted(r == VANILLA_SUCCESS);
|
2024-08-04 13:53:00 -04:00
|
|
|
watcher->deleteLater();
|
2024-08-02 13:46:16 -04:00
|
|
|
}
|
2024-05-23 22:47:32 -04:00
|
|
|
|
2024-08-02 13:46:16 -04:00
|
|
|
BackendPipe::BackendPipe(const QString &wirelessInterface, QObject *parent) : QObject(parent)
|
2024-05-24 11:37:54 -04:00
|
|
|
{
|
|
|
|
m_process = nullptr;
|
2024-08-02 13:46:16 -04:00
|
|
|
m_wirelessInterface = wirelessInterface;
|
2024-08-10 16:13:21 -04:00
|
|
|
|
|
|
|
m_process = new QProcess(this);
|
|
|
|
m_process->setReadChannel(QProcess::StandardError);
|
|
|
|
connect(m_process, &QProcess::readyReadStandardError, this, &BackendPipe::receivedData);
|
|
|
|
connect(m_process, &QProcess::finished, this, &BackendPipe::closed);
|
2024-05-24 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BackendPipe::~BackendPipe()
|
|
|
|
{
|
2024-08-12 23:22:51 -04:00
|
|
|
quit();
|
2024-05-24 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
2024-09-18 14:57:38 -04:00
|
|
|
void BackendPipe::start()
|
2024-08-10 16:13:21 -04:00
|
|
|
{
|
2024-09-18 14:57:38 -04:00
|
|
|
m_process->start(QStringLiteral("pkexec"), {pipeProcessFilename(), m_wirelessInterface});
|
2024-08-10 16:13:21 -04:00
|
|
|
}
|
2024-06-19 21:20:55 -04:00
|
|
|
|
2024-08-10 16:13:21 -04:00
|
|
|
QString BackendPipe::pipeProcessFilename()
|
|
|
|
{
|
|
|
|
return QDir(QCoreApplication::applicationDirPath()).filePath(QStringLiteral("vanilla-pipe"));
|
2024-05-24 11:37:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackendPipe::receivedData()
|
|
|
|
{
|
|
|
|
while (m_process->canReadLine()) {
|
|
|
|
QByteArray a = m_process->readLine().trimmed();
|
2024-06-19 21:20:55 -04:00
|
|
|
if (a == QByteArrayLiteral("READY")) {
|
2024-10-09 14:42:21 -04:00
|
|
|
emit pipeAvailable();
|
2024-05-24 11:37:54 -04:00
|
|
|
} else {
|
|
|
|
printf("%s\n", a.constData());
|
2024-05-24 03:08:17 -04:00
|
|
|
}
|
2024-05-23 22:47:32 -04:00
|
|
|
}
|
2024-08-12 23:22:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BackendPipe::quit()
|
|
|
|
{
|
|
|
|
if (m_process) {
|
2024-08-13 15:26:46 -04:00
|
|
|
m_process->write(QByteArrayLiteral("QUIT\n"));
|
2024-08-12 23:22:51 -04:00
|
|
|
m_process->waitForFinished();
|
|
|
|
m_process->deleteLater();
|
|
|
|
m_process = nullptr;
|
|
|
|
}
|
2024-10-09 14:42:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BackendViaInternalPipe::BackendViaInternalPipe(const QString &wirelessInterface, QObject *parent) : Backend(parent)
|
|
|
|
{
|
|
|
|
m_wirelessInterface = wirelessInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BackendViaInternalPipe::initInternal()
|
|
|
|
{
|
|
|
|
m_pipe = new BackendPipe(m_wirelessInterface, this);
|
|
|
|
connect(m_pipe, &BackendPipe::pipeAvailable, this, &BackendViaInternalPipe::ready);
|
2024-10-14 15:05:02 -04:00
|
|
|
connect(m_pipe, &BackendPipe::closed, this, &BackendViaInternalPipe::closed);
|
2024-10-09 14:42:21 -04:00
|
|
|
m_pipe->start();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BackendViaInternalPipe::syncInternal(uint16_t code)
|
|
|
|
{
|
|
|
|
return vanilla_sync(code, QHostAddress(QHostAddress::LocalHost).toIPv4Address());
|
|
|
|
}
|
|
|
|
|
|
|
|
int BackendViaInternalPipe::connectInternal()
|
|
|
|
{
|
2024-10-14 06:09:20 -04:00
|
|
|
return vanilla_start(QHostAddress(QHostAddress::LocalHost).toIPv4Address());
|
2024-10-09 14:42:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
BackendViaExternalPipe::BackendViaExternalPipe(const QHostAddress &udpServer, QObject *parent) : Backend(parent)
|
|
|
|
{
|
|
|
|
m_serverAddress = udpServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BackendViaExternalPipe::syncInternal(uint16_t code)
|
|
|
|
{
|
|
|
|
return vanilla_sync(code, m_serverAddress.toIPv4Address());
|
|
|
|
}
|
|
|
|
|
|
|
|
int BackendViaExternalPipe::connectInternal()
|
|
|
|
{
|
2024-10-14 06:09:20 -04:00
|
|
|
return vanilla_start(m_serverAddress.toIPv4Address());
|
2024-05-20 19:16:40 -04:00
|
|
|
}
|