Add onscreen title in replyShowWindow
[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 _onscreen_title[] = "onscreenTitle";
33 const char _button_name[] = "buttonName";
34 const char _drawing_name[] = "drawing_name";
35 const char _application_id[] = "application_id";
36
37
38 void* EventHandler::myThis = 0;
39
40 EventHandler::EventHandler(QObject *parent) :
41     QObject(parent),
42     mp_hs(nullptr),
43     mp_wm(nullptr),
44     m_dsp_sts(false)
45 {
46 }
47
48 EventHandler::~EventHandler()
49 {
50     if (mp_hs != nullptr) {
51         delete mp_hs;
52     }
53     if (mp_wm != nullptr) {
54         delete mp_wm;
55     }
56 }
57
58 void EventHandler::init(int port, const char *token)
59 {
60     myThis = this;
61     mp_wm = new QLibWindowmanager();
62     mp_wm->init(port, token);
63
64     mp_hs = new LibHomeScreen();
65     mp_hs->init(port, token);
66
67     mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
68     mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
69         /*
70         {
71             "application_id": "onscreenapp",
72             "parameter": {
73                 "title": "onscreen title",
74                 "type": "critical,exclamation,question,information",
75                 "contents": "message contents",
76                 "buttons": ["button_name1", "button_name2", "button_name3"],
77                 "replyto":"caller application id"
78             }
79         } */
80         HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
81
82         struct json_object *param;
83         if(!json_object_object_get_ex(object, _parameter, &param)
84         || json_object_get_type(param) != json_type_object) {
85             HMI_DEBUG(APP_ID, "parameter error!");
86             return;
87         }
88
89         struct json_object *replyid;
90         const char *replyto = nullptr;
91         if(json_object_object_get_ex(param, _replyto, &replyid))
92             replyto = json_object_get_string(replyid);
93         if(replyto == nullptr) {
94             HMI_DEBUG(APP_ID, "received replyto is null!");
95             return;
96         }
97         m_req = qMakePair(QString(replyto), QString(json_object_to_json_string(param)));
98
99         if (this->getDisplayStatus() == HIDING) {
100             this->activateWindow(_myrole, "on_screen");
101         }
102         else if(this->getDisplayStatus() == SHOWING) {
103             this->setDisplayStatus(SWAPPING);
104             emit this->hideOnScreen();
105         }
106         else {
107             HMI_DEBUG(APP_ID, "onscreen swapping!");
108         }
109         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
110     });
111
112     mp_hs->set_event_handler(LibHomeScreen::Event_HideWindow, [this](json_object *object){
113         emit this->hideOnScreen();
114         HMI_DEBUG(APP_ID, "hideWindow json_object=%s", json_object_get_string(object));
115     });
116
117     if (mp_wm->requestSurface(_myrole) != 0) {
118         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
119         exit(EXIT_FAILURE);
120     }
121
122     mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
123         HMI_DEBUG(APP_ID, "Surface %s got syncDraw!", _myrole);
124         this->mp_wm->endDraw(QString(_myrole));
125     });
126
127     mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
128         struct json_object *value;
129         json_object_object_get_ex(object, _drawing_name, &value);
130         const char *name = json_object_get_string(value);
131         if(!strcasecmp(_myrole, name)){
132             this->setDisplayStatus(SHOWING);
133             this->m_dsp = this->m_req;
134             this->updateModel(QVariant(this->m_dsp.second));
135             emit this->showOnScreen();
136         }
137
138         HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
139     });
140
141     mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
142         struct json_object *value;
143         json_object_object_get_ex(object, _drawing_name, &value);
144         const char *name = json_object_get_string(value);
145
146         HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
147     });
148
149     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
150 }
151
152 void EventHandler::onRep_static(struct json_object* reply_contents)
153 {
154     static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
155 }
156
157 void EventHandler::onRep(struct json_object* reply_contents)
158 {
159     const char* str = json_object_to_json_string(reply_contents);
160     HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
161 }
162
163 void EventHandler::activateWindow(const char *role, const char *area)
164 {
165     HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
166     mp_wm->activateWindow(role, area);
167 }
168
169 void EventHandler::deactivateWindow()
170 {
171     HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
172     if(getDisplayStatus() == SWAPPING) {
173         setDisplayStatus(SHOWING);
174         m_dsp = m_req;
175         updateModel(QVariant(this->m_dsp.second));
176         emit showOnScreen();
177     }
178     else {
179         this->setDisplayStatus(HIDING);
180         mp_wm->deactivateWindow(_myrole);
181     }
182 }
183
184 void EventHandler::onScreenReply(const QString &ons_title, const QString &btn_name)
185 {
186     HMI_DEBUG(APP_ID, "ons_title=%s btn_name=%s", ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
187     emit this->hideOnScreen();
188
189     struct json_object* j_param = json_object_new_object();
190     json_object_object_add(j_param, _onscreen_title, json_object_new_string(ons_title.toStdString().c_str()));
191     json_object_object_add(j_param, _button_name, json_object_new_string(btn_name.toStdString().c_str()));
192     mp_hs->replyShowWindow(m_dsp.first.toStdString().c_str(), j_param);
193 }