homescreen: Add support for v3 of agl-shell protocol
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
4  * Copyright (c) 2022 Konsulko Group
5  */
6
7 #include <QGuiApplication>
8 #include <QFileInfo>
9 #include <functional>
10
11 #include "homescreenhandler.h"
12 #include "hmi-debug.h"
13
14 #include <qpa/qplatformnativeinterface.h>
15
16 // LAUNCHER_APP_ID shouldn't be started by applaunchd as it is started as
17 // a user session by systemd
18 #define LAUNCHER_APP_ID          "launcher"
19
20 HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *launcher, QObject *parent) :
21         QObject(parent),
22         aglShell(_aglShell)
23 {
24         mp_launcher = launcher;
25         mp_applauncher_client = new AppLauncherClient();
26
27         //
28         // The "started" event is received any time a start request is made to applaunchd,
29         // and the application either starts successfully or is already running. This
30         // effectively acts as a "switch to app X" action.
31         //
32         connect(mp_applauncher_client,
33                 &AppLauncherClient::appStatusEvent,
34                 this,
35                 &HomescreenHandler::processAppStatusEvent);
36 }
37
38 HomescreenHandler::~HomescreenHandler()
39 {
40         delete mp_applauncher_client;
41 }
42
43 static struct wl_output *
44 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
45 {
46         void *output = native->nativeResourceForScreen("output", screen);
47         return static_cast<struct ::wl_output*>(output);
48 }
49
50 void HomescreenHandler::tapShortcut(QString app_id)
51 {
52         HMI_DEBUG("HomeScreen","tapShortcut %s", app_id.toStdString().c_str());
53
54         if (app_id == LAUNCHER_APP_ID) {
55                 activateApp(app_id);
56                 return;
57         }
58
59         if (!mp_applauncher_client->startApplication(app_id)) {
60                 HMI_ERROR("HomeScreen","Unable to start application '%s'",
61                           app_id.toStdString().c_str());
62                 return;
63         }
64 }
65
66 /*
67  * Keep track of currently running apps and the order in which
68  * they were activated. That way, when an app is closed, we can
69  * switch back to the previously active one.
70  */
71 void HomescreenHandler::addAppToStack(const QString& app_id)
72 {
73         if (app_id == "homescreen")
74                 return;
75
76         if (!apps_stack.contains(app_id)) {
77                 apps_stack << app_id;
78         } else {
79                 int current_pos = apps_stack.indexOf(app_id);
80                 int last_pos = apps_stack.size() - 1;
81
82                 if (current_pos != last_pos)
83                         apps_stack.move(current_pos, last_pos);
84         }
85 }
86
87 void HomescreenHandler::activateApp(const QString& app_id)
88 {
89         struct agl_shell *agl_shell = aglShell->shell.get();
90         QPlatformNativeInterface *native = qApp->platformNativeInterface();
91         struct wl_output *output = getWlOutput(native, qApp->screens().first());
92
93         if (mp_launcher) {
94                 mp_launcher->setCurrent(app_id);
95         }
96
97         HMI_DEBUG("HomeScreen", "Activating application %s", app_id.toStdString().c_str());
98         agl_shell_activate_app(agl_shell, app_id.toStdString().c_str(), output);
99 }
100
101 void HomescreenHandler::deactivateApp(const QString& app_id)
102 {
103         if (apps_stack.contains(app_id)) {
104                 apps_stack.removeOne(app_id);
105                 if (!apps_stack.isEmpty())
106                         activateApp(apps_stack.last());
107         }
108 }
109
110 void HomescreenHandler::processAppStatusEvent(const QString &app_id, const QString &status)
111 {
112         HMI_DEBUG("HomeScreen", "Processing application %s, status %s",
113                         app_id.toStdString().c_str(), status.toStdString().c_str());
114
115         if (status == "started") {
116                 activateApp(app_id);
117         } else if (status == "terminated") {
118                 HMI_DEBUG("HomeScreen", "Application %s terminated, activating last app", app_id.toStdString().c_str());
119                 deactivateApp(app_id);
120         }
121 }