diff --git a/include/f1x/openauto/autoapp/USB/USBApp.hpp b/include/f1x/openauto/autoapp/App.hpp similarity index 77% rename from include/f1x/openauto/autoapp/USB/USBApp.hpp rename to include/f1x/openauto/autoapp/App.hpp index 6b0a81b..f31e2d5 100644 --- a/include/f1x/openauto/autoapp/USB/USBApp.hpp +++ b/include/f1x/openauto/autoapp/App.hpp @@ -29,23 +29,21 @@ namespace openauto { namespace autoapp { -namespace usb -{ -class USBApp: public projection::IAndroidAutoEntityEventHandler, public std::enable_shared_from_this +class App: public projection::IAndroidAutoEntityEventHandler, public std::enable_shared_from_this { public: - typedef std::shared_ptr Pointer; + typedef std::shared_ptr Pointer; - USBApp(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, - aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator); + App(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, + aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator); void start(); void stop(); void onAndroidAutoQuit() override; private: - using std::enable_shared_from_this::shared_from_this; + using std::enable_shared_from_this::shared_from_this; void enumerateDevices(); void waitForDevice(); @@ -64,4 +62,3 @@ private: } } } -} diff --git a/include/f1x/openauto/autoapp/USB/USBMain.hpp b/include/f1x/openauto/autoapp/Main.hpp similarity index 84% rename from include/f1x/openauto/autoapp/USB/USBMain.hpp rename to include/f1x/openauto/autoapp/Main.hpp index 32f7977..4cb6746 100644 --- a/include/f1x/openauto/autoapp/USB/USBMain.hpp +++ b/include/f1x/openauto/autoapp/Main.hpp @@ -22,26 +22,22 @@ #include #include #include -#include +#include #include #include #include -class QApplication; - namespace f1x { namespace openauto { namespace autoapp { -namespace usb -{ -class USBMain +class Main { public: - USBMain(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration); + Main(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration); void start(); void stop(); @@ -53,10 +49,9 @@ private: aasdk::usb::AccessoryModeQueryChainFactory queryChainFactory_; projection::ServiceFactory serviceFactory_; projection::AndroidAutoEntityFactory androidAutoEntityFactory_; - autoapp::usb::USBApp::Pointer usbApp_; + autoapp::App::Pointer app_; }; } } } -} diff --git a/src/autoapp/USB/USBApp.cpp b/src/autoapp/App.cpp similarity index 66% rename from src/autoapp/USB/USBApp.cpp rename to src/autoapp/App.cpp index fe4064b..73a7b3d 100644 --- a/src/autoapp/USB/USBApp.cpp +++ b/src/autoapp/App.cpp @@ -17,7 +17,7 @@ */ #include -#include +#include #include namespace f1x @@ -26,11 +26,9 @@ namespace openauto { namespace autoapp { -namespace usb -{ -USBApp::USBApp(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, - aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator) +App::App(boost::asio::io_service& ioService, projection::IAndroidAutoEntityFactory& androidAutoEntityFactory, + aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator) : ioService_(ioService) , strand_(ioService_) , androidAutoEntityFactory_(androidAutoEntityFactory) @@ -41,7 +39,7 @@ USBApp::USBApp(boost::asio::io_service& ioService, projection::IAndroidAutoEntit } -void USBApp::start() +void App::start() { strand_.dispatch([this, self = this->shared_from_this()]() { this->waitForDevice(); @@ -49,7 +47,7 @@ void USBApp::start() }); } -void USBApp::stop() +void App::stop() { strand_.dispatch([this, self = this->shared_from_this()]() { isStopped_ = true; @@ -63,9 +61,9 @@ void USBApp::stop() }); } -void USBApp::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) +void App::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) { - OPENAUTO_LOG(info) << "[USBApp] Device connected."; + OPENAUTO_LOG(info) << "[App] Device connected."; if(androidAutoEntity_ == nullptr) { @@ -76,7 +74,7 @@ void USBApp::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) } catch(const aasdk::error::Error& error) { - OPENAUTO_LOG(error) << "[USBApp] AndroidAutoEntity create error: " << error.what(); + OPENAUTO_LOG(error) << "[App] AndroidAutoEntity create error: " << error.what(); androidAutoEntity_.reset(); this->waitForDevice(); @@ -84,37 +82,37 @@ void USBApp::aoapDeviceHandler(aasdk::usb::DeviceHandle deviceHandle) } else { - OPENAUTO_LOG(warning) << "[USBApp] android auto entity is still running."; + OPENAUTO_LOG(warning) << "[App] android auto entity is still running."; } } -void USBApp::enumerateDevices() +void App::enumerateDevices() { auto promise = aasdk::usb::IConnectedAccessoriesEnumerator::Promise::defer(strand_); promise->then([this, self = this->shared_from_this()](auto result) { - OPENAUTO_LOG(info) << "[USBApp] Devices enumeration result: " << result; + OPENAUTO_LOG(info) << "[App] Devices enumeration result: " << result; }, [this, self = this->shared_from_this()](auto e) { - OPENAUTO_LOG(error) << "[USBApp] Devices enumeration failed: " << e.what(); + OPENAUTO_LOG(error) << "[App] Devices enumeration failed: " << e.what(); }); connectedAccessoriesEnumerator_->enumerate(std::move(promise)); } -void USBApp::waitForDevice() +void App::waitForDevice() { - OPENAUTO_LOG(info) << "[USBApp] Waiting for device..."; + OPENAUTO_LOG(info) << "[App] Waiting for device..."; auto promise = aasdk::usb::IUSBHub::Promise::defer(strand_); - promise->then(std::bind(&USBApp::aoapDeviceHandler, this->shared_from_this(), std::placeholders::_1), - std::bind(&USBApp::onUSBHubError, this->shared_from_this(), std::placeholders::_1)); + promise->then(std::bind(&App::aoapDeviceHandler, this->shared_from_this(), std::placeholders::_1), + std::bind(&App::onUSBHubError, this->shared_from_this(), std::placeholders::_1)); usbHub_->start(std::move(promise)); } -void USBApp::onAndroidAutoQuit() +void App::onAndroidAutoQuit() { strand_.dispatch([this, self = this->shared_from_this()]() { - OPENAUTO_LOG(info) << "[USBApp] quit."; + OPENAUTO_LOG(info) << "[App] quit."; androidAutoEntity_->stop(); androidAutoEntity_.reset(); @@ -126,9 +124,9 @@ void USBApp::onAndroidAutoQuit() }); } -void USBApp::onUSBHubError(const aasdk::error::Error& error) +void App::onUSBHubError(const aasdk::error::Error& error) { - OPENAUTO_LOG(error) << "[USBApp] usb hub error: " << error.what(); + OPENAUTO_LOG(error) << "[App] usb hub error: " << error.what(); if(error.getCode() == aasdk::error::ErrorCode::OPERATION_ABORTED || error.getCode() == aasdk::error::ErrorCode::OPERATION_IN_PROGRESS) @@ -140,4 +138,3 @@ void USBApp::onUSBHubError(const aasdk::error::Error& error) } } } -} diff --git a/src/autoapp/Main.cpp b/src/autoapp/Main.cpp index e5de25d..84ea91e 100644 --- a/src/autoapp/Main.cpp +++ b/src/autoapp/Main.cpp @@ -16,89 +16,43 @@ * along with openauto. If not, see . */ -#include #include -#include -#include -#include -#include -#include +#include +#include +#include -namespace aasdk = f1x::aasdk; -namespace autoapp = f1x::openauto::autoapp; -using ThreadPool = std::vector; - -void startUSBWorkers(boost::asio::io_service& ioService, libusb_context* usbContext, ThreadPool& threadPool) +namespace f1x +{ +namespace openauto +{ +namespace autoapp { - auto usbWorker = [&ioService, usbContext]() { - timeval libusbEventTimeout{180, 0}; - while(!ioService.stopped()) - { - libusb_handle_events_timeout_completed(usbContext, &libusbEventTimeout, nullptr); - } - }; +Main::Main(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration) + : usbWrapper_(usbWrapper) + , ioService_(ioService) + , queryFactory_(usbWrapper_, ioService_) + , queryChainFactory_(usbWrapper_, ioService_, queryFactory_) + , serviceFactory_(ioService_, configuration) + , androidAutoEntityFactory_(usbWrapper_, ioService_, configuration, serviceFactory_) +{ + auto usbHub(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); + auto ConnectedAccessoriesEnumerator(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); - threadPool.emplace_back(usbWorker); - threadPool.emplace_back(usbWorker); - threadPool.emplace_back(usbWorker); - threadPool.emplace_back(usbWorker); + app_ = std::make_shared(ioService_, androidAutoEntityFactory_, + std::move(usbHub), std::move(ConnectedAccessoriesEnumerator)); } -void startIOServiceWorkers(boost::asio::io_service& ioService, ThreadPool& threadPool) +void Main::start() { - auto ioServiceWorker = [&ioService]() { - ioService.run(); - }; - - threadPool.emplace_back(ioServiceWorker); - threadPool.emplace_back(ioServiceWorker); - threadPool.emplace_back(ioServiceWorker); - threadPool.emplace_back(ioServiceWorker); + app_->start(); } -int main(int argc, char* argv[]) +void Main::stop() { - libusb_context* usbContext; - if(libusb_init(&usbContext) != 0) - { - OPENAUTO_LOG(error) << "[OpenAuto] libusb init failed."; - return 1; - } - - QApplication qApplication(argc, argv); - boost::asio::io_service ioService; - boost::asio::io_service::work work(ioService); - - autoapp::ui::MainWindow mainWindow; - //mainWindow.setWindowFlags(Qt::WindowStaysOnTopHint); - mainWindow.showFullScreen(); - - auto configuration = std::make_shared(); - 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 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; + app_->stop(); +} + +} +} } diff --git a/src/autoapp/USB/USBMain.cpp b/src/autoapp/USB/USBMain.cpp deleted file mode 100644 index b4ddb2e..0000000 --- a/src/autoapp/USB/USBMain.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* -* 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 . -*/ - -#include -#include -#include -#include - -namespace f1x -{ -namespace openauto -{ -namespace autoapp -{ -namespace usb -{ - -USBMain::USBMain(aasdk::usb::IUSBWrapper& usbWrapper, boost::asio::io_service& ioService, configuration::IConfiguration::Pointer configuration) - : usbWrapper_(usbWrapper) - , ioService_(ioService) - , queryFactory_(usbWrapper_, ioService_) - , queryChainFactory_(usbWrapper_, ioService_, queryFactory_) - , serviceFactory_(ioService_, configuration) - , androidAutoEntityFactory_(usbWrapper_, ioService_, configuration, serviceFactory_) -{ - auto usbHub(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); - auto ConnectedAccessoriesEnumerator(std::make_shared(usbWrapper_, ioService_, queryChainFactory_)); - - usbApp_ = std::make_shared(ioService_, androidAutoEntityFactory_, - std::move(usbHub), std::move(ConnectedAccessoriesEnumerator)); -} - -void USBMain::start() -{ - usbApp_->start(); -} - -void USBMain::stop() -{ - usbApp_->stop(); -} - -} -} -} -} diff --git a/src/autoapp/autoapp.cpp b/src/autoapp/autoapp.cpp new file mode 100644 index 0000000..0eaaf70 --- /dev/null +++ b/src/autoapp/autoapp.cpp @@ -0,0 +1,104 @@ +/* +* 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 . +*/ + +#include +#include +#include +#include +#include +#include +#include + +namespace aasdk = f1x::aasdk; +namespace autoapp = f1x::openauto::autoapp; +using ThreadPool = std::vector; + +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; + } + + QApplication qApplication(argc, argv); + boost::asio::io_service ioService; + boost::asio::io_service::work work(ioService); + + autoapp::ui::MainWindow mainWindow; + //mainWindow.setWindowFlags(Qt::WindowStaysOnTopHint); + mainWindow.showFullScreen(); + + auto configuration = std::make_shared(); + 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::Main 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 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; +}