homescreenhandler: Add the ability to specify the output based on the
[apps/launcher.git] / launcher / src / homescreenhandler.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
3  * Copyright (c) 2018,2019 TOYOTA MOTOR CORPORATION
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include <QFileInfo>
19 #include "homescreenhandler.h"
20 #include <functional>
21 #include "hmi-debug.h"
22
23 #include <QGuiApplication>
24 #include <wayland-client.h>
25 #include <qpa/qplatformnativeinterface.h>
26
27 #include "shell-desktop.h"
28
29 void* HomescreenHandler::myThis = 0;
30
31 static struct wl_output *
32 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
33 {
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
53 global_remove(void *data, struct wl_registry *reg, uint32_t id)
54 {
55         (void) data;
56         (void) reg;
57         (void) id;
58 }
59
60 static const struct wl_registry_listener registry_listener = {
61         global_add,
62         global_remove,
63 };
64
65
66 static struct agl_shell_desktop *
67 register_agl_shell_desktop(void)
68 {
69         struct wl_display *wl;
70         struct wl_registry *registry;
71         struct agl_shell_desktop *shell = nullptr;
72
73         QPlatformNativeInterface *native = qApp->platformNativeInterface();
74
75         wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
76         registry = wl_display_get_registry(wl);
77
78         wl_registry_add_listener(registry, &registry_listener, &shell);
79         // Roundtrip to get all globals advertised by the compositor
80         wl_display_roundtrip(wl);
81         wl_registry_destroy(registry);
82
83         return shell;
84 }
85
86 HomescreenHandler::HomescreenHandler(QObject *parent) : QObject(parent)
87 {
88 }
89
90 HomescreenHandler::~HomescreenHandler()
91 {
92 }
93
94 void HomescreenHandler::init(int port, const char *token, QString myname)
95 {
96     myThis = this;
97     m_myname = myname;
98
99     mp_hs = new LibHomeScreen();
100     mp_hs->init(port, token);
101     mp_hs->registerCallback(nullptr, HomescreenHandler::onRep_static);
102
103     struct agl_shell_desktop *agl_shell = register_agl_shell_desktop();
104     if (!agl_shell) {
105             fprintf(stderr, "agl_shell extension is not advertised. "
106                             "Are you sure that agl-compositor is running?\n");
107             exit(EXIT_FAILURE);
108     }
109
110     std::shared_ptr<struct agl_shell_desktop> shell{agl_shell, agl_shell_desktop_destroy};
111     this->aglShell = new Shell(shell, nullptr);
112
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));
115
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",
124                  *   "parameter":{
125                  *      "operation": "uninstall",
126                  *      "data": "onstestapp@0.1"
127                  *   }
128                  * } */
129                 QString id = json_object_get_string(obj_data);
130                 QStringList info;
131                 // icon, name, id
132                 info << "" << "" << id;
133                 emit appListUpdate(info);
134             }
135             else if(oper == "install") {
136                 /* { "application_id": "launcher",
137                  *   "type": "application-list-changed",
138                  *   "parameter": {
139                  *      "operation": "install",
140                  *      "data": {
141                  *          "description":"This is a demo onstestapp application",
142                  *          "name": "onstestapp",
143                  *          "shortname": "",
144                  *          "id": "onstestapp@0.1",
145                  *          "version": "0.1",
146                  *          "author": "Qt",
147                  *          "author-email": "",
148                  *          "width": "",
149                  *          "height": "",
150                  *          "icon": "\/var\/local\/lib\/afm\/applications\/onstestapp\/0.1\/icon.svg",
151                  *          "http-port": 31022
152                  *      }
153                  *    }
154                  * } */
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);
162                     QStringList info;
163                     // icon, name, id
164                     info << icon << name << id;
165                     emit appListUpdate(info);
166                 }
167             }
168             else {
169                 HMI_ERROR("Launcher","error operation.\n");
170             }
171         }
172     });
173 }
174
175 void HomescreenHandler::tapShortcut(QString application_id, QString output_name)
176 {
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);
182
183         mp_hs->showWindow(application_id.toStdString().c_str(), j_json);
184         if (output_name.isEmpty())
185                 aglShell->activate_app(nullptr, application_id, nullptr);
186         else
187                 aglShell->activate_app_by_screen(output_name, application_id, nullptr);
188 }
189
190 void HomescreenHandler::onRep_static(struct json_object* reply_contents)
191 {
192     static_cast<HomescreenHandler*>(HomescreenHandler::myThis)->onRep(reply_contents);
193 }
194
195 void HomescreenHandler::onRep(struct json_object* reply_contents)
196 {
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);
207         }
208     }
209 }
210
211 void HomescreenHandler::getRunnables(void)
212 {
213     mp_hs->getRunnables();
214 }