Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: singleton uistate #22789

Merged
merged 14 commits into from
Dec 15, 2021
3 changes: 1 addition & 2 deletions selfdrive/ui/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ if arch in ['x86_64', 'Darwin'] or GetOption('extras'):
replay_lib = qt_env.Library("qt_replay", replay_lib_src, LIBS=base_libs)
replay_libs = [replay_lib, 'avutil', 'avcodec', 'avformat', 'bz2', 'curl', 'yuv'] + qt_libs
qt_env.Program("replay/replay", ["replay/main.cc"], LIBS=replay_libs)

qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11'])
qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11', 'zmq', 'visionipc', 'messaging'])

if GetOption('test'):
qt_env.Program('replay/tests/test_replay', ['replay/tests/test_runner.cc', 'replay/tests/test_replay.cc'], LIBS=[replay_libs])
Expand Down
6 changes: 1 addition & 5 deletions selfdrive/ui/qt/home.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {

sidebar = new Sidebar(this);
main_layout->addWidget(sidebar);
QObject::connect(this, &HomeWindow::update, sidebar, &Sidebar::updateState);
QObject::connect(sidebar, &Sidebar::openSettings, this, &HomeWindow::openSettings);

slayout = new QStackedLayout();
Expand All @@ -31,15 +30,13 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {
onroad = new OnroadWindow(this);
slayout->addWidget(onroad);

QObject::connect(this, &HomeWindow::update, onroad, &OnroadWindow::updateStateSignal);
QObject::connect(this, &HomeWindow::offroadTransitionSignal, onroad, &OnroadWindow::offroadTransitionSignal);

driver_view = new DriverViewWindow(this);
connect(driver_view, &DriverViewWindow::done, [=] {
showDriverView(false);
});
slayout->addWidget(driver_view);
setAttribute(Qt::WA_NoSystemBackground);
QObject::connect(uiState(), &UIState::offroadTransition, this, &HomeWindow::offroadTransition);
}

void HomeWindow::showSidebar(bool show) {
Expand All @@ -53,7 +50,6 @@ void HomeWindow::offroadTransition(bool offroad) {
} else {
slayout->setCurrentWidget(onroad);
}
emit offroadTransitionSignal(offroad);
}

void HomeWindow::showDriverView(bool show) {
Expand Down
4 changes: 0 additions & 4 deletions selfdrive/ui/qt/home.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ class HomeWindow : public QWidget {
void openSettings();
void closeSettings();

// forwarded signals
void update(const UIState &s);
void offroadTransitionSignal(bool offroad);

public slots:
void offroadTransition(bool offroad);
void showDriverView(bool show);
Expand Down
6 changes: 3 additions & 3 deletions selfdrive/ui/qt/maps/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void MapWindow::initLayers() {
}

void MapWindow::timerUpdate() {
if (!QUIState::ui_state.scene.started) {
if (!uiState()->scene.started) {
return;
}

Expand Down Expand Up @@ -387,7 +387,7 @@ void MapInstructions::updateDistance(float d) {
d = std::max(d, 0.0f);
QString distance_str;

if (QUIState::ui_state.scene.is_metric) {
if (uiState()->scene.is_metric) {
if (d > 500) {
distance_str.setNum(d / 1000, 'f', 1);
distance_str += " km";
Expand Down Expand Up @@ -620,7 +620,7 @@ void MapETA::updateETA(float s, float s_typical, float d) {
// Distance
QString distance_str;
float num = 0;
if (QUIState::ui_state.scene.is_metric) {
if (uiState()->scene.is_metric) {
num = d / 1000.0;
distance_unit->setText("km");
} else {
Expand Down
14 changes: 7 additions & 7 deletions selfdrive/ui/qt/offroad/settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ TogglesPanel::TogglesPanel(SettingsWindow *parent) : ListWidget(parent) {
bool locked = params.getBool((param + "Lock").toStdString());
toggle->setEnabled(!locked);
if (!locked) {
connect(parent, &SettingsWindow::offroadTransition, toggle, &ParamControl::setEnabled);
connect(uiState(), &UIState::offroadTransition, toggle, &ParamControl::setEnabled);
}
addItem(toggle);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ DevicePanel::DevicePanel(SettingsWindow *parent) : ListWidget(parent) {
addItem(regulatoryBtn);
}

QObject::connect(parent, &SettingsWindow::offroadTransition, [=](bool offroad) {
QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) {
for (auto btn : findChildren<ButtonControl *>()) {
btn->setEnabled(offroad);
}
Expand Down Expand Up @@ -198,10 +198,10 @@ void DevicePanel::updateCalibDescription() {
}

void DevicePanel::reboot() {
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) {
if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
if (ConfirmationDialog::confirm("Are you sure you want to reboot?", this)) {
// Check engaged again in case it changed while the dialog was open
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) {
if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
Params().putBool("DoReboot", true);
}
}
Expand All @@ -211,10 +211,10 @@ void DevicePanel::reboot() {
}

void DevicePanel::poweroff() {
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) {
if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
if (ConfirmationDialog::confirm("Are you sure you want to power off?", this)) {
// Check engaged again in case it changed while the dialog was open
if (QUIState::ui_state.status == UIStatus::STATUS_DISENGAGED) {
if (uiState()->status == UIStatus::STATUS_DISENGAGED) {
Params().putBool("DoShutdown", true);
}
}
Expand Down Expand Up @@ -247,7 +247,7 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) {
params.putBool("DoUninstall", true);
}
});
connect(parent, SIGNAL(offroadTransition(bool)), uninstallBtn, SLOT(setEnabled(bool)));
connect(uiState(), &UIState::offroadTransition, uninstallBtn, &QPushButton::setEnabled);

QWidget *widgets[] = {versionLbl, lastUpdateLbl, updateBtn, gitBranchLbl, gitCommitLbl, osVersionLbl, uninstallBtn};
for (QWidget* w : widgets) {
Expand Down
1 change: 0 additions & 1 deletion selfdrive/ui/qt/offroad/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class SettingsWindow : public QFrame {

signals:
void closeSettings();
void offroadTransition(bool offroad);
void reviewTrainingGuide();
void showDriverView();

Expand Down
14 changes: 7 additions & 7 deletions selfdrive/ui/qt/onroad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) {
alerts->raise();

setAttribute(Qt::WA_OpaquePaintEvent);
QObject::connect(this, &OnroadWindow::updateStateSignal, this, &OnroadWindow::updateState);
QObject::connect(this, &OnroadWindow::offroadTransitionSignal, this, &OnroadWindow::offroadTransition);
QObject::connect(uiState(), &UIState::uiUpdate, this, &OnroadWindow::updateState);
QObject::connect(uiState(), &UIState::offroadTransition, this, &OnroadWindow::offroadTransition);
}

void OnroadWindow::updateState(const UIState &s) {
Expand Down Expand Up @@ -76,10 +76,10 @@ void OnroadWindow::mousePressEvent(QMouseEvent* e) {
void OnroadWindow::offroadTransition(bool offroad) {
#ifdef ENABLE_MAPS
if (!offroad) {
if (map == nullptr && (QUIState::ui_state.has_prime || !MAPBOX_TOKEN.isEmpty())) {
if (map == nullptr && (uiState()->has_prime || !MAPBOX_TOKEN.isEmpty())) {
MapWindow * m = new MapWindow(get_mapbox_settings());
m->setFixedWidth(topWidget(this)->width() / 2);
QObject::connect(this, &OnroadWindow::offroadTransitionSignal, m, &MapWindow::offroadTransition);
QObject::connect(uiState(), &UIState::offroadTransition, m, &MapWindow::offroadTransition);
split->addWidget(m, 0, Qt::AlignRight);
map = m;
}
Expand Down Expand Up @@ -274,7 +274,7 @@ void NvgWindow::initializeGL() {
void NvgWindow::updateFrameMat(int w, int h) {
CameraViewWidget::updateFrameMat(w, h);

UIState *s = &QUIState::ui_state;
UIState *s = uiState();
s->fb_w = w;
s->fb_h = h;
auto intrinsic_matrix = s->wide_camera ? ecam_intrinsic_matrix : fcam_intrinsic_matrix;
Expand Down Expand Up @@ -348,7 +348,7 @@ void NvgWindow::drawLead(QPainter &painter, const cereal::ModelDataV2::LeadDataV
void NvgWindow::paintGL() {
CameraViewWidget::paintGL();

UIState *s = &QUIState::ui_state;
UIState *s = uiState();
if (s->scene.world_objects_visible) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
Expand Down Expand Up @@ -379,6 +379,6 @@ void NvgWindow::paintGL() {
void NvgWindow::showEvent(QShowEvent *event) {
CameraViewWidget::showEvent(event);

ui_update_params(&QUIState::ui_state);
ui_update_params(uiState());
prev_draw_t = millis_since_boot();
}
4 changes: 0 additions & 4 deletions selfdrive/ui/qt/onroad.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,6 @@ class OnroadWindow : public QWidget {
QWidget *map = nullptr;
QHBoxLayout* split;

signals:
void updateStateSignal(const UIState &s);
void offroadTransitionSignal(bool offroad);

private slots:
void offroadTransition(bool offroad);
void updateState(const UIState &s);
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/ui/qt/request_repeater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con
timer = new QTimer(this);
timer->setTimerType(Qt::VeryCoarseTimer);
QObject::connect(timer, &QTimer::timeout, [=]() {
if ((!QUIState::ui_state.scene.started || while_onroad) && QUIState::ui_state.awake && !active()) {
if ((!uiState()->scene.started || while_onroad) && uiState()->awake && !active()) {
sendRequest(requestURL);
}
});
Expand Down
2 changes: 2 additions & 0 deletions selfdrive/ui/qt/sidebar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Sidebar::Sidebar(QWidget *parent) : QFrame(parent) {
setAttribute(Qt::WA_OpaquePaintEvent);
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
setFixedWidth(300);

QObject::connect(uiState(), &UIState::uiUpdate, this, &Sidebar::updateState);
}

void Sidebar::mouseReleaseEvent(QMouseEvent *event) {
Expand Down
4 changes: 2 additions & 2 deletions selfdrive/ui/qt/widgets/prime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ void SetupWidget::replyFinished(const QString &response, bool success) {

bool prime = json["prime"].toBool();

if (QUIState::ui_state.has_prime != prime) {
QUIState::ui_state.has_prime = prime;
if (uiState()->has_prime != prime) {
uiState()->has_prime = prime;
Params().putBool("HasPrime", prime);
}

Expand Down
9 changes: 2 additions & 7 deletions selfdrive/ui/qt/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {
main_layout->addWidget(homeWindow);
QObject::connect(homeWindow, &HomeWindow::openSettings, this, &MainWindow::openSettings);
QObject::connect(homeWindow, &HomeWindow::closeSettings, this, &MainWindow::closeSettings);
QObject::connect(&qs, &QUIState::uiUpdate, homeWindow, &HomeWindow::update);
QObject::connect(&qs, &QUIState::offroadTransition, homeWindow, &HomeWindow::offroadTransition);
QObject::connect(&qs, &QUIState::offroadTransition, homeWindow, &HomeWindow::offroadTransitionSignal);

settingsWindow = new SettingsWindow(this);
main_layout->addWidget(settingsWindow);
QObject::connect(settingsWindow, &SettingsWindow::closeSettings, this, &MainWindow::closeSettings);
QObject::connect(&qs, &QUIState::offroadTransition, settingsWindow, &SettingsWindow::offroadTransition);
QObject::connect(settingsWindow, &SettingsWindow::reviewTrainingGuide, [=]() {
onboardingWindow->showTrainingGuide();
main_layout->setCurrentWidget(onboardingWindow);
Expand All @@ -37,8 +33,7 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {
main_layout->setCurrentWidget(onboardingWindow);
}

QObject::connect(&qs, &QUIState::uiUpdate, &device, &Device::update);
QObject::connect(&qs, &QUIState::offroadTransition, [=](bool offroad) {
QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) {
if (!offroad) {
closeSettings();
}
Expand Down Expand Up @@ -79,7 +74,7 @@ void MainWindow::openSettings() {
void MainWindow::closeSettings() {
main_layout->setCurrentWidget(homeWindow);

if (QUIState::ui_state.scene.started) {
if (uiState()->scene.started) {
homeWindow->showSidebar(false);
}
}
Expand Down
1 change: 0 additions & 1 deletion selfdrive/ui/qt/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class MainWindow : public QWidget {
void closeSettings();

Device device;
QUIState qs;

QStackedLayout *main_layout;
HomeWindow *homeWindow;
Expand Down
37 changes: 22 additions & 15 deletions selfdrive/ui/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,49 +218,51 @@ static void update_status(UIState *s) {
}


QUIState::QUIState(QObject *parent) : QObject(parent) {
ui_state.sm = std::make_unique<SubMaster, const std::initializer_list<const char *>>({
UIState::UIState(QObject *parent) : QObject(parent) {
sm = std::make_unique<SubMaster, const std::initializer_list<const char *>>({
"modelV2", "controlsState", "liveCalibration", "radarState", "deviceState", "roadCameraState",
"pandaStates", "carParams", "driverMonitoringState", "sensorEvents", "carState", "liveLocationKalman",
});

Params params;
ui_state.wide_camera = Hardware::TICI() ? params.getBool("EnableWideCamera") : false;
ui_state.has_prime = params.getBool("HasPrime");
wide_camera = Hardware::TICI() ? params.getBool("EnableWideCamera") : false;
has_prime = params.getBool("HasPrime");

// update timer
timer = new QTimer(this);
QObject::connect(timer, &QTimer::timeout, this, &QUIState::update);
QObject::connect(timer, &QTimer::timeout, this, &UIState::update);
timer->start(1000 / UI_FREQ);
}

void QUIState::update() {
update_sockets(&ui_state);
update_state(&ui_state);
update_status(&ui_state);
void UIState::update() {
update_sockets(this);
update_state(this);
update_status(this);

if (ui_state.scene.started != started_prev || ui_state.sm->frame == 1) {
started_prev = ui_state.scene.started;
emit offroadTransition(!ui_state.scene.started);
if (scene.started != started_prev || sm->frame == 1) {
started_prev = scene.started;
emit offroadTransition(!scene.started);
}

if (ui_state.sm->frame % UI_FREQ == 0) {
if (sm->frame % UI_FREQ == 0) {
watchdog_kick();
}
emit uiUpdate(ui_state);
emit uiUpdate(*this);
}

Device::Device(QObject *parent) : brightness_filter(BACKLIGHT_OFFROAD, BACKLIGHT_TS, BACKLIGHT_DT), QObject(parent) {
setAwake(true);
resetInteractiveTimout();

QObject::connect(uiState(), &UIState::uiUpdate, this, &Device::update);
}

void Device::update(const UIState &s) {
updateBrightness(s);
updateWakefulness(s);

// TODO: remove from UIState and use signals
QUIState::ui_state.awake = awake;
uiState()->awake = awake;
}

void Device::setAwake(bool on) {
Expand Down Expand Up @@ -329,3 +331,8 @@ void Device::updateWakefulness(const UIState &s) {

setAwake(s.scene.ignition || interactive_timeout > 0);
}

UIState *uiState() {
static UIState ui_state;
return &ui_state;
}
19 changes: 7 additions & 12 deletions selfdrive/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ typedef struct UIScene {
uint64_t started_frame;
} UIScene;

typedef struct UIState {
class UIState : public QObject {
Q_OBJECT

public:
UIState(QObject* parent = 0);

int fb_w = 0, fb_h = 0;

std::unique_ptr<SubMaster> sm;
Expand All @@ -114,17 +119,6 @@ typedef struct UIState {

QTransform car_space_transform;
bool wide_camera;
} UIState;


class QUIState : public QObject {
Q_OBJECT

public:
QUIState(QObject* parent = 0);

// TODO: get rid of this, only use signal
inline static UIState ui_state = {0};

signals:
void uiUpdate(const UIState &s);
Expand All @@ -138,6 +132,7 @@ private slots:
bool started_prev = true;
};

UIState *uiState();

// device management class

Expand Down