update
[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 <QQuickWindow>
23 #include <cstring>
24
25 #include "eventhandler.h"
26
27 void* EventHandler::myThis = 0;
28
29 EventHandler::EventHandler(QObject *parent) :
30     QObject(parent),
31     mp_hs(NULL),
32     mp_wm(NULL),
33     mp_qw(NULL)
34 {
35     m_dspreq = QString("");
36     my_id = QString(APP_ID);
37     my_role = QString(ROLE_NAME);
38     m_req.clear();
39 }
40
41 EventHandler::~EventHandler()
42 {
43     if (mp_hs != NULL) {
44         delete mp_hs;
45     }
46     if (mp_wm != NULL) {
47         delete mp_wm;
48     }
49 }
50
51 void EventHandler::init(int port, const char *token)
52 {
53     myThis = this;
54     mp_wm = new QLibWindowmanager();
55     mp_wm->init(port, token);
56
57     mp_hs = new LibHomeScreen();
58     mp_hs->init(port, token);
59     
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);
69             return;
70         }
71         else
72             HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
73
74         struct json_object *param;
75         json_object_object_get_ex(object, "parameter", &param);
76         if(json_object_get_type(param) != json_type_object ) {
77             HMI_DEBUG(APP_ID, "parameter error!");
78             return;
79         }
80
81         struct json_object *replyid;
82         json_object_object_get_ex(param, "replyid", &replyid);
83         const char *_replyid = json_object_get_string(replyid);
84
85         struct json_object *path;
86         json_object_object_get_ex(param, "file", &path);
87         const char *_path = json_object_get_string(path);
88
89         struct json_object *data;
90         json_bool rtn = json_object_object_get_ex(param, "data", &data);
91         const char* _data = "";
92         if(rtn) {
93             _data = json_object_to_json_string(data);
94         }
95
96         m_dspreq = QString(_replyid);
97         if(m_req.contains(m_dspreq)) {
98             m_req[m_dspreq] = qMakePair(QString(_path), QString(_data));
99         }
100         else
101             m_req.insert(QString(m_dspreq), qMakePair(QString(_path), QString(_data)));
102
103         this->mp_wm->activateWindow(ROLE_NAME);
104         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
105     });
106
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);
111
112         HMI_DEBUG(APP_ID, "release onScreen is %s!", appid);
113
114         if (appid == my_id) {
115             emit this->signalLoader(QVariant(""));
116             this->mp_wm->deactivateWindow(ROLE_NAME);
117         }
118         else {
119             HMI_DEBUG(APP_ID, "!!!!releaseOnScreen is not displaying!!!!!");
120         }
121     });
122
123     if (mp_wm->requestSurface(ROLE_NAME) != 0) {
124         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
125         exit(EXIT_FAILURE);
126     }
127
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));
133     });
134
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);
139
140         HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
141     });
142
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);
147
148         HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
149     });
150
151     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
152 }
153
154 void EventHandler::setQuickWindow(QQuickWindow *qw)
155 {
156     mp_qw = qw;
157 }
158
159 void EventHandler::onRep_static(struct json_object* reply_contents)
160 {
161     static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
162 }
163 void EventHandler::onRep(struct json_object* reply_contents)
164 {
165     const char* str = json_object_to_json_string(reply_contents);
166     HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
167 }
168
169 void EventHandler::activateWindow(QString &role)
170 {
171     HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
172     mp_wm->activateWindow(role);
173 }
174
175 void EventHandler::deactivateWindow(QString &role)
176 {
177     HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
178
179     emit this->signalLoader(QVariant(""));
180     mp_wm->deactivateWindow(role);
181 }
182
183 void EventHandler::onScreenReply(const QString &btn_name)
184 {
185     HMI_DEBUG(APP_ID, "EventHandler::onScreenReply()");
186     deactivateWindow(my_role);
187
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()));
190
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);
196
197     mp_hs->replyShowWindow(m_dspreq.toLatin1(), j_obj);
198 }