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;
80 application_state_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
81 const char *app_id, const char *app_data, uint32_t app_state, uint32_t app_role)
89 (void) agl_shell_desktop;
92 static const struct agl_shell_desktop_listener agl_shell_desk_listener = {
94 application_state_event,
97 static struct agl_shell_desktop *
98 register_agl_shell_desktop(void)
100 struct wl_display *wl;
101 struct wl_registry *registry;
102 struct agl_shell_desktop *shell = nullptr;
104 QPlatformNativeInterface *native = qApp->platformNativeInterface();
106 wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
107 registry = wl_display_get_registry(wl);
109 wl_registry_add_listener(registry, ®istry_listener, &shell);
110 // Roundtrip to get all globals advertised by the compositor
111 wl_display_roundtrip(wl);
112 wl_registry_destroy(registry);
118 void* EventHandler::myThis = 0;
120 const char _drawing_name[] = "drawing_name";
122 EventHandler::EventHandler(QObject *parent) :
123 QObject(parent), mp_qw(NULL)
128 EventHandler::~EventHandler()
131 agl_shell_desktop_destroy(shell_desktop);
134 void EventHandler::init(int port, const char *token)
139 shell_desktop = register_agl_shell_desktop();
141 agl_shell_desktop_add_listener(shell_desktop, &agl_shell_desk_listener, this);
144 m_launcher = new Launcher(DEFAULT_AFM_UNIX_SOCK, nullptr);
145 if (m_launcher->setup_pws_connection() != 0)
146 HMI_DEBUG("onscreen", "EventHandler::init failed to set-up connection to afm-system-daemon");
149 void EventHandler::setQuickWindow(QQuickWindow *qw)
154 void EventHandler::showWindow(QString id, QString json)
157 struct wl_output *output = getWlOutput(qApp->screens().first());
158 qInfo() << "sending activate_app";
159 agl_shell_desktop_activate_app(shell_desktop,
160 id.toStdString().c_str(),
161 json.toStdString().c_str(),
165 qInfo() << "data from json: " << json.toStdString().c_str();
168 void EventHandler::hideWindow(QString id)
171 agl_shell_desktop_deactivate_app(shell_desktop, id.toStdString().c_str());
175 EventHandler::start(const QString &app_id)
179 if (m_launcher && m_launcher->connection_is_set())
180 pid = m_launcher->start(app_id);
186 EventHandler::is_running(const QString &app_id)
188 if (m_launcher && m_launcher->connection_is_set())
189 return m_launcher->is_running(app_id);
195 EventHandler::set_window_popup(const QString &app_id, int x, int y)
197 struct wl_output *output = getWlOutput(qApp->screens().first());
200 agl_shell_desktop_set_app_property(shell_desktop,
201 app_id.toStdString().c_str(),
202 AGL_SHELL_DESKTOP_APP_ROLE_POPUP, x, y, output);