935371368f3cf5af84da5fa64a0c2ea4042ee578
[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 QScreen *find_screen(const char *output);
15
16 // defined by meson build file
17 #include QT_QPA_HEADER
18
19 // LAUNCHER_APP_ID shouldn't be started by applaunchd as it is started as
20 // a user session by systemd
21 #define LAUNCHER_APP_ID          "launcher"
22
23 static struct wl_output *
24 getWlOutput(QPlatformNativeInterface *native, QScreen *screen);
25
26 HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *launcher, QObject *parent) :
27         QObject(parent),
28         aglShell(_aglShell)
29 {
30         mp_launcher = launcher;
31         mp_applauncher_client = new AppLauncherClient();
32
33         //
34         // The "started" event is received any time a start request is made to applaunchd,
35         // and the application either starts successfully or is already running. This
36         // effectively acts as a "switch to app X" action.
37         //
38         connect(mp_applauncher_client,
39                 &AppLauncherClient::appStatusEvent,
40                 this,
41                 &HomescreenHandler::processAppStatusEvent);
42 }
43
44 HomescreenHandler::~HomescreenHandler()
45 {
46         delete mp_applauncher_client;
47 }
48
49 static struct wl_output *
50 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
51 {
52         void *output = native->nativeResourceForScreen("output", screen);
53         return static_cast<struct ::wl_output*>(output);
54 }
55
56 void HomescreenHandler::tapShortcut(QString app_id)
57 {
58         HMI_DEBUG("HomeScreen","tapShortcut %s", app_id.toStdString().c_str());
59
60         if (app_id == LAUNCHER_APP_ID) {
61                 activateApp(app_id);
62                 return;
63         }
64
65         if (!mp_applauncher_client->startApplication(app_id)) {
66                 HMI_ERROR("HomeScreen","Unable to start application '%s'",
67                           app_id.toStdString().c_str());
68                 return;
69         }
70 }
71
72 /*
73  * Keep track of currently running apps and the order in which
74  * they were activated. That way, when an app is closed, we can
75  * switch back to the previously active one.
76  */
77 void HomescreenHandler::addAppToStack(const QString& app_id)
78 {
79         if (app_id == "homescreen")
80                 return;
81
82         if (!apps_stack.contains(app_id)) {
83                 apps_stack << app_id;
84         } else {
85                 int current_pos = apps_stack.indexOf(app_id);
86                 int last_pos = apps_stack.size() - 1;
87
88                 if (current_pos != last_pos)
89                         apps_stack.move(current_pos, last_pos);
90         }
91 }
92
93 void HomescreenHandler::activateApp(const QString& app_id)
94 {
95         struct agl_shell *agl_shell = aglShell->shell.get();
96         QScreen *tmp_screen = qApp->screens().first();
97         QPlatformNativeInterface *native = qApp->platformNativeInterface();
98         struct wl_output *mm_output = nullptr;
99
100         if (!tmp_screen) {
101                 HMI_DEBUG("HomeScreen", "No output found to activate on!\n");
102         } else {
103                 mm_output = getWlOutput(native, tmp_screen);
104
105                 HMI_DEBUG("HomeScreen", "Activating app_id %s by default on output %p\n",
106                                 app_id.toStdString().c_str(), mm_output);
107         }
108
109         if (mp_launcher) {
110                 mp_launcher->setCurrent(app_id);
111         }
112
113         // search for a pending application which might have a different output
114         auto iter = pending_app_list.begin();
115         bool found_pending_app = false;
116         while (iter != pending_app_list.end()) {
117                 const QString &app_to_search = iter->first;
118
119                 if (app_to_search == app_id) {
120                         found_pending_app = true;
121                         HMI_DEBUG("HomeScreen", "Found app_id %s in pending list  of applications",
122                                         app_id.toStdString().c_str());
123                         break;
124                 }
125
126                 iter++;
127         }
128
129         if (found_pending_app) {
130                 const QString &output_name = iter->second;
131                 QScreen *screen =
132                         ::find_screen(output_name.toStdString().c_str());
133
134                 if (!screen) {
135                         HMI_DEBUG("HomeScreen", "Can't activate application %s on another "
136                                   "output, because output %s could not be found. "
137                                   "Trying with remoting ones.",
138                                   app_id.toStdString().c_str(),
139                                   output_name.toStdString().c_str());
140
141                         // try with remoting-remote-X which is the streaming
142                         // one
143                         std::string new_remote_output = 
144                                 "remoting-" + output_name.toStdString();
145
146                         screen = ::find_screen(new_remote_output.c_str());
147                         if (!screen) {
148                                 HMI_DEBUG("HomeScreen", "Can't activate application %s on another "
149                                           "output, because output remoting-%s could not be found",
150                                           app_id.toStdString().c_str(),
151                                           output_name.toStdString().c_str());
152                                 return;
153                         }
154
155                         HMI_DEBUG("HomeScreen", "Found a stream remoting output %s to activate application %s on",
156                                   new_remote_output.c_str(),
157                                   app_id.toStdString().c_str());
158                 }
159
160                 mm_output = getWlOutput(native, screen);
161                 pending_app_list.erase(iter);
162
163                 HMI_DEBUG("HomeScreen", "For application %s found another "
164                                 "output to activate %s\n",
165                                 app_id.toStdString().c_str(),
166                                 output_name.toStdString().c_str());
167         }
168
169         if (!mm_output) {
170                 HMI_DEBUG("HomeScreen", "No suitable output found for activating %s",
171                                 app_id.toStdString().c_str());
172                 return;
173         }
174
175         HMI_DEBUG("HomeScreen", "Activating application %s",
176                         app_id.toStdString().c_str());
177
178         agl_shell_activate_app(agl_shell, app_id.toStdString().c_str(), mm_output);
179 }
180
181 void HomescreenHandler::deactivateApp(const QString& app_id)
182 {
183         if (apps_stack.contains(app_id)) {
184                 apps_stack.removeOne(app_id);
185                 if (!apps_stack.isEmpty())
186                         activateApp(apps_stack.last());
187         }
188 }
189
190 void HomescreenHandler::processAppStatusEvent(const QString &app_id, const QString &status)
191 {
192         HMI_DEBUG("HomeScreen", "Processing application %s, status %s",
193                         app_id.toStdString().c_str(), status.toStdString().c_str());
194
195         if (status == "started") {
196                 activateApp(app_id);
197         } else if (status == "terminated") {
198                 HMI_DEBUG("HomeScreen", "Application %s terminated, activating last app", app_id.toStdString().c_str());
199                 deactivateApp(app_id);
200         } else if (status == "deactivated") {
201                 HMI_DEBUG("HomeScreen", "Application %s deactivated, activating last app", app_id.toStdString().c_str());
202         }
203 }