e1ece216b9706696c7bbb86bbce5d62206cbb643
[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 <QGuiApplication>
21 #include <QJsonDocument>
22 #include <QJsonObject>
23 #include <QQmlContext>
24 #include <QtQml/QQmlApplicationEngine>
25 #include <cstring>
26 #include <QFileInfo>
27 #include <qpa/qplatformnativeinterface.h>
28
29 #include "eventhandler.h"
30
31 const char _myrole[] = "on_screen";
32 const char _parameter[] = "parameter";
33 const char _replyto[] = "replyto";
34 const char _onscreen_title[] = "onscreenTitle";
35 const char _button_name[] = "buttonName";
36 const char _drawing_name[] = "drawing_name";
37 const char _application_id[] = "application_id";
38
39
40 void* EventHandler::myThis = 0;
41
42 static struct wl_output *
43 getWlOutput(QScreen *screen)
44 {
45         QPlatformNativeInterface *native = qApp->platformNativeInterface();
46         void *output = native->nativeResourceForScreen("output", screen);
47         return static_cast<struct ::wl_output*>(output);
48 }
49
50 static void
51 global_add(void *data, struct wl_registry *reg, uint32_t name,
52            const char *interface, uint32_t version)
53 {
54         struct agl_shell_desktop **shell =
55                 static_cast<struct agl_shell_desktop **>(data);
56
57         if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
58                 *shell = static_cast<struct agl_shell_desktop *>(
59                         wl_registry_bind(reg, name, &agl_shell_desktop_interface, version)
60                 );
61         }
62 }
63
64 static void global_remove(void *data, struct wl_registry *reg, uint32_t id)
65 {
66         (void) data;
67         (void) reg;
68         (void) id;
69 }
70
71 static const struct wl_registry_listener registry_listener = {
72         global_add,
73         global_remove,
74 };
75
76 static void
77 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
78                 const char *app_id)
79 {
80         EventHandler *ev_handler = static_cast<EventHandler *>(data);
81         (void) agl_shell_desktop;
82
83         // should probably add here to a list the application or trigger emit
84         // for QML code, also note that we get here our own application
85         if (strcmp(app_id, APP_ID) == 0)
86                 return;
87
88         qInfo() << "app_id: " << app_id;
89 }
90
91 static void
92 application_state_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
93                         const char *app_id, const char *app_data,
94                         uint32_t app_state, uint32_t app_role)
95 {
96         EventHandler *ev_handler = static_cast<EventHandler *>(data);
97         (void) agl_shell_desktop;
98
99         qInfo() << "got application_state_event() for app_id=" <<
100                 app_id << ", state=" << app_state << ", role=" << app_role;
101
102         if (strcmp(app_id, APP_ID))
103                 return;
104
105         if (app_state != AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED)
106                 return;
107
108         if (app_role != AGL_SHELL_DESKTOP_APP_ROLE_POPUP)
109                 return;
110
111         qInfo() << "Got message " << app_data;
112
113         // should signal that we got the message
114         emit ev_handler->updateModel(QVariant(app_data));
115         emit ev_handler->showOnScreen();
116 }
117
118 static const struct agl_shell_desktop_listener agl_shell_desk_listener = {
119         application_id_event,
120         application_state_event,
121 };
122
123 static struct agl_shell_desktop *
124 register_agl_shell_desktop(void)
125 {
126         struct wl_display *wl;
127         struct wl_registry *registry;
128         struct agl_shell_desktop *shell = nullptr;
129
130         QPlatformNativeInterface *native = qApp->platformNativeInterface();
131
132         wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
133         registry = wl_display_get_registry(wl);
134
135         wl_registry_add_listener(registry, &registry_listener, &shell);
136         // Roundtrip to get all globals advertised by the compositor
137         wl_display_roundtrip(wl);
138         wl_registry_destroy(registry);
139
140         return shell;
141 }
142
143
144 EventHandler::EventHandler(QObject *parent) :
145     QObject(parent),
146     m_dsp_sts(false)
147 {
148 }
149
150 EventHandler::~EventHandler()
151 {
152     if (shell_desktop)
153             agl_shell_desktop_destroy(shell_desktop);
154 }
155
156 void EventHandler::init(int port, const char *token)
157 {
158         (void) port;
159         (void) token;
160
161         shell_desktop = register_agl_shell_desktop();
162         if (shell_desktop)
163                 agl_shell_desktop_add_listener(shell_desktop, &agl_shell_desk_listener, this);
164 #if 0
165     myThis = this;
166     mp_wm = new QLibWindowmanager();
167     mp_wm->init(port, token);
168
169     mp_hs = new LibHomeScreen();
170     mp_hs->init(port, token);
171
172     mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
173     mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
174         /*
175         {
176             "application_id": "onscreenapp",
177             "parameter": {
178                 "title": "onscreen title",
179                 "type": "critical,exclamation,question,information",
180                 "contents": "message contents",
181                 "buttons": ["button_name1", "button_name2", "button_name3"],
182                 "replyto":"caller application id"
183             }
184         } */
185         HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
186
187         struct json_object *param;
188         if(!json_object_object_get_ex(object, _parameter, &param)
189         || json_object_get_type(param) != json_type_object) {
190             HMI_DEBUG(APP_ID, "parameter error!");
191             return;
192         }
193
194         struct json_object *replyid;
195         const char *replyto = nullptr;
196         if(json_object_object_get_ex(param, _replyto, &replyid))
197             replyto = json_object_get_string(replyid);
198         if(replyto == nullptr) {
199             HMI_DEBUG(APP_ID, "received replyto is null!");
200             return;
201         }
202         m_req = qMakePair(QString(replyto), QString(json_object_to_json_string(param)));
203
204         if (this->getDisplayStatus() == HIDING) {
205             this->activateWindow(_myrole, "on_screen");
206         }
207         else if(this->getDisplayStatus() == SHOWING) {
208             this->setDisplayStatus(SWAPPING);
209             emit this->hideOnScreen();
210         }
211         else {
212             HMI_DEBUG(APP_ID, "onscreen swapping!");
213         }
214         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
215     });
216
217     mp_hs->set_event_handler(LibHomeScreen::Event_HideWindow, [this](json_object *object){
218         emit this->hideOnScreen();
219         HMI_DEBUG(APP_ID, "hideWindow json_object=%s", json_object_get_string(object));
220     });
221
222     if (mp_wm->requestSurface(_myrole) != 0) {
223         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
224         exit(EXIT_FAILURE);
225     }
226
227     mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
228         HMI_DEBUG(APP_ID, "Surface %s got syncDraw!", _myrole);
229         this->mp_wm->endDraw(QString(_myrole));
230     });
231
232     mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
233         struct json_object *value;
234         json_object_object_get_ex(object, _drawing_name, &value);
235         const char *name = json_object_get_string(value);
236         if(!strcasecmp(_myrole, name)){
237             this->setDisplayStatus(SHOWING);
238             this->m_dsp = this->m_req;
239             this->updateModel(QVariant(this->m_dsp.second));
240             emit this->showOnScreen();
241         }
242
243         HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
244     });
245
246     mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
247         struct json_object *value;
248         json_object_object_get_ex(object, _drawing_name, &value);
249         const char *name = json_object_get_string(value);
250
251         HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
252     });
253
254     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
255 #endif
256 }
257
258 #if 0
259 void EventHandler::onRep_static(struct json_object* reply_contents)
260 {
261     static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
262 }
263
264 void EventHandler::onRep(struct json_object* reply_contents)
265 {
266     const char* str = json_object_to_json_string(reply_contents);
267     HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
268 }
269 #endif
270
271 void EventHandler::activateWindow(const char *role, const char *area)
272 {
273 #if 0
274     HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
275     mp_wm->activateWindow(role, area);
276 #endif
277         fprintf(stderr, "EventHandler::activateWindow() role %s, area %s\n",
278                         role, area);
279 }
280
281 void EventHandler::deactivateWindow()
282 {
283 #if 0
284     HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
285     if(getDisplayStatus() == SWAPPING) {
286         setDisplayStatus(SHOWING);
287         m_dsp = m_req;
288         updateModel(QVariant(this->m_dsp.second));
289         emit showOnScreen();
290     }
291     else {
292         this->setDisplayStatus(HIDING);
293         mp_wm->deactivateWindow(_myrole);
294     }
295 #endif
296         int display_status = getDisplayStatus();
297         fprintf(stderr, "EventHandler::deactivateWindow(), "
298                         "display_status %d\n", display_status);
299 }
300
301 void EventHandler::onScreenReply(const QString &ons_title, const QString &btn_name)
302 {
303     fprintf(stderr, "ons_title=%s btn_name=%s\n", ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
304     emit this->hideOnScreen();
305
306 #if 0
307     struct json_object* j_param = json_object_new_object();
308     json_object_object_add(j_param, _onscreen_title, json_object_new_string(ons_title.toStdString().c_str()));
309     json_object_object_add(j_param, _button_name, json_object_new_string(btn_name.toStdString().c_str()));
310     mp_hs->replyShowWindow(m_dsp.first.toStdString().c_str(), j_param);
311 #endif
312     fprintf(stderr, "EventHandler::onScreenReply with ons_title %s, btn_name %s\n",
313                     ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
314 }