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 <QQuickWindow>
25 #include "eventhandler.h"
27 void* EventHandler::myThis = 0;
29 EventHandler::EventHandler(QObject *parent) :
35 m_dspreq = QString("");
36 my_id = QString(APP_ID);
37 my_role = QString(ROLE_NAME);
41 EventHandler::~EventHandler()
51 void EventHandler::init(int port, const char *token)
54 mp_wm = new QLibWindowmanager();
55 mp_wm->init(port, token);
57 mp_hs = new LibHomeScreen();
58 mp_hs->init(port, token);
60 // my_id = QString(std::strtok(std::getenv("AFM_ID"), "@"));
61 mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
62 mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
63 // {"id": "onscreenXXX", "parameter": {"replyid": "app's id", "file": "onsreen file path", "data": {"key": "value", ...}}}
64 struct json_object *appid;
65 json_object_object_get_ex(object, "application_id", &appid);
66 const char *id = json_object_get_string(appid);
67 if(my_id != QString(id)) {
68 HMI_DEBUG(APP_ID, "Event_ShowWindow id = %s", id);
72 HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
74 struct json_object *param;
75 json_object_object_get_ex(object, "parameter", ¶m);
76 if(json_object_get_type(param) != json_type_object ) {
77 HMI_DEBUG(APP_ID, "parameter error!");
81 struct json_object *replyid;
82 json_object_object_get_ex(param, "replyid", &replyid);
83 const char *_replyid = json_object_get_string(replyid);
85 struct json_object *path;
86 json_object_object_get_ex(param, "file", &path);
87 const char *_path = json_object_get_string(path);
89 struct json_object *data;
90 json_bool rtn = json_object_object_get_ex(param, "data", &data);
91 const char* _data = "";
93 _data = json_object_to_json_string(data);
96 m_dspreq = QString(_replyid);
97 if(m_req.contains(m_dspreq)) {
98 m_req[m_dspreq] = qMakePair(QString(_path), QString(_data));
101 m_req.insert(QString(m_dspreq), qMakePair(QString(_path), QString(_data)));
103 this->mp_wm->activateWindow(ROLE_NAME);
104 HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
107 mp_hs->set_event_handler(LibHomeScreen::Event_HideWindow, [this](json_object *object){
108 struct json_object *value;
109 json_object_object_get_ex(object, "application_id", &value);
110 const char *appid = json_object_get_string(value);
112 HMI_DEBUG(APP_ID, "release onScreen is %s!", appid);
114 if (appid == my_id) {
115 emit this->signalLoader(QVariant(""));
116 this->mp_wm->deactivateWindow(ROLE_NAME);
119 HMI_DEBUG(APP_ID, "!!!!releaseOnScreen is not displaying!!!!!");
123 if (mp_wm->requestSurface(ROLE_NAME) != 0) {
124 HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
128 mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
129 HMI_DEBUG(APP_ID, "Surface %s got syncDraw!", ROLE_NAME);
130 emit this->signalLoader(QVariant("file://" + m_req[m_dspreq].first));
131 emit this->signalOnScreenParameter(QVariant(m_req[m_dspreq].second));
132 this->mp_wm->endDraw(QString(ROLE_NAME));
135 mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
136 struct json_object *value;
137 json_object_object_get_ex(object, "drawing_name", &value);
138 const char *name = json_object_get_string(value);
140 HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
143 mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
144 struct json_object *value;
145 json_object_object_get_ex(object, "drawing_name", &value);
146 const char *name = json_object_get_string(value);
148 HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
151 HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
154 void EventHandler::setQuickWindow(QQuickWindow *qw)
159 void EventHandler::onRep_static(struct json_object* reply_contents)
161 static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
163 void EventHandler::onRep(struct json_object* reply_contents)
165 const char* str = json_object_to_json_string(reply_contents);
166 HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
169 void EventHandler::activateWindow(QString &role)
171 HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
172 mp_wm->activateWindow(role);
175 void EventHandler::deactivateWindow(QString &role)
177 HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
179 emit this->signalLoader(QVariant(""));
180 mp_wm->deactivateWindow(role);
183 void EventHandler::onScreenReply(const QString &btn_name)
185 HMI_DEBUG(APP_ID, "EventHandler::onScreenReply()");
186 deactivateWindow(my_role);
188 struct json_object* j_obj = json_object_new_object();
189 json_object_object_add(j_obj, "application_id", json_object_new_string(m_dspreq.toLatin1()));
191 struct json_object* j_param = json_object_new_object();
192 json_object_object_add(j_param, "method", json_object_new_string("Buttons.ButtonPress"));
193 json_object_object_add(j_param, "buttonName", json_object_new_string(btn_name.toStdString().c_str()));
194 json_object_object_add(j_param, "buttonPressMode", json_object_new_string("Short"));
195 json_object_object_add(j_obj, "parameter", j_param);
197 mp_hs->replyShowWindow(m_dspreq.toLatin1(), j_obj);