main: Handle terminating event
[apps/homescreen.git] / homescreen / src / main.cpp
index 8598a33..a98a15b 100644 (file)
@@ -16,6 +16,7 @@
 #include <QtQml/qqml.h>
 #include <QQuickWindow>
 #include <QTimer>
+#include <QScreen>
 
 #include <weather.h>
 #include <bluetooth.h>
@@ -31,7 +32,6 @@
 #include <wayland-client.h>
 
 #include "agl-shell-client-protocol.h"
-#include "shell.h"
 
 #include <thread>
 #include "AglShellGrpcClient.h"
@@ -87,34 +87,7 @@ static void
 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
                const char *app_id, uint32_t state)
 {
-#if 0
-       struct shell_data *shell_data = static_cast<struct shell_data *>(data);
-       HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
-
-       if (!homescreenHandler)
-               return;
-
-       switch (state) {
-       case AGL_SHELL_APP_STATE_STARTED:
-               qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
-               homescreenHandler->processAppStatusEvent(app_id, "started");
-               break;
-       case AGL_SHELL_APP_STATE_TERMINATED:
-               qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
-               // handled by HomescreenHandler::processAppStatusEvent
-               break;
-       case AGL_SHELL_APP_STATE_ACTIVATED:
-               qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
-               homescreenHandler->addAppToStack(app_id);
-               break;
-       case AGL_SHELL_APP_STATE_DEACTIVATED:
-               qDebug() << "Got AGL_SHELL_APP_STATE_DEACTIVATED for app_id " << app_id;
-               homescreenHandler->processAppStatusEvent(app_id, "deactivated");
-               break;
-       default:
-               break;
-       }
-#endif
+       /* unused */
 }
 
 static void
@@ -145,8 +118,9 @@ agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
        homescreenHandler->pending_app_list.push_back(new_pending_app);
 
        if (homescreenHandler->apps_stack.contains(QString(app_id))) {
-               qDebug() << "Gove event to move " << app_id <<
+               qDebug() << "Got event to move " << app_id <<
                        " to another output " << output_name;
+
                homescreenHandler->processAppStatusEvent(app_id, "started");
        }
 }
@@ -348,8 +322,7 @@ run_in_thread(GrpcClient *client)
 
 static void
 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
-                  struct shell_data shell_data, const char *screen_name,
-                  bool is_demo, GrpcClient *client)
+                  struct shell_data shell_data, const char *screen_name, bool is_demo)
 {
        QScreen *screen = nullptr;
        HomescreenHandler *homescreenHandler = shell_data.homescreenHandler;
@@ -399,7 +372,7 @@ app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data)
                break;
        case AGL_SHELL_APP_STATE_TERMINATED:
                qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
-               // handled by HomescreenHandler::processAppStatusEvent
+               homescreenHandler->processAppStatusEvent(app_id, "terminated");
                break;
        case AGL_SHELL_APP_STATE_ACTIVATED:
                qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
@@ -445,6 +418,9 @@ int main(int argc, char *argv[])
        // we need to have an app_id
        app.setDesktopFileName("homescreen");
 
+       GrpcClient *client = new GrpcClient();
+       // create a new thread to listner for gRPC events
+       std::thread th = std::thread(run_in_thread, client);
 
        register_agl_shell(native, &shell_data);
        if (!shell_data.shell) {
@@ -471,10 +447,6 @@ int main(int argc, char *argv[])
 
        std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
 
-       GrpcClient *client = new GrpcClient();
-       // create a new thread to listner for gRPC events
-       std::thread th = std::thread(run_in_thread, client);
-
        // Import C++ class to QML
        qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
        qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
@@ -486,6 +458,9 @@ int main(int argc, char *argv[])
        shell_data.homescreenHandler = homescreenHandler;
        shell_data.homescreenHandler->setGrpcClient(client);
 
+       // blocks until we're sure connected with the server
+       HMI_DEBUG("HomescreenHandler", "Checking if connected to the gRPC server...");
+       client->WaitForConnected(500, 10);
        client->AppStatusState(app_status_callback, homescreenHandler);
 
        QQmlApplicationEngine engine;
@@ -497,7 +472,7 @@ int main(int argc, char *argv[])
        context->setContextProperty("bluetooth", new Bluetooth(false, context));
 
        load_agl_shell_app(native, &engine, shell_data,
-                          screen_name, is_demo_val, client);
+                          screen_name, is_demo_val);
 
        return app.exec();
 }