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