layout: Remove the quirks as now we have the policy hooks in place
[src/agl-compositor.git] / src / layout.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 "ivi-compositor.h"
27 #include "policy.h"
28
29 #include <assert.h>
30 #include <string.h>
31
32 #include <libweston/libweston.h>
33 #include <libweston-desktop/libweston-desktop.h>
34
35 #include "agl-shell-desktop-server-protocol.h"
36
37 #define AGL_COMP_DEBUG
38
39 static void
40 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
41 {
42         struct weston_output *woutput = output->output;
43         struct ivi_surface *bg = output->background;
44         struct weston_view *view;
45
46         if (!bg) {
47                 weston_log("WARNING: Output does not have a background\n");
48                 return;
49         }
50
51         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
52
53         view = bg->view;
54
55         weston_view_set_output(view, woutput);
56         weston_view_set_position(view, woutput->x, woutput->y);
57
58 #ifdef AGL_COMP_DEBUG
59         weston_log("(background) position view %p, x %d, y %d\n", view,
60                         woutput->x, woutput->y);
61 #endif
62
63         view->is_mapped = true;
64         view->surface->is_mapped = true;
65
66         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
67 }
68
69 static void
70 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
71                struct ivi_surface *panel)
72 {
73         struct weston_output *woutput = output->output;
74         struct weston_desktop_surface *dsurface;
75         struct weston_view *view;
76         struct weston_geometry geom;
77         int x = woutput->x;
78         int y = woutput->y;
79
80         if (!panel)
81                 return;
82
83         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
84         dsurface = panel->dsurface;
85         view = panel->view;
86         geom = weston_desktop_surface_get_geometry(dsurface);
87 #ifdef AGL_COMP_DEBUG
88         weston_log("geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
89                         geom.width, geom.height, geom.x, geom.y);
90 #endif
91         switch (panel->panel.edge) {
92         case AGL_SHELL_EDGE_TOP:
93                 output->area.y += geom.height;
94                 output->area.height -= geom.height;
95                 break;
96         case AGL_SHELL_EDGE_BOTTOM:
97                 y += woutput->height - geom.height;
98                 output->area.height -= geom.height;
99                 break;
100         case AGL_SHELL_EDGE_LEFT:
101                 output->area.x += geom.width;
102                 output->area.width -= geom.width;
103                 break;
104         case AGL_SHELL_EDGE_RIGHT:
105                 x += woutput->width - geom.width;
106                 output->area.width -= geom.width;
107                 break;
108         }
109
110         x -= geom.x;
111         y -= geom.y;
112
113         weston_view_set_output(view, woutput);
114         weston_view_set_position(view, x, y);
115 #ifdef AGL_COMP_DEBUG
116         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
117                         panel->panel.edge, view, x, y);
118 #endif
119
120         /* this is necessary for cases we already mapped it desktop_committed()
121          * but we not running the older qtwayland, so we still have a chance
122          * for this to run at the next test */
123         if (view->surface->is_mapped) {
124                 weston_layer_entry_remove(&view->layer_link);
125
126                 view->is_mapped = false;
127                 view->surface->is_mapped = false;
128         }
129
130         /* give ivi_layout_panel_committed() a chance to map the view/surface
131          * instead */
132         if ((geom.width == geom.height && geom.width == 0) &&
133             (geom.x == geom.y && geom.x == 0) &&
134             panel->panel.edge != AGL_SHELL_EDGE_TOP)
135                 return;
136
137         view->is_mapped = true;
138         view->surface->is_mapped = true;
139 #ifdef AGL_COMP_DEBUG
140         weston_log("panel type %d inited\n", panel->panel.edge);
141 #endif
142         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
143 }
144
145 /*
146  * Initializes all static parts of the layout, i.e. the background and panels.
147  */
148 void
149 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
150 {
151         ivi_background_init(ivi, output);
152
153         output->area.x = 0;
154         output->area.y = 0;
155         output->area.width = output->output->width;
156         output->area.height = output->output->height;
157
158         ivi_panel_init(ivi, output, output->top);
159         ivi_panel_init(ivi, output, output->bottom);
160         ivi_panel_init(ivi, output, output->left);
161         ivi_panel_init(ivi, output, output->right);
162
163         weston_compositor_schedule_repaint(ivi->compositor);
164
165         weston_log("Usable area: %dx%d+%d,%d\n",
166                    output->area.width, output->area.height,
167                    output->area.x, output->area.y);
168 }
169
170 struct ivi_surface *
171 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
172 {
173         struct ivi_surface *surf;
174         const char *id;
175
176         wl_list_for_each(surf, &ivi->surfaces, link) {
177                 id = weston_desktop_surface_get_app_id(surf->dsurface);
178                 if (id && strcmp(app_id, id) == 0)
179                         return surf;
180         }
181
182         return NULL;
183 }
184
185 static void
186 ivi_layout_activate_complete(struct ivi_output *output,
187                              struct ivi_surface *surf)
188 {
189         struct ivi_compositor *ivi = output->ivi;
190         struct weston_output *woutput = output->output;
191         struct weston_view *view = surf->view;
192
193         if (weston_view_is_mapped(view)) {
194                 weston_layer_entry_remove(&view->layer_link);
195         }
196
197         weston_view_set_output(view, woutput);
198         weston_view_set_position(view,
199                                  woutput->x + output->area.x,
200                                  woutput->y + output->area.y);
201
202         view->is_mapped = true;
203         view->surface->is_mapped = true;
204
205         if (output->active) {
206                 output->active->view->is_mapped = false;
207                 output->active->view->surface->is_mapped = false;
208
209                 weston_layer_entry_remove(&output->active->view->layer_link);
210         }
211         output->previous_active = output->active;
212         output->active = surf;
213
214         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
215         weston_view_update_transform(view);
216
217         /* force repaint of the entire output */
218         weston_output_damage(output->output);
219         surf->desktop.last_output = surf->desktop.pending_output;
220         surf->desktop.pending_output = NULL;
221 }
222
223 static struct ivi_output *
224 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
225 {
226         struct ivi_output *out;
227
228         wl_list_for_each(out, &ivi->outputs, link) {
229                 if (out->background &&
230                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
231                         return out;
232         }
233
234         return NULL;
235 }
236
237 void
238 ivi_layout_desktop_committed(struct ivi_surface *surf)
239 {
240         struct weston_desktop_surface *dsurf = surf->dsurface;
241         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
242         struct ivi_output *output;
243
244         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
245
246         output = surf->desktop.pending_output;
247         if (!output) {
248                 struct ivi_output *ivi_bg_output;
249                 struct ivi_policy *policy = surf->ivi->policy;
250
251                 if (policy && policy->api.surface_activate_by_default &&
252                     !policy->api.surface_activate_by_default(surf, surf->ivi))
253                         return;
254
255                 /* we can only activate it again by using the protocol */
256                 if (surf->activated_by_default)
257                         return;
258
259                 ivi_bg_output = ivi_layout_find_bg_output(surf->ivi);
260
261                 /* use the output of the bg to activate the app on start-up by
262                  * default */
263                 if (surf->view && ivi_bg_output) {
264                         const char *app_id =
265                                 weston_desktop_surface_get_app_id(dsurf);
266                         if (app_id && ivi_bg_output) {
267                                 ivi_layout_activate(ivi_bg_output, app_id);
268                                 surf->activated_by_default = true;
269                         }
270                 }
271
272                 return;
273         }
274
275         if (!weston_desktop_surface_get_maximized(dsurf) ||
276             geom.width != output->area.width ||
277             geom.height != output->area.height)
278                 return;
279
280         ivi_layout_activate_complete(output, surf);
281 }
282
283 void
284 ivi_layout_fullscreen_committed(struct ivi_surface *surface)
285 {
286         struct ivi_compositor *ivi = surface->ivi;
287
288         struct weston_desktop_surface *dsurface = surface->dsurface;
289         struct weston_surface *wsurface =
290                 weston_desktop_surface_get_surface(dsurface);
291
292         struct ivi_output *output = surface->split.output;
293         struct weston_output *woutput = output->output;
294
295         struct weston_view *view = surface->view;
296         struct weston_geometry geom;
297
298         if (surface->view->is_mapped)
299                 return;
300
301         geom = weston_desktop_surface_get_geometry(dsurface);
302         weston_log("(fs) geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
303                         geom.width, geom.height);
304
305         assert(surface->role == IVI_SURFACE_ROLE_FULLSCREEN);
306
307         weston_desktop_surface_set_fullscreen(dsurface, true);
308
309         weston_view_set_output(view, woutput);
310         weston_view_set_position(view, woutput->x, woutput->y);
311         weston_layer_entry_insert(&ivi->fullscreen.view_list, &view->layer_link);
312
313         weston_view_update_transform(view);
314         weston_view_damage_below(view);
315
316         wsurface->is_mapped = true;
317         surface->view->is_mapped = true;
318 }
319
320 void
321 ivi_layout_desktop_resize(struct ivi_surface *surface,
322                           struct weston_geometry area)
323 {
324         struct weston_desktop_surface *dsurf = surface->dsurface;
325         struct weston_view *view = surface->view;
326
327         int x = area.x;
328         int y = area.y;
329         int width = area.width;
330         int height = area.height;
331
332         weston_desktop_surface_set_size(dsurf,
333                                         width, height);
334
335         weston_view_set_position(view, x, y);
336         weston_view_update_transform(view);
337         weston_view_damage_below(view);
338 }
339
340 void
341 ivi_layout_split_committed(struct ivi_surface *surface)
342 {
343         struct ivi_compositor *ivi = surface->ivi;
344
345         struct weston_desktop_surface *dsurface = surface->dsurface;
346         struct weston_surface *wsurface =
347                 weston_desktop_surface_get_surface(dsurface);
348
349         struct ivi_output *output = surface->split.output;
350         struct weston_output *woutput = output->output;
351
352         struct weston_view *view = surface->view;
353         struct weston_geometry geom;
354
355         int x, y;
356         int width, height;
357
358         x = woutput->x;
359         y = woutput->y;
360
361         if (surface->view->is_mapped)
362                 return;
363
364         geom = weston_desktop_surface_get_geometry(dsurface);
365
366         assert(surface->role == IVI_SURFACE_ROLE_SPLIT_H ||
367                surface->role == IVI_SURFACE_ROLE_SPLIT_V);
368
369         /* save the previous area in order to recover it back when if this kind
370          * of surface is being destroyed/removed */
371         output->area_saved = output->area;
372
373         switch (surface->role) {
374         case IVI_SURFACE_ROLE_SPLIT_V:
375                 if (geom.width == woutput->width &&
376                     geom.height == woutput->height)
377                         geom.width = (output->area.width / 2);
378
379                 x += woutput->width - geom.width;
380                 output->area.width -= geom.width;
381
382                 width = woutput->width - x;
383                 height = output->area.height;
384                 y = output->area.y;
385
386                 break;
387         case IVI_SURFACE_ROLE_SPLIT_H:
388                 if (geom.width == woutput->width &&
389                     geom.height == woutput->height)
390                         geom.height = (output->area.height / 2);
391
392                 y = output->area.y;
393                 output->area.y += geom.height;
394                 output->area.height -= geom.height;
395
396                 width = output->area.width;
397                 height = output->area.height;
398
399                 x = output->area.x;
400
401                 break;
402         default:
403                 assert(!"Invalid split orientation\n");
404         }
405
406         weston_desktop_surface_set_size(dsurface,
407                                         width, height);
408
409         /* resize the active surface first, output->area already contains
410          * correct area to resize to */
411         if (output->active)
412                 ivi_layout_desktop_resize(output->active, output->area);
413
414         weston_view_set_output(view, woutput);
415         weston_view_set_position(view, x, y);
416         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
417
418         weston_view_update_transform(view);
419         weston_view_damage_below(view);
420
421         wsurface->is_mapped = true;
422         surface->view->is_mapped = true;
423 }
424
425 void
426 ivi_layout_popup_committed(struct ivi_surface *surface)
427 {
428         struct ivi_compositor *ivi = surface->ivi;
429
430         struct weston_desktop_surface *dsurface = surface->dsurface;
431         struct weston_surface *wsurface =
432                 weston_desktop_surface_get_surface(dsurface);
433
434         struct ivi_output *output = surface->popup.output;
435         struct weston_output *woutput = output->output;
436
437         struct weston_view *view = surface->view;
438         struct weston_geometry geom;
439
440         if (surface->view->is_mapped)
441                 return;
442
443         geom = weston_desktop_surface_get_geometry(dsurface);
444         weston_log("geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
445                         geom.width, geom.height);
446
447         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
448
449         weston_view_set_output(view, woutput);
450         if (surface->popup.x || surface->popup.y)
451                 weston_view_set_position(view, surface->popup.x, surface->popup.y);
452         else
453                 weston_view_set_position(view, geom.x, geom.y);
454         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
455
456         weston_view_update_transform(view);
457         weston_view_damage_below(view);
458
459         wsurface->is_mapped = true;
460         surface->view->is_mapped = true;
461 }
462
463 static void
464 ivi_layout_popup_re_add(struct ivi_surface *surface)
465 {
466         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
467         struct weston_view *view = surface->view;
468
469         if (weston_view_is_mapped(view)) {
470                 struct weston_desktop_surface *dsurface = surface->dsurface;
471                 struct weston_surface *wsurface =
472                         weston_desktop_surface_get_surface(dsurface);
473
474                 weston_layer_entry_remove(&view->layer_link);
475
476                 wsurface->is_mapped = false;
477                 view->is_mapped = false;
478         }
479
480         ivi_layout_popup_committed(surface);
481 }
482
483 void
484 ivi_layout_panel_committed(struct ivi_surface *surface)
485 {
486         struct ivi_compositor *ivi = surface->ivi;
487         struct ivi_output *output = surface->bg.output;
488         struct weston_output *woutput = output->output;
489         struct weston_desktop_surface *dsurface = surface->dsurface;
490         struct weston_surface *wsurface =
491                 weston_desktop_surface_get_surface(dsurface);
492         struct weston_geometry geom;
493         int x = woutput->x;
494         int y = woutput->y;
495
496         assert(surface->role == IVI_SURFACE_ROLE_PANEL);
497
498         /*
499          * If the desktop_surface geometry is not set and the panel is not a
500          * top one, we'll give this a chance to run, as some qtwayland version
501          * seem to have a 'problem', where the panel initilization part will
502          * have a desktop surface with 0 as geometry for *all* its members
503          * (width/height). Doing that will result in the panel not being
504          * displayed at all.
505          *
506          * Later versions of qtwayland do have the correct window geometry for
507          * the desktop surface so the weston_surface is already mapped in
508          * ivi_panel_init().
509          */
510         if (wsurface->is_mapped)
511                 return;
512
513         geom = weston_desktop_surface_get_geometry(dsurface);
514
515 #ifdef AGL_COMP_DEBUG
516         weston_log("geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
517                         geom.width, geom.height, geom.x, geom.y);
518 #endif
519
520         switch (surface->panel.edge) {
521         case AGL_SHELL_EDGE_TOP:
522                 /* Do nothing */
523                 break;
524         case AGL_SHELL_EDGE_BOTTOM:
525                 y += woutput->height - geom.height;
526                 break;
527         case AGL_SHELL_EDGE_LEFT:
528                 /* Do nothing */
529                 break;
530         case AGL_SHELL_EDGE_RIGHT:
531                 x += woutput->width - geom.width;
532                 break;
533         }
534 #ifndef AGL_COMP_DEBUG
535         weston_log("panel type %d commited\n", surface->panel.edge);
536 #endif
537
538         weston_view_set_output(surface->view, woutput);
539         weston_view_set_position(surface->view, x, y);
540         weston_layer_entry_insert(&ivi->panel.view_list,
541                                   &surface->view->layer_link);
542
543         weston_view_update_transform(surface->view);
544         weston_view_schedule_repaint(surface->view);
545
546         wsurface->is_mapped = true;
547         surface->view->is_mapped = true;
548 }
549
550 static bool
551 ivi_layout_surface_is_split_or_fullscreen(struct ivi_surface *surf)
552 {
553         struct ivi_compositor *ivi = surf->ivi;
554         struct ivi_surface *is;
555
556         if (surf->role != IVI_SURFACE_ROLE_SPLIT_H &&
557             surf->role != IVI_SURFACE_ROLE_SPLIT_V &&
558             surf->role != IVI_SURFACE_ROLE_FULLSCREEN)
559                 return false;
560
561         wl_list_for_each(is, &ivi->surfaces, link)
562                 if (is == surf)
563                         return true;
564
565         return false;
566 }
567
568 void
569 ivi_layout_activate(struct ivi_output *output, const char *app_id)
570 {
571         struct ivi_compositor *ivi = output->ivi;
572         struct ivi_surface *surf;
573         struct weston_desktop_surface *dsurf;
574         struct weston_view *view;
575         struct weston_geometry geom;
576         struct ivi_policy *policy = output->ivi->policy;
577
578         surf = ivi_find_app(ivi, app_id);
579         if (!surf)
580                 return;
581
582         if (policy && policy->api.surface_activate &&
583             !policy->api.surface_activate(surf, surf->ivi)) {
584                 return;
585         }
586
587 #ifdef AGL_COMP_DEBUG
588         weston_log("Found app_id %s\n", app_id);
589 #endif
590
591         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
592                 ivi_layout_popup_re_add(surf);
593                 return;
594         }
595
596         /* do not 're'-activate surfaces that are split or active */
597         if (surf == output->active ||
598             ivi_layout_surface_is_split_or_fullscreen(surf))
599                 return;
600
601
602         dsurf = surf->dsurface;
603         view = surf->view;
604         geom = weston_desktop_surface_get_geometry(dsurf);
605
606         surf->desktop.pending_output = output;
607         if (weston_desktop_surface_get_maximized(dsurf) &&
608             geom.width == output->area.width &&
609             geom.height == output->area.height) {
610                 ivi_layout_activate_complete(output, surf);
611                 return;
612         }
613
614         weston_desktop_surface_set_maximized(dsurf, true);
615         weston_desktop_surface_set_size(dsurf,
616                                         output->area.width,
617                                         output->area.height);
618
619         /*
620          * If the view isn't mapped, we put it onto the hidden layer so it will
621          * start receiving frame events, and will be able to act on our
622          * configure event.
623          */
624         if (!weston_view_is_mapped(view)) {
625                 view->is_mapped = true;
626                 view->surface->is_mapped = true;
627
628                 weston_view_set_output(view, output->output);
629                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
630                 /* force repaint of the entire output */
631                 weston_output_damage(output->output);
632         }
633 }
634
635 struct ivi_output *
636 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
637 {
638         struct ivi_output *ivi_output = NULL;
639
640         switch (surf->role) {
641         case IVI_SURFACE_ROLE_DESKTOP:
642                 if (surf->desktop.pending_output)
643                         ivi_output = surf->desktop.pending_output;
644                 else
645                         ivi_output = surf->desktop.last_output;
646                 break;
647         case IVI_SURFACE_ROLE_POPUP:
648                 ivi_output = surf->popup.output;
649                 break;
650         case IVI_SURFACE_ROLE_BACKGROUND:
651                 ivi_output = surf->bg.output;
652                 break;
653         case IVI_SURFACE_ROLE_PANEL:
654                 ivi_output = surf->panel.output;
655                 break;
656         case IVI_SURFACE_ROLE_FULLSCREEN:
657                 ivi_output = surf->fullscreen.output;
658                 break;
659         case IVI_SURFACE_ROLE_SPLIT_H:
660         case IVI_SURFACE_ROLE_SPLIT_V:
661                 ivi_output = surf->split.output;
662                 break;
663         case IVI_SURFACE_ROLE_NONE:
664         default:
665                 break;
666         }
667
668         return ivi_output;
669 }
670
671 void
672 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
673 {
674         struct ivi_surface *surf;
675         struct ivi_output *ivi_output;
676         struct ivi_policy *policy = ivi->policy;
677
678         surf = ivi_find_app(ivi, app_id);
679         if (!surf)
680                 return;
681
682         if (policy && policy->api.surface_deactivate &&
683             !policy->api.surface_deactivate(surf, surf->ivi)) {
684                 return;
685         }
686
687         ivi_output = ivi_layout_get_output_from_surface(surf);
688         weston_log("deactiving %s\n", app_id);
689
690         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
691                 struct ivi_surface *previous_active;
692
693                 previous_active = ivi_output->previous_active;
694                 if (!previous_active) {
695                         /* we don't have a previous active it means we should
696                          * display the bg */
697                         if (ivi_output->active) {
698                                 struct weston_view *view;
699
700                                 view = ivi_output->active->view;
701                                 view->is_mapped = false;
702                                 view->surface->is_mapped = false;
703
704                                 weston_layer_entry_remove(&view->layer_link);
705                                 weston_output_damage(ivi_output->output);
706                         }
707                 } else {
708                         struct weston_desktop_surface *dsurface;
709                         const char *previous_active_app_id;
710
711                         dsurface = previous_active->dsurface;
712                         previous_active_app_id =
713                                 weston_desktop_surface_get_app_id(dsurface);
714                         ivi_layout_activate(ivi_output, previous_active_app_id);
715                 }
716         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
717                 struct weston_view *view  = surf->view;
718
719                 weston_layer_entry_remove(&view->layer_link);
720                 weston_view_damage_below(view);
721         }
722 }