compositor: Added layout_save/layout_restore
[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 void
95 ivi_shell_activate_surface(struct ivi_surface *ivi_surf,
96                           struct ivi_shell_seat *ivi_seat,
97                           uint32_t flags)
98 {
99        struct weston_desktop_surface *dsurface = ivi_surf->dsurface;
100        struct weston_surface *surface =
101                weston_desktop_surface_get_surface(dsurface);
102
103        weston_view_activate_input(ivi_surf->view, ivi_seat->seat, flags);
104
105        if (ivi_seat->focused_surface) {
106                struct ivi_surface *current_focus =
107                        get_ivi_shell_surface(ivi_seat->focused_surface);
108                struct weston_desktop_surface *dsurface_focus;
109                assert(current_focus);
110
111                dsurface_focus = current_focus->dsurface;
112                if (--current_focus->focus_count == 0)
113                        weston_desktop_surface_set_activated(dsurface_focus, false);
114        }
115
116        ivi_seat->focused_surface = surface;
117        if (ivi_surf->focus_count++ == 0)
118                weston_desktop_surface_set_activated(dsurface, true);
119 }
120
121
122 static void
123 desktop_surface_added_configure(struct ivi_surface *surface,
124                                 struct ivi_output *ivi_output)
125 {
126         enum ivi_surface_role role = IVI_SURFACE_ROLE_NONE;
127         struct weston_desktop_surface *dsurface = surface->dsurface;
128
129         ivi_check_pending_surface_desktop(surface, &role);
130         if ((role != IVI_SURFACE_ROLE_DESKTOP &&
131              role != IVI_SURFACE_ROLE_FULLSCREEN &&
132              role != IVI_SURFACE_ROLE_REMOTE) ||
133              role == IVI_SURFACE_ROLE_NONE)
134                 return;
135
136         if (role == IVI_SURFACE_ROLE_FULLSCREEN) {
137                 struct ivi_output *bg_output =
138                         ivi_layout_find_bg_output(surface->ivi);
139                 assert(bg_output);
140                 weston_desktop_surface_set_fullscreen(dsurface, true);
141                 weston_desktop_surface_set_size(dsurface,
142                                                 bg_output->output->width,
143                                                 bg_output->output->height);
144                 return;
145         }
146
147         weston_desktop_surface_set_maximized(dsurface, true);
148         weston_desktop_surface_set_size(dsurface,
149                                         ivi_output->area.width,
150                                         ivi_output->area.height);
151 }
152
153
154 static void
155 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
156 {
157         struct ivi_compositor *ivi = userdata;
158         struct weston_desktop_client *dclient;
159         struct wl_client *client;
160         struct ivi_surface *surface;
161         struct ivi_output *active_output = NULL;
162         struct weston_output *output = NULL;
163         const char *app_id = NULL;
164
165         dclient = weston_desktop_surface_get_client(dsurface);
166         client = weston_desktop_client_get_client(dclient);
167
168         if (ivi->shell_client.resource &&
169             ivi->shell_client.status == BOUND_FAILED) {
170                 wl_client_post_implementation_error(client,
171                                        "agl_shell has already been bound. "
172                                        "Check out bound_fail event");
173                 return;
174         }
175
176         surface = zalloc(sizeof *surface);
177         if (!surface) {
178                 wl_client_post_no_memory(client);
179                 return;
180         }
181
182         surface->view = weston_desktop_surface_create_view(dsurface);
183         if (!surface->view) {
184                 free(surface);
185                 wl_client_post_no_memory(client);
186                 return;
187         }
188
189         surface->ivi = ivi;
190         surface->dsurface = dsurface;
191         surface->role = IVI_SURFACE_ROLE_NONE;
192         surface->mapped = false;
193         surface->advertised_on_launch = false;
194         surface->checked_pending = false;
195         wl_list_init(&surface->link);
196
197         wl_signal_init(&surface->signal_advertise_app);
198
199         surface->listener_advertise_app.notify = desktop_advertise_app;
200         wl_signal_add(&surface->signal_advertise_app,
201                       &surface->listener_advertise_app);
202
203         weston_desktop_surface_set_user_data(dsurface, surface);
204
205         if (ivi->policy && ivi->policy->api.surface_create &&
206             !ivi->policy->api.surface_create(surface, ivi)) {
207                 wl_client_post_no_memory(client);
208                 return;
209         }
210
211
212         app_id = weston_desktop_surface_get_app_id(dsurface);
213
214         if ((active_output = ivi_layout_find_with_app_id(app_id, ivi)))
215                 ivi_set_pending_desktop_surface_remote(active_output, app_id);
216
217         /* reset any caps to make sure we apply the new caps */
218         ivi_seat_reset_caps_sent(ivi);
219
220         output =  get_focused_output(ivi->compositor);
221         if (!output)
222                 output = get_default_output(ivi->compositor);
223
224         if (output && ivi->shell_client.ready) {
225                 struct ivi_output *ivi_output = to_ivi_output(output);
226                 if (active_output)
227                         desktop_surface_added_configure(surface, active_output);
228                 else
229                         desktop_surface_added_configure(surface, ivi_output);
230         }
231         /*
232          * We delay creating "normal" desktop surfaces until later, to
233          * give the shell-client an oppurtunity to set the surface as a
234          * background/panel.
235          * Also delay the creation in order to have a valid app_id
236          * which will be used to set the proper role.
237          */
238         weston_log("Added surface %p, app_id %s to pending list\n",
239                         surface, app_id);
240         wl_list_insert(&ivi->pending_surfaces, &surface->link);
241
242 }
243
244 static bool
245 desktop_surface_check_last_surfaces(struct ivi_output *ivi_output, enum ivi_surface_role role)
246 {
247         int count = 0;
248         struct ivi_surface *surf;
249
250         wl_list_for_each(surf, &ivi_output->ivi->surfaces, link)
251                 if (surf->role == role &&
252                     surf->current_completed_output == ivi_output)
253                         count++;
254
255         return (count == 1);
256 }
257
258 static void
259 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
260 {
261         struct ivi_surface *surface =
262                 weston_desktop_surface_get_user_data(dsurface);
263         struct weston_surface *wsurface =
264                 weston_desktop_surface_get_surface(dsurface);
265         const char *app_id = NULL;
266         struct weston_seat *wseat = NULL;
267         struct ivi_shell_seat *ivi_seat = NULL;
268         struct ivi_output *output = NULL;
269
270         /* we might not have a valid ivi_surface if _added failed due to
271          * protocol errors */
272         if (!surface)
273                 return;
274
275         wseat = get_ivi_shell_weston_first_seat(surface->ivi);
276         if (wseat)
277                 ivi_seat = get_ivi_shell_seat(wseat);
278
279         output = ivi_layout_get_output_from_surface(surface);
280
281         wl_list_remove(&surface->listener_advertise_app.link);
282         surface->listener_advertise_app.notify = NULL;
283
284         app_id = weston_desktop_surface_get_app_id(dsurface);
285
286         /* special corner-case, pending_surfaces which are never activated or
287          * being assigned an output might land here so just remove the surface;
288          *
289          * the DESKTOP role can happen here as well, because we can fall-back 
290          * to that when we try to determine the role type. Application that
291          * do not set the app_id will be land here, when destroyed */
292         if ((output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
293                                 surface->role == IVI_SURFACE_ROLE_DESKTOP)) ||
294              output->output == NULL)
295                 goto skip_output_asignment;
296
297         assert(output != NULL);
298
299         /* resize the active surface to the original size */
300         if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
301             surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
302                 if (output && output->active) {
303                         ivi_layout_desktop_resize(output->active, output->area_saved);
304                 }
305                 /* restore the area back so we can re-use it again if needed */
306                 output->area = output->area_saved;
307         }
308
309         /* reset the active surface as well */
310         if (output && output->active && output->active == surface) {
311                 output->active->view->is_mapped = false;
312                 output->active->view->surface->is_mapped = false;
313
314                 weston_layer_entry_remove(&output->active->view->layer_link);
315                 output->active = NULL;
316         }
317
318         /* clear out focused_surface to avoid a stale focused_surface. the
319          * client shell is responsible for keeping track and switch back to the
320          * last active surface so we don't get do anything at removal, just
321          * reset it */
322         if (ivi_seat && ivi_seat->focused_surface == wsurface)
323                 ivi_seat->focused_surface = NULL;
324
325         if (surface->role == IVI_SURFACE_ROLE_REMOTE &&
326             output->type == OUTPUT_REMOTE)
327                 ivi_destroy_waltham_destroy(surface);
328
329         /* check if there's a last 'remote' surface and insert a black
330          * surface view if there's no background set for that output
331          */
332         if (desktop_surface_check_last_surfaces(output, IVI_SURFACE_ROLE_REMOTE) ||
333             desktop_surface_check_last_surfaces(output, IVI_SURFACE_ROLE_DESKTOP))
334                 if (!output->background)
335                         insert_black_curtain(output);
336
337
338         if (weston_surface_is_mapped(wsurface)) {
339                 weston_desktop_surface_unlink_view(surface->view);
340                 weston_view_destroy(surface->view);
341         }
342
343         /* invalidate agl-shell surfaces so we can re-use them when
344          * binding again */
345         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
346                 switch (surface->panel.edge) {
347                 case AGL_SHELL_EDGE_TOP:
348                         output->top = NULL;
349                         break;
350                 case AGL_SHELL_EDGE_BOTTOM:
351                         output->bottom = NULL;
352                         break;
353                 case AGL_SHELL_EDGE_LEFT:
354                         output->left = NULL;
355                         break;
356                 case AGL_SHELL_EDGE_RIGHT:
357                         output->right = NULL;
358                         break;
359                 default:
360                         assert(!"Invalid edge detected\n");
361                 }
362         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
363                 output->background = NULL;
364         }
365
366 skip_output_asignment:
367         weston_log("Removed surface %p, app_id %s, role %s\n", surface,
368                         app_id, ivi_layout_get_surface_role_name(surface));
369
370         if (app_id && output && output->output) {
371                 shell_advertise_app_state(output->ivi, app_id,
372                                           NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
373                 if (output->ivi->shell_client.ready)
374                         shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
375         }
376
377         wl_list_remove(&surface->link);
378
379         free(surface);
380 }
381
382 static void
383 desktop_committed(struct weston_desktop_surface *dsurface, 
384                   int32_t sx, int32_t sy, void *userdata)
385 {
386         struct ivi_compositor *ivi = userdata;
387         struct ivi_surface *surface =
388                 weston_desktop_surface_get_user_data(dsurface);
389         struct ivi_policy *policy = surface->ivi->policy;
390
391         if (policy && policy->api.surface_commited &&
392             !policy->api.surface_commited(surface, surface->ivi))
393                 return;
394
395         if (ivi->shell_client.ready && !surface->checked_pending) {
396                 const char * app_id =   weston_desktop_surface_get_app_id(dsurface);
397                 weston_log("Checking pending surface %p, app_id %s\n", surface,
398                         app_id);
399                 wl_list_remove(&surface->link);
400                 wl_list_init(&surface->link);
401                 ivi_check_pending_desktop_surface(surface);
402                 surface->checked_pending = true;
403
404                 /* we'll do it now at commit time, because we might not have an
405                  * appid by the time we've created the weston_desktop_surface
406                  * */
407                 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
408         }
409
410         if (!surface->advertised_on_launch &&
411             !wl_list_empty(&surface->ivi->desktop_clients))
412                 wl_signal_emit(&surface->signal_advertise_app, surface);
413
414         /* this repaint schedule is needed to allow resizing to work with the
415          * help of the hidden layer:
416          *
417          * 1. add the view in the hidden layer and send out correct dimensions
418          * 2. clients changes its dimensions
419          * 3. client commits with the new dimensions
420          *
421          * For desktop and fullscreen, desktop_surface_added() sends the
422          * dimensions from the beginning so applications no need to resize, but
423          * if that weren't the case we still need this in.
424          */
425         weston_compositor_schedule_repaint(surface->ivi->compositor);
426
427         switch (surface->role) {
428         case IVI_SURFACE_ROLE_DESKTOP:
429         case IVI_SURFACE_ROLE_REMOTE:
430                 ivi_layout_desktop_committed(surface);
431                 break;
432         case IVI_SURFACE_ROLE_POPUP:
433                 ivi_layout_popup_committed(surface);
434                 break;
435         case IVI_SURFACE_ROLE_FULLSCREEN:
436                 ivi_layout_fullscreen_committed(surface);
437                 break;
438         case IVI_SURFACE_ROLE_SPLIT_H:
439         case IVI_SURFACE_ROLE_SPLIT_V:
440                 ivi_layout_split_committed(surface);
441                 break;
442         case IVI_SURFACE_ROLE_NONE:
443         case IVI_SURFACE_ROLE_BACKGROUND:
444         case IVI_SURFACE_ROLE_PANEL:
445         default: /* fall through */
446                 break;
447         }
448 }
449
450 static void
451 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
452                          struct weston_seat *seat, int32_t x, int32_t y,
453                          void *userdata)
454 {
455         /* not supported */
456 }
457
458 static void
459 desktop_set_parent(struct weston_desktop_surface *dsurface,
460                    struct weston_desktop_surface *parent, void *userdata)
461 {
462         /* not supported */
463 }
464
465 static void
466 desktop_move(struct weston_desktop_surface *dsurface,
467              struct weston_seat *seat, uint32_t serial, void *userdata)
468 {
469         /* not supported */
470 }
471
472 static void
473 desktop_resize(struct weston_desktop_surface *dsurface,
474                struct weston_seat *seat, uint32_t serial,
475                enum weston_desktop_surface_edge edges, void *user_data)
476 {
477         /* not supported */
478 }
479
480 static void
481 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
482                              bool fullscreen, struct weston_output *output,
483                              void *userdata)
484 {
485         /* not supported */
486 }
487
488 static void
489 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
490                             bool maximized, void *userdata)
491 {
492         /* not supported */
493 }
494
495 static void
496 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
497                             void *userdata)
498 {
499         /* not supported */
500 }
501
502 static void
503 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
504                               int32_t x, int32_t y, void *userdata)
505 {
506         /* not supported */
507 }
508
509 static const struct weston_desktop_api desktop_api = {
510         .struct_size = sizeof desktop_api,
511         .ping_timeout = desktop_ping_timeout,
512         .pong = desktop_pong,
513         .surface_added = desktop_surface_added,
514         .surface_removed = desktop_surface_removed,
515         .committed = desktop_committed,
516         .show_window_menu = desktop_show_window_menu,
517         .set_parent = desktop_set_parent,
518         .move = desktop_move,
519         .resize = desktop_resize,
520         .fullscreen_requested = desktop_fullscreen_requested,
521         .maximized_requested = desktop_maximized_requested,
522         .minimized_requested = desktop_minimized_requested,
523         .set_xwayland_position = desktop_set_xwayland_position,
524 };
525
526 static void
527 ivi_shell_destroy(struct wl_listener *listener, void *data)
528 {
529         struct ivi_compositor *ivi = container_of(listener,
530                                 struct ivi_compositor, destroy_listener);
531
532         ivi_shell_finalize(ivi);
533         ivi_compositor_destroy_pending_surfaces(ivi);
534         ivi_layout_destroy_saved_outputs(ivi);
535
536         weston_desktop_destroy(ivi->desktop);
537         wl_list_remove(&listener->link);
538 }
539
540 int
541 ivi_desktop_init(struct ivi_compositor *ivi)
542 {
543         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
544         if (!ivi->desktop) {
545                 weston_log("Failed to create desktop globals");
546                 return -1;
547         }
548
549         if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
550                         &ivi->destroy_listener, ivi_shell_destroy)) {
551                 return -1;
552         }
553
554         return 0;
555 }