desktop: Do not attempt to advertise the clients if there are none
[src/agl-compositor.git] / src / desktop.c
1 /*
2  * Copyright © 2019 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include <assert.h>
27 #include "ivi-compositor.h"
28 #include "policy.h"
29
30 #include <libweston/libweston.h>
31 #include <libweston-desktop/libweston-desktop.h>
32
33 #if 0
34 static struct weston_output *
35 get_default_output(struct weston_compositor *compositor)
36 {
37         if (wl_list_empty(&compositor->output_list))
38                 return NULL;
39
40         return wl_container_of(compositor->output_list.next,
41                                struct weston_output, link);
42 }
43 #endif
44
45 static void
46 desktop_advertise_app(struct wl_listener *listener, void *data)
47 {
48         struct ivi_surface *surface;
49
50         surface = wl_container_of(listener, surface, listener_advertise_app);
51
52         agl_shell_desktop_advertise_application_id(surface->ivi, surface);
53 }
54
55 static void
56 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
57 {
58         /* not supported */
59 }
60
61 static void
62 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
63 {
64         /* not supported */
65 }
66
67 static void
68 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
69 {
70         struct ivi_compositor *ivi = userdata;
71         struct weston_desktop_client *dclient;
72         struct wl_client *client;
73         struct ivi_surface *surface;
74         struct ivi_output *active_output = NULL;
75         const char *app_id = NULL;
76
77         dclient = weston_desktop_surface_get_client(dsurface);
78         client = weston_desktop_client_get_client(dclient);
79
80         surface = zalloc(sizeof *surface);
81         if (!surface) {
82                 wl_client_post_no_memory(client);
83                 return;
84         }
85
86         surface->view = weston_desktop_surface_create_view(dsurface);
87         if (!surface->view) {
88                 free(surface);
89                 wl_client_post_no_memory(client);
90                 return;
91         }
92
93         surface->ivi = ivi;
94         surface->dsurface = dsurface;
95         surface->role = IVI_SURFACE_ROLE_NONE;
96         surface->activated_by_default = false;
97         surface->advertised_on_launch = false;
98         surface->checked_pending = false;
99
100         wl_signal_init(&surface->signal_advertise_app);
101
102         surface->listener_advertise_app.notify = desktop_advertise_app;
103         wl_signal_add(&surface->signal_advertise_app,
104                       &surface->listener_advertise_app);
105
106         weston_desktop_surface_set_user_data(dsurface, surface);
107
108         if (ivi->policy && ivi->policy->api.surface_create &&
109             !ivi->policy->api.surface_create(surface, ivi)) {
110                 wl_client_post_no_memory(client);
111                 return;
112         }
113
114
115         app_id = weston_desktop_surface_get_app_id(dsurface);
116
117         if ((active_output = ivi_layout_find_with_app_id(app_id, ivi)))
118                 ivi_set_pending_desktop_surface_remote(active_output, app_id);
119
120         /* reset any caps to make sure we apply the new caps */
121         ivi_seat_reset_caps_sent(ivi);
122
123         /*
124          * We delay creating "normal" desktop surfaces until later, to
125          * give the shell-client an oppurtunity to set the surface as a
126          * background/panel.
127          * Also delay the creation in order to have a valid app_id
128          * which will be used to set the proper role.
129          */
130         weston_log("Added surface %p, app_id %s to pending list\n",
131                         surface, app_id);
132         wl_list_insert(&ivi->pending_surfaces, &surface->link);
133
134 }
135
136 static bool
137 desktop_surface_check_last_remote_surfaces(struct ivi_compositor *ivi, enum ivi_surface_role role)
138 {
139         int count = 0;
140         struct ivi_surface *surf;
141
142         wl_list_for_each(surf, &ivi->surfaces, link)
143                 if (surf->role == role)
144                         count++;
145
146         return (count == 1);
147 }
148
149 static void
150 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
151 {
152         struct ivi_surface *surface =
153                 weston_desktop_surface_get_user_data(dsurface);
154         struct weston_surface *wsurface =
155                 weston_desktop_surface_get_surface(dsurface);
156
157         struct ivi_output *output = ivi_layout_get_output_from_surface(surface);
158
159         wl_list_remove(&surface->listener_advertise_app.link);
160         surface->listener_advertise_app.notify = NULL;
161
162         /* special corner-case, pending_surfaces which are never activated or
163          * being assigned an output might land here so just remove the surface;
164          *
165          * the DESKTOP role can happen here as well, because we can fall-back 
166          * to that when we try to determine the role type. Application that
167          * do not set the app_id will be land here, when destroyed */
168         if (output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
169                                surface->role == IVI_SURFACE_ROLE_DESKTOP))
170                 goto skip_output_asignment;
171
172         assert(output != NULL);
173
174         /* resize the active surface to the original size */
175         if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
176             surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
177                 if (output && output->active) {
178                         ivi_layout_desktop_resize(output->active, output->area_saved);
179                 }
180                 /* restore the area back so we can re-use it again if needed */
181                 output->area = output->area_saved;
182         }
183
184         /* reset the active surface as well */
185         if (output && output->active && output->active == surface) {
186                 output->active->view->is_mapped = false;
187                 output->active->view->surface->is_mapped = false;
188
189                 weston_layer_entry_remove(&output->active->view->layer_link);
190                 output->active = NULL;
191         }
192
193         if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
194             output->type == OUTPUT_REMOTE)
195                 ivi_destroy_waltham_destroy(surface);
196
197         /* check if there's a last 'remote' surface and insert a black
198          * surface view if there's no background set for that output
199          */
200         if ((desktop_surface_check_last_remote_surfaces(output->ivi,
201                 IVI_SURFACE_ROLE_REMOTE) ||
202             desktop_surface_check_last_remote_surfaces(output->ivi,
203                 IVI_SURFACE_ROLE_DESKTOP)) && output->type == OUTPUT_REMOTE)
204                 if (!output->background)
205                         insert_black_surface(output);
206
207
208         if (weston_surface_is_mapped(wsurface)) {
209                 weston_desktop_surface_unlink_view(surface->view);
210                 weston_view_destroy(surface->view);
211         }
212
213         /* invalidate agl-shell surfaces so we can re-use them when
214          * binding again */
215         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
216                 switch (surface->panel.edge) {
217                 case AGL_SHELL_EDGE_TOP:
218                         output->top = NULL;
219                         break;
220                 case AGL_SHELL_EDGE_BOTTOM:
221                         output->bottom = NULL;
222                         break;
223                 case AGL_SHELL_EDGE_LEFT:
224                         output->left = NULL;
225                         break;
226                 case AGL_SHELL_EDGE_RIGHT:
227                         output->right = NULL;
228                         break;
229                 default:
230                         assert(!"Invalid edge detected\n");
231                 }
232         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
233                 output->background = NULL;
234         }
235
236 skip_output_asignment:
237         weston_log("Removed surface %p, app_id %s, role %s\n", surface,
238                         weston_desktop_surface_get_app_id(dsurface),
239                         ivi_layout_get_surface_role_name(surface));
240
241         /* we weren't added to any list if we are still with 'none' as role */
242         if (surface->role != IVI_SURFACE_ROLE_NONE)
243                 wl_list_remove(&surface->link);
244
245         free(surface);
246 }
247
248 static void
249 desktop_committed(struct weston_desktop_surface *dsurface, 
250                   int32_t sx, int32_t sy, void *userdata)
251 {
252         struct ivi_compositor *ivi = userdata;
253         struct ivi_surface *surface =
254                 weston_desktop_surface_get_user_data(dsurface);
255         struct ivi_policy *policy = surface->ivi->policy;
256
257         if (policy && policy->api.surface_commited &&
258             !policy->api.surface_commited(surface, surface->ivi))
259                 return;
260
261         if (ivi->shell_client.ready && !surface->checked_pending) {
262                 const char * app_id =   weston_desktop_surface_get_app_id(dsurface);
263                 weston_log("Checking pending surface %p, app_id %s\n", surface,
264                         app_id);
265                 wl_list_remove(&surface->link);
266                 wl_list_init(&surface->link);
267                 ivi_check_pending_desktop_surface(surface);
268                 surface->checked_pending = true;
269         }
270
271         if (!surface->advertised_on_launch &&
272             !wl_list_empty(&surface->ivi->desktop_clients))
273                 wl_signal_emit(&surface->signal_advertise_app, surface);
274
275         weston_compositor_schedule_repaint(surface->ivi->compositor);
276
277         switch (surface->role) {
278         case IVI_SURFACE_ROLE_DESKTOP:
279         case IVI_SURFACE_ROLE_REMOTE:
280                 ivi_layout_desktop_committed(surface);
281                 break;
282         case IVI_SURFACE_ROLE_POPUP:
283                 ivi_layout_popup_committed(surface);
284                 break;
285         case IVI_SURFACE_ROLE_FULLSCREEN:
286                 ivi_layout_fullscreen_committed(surface);
287                 break;
288         case IVI_SURFACE_ROLE_SPLIT_H:
289         case IVI_SURFACE_ROLE_SPLIT_V:
290                 ivi_layout_split_committed(surface);
291                 break;
292         case IVI_SURFACE_ROLE_NONE:
293         case IVI_SURFACE_ROLE_BACKGROUND:
294         case IVI_SURFACE_ROLE_PANEL:
295         default: /* fall through */
296                 break;
297         }
298 }
299
300 static void
301 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
302                          struct weston_seat *seat, int32_t x, int32_t y,
303                          void *userdata)
304 {
305         /* not supported */
306 }
307
308 static void
309 desktop_set_parent(struct weston_desktop_surface *dsurface,
310                    struct weston_desktop_surface *parent, void *userdata)
311 {
312         /* not supported */
313 }
314
315 static void
316 desktop_move(struct weston_desktop_surface *dsurface,
317              struct weston_seat *seat, uint32_t serial, void *userdata)
318 {
319         /* not supported */
320 }
321
322 static void
323 desktop_resize(struct weston_desktop_surface *dsurface,
324                struct weston_seat *seat, uint32_t serial,
325                enum weston_desktop_surface_edge edges, void *user_data)
326 {
327         /* not supported */
328 }
329
330 static void
331 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
332                              bool fullscreen, struct weston_output *output,
333                              void *userdata)
334 {
335         /* not supported */
336 }
337
338 static void
339 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
340                             bool maximized, void *userdata)
341 {
342         /* not supported */
343 }
344
345 static void
346 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
347                             void *userdata)
348 {
349         /* not supported */
350 }
351
352 static void
353 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
354                               int32_t x, int32_t y, void *userdata)
355 {
356         /* not supported */
357 }
358
359 static const struct weston_desktop_api desktop_api = {
360         .struct_size = sizeof desktop_api,
361         .ping_timeout = desktop_ping_timeout,
362         .pong = desktop_pong,
363         .surface_added = desktop_surface_added,
364         .surface_removed = desktop_surface_removed,
365         .committed = desktop_committed,
366         .show_window_menu = desktop_show_window_menu,
367         .set_parent = desktop_set_parent,
368         .move = desktop_move,
369         .resize = desktop_resize,
370         .fullscreen_requested = desktop_fullscreen_requested,
371         .maximized_requested = desktop_maximized_requested,
372         .minimized_requested = desktop_minimized_requested,
373         .set_xwayland_position = desktop_set_xwayland_position,
374 };
375
376 int
377 ivi_desktop_init(struct ivi_compositor *ivi)
378 {
379         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
380         if (!ivi->desktop) {
381                 weston_log("Failed to create desktop globals");
382                 return -1;
383         }
384
385         return 0;
386 }