d76fcbead777cc080c51a8efb4e839181cf1eecc
[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 const char _onscreen_directory[] = "/usr/lib/qt5/qml/AGL/OnScreen/";
41
42 void* EventHandler::myThis = 0;
43
44 EventHandler::EventHandler(QObject *parent) :
45     QObject(parent),
46     mp_hs(nullptr),
47     mp_wm(nullptr)
48 {
49     m_dspreq = QString("");
50     m_req.clear();
51 }
52
53 EventHandler::~EventHandler()
54 {
55     if (mp_hs != nullptr) {
56         delete mp_hs;
57     }
58     if (mp_wm != nullptr) {
59         delete mp_wm;
60     }
61 }
62
63 void EventHandler::init(int port, const char *token)
64 {
65     myThis = this;
66     mp_wm = new QLibWindowmanager();
67     mp_wm->init(port, token);
68
69     mp_hs = new LibHomeScreen();
70     mp_hs->init(port, token);
71     
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));
76
77         struct json_object *param;
78         json_object_object_get_ex(object, _parameter, &param);
79         if(json_object_get_type(param) != json_type_object ) {
80             HMI_DEBUG(APP_ID, "parameter error!");
81             return;
82         }
83
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);
88         if(file == nullptr) {
89             HMI_DEBUG(APP_ID, "received qml file is null!");
90             return;
91         }
92
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);
97             return;
98         }
99
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!");
106             return;
107         }
108
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);
113
114         struct json_object *param_data;
115         const char* data = nullptr;
116         if(json_object_object_get_ex(param, _data, &param_data))
117             data = json_object_to_json_string(param_data);
118
119         m_dspreq = QString(replyto);
120         if(m_req.contains(m_dspreq)) {
121             m_req[m_dspreq] = qMakePair(qml_file, QString(data));
122         }
123         else
124             m_req.insert(QString(m_dspreq), qMakePair(qml_file, QString(data)));
125
126         if(area == nullptr)
127             this->activateWindow(ROLE_NAME);
128         else
129             this->activateWindow(ROLE_NAME, area);
130
131         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
132     });
133
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);
139
140         // onscreenapp only can release by application which request show
141         if (appid == m_dspreq)
142             this->deactivateWindow();
143         else
144             HMI_DEBUG(APP_ID, "request hideWindow application isn't request displaying application, m_dspreq=%s", m_dspreq.toStdString().c_str());
145     });
146
147     if (mp_wm->requestSurface(ROLE_NAME) != 0) {
148         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
149         exit(EXIT_FAILURE);
150     }
151
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));
157     });
158
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);
163
164         HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
165     });
166
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);
171
172         HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
173     });
174
175     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
176 }
177
178 void EventHandler::onRep_static(struct json_object* reply_contents)
179 {
180     static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
181 }
182
183 void EventHandler::onRep(struct json_object* reply_contents)
184 {
185     const char* str = json_object_to_json_string(reply_contents);
186     HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
187 }
188
189 void EventHandler::activateWindow(const char *role, const char *area)
190 {
191     HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
192     mp_wm->activateWindow(role, area);
193 }
194
195 void EventHandler::deactivateWindow()
196 {
197     HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
198
199     emit this->signalLoader(QVariant(""));
200     mp_wm->deactivateWindow(ROLE_NAME);
201 }
202
203 void EventHandler::onScreenReply(const QString &btn_name, const QString &press_mode, const QString &press_state)
204 {
205     HMI_DEBUG(APP_ID, "EventHandler::onScreenReply()");
206 //    deactivateWindow();
207
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()));
210
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);
216
217     mp_hs->replyShowWindow(m_dspreq.toLatin1(), j_obj);
218 }