diff --git a/Daemon.cpp b/Daemon.cpp index 7ca28a6f..a00d85a2 100644 --- a/Daemon.cpp +++ b/Daemon.cpp @@ -265,5 +265,13 @@ namespace i2p return true; } - } + + bool DaemonQT::init(int argc, char* argv[]) + { + #if 0 + m_Impl = std::make_shared (argc, argv); + #endif + return Daemon_Singleton::init(argc, argv); + } + } } diff --git a/Daemon.h b/Daemon.h index 7467a518..fd9afeec 100644 --- a/Daemon.h +++ b/Daemon.h @@ -33,8 +33,10 @@ namespace i2p #if defined(QT_GUI_LIB) // check if QT #define Daemon i2p::util::DaemonQT::Instance() - class DaemonQTImpl; - class DaemonQT: public i2p::util::Daemon_Singleton +#if 0 + class DaemonQTImpl; +#endif + class DaemonQT: public i2p::util::Daemon_Singleton { public: @@ -45,12 +47,14 @@ namespace i2p } bool init(int argc, char* argv[]); - void run (); +#if 0 + void run (); - private: + private: std::shared_ptr m_Impl; - }; +#endif + }; #elif defined(_WIN32) #define Daemon i2p::util::DaemonWin32::Instance() diff --git a/i2pd.cpp b/i2pd.cpp index f3ac6b3f..2e9d5a19 100644 --- a/i2pd.cpp +++ b/i2pd.cpp @@ -1,15 +1,54 @@ -#include -#include "Daemon.h" +#ifndef ANDROID +# include +# include "Daemon.h" +#else +# include "qt/i2pd_qt/i2pd_qt_gui.h" +# include +# include +# include "DaemonQT.h" +# include "mainwindow.h" +# include +#endif int main( int argc, char* argv[] ) { - if (Daemon.init(argc, argv)) +#ifdef ANDROID + //int result = runGUI(argc, argv); + //QMessageBox::information(0,"Debug","runGUI completed"); + QApplication app(argc, argv); + qDebug("Initialising the daemon..."); + bool daemonInitSuccess = i2p::util::DaemonQt::DaemonQTImpl::init(argc, argv); + if(!daemonInitSuccess) { + QMessageBox::critical(0, "Error", "Daemon init failed"); + return 1; + } + qDebug("Initialised, creating the main window..."); + MainWindow w; + qDebug("Before main window.show()..."); + w.show (); + int result; + { + i2p::util::DaemonQt::Controller daemonQtController; + qDebug("Starting the daemon..."); + emit daemonQtController.startDaemon(); + qDebug("Starting gui event loop..."); + result = app.exec(); + //QMessageBox::information(&w, "Debug", "exec finished"); + } + i2p::util::DaemonQt::DaemonQTImpl::deinit(); + //QMessageBox::information(&w, "Debug", "demon stopped"); + //exit(result); //return from main() causes intermittent sigsegv bugs in some Androids. exit() is a workaround for this + qDebug("Exiting the application"); + return result; +#else + if (Daemon.init(argc, argv)) { if (Daemon.start()) Daemon.run (); Daemon.stop(); } return EXIT_SUCCESS; +#endif } #ifdef _WIN32 diff --git a/qt/i2pd_qt/DaemonQT.cpp b/qt/i2pd_qt/DaemonQT.cpp index 41242e3b..121d547c 100644 --- a/qt/i2pd_qt/DaemonQT.cpp +++ b/qt/i2pd_qt/DaemonQT.cpp @@ -1,3 +1,83 @@ +#include "DaemonQT.h" +#include "../../Daemon.h" +#include +#include + +namespace i2p +{ +namespace util +{ +namespace DaemonQt +{ + +void Worker::startDaemon() { + qDebug("Performing daemon start..."); + DaemonQTImpl::start(); + qDebug("Daemon started."); + emit resultReady(); +} +void Worker::restartDaemon() { + qDebug("Performing daemon restart..."); + DaemonQTImpl::restart(); + qDebug("Daemon restarted."); + emit resultReady(); +} +void Worker::stopDaemon() { + qDebug("Performing daemon stop..."); + DaemonQTImpl::stop(); + qDebug("Daemon stopped."); + emit resultReady(); +} + +Controller::Controller() { + Worker *worker = new Worker; + worker->moveToThread(&workerThread); + connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater); + connect(this, &Controller::startDaemon, worker, &Worker::startDaemon); + connect(this, &Controller::stopDaemon, worker, &Worker::stopDaemon); + connect(this, &Controller::restartDaemon, worker, &Worker::restartDaemon); + connect(worker, &Worker::resultReady, this, &Controller::handleResults); + workerThread.start(); +} +Controller::~Controller() { + qDebug("Closing and waiting for daemon worker thread..."); + workerThread.quit(); + workerThread.wait(); + qDebug("Waiting for daemon worker thread finished."); + if(i2p::util::DaemonQt::DaemonQTImpl::isRunning()) { + qDebug("Stopping the daemon..."); + i2p::util::DaemonQt::DaemonQTImpl::stop(); + qDebug("Stopped the daemon."); + } +} + + + +static DaemonQTImpl::runningChangedCallback DaemonQTImpl_runningChanged; +static bool DaemonQTImpl_running; +static QMutex* mutex; + +bool DaemonQTImpl::init(int argc, char* argv[]){mutex=new QMutex(QMutex::Recursive);setRunningCallback(0);DaemonQTImpl_running=false;return Daemon.init(argc,argv);} +void DaemonQTImpl::deinit(){delete mutex;} +void DaemonQTImpl::start(){QMutexLocker locker(mutex);setRunning(true);Daemon.start();} +void DaemonQTImpl::stop(){QMutexLocker locker(mutex);Daemon.stop();setRunning(false);} +void DaemonQTImpl::restart(){QMutexLocker locker(mutex);stop();start();} + +void DaemonQTImpl::setRunningCallback(runningChangedCallback cb){DaemonQTImpl_runningChanged=cb;} +bool DaemonQTImpl::isRunning(){return DaemonQTImpl_running;} +void DaemonQTImpl::setRunning(bool newValue){ + bool oldValue = DaemonQTImpl_running; + if(oldValue!=newValue) { + DaemonQTImpl_running = newValue; + if(DaemonQTImpl_runningChanged!=0)DaemonQTImpl_runningChanged(); + } +} + +} +} +} + +#if 0 #include #include "mainwindow.h" #include @@ -19,7 +99,7 @@ namespace util void Run () { - MainWindow w; + MainWindow w(m_App); w.show (); m_App.exec(); } @@ -62,3 +142,4 @@ namespace util } } } +#endif diff --git a/qt/i2pd_qt/DaemonQT.h b/qt/i2pd_qt/DaemonQT.h index 5c0ec14e..1d57d7e8 100644 --- a/qt/i2pd_qt/DaemonQT.h +++ b/qt/i2pd_qt/DaemonQT.h @@ -1,4 +1,66 @@ #ifndef DAEMONQT_H #define DAEMONQT_H +#include +#include + +namespace i2p +{ +namespace util +{ +namespace DaemonQt +{ + class Worker : public QObject + { + Q_OBJECT + + public slots: + void startDaemon(); + void restartDaemon(); + void stopDaemon(); + + signals: + void resultReady(); + }; + + class DaemonQTImpl + { + public: + typedef void (*runningChangedCallback)(); + + /** + * @brief init + * @param argc + * @param argv + * @return success + */ + bool static init(int argc, char* argv[]); + void static deinit(); + void static start(); + void static stop(); + void static restart(); + void static setRunningCallback(runningChangedCallback cb); + bool static isRunning(); + private: + void static setRunning(bool running); + }; + + class Controller : public QObject + { + Q_OBJECT + QThread workerThread; + public: + Controller(); + ~Controller(); + public slots: + void handleResults(){} + signals: + void startDaemon(); + void stopDaemon(); + void restartDaemon(); + }; +} +} +} + #endif // DAEMONQT_H diff --git a/qt/i2pd_qt/android/AndroidManifest.xml b/qt/i2pd_qt/android/AndroidManifest.xml index 603776e7..73f6eb6d 100644 --- a/qt/i2pd_qt/android/AndroidManifest.xml +++ b/qt/i2pd_qt/android/AndroidManifest.xml @@ -1,6 +1,6 @@ - + @@ -46,7 +46,7 @@ - +