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.
19 #include <QGuiApplication>
20 #include <QJsonDocument>
21 #include <QJsonObject>
22 #include <QQuickWindow>
23 #include <QtQml/QQmlContext>
24 #include <QQmlContext>
25 #include <QtQml/QQmlApplicationEngine>
26 #include <qpa/qplatformnativeinterface.h>
28 #include "eventhandler.h"
30 static struct wl_output *
31 getWlOutput(QScreen *screen)
33 QPlatformNativeInterface *native = qApp->platformNativeInterface();
34 void *output = native->nativeResourceForScreen("output", screen);
35 return static_cast<struct ::wl_output*>(output);
39 global_add(void *data, struct wl_registry *reg, uint32_t name,
40 const char *interface, uint32_t version)
42 struct agl_shell_desktop **shell =
43 static_cast<struct agl_shell_desktop **>(data);
45 if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
46 *shell = static_cast<struct agl_shell_desktop *>(
47 wl_registry_bind(reg, name, &agl_shell_desktop_interface, version)
52 static void global_remove(void *data, struct wl_registry *reg, uint32_t id)
59 static const struct wl_registry_listener registry_listener = {
65 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
68 EventHandler *ev_handler = static_cast<EventHandler *>(data);
69 (void) agl_shell_desktop;
71 // should probably add here to a list the application or trigger emit
72 // for QML code, also note that we get here our own application
73 if (strcmp(app_id, APP_ID) == 0)
76 qInfo() << "app_id: " << app_id;
78 if (strcmp(app_id, "onescreenapp") == 0)
79 emit ev_handler->signalOnReplyShowWindow(app_id);
82 static const struct agl_shell_desktop_listener agl_shell_desk_listener = {
86 static struct agl_shell_desktop *
87 register_agl_shell_desktop(void)
89 struct wl_display *wl;
90 struct wl_registry *registry;
91 struct agl_shell_desktop *shell = nullptr;
93 QPlatformNativeInterface *native = qApp->platformNativeInterface();
95 wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
96 registry = wl_display_get_registry(wl);
98 wl_registry_add_listener(registry, ®istry_listener, &shell);
99 // Roundtrip to get all globals advertised by the compositor
100 wl_display_roundtrip(wl);
101 wl_registry_destroy(registry);
107 void* EventHandler::myThis = 0;
109 const char _drawing_name[] = "drawing_name";
111 EventHandler::EventHandler(QObject *parent) :
112 QObject(parent), mp_qw(NULL)
117 EventHandler::~EventHandler()
128 agl_shell_desktop_destroy(shell_desktop);
131 void EventHandler::init(int port, const char *token)
136 shell_desktop = register_agl_shell_desktop();
138 agl_shell_desktop_add_listener(shell_desktop, &agl_shell_desk_listener, this);
141 m_launcher = new Launcher(DEFAULT_AFM_UNIX_SOCK, nullptr);
142 if (m_launcher->setup_pws_connection() != 0)
143 HMI_DEBUG("onscreen", "EventHandler::init failed to set-up connection to afm-system-daemon");
147 mp_wm = new QLibWindowmanager();
148 mp_wm->init(port, token);
150 mp_hs = new QLibHomeScreen();
151 mp_hs->init(port, token);
153 mp_hs->set_event_handler(QLibHomeScreen::Event_ShowWindow, [this](json_object *object){
154 this->mp_wm->activateWindow(ROLE_NAME, "normal");
155 HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
158 mp_hs->set_event_handler(QLibHomeScreen::Event_ReplyShowWindow, [this](json_object *object){
159 HMI_DEBUG(APP_ID, "got Event_ReplyShowWindow!\n");
160 const char* msg = json_object_to_json_string(object);
161 emit this->signalOnReplyShowWindow(msg);
164 if (mp_wm->requestSurface(ROLE_NAME) != 0) {
165 HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
169 // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs
170 mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
171 HMI_DEBUG(APP_ID, "Surface got syncDraw!");
172 this->mp_wm->endDraw(ROLE_NAME);
175 mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
176 struct json_object *value;
177 json_object_object_get_ex(object, _drawing_name, &value);
178 const char *name = json_object_get_string(value);
180 HMI_DEBUG(APP_ID, "Event_Active kKeyDrawingName = %s", name);
183 mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
184 struct json_object *value;
185 json_object_object_get_ex(object, _drawing_name, &value);
186 const char *name = json_object_get_string(value);
188 HMI_DEBUG(APP_ID, "Event_Inactive kKeyDrawingName = %s", name);
191 HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
195 void EventHandler::setQuickWindow(QQuickWindow *qw)
198 //QObject::connect(mp_qw, SIGNAL(frameSwapped()), mp_wm, SLOT(slotActivateSurface()));
201 void EventHandler::showWindow(QString id, QString json)
204 struct wl_output *output = getWlOutput(qApp->screens().first());
205 agl_shell_desktop_activate_app(shell_desktop, id.toStdString().c_str(), output);
208 qInfo() << "data from json: " << json.toStdString().c_str();
211 mp_hs->tapShortcut(id);
213 mp_hs->showWindow(id.toStdString().c_str(), json_tokener_parse(json.toStdString().c_str()));
217 void EventHandler::hideWindow(QString id)
220 agl_shell_desktop_deactivate_app(shell_desktop, id.toStdString().c_str());
222 mp_hs->hideWindow(id.toStdString().c_str());
227 EventHandler::start(const QString &app_id)
231 if (m_launcher && m_launcher->connection_is_set())
232 pid = m_launcher->start(app_id);
238 EventHandler::is_running(const QString &app_id)
240 if (m_launcher && m_launcher->connection_is_set())
241 return m_launcher->is_running(app_id);