app: Fixes and some monior tweaks to the qml file
[apps/onscreenapp.git] / 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 <QDebug>
20 #include <QGuiApplication>
21 #include <QJsonDocument>
22 #include <QJsonObject>
23 #include <QQmlContext>
24 #include <QtQml/QQmlApplicationEngine>
25 #include <cstring>
26 #include <QFileInfo>
27 #include <qpa/qplatformnativeinterface.h>
28
29 #include "eventhandler.h"
30
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";
38
39
40 void* EventHandler::myThis = 0;
41
42 static struct wl_output *
43 getWlOutput(QScreen *screen)
44 {
45         QPlatformNativeInterface *native = qApp->platformNativeInterface();
46         void *output = native->nativeResourceForScreen("output", screen);
47         return static_cast<struct ::wl_output*>(output);
48 }
49
50 static void
51 global_add(void *data, struct wl_registry *reg, uint32_t name,
52            const char *interface, uint32_t version)
53 {
54         struct agl_shell_desktop **shell =
55                 static_cast<struct agl_shell_desktop **>(data);
56
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)
60                 );
61         }
62 }
63
64 static void global_remove(void *data, struct wl_registry *reg, uint32_t id)
65 {
66         (void) data;
67         (void) reg;
68         (void) id;
69 }
70
71 static const struct wl_registry_listener registry_listener = {
72         global_add,
73         global_remove,
74 };
75
76 static void
77 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
78                 const char *app_id)
79 {
80         EventHandler *ev_handler = static_cast<EventHandler *>(data);
81         (void) agl_shell_desktop;
82
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)
86                 return;
87
88         qInfo() << "app_id: " << app_id;
89 }
90
91 static void
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)
95 {
96         EventHandler *ev_handler = static_cast<EventHandler *>(data);
97         (void) agl_shell_desktop;
98
99         qInfo() << "got application_state_event() for app_id=" <<
100                 app_id << ", state=" << app_state << ", role=" << app_role;
101
102         if (strcmp(app_id, APP_ID))
103                 return;
104
105         if (app_state != AGL_SHELL_DESKTOP_APP_STATE_ACTIVATED)
106                 return;
107
108         if (app_role != AGL_SHELL_DESKTOP_APP_ROLE_POPUP)
109                 return;
110
111         qInfo() << "Got message " << app_data;
112
113         // should signal that we got the message
114         emit ev_handler->updateModel(QVariant(app_data));
115         emit ev_handler->showOnScreen();
116 }
117
118 static const struct agl_shell_desktop_listener agl_shell_desk_listener = {
119         application_id_event,
120         application_state_event,
121 };
122
123 static struct agl_shell_desktop *
124 register_agl_shell_desktop(void)
125 {
126         struct wl_display *wl;
127         struct wl_registry *registry;
128         struct agl_shell_desktop *shell = nullptr;
129
130         QPlatformNativeInterface *native = qApp->platformNativeInterface();
131
132         wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
133         registry = wl_display_get_registry(wl);
134
135         wl_registry_add_listener(registry, &registry_listener, &shell);
136         // Roundtrip to get all globals advertised by the compositor
137         wl_display_roundtrip(wl);
138         wl_registry_destroy(registry);
139
140         return shell;
141 }
142
143
144 EventHandler::EventHandler(QObject *parent) :
145     QObject(parent),
146     m_dsp_sts(false)
147 {
148 }
149
150 EventHandler::~EventHandler()
151 {
152     if (shell_desktop)
153             agl_shell_desktop_destroy(shell_desktop);
154 }
155
156 void EventHandler::init(int port, const char *token)
157 {
158         (void) port;
159         (void) token;
160
161         shell_desktop = register_agl_shell_desktop();
162         if (shell_desktop)
163                 agl_shell_desktop_add_listener(shell_desktop, &agl_shell_desk_listener, this);
164 }
165
166 void EventHandler::activateWindow(const char *role, const char *area)
167 {
168         fprintf(stderr, "EventHandler::activateWindow() role %s, area %s\n",
169                         role, area);
170 }
171
172 void EventHandler::deactivateWindow()
173 {
174         int display_status = getDisplayStatus();
175         fprintf(stderr, "EventHandler::deactivateWindow(), "
176                         "display_status %d\n", display_status);
177
178         if (shell_desktop) {
179                 agl_shell_desktop_deactivate_app(shell_desktop, APP_ID);
180         }
181 }
182
183 void EventHandler::onScreenReply(const QString &ons_title, const QString &btn_name)
184 {
185     fprintf(stderr, "ons_title=%s btn_name=%s\n", ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
186     emit this->hideOnScreen();
187
188     fprintf(stderr, "EventHandler::onScreenReply with ons_title %s, btn_name %s\n",
189                     ons_title.toStdString().c_str(), btn_name.toStdString().c_str());
190 }