agl-shell-desktop: Add the ability to hide client windows
[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->previous_active = output->active;
210         output->active = surf;
211
212         weston_layer_entry_insert(&ivi->normal.view_list, &view->layer_link);
213         weston_view_update_transform(view);
214
215         /* force repaint of the entire output */
216         weston_output_damage(output->output);
217         surf->desktop.last_output = surf->desktop.pending_output;
218         surf->desktop.pending_output = NULL;
219 }
220
221 static struct ivi_output *
222 ivi_layout_find_bg_output(struct ivi_compositor *ivi)
223 {
224         struct ivi_output *out;
225
226         wl_list_for_each(out, &ivi->outputs, link) {
227                 if (out->background &&
228                     out->background->role == IVI_SURFACE_ROLE_BACKGROUND)
229                         return out;
230         }
231
232         return NULL;
233 }
234
235 void
236 ivi_layout_desktop_committed(struct ivi_surface *surf)
237 {
238         struct weston_desktop_surface *dsurf = surf->dsurface;
239         struct weston_geometry geom = weston_desktop_surface_get_geometry(dsurf);
240         struct ivi_output *output;
241
242         assert(surf->role == IVI_SURFACE_ROLE_DESKTOP);
243
244         output = surf->desktop.pending_output;
245         if (!output) {
246                 struct ivi_output *ivi_bg_output;
247                 struct ivi_policy *policy = surf->ivi->policy;
248
249                 if (policy && policy->api.surface_activate_by_default)
250                         if (policy->api.surface_activate_by_default(surf, surf->ivi))
251                                 goto skip_config_check;
252
253                 if (!surf->ivi->quirks.activate_apps_by_default)
254                         return;
255
256 skip_config_check:
257                 /* we can only activate it again by using the protocol */
258                 if (surf->activated_by_default)
259                         return;
260
261                 ivi_bg_output = ivi_layout_find_bg_output(surf->ivi);
262
263                 /* use the output of the bg to activate the app on start-up by
264                  * default */
265                 if (surf->view && ivi_bg_output) {
266                         const char *app_id =
267                                 weston_desktop_surface_get_app_id(dsurf);
268                         if (app_id && ivi_bg_output) {
269                                 ivi_layout_activate(ivi_bg_output, app_id);
270                                 surf->activated_by_default = true;
271                         }
272                 }
273
274                 return;
275         }
276
277         if (!weston_desktop_surface_get_maximized(dsurf) ||
278             geom.width != output->area.width ||
279             geom.height != output->area.height)
280                 return;
281
282         ivi_layout_activate_complete(output, surf);
283 }
284
285 void
286 ivi_layout_popup_committed(struct ivi_surface *surface)
287 {
288         struct ivi_compositor *ivi = surface->ivi;
289
290         struct weston_desktop_surface *dsurface = surface->dsurface;
291         struct weston_surface *wsurface =
292                 weston_desktop_surface_get_surface(dsurface);
293
294         struct ivi_output *output = surface->popup.output;
295         struct weston_output *woutput = output->output;
296
297         struct weston_view *view = surface->view;
298         struct weston_geometry geom;
299
300         if (surface->view->is_mapped)
301                 return;
302
303         geom = weston_desktop_surface_get_geometry(dsurface);
304         weston_log("geom x %d, y %d, width %d, height %d\n", geom.x, geom.y,
305                         geom.width, geom.height);
306
307         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
308
309         weston_view_set_output(view, woutput);
310         if (surface->popup.x || surface->popup.y)
311                 weston_view_set_position(view, surface->popup.x, surface->popup.y);
312         else
313                 weston_view_set_position(view, geom.x, geom.y);
314         weston_layer_entry_insert(&ivi->popup.view_list, &view->layer_link);
315
316         weston_view_update_transform(view);
317         weston_view_damage_below(view);
318
319         wsurface->is_mapped = true;
320         surface->view->is_mapped = true;
321 }
322
323 static void
324 ivi_layout_popup_re_add(struct ivi_surface *surface)
325 {
326         assert(surface->role == IVI_SURFACE_ROLE_POPUP);
327         struct weston_view *view = surface->view;
328         struct ivi_compositor *ivi = surface->ivi;
329
330         if (weston_view_is_mapped(view)) {
331                 struct weston_desktop_surface *dsurface = surface->dsurface;
332                 struct weston_surface *wsurface =
333                         weston_desktop_surface_get_surface(dsurface);
334
335                 weston_layer_entry_remove(&view->layer_link);
336
337                 wsurface->is_mapped = false;
338                 view->is_mapped = false;
339         }
340
341         ivi_layout_popup_committed(surface);
342 }
343
344 void
345 ivi_layout_panel_committed(struct ivi_surface *surface)
346 {
347         struct ivi_compositor *ivi = surface->ivi;
348         struct ivi_output *output = surface->bg.output;
349         struct weston_output *woutput = output->output;
350         struct weston_desktop_surface *dsurface = surface->dsurface;
351         struct weston_surface *wsurface =
352                 weston_desktop_surface_get_surface(dsurface);
353         struct weston_geometry geom;
354         int x = woutput->x;
355         int y = woutput->y;
356
357         assert(surface->role == IVI_SURFACE_ROLE_PANEL);
358
359         /*
360          * If the desktop_surface geometry is not set and the panel is not a
361          * top one, we'll give this a chance to run, as some qtwayland version
362          * seem to have a 'problem', where the panel initilization part will
363          * have a desktop surface with 0 as geometry for *all* its members
364          * (width/height). Doing that will result in the panel not being
365          * displayed at all.
366          *
367          * Later versions of qtwayland do have the correct window geometry for
368          * the desktop surface so the weston_surface is already mapped in
369          * ivi_panel_init().
370          */
371         if (wsurface->is_mapped)
372                 return;
373
374         geom = weston_desktop_surface_get_geometry(dsurface);
375
376 #ifdef AGL_COMP_DEBUG
377         weston_log("geom.width %d, geom.height %d, geom.x %d, geom.y %d\n",
378                         geom.width, geom.height, geom.x, geom.y);
379 #endif
380
381         switch (surface->panel.edge) {
382         case AGL_SHELL_EDGE_TOP:
383                 /* Do nothing */
384                 break;
385         case AGL_SHELL_EDGE_BOTTOM:
386                 y += woutput->height - geom.height;
387                 break;
388         case AGL_SHELL_EDGE_LEFT:
389                 /* Do nothing */
390                 break;
391         case AGL_SHELL_EDGE_RIGHT:
392                 x += woutput->width - geom.width;
393                 break;
394         }
395 #ifndef AGL_COMP_DEBUG
396         weston_log("panel type %d commited\n", surface->panel.edge);
397 #endif
398
399         weston_view_set_output(surface->view, woutput);
400         weston_view_set_position(surface->view, x, y);
401         weston_layer_entry_insert(&ivi->panel.view_list,
402                                   &surface->view->layer_link);
403
404         weston_view_update_transform(surface->view);
405         weston_view_schedule_repaint(surface->view);
406
407         wsurface->is_mapped = true;
408         surface->view->is_mapped = true;
409 }
410
411 void
412 ivi_layout_activate(struct ivi_output *output, const char *app_id)
413 {
414         struct ivi_compositor *ivi = output->ivi;
415         struct ivi_surface *surf;
416         struct weston_desktop_surface *dsurf;
417         struct weston_view *view;
418         struct weston_geometry geom;
419         struct ivi_policy *policy = output->ivi->policy;
420
421         surf = ivi_find_app(ivi, app_id);
422         if (!surf)
423                 return;
424
425         if (policy && policy->api.surface_activate &&
426             !policy->api.surface_activate(surf, surf->ivi)) {
427                 return;
428         }
429
430 #ifdef AGL_COMP_DEBUG
431         weston_log("Found app_id %s\n", app_id);
432 #endif
433
434         if (surf->role == IVI_SURFACE_ROLE_POPUP) {
435                 ivi_layout_popup_re_add(surf);
436                 return;
437         }
438
439         if (surf == output->active)
440                 return;
441
442         dsurf = surf->dsurface;
443         view = surf->view;
444         geom = weston_desktop_surface_get_geometry(dsurf);
445
446         surf->desktop.pending_output = output;
447         if (weston_desktop_surface_get_maximized(dsurf) &&
448             geom.width == output->area.width &&
449             geom.height == output->area.height) {
450                 ivi_layout_activate_complete(output, surf);
451                 return;
452         }
453
454         weston_desktop_surface_set_maximized(dsurf, true);
455         weston_desktop_surface_set_size(dsurf,
456                                         output->area.width,
457                                         output->area.height);
458
459         /*
460          * If the view isn't mapped, we put it onto the hidden layer so it will
461          * start receiving frame events, and will be able to act on our
462          * configure event.
463          */
464         if (!weston_view_is_mapped(view)) {
465                 view->is_mapped = true;
466                 view->surface->is_mapped = true;
467
468                 weston_view_set_output(view, output->output);
469                 weston_layer_entry_insert(&ivi->hidden.view_list, &view->layer_link);
470                 /* force repaint of the entire output */
471                 weston_output_damage(output->output);
472         }
473
474 }
475
476 static struct ivi_output *
477 ivi_layout_get_output_from_surface(struct ivi_surface *surf)
478 {
479         struct ivi_output *ivi_output = NULL;
480
481         switch (surf->role) {
482         case IVI_SURFACE_ROLE_DESKTOP:
483                 if (surf->desktop.pending_output)
484                         ivi_output = surf->desktop.pending_output;
485                 else
486                         ivi_output = surf->desktop.last_output;
487                 break;
488         case IVI_SURFACE_ROLE_POPUP:
489                 ivi_output = surf->popup.output;
490                 break;
491         default:
492         case IVI_SURFACE_ROLE_BACKGROUND:
493         case IVI_SURFACE_ROLE_PANEL:
494         case IVI_SURFACE_ROLE_NONE:
495                 break;
496         }
497
498         return ivi_output;
499 }
500
501 void
502 ivi_layout_deactivate(struct ivi_compositor *ivi, const char *app_id)
503 {
504         struct ivi_surface *surf;
505         struct ivi_output *ivi_output;
506
507         surf = ivi_find_app(ivi, app_id);
508         if (!surf)
509                 return;
510
511         ivi_output = ivi_layout_get_output_from_surface(surf);
512         weston_log("deactiving %s\n", app_id);
513
514         if (surf->role == IVI_SURFACE_ROLE_DESKTOP) {
515                 struct ivi_surface *previous_active;
516
517                 previous_active = ivi_output->previous_active;
518                 if (!previous_active) {
519                         /* we don't have a previous active it means we should
520                          * display the bg */
521                         if (ivi_output->active) {
522                                 struct weston_view *view;
523
524                                 view = ivi_output->active->view;
525                                 view->is_mapped = false;
526                                 view->surface->is_mapped = false;
527
528                                 weston_layer_entry_remove(&view->layer_link);
529                                 weston_output_damage(ivi_output->output);
530                         }
531                 } else {
532                         struct weston_desktop_surface *dsurface;
533                         const char *previous_active_app_id;
534
535                         dsurface = previous_active->dsurface;
536                         previous_active_app_id =
537                                 weston_desktop_surface_get_app_id(dsurface);
538                         ivi_layout_activate(ivi_output, previous_active_app_id);
539                 }
540         } else if (surf->role == IVI_SURFACE_ROLE_POPUP) {
541                 struct weston_view *view  = surf->view;
542
543                 weston_layer_entry_remove(&view->layer_link);
544                 weston_view_damage_below(view);
545         }
546 }