vanilla/app/backend.cpp

199 lines
4.5 KiB
C++
Raw Permalink Normal View History

#include "backend.h"
#include <QBuffer>
#include <QCoreApplication>
#include <QDir>
#include <QMessageBox>
2024-08-02 13:46:16 -04:00
#include <QNetworkDatagram>
#include <QtConcurrent/QtConcurrent>
2024-08-02 13:46:16 -04:00
#include <arpa/inet.h>
2024-05-23 22:47:32 -04:00
#include <fcntl.h>
#include <signal.h>
2024-05-23 22:47:32 -04:00
#include <stdio.h>
#include <unistd.h>
#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;
}
vanilla_free_event(&event);
2024-05-18 15:55:00 -04:00
}
}
Backend::Backend(QObject *parent) : QObject(parent)
{
2024-08-02 13:46:16 -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-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-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-10-09 14:42:21 -04:00
void Backend::sync(uint16_t code)
{
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-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-10-09 14:42:21 -04:00
void Backend::connectToConsole()
{
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-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-08-02 13:46:16 -04:00
vanilla_set_button(button, value);
}
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-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()
{
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);
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)
{
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);
}
BackendPipe::~BackendPipe()
{
quit();
}
void BackendPipe::start()
2024-08-10 16:13:21 -04:00
{
m_process->start(QStringLiteral("pkexec"), {pipeProcessFilename(), m_wirelessInterface});
2024-08-10 16:13:21 -04:00
}
2024-08-10 16:13:21 -04:00
QString BackendPipe::pipeProcessFilename()
{
return QDir(QCoreApplication::applicationDirPath()).filePath(QStringLiteral("vanilla-pipe"));
}
void BackendPipe::receivedData()
{
while (m_process->canReadLine()) {
QByteArray a = m_process->readLine().trimmed();
if (a == QByteArrayLiteral("READY")) {
2024-10-09 14:42:21 -04:00
emit pipeAvailable();
} else {
printf("%s\n", a.constData());
}
2024-05-23 22:47:32 -04:00
}
}
void BackendPipe::quit()
{
if (m_process) {
2024-08-13 15:26:46 -04:00
m_process->write(QByteArrayLiteral("QUIT\n"));
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);
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
}