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 void* HomescreenHandler::myThis = 0;
27 HomescreenHandler::HomescreenHandler(Shell *_aglShell, ApplicationLauncher *launcher, QObject *parent) :
31 mp_launcher = launcher;
34 HomescreenHandler::~HomescreenHandler()
41 void HomescreenHandler::init(int port, const char *token)
43 mp_hs = new LibHomeScreen();
44 mp_hs->init(port, token);
49 mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
51 mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
52 const char *display_message = json_object_get_string(
53 json_object_object_get(object, "display_message"));
54 HMI_DEBUG("HomeScreen","set_event_handler Event_OnScreenMessage display_message = %s", display_message);
57 // should be handled in the top panel
58 mp_hs->set_event_handler(LibHomeScreen::Event_ShowNotification,[this](json_object *object){
59 json_object *p_obj = json_object_object_get(object, "parameter");
60 const char *icon = json_object_get_string(
61 json_object_object_get(p_obj, "icon"));
62 const char *text = json_object_get_string(
63 json_object_object_get(p_obj, "text"));
64 const char *app_id = json_object_get_string(
65 json_object_object_get(p_obj, "caller"));
66 HMI_DEBUG("HomeScreen","Event_ShowNotification icon=%s, text=%s, caller=%s", icon, text, app_id);
67 QFileInfo icon_file(icon);
69 if (icon_file.isFile() && icon_file.exists()) {
70 icon_path = QString(QLatin1String(icon));
72 icon_path = "./images/Utility_Logo_Grey-01.svg";
75 emit showNotification(QString(QLatin1String(app_id)), icon_path, QString(QLatin1String(text)));
78 // should be handled in the bottom panel
79 mp_hs->set_event_handler(LibHomeScreen::Event_ShowInformation,[this](json_object *object){
80 json_object *p_obj = json_object_object_get(object, "parameter");
81 const char *info = json_object_get_string(
82 json_object_object_get(p_obj, "info"));
84 emit showInformation(QString(QLatin1String(info)));
88 static struct wl_output *
89 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
91 void *output = native->nativeResourceForScreen("output", screen);
92 return static_cast<struct ::wl_output*>(output);
95 void HomescreenHandler::tapShortcut(QString application_id)
97 HMI_DEBUG("HomeScreen","tapShortcut %s", application_id.toStdString().c_str());
99 struct json_object* j_json = json_object_new_object();
100 struct json_object* value;
102 struct agl_shell *agl_shell = aglShell->shell.get();
103 QPlatformNativeInterface *native = qApp->platformNativeInterface();
104 struct wl_output *output = getWlOutput(native, qApp->screens().first());
106 value = json_object_new_string("normal.full");
107 json_object_object_add(j_json, "area", value);
109 mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
111 // this works (and it is redundant the first time), due to the default
112 // policy engine installed which actives the application, when starting
113 // the first time. Later calls to HomescreenHandler::tapShortcut will
114 // require calling 'agl_shell_activate_app'
115 agl_shell_activate_app(agl_shell, application_id.toStdString().c_str(), output);
118 mp_launcher->setCurrent(application_id);
122 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
124 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
127 void HomescreenHandler::onEv_static(const string& event, struct json_object* event_contents)
129 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onEv(event, event_contents);
132 void HomescreenHandler::onRep(struct json_object* reply_contents)
134 const char* str = json_object_to_json_string(reply_contents);
135 HMI_DEBUG("HomeScreen","HomeScreen onReply %s", str);
138 void HomescreenHandler::onEv(const string& event, struct json_object* event_contents)
140 const char* str = json_object_to_json_string(event_contents);
141 HMI_DEBUG("HomeScreen","HomeScreen onEv %s, contents: %s", event.c_str(), str);
143 if (event.compare("homescreen/on_screen_message") == 0) {
144 struct json_object *json_data = json_object_object_get(event_contents, "data");
145 struct json_object *json_display_message = json_object_object_get(json_data, "display_message");
146 const char* display_message = json_object_get_string(json_display_message);
148 HMI_DEBUG("HomeScreen","display_message = %s", display_message);