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