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 void EventHandler::activateWindow(const char *role, const char *area)
168 fprintf(stderr, "EventHandler::activateWindow() role %s, area %s\n",
172 void EventHandler::deactivateWindow()
174 int display_status = getDisplayStatus();
175 fprintf(stderr, "EventHandler::deactivateWindow(), "
176 "display_status %d\n", display_status);
179 agl_shell_desktop_deactivate_app(shell_desktop, APP_ID);
183 void EventHandler::onScreenReply(const QString &ons_title, const QString &btn_name)
185 fprintf(stderr, "ons_title=%s btn_name=%s\n", ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
186 emit this->hideOnScreen();
188 fprintf(stderr, "EventHandler::onScreenReply with ons_title %s, btn_name %s\n",
189 ons_title.toStdString().c_str(), btn_name.toStdString().c_str());