policy: Init policy framework API
[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 #define AGL_COMP_DEBUG
36
37 static void
38 ivi_background_init(struct ivi_compositor *ivi, struct ivi_output *output)
39 {
40         struct weston_output *woutput = output->output;
41         struct ivi_surface *bg = output->background;
42         struct weston_view *view;
43
44         if (!bg) {
45                 weston_log("WARNING: Output does not have a background\n");
46                 return;
47         }
48
49         assert(bg->role == IVI_SURFACE_ROLE_BACKGROUND);
50
51         view = bg->view;
52
53         weston_view_set_output(view, woutput);
54         weston_view_set_position(view, woutput->x, woutput->y);
55
56 #ifdef AGL_COMP_DEBUG
57         weston_log("(background) position view %p, x %d, y %d\n", view,
58                         woutput->x, woutput->y);
59 #endif
60
61         view->is_mapped = true;
62         view->surface->is_mapped = true;
63
64         weston_layer_entry_insert(&ivi->background.view_list, &view->layer_link);
65 }
66
67 static void
68 ivi_panel_init(struct ivi_compositor *ivi, struct ivi_output *output,
69                struct ivi_surface *panel)
70 {
71         struct weston_output *woutput = output->output;
72         struct weston_desktop_surface *dsurface;
73         struct weston_view *view;
74         struct weston_geometry geom;
75         int x = woutput->x;
76         int y = woutput->y;
77
78         if (!panel)
79                 return;
80
81         assert(panel->role == IVI_SURFACE_ROLE_PANEL);
82         dsurface = panel->dsurface;
83         view = panel->view;
84         geom = weston_desktop_surface_get_geometry(dsurface);
85 #ifdef AGL_COMP_DEBUG
86         weston_log("geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
87                         geom.width, geom.height, geom.x, geom.y);
88 #endif
89         switch (panel->panel.edge) {
90         case AGL_SHELL_EDGE_TOP:
91                 output->area.y += geom.height;
92                 output->area.height -= geom.height;
93                 break;
94         case AGL_SHELL_EDGE_BOTTOM:
95                 y += woutput->height - geom.height;
96                 output->area.height -= geom.height;
97                 break;
98         case AGL_SHELL_EDGE_LEFT:
99                 output->area.x += geom.width;
100                 output->area.width -= geom.width;
101                 break;
102         case AGL_SHELL_EDGE_RIGHT:
103                 x += woutput->width - geom.width;
104                 output->area.width -= geom.width;
105                 break;
106         }
107
108         x -= geom.x;
109         y -= geom.y;
110
111         weston_view_set_output(view, woutput);
112         weston_view_set_position(view, x, y);
113 #ifdef AGL_COMP_DEBUG
114         weston_log("(panel) edge %d position view %p, x %d, y %d\n",
115                         panel->panel.edge, view, x, y);
116 #endif
117
118         /* this is necessary for cases we already mapped it desktop_committed()
119          * but we not running the older qtwayland, so we still have a chance
120          * for this to run at the next test */
121         if (view->surface->is_mapped) {
122                 weston_layer_entry_remove(&view->layer_link);
123
124                 view->is_mapped = false;
125                 view->surface->is_mapped = false;
126         }
127
128         /* give ivi_layout_panel_committed() a chance to map the view/surface
129          * instead */
130         if ((geom.width == geom.height && geom.width == 0) &&
131             (geom.x == geom.y && geom.x == 0) &&
132             panel->panel.edge != AGL_SHELL_EDGE_TOP)
133                 return;
134
135         view->is_mapped = true;
136         view->surface->is_mapped = true;
137 #ifdef AGL_COMP_DEBUG
138         weston_log("panel type %d inited\n", panel->panel.edge);
139 #endif
140         weston_layer_entry_insert(&ivi->panel.view_list, &view->layer_link);
141 }
142
143 /*
144  * Initializes all static parts of the layout, i.e. the background and panels.
145  */
146 void
147 ivi_layout_init(struct ivi_compositor *ivi, struct ivi_output *output)
148 {
149         ivi_background_init(ivi, output);
150
151         output->area.x = 0;
152         output->area.y = 0;
153         output->area.width = output->output->width;
154         output->area.height = output->output->height;
155
156         ivi_panel_init(ivi, output, output->top);
157         ivi_panel_init(ivi, output, output->bottom);
158         ivi_panel_init(ivi, output, output->left);
159         ivi_panel_init(ivi, output, output->right);
160
161         weston_compositor_schedule_repaint(ivi->compositor);
162
163         weston_log("Usable area: %dx%d+%d,%d\n",
164                    output->area.width, output->area.height,
165                    output->area.x, output->area.y);
166 }
167
168 static struct ivi_surface *
169 ivi_find_app(struct ivi_compositor *ivi, const char *app_id)
170 {
171         struct ivi_surface *surf;
172         const char *id;
173
174         wl_list_for_each(surf, &ivi->surfaces, link) {
175                 id = weston_desktop_surface_get_app_id(surf->dsurface);
176                 if (id && strcmp(app_id, id) == 0)
177                         return surf;
178         }
179
180         return NULL;
181 }
182
183 static void
184 ivi_layout_activate_complete(struct ivi_output *output,
185                              struct ivi_surface *surf)
186 {
187         struct ivi_compositor *ivi = output->ivi;
188         struct weston_output *woutput = output->output;
189         struct weston_view *view = surf->view;
190
191         if (weston_view_is_mapped(view)) {
192                 weston_layer_entry_remove(&view->layer_link);
193         }
194
195         weston_view_set_output(view, woutput);
196         weston_view_set_position(view,
197                                  woutput->x + output->area.x,
198                                  woutput->y + output->area.y);
199
200         view->is_mapped = true;
201         view->surface->is_mapped = true;
202
203         if (output->active) {
204                 output->active->view->is_mapped = false;
205                 output->active->view->surface->is_mapped = false;
206
207                 weston_layer_entry_remove(&output->active->view->layer_link);
208         }
209         output->active = surf;
210
211         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
212         weston_view_update_transform(view);
213
214         /* force repaint of the entire output */
215         weston_output_damage(output->output);
216         surf->desktop.last_output = surf->desktop.pending_output;
217         surf->desktop.pending_output = NULL;
218 }
219
220 static struct ivi_output *
221 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
222 {
223         struct ivi_output *out;
224
225         wl_list_for_each(out, &ivi->outputs, link) {
226                 if (out->background &&
227                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
228                         return out;
229         }
230
231         return NULL;
232 }
233
234 void
235 ivi_layout_desktop_committed(struct ivi_surface *surf)
236 {
237         struct weston_desktop_surface *dsurf = surf->dsurface;
238         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
239         struct ivi_output *output;
240
241         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
242
243         output = surf->desktop.pending_output;
244         if (!output) {
245                 struct ivi_output *ivi_bg_output;
246
247                 /* FIXME: This should be changed to determine if the policy
248                  * database allows that to happen */
249                 if (!surf->ivi->quirks.activate_apps_by_default)
250                         return;
251
252                 ivi_bg_output = ivi_layout_find_bg_output(surf->ivi);
253
254                 /* use the output of the bg to activate the app on start-up by
255                  * default */
256                 if (surf->view && ivi_bg_output) {
257                         const char *app_id =
258                                 weston_desktop_surface_get_app_id(dsurf);
259                         if (app_id && ivi_bg_output)
260                                 ivi_layout_activate(ivi_bg_output, app_id);
261                 }
262
263                 return;
264         }
265
266         if (!weston_desktop_surface_get_maximized(dsurf) ||
267             geom.width != output->area.width ||
268             geom.height != output->area.height)
269                 return;
270
271         ivi_layout_activate_complete(output, surf);
272 }
273
274 void
275 ivi_layout_panel_committed(struct ivi_surface *surface)
276 {
277         struct ivi_compositor *ivi = surface->ivi;
278         struct ivi_output *output = surface->bg.output;
279         struct weston_output *woutput = output->output;
280         struct weston_desktop_surface *dsurface = surface->dsurface;
281         struct weston_surface *wsurface =
282                 weston_desktop_surface_get_surface(dsurface);
283         struct weston_geometry geom;
284         int x = woutput->x;
285         int y = woutput->y;
286
287         assert(surface->role == IVI_SURFACE_ROLE_PANEL);
288
289         /*
290          * If the desktop_surface geometry is not set and the panel is not a
291          * top one, we'll give this a chance to run, as some qtwayland version
292          * seem to have a 'problem', where the panel initilization part will
293          * have a desktop surface with 0 as geometry for *all* its members
294          * (width/height). Doing that will result in the panel not being
295          * displayed at all.
296          *
297          * Later versions of qtwayland do have the correct window geometry for
298          * the desktop surface so the weston_surface is already mapped in
299          * ivi_panel_init().
300          */
301         if (wsurface->is_mapped)
302                 return;
303
304         geom = weston_desktop_surface_get_geometry(dsurface);
305
306 #ifdef AGL_COMP_DEBUG
307         weston_log("geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
308                         geom.width, geom.height, geom.x, geom.y);
309 #endif
310
311         switch (surface->panel.edge) {
312         case AGL_SHELL_EDGE_TOP:
313                 /* Do nothing */
314                 break;
315         case AGL_SHELL_EDGE_BOTTOM:
316                 y += woutput->height - geom.height;
317                 break;
318         case AGL_SHELL_EDGE_LEFT:
319                 /* Do nothing */
320                 break;
321         case AGL_SHELL_EDGE_RIGHT:
322                 x += woutput->width - geom.width;
323                 break;
324         }
325 #ifndef AGL_COMP_DEBUG
326         weston_log("panel type %d commited\n", surface->panel.edge);
327 #endif
328
329         weston_view_set_output(surface->view, woutput);
330         weston_view_set_position(surface->view, x, y);
331         weston_layer_entry_insert(&ivi->panel.view_list,
332                                   &surface->view->layer_link);
333
334         weston_view_update_transform(surface->view);
335         weston_view_schedule_repaint(surface->view);
336
337         wsurface->is_mapped = true;
338         surface->view->is_mapped = true;
339 }
340
341 void
342 ivi_layout_activate(struct ivi_output *output, const char *app_id)
343 {
344         struct ivi_compositor *ivi = output->ivi;
345         struct ivi_surface *surf;
346         struct weston_desktop_surface *dsurf;
347         struct weston_view *view;
348         struct weston_geometry geom;
349         struct ivi_policy *policy = output->ivi->policy;
350
351         surf = ivi_find_app(ivi, app_id);
352         if (!surf)
353                 return;
354
355         if (policy && policy->api.surface_activate &&
356             !policy->api.surface_activate(surf, surf->ivi)) {
357                 return;
358         }
359
360 #ifdef AGL_COMP_DEBUG
361         weston_log("Found app_id %s\n", app_id);
362 #endif
363         if (surf == output->active)
364                 return;
365
366         dsurf = surf->dsurface;
367         view = surf->view;
368         geom = weston_desktop_surface_get_geometry(dsurf);
369
370         if (weston_desktop_surface_get_maximized(dsurf) &&
371             geom.width == output->area.width &&
372             geom.height == output->area.height) {
373                 ivi_layout_activate_complete(output, surf);
374                 return;
375         }
376
377         weston_desktop_surface_set_maximized(dsurf, true);
378         weston_desktop_surface_set_size(dsurf,
379                                         output->area.width,
380                                         output->area.height);
381
382         /*
383          * If the view isn't mapped, we put it onto the hidden layer so it will
384          * start receiving frame events, and will be able to act on our
385          * configure event.
386          */
387         if (!weston_view_is_mapped(view)) {
388                 view->is_mapped = true;
389                 view->surface->is_mapped = true;
390
391                 weston_view_set_output(view, output->output);
392                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
393                 /* force repaint of the entire output */
394                 weston_output_damage(output->output);
395         }
396
397         surf->desktop.pending_output = output;
398 }