From cc1308c7dffa8735bcb8f0a021f9755588efdf30 Mon Sep 17 00:00:00 2001 From: grekiki Date: Mon, 1 Feb 2021 19:46:03 +0100 Subject: [PATCH] Multithreaded ssh activation (#19988) No more lag on SSH enable/disable --- SConstruct | 2 +- selfdrive/ui/qt/offroad/networking.cc | 30 +++++++++++++++++++++------ selfdrive/ui/qt/widgets/toggle.cc | 25 ++++++++++++++++++++-- selfdrive/ui/qt/widgets/toggle.hpp | 6 +++++- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/SConstruct b/SConstruct index cdab6220ba..bad66142aa 100644 --- a/SConstruct +++ b/SConstruct @@ -265,7 +265,7 @@ qt_env = None if arch in ["x86_64", "Darwin", "larch64"]: qt_env = env.Clone() - qt_modules = ["Widgets", "Gui", "Core", "DBus", "Multimedia", "Network"] + qt_modules = ["Widgets", "Gui", "Core", "DBus", "Multimedia", "Network", "Concurrent"] qt_libs = [] if arch == "Darwin": diff --git a/selfdrive/ui/qt/offroad/networking.cc b/selfdrive/ui/qt/offroad/networking.cc index 53bf945f34..7e990b64d2 100644 --- a/selfdrive/ui/qt/offroad/networking.cc +++ b/selfdrive/ui/qt/offroad/networking.cc @@ -5,6 +5,7 @@ #include #include #include +#include #include "networking.hpp" @@ -245,7 +246,6 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid QHBoxLayout* enableSSHLayout = new QHBoxLayout(this); enableSSHLayout->addWidget(new QLabel("Enable SSH", this)); toggle_switch_SSH = new Toggle(this); - toggle_switch_SSH->immediateOffset = 40; toggle_switch_SSH->setFixedSize(150, 100); if (isSSHEnabled()) { toggle_switch_SSH->togglePosition(); @@ -293,9 +293,15 @@ bool AdvancedNetworking::isSSHEnabled(){ void AdvancedNetworking::refresh(){ ipLabel->setText(wifi->ipv4_address); + // Don't refresh while changing SSH state + if(!toggle_switch_SSH->getEnabled()){ + return; + } if (toggle_switch_SSH->on != isSSHEnabled()) { toggle_switch_SSH->togglePosition(); } + //Qt can be lazy + repaint(); } void AdvancedNetworking::toggleTethering(int enable) { @@ -306,16 +312,28 @@ void AdvancedNetworking::toggleTethering(int enable) { } editPasswordButton->setEnabled(!enable); } + +void enableSSH(Toggle* toggle_switch_SSH){ + system("sudo systemctl enable ssh"); + system("sudo systemctl start ssh"); + toggle_switch_SSH->setEnabled(true); +} + +void disableSSH(Toggle* toggle_switch_SSH){ + system("sudo systemctl stop ssh"); + system("sudo systemctl disable ssh"); + toggle_switch_SSH->setEnabled(true); +} + void AdvancedNetworking::toggleSSH(int enable) { + toggle_switch_SSH->setEnabled(false); if (enable) { - system("sudo systemctl enable ssh"); - system("sudo systemctl start ssh"); + QtConcurrent::run(enableSSH, toggle_switch_SSH); } else { - system("sudo systemctl stop ssh"); - system("sudo systemctl disable ssh"); - + QtConcurrent::run(disableSSH, toggle_switch_SSH); } } + void AdvancedNetworking::receiveText(QString text){ wifi->changeTetheringPassword(text); s->setCurrentIndex(1); diff --git a/selfdrive/ui/qt/widgets/toggle.cc b/selfdrive/ui/qt/widgets/toggle.cc index 76241fcbd0..551630dd7b 100644 --- a/selfdrive/ui/qt/widgets/toggle.cc +++ b/selfdrive/ui/qt/widgets/toggle.cc @@ -10,6 +10,9 @@ _anim(new QPropertyAnimation(this, "offset_circle", this)) _x_circle = _radius; _y_circle = _radius; _y_rect = (_height - _height_rect)/2; + circleColor = QColor(0xffffff); // placeholder + green = QColor(0xffffff); // placeholder + setEnabled(true); } void Toggle::paintEvent(QPaintEvent *e) { @@ -19,7 +22,7 @@ void Toggle::paintEvent(QPaintEvent *e) { p.setRenderHint(QPainter::Antialiasing, true); // Draw toggle background left - p.setBrush(QColor(0x33ab4c)); + p.setBrush(green); p.drawRoundedRect(QRect(0, _y_rect, _x_circle + _radius, _height_rect), _height_rect/2, _height_rect/2); // Draw toggle background right @@ -27,11 +30,14 @@ void Toggle::paintEvent(QPaintEvent *e) { p.drawRoundedRect(QRect(_x_circle - _radius, _y_rect, width() - (_x_circle - _radius), _height_rect), _height_rect/2, _height_rect/2); // Draw toggle circle - p.setBrush(QColor(0xfafafa)); + p.setBrush(circleColor); p.drawEllipse(QRectF(_x_circle - _radius, _y_circle - _radius, 2 * _radius, 2 * _radius)); } void Toggle::mouseReleaseEvent(QMouseEvent *e) { + if(!enabled){ + return; + } const int left = _radius; const int right = width() - _radius; if(_x_circle != left && _x_circle != right){ @@ -58,3 +64,18 @@ void Toggle::togglePosition() { void Toggle::enterEvent(QEvent *e) { QAbstractButton::enterEvent(e); } + +bool Toggle::getEnabled(){ + return enabled; +} + +void Toggle::setEnabled(bool value){ + enabled = value; + if(value){ + circleColor.setRgb(0xfafafa); + green.setRgb(0x33ab4c); + }else{ + circleColor.setRgb(0x888888); + green.setRgb(0x227722); + } +} \ No newline at end of file diff --git a/selfdrive/ui/qt/widgets/toggle.hpp b/selfdrive/ui/qt/widgets/toggle.hpp index ad2e158ba8..4549245dc7 100644 --- a/selfdrive/ui/qt/widgets/toggle.hpp +++ b/selfdrive/ui/qt/widgets/toggle.hpp @@ -19,7 +19,8 @@ public: _x_circle = o; update(); } - + bool getEnabled(); + void setEnabled(bool value); protected: void paintEvent(QPaintEvent*) override; @@ -27,6 +28,9 @@ protected: void enterEvent(QEvent*) override; private: + QColor circleColor; + QColor green; + bool enabled = true; int _x_circle, _y_circle; int _height, _radius; int _height_rect, _y_rect;