openauto/src/autoapp/autoapp.cpp

135 lines
5.5 KiB
C++
Raw Normal View History

2018-03-17 17:27:24 +01:00
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#include <thread>
#include <QApplication>
2018-03-19 20:58:40 +01:00
#include <f1x/aasdk/USB/USBHub.hpp>
#include <f1x/aasdk/USB/ConnectedAccessoriesEnumerator.hpp>
#include <f1x/aasdk/USB/AccessoryModeQueryChain.hpp>
#include <f1x/aasdk/USB/AccessoryModeQueryChainFactory.hpp>
#include <f1x/aasdk/USB/AccessoryModeQueryFactory.hpp>
2018-03-19 21:34:32 +01:00
#include <f1x/aasdk/TCP/TCPWrapper.hpp>
2018-03-19 20:58:40 +01:00
#include <f1x/openauto/autoapp/App.hpp>
#include <f1x/openauto/autoapp/Configuration/IConfiguration.hpp>
2018-03-25 01:58:38 +01:00
#include <f1x/openauto/autoapp/Configuration/RecentAddressesList.hpp>
2018-04-07 00:38:43 +02:00
#include <f1x/openauto/autoapp/Service/AndroidAutoEntityFactory.hpp>
#include <f1x/openauto/autoapp/Service/ServiceFactory.hpp>
2018-03-17 17:27:24 +01:00
#include <f1x/openauto/autoapp/Configuration/Configuration.hpp>
#include <f1x/openauto/autoapp/UI/MainWindow.hpp>
#include <f1x/openauto/autoapp/UI/SettingsWindow.hpp>
2018-03-21 19:38:20 +01:00
#include <f1x/openauto/autoapp/UI/ConnectDialog.hpp>
2018-03-17 17:27:24 +01:00
#include <f1x/openauto/Common/Log.hpp>
namespace aasdk = f1x::aasdk;
namespace autoapp = f1x::openauto::autoapp;
using ThreadPool = std::vector<std::thread>;
void startUSBWorkers(boost::asio::io_service& ioService, libusb_context* usbContext, ThreadPool& threadPool)
{
auto usbWorker = [&ioService, usbContext]() {
timeval libusbEventTimeout{180, 0};
while(!ioService.stopped())
{
libusb_handle_events_timeout_completed(usbContext, &libusbEventTimeout, nullptr);
}
};
threadPool.emplace_back(usbWorker);
threadPool.emplace_back(usbWorker);
threadPool.emplace_back(usbWorker);
threadPool.emplace_back(usbWorker);
}
void startIOServiceWorkers(boost::asio::io_service& ioService, ThreadPool& threadPool)
{
auto ioServiceWorker = [&ioService]() {
ioService.run();
};
threadPool.emplace_back(ioServiceWorker);
threadPool.emplace_back(ioServiceWorker);
threadPool.emplace_back(ioServiceWorker);
threadPool.emplace_back(ioServiceWorker);
}
int main(int argc, char* argv[])
{
libusb_context* usbContext;
if(libusb_init(&usbContext) != 0)
{
OPENAUTO_LOG(error) << "[OpenAuto] libusb init failed.";
return 1;
}
boost::asio::io_service ioService;
boost::asio::io_service::work work(ioService);
2018-03-19 20:58:40 +01:00
std::vector<std::thread> threadPool;
startUSBWorkers(ioService, usbContext, threadPool);
startIOServiceWorkers(ioService, threadPool);
2018-03-17 17:27:24 +01:00
2018-03-19 20:58:40 +01:00
QApplication qApplication(argc, argv);
2018-03-17 17:27:24 +01:00
autoapp::ui::MainWindow mainWindow;
2018-03-17 17:29:31 +01:00
mainWindow.setWindowFlags(Qt::WindowStaysOnTopHint);
2018-03-17 17:27:24 +01:00
auto configuration = std::make_shared<autoapp::configuration::Configuration>();
autoapp::ui::SettingsWindow settingsWindow(configuration);
2018-03-17 17:29:31 +01:00
settingsWindow.setWindowFlags(Qt::WindowStaysOnTopHint);
2018-03-17 17:27:24 +01:00
autoapp::configuration::RecentAddressesList recentAddressesList(7);
2018-03-25 01:58:38 +01:00
recentAddressesList.read();
2018-03-21 23:51:31 +01:00
aasdk::tcp::TCPWrapper tcpWrapper;
2018-03-25 01:58:38 +01:00
autoapp::ui::ConnectDialog connectDialog(ioService, tcpWrapper, recentAddressesList);
2018-03-21 23:51:31 +01:00
connectDialog.setWindowFlags(Qt::WindowStaysOnTopHint);
2018-03-21 19:38:20 +01:00
2018-03-19 20:58:40 +01:00
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::exit, []() { std::exit(0); });
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openSettings, &settingsWindow, &autoapp::ui::SettingsWindow::showFullScreen);
2018-03-21 19:38:20 +01:00
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openConnectDialog, &connectDialog, &autoapp::ui::ConnectDialog::exec);
2018-03-19 20:58:40 +01:00
2018-03-17 17:27:24 +01:00
qApplication.setOverrideCursor(Qt::BlankCursor);
2018-03-19 20:58:40 +01:00
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::toggleCursor, [&qApplication]() {
const auto cursor = qApplication.overrideCursor()->shape() == Qt::BlankCursor ? Qt::ArrowCursor : Qt::BlankCursor;
qApplication.setOverrideCursor(cursor);
2018-03-17 17:27:24 +01:00
});
2018-03-19 20:58:40 +01:00
mainWindow.showFullScreen();
2018-03-17 17:27:24 +01:00
2018-03-19 20:58:40 +01:00
aasdk::usb::USBWrapper usbWrapper(usbContext);
aasdk::usb::AccessoryModeQueryFactory queryFactory(usbWrapper, ioService);
aasdk::usb::AccessoryModeQueryChainFactory queryChainFactory(usbWrapper, ioService, queryFactory);
2018-04-07 00:38:43 +02:00
autoapp::service::ServiceFactory serviceFactory(ioService, configuration);
autoapp::service::AndroidAutoEntityFactory androidAutoEntityFactory(ioService, configuration, serviceFactory);
2018-03-19 20:58:40 +01:00
auto usbHub(std::make_shared<aasdk::usb::USBHub>(usbWrapper, ioService, queryChainFactory));
2018-03-21 19:38:20 +01:00
auto connectedAccessoriesEnumerator(std::make_shared<aasdk::usb::ConnectedAccessoriesEnumerator>(usbWrapper, ioService, queryChainFactory));
auto app = std::make_shared<autoapp::App>(ioService, usbWrapper, tcpWrapper, androidAutoEntityFactory, std::move(usbHub), std::move(connectedAccessoriesEnumerator));
2018-03-21 23:51:31 +01:00
2018-03-22 16:33:23 +01:00
QObject::connect(&connectDialog, &autoapp::ui::ConnectDialog::connectionSucceed, [&app](auto socket) {
2018-03-21 23:51:31 +01:00
app->start(std::move(socket));
});
2018-03-19 20:58:40 +01:00
app->waitForUSBDevice();
2018-03-17 17:27:24 +01:00
auto result = qApplication.exec();
std::for_each(threadPool.begin(), threadPool.end(), std::bind(&std::thread::join, std::placeholders::_1));
libusb_exit(usbContext);
return result;
}