39db933df723948e8d0347fb83c101070c480d4c
[apps/onscreenapp.git] / app / eventhandler.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <functional>
18 #include <QUrl>
19 #include <QDebug>
20 #include <QJsonDocument>
21 #include <QJsonObject>
22 #include <QQmlContext>
23 #include <QtQml/QQmlApplicationEngine>
24 #include <cstring>
25 #include <QFileInfo>
26
27 #include "eventhandler.h"
28
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
41 void* EventHandler::myThis = 0;
42
43 EventHandler::EventHandler(QObject *parent) :
44     QObject(parent),
45     mp_hs(nullptr),
46     mp_wm(nullptr)
47 {
48     m_dspreq = QString("");
49     m_req.clear();
50 }
51
52 EventHandler::~EventHandler()
53 {
54     if (mp_hs != nullptr) {
55         delete mp_hs;
56     }
57     if (mp_wm != nullptr) {
58         delete mp_wm;
59     }
60 }
61
62 void EventHandler::init(int port, const char *token)
63 {
64     myThis = this;
65     mp_wm = new QLibWindowmanager();
66     mp_wm->init(port, token);
67
68     mp_hs = new LibHomeScreen();
69     mp_hs->init(port, token);
70     
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));
75
76         struct json_object *param;
77         json_object_object_get_ex(object, _parameter, &param);
78         if(json_object_get_type(param) != json_type_object ) {
79             HMI_DEBUG(APP_ID, "parameter error!");
80             return;
81         }
82
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);
87         if(file == nullptr) {
88             HMI_DEBUG(APP_ID, "received qml file is null!");
89             return;
90         }
91
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);
95             return;
96         }
97
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!");
104             return;
105         }
106
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);
111
112         struct json_object *param_data;
113         const char* data = nullptr;
114         if(json_object_object_get_ex(param, _data, &param_data))
115             data = json_object_to_json_string(param_data);
116
117         m_dspreq = QString(replyto);
118         if(m_req.contains(m_dspreq)) {
119             m_req[m_dspreq] = qMakePair(QString(file), QString(data));
120         }
121         else
122             m_req.insert(QString(m_dspreq), qMakePair(QString(file), QString(data)));
123
124         if(area == nullptr)
125             this->activateWindow(ROLE_NAME);
126         else
127             this->activateWindow(ROLE_NAME, area);
128
129         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
130     });
131
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);
137
138         // onscreenapp only can release by application which request show
139         if (appid == m_dspreq)
140             this->deactivateWindow();
141         else
142             HMI_DEBUG(APP_ID, "request hideWindow application isn't request displaying application, m_dspreq=%s", m_dspreq.toStdString().c_str());
143     });
144
145     if (mp_wm->requestSurface(ROLE_NAME) != 0) {
146         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
147         exit(EXIT_FAILURE);
148     }
149
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));
155     });
156
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);
161
162         HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
163     });
164
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);
169
170         HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
171     });
172
173     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
174 }
175
176 void EventHandler::onRep_static(struct json_object* reply_contents)
177 {
178     static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
179 }
180
181 void EventHandler::onRep(struct json_object* reply_contents)
182 {
183     const char* str = json_object_to_json_string(reply_contents);
184     HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
185 }
186
187 void EventHandler::activateWindow(const char *role, const char *area)
188 {
189     HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
190     mp_wm->activateWindow(role, area);
191 }
192
193 void EventHandler::deactivateWindow()
194 {
195     HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
196
197     emit this->signalLoader(QVariant(""));
198     mp_wm->deactivateWindow(ROLE_NAME);
199 }
200
201 void EventHandler::onScreenReply(const QString &btn_name, const QString &press_mode, const QString &press_state)
202 {
203     HMI_DEBUG(APP_ID, "EventHandler::onScreenReply()");
204 //    deactivateWindow();
205
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()));
208
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);
214
215     mp_hs->replyShowWindow(m_dspreq.toLatin1(), j_obj);
216 }