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