AglShellGrpcClient: Add activation with gRPC proxy
[apps/homescreen.git] / homescreen / src / main.cpp
index d2967ca..b004fd5 100644 (file)
@@ -3,6 +3,7 @@
  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
  * Copyright (c) 2017, 2018 TOYOTA MOTOR CORPORATION
  * Copyright (c) 2022 Konsulko Group
+ * Copyright (c) 2023 Collabora, Ltd.
  */
 
 #include <QGuiApplication>
@@ -32,6 +33,9 @@
 #include "agl-shell-client-protocol.h"
 #include "shell.h"
 
+#include <thread>
+#include "AglShellGrpcClient.h"
+
 #ifndef MIN
 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
 #endif
@@ -102,6 +106,10 @@ agl_shell_app_state(void *data, struct agl_shell *agl_shell,
                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;
        }
@@ -117,9 +125,28 @@ agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
        if (!homescreenHandler)
                return;
 
+       // a couple of use-cases, if there is no app_id in the app_list then it
+       // means this is a request to map the application, from the start to a
+       // different output that the default one. We'd get an
+       // AGL_SHELL_APP_STATE_STARTED which will handle activation.
+       //
+       // if there's an app_id then it means we might have gotten an event to
+       // move the application to another output; so we'd need to process it
+       // by explicitly calling processAppStatusEvent() which would ultimately
+       // activate the application on other output. We'd have to pick-up the
+       // last activated window and activate the default output.
+       //
+       // finally if the outputs are identical probably that's an user-error -
+       // but the compositor won't activate it again, so we don't handle that.
        std::pair new_pending_app = std::pair(QString(app_id),
                                              QString(output_name));
        homescreenHandler->pending_app_list.push_back(new_pending_app);
+
+       if (homescreenHandler->apps_stack.contains(QString(app_id))) {
+               qDebug() << "Gove event to move " << app_id <<
+                       " to another output " << output_name;
+               homescreenHandler->processAppStatusEvent(app_id, "started");
+       }
 }
 
 
@@ -338,6 +365,20 @@ load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engi
        });
 }
 
+static void
+run_in_thread(GrpcClient *client)
+{
+        grpc::Status status = client->Wait();
+}
+
+static void
+app_status_callback(::agl_shell_ipc::AppStateResponse app_response)
+{
+       std::cout << " >> AppStateResponse app_id " <<
+               app_response.app_id() << ", with state " <<
+               app_response.state() << std::endl;
+}
+
 int main(int argc, char *argv[])
 {
        setenv("QT_QPA_PLATFORM", "wayland", 1);
@@ -393,7 +434,6 @@ int main(int argc, char *argv[])
 
 
        std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
-       Shell *aglShell = new Shell(agl_shell, &app);
 
        // Import C++ class to QML
        qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
@@ -402,7 +442,13 @@ int main(int argc, char *argv[])
        ApplicationLauncher *launcher = new ApplicationLauncher();
        launcher->setCurrent(QStringLiteral("launcher"));
 
-       HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
+       GrpcClient *client = new GrpcClient();
+
+       // create a new thread to listner for gRPC events
+       std::thread th = std::thread(run_in_thread, client);
+       client->AppStatusState(app_status_callback);
+
+       HomescreenHandler* homescreenHandler = new HomescreenHandler(launcher, client);
        shell_data.homescreenHandler = homescreenHandler;
 
        QQmlApplicationEngine engine;
@@ -413,9 +459,6 @@ int main(int argc, char *argv[])
        context->setContextProperty("weather", new Weather());
        context->setContextProperty("bluetooth", new Bluetooth(false, context));
 
-       // We add it here even if we don't use it
-       context->setContextProperty("shell", aglShell);
-
        load_agl_shell_app(native, &engine, shell_data.shell,
                           screen_name, is_demo_val);