918237a48788e46f03bf6940f6e8c429213b0463
[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         QPlatformNativeInterface *native = qApp->platformNativeInterface();
33
34         //
35         // The "started" event is received any time a start request is made to applaunchd,
36         // and the application either starts successfully or is already running. This
37         // effectively acts as a "switch to app X" action.
38         //
39         connect(mp_applauncher_client,
40                 &AppLauncherClient::appStatusEvent,
41                 this,
42                 &HomescreenHandler::processAppStatusEvent);
43 }
44
45 HomescreenHandler::~HomescreenHandler()
46 {
47         delete mp_applauncher_client;
48 }
49
50 static struct wl_output *
51 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
52 {
53         void *output = native->nativeResourceForScreen("output", screen);
54         return static_cast<struct ::wl_output*>(output);
55 }
56
57 void HomescreenHandler::tapShortcut(QString app_id)
58 {
59         HMI_DEBUG("HomeScreen","tapShortcut %s", app_id.toStdString().c_str());
60
61         if (app_id == LAUNCHER_APP_ID) {
62                 activateApp(app_id);
63                 return;
64         }
65
66         if (!mp_applauncher_client->startApplication(app_id)) {
67                 HMI_ERROR("HomeScreen","Unable to start application '%s'",
68                           app_id.toStdString().c_str());
69                 return;
70         }
71 }
72
73 /*
74  * Keep track of currently running apps and the order in which
75  * they were activated. That way, when an app is closed, we can
76  * switch back to the previously active one.
77  */
78 void HomescreenHandler::addAppToStack(const QString& app_id)
79 {
80         if (app_id == "homescreen")
81                 return;
82
83         if (!apps_stack.contains(app_id)) {
84                 apps_stack << app_id;
85         } else {
86                 int current_pos = apps_stack.indexOf(app_id);
87                 int last_pos = apps_stack.size() - 1;
88
89                 if (current_pos != last_pos)
90                         apps_stack.move(current_pos, last_pos);
91         }
92 }
93
94 void HomescreenHandler::activateApp(const QString& app_id)
95 {
96         struct agl_shell *agl_shell = aglShell->shell.get();
97         QPlatformNativeInterface *native = qApp->platformNativeInterface();
98         struct wl_output *mm_output = getWlOutput(native, qApp->screens().first());
99
100         if (mp_launcher) {
101                 mp_launcher->setCurrent(app_id);
102         }
103         HMI_DEBUG("HomeScreen", "Activating app_id %s by default output %p\n",
104                         app_id.toStdString().c_str(), mm_output);
105
106         // search for a pending application which might have a different output
107         auto iter = pending_app_list.begin();
108         bool found_pending_app = false;
109         while (iter != pending_app_list.end()) {
110                 const QString &app_to_search = iter->first;
111
112                 if (app_to_search == app_id) {
113                         found_pending_app = true;
114                         break;
115                 }
116
117                 iter++;
118         }
119
120         if (found_pending_app) {
121                 const QString &output_name = iter->second;
122                 QScreen *screen =
123                         ::find_screen(output_name.toStdString().c_str());
124
125                 mm_output = getWlOutput(native, screen);
126                 pending_app_list.erase(iter);
127
128                 HMI_DEBUG("HomeScreen", "For application %s found another "
129                                 "output to activate %s\n",
130                                 app_id.toStdString().c_str(),
131                                 output_name.toStdString().c_str());
132         }
133
134         HMI_DEBUG("HomeScreen", "Activating application %s",
135                         app_id.toStdString().c_str());
136
137         agl_shell_activate_app(agl_shell, app_id.toStdString().c_str(), mm_output);
138 }
139
140 void HomescreenHandler::deactivateApp(const QString& app_id)
141 {
142         if (apps_stack.contains(app_id)) {
143                 apps_stack.removeOne(app_id);
144                 if (!apps_stack.isEmpty())
145                         activateApp(apps_stack.last());
146         }
147 }
148
149 void HomescreenHandler::processAppStatusEvent(const QString &app_id, const QString &status)
150 {
151         HMI_DEBUG("HomeScreen", "Processing application %s, status %s",
152                         app_id.toStdString().c_str(), status.toStdString().c_str());
153
154         if (status == "started") {
155                 activateApp(app_id);
156         } else if (status == "terminated") {
157                 HMI_DEBUG("HomeScreen", "Application %s terminated, activating last app", app_id.toStdString().c_str());
158                 deactivateApp(app_id);
159         }
160 }