2 * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3 * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include "homescreenhandler.h"
21 #include "hmi-debug.h"
23 #include <QGuiApplication>
24 #include <wayland-client.h>
25 #include <qpa/qplatformnativeinterface.h>
27 #include "shell-desktop.h"
29 void* HomescreenHandler::myThis = 0;
31 static struct wl_output *
32 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
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)
53 global_remove(void *data, struct wl_registry *reg, uint32_t id)
60 static const struct wl_registry_listener registry_listener = {
66 static struct agl_shell_desktop *
67 register_agl_shell_desktop(void)
69 struct wl_display *wl;
70 struct wl_registry *registry;
71 struct agl_shell_desktop *shell = nullptr;
73 QPlatformNativeInterface *native = qApp->platformNativeInterface();
75 wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
76 registry = wl_display_get_registry(wl);
78 wl_registry_add_listener(registry, ®istry_listener, &shell);
79 // Roundtrip to get all globals advertised by the compositor
80 wl_display_roundtrip(wl);
81 wl_registry_destroy(registry);
86 HomescreenHandler::HomescreenHandler(QObject *parent) : QObject(parent)
90 HomescreenHandler::~HomescreenHandler()
94 void HomescreenHandler::init(int port, const char *token, QString myname)
99 mp_hs = new LibHomeScreen();
100 mp_hs->init(port, token);
101 mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
103 struct agl_shell_desktop *agl_shell = register_agl_shell_desktop();
105 fprintf(stderr, "agl_shell extension is not advertised. "
106 "Are you sure that agl-compositor is running?\n");
110 std::shared_ptr<struct agl_shell_desktop> shell{agl_shell, agl_shell_desktop_destroy};
111 this->aglShell = new Shell(shell, nullptr);
113 mp_hs->set_event_handler(LibHomeScreen::Event_AppListChanged,[this](json_object *object){
114 HMI_DEBUG("Launcher","laucher: appListChanged [%s]\n", json_object_to_json_string(object));
116 struct json_object *obj_param, *obj_oper, *obj_data;
117 if(json_object_object_get_ex(object, "parameter", &obj_param)
118 && json_object_object_get_ex(obj_param, "operation", &obj_oper)
119 && json_object_object_get_ex(obj_param, "data", &obj_data)) {
120 QString oper = json_object_get_string(obj_oper);
121 if(oper == "uninstall") {
122 /* { "application_id": "launcher",
123 * "type": "application-list-changed",
125 * "operation": "uninstall",
126 * "data": "onstestapp@0.1"
129 QString id = json_object_get_string(obj_data);
132 info << "" << "" << id;
133 emit appListUpdate(info);
135 else if(oper == "install") {
136 /* { "application_id": "launcher",
137 * "type": "application-list-changed",
139 * "operation": "install",
141 * "description":"This is a demo onstestapp application",
142 * "name": "onstestapp",
144 * "id": "onstestapp@0.1",
147 * "author-email": "",
150 * "icon": "\/var\/local\/lib\/afm\/applications\/onstestapp\/0.1\/icon.svg",
155 struct json_object *obj_icon, *obj_name, *obj_id;
156 if(json_object_object_get_ex(obj_data, "icon", &obj_icon)
157 && json_object_object_get_ex(obj_data, "name", &obj_name)
158 && json_object_object_get_ex(obj_data, "id", &obj_id)) {
159 QString icon = json_object_get_string(obj_icon);
160 QString name = json_object_get_string(obj_name);
161 QString id = json_object_get_string(obj_id);
164 info << icon << name << id;
165 emit appListUpdate(info);
169 HMI_ERROR("Launcher","error operation.\n");
175 void HomescreenHandler::tapShortcut(QString application_id, QString output_name)
177 HMI_DEBUG("Launcher","tapShortcut %s", application_id.toStdString().c_str());
178 struct json_object* j_json = json_object_new_object();
179 struct json_object* value;
180 value = json_object_new_string("normal.full");
181 json_object_object_add(j_json, "area", value);
183 mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
184 if (output_name.isEmpty())
185 aglShell->activate_app(nullptr, application_id, nullptr);
187 aglShell->activate_app_by_screen(output_name, application_id, nullptr);
190 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
192 static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
195 void HomescreenHandler::onRep(struct json_object* reply_contents)
197 HMI_DEBUG("launcher","HomeScreen onReply %s", json_object_to_json_string(reply_contents));
198 struct json_object *obj_res, *obj_verb;
199 if(json_object_object_get_ex(reply_contents, "response", &obj_res)
200 && json_object_object_get_ex(obj_res, "verb", &obj_verb)
201 && !strcasecmp("getRunnables", json_object_get_string(obj_verb))) {
202 struct json_object *obj_data;
203 if(json_object_object_get_ex(obj_res, "data", &obj_data)) {
204 HMI_DEBUG("launcher","HomescreenHandler emit initAppList");
205 QString data = json_object_to_json_string(obj_data);
206 emit initAppList(data);
211 void HomescreenHandler::getRunnables(void)
213 mp_hs->getRunnables();