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