d5e46fac81bb4ce20ed77b3963bd88d195c51431
[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 #if 0
42     if (mp_hs != NULL) {
43         delete mp_hs;
44     }
45 #endif
46 }
47
48 void HomescreenHandler::init(void)
49 {
50 #if 0
51     mp_hs = new LibHomeScreen();
52     mp_hs->init(port, token);
53 #endif
54     myThis = this;
55
56     /*
57      * The "started" signal is received any time a start request is made to applaunchd,
58      * and the application either starts successfully or is already running. This
59      * effectively acts as a "switch to app X" action.
60      */
61     connect(applaunch_iface, SIGNAL(started(QString)), this, SLOT(appStarted(QString)));
62     connect(applaunch_iface, SIGNAL(terminated(QString)), this, SLOT(appTerminated(QString)));
63
64 #if 0
65     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
66
67     mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
68         const char *display_message = json_object_get_string(
69             json_object_object_get(object, "display_message"));
70         HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
71     });
72
73     // should be handled in the top panel
74     mp_hs->set_event_handler(LibHomeScreen::Event_ShowNotification,[this](json_object *object){
75        json_object *p_obj = json_object_object_get(object, "parameter");
76        const char *icon = json_object_get_string(
77                    json_object_object_get(p_obj, "icon"));
78        const char *text = json_object_get_string(
79                    json_object_object_get(p_obj, "text"));
80        const char *app_id = json_object_get_string(
81                    json_object_object_get(p_obj, "caller"));
82        HMI_DEBUG("HomeScreen","Event_ShowNotification icon=%s, text=%s, caller=%s", icon, text, app_id);
83        QFileInfo icon_file(icon);
84        QString icon_path;
85        if (icon_file.isFile() && icon_file.exists()) {
86            icon_path = QString(QLatin1String(icon));
87        } else {
88            icon_path = "./images/Utility_Logo_Grey-01.svg";
89        }
90
91        emit showNotification(QString(QLatin1String(app_id)), icon_path, QString(QLatin1String(text)));
92     });
93
94     // should be handled in the bottom panel
95     mp_hs->set_event_handler(LibHomeScreen::Event_ShowInformation,[this](json_object *object){
96        json_object *p_obj = json_object_object_get(object, "parameter");
97        const char *info = json_object_get_string(
98                    json_object_object_get(p_obj, "info"));
99
100        emit showInformation(QString(QLatin1String(info)));
101     });
102 #endif
103 }
104
105 static struct wl_output *
106 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
107 {
108         void *output = native->nativeResourceForScreen("output", screen);
109         return static_cast<struct ::wl_output*>(output);
110 }
111
112 void HomescreenHandler::tapShortcut(QString application_id)
113 {
114         HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
115 #if 0
116         struct json_object* j_json = json_object_new_object();
117         struct json_object* value;
118
119         struct agl_shell *agl_shell = aglShell->shell.get();
120         QPlatformNativeInterface *native = qApp->platformNativeInterface();
121         struct wl_output *output = getWlOutput(native, qApp->screens().first());
122
123         value = json_object_new_string("normal.full");
124         json_object_object_add(j_json, "area", value);
125
126         mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
127
128         // this works (and it is redundant the first time), due to the default
129         // policy engine installed which actives the application, when starting
130         // the first time. Later calls to HomescreenHandler::tapShortcut will
131         // require calling 'agl_shell_activate_app'
132         agl_shell_activate_app(agl_shell, application_id.toStdString().c_str(), output);
133
134 #endif
135
136     if (mp_launcher) {
137         mp_launcher->setCurrent(application_id);
138     }
139     appStarted(application_id);
140 }
141
142 #if 0
143 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
144 {
145     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
146 }
147
148 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
149 {
150     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
151 }
152
153 void HomescreenHandler::onRep(struct json_object* reply_contents)
154 {
155     const char* str = json_object_to_json_string(reply_contents);
156     HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
157 }
158
159 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
160 {
161     const char* str = json_object_to_json_string(event_contents);
162     HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
163
164     if (event.compare("homescreen/on_screen_message") == 0) {
165         struct json_object *json_data = json_object_object_get(event_contents, "data");
166         struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
167         const char* display_message = json_object_get_string(json_display_message);
168
169         HMI_DEBUG("HomeScreen","display_message = %s", display_message);
170     }
171 }
172 #endif
173
174 void HomescreenHandler::appStarted(const QString& application_id)
175 {
176     struct agl_shell *agl_shell = aglShell->shell.get();
177     QPlatformNativeInterface *native = qApp->platformNativeInterface();
178     struct wl_output *output = getWlOutput(native, qApp->screens().first());
179
180     HMI_DEBUG("HomeScreen", "Activating application %s", application_id.toStdString().c_str());
181     agl_shell_activate_app(agl_shell, application_id.toStdString().c_str(), output);
182 }
183
184 void HomescreenHandler::appTerminated(const QString& application_id)
185 {
186     HMI_DEBUG("HomeScreen", "Application %s terminated, activating launcher", application_id.toStdString().c_str());
187     appStarted("launcher");
188 }