sample/app: Minor tweaks
[apps/onscreenapp.git] / sample / 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 <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>
27
28 #include "eventhandler.h"
29
30 static struct wl_output *
31 getWlOutput(QScreen *screen)
32 {
33         QPlatformNativeInterface *native = qApp->platformNativeInterface();
34         void *output = native->nativeResourceForScreen("output", screen);
35         return static_cast<struct ::wl_output*>(output);
36 }
37
38 static void
39 global_add(void *data, struct wl_registry *reg, uint32_t name,
40            const char *interface, uint32_t version)
41 {
42         struct agl_shell_desktop **shell =
43                 static_cast<struct agl_shell_desktop **>(data);
44
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)
48                 );
49         }
50 }
51
52 static void global_remove(void *data, struct wl_registry *reg, uint32_t id)
53 {
54         (void) data;
55         (void) reg;
56         (void) id;
57 }
58
59 static const struct wl_registry_listener registry_listener = {
60         global_add,
61         global_remove,
62 };
63
64 static void
65 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
66                 const char *app_id)
67 {
68         EventHandler *ev_handler = static_cast<EventHandler *>(data);
69         (void) agl_shell_desktop;
70
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)
74                 return;
75
76         qInfo() << "app_id: " << app_id;
77 }
78
79 static void
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)
82 {
83         /* unused */
84         (void) data;
85         (void) app_id;
86         (void) app_data;
87         (void) app_role;
88         (void) app_state;
89         (void) agl_shell_desktop;
90 }
91
92 static const struct agl_shell_desktop_listener agl_shell_desk_listener = {
93         application_id_event,
94         application_state_event,
95 };
96
97 static struct agl_shell_desktop *
98 register_agl_shell_desktop(void)
99 {
100         struct wl_display *wl;
101         struct wl_registry *registry;
102         struct agl_shell_desktop *shell = nullptr;
103
104         QPlatformNativeInterface *native = qApp->platformNativeInterface();
105
106         wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
107         registry = wl_display_get_registry(wl);
108
109         wl_registry_add_listener(registry, &registry_listener, &shell);
110         // Roundtrip to get all globals advertised by the compositor
111         wl_display_roundtrip(wl);
112         wl_registry_destroy(registry);
113
114         return shell;
115 }
116
117
118 void* EventHandler::myThis = 0;
119
120 const char _drawing_name[] = "drawing_name";
121
122 EventHandler::EventHandler(QObject *parent) :
123     QObject(parent), mp_qw(NULL)
124 {
125
126 }
127
128 EventHandler::~EventHandler()
129 {
130 #if 0
131     if (mp_hs != NULL) {
132         delete mp_hs;
133     }
134     if (mp_wm != NULL) {
135         delete mp_wm;
136     }
137 #endif
138     if (shell_desktop)
139             agl_shell_desktop_destroy(shell_desktop);
140 }
141
142 void EventHandler::init(int port, const char *token)
143 {
144         (void) port;
145         (void) token;
146
147         shell_desktop = register_agl_shell_desktop();
148         if (shell_desktop)
149                 agl_shell_desktop_add_listener(shell_desktop, &agl_shell_desk_listener, this);
150
151
152         m_launcher = new Launcher(DEFAULT_AFM_UNIX_SOCK, nullptr);
153         if (m_launcher->setup_pws_connection() != 0)
154                 HMI_DEBUG("onscreen", "EventHandler::init failed to set-up connection to afm-system-daemon");
155
156 #if 0
157     myThis = this;
158     mp_wm = new QLibWindowmanager();
159     mp_wm->init(port, token);
160
161     mp_hs = new QLibHomeScreen();
162     mp_hs->init(port, token);
163
164     mp_hs->set_event_handler(QLibHomeScreen::Event_ShowWindow, [this](json_object *object){
165         this->mp_wm->activateWindow(ROLE_NAME, "normal");
166         HMI_DEBUG(APP_ID, "received showWindow event, end!, line=%d", __LINE__);
167     });
168
169     mp_hs->set_event_handler(QLibHomeScreen::Event_ReplyShowWindow, [this](json_object *object){
170         HMI_DEBUG(APP_ID, "got Event_ReplyShowWindow!\n");
171         const char* msg = json_object_to_json_string(object);
172         emit this->signalOnReplyShowWindow(msg);
173     });
174
175     if (mp_wm->requestSurface(ROLE_NAME) != 0) {
176         HMI_DEBUG(APP_ID, "!!!!LayoutHandler requestSurface Failed!!!!!");
177         exit(EXIT_FAILURE);
178     }
179
180     // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs
181     mp_wm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [this](json_object *object) {
182         HMI_DEBUG(APP_ID, "Surface got syncDraw!");
183         this->mp_wm->endDraw(ROLE_NAME);
184     });
185
186     mp_wm->set_event_handler(QLibWindowmanager::Event_Visible, [this](json_object *object) {
187         struct json_object *value;
188         json_object_object_get_ex(object, _drawing_name, &value);
189         const char *name = json_object_get_string(value);
190
191         HMI_DEBUG(APP_ID, "Event_Active kKeyDrawingName = %s", name);
192     });
193
194     mp_wm->set_event_handler(QLibWindowmanager::Event_Invisible, [this](json_object *object) {
195         struct json_object *value;
196         json_object_object_get_ex(object, _drawing_name, &value);
197         const char *name = json_object_get_string(value);
198
199         HMI_DEBUG(APP_ID, "Event_Inactive kKeyDrawingName = %s", name);
200     });
201
202     HMI_DEBUG(APP_ID, "LayoutHander::init() finished.");
203 #endif
204 }
205
206 void EventHandler::setQuickWindow(QQuickWindow *qw)
207 {
208     mp_qw = qw;
209     //QObject::connect(mp_qw, SIGNAL(frameSwapped()), mp_wm, SLOT(slotActivateSurface()));
210 }
211
212 void EventHandler::showWindow(QString id, QString json)
213 {
214         if (shell_desktop) {
215                 struct wl_output *output = getWlOutput(qApp->screens().first());
216                 qInfo() << "sending activate_app";
217                 agl_shell_desktop_activate_app(shell_desktop,
218                                 id.toStdString().c_str(),
219                                 json.toStdString().c_str(),
220                                 output);
221         }
222
223         qInfo() << "data from json: " << json.toStdString().c_str();
224 #if 0
225     if(json.isNull())
226         mp_hs->tapShortcut(id);
227     else
228         mp_hs->showWindow(id.toStdString().c_str(), json_tokener_parse(json.toStdString().c_str()));
229 #endif
230 }
231
232 void EventHandler::hideWindow(QString id)
233 {
234         if (shell_desktop)
235                 agl_shell_desktop_deactivate_app(shell_desktop, id.toStdString().c_str());
236 #if 0
237     mp_hs->hideWindow(id.toStdString().c_str());
238 #endif
239 }
240
241 int
242 EventHandler::start(const QString &app_id)
243 {
244         int pid = -1;
245
246         if (m_launcher && m_launcher->connection_is_set())
247                 pid = m_launcher->start(app_id);
248
249         return pid;
250 }
251
252 bool
253 EventHandler::is_running(const QString &app_id)
254 {
255         if (m_launcher && m_launcher->connection_is_set())
256                 return m_launcher->is_running(app_id);
257
258         return false;
259 }
260
261 void
262 EventHandler::set_window_popup(const QString &app_id, int x, int y)
263 {
264         struct wl_output *output = getWlOutput(qApp->screens().first());
265
266         if (shell_desktop)
267                 agl_shell_desktop_set_app_property(shell_desktop,
268                                 app_id.toStdString().c_str(),
269                                 AGL_SHELL_DESKTOP_APP_ROLE_POPUP, x, y, output);
270 }