add source for ces2019 horizontal
[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 "eventhandler.h"
18 #include <functional>
19 #include <QUrl>
20 #include <QDebug>
21 #include <QJsonDocument>
22 #include <QJsonObject>
23 #include <QQuickWindow>
24 #include <QPixmap>
25 #include <QBitmap>
26 #include <QImage>
27
28 void* EventHandler::myThis = 0;
29 #define SCREEN_AREA "OnScreen"
30 #define SCREEN_NAME "OnScreenApp"
31
32 EventHandler::EventHandler(QObject *parent) :
33     QObject(parent),
34     mp_hs(NULL),
35     mp_wm(NULL),
36     mp_qw(NULL)
37 {
38     m_isActive = false;
39     m_vecOSM.clear();
40 }
41
42 EventHandler::~EventHandler()
43 {
44     if (mp_hs != NULL) {
45         delete mp_hs;
46     }
47     if (mp_wm != NULL) {
48         delete mp_wm;
49     }
50 }
51
52 void EventHandler::init(int port, const char *token)
53 {
54     myThis = this;
55     mp_wm = new QLibWindowmanager();
56     mp_wm->init(port, token);
57
58     mp_hs = new LibHomeScreen();
59     mp_hs->init(port, token);
60
61     mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
62     mp_hs->set_event_handler(LibHomeScreen::Event_OnScreenMessage, [this](json_object *object){
63         struct json_object *value;
64         json_object_object_get_ex(object, "display_message", &value);
65         const char *display_message = json_object_get_string(value);
66
67         HMI_DEBUG("onscreenapp", "display_message = %s", display_message);
68
69         QString data(display_message);
70         QJsonDocument doc = QJsonDocument::fromJson(data.toUtf8());
71         QJsonObject jsonObj(doc.object());
72
73         ON_SCREEN_MESSAGE osm;
74
75         if (jsonObj.contains("app")) {
76             HMI_DEBUG("onscreenapp", "json parse. app is %s", jsonObj["app"].toString().toStdString().c_str());
77             osm.app = jsonObj["app"].toString();
78         }
79
80         if (jsonObj.contains("message")) {
81             HMI_DEBUG("onscreenapp", "json parse. message is %s", jsonObj["message"].toString().toStdString().c_str());
82             osm.message = jsonObj["message"].toString();
83         }
84
85         if (jsonObj.contains("file")) {
86             HMI_DEBUG("onscreenapp", "json parse. file is %s", jsonObj["file"].toString().toStdString().c_str());
87             osm.file = jsonObj["file"].toString();
88
89             if (osm.file.left(1) == "/") {
90                 osm.file = QUrl::fromLocalFile(osm.file).toString();
91             }
92         }
93
94         bool isNewApp = true;
95         for(QVector<ON_SCREEN_MESSAGE>::iterator it = m_vecOSM.begin(); it != m_vecOSM.end(); ++it) {
96             if (it->app == osm.app) {
97                 it->message = osm.message;
98                 it->file = osm.file;
99                 isNewApp = false;
100                 break;
101             }
102         }
103
104         if (isNewApp) {
105             m_vecOSM.append(osm);
106         }
107
108         if (osm.app == "Phone") {
109             if (osm.message == "incoming call") {
110                 this->activateSurface(osm.app);
111                 emit this->signalLoader(QVariant(osm.file));
112                 emit this->signalSetClearBackgroud();
113             }
114             else if (osm.message == "call rejected") {
115                 this->deactivateSurface(osm.app);
116             }
117         }
118         else if (osm.app == "System") {
119             if (osm.message == "error") {
120                 this->activateSurface(osm.app);
121                 emit this->signalLoader(QVariant(osm.file));
122                 emit this->signalSetClearBackgroud();
123             }
124             else if (osm.message == "rejected") {
125                 this->deactivateSurface(osm.app);
126             }
127         }
128         else {
129             if (osm.message == "onscreenapp:unload") {
130                 this->deactivateSurface(osm.app);
131             }
132             else {
133                 this->activateSurface(osm.app);
134                 if (osm.file.isEmpty()) {
135                     emit this->signalSetDefaultBackgroud(QVariant(osm.message));
136                 }
137                 else {
138                     emit this->signalLoader(QVariant(osm.file));
139                     emit this->signalSetClearBackgroud();
140                 }
141
142                 emit this->signalOnScreenMessage(QVariant(osm.message));
143             }
144         }
145
146     });
147
148     if (mp_wm->requestSurface(SCREEN_NAME) != 0) {
149         HMI_DEBUG("onscreenapp", "!!!!LayoutHandler requestSurface Failed!!!!!");
150         exit(EXIT_FAILURE);
151     }
152
153     mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
154         HMI_DEBUG("onscreenapp", "Surface %s got syncDraw!", SCREEN_NAME);
155         this->mp_wm->endDraw(SCREEN_NAME);
156     });
157
158     mp_wm->set_event_handler(QLibWindowmanager::Event_Active, [this](json_object *object) {
159         struct json_object *value;
160         json_object_object_get_ex(object, "drawing_name", &value);
161         const char *name = json_object_get_string(value);
162
163         HMI_DEBUG("onscreenapp", "Event_Active kKeyDrawingName = %s", name);
164     });
165
166     mp_wm->set_event_handler(QLibWindowmanager::Event_Inactive, [this](json_object *object) {
167         struct json_object *value;
168         json_object_object_get_ex(object, "drawing_name", &value);
169         const char *name = json_object_get_string(value);
170
171         HMI_DEBUG("onscreenapp", "Event_Inactive kKeyDrawingName = %s", name);
172     });
173
174     HMI_DEBUG("onscreenapp", "LayoutHander::init() finished.");
175 }
176
177 void EventHandler::setQuickWindow(QQuickWindow *qw)
178 {
179     mp_qw = qw;
180 }
181
182 void EventHandler::onRep_static(struct json_object* reply_contents)
183 {
184     static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
185 }
186 void EventHandler::onRep(struct json_object* reply_contents)
187 {
188     const char* str = json_object_to_json_string(reply_contents);
189     HMI_DEBUG("onscreenapp", "EventHandler::onReply %s", str);
190 }
191
192 void EventHandler::activateSurface(QString app)
193 {
194     HMI_DEBUG("onscreenapp", "EventHandler::activateSurface()");
195     if (m_isActive == false) {
196         m_isActive = true;
197         mp_wm->activateSurface(SCREEN_NAME);
198     }
199
200     QString label = SCREEN_AREA + app;
201     mp_wm->activateSurface(label, SCREEN_AREA);
202 }
203
204 void EventHandler::deactivateSurface(QString app)
205 {
206     HMI_DEBUG("onscreenapp", "EventHandler::deactivateSurface()");
207
208     for(QVector<ON_SCREEN_MESSAGE>::iterator it = m_vecOSM.begin(); it != m_vecOSM.end(); ++it) {
209         if (it->app == app) {
210             m_vecOSM.erase(it);
211             break;
212         }
213     }
214
215     if (m_vecOSM.length() > 0) {
216         ON_SCREEN_MESSAGE osm = m_vecOSM.last();
217
218         if (!osm.file.isEmpty()) {
219             emit this->signalLoader(QVariant(osm.file));
220             emit this->signalSetClearBackgroud();
221             emit this->signalOnScreenMessage(QVariant(osm.message));
222         }
223
224     } else {
225         // nothing on screen
226         emit this->signalLoader(QVariant(""));
227         mp_wm->deactivateSurface(SCREEN_NAME);
228         m_isActive = false;
229     }
230 }
231
232 void EventHandler::onScreenReply(const QString &message)
233 {
234     HMI_DEBUG("onscreenapp", "EventHandler::onScreenReply()");
235     mp_hs->onScreenReply(message.toLatin1());
236 }
237
238 #if USE_TEST_DISPLAY
239 void EventHandler::slotActivateSurface(){
240     // This is needed for first rendering when the app is launched
241     if(!m_isActive){
242         m_isActive = true;
243         this->activateSurface();
244     }
245 }
246 #endif