Connect via USB

This commit is contained in:
Guillaume Meunier
2024-10-10 23:26:11 +02:00
parent b11bf029f8
commit 14ac4d6e66
7 changed files with 292 additions and 131 deletions
+105 -70
View File
@@ -20,9 +20,10 @@
#include <QDebug>
#include <QProcess>
#include <QPromise>
#include <exception>
#include <memory>
#include <chrono>
#include <vector>
using namespace std::chrono_literals;
template <typename... Args>
std::unique_ptr<QProcess> escape_sandbox(const std::string & executable, Args &&... args_orig)
@@ -48,79 +49,78 @@ std::unique_ptr<QProcess> escape_sandbox(const std::string & executable, Args &&
return process;
}
std::shared_ptr<QPromise<std::vector<adb::device>>> adb::devices()
adb::adb()
{
auto process = escape_sandbox("adb", "devices", "-l");
poll_devices_timer.setInterval(500ms);
connect(&poll_devices_timer, &QTimer::timeout, this, &adb::on_poll_devices_timeout);
process->start();
on_poll_devices_timeout();
poll_devices_timer.start();
}
auto promise = std::make_shared<QPromise<std::vector<adb::device>>>();
void adb::on_poll_devices_timeout()
{
if (poll_devices_process)
return;
auto p = process.get();
poll_devices_process = escape_sandbox("adb", "devices", "-l");
connect(poll_devices_process.get(), &QProcess::finished, this, &adb::on_poll_devices_process_finished);
poll_devices_process->start();
}
QObject::connect(p, &QProcess::finished, [p = std::move(process), promise](int exitCode, QProcess::ExitStatus exitStatus) {
try
void adb::on_poll_devices_process_finished(int exit_code, QProcess::ExitStatus exit_status)
{
if (exit_code != 0 or exit_status != QProcess::NormalExit)
{
qDebug() << "adb devices exited with code" << exit_code << ", status" << exit_status;
QString out = poll_devices_process->readAllStandardOutput();
qDebug() << "stdout:" << out;
QString err = poll_devices_process->readAllStandardError();
qDebug() << "stderr:" << err;
}
else
{
auto out = poll_devices_process->readAllStandardOutput();
auto lines = QString{out}.split('\n');
if (not lines.empty())
lines.pop_front();
std::vector<adb::device> devs;
for (auto & line: lines)
{
if (exitCode != 0 or exitStatus != QProcess::NormalExit)
auto words = line.split(' ', Qt::SkipEmptyParts);
device dev;
if (words.empty())
continue;
dev._serial = words.front().toStdString();
words.pop_front();
if (words.empty())
continue;
dev._state = words.front().toStdString();
words.pop_front();
for (auto & word: words)
{
qDebug() << "adb devices exited with code" << exitCode << ", status" << exitStatus;
QString out = p->readAllStandardOutput();
qDebug() << "stdout:" << out;
QString err = p->readAllStandardError();
qDebug() << "stderr:" << err;
throw std::runtime_error("adb devices failed");
}
else
{
auto out = p->readAllStandardOutput();
auto lines = QString{out}.split('\n');
if (not lines.empty())
lines.pop_front();
std::vector<adb::device> devs;
for (auto & line: lines)
auto idx = word.indexOf(':');
if (idx >= 0)
{
auto words = line.split(' ', Qt::SkipEmptyParts);
device dev;
if (words.empty())
continue;
dev._serial = words.front().toStdString();
words.pop_front();
if (words.empty())
continue;
dev._state = words.front().toStdString();
words.pop_front();
for (auto & word: words)
{
auto idx = word.indexOf(':');
if (idx >= 0)
{
dev._properties[word.left(idx).toStdString()] = word.mid(idx + 1).toStdString();
}
}
devs.push_back(dev);
dev._properties[word.left(idx).toStdString()] = word.mid(idx + 1).toStdString();
}
promise->addResult(std::move(devs));
}
}
catch (...)
{
promise->setException(std::current_exception());
devs.push_back(dev);
}
promise->finish();
});
_android_devices = std::move(devs);
android_devices_changed(_android_devices);
}
return promise;
poll_devices_process.release()->deleteLater();
}
std::unique_ptr<QProcess> adb::device::install(const std::filesystem::path & path)
@@ -133,6 +133,32 @@ std::unique_ptr<QProcess> adb::device::uninstall(const std::string & app)
return escape_sandbox("adb", "-s", _serial, "uninstall", app);
}
std::vector<std::string> adb::device::installed_apps()
{
auto process = escape_sandbox("adb", "-s", _serial, "shell", "pm", "list", "packages");
process->start();
process->waitForFinished();
if (process->exitCode() != 0 or process->exitStatus() != QProcess::NormalExit)
throw std::runtime_error("Cannot list packages");
auto out = process->readAllStandardOutput();
auto lines = QString{out}.split('\n');
std::vector<std::string> apps;
for (auto & line: lines)
{
QString prefix = "package:";
if (line.startsWith(prefix))
{
apps.push_back(line.toStdString().substr(prefix.size()));
}
}
return apps;
}
// void adb::device::start(const std::string& app)
// {
// auto process = escape_sandbox("adb", "-s", _serial, "shell", "am", "start", app);
@@ -147,10 +173,19 @@ std::unique_ptr<QProcess> adb::device::uninstall(const std::string & app)
// process->waitForFinished();
// }
//
// void adb::device::start(const std::string& app, const std::string& action, const std::string& uri)
// {
// // action: "android.intent.action.VIEW" or "android.intent.action.MAIN"
// auto process = escape_sandbox("adb", "-s", _serial, "shell", "am", "start", "-a", action, "-d", uri, app);
// process->start();
// process->waitForFinished();
// }
void adb::device::start(const std::string & app, const std::string & action, const std::string & uri)
{
// action: "android.intent.action.VIEW" or "android.intent.action.MAIN"
auto process = escape_sandbox("adb", "-s", _serial, "shell", "am", "start", "-a", action, "-d", uri, app);
process->start();
process->waitForFinished();
}
void adb::device::reverse_forward(int local_port, int device_port)
{
auto process = escape_sandbox("adb", "-s", _serial, "reverse", "tcp:" + std::to_string(local_port), "tcp:" + std::to_string(device_port));
process->start();
process->waitForFinished();
}
#include "moc_adb.cpp"
+41 -7
View File
@@ -18,17 +18,20 @@
#pragma once
#include <QFuture>
#include <QObject>
#include <QProcess>
#include <QTimer>
#include <filesystem>
#include <map>
#include <memory>
#include <string>
#include <vector>
class QProcess;
class adb
class adb : public QObject
{
Q_OBJECT
public:
class device
{
@@ -65,12 +68,43 @@ public:
return _serial == other._serial;
}
operator bool() const
{
return _serial != "";
}
std::unique_ptr<QProcess> install(const std::filesystem::path & path);
std::unique_ptr<QProcess> uninstall(const std::string & app);
// void start(const std::string& app);
// void stop(const std::string& app);
// void start(const std::string& app, const std::string& action, const std::string& uri);
void start(const std::string & app, const std::string & action, const std::string & uri);
void reverse_forward(int local_port, int device_port);
std::vector<std::string> installed_apps();
};
std::shared_ptr<QPromise<std::vector<adb::device>>> devices();
private:
QTimer poll_devices_timer;
std::unique_ptr<QProcess> poll_devices_process;
std::vector<adb::device> _android_devices;
void on_poll_devices_timeout();
void on_poll_devices_process_finished(int exit_code, QProcess::ExitStatus exit_status);
public:
adb();
const std::vector<adb::device> & devices() const
{
return _android_devices;
}
void start()
{
poll_devices_timer.start();
}
void stop()
{
poll_devices_timer.stop();
}
Q_SIGNALS:
void android_devices_changed(const std::vector<adb::device> &);
};
+122 -2
View File
@@ -28,13 +28,13 @@
#include <chrono>
#include <cmath>
#include "adb.h"
#include "gui_config.h"
#include "settings.h"
#include "ui_main_window.h"
#include "wivrn_server.h"
#include "wizard.h"
#include "gui_config.h"
Q_LOGGING_CATEGORY(wivrn_log_category, "wivrn")
enum class server_state
@@ -122,6 +122,53 @@ main_window::main_window()
QGuiApplication::clipboard()->setText(ui->label_steam_command->text());
});
usb_device_menu = new QMenu(this);
ui->button_usb->setMenu(usb_device_menu);
connect(&adb_service, &adb::android_devices_changed, this, [this](const std::vector<adb::device> & devices) {
ui->button_usb->setDisabled(devices.empty());
if (devices.empty())
ui->button_usb->setToolTip(tr("No device detected"));
else
ui->button_usb->setToolTip("");
std::vector<std::string> to_be_removed;
for (auto & [serial, action]: usb_actions)
{
if (std::ranges::none_of(devices, [&](const adb::device & x) { return x.serial() == serial; }))
{
qDebug() << "Removed" << QString::fromStdString(serial);
usb_device_menu->removeAction(action.get());
to_be_removed.push_back(serial);
}
}
for (const auto & serial: to_be_removed)
{
usb_actions.erase(serial);
}
for (auto & device: devices)
{
if (std::ranges::none_of(usb_actions, [&](const std::pair<const std::string, std::unique_ptr<QAction>> & x) { return x.first == device.serial(); }))
{
qDebug() << "Detected" << QString::fromStdString(device.serial());
std::unique_ptr<QAction> device_action = std::make_unique<QAction>(this);
device_action->setText(QString::fromStdString(device.properties().at("model")));
usb_device_menu->addAction(device_action.get());
device_action->setData(QString::fromStdString(device.serial()));
connect(device_action.get(), &QAction::triggered, this, [this, serial = device.serial()]() {
on_action_usb(serial);
});
usb_actions.insert(std::make_pair(device.serial(), std::move(device_action)));
}
}
});
retranslate();
}
@@ -221,11 +268,13 @@ void main_window::setVisible(bool visible)
{
action_show.setVisible(false);
action_hide.setVisible(true);
adb_service.start();
}
else
{
action_show.setVisible(true);
action_hide.setVisible(false);
adb_service.stop();
}
QMainWindow::setVisible(visible);
@@ -266,6 +315,7 @@ void main_window::on_headset_connected_changed(bool connected)
ui->button_disconnect->setVisible(connected);
ui->button_wizard->setHidden(connected);
ui->button_details->setVisible(connected);
ui->button_usb->setHidden(connected);
ui->headset_properties->setVisible(connected and ui->button_details->isChecked());
ui->label_how_to_connect->setHidden(connected);
@@ -506,6 +556,76 @@ void main_window::on_action_wizard()
wizard_window->exec();
}
void main_window::on_action_usb(const std::string & serial)
{
auto devs = adb_service.devices();
if (devs.empty())
return;
adb::device dev;
for (auto & i: adb_service.devices())
{
if (i.serial() == serial)
{
dev = i;
break;
}
}
if (!dev)
return;
// TODO: in another thread
bool ok = false;
for (auto & app: dev.installed_apps())
{
if (app.starts_with("org.meumeu.wivrn.") or app == "org.meumeu.wivrn")
{
dev.reverse_forward(9757, 9757);
dev.start(app, "android.intent.action.VIEW", "wivrn+tcp://127.0.0.1:9757");
ok = true;
break;
}
}
if (not ok)
{
QMessageBox msgbox /*(this)*/;
msgbox.setIcon(QMessageBox::Critical);
msgbox.setText(tr("The WiVRn app is not installed on your headset.\nDo you want to install it?"));
msgbox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
#ifdef WORKAROUND_QTBUG_90005
setEnabled(false);
#endif
msgbox.exec();
#ifdef WORKAROUND_QTBUG_90005
setEnabled(true);
#endif
if (msgbox.clickedButton() == msgbox.button(QMessageBox::Yes))
{
QMessageBox msgbox /*(this)*/;
msgbox.setIcon(QMessageBox::Critical);
msgbox.setText("Not implemented");
msgbox.setStandardButtons(QMessageBox::Close);
#ifdef WORKAROUND_QTBUG_90005
setEnabled(false);
#endif
msgbox.exec();
#ifdef WORKAROUND_QTBUG_90005
setEnabled(true);
#endif
}
}
}
void main_window::start_server()
{
// TODO activate by dbus?
+7
View File
@@ -26,6 +26,7 @@
#include <QTimer>
#include <QVariantMap>
#include "adb.h"
#include "wivrn_qdbus_types.h"
Q_DECLARE_LOGGING_CATEGORY(wivrn_log_category)
@@ -62,6 +63,11 @@ class main_window : public QMainWindow
QProcess * server_process = nullptr;
QTimer * server_process_timeout = nullptr;
QMenu * usb_device_menu;
std::map<std::string, std::unique_ptr<QAction>> usb_actions;
adb adb_service;
public:
main_window();
~main_window();
@@ -95,6 +101,7 @@ private:
void on_action_settings();
void on_action_wizard();
void on_action_usb(const std::string & serial);
void start_server();
void stop_server();
void disconnect_client();
+11 -4
View File
@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>898</width>
<height>701</height>
<height>796</height>
</rect>
</property>
<property name="windowTitle">
@@ -218,7 +218,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0,0,0">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0,0,0,0">
<item>
<widget class="QLabel" name="label_client_status">
<property name="font">
@@ -231,6 +231,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="button_usb">
<property name="text">
<string>Connect by USB</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="button_wizard">
<property name="text">
@@ -450,8 +457,8 @@
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
+4 -40
View File
@@ -32,7 +32,6 @@
#include <QStandardPaths>
#include <QUrl>
#include <QWizard>
#include <algorithm>
#include <nlohmann/json.hpp>
#include "adb.h"
@@ -182,17 +181,8 @@ wizard::wizard() :
}
// Detect connected android devices
poll_devices_timer.setInterval(std::chrono::milliseconds(500));
connect(&poll_devices_timer, &QTimer::timeout, this, [this]() {
if (android_devices_future.isFinished())
{
android_devices_promise = adb{}.devices();
android_devices_future = android_devices_promise->future();
android_devices_future_watcher.setFuture(android_devices_future);
}
});
poll_devices_timer.start();
connect(&android_devices_future_watcher, &QFutureWatcher<std::vector<adb::device>>::finished, this, &wizard::on_android_devices_future_finished);
connect(&adb_service, &adb::android_devices_changed, this, &wizard::on_android_device_list_changed);
on_android_device_list_changed(adb_service.devices());
}
void wizard::on_latest_release_finished()
@@ -392,7 +382,6 @@ void wizard::on_page_changed(int id)
case wizard_page::sideload_devmode:
setButtonLayout({QWizard::Stretch, QWizard::BackButton, QWizard::NextButton, QWizard::CancelButton});
on_adb_device_list_changed();
return;
case wizard_page::sideload_install:
@@ -580,36 +569,11 @@ void wizard::on_download_finished()
}
}
void wizard::on_android_devices_future_finished()
void wizard::on_android_device_list_changed(const std::vector<adb::device>& new_devs)
{
auto old_devs = android_devices;
android_devices = android_devices_future.result();
android_devices = new_devs;
bool changed = false;
for (auto & i: android_devices)
{
if (i.state() != "device")
continue;
if (std::ranges::find(old_devs, i) == old_devs.end())
changed = true;
}
for (auto & i: old_devs)
{
if (i.state() != "device")
continue;
if (std::ranges::find(android_devices, i) == android_devices.end())
changed = true;
}
if (changed)
on_adb_device_list_changed();
}
void wizard::on_adb_device_list_changed()
{
if (android_devices.empty())
{
ui->label_device_detected->setText(tr("No device detected."));
+2 -8
View File
@@ -28,7 +28,6 @@
#include <QStandardItemModel>
#include <QTimer>
#include <QWizard>
#include <memory>
#include <string>
namespace Ui
@@ -65,13 +64,9 @@ class wizard : public QWizard
std::string latest_release;
std::shared_ptr<QPromise<std::vector<adb::device>>> android_devices_promise;
QFuture<std::vector<adb::device>> android_devices_future;
QFutureWatcher<std::vector<adb::device>> android_devices_future_watcher;
adb adb_service;
std::vector<adb::device> android_devices;
QTimer poll_devices_timer;
// std::unique_ptr<QProcess> process_adb_devices;
std::unique_ptr<QProcess> process_adb_install;
public:
@@ -91,8 +86,7 @@ public:
void on_download_error(QNetworkReply::NetworkError code);
void on_download_finished();
void on_android_devices_future_finished();
void on_adb_device_list_changed();
void on_android_device_list_changed(const std::vector<adb::device>&);
void start_install();
void on_install_finished(int exit_code, QProcess::ExitStatus exit_status);