2 * Copyright (c) 2017 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 "eventhandler.h"
21 #include <QJsonDocument>
22 #include <QJsonObject>
23 #include <QQuickWindow>
28 void* EventHandler::myThis = 0;
29 #define SCREEN_AREA "OnScreen"
30 #define SCREEN_NAME "OnScreenApp"
32 EventHandler::EventHandler(QObject *parent) :
42 EventHandler::~EventHandler()
52 void EventHandler::init(int port, const char *token)
55 mp_wm = new QLibWindowmanager();
56 mp_wm->init(port, token);
58 mp_hs = new LibHomeScreen();
59 mp_hs->init(port, token);
61 mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
62 mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
63 struct json_object *value;
64 json_object_object_get_ex(object, "display_message", &value);
65 const char *display_message = json_object_get_string(value);
67 HMI_DEBUG("onscreenapp", "display_message = %s", display_message);
69 QString data(display_message);
70 QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
71 QJsonObject jsonObj(doc.object());
73 ON_SCREEN_MESSAGE osm;
75 if (jsonObj.contains("app")) {
76 HMI_DEBUG("onscreenapp", "json parse. app is %s", jsonObj["app"].toString().toStdString().c_str());
77 osm.app = jsonObj["app"].toString();
80 if (jsonObj.contains("message")) {
81 HMI_DEBUG("onscreenapp", "json parse. message is %s", jsonObj["message"].toString().toStdString().c_str());
82 osm.message = jsonObj["message"].toString();
85 if (jsonObj.contains("file")) {
86 HMI_DEBUG("onscreenapp", "json parse. file is %s", jsonObj["file"].toString().toStdString().c_str());
87 osm.file = jsonObj["file"].toString();
89 if (osm.file.left(1) == "/") {
90 osm.file = QUrl::fromLocalFile(osm.file).toString();
95 for(QVector<ON_SCREEN_MESSAGE>::iterator it = m_vecOSM.begin(); it != m_vecOSM.end(); ++it) {
96 if (it->app == osm.app) {
97 it->message = osm.message;
105 m_vecOSM.append(osm);
108 if (osm.app == "Phone") {
109 if (osm.message == "incoming call") {
110 this->activateSurface(osm.app);
111 emit this->signalLoader(QVariant(osm.file));
112 emit this->signalSetClearBackgroud();
114 else if (osm.message == "call rejected") {
115 this->deactivateSurface(osm.app);
118 else if (osm.app == "System") {
119 if (osm.message == "error") {
120 this->activateSurface(osm.app);
121 emit this->signalLoader(QVariant(osm.file));
122 emit this->signalSetClearBackgroud();
124 else if (osm.message == "rejected") {
125 this->deactivateSurface(osm.app);
129 if (osm.message == "onscreenapp:unload") {
130 this->deactivateSurface(osm.app);
133 this->activateSurface(osm.app);
134 if (osm.file.isEmpty()) {
135 emit this->signalSetDefaultBackgroud(QVariant(osm.message));
138 emit this->signalLoader(QVariant(osm.file));
139 emit this->signalSetClearBackgroud();
142 emit this->signalOnScreenMessage(QVariant(osm.message));
148 if (mp_wm->requestSurface(SCREEN_NAME) != 0) {
149 HMI_DEBUG("onscreenapp", "!!!!LayoutHandler requestSurface Failed!!!!!");
153 mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
154 HMI_DEBUG("onscreenapp", "Surface %s got syncDraw!", SCREEN_NAME);
155 this->mp_wm->endDraw(SCREEN_NAME);
158 mp_wm->set_event_handler(QLibWindowmanager::Event_Active, [this](json_object *object) {
159 struct json_object *value;
160 json_object_object_get_ex(object, "drawing_name", &value);
161 const char *name = json_object_get_string(value);
163 HMI_DEBUG("onscreenapp", "Event_Active kKeyDrawingName = %s", name);
166 mp_wm->set_event_handler(QLibWindowmanager::Event_Inactive, [this](json_object *object) {
167 struct json_object *value;
168 json_object_object_get_ex(object, "drawing_name", &value);
169 const char *name = json_object_get_string(value);
171 HMI_DEBUG("onscreenapp", "Event_Inactive kKeyDrawingName = %s", name);
174 HMI_DEBUG("onscreenapp", "LayoutHander::init() finished.");
177 void EventHandler::setQuickWindow(QQuickWindow *qw)
182 void EventHandler::onRep_static(struct json_object* reply_contents)
184 static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
186 void EventHandler::onRep(struct json_object* reply_contents)
188 const char* str = json_object_to_json_string(reply_contents);
189 HMI_DEBUG("onscreenapp", "EventHandler::onReply %s", str);
192 void EventHandler::activateSurface(QString app)
194 HMI_DEBUG("onscreenapp", "EventHandler::activateSurface()");
195 if (m_isActive == false) {
197 mp_wm->activateSurface(SCREEN_NAME);
200 QString label = SCREEN_AREA + app;
201 mp_wm->activateSurface(label, SCREEN_AREA);
204 void EventHandler::deactivateSurface(QString app)
206 HMI_DEBUG("onscreenapp", "EventHandler::deactivateSurface()");
208 for(QVector<ON_SCREEN_MESSAGE>::iterator it = m_vecOSM.begin(); it != m_vecOSM.end(); ++it) {
209 if (it->app == app) {
215 if (m_vecOSM.length() > 0) {
216 ON_SCREEN_MESSAGE osm = m_vecOSM.last();
218 if (!osm.file.isEmpty()) {
219 emit this->signalLoader(QVariant(osm.file));
220 emit this->signalSetClearBackgroud();
221 emit this->signalOnScreenMessage(QVariant(osm.message));
226 emit this->signalLoader(QVariant(""));
227 mp_wm->deactivateSurface(SCREEN_NAME);
232 void EventHandler::onScreenReply(const QString &message)
234 HMI_DEBUG("onscreenapp", "EventHandler::onScreenReply()");
235 mp_hs->onScreenReply(message.toLatin1());
239 void EventHandler::slotActivateSurface(){
240 // This is needed for first rendering when the app is launched
243 this->activateSurface();