homescreenhandler: Remove old artifacts
[apps/homescreen.git] / homescreen / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <QGuiApplication>
18 #include <QFileInfo>
19 #include "homescreenhandler.h"
20 #include <functional>
21 #include "hmi-debug.h"
22
23 #include <qpa/qplatformnativeinterface.h>
24
25 #define APPLAUNCH_DBUS_IFACE     "org.automotivelinux.AppLaunch"
26 #define APPLAUNCH_DBUS_OBJECT    "/org/automotivelinux/AppLaunch"
27
28 void* HomescreenHandler::myThis = 0;
29
30 HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *launcher, QObject *parent) :
31     QObject(parent),
32     aglShell(_aglShell)
33 {
34     mp_launcher = launcher;
35     applaunch_iface = new org::automotivelinux::AppLaunch(APPLAUNCH_DBUS_IFACE, APPLAUNCH_DBUS_OBJECT,
36                                                           QDBusConnection::sessionBus(), this);
37 }
38
39 HomescreenHandler::~HomescreenHandler()
40 {
41 }
42
43 void HomescreenHandler::init(void)
44 {
45     myThis = this;
46
47     /*
48      * The "started" signal is received any time a start request is made to applaunchd,
49      * and the application either starts successfully or is already running. This
50      * effectively acts as a "switch to app X" action.
51      */
52     connect(applaunch_iface, SIGNAL(started(QString)), this, SLOT(appStarted(QString)));
53     connect(applaunch_iface, SIGNAL(terminated(QString)), this, SLOT(appTerminated(QString)));
54
55 }
56
57 static struct wl_output *
58 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
59 {
60         void *output = native->nativeResourceForScreen("output", screen);
61         return static_cast<struct ::wl_output*>(output);
62 }
63
64 void HomescreenHandler::tapShortcut(QString application_id)
65 {
66     HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
67
68     QDBusPendingReply<> reply = applaunch_iface->start(application_id);
69     reply.waitForFinished();
70     if (reply.isError()) {
71         HMI_ERROR("HomeScreen","Unable to start application '%s': %s",
72             application_id.toStdString().c_str(),
73             reply.error().message().toStdString().c_str());
74     } else {
75         if (mp_launcher) {
76             mp_launcher->setCurrent(application_id);
77         }
78         appStarted(application_id);
79     }
80 }
81
82 void HomescreenHandler::appStarted(const QString& application_id)
83 {
84     struct agl_shell *agl_shell = aglShell->shell.get();
85     QPlatformNativeInterface *native = qApp->platformNativeInterface();
86     struct wl_output *output = getWlOutput(native, qApp->screens().first());
87
88     HMI_DEBUG("HomeScreen", "Activating application %s", application_id.toStdString().c_str());
89     agl_shell_activate_app(agl_shell, application_id.toStdString().c_str(), output);
90 }
91
92 void HomescreenHandler::appTerminated(const QString& application_id)
93 {
94     HMI_DEBUG("HomeScreen", "Application %s terminated, activating launcher", application_id.toStdString().c_str());
95     appStarted("launcher");
96 }