2021-08-11 00:07:12 +01:00
|
|
|
#include "Insight.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <typeindex>
|
|
|
|
|
#include <QTimer>
|
2021-09-25 01:10:01 +01:00
|
|
|
#include <QFontDatabase>
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2021-05-30 19:05:18 +01:00
|
|
|
#include "src/Helpers/Paths.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
#include "src/Logger/Logger.hpp"
|
|
|
|
|
#include "src/Exceptions/InvalidConfig.hpp"
|
|
|
|
|
#include "src/Targets/TargetState.hpp"
|
|
|
|
|
|
2021-10-18 01:09:53 +01:00
|
|
|
#include "src/Application.hpp"
|
|
|
|
|
#include "InsightWorker/Tasks/QueryLatestVersionNumber.hpp"
|
2021-10-19 23:00:54 +01:00
|
|
|
#include "src/VersionNumber.hpp"
|
2021-10-18 01:09:53 +01:00
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
using namespace Bloom;
|
2021-05-24 20:58:49 +01:00
|
|
|
using namespace Bloom::Exceptions;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
void Insight::run() {
|
|
|
|
|
try {
|
|
|
|
|
this->startup();
|
|
|
|
|
|
|
|
|
|
this->workerThread->start();
|
2021-05-29 21:39:00 +01:00
|
|
|
this->setThreadState(ThreadState::READY);
|
2021-04-04 21:04:12 +01:00
|
|
|
Logger::info("Insight ready");
|
2021-07-23 23:15:52 +01:00
|
|
|
this->application.exec();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error("Insight encountered a fatal error. See below for errors:");
|
|
|
|
|
Logger::error(exception.getMessage());
|
|
|
|
|
|
|
|
|
|
} catch (const std::exception& exception) {
|
|
|
|
|
Logger::error("Insight encountered a fatal error. See below for errors:");
|
|
|
|
|
Logger::error(std::string(exception.what()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Insight::startup() {
|
|
|
|
|
Logger::info("Starting Insight");
|
2021-05-29 21:39:00 +01:00
|
|
|
this->setThreadState(ThreadState::STARTING);
|
2021-04-04 21:04:12 +01:00
|
|
|
this->eventManager.registerListener(this->eventListener);
|
|
|
|
|
|
2021-06-22 14:44:00 +01:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::ShutdownApplication>(
|
2021-04-04 21:04:12 +01:00
|
|
|
std::bind(&Insight::onShutdownApplicationEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
|
|
|
|
|
2021-06-22 14:44:00 +01:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetControllerThreadStateChanged>(
|
2021-05-24 21:09:50 +01:00
|
|
|
std::bind(&Insight::onTargetControllerThreadStateChangedEvent, this, std::placeholders::_1)
|
2021-04-04 21:04:12 +01:00
|
|
|
);
|
|
|
|
|
|
2021-07-04 00:32:05 +01:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::DebugServerThreadStateChanged>(
|
|
|
|
|
std::bind(&Insight::onDebugServerThreadStateChangedEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
|
|
|
|
|
2021-04-24 20:23:17 +01:00
|
|
|
auto targetDescriptor = this->targetControllerConsole.getTargetDescriptor();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2021-07-23 23:15:52 +01:00
|
|
|
this->application.setQuitOnLastWindowClosed(true);
|
2021-05-30 16:53:24 +01:00
|
|
|
qRegisterMetaType<Bloom::Targets::TargetDescriptor>();
|
2021-04-04 21:04:12 +01:00
|
|
|
qRegisterMetaType<Bloom::Targets::TargetPinDescriptor>();
|
|
|
|
|
qRegisterMetaType<Bloom::Targets::TargetPinState>();
|
|
|
|
|
qRegisterMetaType<Bloom::Targets::TargetState>();
|
|
|
|
|
qRegisterMetaType<std::map<int, Bloom::Targets::TargetPinState>>();
|
|
|
|
|
|
2021-09-25 01:10:01 +01:00
|
|
|
// Load Ubuntu fonts
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-B.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-BI.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-C.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-L.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-LI.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-M.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-MI.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/UbuntuMono-B.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/UbuntuMono-BI.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/UbuntuMono-R.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/UbuntuMono-RI.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-R.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-RI.ttf")
|
|
|
|
|
);
|
|
|
|
|
QFontDatabase::addApplicationFont(
|
|
|
|
|
QString::fromStdString(Paths::resourcesDirPath() + "/Fonts/Ubuntu/Ubuntu-Th.ttf")
|
|
|
|
|
);
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
/*
|
|
|
|
|
* We can't run our own event loop here - we have to use Qt's event loop. But we still need to be able to
|
|
|
|
|
* process our events. To address this, we use a QTimer to dispatch our events on an interval.
|
|
|
|
|
*
|
|
|
|
|
* This allows us to use Qt's event loop whilst still being able to process our own events.
|
|
|
|
|
*/
|
2021-07-23 23:15:52 +01:00
|
|
|
auto eventDispatchTimer = new QTimer(&(this->application));
|
2021-09-02 21:19:46 +01:00
|
|
|
this->connect(eventDispatchTimer, &QTimer::timeout, this, &Insight::dispatchEvents);
|
2021-04-04 21:04:12 +01:00
|
|
|
eventDispatchTimer->start(100);
|
|
|
|
|
|
2021-09-02 21:19:46 +01:00
|
|
|
this->connect(this->insightWorker, &InsightWorker::targetControllerSuspended, this->mainWindow, &InsightWindow::onTargetControllerSuspended);
|
|
|
|
|
this->connect(this->insightWorker, &InsightWorker::targetControllerResumed, this->mainWindow, &InsightWindow::onTargetControllerResumed);
|
|
|
|
|
this->connect(this->insightWorker, &InsightWorker::targetStateUpdated, this->mainWindow, &InsightWindow::onTargetStateUpdate);
|
|
|
|
|
this->connect(this->insightWorker, &InsightWorker::targetProgramCounterUpdated, this->mainWindow, &InsightWindow::onTargetProgramCounterUpdate);
|
|
|
|
|
this->connect(this->mainWindow, &InsightWindow::refreshTargetPinStates, this->insightWorker, &InsightWorker::requestPinStates);
|
|
|
|
|
|
|
|
|
|
this->mainWindow->setInsightConfig(this->insightConfig);
|
|
|
|
|
this->mainWindow->setEnvironmentConfig(this->environmentConfig);
|
|
|
|
|
|
|
|
|
|
this->mainWindow->init(targetDescriptor);
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
// Prepare worker thread
|
|
|
|
|
this->workerThread = new QThread();
|
|
|
|
|
this->workerThread->setObjectName("IW");
|
2021-08-22 20:46:52 +01:00
|
|
|
this->insightWorker->moveToThread(this->workerThread);
|
2021-09-02 21:19:46 +01:00
|
|
|
this->connect(this->workerThread, &QThread::started, this->insightWorker, &InsightWorker::startup);
|
|
|
|
|
this->connect(this->workerThread, &QThread::finished, this->insightWorker, &QObject::deleteLater);
|
|
|
|
|
this->connect(this->workerThread, &QThread::finished, this->workerThread, &QThread::deleteLater);
|
2021-08-22 20:46:52 +01:00
|
|
|
|
2021-10-18 01:09:53 +01:00
|
|
|
this->connect(this->insightWorker, &InsightWorker::ready, this, [this] {
|
|
|
|
|
this->checkBloomVersion();
|
|
|
|
|
});
|
|
|
|
|
|
2021-09-02 21:19:46 +01:00
|
|
|
this->mainWindow->show();
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Insight::shutdown() {
|
2021-05-29 21:39:00 +01:00
|
|
|
if (this->getThreadState() == ThreadState::STOPPED) {
|
2021-04-04 21:04:12 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger::info("Shutting down Insight");
|
2021-09-02 21:19:46 +01:00
|
|
|
this->mainWindow->close();
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
if (this->workerThread != nullptr && this->workerThread->isRunning()) {
|
|
|
|
|
this->workerThread->quit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-23 23:15:52 +01:00
|
|
|
this->application.exit(0);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2021-05-29 21:39:00 +01:00
|
|
|
this->setThreadState(ThreadState::STOPPED);
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2021-10-18 01:09:53 +01:00
|
|
|
void Insight::checkBloomVersion() {
|
2021-10-19 23:00:54 +01:00
|
|
|
auto currentVersionNumber = Application::VERSION;
|
2021-10-18 01:09:53 +01:00
|
|
|
|
|
|
|
|
auto versionQueryTask = new QueryLatestVersionNumber(
|
2021-10-19 23:00:54 +01:00
|
|
|
currentVersionNumber
|
2021-10-18 01:09:53 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this->connect(
|
|
|
|
|
versionQueryTask,
|
|
|
|
|
&QueryLatestVersionNumber::latestVersionNumberRetrieved,
|
|
|
|
|
this,
|
2021-10-19 23:00:54 +01:00
|
|
|
[this, currentVersionNumber] (const VersionNumber& latestVersionNumber) {
|
|
|
|
|
if (latestVersionNumber > currentVersionNumber) {
|
2021-10-18 01:09:53 +01:00
|
|
|
Logger::warning(
|
2021-10-19 23:00:54 +01:00
|
|
|
"Bloom v" + latestVersionNumber.toString()
|
2021-10-18 01:09:53 +01:00
|
|
|
+ " is available to download - upgrade via https://bloom.oscillate.io"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this->insightWorker->queueTask(versionQueryTask);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 14:44:00 +01:00
|
|
|
void Insight::onShutdownApplicationEvent(const Events::ShutdownApplication&) {
|
2021-04-04 21:04:12 +01:00
|
|
|
/*
|
|
|
|
|
* Once Insight shuts down, control of the main thread will be returned to Application::run(), which
|
|
|
|
|
* will pickup the ShutdownApplication event and proceed with the shutdown.
|
|
|
|
|
*/
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 14:44:00 +01:00
|
|
|
void Insight::onTargetControllerThreadStateChangedEvent(const Events::TargetControllerThreadStateChanged& event) {
|
2021-06-22 03:06:20 +01:00
|
|
|
if (event.getState() == ThreadState::STOPPED) {
|
2021-04-04 21:04:12 +01:00
|
|
|
// Something horrible has happened with the TargetController - Insight is useless without the TargetController
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-04 00:32:05 +01:00
|
|
|
|
|
|
|
|
void Insight::onDebugServerThreadStateChangedEvent(const Events::DebugServerThreadStateChanged& event) {
|
|
|
|
|
if (event.getState() == ThreadState::STOPPED) {
|
|
|
|
|
// Something horrible has happened with the DebugServer
|
|
|
|
|
this->shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|