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