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