2 * Copyright (c) 2019 TOYOTA MOTOR CORPORATION
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <QGuiApplication>
21 #include <QJsonDocument>
22 #include <QJsonObject>
23 #include <QQmlContext>
24 #include <QtQml/QQmlApplicationEngine>
27 #include <qpa/qplatformnativeinterface.h>
29 #include "eventhandler.h"
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";
40 void* EventHandler::myThis = 0;
42 static struct wl_output *
43 getWlOutput(QScreen *screen)
45 QPlatformNativeInterface *native = qApp->platformNativeInterface();
46 void *output = native->nativeResourceForScreen("output", screen);
47 return static_cast<struct ::wl_output*>(output);
51 global_add(void *data, struct wl_registry *reg, uint32_t name,
52 const char *interface, uint32_t version)
54 struct agl_shell_desktop **shell =
55 static_cast<struct agl_shell_desktop **>(data);
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)
64 static void global_remove(void *data, struct wl_registry *reg, uint32_t id)
71 static const struct wl_registry_listener registry_listener = {
77 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
80 EventHandler *ev_handler = static_cast<EventHandler *>(data);
81 (void) agl_shell_desktop;
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)
88 qInfo() << "app_id: " << app_id;
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)
96 EventHandler *ev_handler = static_cast<EventHandler *>(data);
97 (void) agl_shell_desktop;
99 qInfo() << "got application_state_event() for app_id=" <<
100 app_id << ", state=" << app_state << ", role=" << app_role;
102 if (strcmp(app_id, APP_ID))
105 if (app_state != AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED)
108 if (app_role != AGL_SHELL_DESKTOP_APP_ROLE_POPUP)
111 qInfo() << "Got message " << app_data;
113 // should signal that we got the message
114 emit ev_handler->updateModel(QVariant(app_data));
115 emit ev_handler->showOnScreen();
118 static const struct agl_shell_desktop_listener agl_shell_desk_listener = {
119 application_id_event,
120 application_state_event,
123 static struct agl_shell_desktop *
124 register_agl_shell_desktop(void)
126 struct wl_display *wl;
127 struct wl_registry *registry;
128 struct agl_shell_desktop *shell = nullptr;
130 QPlatformNativeInterface *native = qApp->platformNativeInterface();
132 wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
133 registry = wl_display_get_registry(wl);
135 wl_registry_add_listener(registry, ®istry_listener, &shell);
136 // Roundtrip to get all globals advertised by the compositor
137 wl_display_roundtrip(wl);
138 wl_registry_destroy(registry);
144 EventHandler::EventHandler(QObject *parent) :
150 EventHandler::~EventHandler()
153 agl_shell_desktop_destroy(shell_desktop);
156 void EventHandler::init(int port, const char *token)
161 shell_desktop = register_agl_shell_desktop();
163 agl_shell_desktop_add_listener(shell_desktop, &agl_shell_desk_listener, this);
166 mp_wm = new QLibWindowmanager();
167 mp_wm->init(port, token);
169 mp_hs = new LibHomeScreen();
170 mp_hs->init(port, token);
172 mp_hs->registerCallback(nullptr, EventHandler::onRep_static);
173 mp_hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [this](json_object *object){
176 "application_id": "onscreenapp",
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"
185 HMI_DEBUG(APP_ID, "recived json message is[%s]", json_object_get_string(object));
187 struct json_object *param;
188 if(!json_object_object_get_ex(object, _parameter, ¶m)
189 || json_object_get_type(param) != json_type_object) {
190 HMI_DEBUG(APP_ID, "parameter error!");
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!");
202 m_req = qMakePair(QString(replyto), QString(json_object_to_json_string(param)));
204 if (this->getDisplayStatus() == HIDING) {
205 this->activateWindow(_myrole, "on_screen");
207 else if(this->getDisplayStatus() == SHOWING) {
208 this->setDisplayStatus(SWAPPING);
209 emit this->hideOnScreen();
212 HMI_DEBUG(APP_ID, "onscreen swapping!");
214 HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
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));
222 if (mp_wm->requestSurface(_myrole) != 0) {
223 HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
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));
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();
243 HMI_DEBUG(APP_ID, "Event_Visible kKeyDrawingName = %s", name);
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);
251 HMI_DEBUG(APP_ID, "Event_Invisible kKeyDrawingName = %s", name);
254 HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
259 void EventHandler::onRep_static(struct json_object* reply_contents)
261 static_cast<EventHandler*>(EventHandler::myThis)->onRep(reply_contents);
264 void EventHandler::onRep(struct json_object* reply_contents)
266 const char* str = json_object_to_json_string(reply_contents);
267 HMI_DEBUG(APP_ID, "EventHandler::onReply %s", str);
271 void EventHandler::activateWindow(const char *role, const char *area)
274 HMI_DEBUG(APP_ID, "EventHandler::activateWindow()");
275 mp_wm->activateWindow(role, area);
277 fprintf(stderr, "EventHandler::activateWindow() role %s, area %s\n",
281 void EventHandler::deactivateWindow()
284 HMI_DEBUG(APP_ID, "EventHandler::deactivateWindow()");
285 if(getDisplayStatus() == SWAPPING) {
286 setDisplayStatus(SHOWING);
288 updateModel(QVariant(this->m_dsp.second));
292 this->setDisplayStatus(HIDING);
293 mp_wm->deactivateWindow(_myrole);
296 int display_status = getDisplayStatus();
297 fprintf(stderr, "EventHandler::deactivateWindow(), "
298 "display_status %d\n", display_status);
301 void EventHandler::onScreenReply(const QString &ons_title, const QString &btn_name)
303 fprintf(stderr, "ons_title=%s btn_name=%s\n", ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
304 emit this->hideOnScreen();
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);
312 fprintf(stderr, "EventHandler::onScreenReply with ons_title %s, btn_name %s\n",
313 ons_title.toStdString().c_str(), btn_name.toStdString().c_str());