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";
40 const char _onscreen_directory[] = "/usr/lib/qt5/qml/AGL/OnScreen/";
42 void* EventHandler::myThis = 0;
44 EventHandler::EventHandler(QObject *parent) :
49 m_dspreq = QString("");
53 EventHandler::~EventHandler()
55 if (mp_hs != nullptr) {
58 if (mp_wm != nullptr) {
63 void EventHandler::init(int port, const char *token)
66 mp_wm = new QLibWindowmanager();
67 mp_wm->init(port, token);
69 mp_hs = new LibHomeScreen();
70 mp_hs->init(port, token);
72 mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
73 mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
74 // {"id": "onscreenXXX", "parameter": {"file": "onsreen file path", "data": {"key": "value", ...}}, "replyto": "app's id"}
75 HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
77 struct json_object *param;
78 json_object_object_get_ex(object, _parameter, ¶m);
79 if(json_object_get_type(param) != json_type_object ) {
80 HMI_DEBUG(APP_ID, "parameter error!");
84 struct json_object *qml_path;
85 const char *file = nullptr;
86 if(json_object_object_get_ex(param, _file, &qml_path))
87 file = json_object_get_string(qml_path);
89 HMI_DEBUG(APP_ID, "received qml file is null!");
93 QString qml_file = QString(_onscreen_directory) + file;
94 QFileInfo file_info(qml_file);
95 if(!file_info.isFile() || !qml_file.contains(QString(_suffix), Qt::CaseSensitive)) {
96 HMI_DEBUG(APP_ID, "received qml file error! file=%s.", file);
100 struct json_object *replyid;
101 const char *replyto = nullptr;
102 if(json_object_object_get_ex(param, _replyto, &replyid))
103 replyto = json_object_get_string(replyid);
104 if(replyto == nullptr) {
105 HMI_DEBUG(APP_ID, "received replyto is null!");
109 struct json_object *display_area;
110 const char *area = nullptr;
111 if(json_object_object_get_ex(param, _area, &display_area))
112 area = json_object_get_string(display_area);
114 struct json_object *param_data;
115 const char* data = nullptr;
116 if(json_object_object_get_ex(param, _data, ¶m_data))
117 data = json_object_to_json_string(param_data);
119 m_dspreq = QString(replyto);
120 if(m_req.contains(m_dspreq)) {
121 m_req[m_dspreq] = qMakePair(qml_file, QString(data));
124 m_req.insert(QString(m_dspreq), qMakePair(qml_file, QString(data)));
127 this->activateWindow(ROLE_NAME);
129 this->activateWindow(ROLE_NAME, area);
131 HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
134 mp_hs->set_event_handler(LibHomeScreen::Event_HideWindow, [this](json_object *object){
135 struct json_object *value;
136 json_object_object_get_ex(object, _application_id, &value);
137 const char *appid = json_object_get_string(value);
138 HMI_DEBUG(APP_ID, "request release onScreen application is %s!", appid);
140 // onscreenapp only can release by application which request show
141 if (appid == m_dspreq)
142 this->deactivateWindow();
144 HMI_DEBUG(APP_ID, "request hideWindow application isn't request displaying application, m_dspreq=%s", m_dspreq.toStdString().c_str());
147 if (mp_wm->requestSurface(ROLE_NAME) != 0) {
148 HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
152 mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
153 HMI_DEBUG(APP_ID, "Surface %s got syncDraw!", ROLE_NAME);
154 emit this->signalLoader(QVariant("file://" + m_req[m_dspreq].first));
155 emit this->signalOnScreenParameter(QVariant(m_req[m_dspreq].second));
156 this->mp_wm->endDraw(QString(ROLE_NAME));
159 mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
160 struct json_object *value;
161 json_object_object_get_ex(object, _drawing_name, &value);
162 const char *name = json_object_get_string(value);
164 HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
167 mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
168 struct json_object *value;
169 json_object_object_get_ex(object, _drawing_name, &value);
170 const char *name = json_object_get_string(value);
172 HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
175 HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
178 void EventHandler::onRep_static(struct json_object* reply_contents)
180 static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
183 void EventHandler::onRep(struct json_object* reply_contents)
185 const char* str = json_object_to_json_string(reply_contents);
186 HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
189 void EventHandler::activateWindow(const char *role, const char *area)
191 HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
192 mp_wm->activateWindow(role, area);
195 void EventHandler::deactivateWindow()
197 HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
199 emit this->signalLoader(QVariant(""));
200 mp_wm->deactivateWindow(ROLE_NAME);
203 void EventHandler::onScreenReply(const QString &btn_name, const QString &press_mode, const QString &press_state)
205 HMI_DEBUG(APP_ID, "EventHandler::onScreenReply()");
206 // deactivateWindow();
208 struct json_object* j_obj = json_object_new_object();
209 json_object_object_add(j_obj, _application_id, json_object_new_string(m_dspreq.toLatin1()));
211 struct json_object* j_param = json_object_new_object();
212 json_object_object_add(j_param, _button_name, json_object_new_string(btn_name.toStdString().c_str()));
213 json_object_object_add(j_param, _button_press_mode, json_object_new_string(press_mode.toStdString().c_str()));
214 json_object_object_add(j_param, _button_press_state, json_object_new_string(press_state.toStdString().c_str()));
215 json_object_object_add(j_obj, _parameter, j_param);
217 mp_hs->replyShowWindow(m_dspreq.toLatin1(), j_obj);