layout: Migrate the layout save/restore to a more useful place
[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.h>
33 #ifdef BUILD_XWAYLAND
34 #include <libweston/xwayland-api.h>
35 #endif
36
37 #include "agl-shell-desktop-server-protocol.h"
38
39 static void
40 ivi_layout_destroy_saved_outputs(struct ivi_compositor *ivi)
41 {
42         struct ivi_output *output, *output_next;
43
44         wl_list_for_each_safe(output, output_next, &ivi->saved_outputs, link) {
45                 free(output->app_ids);
46                 free(output->name);
47
48                 wl_list_remove(&output->link);
49                 free(output);
50         }
51 }
52
53 static void
54 desktop_advertise_app(struct wl_listener *listener, void *data)
55 {
56         struct ivi_surface *surface;
57
58         surface = wl_container_of(listener, surface, listener_advertise_app);
59
60         agl_shell_desktop_advertise_application_id(surface->ivi, surface);
61 }
62
63 static void
64 desktop_ping_timeout(struct weston_desktop_client *dclient, void *userdata)
65 {
66         /* not supported */
67 }
68
69 static void
70 desktop_pong(struct weston_desktop_client *dclient, void *userdata)
71 {
72         /* not supported */
73 }
74
75 struct weston_output *
76 get_default_output(struct weston_compositor *compositor)
77 {
78         if (wl_list_empty(&compositor->output_list))
79                 return NULL;
80
81         return container_of(compositor->output_list.next,
82                         struct weston_output, link);
83 }
84
85 struct weston_output *
86 get_focused_output(struct weston_compositor *compositor)
87 {
88         struct weston_seat *seat;
89         struct weston_output *output = NULL;
90
91         wl_list_for_each(seat, &compositor->seat_list, link) {
92                 struct weston_touch *touch = weston_seat_get_touch(seat);
93                 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
94                 struct weston_keyboard *keyboard =
95                         weston_seat_get_keyboard(seat);
96
97                 if (touch && touch->focus)
98                         output = touch->focus->output;
99                 else if (pointer && pointer->focus)
100                         output = pointer->focus->output;
101                 else if (keyboard && keyboard->focus)
102                         output = keyboard->focus->output;
103
104                 if (output)
105                         break;
106         }
107
108         return output;
109 }
110
111 void
112 ivi_shell_activate_surface(struct ivi_surface *ivi_surf,
113                           struct ivi_shell_seat *ivi_seat,
114                           uint32_t flags)
115 {
116        struct weston_desktop_surface *dsurface = ivi_surf->dsurface;
117        struct weston_surface *surface =
118                weston_desktop_surface_get_surface(dsurface);
119
120        weston_view_activate_input(ivi_surf->view, ivi_seat->seat, flags);
121
122        if (ivi_seat->focused_surface) {
123                struct ivi_surface *current_focus =
124                        get_ivi_shell_surface(ivi_seat->focused_surface);
125                struct weston_desktop_surface *dsurface_focus;
126                assert(current_focus);
127
128                dsurface_focus = current_focus->dsurface;
129                if (--current_focus->focus_count == 0)
130                        weston_desktop_surface_set_activated(dsurface_focus, false);
131        }
132
133        ivi_seat->focused_surface = surface;
134        if (ivi_surf->focus_count++ == 0)
135                weston_desktop_surface_set_activated(dsurface, true);
136 }
137
138
139 static void
140 desktop_surface_added_configure(struct ivi_surface *surface,
141                                 struct ivi_output *ivi_output)
142 {
143         enum ivi_surface_role role = IVI_SURFACE_ROLE_NONE;
144         struct weston_desktop_surface *dsurface = surface->dsurface;
145
146         ivi_check_pending_surface_desktop(surface, &role);
147         if ((role != IVI_SURFACE_ROLE_DESKTOP &&
148              role != IVI_SURFACE_ROLE_FULLSCREEN &&
149              role != IVI_SURFACE_ROLE_REMOTE) ||
150              role == IVI_SURFACE_ROLE_NONE)
151                 return;
152
153         if (role == IVI_SURFACE_ROLE_FULLSCREEN) {
154                 struct ivi_output *bg_output =
155                         ivi_layout_find_bg_output(surface->ivi);
156                 assert(bg_output);
157                 weston_desktop_surface_set_fullscreen(dsurface, true);
158                 weston_desktop_surface_set_size(dsurface,
159                                                 bg_output->output->width,
160                                                 bg_output->output->height);
161                 return;
162         }
163
164         weston_desktop_surface_set_maximized(dsurface, true);
165         weston_desktop_surface_set_size(dsurface,
166                                         ivi_output->area.width,
167                                         ivi_output->area.height);
168 }
169
170
171 static void
172 desktop_surface_added(struct weston_desktop_surface *dsurface, void *userdata)
173 {
174         struct ivi_compositor *ivi = userdata;
175         struct weston_desktop_client *dclient;
176         struct wl_client *client;
177         struct ivi_surface *surface;
178         struct ivi_output *active_output = NULL;
179         struct weston_output *output = NULL;
180         const char *app_id = NULL;
181
182         dclient = weston_desktop_surface_get_client(dsurface);
183         client = weston_desktop_client_get_client(dclient);
184
185         if (ivi->shell_client.resource &&
186             ivi->shell_client.status == BOUND_FAILED) {
187                 wl_client_post_implementation_error(client,
188                                        "agl_shell has already been bound. "
189                                        "Check out bound_fail event");
190                 return;
191         }
192
193         surface = zalloc(sizeof *surface);
194         if (!surface) {
195                 wl_client_post_no_memory(client);
196                 return;
197         }
198
199         surface->view = weston_desktop_surface_create_view(dsurface);
200         if (!surface->view) {
201                 free(surface);
202                 wl_client_post_no_memory(client);
203                 return;
204         }
205
206         surface->ivi = ivi;
207         surface->dsurface = dsurface;
208         surface->role = IVI_SURFACE_ROLE_NONE;
209         surface->mapped = false;
210         surface->advertised_on_launch = false;
211         surface->checked_pending = false;
212         wl_list_init(&surface->link);
213
214         wl_signal_init(&surface->signal_advertise_app);
215
216         surface->listener_advertise_app.notify = desktop_advertise_app;
217         wl_signal_add(&surface->signal_advertise_app,
218                       &surface->listener_advertise_app);
219
220         weston_desktop_surface_set_user_data(dsurface, surface);
221
222         if (ivi->policy && ivi->policy->api.surface_create &&
223             !ivi->policy->api.surface_create(surface, ivi)) {
224                 wl_client_post_no_memory(client);
225                 return;
226         }
227
228
229         app_id = weston_desktop_surface_get_app_id(dsurface);
230
231         if ((active_output = ivi_layout_find_with_app_id(app_id, ivi))) {
232                 ivi_set_pending_desktop_surface_remote(active_output, app_id);
233                 shell_send_app_on_output(ivi, app_id, active_output->output->name);
234         }
235
236         /* reset any caps to make sure we apply the new caps */
237         ivi_seat_reset_caps_sent(ivi);
238
239         output =  get_focused_output(ivi->compositor);
240         if (!output)
241                 output = get_default_output(ivi->compositor);
242
243         if (output && ivi->shell_client.ready) {
244                 struct ivi_output *ivi_output = to_ivi_output(output);
245                 if (active_output)
246                         desktop_surface_added_configure(surface, active_output);
247                 else
248                         desktop_surface_added_configure(surface, ivi_output);
249         }
250         /*
251          * We delay creating "normal" desktop surfaces until later, to
252          * give the shell-client an oppurtunity to set the surface as a
253          * background/panel.
254          * Also delay the creation in order to have a valid app_id
255          * which will be used to set the proper role.
256          */
257         weston_log("Added surface %p, app_id %s to pending list\n",
258                         surface, app_id);
259         wl_list_insert(&ivi->pending_surfaces, &surface->link);
260
261 }
262
263 bool
264 ivi_surface_count_one(struct ivi_output *ivi_output,
265                       enum ivi_surface_role role)
266 {
267         int count = 0;
268         struct ivi_surface *surf;
269
270         wl_list_for_each(surf, &ivi_output->ivi->surfaces, link)
271                 if (surf->role == role &&
272                     surf->current_completed_output == ivi_output)
273                         count++;
274
275         return (count == 1);
276 }
277
278 static void
279 desktop_surface_removed(struct weston_desktop_surface *dsurface, void *userdata)
280 {
281         struct ivi_surface *surface =
282                 weston_desktop_surface_get_user_data(dsurface);
283         struct weston_surface *wsurface =
284                 weston_desktop_surface_get_surface(dsurface);
285         const char *app_id = NULL;
286         struct weston_seat *wseat = NULL;
287         struct ivi_shell_seat *ivi_seat = NULL;
288         struct ivi_output *output = NULL;
289
290         /* we might not have a valid ivi_surface if _added failed due to
291          * protocol errors */
292         if (!surface)
293                 return;
294
295         wseat = get_ivi_shell_weston_first_seat(surface->ivi);
296         if (wseat)
297                 ivi_seat = get_ivi_shell_seat(wseat);
298
299         output = ivi_layout_get_output_from_surface(surface);
300
301         wl_list_remove(&surface->listener_advertise_app.link);
302         surface->listener_advertise_app.notify = NULL;
303
304         app_id = weston_desktop_surface_get_app_id(dsurface);
305
306         /* special corner-case, pending_surfaces which are never activated or
307          * being assigned an output might land here so just remove the surface;
308          *
309          * the DESKTOP role can happen here as well, because we can fall-back 
310          * to that when we try to determine the role type. Application that
311          * do not set the app_id will be land here, when destroyed */
312         if ((output == NULL && (surface->role == IVI_SURFACE_ROLE_NONE ||
313                                 surface->role == IVI_SURFACE_ROLE_DESKTOP)) ||
314              output->output == NULL)
315                 goto skip_output_asignment;
316
317         assert(output != NULL);
318
319         /* resize the active surface to the original size */
320         if (surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
321             surface->role == IVI_SURFACE_ROLE_SPLIT_V) {
322                 if (output && output->active) {
323                         ivi_layout_desktop_resize(output->active, output->area_saved);
324                 }
325                 /* restore the area back so we can re-use it again if needed */
326                 output->area = output->area_saved;
327         }
328
329
330         /* reset the active surface as well */
331         if (output && output->active && output->active == surface) {
332                 output->active->view->is_mapped = false;
333                 output->active->view->surface->is_mapped = false;
334
335                 weston_view_move_to_layer(output->active->view, NULL);
336                 output->active = NULL;
337         }
338
339         /* clear out focused_surface to avoid a stale focused_surface. the
340          * client shell is responsible for keeping track and switch back to the
341          * last active surface so we don't get do anything at removal, just
342          * reset it */
343         if (ivi_seat && ivi_seat->focused_surface == wsurface)
344                 ivi_seat->focused_surface = NULL;
345
346         /* check if there's a last 'remote' surface and insert a black
347          * surface view if there's no background set for that output
348          */
349         if (ivi_surface_count_one(output, IVI_SURFACE_ROLE_REMOTE) ||
350             ivi_surface_count_one(output, IVI_SURFACE_ROLE_DESKTOP))
351                 if (!output->background)
352                         insert_black_curtain(output);
353
354
355         if (weston_surface_is_mapped(wsurface)) {
356                 weston_desktop_surface_unlink_view(surface->view);
357                 weston_view_destroy(surface->view);
358         }
359
360         if (surface->role == IVI_SURFACE_ROLE_TILE) {
361                 ivi_layout_reset_split_surfaces(surface->ivi);
362                 // activate previous when resizing back to give input set
363                 // output active for allowing to resizing again if needed
364                 if (output->previous_active)
365                         ivi_layout_activate_by_surf(output, output->previous_active);
366         }
367
368         /* invalidate agl-shell surfaces so we can re-use them when
369          * binding again */
370         if (surface->role == IVI_SURFACE_ROLE_PANEL) {
371                 switch (surface->panel.edge) {
372                 case AGL_SHELL_EDGE_TOP:
373                         output->top = NULL;
374                         break;
375                 case AGL_SHELL_EDGE_BOTTOM:
376                         output->bottom = NULL;
377                         break;
378                 case AGL_SHELL_EDGE_LEFT:
379                         output->left = NULL;
380                         break;
381                 case AGL_SHELL_EDGE_RIGHT:
382                         output->right = NULL;
383                         break;
384                 default:
385                         assert(!"Invalid edge detected\n");
386                 }
387         } else if (surface->role == IVI_SURFACE_ROLE_BACKGROUND) {
388                 output->background = NULL;
389         }
390
391 skip_output_asignment:
392         weston_log("Removed surface %p, app_id %s, role %s\n", surface,
393                         app_id, ivi_layout_get_surface_role_name(surface));
394
395         if (app_id && output && output->output) {
396                 shell_advertise_app_state(output->ivi, app_id,
397                                           NULL, AGL_SHELL_DESKTOP_APP_STATE_DESTROYED);
398                 if (output->ivi->shell_client.ready)
399                         shell_send_app_state(output->ivi, app_id, AGL_SHELL_APP_STATE_TERMINATED);
400         }
401
402         wl_list_remove(&surface->link);
403
404         free(surface);
405 }
406
407 static void
408 desktop_committed(struct weston_desktop_surface *dsurface, 
409                   struct weston_coord_surface buf_offset, void *userdata)
410 {
411         struct ivi_compositor *ivi = userdata;
412         struct ivi_surface *surface =
413                 weston_desktop_surface_get_user_data(dsurface);
414         struct ivi_policy *policy = surface->ivi->policy;
415
416         if (policy && policy->api.surface_commited &&
417             !policy->api.surface_commited(surface, surface->ivi))
418                 return;
419
420         if (ivi->shell_client.ready && !surface->checked_pending) {
421                 struct ivi_output *remote_output = NULL;
422                 const char *app_id =    weston_desktop_surface_get_app_id(dsurface);
423                 weston_log("Checking pending surface %p, app_id %s\n", surface,
424                         app_id);
425                 wl_list_remove(&surface->link);
426                 wl_list_init(&surface->link);
427
428                 if ((remote_output = ivi_layout_find_with_app_id(app_id, ivi))) {
429                         ivi_set_pending_desktop_surface_remote(remote_output, app_id);
430                         shell_send_app_on_output(ivi, app_id, remote_output->output->name);
431                 }
432
433
434                 ivi_check_pending_desktop_surface(surface);
435                 surface->checked_pending = true;
436
437                 /* we'll do it now at commit time, because we might not have an
438                  * appid by the time we've created the weston_desktop_surface
439                  * */
440                 shell_send_app_state(ivi, app_id, AGL_SHELL_APP_STATE_STARTED);
441         }
442
443         if (!surface->advertised_on_launch &&
444             !wl_list_empty(&surface->ivi->desktop_clients))
445                 wl_signal_emit(&surface->signal_advertise_app, surface);
446
447         /* this repaint schedule is needed to allow resizing to work with the
448          * help of the hidden layer:
449          *
450          * 1. add the view in the hidden layer and send out correct dimensions
451          * 2. clients changes its dimensions
452          * 3. client commits with the new dimensions
453          *
454          * For desktop and fullscreen, desktop_surface_added() sends the
455          * dimensions from the beginning so applications no need to resize, but
456          * if that weren't the case we still need this in.
457          */
458         weston_compositor_schedule_repaint(surface->ivi->compositor);
459
460         switch (surface->role) {
461         case IVI_SURFACE_ROLE_DESKTOP:
462                 ivi_layout_desktop_committed(surface);
463                 break;
464         case IVI_SURFACE_ROLE_REMOTE:
465                 ivi_layout_remote_committed(surface);
466                 break;
467         case IVI_SURFACE_ROLE_POPUP:
468                 ivi_layout_popup_committed(surface);
469                 break;
470         case IVI_SURFACE_ROLE_FULLSCREEN:
471                 ivi_layout_fullscreen_committed(surface);
472                 break;
473         case IVI_SURFACE_ROLE_SPLIT_H:
474         case IVI_SURFACE_ROLE_SPLIT_V:
475                 ivi_layout_split_committed(surface);
476                 break;
477         case IVI_SURFACE_ROLE_NONE:
478         case IVI_SURFACE_ROLE_BACKGROUND:
479         case IVI_SURFACE_ROLE_PANEL:
480         default: /* fall through */
481                 break;
482         }
483 }
484
485 static void
486 desktop_show_window_menu(struct weston_desktop_surface *dsurface,
487                          struct weston_seat *seat, struct weston_coord_surface offset,
488                          void *userdata)
489 {
490         /* not supported */
491 }
492
493 static void
494 desktop_set_parent(struct weston_desktop_surface *dsurface,
495                    struct weston_desktop_surface *parent, void *userdata)
496 {
497         /* not supported */
498 }
499
500 static void
501 desktop_move(struct weston_desktop_surface *dsurface,
502              struct weston_seat *seat, uint32_t serial, void *userdata)
503 {
504         /* not supported */
505 }
506
507 static void
508 desktop_resize(struct weston_desktop_surface *dsurface,
509                struct weston_seat *seat, uint32_t serial,
510                enum weston_desktop_surface_edge edges, void *user_data)
511 {
512         /* not supported */
513 }
514
515 static void
516 desktop_fullscreen_requested(struct weston_desktop_surface *dsurface,
517                              bool fullscreen, struct weston_output *output,
518                              void *userdata)
519 {
520         /* not supported */
521 }
522
523 static void
524 desktop_maximized_requested(struct weston_desktop_surface *dsurface,
525                             bool maximized, void *userdata)
526 {
527         /* not supported */
528 }
529
530 static void
531 desktop_minimized_requested(struct weston_desktop_surface *dsurface,
532                             void *userdata)
533 {
534         /* not supported */
535 }
536
537 static void
538 desktop_set_xwayland_position(struct weston_desktop_surface *dsurface,
539                               struct weston_coord_global pos, void *userdata)
540 {
541         struct ivi_surface *ivisurf =
542                 weston_desktop_surface_get_user_data(dsurface);
543
544         ivisurf->xwayland.x = pos.c.x;
545         ivisurf->xwayland.y = pos.c.y;
546         ivisurf->xwayland.is_set = true;
547 }
548
549 static const struct weston_desktop_api desktop_api = {
550         .struct_size = sizeof desktop_api,
551         .ping_timeout = desktop_ping_timeout,
552         .pong = desktop_pong,
553         .surface_added = desktop_surface_added,
554         .surface_removed = desktop_surface_removed,
555         .committed = desktop_committed,
556         .show_window_menu = desktop_show_window_menu,
557         .set_parent = desktop_set_parent,
558         .move = desktop_move,
559         .resize = desktop_resize,
560         .fullscreen_requested = desktop_fullscreen_requested,
561         .maximized_requested = desktop_maximized_requested,
562         .minimized_requested = desktop_minimized_requested,
563         .set_xwayland_position = desktop_set_xwayland_position,
564 };
565
566 static void
567 ivi_shell_destroy(struct wl_listener *listener, void *data)
568 {
569         struct ivi_compositor *ivi = container_of(listener,
570                                 struct ivi_compositor, destroy_listener);
571
572         ivi_shell_finalize(ivi);
573         ivi_compositor_destroy_pending_surfaces(ivi);
574         ivi_layout_destroy_saved_outputs(ivi);
575
576         weston_desktop_destroy(ivi->desktop);
577         wl_list_remove(&ivi->transform_listener.link);
578         wl_list_remove(&listener->link);
579 }
580
581 static void
582 transform_handler(struct wl_listener *listener, void *data)
583 {
584 #ifdef BUILD_XWAYLAND
585         struct weston_surface *surface = data;
586         struct ivi_surface *ivisurf = get_ivi_shell_surface(surface);
587         const struct weston_xwayland_surface_api *api;
588         int x, y;
589
590         if (!ivisurf)
591                 return;
592
593         api = ivisurf->ivi->xwayland_surface_api;
594         if (!api) {
595                 api = weston_xwayland_surface_get_api(ivisurf->ivi->compositor);
596                 ivisurf->ivi->xwayland_surface_api = api;
597         }
598
599         if (!api || !api->is_xwayland_surface(surface))
600                 return;
601
602         if (!weston_view_is_mapped(ivisurf->view))
603                 return;
604
605         x = ivisurf->view->geometry.x;
606         y = ivisurf->view->geometry.y;
607
608         api->send_position(surface, x, y);
609 #endif
610 }
611
612 bool
613 is_shell_surface_xwayland(struct ivi_surface *surf)
614 {
615 #ifdef BUILD_XWAYLAND
616         const struct weston_xwayland_surface_api *api;
617         struct ivi_compositor *ivi = surf->ivi;
618         struct weston_surface *surface;
619
620         api = ivi->xwayland_surface_api;
621
622         if (!api)
623                 return false;
624
625         surface = weston_desktop_surface_get_surface(surf->dsurface);
626         return api->is_xwayland_surface(surface);
627 #else
628         return false;
629 #endif
630 }
631
632 int
633 ivi_desktop_init(struct ivi_compositor *ivi)
634 {
635         ivi->desktop = weston_desktop_create(ivi->compositor, &desktop_api, ivi);
636         if (!ivi->desktop) {
637                 weston_log("Failed to create desktop globals");
638                 return -1;
639         }
640
641         if (!weston_compositor_add_destroy_listener_once(ivi->compositor,
642                         &ivi->destroy_listener, ivi_shell_destroy)) {
643                 return -1;
644         }
645
646         ivi->transform_listener.notify = transform_handler;
647         wl_signal_add(&ivi->compositor->transform_signal,
648                       &ivi->transform_listener);
649
650
651         return 0;
652 }