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