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.
20 #include <QJsonDocument>
21 #include <QJsonObject>
22 #include <QQmlContext>
23 #include <QtQml/QQmlApplicationEngine>
27 #include "eventhandler.h"
29 const char _parameter[] = "parameter";
30 const char _replyto[] = "replyto";
31 const char _data[] = "data";
32 const char _file[] = "file";
33 const char _area[] = "area";
34 const char _suffix[] = ".qml";
35 const char _button_name[] = "buttonName";
36 const char _button_press_mode[] = "buttonPressMode";
37 const char _button_press_state[] = "buttonPressState";
38 const char _drawing_name[] = "drawing_name";
39 const char _application_id[] = "application_id";
41 void* EventHandler::myThis = 0;
43 EventHandler::EventHandler(QObject *parent) :
48 m_dspreq = QString("");
52 EventHandler::~EventHandler()
54 if (mp_hs != nullptr) {
57 if (mp_wm != nullptr) {
62 void EventHandler::init(int port, const char *token)
65 mp_wm = new QLibWindowmanager();
66 mp_wm->init(port, token);
68 mp_hs = new LibHomeScreen();
69 mp_hs->init(port, token);
71 mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
72 mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
73 // {"id": "onscreenXXX", "parameter": {"file": "onsreen file path", "data": {"key": "value", ...}}, "replyto": "app's id"}
74 HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
76 struct json_object *param;
77 json_object_object_get_ex(object, _parameter, ¶m);
78 if(json_object_get_type(param) != json_type_object ) {
79 HMI_DEBUG(APP_ID, "parameter error!");
83 struct json_object *qml_path;
84 const char *file = nullptr;
85 if(json_object_object_get_ex(param, _file, &qml_path))
86 file = json_object_get_string(qml_path);
88 HMI_DEBUG(APP_ID, "received qml file is null!");
92 QFileInfo file_info(file);
93 if(!file_info.isFile() || !QString(file).contains(QString(_suffix), Qt::CaseSensitive)) {
94 HMI_DEBUG(APP_ID, "received qml file error! file=%s.", file);
98 struct json_object *replyid;
99 const char *replyto = nullptr;
100 if(json_object_object_get_ex(param, _replyto, &replyid))
101 replyto = json_object_get_string(replyid);
102 if(replyto == nullptr) {
103 HMI_DEBUG(APP_ID, "received replyto is null!");
107 struct json_object *display_area;
108 const char *area = nullptr;
109 if(json_object_object_get_ex(param, _area, &display_area))
110 area = json_object_get_string(display_area);
112 struct json_object *param_data;
113 const char* data = nullptr;
114 if(json_object_object_get_ex(param, _data, ¶m_data))
115 data = json_object_to_json_string(param_data);
117 m_dspreq = QString(replyto);
118 if(m_req.contains(m_dspreq)) {
119 m_req[m_dspreq] = qMakePair(QString(file), QString(data));
122 m_req.insert(QString(m_dspreq), qMakePair(QString(file), QString(data)));
125 this->activateWindow(ROLE_NAME);
127 this->activateWindow(ROLE_NAME, area);
129 HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
132 mp_hs->set_event_handler(LibHomeScreen::Event_HideWindow, [this](json_object *object){
133 struct json_object *value;
134 json_object_object_get_ex(object, _application_id, &value);
135 const char *appid = json_object_get_string(value);
136 HMI_DEBUG(APP_ID, "request release onScreen application is %s!", appid);
138 // onscreenapp only can release by application which request show
139 if (appid == m_dspreq)
140 this->deactivateWindow();
142 HMI_DEBUG(APP_ID, "request hideWindow application isn't request displaying application, m_dspreq=%s", m_dspreq.toStdString().c_str());
145 if (mp_wm->requestSurface(ROLE_NAME) != 0) {
146 HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
150 mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
151 HMI_DEBUG(APP_ID, "Surface %s got syncDraw!", ROLE_NAME);
152 emit this->signalLoader(QVariant("file://" + m_req[m_dspreq].first));
153 emit this->signalOnScreenParameter(QVariant(m_req[m_dspreq].second));
154 this->mp_wm->endDraw(QString(ROLE_NAME));
157 mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
158 struct json_object *value;
159 json_object_object_get_ex(object, _drawing_name, &value);
160 const char *name = json_object_get_string(value);
162 HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
165 mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
166 struct json_object *value;
167 json_object_object_get_ex(object, _drawing_name, &value);
168 const char *name = json_object_get_string(value);
170 HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
173 HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
176 void EventHandler::onRep_static(struct json_object* reply_contents)
178 static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
181 void EventHandler::onRep(struct json_object* reply_contents)
183 const char* str = json_object_to_json_string(reply_contents);
184 HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
187 void EventHandler::activateWindow(const char *role, const char *area)
189 HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
190 mp_wm->activateWindow(role, area);
193 void EventHandler::deactivateWindow()
195 HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
197 emit this->signalLoader(QVariant(""));
198 mp_wm->deactivateWindow(ROLE_NAME);
201 void EventHandler::onScreenReply(const QString &btn_name, const QString &press_mode, const QString &press_state)
203 HMI_DEBUG(APP_ID, "EventHandler::onScreenReply()");
204 // deactivateWindow();
206 struct json_object* j_obj = json_object_new_object();
207 json_object_object_add(j_obj, _application_id, json_object_new_string(m_dspreq.toLatin1()));
209 struct json_object* j_param = json_object_new_object();
210 json_object_object_add(j_param, _button_name, json_object_new_string(btn_name.toStdString().c_str()));
211 json_object_object_add(j_param, _button_press_mode, json_object_new_string(press_mode.toStdString().c_str()));
212 json_object_object_add(j_param, _button_press_state, json_object_new_string(press_state.toStdString().c_str()));
213 json_object_object_add(j_obj, _parameter, j_param);
215 mp_hs->replyShowWindow(m_dspreq.toLatin1(), j_obj);