mirror of https://github.com/f1xpl/openauto.git
Move Qt related stuff outside USBMain.cpp
parent
99b96296ab
commit
9e114226a9
|
@ -18,7 +18,6 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <thread>
|
|
||||||
#include <f1x/aasdk/USB/USBWrapper.hpp>
|
#include <f1x/aasdk/USB/USBWrapper.hpp>
|
||||||
#include <f1x/aasdk/USB/AccessoryModeQueryChain.hpp>
|
#include <f1x/aasdk/USB/AccessoryModeQueryChain.hpp>
|
||||||
#include <f1x/aasdk/USB/AccessoryModeQueryChainFactory.hpp>
|
#include <f1x/aasdk/USB/AccessoryModeQueryChainFactory.hpp>
|
||||||
|
@ -28,6 +27,8 @@
|
||||||
#include <f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp>
|
#include <f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp>
|
||||||
#include <f1x/openauto/autoapp/Projection/ServiceFactory.hpp>
|
#include <f1x/openauto/autoapp/Projection/ServiceFactory.hpp>
|
||||||
|
|
||||||
|
class QApplication;
|
||||||
|
|
||||||
namespace f1x
|
namespace f1x
|
||||||
{
|
{
|
||||||
namespace openauto
|
namespace openauto
|
||||||
|
@ -40,26 +41,19 @@ namespace usb
|
||||||
class USBMain
|
class USBMain
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
USBMain(libusb_context* context);
|
USBMain(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration);
|
||||||
|
|
||||||
int exec(int argc, char* argv[]);
|
void start();
|
||||||
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef std::vector<std::thread> ThreadPool;
|
aasdk::usb::IUSBWrapper& usbWrapper_;
|
||||||
|
boost::asio::io_service& ioService_;
|
||||||
void startUSBWorkers();
|
|
||||||
void startIOServiceWorkers();
|
|
||||||
|
|
||||||
libusb_context* usbContext_;
|
|
||||||
aasdk::usb::USBWrapper usbWrapper_;
|
|
||||||
boost::asio::io_service ioService_;
|
|
||||||
aasdk::usb::AccessoryModeQueryFactory queryFactory_;
|
aasdk::usb::AccessoryModeQueryFactory queryFactory_;
|
||||||
aasdk::usb::AccessoryModeQueryChainFactory queryChainFactory_;
|
aasdk::usb::AccessoryModeQueryChainFactory queryChainFactory_;
|
||||||
configuration::IConfiguration::Pointer configuration_;
|
|
||||||
projection::ServiceFactory serviceFactory_;
|
projection::ServiceFactory serviceFactory_;
|
||||||
projection::AndroidAutoEntityFactory androidAutoEntityFactory_;
|
projection::AndroidAutoEntityFactory androidAutoEntityFactory_;
|
||||||
autoapp::usb::USBApp::Pointer usbApp_;
|
autoapp::usb::USBApp::Pointer usbApp_;
|
||||||
ThreadPool threadPool_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,24 +16,89 @@
|
||||||
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
|
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
|
#include <QApplication>
|
||||||
|
#include <f1x/openauto/autoapp/Configuration/Configuration.hpp>
|
||||||
|
#include <f1x/openauto/autoapp/UI/MainWindow.hpp>
|
||||||
|
#include <f1x/openauto/autoapp/UI/SettingsWindow.hpp>
|
||||||
#include <f1x/openauto/autoapp/USB/USBMain.hpp>
|
#include <f1x/openauto/autoapp/USB/USBMain.hpp>
|
||||||
#include <f1x/openauto/Common/Log.hpp>
|
#include <f1x/openauto/Common/Log.hpp>
|
||||||
|
|
||||||
namespace aasdk = f1x::aasdk;
|
namespace aasdk = f1x::aasdk;
|
||||||
namespace autoapp = f1x::openauto::autoapp;
|
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[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
libusb_context* context;
|
libusb_context* usbContext;
|
||||||
if(libusb_init(&context) != 0)
|
if(libusb_init(&usbContext) != 0)
|
||||||
{
|
{
|
||||||
OPENAUTO_LOG(error) << "[OpenAuto] libusb init failed.";
|
OPENAUTO_LOG(error) << "[OpenAuto] libusb init failed.";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
autoapp::usb::USBMain main(context);
|
QApplication qApplication(argc, argv);
|
||||||
auto result = main.exec(argc, argv);
|
boost::asio::io_service ioService;
|
||||||
|
boost::asio::io_service::work work(ioService);
|
||||||
|
|
||||||
libusb_exit(context);
|
autoapp::ui::MainWindow mainWindow;
|
||||||
|
//mainWindow.setWindowFlags(Qt::WindowStaysOnTopHint);
|
||||||
|
mainWindow.showFullScreen();
|
||||||
|
|
||||||
|
auto configuration = std::make_shared<autoapp::configuration::Configuration>();
|
||||||
|
autoapp::ui::SettingsWindow settingsWindow(configuration);
|
||||||
|
//settingsWindow.setWindowFlags(Qt::WindowStaysOnTopHint);
|
||||||
|
|
||||||
|
qApplication.setOverrideCursor(Qt::BlankCursor);
|
||||||
|
bool cursorVisible = false;
|
||||||
|
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::toggleCursor, [&cursorVisible, &qApplication]() {
|
||||||
|
cursorVisible = !cursorVisible;
|
||||||
|
qApplication.setOverrideCursor(cursorVisible ? Qt::ArrowCursor : Qt::BlankCursor);
|
||||||
|
});
|
||||||
|
|
||||||
|
aasdk::usb::USBWrapper usbWrapper(usbContext);
|
||||||
|
autoapp::usb::USBMain main(usbWrapper, ioService, configuration);
|
||||||
|
|
||||||
|
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::exit, []() { std::exit(0); });
|
||||||
|
QObject::connect(&mainWindow, &autoapp::ui::MainWindow::openSettings, &settingsWindow, &autoapp::ui::SettingsWindow::showFullScreen);
|
||||||
|
|
||||||
|
std::vector<std::thread> threadPool;
|
||||||
|
startUSBWorkers(ioService, usbContext, threadPool);
|
||||||
|
startIOServiceWorkers(ioService, threadPool);
|
||||||
|
main.start();
|
||||||
|
|
||||||
|
auto result = qApplication.exec();
|
||||||
|
std::for_each(threadPool.begin(), threadPool.end(), std::bind(&std::thread::join, std::placeholders::_1));
|
||||||
|
|
||||||
|
libusb_exit(usbContext);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QMainWindow>
|
|
||||||
#include <f1x/aasdk/USB/USBHub.hpp>
|
#include <f1x/aasdk/USB/USBHub.hpp>
|
||||||
#include <f1x/aasdk/USB/ConnectedAccessoriesEnumerator.hpp>
|
#include <f1x/aasdk/USB/ConnectedAccessoriesEnumerator.hpp>
|
||||||
#include <f1x/openauto/autoapp/Configuration/Configuration.hpp>
|
|
||||||
#include <f1x/openauto/autoapp/UI/MainWindow.hpp>
|
|
||||||
#include <f1x/openauto/autoapp/UI/SettingsWindow.hpp>
|
|
||||||
#include <f1x/openauto/autoapp/USB/USBMain.hpp>
|
#include <f1x/openauto/autoapp/USB/USBMain.hpp>
|
||||||
|
|
||||||
namespace f1x
|
namespace f1x
|
||||||
|
@ -34,14 +30,13 @@ namespace autoapp
|
||||||
namespace usb
|
namespace usb
|
||||||
{
|
{
|
||||||
|
|
||||||
USBMain::USBMain(libusb_context* context)
|
USBMain::USBMain(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration)
|
||||||
: usbContext_(context)
|
: usbWrapper_(usbWrapper)
|
||||||
, usbWrapper_(usbContext_)
|
, ioService_(ioService)
|
||||||
, queryFactory_(usbWrapper_, ioService_)
|
, queryFactory_(usbWrapper_, ioService_)
|
||||||
, queryChainFactory_(usbWrapper_, ioService_, queryFactory_)
|
, queryChainFactory_(usbWrapper_, ioService_, queryFactory_)
|
||||||
, configuration_(std::make_shared<configuration::Configuration>())
|
, serviceFactory_(ioService_, configuration)
|
||||||
, serviceFactory_(ioService_, configuration_)
|
, androidAutoEntityFactory_(usbWrapper_, ioService_, configuration, serviceFactory_)
|
||||||
, androidAutoEntityFactory_(usbWrapper_, ioService_, configuration_, serviceFactory_)
|
|
||||||
{
|
{
|
||||||
auto usbHub(std::make_shared<aasdk::usb::USBHub>(usbWrapper_, ioService_, queryChainFactory_));
|
auto usbHub(std::make_shared<aasdk::usb::USBHub>(usbWrapper_, ioService_, queryChainFactory_));
|
||||||
auto ConnectedAccessoriesEnumerator(std::make_shared<aasdk::usb::ConnectedAccessoriesEnumerator>(usbWrapper_, ioService_, queryChainFactory_));
|
auto ConnectedAccessoriesEnumerator(std::make_shared<aasdk::usb::ConnectedAccessoriesEnumerator>(usbWrapper_, ioService_, queryChainFactory_));
|
||||||
|
@ -50,66 +45,14 @@ USBMain::USBMain(libusb_context* context)
|
||||||
std::move(usbHub), std::move(ConnectedAccessoriesEnumerator));
|
std::move(usbHub), std::move(ConnectedAccessoriesEnumerator));
|
||||||
}
|
}
|
||||||
|
|
||||||
int USBMain::exec(int argc, char* argv[])
|
void USBMain::start()
|
||||||
{
|
{
|
||||||
QApplication qApplication(argc, argv);
|
|
||||||
|
|
||||||
ui::MainWindow mainWindow;
|
|
||||||
mainWindow.setWindowFlags(Qt::WindowStaysOnTopHint);
|
|
||||||
|
|
||||||
ui::SettingsWindow settingsWindow(configuration_);
|
|
||||||
settingsWindow.setWindowFlags(Qt::WindowStaysOnTopHint);
|
|
||||||
|
|
||||||
QObject::connect(&mainWindow, &ui::MainWindow::exit, []() { std::exit(0); });
|
|
||||||
QObject::connect(&mainWindow, &ui::MainWindow::openSettings, &settingsWindow, &ui::SettingsWindow::showFullScreen);
|
|
||||||
|
|
||||||
qApplication.setOverrideCursor(Qt::BlankCursor);
|
|
||||||
bool cursorVisible = false;
|
|
||||||
QObject::connect(&mainWindow, &ui::MainWindow::toggleCursor, [&cursorVisible, &qApplication]() {
|
|
||||||
cursorVisible = !cursorVisible;
|
|
||||||
qApplication.setOverrideCursor(cursorVisible ? Qt::ArrowCursor : Qt::BlankCursor);
|
|
||||||
});
|
|
||||||
|
|
||||||
mainWindow.showFullScreen();
|
|
||||||
|
|
||||||
boost::asio::io_service::work work(ioService_);
|
|
||||||
this->startIOServiceWorkers();
|
|
||||||
this->startUSBWorkers();
|
|
||||||
usbApp_->start();
|
usbApp_->start();
|
||||||
|
|
||||||
auto result = qApplication.exec();
|
|
||||||
std::for_each(threadPool_.begin(), threadPool_.end(), std::bind(&std::thread::join, std::placeholders::_1));
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void USBMain::startUSBWorkers()
|
void USBMain::stop()
|
||||||
{
|
{
|
||||||
auto usbWorker = [this]() {
|
usbApp_->stop();
|
||||||
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 USBMain::startIOServiceWorkers()
|
|
||||||
{
|
|
||||||
auto ioServiceWorker = [this]() {
|
|
||||||
while(!ioService_.stopped())
|
|
||||||
{
|
|
||||||
ioService_.run();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
threadPool_.emplace_back(ioServiceWorker);
|
|
||||||
threadPool_.emplace_back(ioServiceWorker);
|
|
||||||
threadPool_.emplace_back(ioServiceWorker);
|
|
||||||
threadPool_.emplace_back(ioServiceWorker);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue