2 * Copyright (c) 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.
20 #include <QJsonDocument>
21 #include <QJsonObject>
22 #include <QQmlContext>
23 #include <QtQml/QQmlApplicationEngine>
27 #include "eventhandler.h"
29 const char _myrole[] = "on_screen";
30 const char _parameter[] = "parameter";
31 const char _replyto[] = "replyto";
32 const char _onscreen_title[] = "onscreenTitle";
33 const char _button_name[] = "buttonName";
34 const char _drawing_name[] = "drawing_name";
35 const char _application_id[] = "application_id";
38 void* EventHandler::myThis = 0;
40 EventHandler::EventHandler(QObject *parent) :
48 EventHandler::~EventHandler()
50 if (mp_hs != nullptr) {
53 if (mp_wm != nullptr) {
58 void EventHandler::init(int port, const char *token)
61 mp_wm = new QLibWindowmanager();
62 mp_wm->init(port, token);
64 mp_hs = new LibHomeScreen();
65 mp_hs->init(port, token);
67 mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
68 mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
71 "application_id": "onscreenapp",
73 "title": "onscreen title",
74 "type": "critical,exclamation,question,information",
75 "contents": "message contents",
76 "buttons": ["button_name1", "button_name2", "button_name3"],
77 "replyto":"caller application id"
80 HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
82 struct json_object *param;
83 if(!json_object_object_get_ex(object, _parameter, ¶m)
84 || json_object_get_type(param) != json_type_object) {
85 HMI_DEBUG(APP_ID, "parameter error!");
89 struct json_object *replyid;
90 const char *replyto = nullptr;
91 if(json_object_object_get_ex(param, _replyto, &replyid))
92 replyto = json_object_get_string(replyid);
93 if(replyto == nullptr) {
94 HMI_DEBUG(APP_ID, "received replyto is null!");
97 m_req = qMakePair(QString(replyto), QString(json_object_to_json_string(param)));
99 if (this->getDisplayStatus() == HIDING) {
100 this->activateWindow(_myrole, "on_screen");
102 else if(this->getDisplayStatus() == SHOWING) {
103 this->setDisplayStatus(SWAPPING);
104 emit this->hideOnScreen();
107 HMI_DEBUG(APP_ID, "onscreen swapping!");
109 HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
112 mp_hs->set_event_handler(LibHomeScreen::Event_HideWindow, [this](json_object *object){
113 emit this->hideOnScreen();
114 HMI_DEBUG(APP_ID, "hideWindow json_object=%s", json_object_get_string(object));
117 if (mp_wm->requestSurface(_myrole) != 0) {
118 HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
122 mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
123 HMI_DEBUG(APP_ID, "Surface %s got syncDraw!", _myrole);
124 this->mp_wm->endDraw(QString(_myrole));
127 mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
128 struct json_object *value;
129 json_object_object_get_ex(object, _drawing_name, &value);
130 const char *name = json_object_get_string(value);
131 if(!strcasecmp(_myrole, name)){
132 this->setDisplayStatus(SHOWING);
133 this->m_dsp = this->m_req;
134 this->updateModel(QVariant(this->m_dsp.second));
135 emit this->showOnScreen();
138 HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
141 mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
142 struct json_object *value;
143 json_object_object_get_ex(object, _drawing_name, &value);
144 const char *name = json_object_get_string(value);
146 HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
149 HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
152 void EventHandler::onRep_static(struct json_object* reply_contents)
154 static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
157 void EventHandler::onRep(struct json_object* reply_contents)
159 const char* str = json_object_to_json_string(reply_contents);
160 HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
163 void EventHandler::activateWindow(const char *role, const char *area)
165 HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
166 mp_wm->activateWindow(role, area);
169 void EventHandler::deactivateWindow()
171 HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
172 if(getDisplayStatus() == SWAPPING) {
173 setDisplayStatus(SHOWING);
175 updateModel(QVariant(this->m_dsp.second));
179 this->setDisplayStatus(HIDING);
180 mp_wm->deactivateWindow(_myrole);
184 void EventHandler::onScreenReply(const QString &ons_title, const QString &btn_name)
186 HMI_DEBUG(APP_ID, "ons_title=%s btn_name=%s", ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
187 emit this->hideOnScreen();
189 struct json_object* j_param = json_object_new_object();
190 json_object_object_add(j_param, _onscreen_title, json_object_new_string(ons_title.toStdString().c_str()));
191 json_object_object_add(j_param, _button_name, json_object_new_string(btn_name.toStdString().c_str()));
192 mp_hs->replyShowWindow(m_dsp.first.toStdString().c_str(), j_param);