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 _button_name[] = "buttonName";
33 const char _drawing_name[] = "drawing_name";
34 const char _application_id[] = "application_id";
37 void* EventHandler::myThis = 0;
39 EventHandler::EventHandler(QObject *parent) :
47 EventHandler::~EventHandler()
49 if (mp_hs != nullptr) {
52 if (mp_wm != nullptr) {
57 void EventHandler::init(int port, const char *token)
60 mp_wm = new QLibWindowmanager();
61 mp_wm->init(port, token);
63 mp_hs = new LibHomeScreen();
64 mp_hs->init(port, token);
66 mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
67 mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
70 "application_id": "onscreenapp",
72 "title": "onscreen title",
73 "type": "critical,exclamation,question,information",
74 "contents": "message contents",
75 "buttons": ["button_name1", "button_name2", "button_name3"],
76 "replyto":"caller application id"
79 HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
81 struct json_object *param;
82 if(!json_object_object_get_ex(object, _parameter, ¶m)
83 || json_object_get_type(param) != json_type_object) {
84 HMI_DEBUG(APP_ID, "parameter error!");
88 struct json_object *replyid;
89 const char *replyto = nullptr;
90 if(json_object_object_get_ex(param, _replyto, &replyid))
91 replyto = json_object_get_string(replyid);
92 if(replyto == nullptr) {
93 HMI_DEBUG(APP_ID, "received replyto is null!");
96 m_req = qMakePair(QString(replyto), QString(json_object_to_json_string(param)));
98 if (this->getDisplayStatus() == HIDING) {
99 this->activateWindow(_myrole, "on_screen");
101 else if(this->getDisplayStatus() == SHOWING) {
102 this->setDisplayStatus(SWAPPING);
103 emit this->hideOnScreen();
106 HMI_DEBUG(APP_ID, "onscreen swapping!");
108 HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
111 mp_hs->set_event_handler(LibHomeScreen::Event_HideWindow, [this](json_object *object){
112 emit this->hideOnScreen();
113 HMI_DEBUG(APP_ID, "hideWindow json_object=%s", json_object_get_string(object));
116 if (mp_wm->requestSurface(_myrole) != 0) {
117 HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
121 mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
122 HMI_DEBUG(APP_ID, "Surface %s got syncDraw!", _myrole);
123 this->mp_wm->endDraw(QString(_myrole));
126 mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
127 struct json_object *value;
128 json_object_object_get_ex(object, _drawing_name, &value);
129 const char *name = json_object_get_string(value);
130 if(!strcasecmp(_myrole, name)){
131 this->setDisplayStatus(SHOWING);
132 this->m_dsp = this->m_req;
133 this->updateModel(QVariant(this->m_dsp.second));
134 emit this->showOnScreen();
137 HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
140 mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
141 struct json_object *value;
142 json_object_object_get_ex(object, _drawing_name, &value);
143 const char *name = json_object_get_string(value);
145 HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
148 HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
151 void EventHandler::onRep_static(struct json_object* reply_contents)
153 static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
156 void EventHandler::onRep(struct json_object* reply_contents)
158 const char* str = json_object_to_json_string(reply_contents);
159 HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
162 void EventHandler::activateWindow(const char *role, const char *area)
164 HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
165 mp_wm->activateWindow(role, area);
168 void EventHandler::deactivateWindow()
170 HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
171 if(getDisplayStatus() == SWAPPING) {
172 setDisplayStatus(SHOWING);
174 updateModel(QVariant(this->m_dsp.second));
178 this->setDisplayStatus(HIDING);
179 mp_wm->deactivateWindow(_myrole);
183 void EventHandler::onScreenReply(const QString &btn_name)
185 HMI_DEBUG(APP_ID, "EventHandler::onScreenReply(),btn_name=%s", btn_name.toStdString().c_str());
186 emit this->hideOnScreen();
188 struct json_object* j_param = json_object_new_object();
189 json_object_object_add(j_param, _button_name, json_object_new_string(btn_name.toStdString().c_str()));
190 mp_hs->replyShowWindow(m_dsp.first.toStdString().c_str(), j_param);