Add onscreenapp
[apps/onscreenapp.git] / app / eventhandler.cpp
1 /*
2  * Copyright (c) 2019 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 _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";
35
36
37 void* EventHandler::myThis = 0;
38
39 EventHandler::EventHandler(QObject *parent) :
40     QObject(parent),
41     mp_hs(nullptr),
42     mp_wm(nullptr),
43     m_dsp_sts(false)
44 {
45 }
46
47 EventHandler::~EventHandler()
48 {
49     if (mp_hs != nullptr) {
50         delete mp_hs;
51     }
52     if (mp_wm != nullptr) {
53         delete mp_wm;
54     }
55 }
56
57 void EventHandler::init(int port, const char *token)
58 {
59     myThis = this;
60     mp_wm = new QLibWindowmanager();
61     mp_wm->init(port, token);
62
63     mp_hs = new LibHomeScreen();
64     mp_hs->init(port, token);
65
66     mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
67     mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
68         /*
69         {
70             "application_id": "onscreenapp",
71             "parameter": {
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"
77             }
78         } */
79         HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
80
81         struct json_object *param;
82         if(!json_object_object_get_ex(object, _parameter, &param)
83         || json_object_get_type(param) != json_type_object) {
84             HMI_DEBUG(APP_ID, "parameter error!");
85             return;
86         }
87
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!");
94             return;
95         }
96         m_req = qMakePair(QString(replyto), QString(json_object_to_json_string(param)));
97
98         if (this->getDisplayStatus() == HIDING) {
99             this->activateWindow(_myrole, "on_screen");
100         }
101         else if(this->getDisplayStatus() == SHOWING) {
102             this->setDisplayStatus(SWAPPING);
103             emit this->hideOnScreen();
104         }
105         else {
106             HMI_DEBUG(APP_ID, "onscreen swapping!");
107         }
108         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
109     });
110
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));
114     });
115
116     if (mp_wm->requestSurface(_myrole) != 0) {
117         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
118         exit(EXIT_FAILURE);
119     }
120
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));
124     });
125
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();
135         }
136
137         HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
138     });
139
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);
144
145         HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
146     });
147
148     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
149 }
150
151 void EventHandler::onRep_static(struct json_object* reply_contents)
152 {
153     static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
154 }
155
156 void EventHandler::onRep(struct json_object* reply_contents)
157 {
158     const char* str = json_object_to_json_string(reply_contents);
159     HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
160 }
161
162 void EventHandler::activateWindow(const char *role, const char *area)
163 {
164     HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
165     mp_wm->activateWindow(role, area);
166 }
167
168 void EventHandler::deactivateWindow()
169 {
170     HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
171     if(getDisplayStatus() == SWAPPING) {
172         setDisplayStatus(SHOWING);
173         m_dsp = m_req;
174         updateModel(QVariant(this->m_dsp.second));
175         emit showOnScreen();
176     }
177     else {
178         this->setDisplayStatus(HIDING);
179         mp_wm->deactivateWindow(_myrole);
180     }
181 }
182
183 void EventHandler::onScreenReply(const QString &btn_name)
184 {
185     HMI_DEBUG(APP_ID, "EventHandler::onScreenReply(),btn_name=%s", btn_name.toStdString().c_str());
186     emit this->hideOnScreen();
187
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);
191 }