diff --git a/dashboard/adb.cpp b/dashboard/adb.cpp index 7e84224e..c83e601e 100644 --- a/dashboard/adb.cpp +++ b/dashboard/adb.cpp @@ -20,9 +20,10 @@ #include #include -#include -#include -#include +#include +#include + +using namespace std::chrono_literals; template std::unique_ptr escape_sandbox(const std::string & executable, Args &&... args_orig) @@ -48,79 +49,78 @@ std::unique_ptr escape_sandbox(const std::string & executable, Args && return process; } -std::shared_ptr>> 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>>(); +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 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 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 adb::device::install(const std::filesystem::path & path) @@ -133,6 +133,32 @@ std::unique_ptr adb::device::uninstall(const std::string & app) return escape_sandbox("adb", "-s", _serial, "uninstall", app); } +std::vector 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 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 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" diff --git a/dashboard/adb.h b/dashboard/adb.h index 3840d4a4..05f7b943 100644 --- a/dashboard/adb.h +++ b/dashboard/adb.h @@ -18,17 +18,20 @@ #pragma once -#include +#include +#include +#include #include #include #include #include -#include 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 install(const std::filesystem::path & path); std::unique_ptr 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 installed_apps(); }; - std::shared_ptr>> devices(); +private: + QTimer poll_devices_timer; + std::unique_ptr poll_devices_process; + std::vector _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 & 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 &); }; diff --git a/dashboard/main_window.cpp b/dashboard/main_window.cpp index 79b90606..ff2f6316 100644 --- a/dashboard/main_window.cpp +++ b/dashboard/main_window.cpp @@ -28,13 +28,13 @@ #include #include +#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 & 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 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> & x) { return x.first == device.serial(); })) + { + qDebug() << "Detected" << QString::fromStdString(device.serial()); + + std::unique_ptr device_action = std::make_unique(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? diff --git a/dashboard/main_window.h b/dashboard/main_window.h index 56923085..ba37cd06 100644 --- a/dashboard/main_window.h +++ b/dashboard/main_window.h @@ -26,6 +26,7 @@ #include #include +#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> 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(); diff --git a/dashboard/main_window.ui b/dashboard/main_window.ui index 26310ecd..6f0b5873 100644 --- a/dashboard/main_window.ui +++ b/dashboard/main_window.ui @@ -7,7 +7,7 @@ 0 0 898 - 701 + 796 @@ -218,7 +218,7 @@ - + @@ -231,6 +231,13 @@ + + + + Connect by USB + + + @@ -450,8 +457,8 @@ - 20 - 40 + 40 + 20 diff --git a/dashboard/wizard.cpp b/dashboard/wizard.cpp index 9893d48e..ffbe96ac 100644 --- a/dashboard/wizard.cpp +++ b/dashboard/wizard.cpp @@ -32,7 +32,6 @@ #include #include #include -#include #include #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>::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& 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.")); diff --git a/dashboard/wizard.h b/dashboard/wizard.h index 47186aef..dcaad508 100644 --- a/dashboard/wizard.h +++ b/dashboard/wizard.h @@ -28,7 +28,6 @@ #include #include #include -#include #include namespace Ui @@ -65,13 +64,9 @@ class wizard : public QWizard std::string latest_release; - std::shared_ptr>> android_devices_promise; - QFuture> android_devices_future; - QFutureWatcher> android_devices_future_watcher; + adb adb_service; std::vector android_devices; - QTimer poll_devices_timer; - // std::unique_ptr process_adb_devices; std::unique_ptr 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&); void start_install(); void on_install_finished(int exit_code, QProcess::ExitStatus exit_status);