2 * Copyright (c) 2017, 2018, 2019 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <QGuiApplication>
19 #include "homescreenhandler.h"
21 #include "hmi-debug.h"
23 #include <qpa/qplatformnativeinterface.h>
25 #define APPLAUNCH_DBUS_IFACE "org.automotivelinux.AppLaunch"
26 #define APPLAUNCH_DBUS_OBJECT "/org/automotivelinux/AppLaunch"
28 void* HomescreenHandler::myThis = 0;
30 HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *launcher, QObject *parent) :
34 mp_launcher = launcher;
35 applaunch_iface = new org::automotivelinux::AppLaunch(APPLAUNCH_DBUS_IFACE, APPLAUNCH_DBUS_OBJECT,
36 QDBusConnection::sessionBus(), this);
39 HomescreenHandler::~HomescreenHandler()
48 void HomescreenHandler::init(void)
51 mp_hs = new LibHomeScreen();
52 mp_hs->init(port, token);
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.
61 connect(applaunch_iface, SIGNAL(started(QString)), this, SLOT(appStarted(QString)));
62 connect(applaunch_iface, SIGNAL(terminated(QString)), this, SLOT(appTerminated(QString)));
65 mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
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);
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);
85 if (icon_file.isFile() && icon_file.exists()) {
86 icon_path = QString(QLatin1String(icon));
88 icon_path = "./images/Utility_Logo_Grey-01.svg";
91 emit showNotification(QString(QLatin1String(app_id)), icon_path, QString(QLatin1String(text)));
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"));
100 emit showInformation(QString(QLatin1String(info)));
105 static struct wl_output *
106 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
108 void *output = native->nativeResourceForScreen("output", screen);
109 return static_cast<struct ::wl_output*>(output);
112 void HomescreenHandler::tapShortcut(QString application_id)
114 HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
116 struct json_object* j_json = json_object_new_object();
117 struct json_object* value;
119 struct agl_shell *agl_shell = aglShell->shell.get();
120 QPlatformNativeInterface *native = qApp->platformNativeInterface();
121 struct wl_output *output = getWlOutput(native, qApp->screens().first());
123 value = json_object_new_string("normal.full");
124 json_object_object_add(j_json, "area", value);
126 mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
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);
137 mp_launcher->setCurrent(application_id);
139 appStarted(application_id);
143 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
145 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
148 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
150 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
153 void HomescreenHandler::onRep(struct json_object* reply_contents)
155 const char* str = json_object_to_json_string(reply_contents);
156 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
159 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
161 const char* str = json_object_to_json_string(event_contents);
162 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
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);
169 HMI_DEBUG("HomeScreen","display_message = %s", display_message);
174 void HomescreenHandler::appStarted(const QString& application_id)
176 struct agl_shell *agl_shell = aglShell->shell.get();
177 QPlatformNativeInterface *native = qApp->platformNativeInterface();
178 struct wl_output *output = getWlOutput(native, qApp->screens().first());
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);
184 void HomescreenHandler::appTerminated(const QString& application_id)
186 HMI_DEBUG("HomeScreen", "Application %s terminated, activating launcher", application_id.toStdString().c_str());
187 appStarted("launcher");